Minor coding style changes.
[artemus.git] / Art5.pm
blob6c439152b9e6f1b35b938d8a66801670224ae776
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);
199 return $self->{op}->{$op};
203 sub exec {
204 my $self = shift;
205 my $prg = shift;
206 my $ret;
208 # aborted or empty? do nothing more
209 if (!ref($prg) || $self->{abort}) {
210 return '';
213 # stream of Artemus5 code
214 my @stream = @{$prg};
216 # pick opcode
217 my $op = shift(@stream);
219 # pick code
220 my $c = $self->code($op);
222 if (ref($c) eq 'CODE') {
223 $ret = $c->(@stream);
225 elsif (ref($c) eq 'ARRAY') {
226 # push the arguments to the stack
227 push(@{$self->{stack}},
228 [ map { $self->exec($_); }
229 @stream ]);
231 $ret = $self->exec($c);
233 # drop stack
234 pop(@{$self->{stack}});
236 else {
237 croak "Artemus5 opcode not found: $op";
240 if (!defined($ret)) {
241 $ret = '';
244 return $ret;
248 sub exec0 {
249 my $self = shift;
251 return $self->exec(@_) || 0;
255 sub init {
256 my $self = shift;
258 $self->{stack} = [ [] ];
260 $self->{op}->{VERSION} = [ '"', $Art5::VERSION ];
262 $self->{op}->{VERSION_STR} = [
263 '?', 'Artemus ', [ 'VERSION' ]
266 # literal
267 $self->{op}->{'"'} = sub {
268 return $_[0];
271 # translateable literal
272 $self->{op}->{'@'} = sub {
273 return $self->{t}->{$_[0]} || $_[0];
276 # argument
277 $self->{op}->{'$'} = sub {
278 return $self->{stack}->[-1]->[$_[0]];
281 # external hash (e.g. CGI variables)
282 $self->{op}->{'%'} = sub {
283 return $self->{xh}->{$_[0]};
286 # joiner
287 $self->{op}->{'?'} = sub {
288 return join('', map { $self->exec($_); } @_);
291 # assignation
292 $self->{op}->{'='} = sub {
293 $self->{op}->{$self->exec($_[0])} =
294 [ '"', $self->exec($_[1]) ];
296 return '';
299 $self->{op}->{eq} = sub {
300 $self->exec($_[0]) eq
301 $self->exec($_[1]) ? 1 : 0;
303 $self->{op}->{ne} = sub {
304 $self->exec($_[0]) ne
305 $self->exec($_[1]) ? 1 : 0;
308 $self->{op}->{and} = sub {
309 $self->exec($_[0]) && $self->exec($_[1]);
311 $self->{op}->{or} = sub {
312 $self->exec($_[0]) || $self->exec($_[1]);
314 $self->{op}->{not} = sub {
315 $self->exec($_[0]) ? 0 : 1;
318 $self->{op}->{if} = sub {
319 my $ret = '';
321 if ($self->exec($_[0])) {
322 $ret = $self->exec($_[1]);
324 elsif (scalar(@_) == 3) {
325 $ret = $self->exec($_[2]);
328 $ret;
331 $self->{op}->{add} = sub {
332 return $self->exec0($_[0]) + $self->exec0($_[1]);
334 $self->{op}->{sub} = sub {
335 return $self->exec0($_[0]) - $self->exec0($_[1]);
337 $self->{op}->{mul} = sub {
338 return $self->exec0($_[0]) * $self->exec0($_[1]);
340 $self->{op}->{div} = sub {
341 return $self->exec0($_[0]) / $self->exec0($_[1]);
344 $self->{op}->{gt} = sub {
345 return $self->exec0($_[0]) > $self->exec0($_[1]);
347 $self->{op}->{lt} = sub {
348 return $self->exec0($_[0]) < $self->exec0($_[1]);
350 $self->{op}->{random} = sub {
351 return $self->exec($_[rand(scalar(@_))]);
354 $self->{op}->{env} = sub {
355 # no arguments? return keys as an arrayref
356 if (scalar(@_) == 0) {
357 return [ keys(%ENV) ];
360 return $ENV{$self->exec($_[0])};
363 $self->{op}->{foreach} = sub {
364 my $list = shift;
365 my $code = shift || [ '$', 0 ];
366 my $sep = shift || [ '"', '' ];
367 my $header = shift || [ '"', '' ];
369 my @ret = ();
370 my $ph = '';
372 # create a stack for the elements
373 push(@{$self->{stack}}, []);
375 foreach my $e (@{$self->exec($list)}) {
376 # store the element in the stack
377 $self->{stack}->[-1] =
378 ref($e) ? $e : [ $e ];
380 # execute the header code
381 my $o = $self->exec($header);
383 # if it's different from previous header,
384 # strip from output; otherwise, remember
385 # for next time
386 if ($ph eq $o) {
387 $o = '';
389 else {
390 $ph = $o;
393 # execute the body code
394 $o .= $self->exec($code);
396 push(@ret, $o);
399 # destroy last stack
400 pop(@{$self->{stack}});
402 return join($self->exec($sep), @ret);
405 $self->{op}->{case} = sub {
406 my $value = $self->exec(shift);
407 my $oth;
409 # if args are odd, the last one is
410 # the 'otherwise' case
411 if (scalar(@_) % 2) {
412 $oth = pop(@_);
415 # now treat the rest of arguments as
416 # pairs of case / result
417 while (@_) {
418 my $case = $self->exec(shift);
419 my $res = shift;
421 if ($value eq $case) {
422 return $self->exec($res);
426 return defined($oth) ? $self->exec($oth) : '';
429 $self->{op}->{seq} = sub {
430 my $from = $self->exec0(shift);
431 my $to = $self->exec0(shift);
433 return [ $from .. $to ];
436 $self->{op}->{sort} = sub {
437 my $list = $self->exec(shift);
438 my $code = shift || [ '$', 0 ];
440 # create a stack for the elements
441 push(@{$self->{stack}}, []);
443 my $ret = [ sort {
444 $self->{stack}->[-1] = ref($a) ? $a : [ $a ];
445 my $va = $self->exec($code);
447 $self->{stack}->[-1] = ref($b) ? $b : [ $b ];
448 my $vb = $self->exec($code);
450 $va cmp $vb;
451 } @{$list} ];
453 # destroy last stack
454 pop(@{$self->{stack}});
456 return $ret;
459 $self->{op}->{reverse} = sub {
460 return [ reverse @{$self->exec(shift)} ];
463 $self->{op}->{size} = sub { return scalar @{$self->exec($_[0])} };
465 $self->{xh}->{arch} = 'Unix';
467 return $self;
471 sub process {
472 my $self = shift;
473 my $src = shift;
475 my $c = $self->compile($src);
477 return $self->exec($c, @_);
481 sub new {
482 my $class = shift;
484 my $self = bless { @_ }, $class;
486 $self->{path} ||= [];
488 return $self->init();
492 __END__