Many more tests.
[artemus.git] / Art5.pm
blobf9c80e99461b80ae6eec89780a7ff60c5aeed540
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.
21 # http://triptico.com
23 #####################################################################
25 use locale;
27 package Art5;
29 use strict;
30 use warnings;
32 $Art5::VERSION = '5.0.0-dev';
34 sub parse {
35 my $self = shift;
36 my $seq = shift;
37 my @ret = ();
39 # delete leading blanks and a possible brace
40 $$seq =~ s/^\s*\{?\s*//;
42 while ($$seq) {
43 # delete comments
44 if ($$seq =~ s/^#.*$//gm) {
45 $$seq =~ s/^\s+//;
47 elsif ($$seq =~ s/^(@?)"(([^"\\]*(\\.[^"\\]*)*))"\s*//) {
48 # double quoted string
49 my $op = $1 || '"';
50 my $str = $2;
52 # replace usual escaped characters
53 $str =~ s/\\n/\n/g;
54 $str =~ s/\\r/\r/g;
55 $str =~ s/\\t/\t/g;
56 $str =~ s/\\"/\"/g;
57 $str =~ s/\\\\/\\/g;
59 push(@ret, [ $op, $str ]);
61 elsif ($$seq =~ s/^(@?)'(([^'\\]*(\\.[^'\\]*)*))'\s*//) {
62 # single quoted string
63 my $op = $1 || '"';
64 my $str = $2;
66 $str =~ s/\\'/\'/g;
67 $str =~ s/\\\\/\\/g;
69 push(@ret, [ $op, $str ]);
71 elsif ($$seq =~ s/^(\d+(\.\d+)?)\s*//) {
72 # number
73 push(@ret, [ '"', $1 ]);
75 elsif ($$seq =~ /^\{\s*/) {
76 # another code sequence
77 push(@ret, $self->parse($seq));
79 elsif ($$seq =~ s/^\}\s*//) {
80 # end of sequence
81 last;
83 elsif ($$seq =~ s/^%([^\s\{\}]+)\s*//) {
84 # external hash value
85 push(@ret, [ '%', $1 ]);
87 elsif ($$seq =~ s/^\$(\d+)\s*//) {
88 # argument
89 push(@ret, [ '$', $1 ]);
91 elsif ($$seq =~ s/^([^\s\{\}]+)\s*//) {
92 # opcode
94 # nothing yet? operator call
95 if (scalar(@ret) == 0) {
96 push(@ret, $1);
98 else {
99 push(@ret, [ $1 ]);
102 else {
103 die "Syntax error near '$$seq'";
107 # no program? return a NOP */
108 if (!@ret) {
109 return [ '"', '' ];
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) {
117 return $ret[0];
120 # otherwise, prepend a '?' (joiner)
121 unshift(@ret, '?');
124 return [ @ret ];
128 sub compile {
129 my $self = shift;
130 my $str = shift;
132 # was this code already compiled?
133 if (exists($self->{pc}->{$str})) {
134 return $self->{pc}->{$str};
137 # joiner opcode
138 my @ret = ( '?' );
140 # split by the Artemus5 marks
141 my @stream = split(/(<\{|\}>)/, $str);
143 # alternate between literal strings and Artemus5 code
144 while (@stream) {
145 my $p = shift(@stream);
147 if ($p eq '<{') {
148 $p = '{' . shift(@stream) . '}';
149 push(@ret, $self->parse(\$p));
150 shift(@stream);
152 elsif ($p) {
153 push(@ret, [ '"', $p ]);
157 my $ret = [ @ret ];
159 return $self->{pc}->{$str} = $ret;
163 sub code {
164 my $self = shift;
165 my $op = shift;
167 if (!exists($self->{op}->{$op})) {
168 my $src = undef;
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>);
185 close F;
187 last;
192 # compile if available
193 if (defined($src)) {
194 $self->{op}->{$op} = $self->compile($src);
198 return $self->{op}->{$op};
202 sub exec {
203 my $self = shift;
204 my $prg = shift;
205 my $ret;
207 # aborted or empty? do nothing more
208 if (!ref($prg) || $self->{abort}) {
209 return '';
212 # stream of Artemus5 code
213 my @stream = @{$prg};
215 # pick opcode
216 my $op = shift(@stream);
218 # pick code
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($_); }
228 @stream ]);
230 $ret = $self->exec($c);
232 # drop stack
233 pop(@{$self->{stack}});
235 else {
236 die "Opcode not found: $op";
239 if (!defined($ret)) {
240 $ret = '';
243 return $ret;
247 sub exec0 {
248 my $self = shift;
250 return $self->exec(@_) || 0;
254 sub init {
255 my $self = shift;
257 $self->{stack} = [ [] ];
259 $self->{op}->{VERSION} = [ '"', $Art5::VERSION ];
261 $self->{op}->{VERSION_STR} = [
262 '?', 'Artemus ', [ 'VERSION' ]
265 # literal
266 $self->{op}->{'"'} = sub {
267 return $_[0];
270 # translateable literal
271 $self->{op}->{'@'} = sub {
272 return $self->{t}->{$_[0]} || $_[0];
275 # argument
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]};
285 # joiner
286 $self->{op}->{'?'} = sub {
287 return join('', map { $self->exec($_); } @_);
290 # assignation
291 $self->{op}->{'='} = sub {
292 $self->{op}->{$self->exec($_[0])} =
293 [ '"', $self->exec($_[1]) ];
295 return '';
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 {
318 my $ret = '';
320 if ($self->exec($_[0])) {
321 $ret = $self->exec($_[1]);
323 elsif (scalar(@_) == 3) {
324 $ret = $self->exec($_[2]);
327 $ret;
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 {
363 my $list = shift;
364 my $code = shift || [ '$', 0 ];
365 my $sep = shift || [ '"', '' ];
366 my $header = shift || [ '"', '' ];
368 my @ret = ();
369 my $ph = '';
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
384 # for next time
385 if ($ph eq $o) {
386 $o = '';
388 else {
389 $ph = $o;
392 # execute the body code
393 $o .= $self->exec($code);
395 push(@ret, $o);
398 # destroy last stack
399 pop(@{$self->{stack}});
401 return join($self->exec($sep), @ret);
404 $self->{op}->{case} = sub {
405 my $value = $self->exec(shift);
406 my $oth;
408 # if args are odd, the last one is
409 # the 'otherwise' case
410 if (scalar(@_) % 2) {
411 $oth = pop(@_);
414 # now treat the rest of arguments as
415 # pairs of case / result
416 while (@_) {
417 my $case = $self->exec(shift);
418 my $res = 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}}, []);
442 my $ret = [ sort {
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);
449 $va cmp $vb;
450 } @{$list} ];
452 # destroy last stack
453 pop(@{$self->{stack}});
455 return $ret;
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';
466 return $self;
470 sub process {
471 my $self = shift;
472 my $src = shift;
474 my $c = $self->compile($src);
476 return $self->exec($c, @_);
480 sub new {
481 my $class = shift;
483 my $self = bless { @_ }, $class;
485 $self->{path} ||= [];
487 return $self->init();
491 __END__