1 #####################################################################
3 # Artemus - Template Toolkit version 5
5 # Copyright (C) 2000/2009 Angel Ortega <angel@triptico.com>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #####################################################################
32 $Art5::VERSION
= '5.0.0-dev';
39 # delete leading blanks and a possible brace
40 $$seq =~ s/^\s*\{?\s*//;
44 if ($$seq =~ s/^#.*$//gm) {
47 elsif ($$seq =~ s/^(@?)"(([^"\\]*(\\.[^"\\]*)*))"\s*//) {
48 # double quoted string
52 # replace usual escaped characters
59 push(@ret, [ $op, $str ]);
61 elsif ($$seq =~ s/^(@?)'(([^'\\]*(\\.[^'\\]*)*))'\s*//) {
62 # single quoted string
69 push(@ret, [ $op, $str ]);
71 elsif ($$seq =~ s/^(\d+(\.\d+)?)\s*//) {
73 push(@ret, [ '"', $1 ]);
75 elsif ($$seq =~ /^\{\s*/) {
76 # another code sequence
77 push(@ret, $self->parse($seq));
79 elsif ($$seq =~ s/^\}\s*//) {
83 elsif ($$seq =~ s/^%([^\s\{\}]+)\s*//) {
85 push(@ret, [ '%', $1 ]);
87 elsif ($$seq =~ s/^\$(\d+)\s*//) {
89 push(@ret, [ '$', $1 ]);
91 elsif ($$seq =~ s/^([^\s\{\}]+)\s*//) {
94 # nothing yet? operator call
95 if (scalar(@ret) == 0) {
103 die "Syntax error near '$$seq'";
107 # no program? return a NOP */
112 # is the first thing in the sequence an array
113 # (instruction) and not a string (opcode)?
114 if (ref($ret[0]) eq 'ARRAY') {
115 # only one instruction? return as is
116 if (scalar(@ret) == 1) {
120 # otherwise, prepend a '?' (joiner)
132 # was this code already compiled?
133 if (exists($self->{pc
}->{$str})) {
134 return $self->{pc
}->{$str};
140 # split by the Artemus5 marks
141 my @stream = split(/(<\{|\}>)/, $str);
143 # alternate between literal strings and Artemus5 code
145 my $p = shift(@stream);
148 $p = '{' . shift(@stream) . '}';
149 push(@ret, $self->parse(\
$p));
153 push(@ret, [ '"', $p ]);
159 return $self->{pc
}->{$str} = $ret;
167 if (!exists($self->{op
}->{$op})) {
170 # filter opcode to only allow
171 # characters valid in file names
172 $op =~ s/[^\w\d_-]//g;
174 # does a loader_func() exist?
175 if (ref($self->{loader_func
}) eq 'CODE') {
176 $src = $self->{loader_func
}->($op);
179 if (!defined($src)) {
180 # try to resolve by loading
181 # a source file from the path
182 foreach my $p (@
{$self->{path
}}) {
183 if (open(F
, $p . '/' . $op)) {
184 $src = join('', <F
>);
192 # compile if available
194 $self->{op
}->{$op} = $self->compile($src);
198 return $self->{op
}->{$op};
207 # aborted or empty? do nothing more
208 if (!ref($prg) || $self->{abort
}) {
212 # stream of Artemus5 code
213 my @stream = @
{$prg};
216 my $op = shift(@stream);
219 my $c = $self->code($op);
221 if (ref($c) eq 'CODE') {
222 $ret = $c->(@stream);
224 elsif (ref($c) eq 'ARRAY') {
225 # push the arguments to the stack
226 push(@
{$self->{stack
}},
227 [ map { $self->exec($_); }
230 $ret = $self->exec($c);
233 pop(@
{$self->{stack
}});
236 die "Opcode not found: $op";
239 if (!defined($ret)) {
250 return $self->exec(@_) || 0;
257 $self->{stack
} = [ [] ];
259 $self->{op
}->{VERSION
} = [ '"', $Art5::VERSION
];
261 $self->{op
}->{VERSION_STR
} = [
262 '?', 'Artemus ', [ 'VERSION' ]
266 $self->{op
}->{'"'} = sub {
270 # translateable literal
271 $self->{op
}->{'@'} = sub {
272 return $self->{t
}->{$_[0]} || $_[0];
276 $self->{op
}->{'$'} = sub {
277 return $self->{stack
}->[-1]->[$_[0]];
280 # external hash (e.g. CGI variables)
281 $self->{op
}->{'%'} = sub {
282 return $self->{xh
}->{$_[0]};
286 $self->{op
}->{'?'} = sub {
287 return join('', map { $self->exec($_); } @_);
291 $self->{op
}->{'='} = sub {
292 $self->{op
}->{$self->exec($_[0])} =
293 [ '"', $self->exec($_[1]) ];
298 $self->{op
}->{eq} = sub {
299 $self->exec($_[0]) eq
300 $self->exec($_[1]) ?
1 : 0;
302 $self->{op
}->{ne} = sub {
303 $self->exec($_[0]) ne
304 $self->exec($_[1]) ?
1 : 0;
307 $self->{op
}->{and} = sub {
308 $self->exec($_[0]) && $self->exec($_[1]);
310 $self->{op
}->{or} = sub {
311 $self->exec($_[0]) || $self->exec($_[1]);
313 $self->{op
}->{not} = sub {
314 $self->exec($_[0]) ?
0 : 1;
317 $self->{op
}->{if} = sub {
320 if ($self->exec($_[0])) {
321 $ret = $self->exec($_[1]);
323 elsif (scalar(@_) == 3) {
324 $ret = $self->exec($_[2]);
330 $self->{op
}->{add
} = sub {
331 return $self->exec0($_[0]) + $self->exec0($_[1]);
333 $self->{op
}->{sub} = sub {
334 return $self->exec0($_[0]) - $self->exec0($_[1]);
336 $self->{op
}->{mul
} = sub {
337 return $self->exec0($_[0]) * $self->exec0($_[1]);
339 $self->{op
}->{div
} = sub {
340 return $self->exec0($_[0]) / $self->exec0($_[1]);
343 $self->{op
}->{gt} = sub {
344 return $self->exec0($_[0]) > $self->exec0($_[1]);
346 $self->{op
}->{lt} = sub {
347 return $self->exec0($_[0]) < $self->exec0($_[1]);
349 $self->{op
}->{random
} = sub {
350 return $self->exec($_[rand(scalar(@_))]);
353 $self->{op
}->{env
} = sub {
354 # no arguments? return keys as an arrayref
355 if (scalar(@_) == 0) {
356 return [ keys(%ENV) ];
359 return $ENV{$self->exec($_[0])};
362 $self->{op
}->{foreach} = sub {
364 my $code = shift || [ '$', 0 ];
365 my $sep = shift || [ '"', '' ];
366 my $header = shift || [ '"', '' ];
371 # create a stack for the elements
372 push(@
{$self->{stack
}}, []);
374 foreach my $e (@
{$self->exec($list)}) {
375 # store the element in the stack
376 $self->{stack
}->[-1] =
377 ref($e) ?
$e : [ $e ];
379 # execute the header code
380 my $o = $self->exec($header);
382 # if it's different from previous header,
383 # strip from output; otherwise, remember
392 # execute the body code
393 $o .= $self->exec($code);
399 pop(@
{$self->{stack
}});
401 return join($self->exec($sep), @ret);
404 $self->{op
}->{case
} = sub {
405 my $value = $self->exec(shift);
408 # if args are odd, the last one is
409 # the 'otherwise' case
410 if (scalar(@_) % 2) {
414 # now treat the rest of arguments as
415 # pairs of case / result
417 my $case = $self->exec(shift);
420 if ($value eq $case) {
421 return $self->exec($res);
425 return defined($oth) ?
$self->exec($oth) : '';
428 $self->{op
}->{seq
} = sub {
429 my $from = $self->exec0(shift);
430 my $to = $self->exec0(shift);
432 return [ $from .. $to ];
435 $self->{op
}->{sort} = sub {
436 my $list = $self->exec(shift);
437 my $code = shift || [ '$', 0 ];
439 # create a stack for the elements
440 push(@
{$self->{stack
}}, []);
443 $self->{stack
}->[-1] = ref($a) ?
$a : [ $a ];
444 my $va = $self->exec($code);
446 $self->{stack
}->[-1] = ref($b) ?
$b : [ $b ];
447 my $vb = $self->exec($code);
453 pop(@
{$self->{stack
}});
458 $self->{op
}->{reverse} = sub {
459 return [ reverse @
{$self->exec(shift)} ];
462 $self->{op
}->{size
} = sub { return scalar @
{$self->exec($_[0])} };
464 $self->{xh
}->{arch
} = 'Unix';
474 my $c = $self->compile($src);
476 return $self->exec($c, @_);
483 my $self = bless { @_ }, $class;
485 $self->{path
} ||= [];
487 return $self->init();