Skip to content

Commit f7e0236

Browse files
author
Chris White
committed
L1: added on{}run{}, $NOW
1 parent eb8c530 commit f7e0236

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

lib/XML/Axk/Base.pm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ use constant {
2626
# When to run an action --- pre, post, or both (CIAO).
2727
HI => 2,
2828
BYE => 1,
29-
CIAO => 0,
29+
CIAO => 0, # the only falsy one
3030
};
3131

3232
our @EXPORT = qw(true false HI BYE CIAO);
33-
our @EXPORT_OK = qw(any SCRIPT_PKG_PREFIX);
33+
our @EXPORT_OK = qw(any SCRIPT_PKG_PREFIX now_names);
3434
our %EXPORT_TAGS = (
3535
default => [@EXPORT],
3636
all => [@EXPORT, @EXPORT_OK]
@@ -79,5 +79,11 @@ sub any (&@)
7979
return 0;
8080
}
8181

82+
# Names of NOW constants, for debugging
83+
sub now_names {
84+
my %names=(HI,"entering", BYE,"leaving", CIAO,"both");
85+
return $names{+shift} || "unknown";
86+
} # now_names
87+
8288
1;
8389
# vi: set ts=4 sts=4 sw=4 et ai fo-=ro foldmethod=marker ft=perl: #

lib/XML/Axk/Core.pm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ sub _run_worklist {
260260
my $self = shift;
261261
my $now = shift; # $now = HI, BYE, or CIAO
262262

263-
my %CPs = (@_); # Core parameters
263+
my %CPs = ( # Core parameters
264+
NOW => $now,
265+
@_
266+
);
264267

265268
# Assign the SPs from the CPs --
266269

@@ -275,13 +278,13 @@ sub _run_worklist {
275278
my ($refPattern, $refAction, $when) = @$lrItem;
276279
#say "At time $now: running ", Dumper($lrItem);
277280

278-
next if $when && ($now != $when);
281+
next if $when && ($now != $when); # CIAO is the only falsy one
279282

280283
next unless $refPattern->test(\%CPs);
281284
# Matchers use CPs so they are independent of language.
282285

283286
eval { &$refAction }; # which context are they evaluated in?
284-
croak "action: $@" if $@;
287+
die "action: $@" if $@;
285288
} #foreach worklist item
286289
} #_run_worklist
287290

lib/XML/Axk/L1.pm

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# `axk_script_*` is used by only one Core instance.
1111

1212
package XML::Axk::L1;
13-
use XML::Axk::Base;
13+
use XML::Axk::Base qw(:default now_names);
1414

1515
use XML::Axk::Matcher::XPath;
1616
use XML::Axk::Matcher::Always;
@@ -23,7 +23,7 @@ require XML::Axk::Language;
2323
require Exporter;
2424
our @EXPORT = qw(
2525
pre_all pre_file post_file post_all perform
26-
always never xpath sel);
26+
always never xpath sel on run);
2727
our @EXPORT_OK = qw( @SP_names );
2828

2929
# Helpers ======================================================== {{{1
@@ -69,8 +69,8 @@ sub post_all :prototype(&) {
6969
## since that's how Perl's prototypes are set up the cleanest (block first).
7070
## @params required &action A block to execute when the pattern matches
7171
## @params required pattern The pattern
72-
sub perform :prototype(&@) {
73-
#say Dumper(\@_);
72+
sub add_to_worklist {
73+
#say "add_to_worklist args: ", Dumper(\@_);
7474
my ($drAction, $refPattern, $when) = @_;
7575
#say "perform(): ", Dumper(\@_);
7676
$when = $when // HI; # only on entry, by default
@@ -83,6 +83,28 @@ sub perform :prototype(&@) {
8383
push @{$sandbox->worklist}, [$refPattern, $drAction, $when];
8484
} #perform()
8585

86+
# User-facing alias for add_to_worklist
87+
sub perform :prototype(&@) {
88+
goto &add_to_worklist; # Need goto so that _sandbox() can use caller(1)
89+
}
90+
91+
# run { action } [optional <when>] - syntactic sugar for sub {}, when
92+
sub run :prototype(&;$) {
93+
return @_;
94+
} #run()
95+
96+
# pattern-first style - on {} run {} [when];
97+
sub on :prototype(&@) {
98+
my ($drMakeMatcher, $drAction, $when) = @_;
99+
100+
#say "MakeMatcher: ", Dumper($drMakeMatcher);
101+
my $matcher = &$drMakeMatcher;
102+
#say "Matcher: ", Dumper($matcher);
103+
104+
@_=($drAction, $matcher, $when);
105+
goto &add_to_worklist;
106+
} # on()
107+
86108
# }}}1
87109
# Definers for matchers ========================================== {{{1
88110

@@ -101,7 +123,7 @@ sub never :prototype() {
101123
} #never()
102124

103125
# Make an XPath matcher
104-
sub xpath :prototype(@) {
126+
sub xpath {
105127
my $path = shift or croak("No expression provided!");
106128
$path = $$path if ref $path;
107129

@@ -114,7 +136,7 @@ sub xpath :prototype(@) {
114136
} #xpath()
115137

116138
# Make a selector matcher
117-
sub sel :prototype(@) {
139+
sub sel {
118140
my $path = shift or croak("No expression provided!");
119141
$path = $$path if ref $path;
120142

@@ -132,7 +154,7 @@ sub sel :prototype(@) {
132154
# Script parameters ============================================== {{{1
133155

134156
# Script-parameter names
135-
our @SP_names = qw($C @F $D $E);
157+
our @SP_names = qw($C @F $D $E $NOW);
136158

137159
sub update {
138160
#say "L1::update: ", Dumper(\@_);
@@ -141,6 +163,8 @@ sub update {
141163

142164
$hrSP->{'$D'} = $opts{document} or croak("No document");
143165
$hrSP->{'$E'} = $opts{record} or croak("No record");
166+
croak("You are in a timeless maze") unless defined $opts{NOW};
167+
$hrSP->{'$NOW'} = now_names $opts{NOW};
144168
#while (my ($key, $value) = each %new_sps) { }
145169
} #update()
146170

0 commit comments

Comments
 (0)