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 $Artemus5::VERSION
= '5.0.0-dev';
39 # delete leading blanks and a possible brace
40 $$seq =~ s/^\s*\{?\s*//;
44 while ($$seq =~ s/^\s*#[^\n]+\n\s*//) {
47 if ($$seq =~ s/^"(([^"\\]*(\\.[^"\\]*)*))"\s*//) {
48 # double quoted string
51 # replace usual escaped characters
58 push(@ret, [ '"', $str ]);
60 elsif ($$seq =~ s/^'(([^'\\]*(\\.[^'\\]*)*))'\s*//) {
61 # single quoted string
67 push(@ret, [ '"', $str ]);
69 elsif ($$seq =~ s/^(\d+(\.\d+)?)\s*//) {
71 push(@ret, [ '"', $1 ]);
73 elsif ($$seq =~ /^\{\s*/) {
74 # another code sequence
75 push(@ret, $self->compile_c($seq));
77 elsif ($$seq =~ s/^\}\s*//) {
81 elsif ($$seq =~ s/^%([^\s\{\}]+)\s*//) {
83 push(@ret, [ '%', $1 ]);
85 elsif ($$seq =~ s/^\$(\d+)\s*//) {
87 push(@ret, [ '$', $1 ]);
89 elsif ($$seq =~ s/^([^\s\{\}]+)\s*//) {
92 # nothing yet? operator call
93 if (scalar(@ret) == 0) {
101 die "Syntax error near '$$seq'";
105 # is the first thing in the sequence an array
106 # (instruction) and not a string (opcode)?
107 if (ref($ret[0]) eq 'ARRAY') {
108 # only one instruction? return as is
109 if (scalar(@ret) == 1) {
113 # otherwise, prepend a '?' (joiner)
125 # was this code already compiled?
126 if (exists($self->{pc
}->{$str})) {
127 return $self->{pc
}->{$str};
133 # split by the Artemus5 marks
134 my @stream = split(/(<\{|\}>)/, $str);
136 # alternate between literal strings and Artemus5 code
138 my $p = shift(@stream);
141 $p = '{' . shift(@stream) . '}';
142 push(@ret, $self->compile_c(\
$p));
146 push(@ret, [ '"', $p ]);
152 return $self->{pc
}->{$str} = $ret;
160 if (!exists($self->{op
}->{$op})) {
163 # does a loader_func() exist?
164 if (ref($self->{loader_func
}) eq 'CODE') {
165 $src = $self->{loader_func
}->{$op};
168 if (!defined($src)) {
169 # try to resolve by loading
170 # a source file from the path
171 foreach my $p (@
{$self->{path
}}) {
172 if (open(F
, $p . '/' . $op)) {
173 $src = join('', <F
>);
181 # compile if available
183 $self->{op
}->{$op} = $self->compile($src);
187 return $self->{op
}->{$op};
196 # aborted? do nothing more
197 if ($self->{abort
}) {
201 # stream of Artemus5 code
202 my @stream = @
{$prg};
205 my $op = shift(@stream);
208 my $c = $self->code($op);
210 if (ref($c) eq 'CODE') {
211 $ret = $c->(@stream);
213 elsif (ref($c) eq 'ARRAY') {
214 # push the arguments to the stack
215 push(@
{$self->{stack
}},
216 [ map { $self->exec($_); }
219 $ret = $self->exec($c);
222 pop(@
{$self->{stack
}});
225 die "Opcode not found: $op";
228 if (!defined($ret)) {
239 return $self->exec(@_) || 0;
246 $self->{stack
} = [ [] ];
248 $self->{op
}->{VERSION
} = [ '"', $Artemus5::VERSION
];
250 $self->{op
}->{VERSION_STR
} = [
251 '?', 'Artemus ', [ 'VERSION' ]
255 $self->{op
}->{'"'} = sub {
260 $self->{op
}->{'$'} = sub {
261 return $self->{stack
}->[-1]->[$_[0]];
264 # external hash (e.g. CGI variables)
265 $self->{op
}->{'%'} = sub {
266 return $self->{xh
}->{$_[0]};
270 $self->{op
}->{'?'} = sub {
271 return join('', map { $self->exec($_); } @_);
275 $self->{op
}->{'='} = sub {
276 $self->{op
}->{$self->exec($_[0])} =
277 [ '"', $self->exec($_[1]) ];
282 $self->{op
}->{eq} = sub {
283 $self->exec($_[0]) eq
284 $self->exec($_[1]) ?
1 : 0;
286 $self->{op
}->{ne} = sub {
287 $self->exec($_[0]) ne
288 $self->exec($_[1]) ?
1 : 0;
291 $self->{op
}->{and} = sub {
292 $self->exec($_[0]) && $self->exec($_[1]);
294 $self->{op
}->{or} = sub {
295 $self->exec($_[0]) || $self->exec($_[1]);
297 $self->{op
}->{not} = sub {
298 $self->exec($_[0]) ?
0 : 1;
301 $self->{op
}->{if} = sub {
304 if ($self->exec($_[0])) {
305 $ret = $self->exec($_[1]);
307 elsif (scalar(@_) == 3) {
308 $ret = $self->exec($_[2]);
314 $self->{op
}->{add
} = sub {
315 return $self->exec0($_[0]) + $self->exec0($_[1]);
317 $self->{op
}->{sub} = sub {
318 return $self->exec0($_[0]) - $self->exec0($_[1]);
320 $self->{op
}->{mul
} = sub {
321 return $self->exec0($_[0]) * $self->exec0($_[1]);
323 $self->{op
}->{div
} = sub {
324 return $self->exec0($_[0]) / $self->exec0($_[1]);
327 $self->{op
}->{gt} = sub {
328 return $self->exec0($_[0]) > $self->exec0($_[1]);
330 $self->{op
}->{lt} = sub {
331 return $self->exec0($_[0]) < $self->exec0($_[1]);
333 $self->{op
}->{random
} = sub {
334 return $self->exec($_[rand(scalar(@_))]);
337 $self->{op
}->{env
} = sub {
338 # no arguments? return keys as an arrayref
339 if (scalar(@_) == 0) {
340 return [ keys(%ENV) ];
343 return $ENV{$self->exec($_[0])};
346 $self->{op
}->{foreach} = sub {
348 my $code = shift || [ '$', 0 ];
349 my $sep = shift || [ '"', '' ];
350 my $header = shift || [ '"', '' ];
355 # create a stack for the elements
356 push(@
{$self->{stack
}}, []);
358 foreach my $e (@
{$self->exec($list)}) {
359 # store the element in the stack
360 $self->{stack
}->[-1] =
361 ref($e) ?
$e : [ $e ];
363 # execute the header code
364 my $o = $self->exec($header);
366 # if it's different from previous header,
367 # strip from output; otherwise, remember
376 # execute the body code
377 $o .= $self->exec($code);
383 pop(@
{$self->{stack
}});
385 return join($self->exec($sep), @ret);
388 $self->{op
}->{case
} = sub {
389 my $value = $self->exec(shift);
392 # if args are odd, the last one is
393 # the 'otherwise' case
394 if (scalar(@_) % 2) {
398 # now treat the rest of arguments as
399 # pairs of case / result
401 my $case = $self->exec(shift);
404 if ($value eq $case) {
405 return $self->exec($res);
409 return defined($oth) ?
$self->exec($oth) : '';
412 $self->{op
}->{seq
} = sub {
413 my $from = $self->exec0(shift);
414 my $to = $self->exec0(shift);
416 return [ $from .. $to ];
419 $self->{op
}->{sort} = sub {
420 my $list = $self->exec(shift);
421 my $code = shift || [ '$', 0 ];
423 # create a stack for the elements
424 push(@
{$self->{stack
}}, []);
427 $self->{stack
}->[-1] = ref($a) ?
$a : [ $a ];
428 my $va = $self->exec($code);
430 $self->{stack
}->[-1] = ref($b) ?
$b : [ $b ];
431 my $vb = $self->exec($code);
437 pop(@
{$self->{stack
}});
442 $self->{op
}->{reverse} = sub {
443 return [ reverse @
{$self->exec(shift)} ];
446 $self->{op
}->{size
} = sub { return scalar @
{$self->exec($_[0])} };
448 $self->{xh
}->{arch
} = 'Unix';
458 my $c = $self->compile($src);
460 return $self->exec($c, @_);
467 my $self = bless { @_ }, $class;
469 $self->{path
} ||= [];
471 return $self->init();