If there is a cache directory, save there all compiled code.
[artemus.git] / Art5.pm
blob22e3df81084df772f987de09659076595a379cd9
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;
31 use Carp;
33 $Art5::VERSION = '5.0.0-dev';
35 sub parse {
36 my $self = shift;
37 my $seq = shift;
38 my @ret = ();
40 # delete leading blanks and a possible brace
41 $$seq =~ s/^\s*\{?\s*//;
43 while ($$seq) {
44 # delete comments
45 if ($$seq =~ s/^#.*$//gm) {
46 $$seq =~ s/^\s+//;
48 elsif ($$seq =~ s/^(@?)"(([^"\\]*(\\.[^"\\]*)*))"\s*//) {
49 # double quoted string
50 my $op = $1 || '"';
51 my $str = $2;
53 # replace usual escaped characters
54 $str =~ s/\\n/\n/g;
55 $str =~ s/\\r/\r/g;
56 $str =~ s/\\t/\t/g;
57 $str =~ s/\\"/\"/g;
58 $str =~ s/\\\\/\\/g;
60 push(@ret, [ $op, $str ]);
62 elsif ($$seq =~ s/^(@?)'(([^'\\]*(\\.[^'\\]*)*))'\s*//) {
63 # single quoted string
64 my $op = $1 || '"';
65 my $str = $2;
67 $str =~ s/\\'/\'/g;
68 $str =~ s/\\\\/\\/g;
70 push(@ret, [ $op, $str ]);
72 elsif ($$seq =~ s/^(\d+(\.\d+)?)\s*//) {
73 # number
74 push(@ret, [ '"', $1 ]);
76 elsif ($$seq =~ /^\{\s*/) {
77 # another code sequence
78 push(@ret, $self->parse($seq));
80 elsif ($$seq =~ s/^\}\s*//) {
81 # end of sequence
82 last;
84 elsif ($$seq =~ s/^%([^\s\{\}]+)\s*//) {
85 # external hash value
86 push(@ret, [ '%', $1 ]);
88 elsif ($$seq =~ s/^\$(\d+)\s*//) {
89 # argument
90 push(@ret, [ '$', $1 ]);
92 elsif ($$seq =~ s/^([^\s\{\}]+)\s*//) {
93 # opcode
95 # nothing yet? operator call
96 if (scalar(@ret) == 0) {
97 push(@ret, $1);
99 else {
100 push(@ret, [ $1 ]);
103 else {
104 croak "Artemus5 syntax error near '$$seq'";
108 # no program? return a NOP */
109 if (!@ret) {
110 return [ '"', '' ];
113 # is the first thing in the sequence an array
114 # (instruction) and not a string (opcode)?
115 if (ref($ret[0]) eq 'ARRAY') {
116 # only one instruction? return as is
117 if (scalar(@ret) == 1) {
118 return $ret[0];
121 # otherwise, prepend a '?' (joiner)
122 unshift(@ret, '?');
125 return [ @ret ];
129 sub compile {
130 my $self = shift;
131 my $str = shift;
133 # was this code already compiled?
134 if (exists($self->{pc}->{$str})) {
135 return $self->{pc}->{$str};
138 # joiner opcode
139 my @ret = ( '?' );
141 # split by the Artemus5 marks
142 my @stream = split(/(<\{|\}>)/, $str);
144 # alternate between literal strings and Artemus5 code
145 while (@stream) {
146 my $p = shift(@stream);
148 if ($p eq '<{') {
149 $p = '{' . shift(@stream) . '}';
150 push(@ret, $self->parse(\$p));
151 shift(@stream);
153 elsif ($p) {
154 push(@ret, [ '"', $p ]);
158 my $ret = [ @ret ];
160 return $self->{pc}->{$str} = $ret;
164 sub code {
165 my $self = shift;
166 my $op = shift;
168 if (!exists($self->{op}->{$op})) {
169 my $src = undef;
171 # filter opcode to only allow
172 # characters valid in file names
173 $op =~ s/[^\w\d_-]//g;
175 # does a loader_func() exist?
176 if (ref($self->{loader_func}) eq 'CODE') {
177 $src = $self->{loader_func}->($op);
180 if (!defined($src)) {
181 # try to resolve by loading
182 # a source file from the path
183 foreach my $p (@{$self->{path}}) {
184 if (open(F, $p . '/' . $op)) {
185 $src = join('', <F>);
186 close F;
188 last;
193 # compile if available
194 if (defined($src)) {
195 $self->{op}->{$op} = $self->compile($src);
197 # if there is a cache directory, save the compiled code
198 if ($self->{cache} and open(F, '>' . $self->{cache} . $op)) {
199 use Data::Dumper;
201 print F Dumper($self->{op}->{$op});
202 close F;
207 return $self->{op}->{$op};
211 sub exec {
212 my $self = shift;
213 my $prg = shift;
214 my $ret;
216 # aborted or empty? do nothing more
217 if (!ref($prg) || $self->{abort}) {
218 return '';
221 # stream of Artemus5 code
222 my @stream = @{$prg};
224 # pick opcode
225 my $op = shift(@stream);
227 # pick code
228 my $c = $self->code($op);
230 if (ref($c) eq 'CODE') {
231 $ret = $c->(@stream);
233 elsif (ref($c) eq 'ARRAY') {
234 # push the arguments to the stack
235 push(@{$self->{stack}},
236 [ map { $self->exec($_); }
237 @stream ]);
239 $ret = $self->exec($c);
241 # drop stack
242 pop(@{$self->{stack}});
244 else {
245 croak "Artemus5 opcode not found: $op";
248 if (!defined($ret)) {
249 $ret = '';
252 return $ret;
256 sub exec0 {
257 my $self = shift;
259 return $self->exec(@_) || 0;
263 sub init {
264 my $self = shift;
266 $self->{stack} = [ [] ];
268 $self->{op}->{VERSION} = [ '"', $Art5::VERSION ];
270 $self->{op}->{VERSION_STR} = [
271 '?', 'Artemus ', [ 'VERSION' ]
274 # literal
275 $self->{op}->{'"'} = sub {
276 return $_[0];
279 # translateable literal
280 $self->{op}->{'@'} = sub {
281 return $self->{t}->{$_[0]} || $_[0];
284 # argument
285 $self->{op}->{'$'} = sub {
286 return $self->{stack}->[-1]->[$_[0]];
289 # external hash (e.g. CGI variables)
290 $self->{op}->{'%'} = sub {
291 return $self->{xh}->{$_[0]};
294 # joiner
295 $self->{op}->{'?'} = sub {
296 if (scalar(@_) == 1) {
297 return $self->exec($_[0]);
300 return join('', map { $self->exec($_); } @_);
303 # array
304 $self->{op}->{'&'} = sub {
305 return [ map { $self->exec($_); } @_ ];
308 # assignation
309 $self->{op}->{'='} = sub {
310 $self->{op}->{$self->exec($_[0])} =
311 [ '"', $self->exec($_[1]) ];
313 return '';
316 $self->{op}->{eq} = sub {
317 $self->exec($_[0]) eq
318 $self->exec($_[1]) ? 1 : 0;
320 $self->{op}->{ne} = sub {
321 $self->exec($_[0]) ne
322 $self->exec($_[1]) ? 1 : 0;
325 $self->{op}->{and} = sub {
326 $self->exec($_[0]) && $self->exec($_[1]);
328 $self->{op}->{or} = sub {
329 $self->exec($_[0]) || $self->exec($_[1]);
331 $self->{op}->{not} = sub {
332 $self->exec($_[0]) ? 0 : 1;
335 $self->{op}->{if} = sub {
336 my $ret = '';
338 if ($self->exec($_[0])) {
339 $ret = $self->exec($_[1]);
341 elsif (scalar(@_) == 3) {
342 $ret = $self->exec($_[2]);
345 $ret;
348 $self->{op}->{add} = sub {
349 return $self->exec0($_[0]) + $self->exec0($_[1]);
351 $self->{op}->{sub} = sub {
352 return $self->exec0($_[0]) - $self->exec0($_[1]);
354 $self->{op}->{mul} = sub {
355 return $self->exec0($_[0]) * $self->exec0($_[1]);
357 $self->{op}->{div} = sub {
358 return $self->exec0($_[0]) / $self->exec0($_[1]);
361 $self->{op}->{gt} = sub {
362 return $self->exec0($_[0]) > $self->exec0($_[1]);
364 $self->{op}->{lt} = sub {
365 return $self->exec0($_[0]) < $self->exec0($_[1]);
367 $self->{op}->{random} = sub {
368 return $self->exec($_[rand(scalar(@_))]);
371 $self->{op}->{env} = sub {
372 # no arguments? return keys as an arrayref
373 if (scalar(@_) == 0) {
374 return [ keys(%ENV) ];
377 return $ENV{$self->exec($_[0])};
380 $self->{op}->{foreach} = sub {
381 my $list = shift;
382 my $code = shift || [ '$', 0 ];
383 my $sep = shift || [ '"', '' ];
384 my $header = shift || [ '"', '' ];
386 my @ret = ();
387 my $ph = '';
389 foreach my $e (@{$self->exec($list)}) {
390 # create a stack for the elements
391 # and store the element in the stack
392 push(@{$self->{stack}}, ref($e) ? $e : [ $e ]);
394 # execute the header code
395 my $o = $self->exec($header);
397 # if it's different from previous header,
398 # strip from output; otherwise, remember
399 # for next time
400 if ($ph eq $o) {
401 $o = '';
403 else {
404 $ph = $o;
407 # execute the body code
408 $o .= $self->exec($code);
410 push(@ret, $o);
412 # destroy last stack
413 pop(@{$self->{stack}});
416 return join($self->exec($sep), @ret);
419 $self->{op}->{case} = sub {
420 my $value = $self->exec(shift);
421 my $oth;
423 # if args are odd, the last one is
424 # the 'otherwise' case
425 if (scalar(@_) % 2) {
426 $oth = pop(@_);
429 # now treat the rest of arguments as
430 # pairs of case / result
431 while (@_) {
432 my $case = $self->exec(shift);
433 my $res = shift;
435 if ($value eq $case) {
436 return $self->exec($res);
440 return defined($oth) ? $self->exec($oth) : '';
443 $self->{op}->{seq} = sub {
444 my $from = $self->exec0(shift);
445 my $to = $self->exec0(shift);
447 return [ $from .. $to ];
450 $self->{op}->{sort} = sub {
451 my $list = $self->exec(shift);
452 my $code = shift || [ '$', 0 ];
454 # create a stack for the elements
455 push(@{$self->{stack}}, []);
457 my $ret = [ sort {
458 $self->{stack}->[-1] = ref($a) ? $a : [ $a ];
459 my $va = $self->exec($code);
461 $self->{stack}->[-1] = ref($b) ? $b : [ $b ];
462 my $vb = $self->exec($code);
464 $va cmp $vb;
465 } @{$list} ];
467 # destroy last stack
468 pop(@{$self->{stack}});
470 return $ret;
473 $self->{op}->{reverse} = sub {
474 return [ reverse @{$self->exec(shift)} ];
477 $self->{op}->{size} = sub { return scalar @{$self->exec($_[0])} };
479 $self->{xh}->{arch} = 'Unix';
481 return $self;
485 sub process {
486 my $self = shift;
487 my $src = shift;
489 my $c = $self->compile($src);
491 return $self->exec($c, @_);
495 sub new {
496 my $class = shift;
498 my $self = bless { @_ }, $class;
500 $self->{path} ||= [];
502 return $self->init();
506 __END__