Set back 'if' to be similar to Artemus4's and deleted 'iff'.
[artemus.git] / Artemus5.pm
blob13c25a56f73c6ccee53fddbb2f4bcce76a56962f
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 Artemus5;
29 use strict;
30 use warnings;
32 $Artemus5::VERSION = '5.0.0-dev';
34 sub compile_c {
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 while ($$seq =~ s/^\s*#[^\n]+\n\s*//) {
47 if ($$seq =~ s/^"(([^"\\]*(\\.[^"\\]*)*))"\s*//) {
48 # double quoted string
49 my $str = $1;
51 # replace usual escaped characters
52 $str =~ s/\\n/\n/g;
53 $str =~ s/\\r/\r/g;
54 $str =~ s/\\t/\t/g;
55 $str =~ s/\\"/\"/g;
56 $str =~ s/\\\\/\\/g;
58 push(@ret, [ '"', $str ]);
60 elsif ($$seq =~ s/^'(([^'\\]*(\\.[^'\\]*)*))'\s*//) {
61 # single quoted string
62 my $str = $1;
64 $str =~ s/\\'/\'/g;
65 $str =~ s/\\\\/\\/g;
67 push(@ret, [ '"', $str ]);
69 elsif ($$seq =~ s/^(\d+(\.\d+)?)\s*//) {
70 # number
71 push(@ret, [ '"', $1 ]);
73 elsif ($$seq =~ /^\{\s*/) {
74 # another code sequence
75 push(@ret, $self->compile_c($seq));
77 elsif ($$seq =~ s/^\}\s*//) {
78 # end of sequence
79 last;
81 elsif ($$seq =~ s/^%([^\s\{\}]+)\s*//) {
82 # external hash value
83 push(@ret, [ '%', $1 ]);
85 elsif ($$seq =~ s/^\$(\d+)\s*//) {
86 # argument
87 push(@ret, [ '$', $1 ]);
89 elsif ($$seq =~ s/^([^\s\{\}]+)\s*//) {
90 # opcode
92 # nothing yet? operator call
93 if (scalar(@ret) == 0) {
94 push(@ret, $1);
96 else {
97 push(@ret, [ $1 ]);
100 else {
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) {
110 return $ret[0];
113 # otherwise, prepend a '?' (joiner)
114 unshift(@ret, '?');
117 return [ @ret ];
121 sub compile {
122 my $self = shift;
123 my $str = shift;
125 # was this code already compiled?
126 if (exists($self->{pc}->{$str})) {
127 return $self->{pc}->{$str};
130 # joiner opcode
131 my @ret = ( '?' );
133 # split by the Artemus5 marks
134 my @stream = split(/(<\{|\}>)/, $str);
136 # alternate between literal strings and Artemus5 code
137 while (@stream) {
138 my $p = shift(@stream);
140 if ($p eq '<{') {
141 $p = '{' . shift(@stream) . '}';
142 push(@ret, $self->compile_c(\$p));
143 shift(@stream);
145 elsif ($p) {
146 push(@ret, [ '"', $p ]);
150 my $ret = [ @ret ];
152 return $self->{pc}->{$str} = $ret;
156 sub code {
157 my $self = shift;
158 my $op = shift;
160 if (!exists($self->{op}->{$op})) {
161 my $src = undef;
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>);
174 close F;
176 last;
181 # compile if available
182 if (defined($src)) {
183 $self->{op}->{$op} = $self->compile($src);
187 return $self->{op}->{$op};
191 sub exec {
192 my $self = shift;
193 my $prg = shift;
194 my $ret;
196 # aborted? do nothing more
197 if ($self->{abort}) {
198 return '';
201 # stream of Artemus5 code
202 my @stream = @{$prg};
204 # pick opcode
205 my $op = shift(@stream);
207 # pick code
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($_); }
217 @stream ]);
219 $ret = $self->exec($c);
221 # drop stack
222 pop(@{$self->{stack}});
224 else {
225 die "Opcode not found: $op";
228 if (!defined($ret)) {
229 $ret = '';
232 return $ret;
236 sub exec0 {
237 my $self = shift;
239 return $self->exec(@_) || 0;
243 sub init {
244 my $self = shift;
246 $self->{stack} = [ [] ];
248 $self->{op}->{VERSION} = [ '"', $Artemus5::VERSION ];
250 $self->{op}->{VERSION_STR} = [
251 '?', 'Artemus ', [ 'VERSION' ]
254 # literal
255 $self->{op}->{'"'} = sub {
256 return $_[0];
259 # argument
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]};
269 # joiner
270 $self->{op}->{'?'} = sub {
271 return join('', map { $self->exec($_); } @_);
274 # assignation
275 $self->{op}->{'='} = sub {
276 $self->{op}->{$self->exec($_[0])} =
277 [ '"', $self->exec($_[1]) ];
279 return '';
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 {
302 my $ret = '';
304 if ($self->exec($_[0])) {
305 $ret = $self->exec($_[1]);
307 elsif (scalar(@_) == 3) {
308 $ret = $self->exec($_[2]);
311 $ret;
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 {
347 my $list = shift;
348 my $code = shift || [ '$', 0 ];
349 my $sep = shift || [ '"', '' ];
350 my $header = shift || [ '"', '' ];
352 my @ret = ();
353 my $ph = '';
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
368 # for next time
369 if ($ph eq $o) {
370 $o = '';
372 else {
373 $ph = $o;
376 # execute the body code
377 $o .= $self->exec($code);
379 push(@ret, $o);
382 # destroy last stack
383 pop(@{$self->{stack}});
385 return join($self->exec($sep), @ret);
388 $self->{op}->{case} = sub {
389 my $value = $self->exec(shift);
390 my $oth;
392 # if args are odd, the last one is
393 # the 'otherwise' case
394 if (scalar(@_) % 2) {
395 $oth = pop(@_);
398 # now treat the rest of arguments as
399 # pairs of case / result
400 while (@_) {
401 my $case = $self->exec(shift);
402 my $res = 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}}, []);
426 my $ret = [ sort {
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);
433 $va cmp $vb;
434 } @{$list} ];
436 # destroy last stack
437 pop(@{$self->{stack}});
439 return $ret;
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';
450 return $self;
454 sub process {
455 my $self = shift;
456 my $src = shift;
458 my $c = $self->compile($src);
460 return $self->exec($c, @_);
464 sub new {
465 my $class = shift;
467 my $self = bless { @_ }, $class;
469 $self->{path} ||= [];
471 return $self->init();
475 __END__