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*//;
43 if ($$seq =~ s/^"(([^"\\]*(\\.[^"\\]*)*))"\s*//) {
44 # double quoted string
47 # replace usual escaped characters
54 push(@ret, [ '"', $str ]);
56 elsif ($$seq =~ s/^'(([^'\\]*(\\.[^'\\]*)*))'\s*//) {
57 # single quoted string
63 push(@ret, [ '"', $str ]);
65 elsif ($$seq =~ s/^(\d+(\.\d+)?)\s*//) {
67 push(@ret, [ '"', $1 ]);
69 elsif ($$seq =~ /^\{\s*/) {
70 # another code sequence
71 push(@ret, $self->compile_c($seq));
73 elsif ($$seq =~ s/^\}\s*//) {
77 elsif ($$seq =~ s/^%([^\s\{]+)\s*//) {
79 push(@ret, [ '%', $1 ]);
81 elsif ($$seq =~ s/^(\$\d+)\s*//) {
83 push(@ret, [ '$', $1 ]);
85 elsif ($$seq =~ s/^([^\s\{]+)\s*//) {
86 # code sequence without arguments
88 # nothing yet? operator call
89 if (scalar(@ret) == 0) {
92 # the rest will be args for this one
99 die "Syntax error near $$seq";
102 # if arrived here with only one instruction,
104 if (scalar(@ret) == 1) {
117 # was this code already compiled?
118 if (exists($self->{pc
}->{$str})) {
119 return $self->{pc
}->{$str};
125 # split by the Artemus5 marks
126 my @stream = split(/(<\{|\}>)/, $str);
128 # alternate between literal strings and Artemus5 code
130 my $p = shift(@stream);
134 push(@ret, $self->compile_c(\
$p));
138 push(@ret, [ '"', $p ]);
144 return $self->{pc
}->{$str} = $ret;
152 if (!exists($self->{op
}->{$op})) {
155 # try to resolve it by loading
156 # and compiling it from the path
157 foreach my $p (@
{$self->{path
}}) {
158 if (open(F
, $p . '/' . $op)) {
167 $self->{op
}->{$op} = $self->compile($c);
171 return $self->{op
}->{$op};
180 # stream of Artemus5 code
181 my @stream = @
{$prg};
184 my $op = shift(@stream);
187 my $c = $self->code($op);
189 if (ref($c) eq 'CODE') {
190 $ret = $c->(@stream);
192 elsif (ref($c) eq 'ARRAY') {
193 # push the arguments to the stack
194 push(@
{$self->{stack
}},
195 [ map { $self->exec($_); }
198 $ret = $self->exec($c);
201 pop(@
{$self->{stack
}});
204 die "Opcode not found: $op";
214 $self->{stack
} = [ [] ];
216 $self->{op
}->{VERSION
} = [ '"', $Artemus5::VERSION
];
218 $self->{op
}->{VERSION_STR
} = [
219 '?', 'Artemus ', [ 'VERSION' ]
223 $self->{op
}->{'"'} = sub {
228 $self->{op
}->{'$'} = sub {
229 return $self->{stack
}->[-1]->[$_[0]] || '';
232 # external hash (e.g. CGI variables)
233 $self->{op
}->{'%'} = sub {
234 return $self->{xh
}->{$_[0]} || '';
240 $self->{op
}->{'?'} = sub {
241 return join('', map { $self->exec($_); } @_);
244 $self->{op
}->{'='} = sub {
245 $self->{op
}->{$self->exec($_[0])} =
246 [ '"', $self->exec($_[1]) ];
251 $self->{op
}->{var
} = sub { $self->{xh
}->{$self->exec($_[0])}; };
253 $self->{op
}->{eq} = sub {
254 ($self->exec($_[0]) || '') eq
255 ($self->exec($_[1]) || '') ?
1 : 0;
257 $self->{op
}->{ne} = sub {
258 ($self->exec($_[0]) || '') ne
259 ($self->exec($_[1]) || '') ?
1 : 0;
262 $self->{op
}->{and} = sub {
263 ($self->exec($_[0]) && $self->exec($_[1])) || '';
265 $self->{op
}->{or} = sub {
266 $self->exec($_[0]) || $self->exec($_[1]) || '';
268 $self->{op
}->{not} = sub {
269 $self->exec($_[0]) ?
0 : 1;
272 $self->{op
}->{if} = sub {
275 if ($self->exec($_[0])) {
276 $ret = $self->exec($_[1]);
278 elsif (scalar(@_) == 3) {
279 $ret = $self->exec($_[2]);
285 $self->{op
}->{add
} = sub {
286 return ($self->exec($_[0]) || 0) + ($self->exec($_[1]) || 0);
288 $self->{op
}->{sub} = sub {
289 return ($self->exec($_[0]) || 0) - ($self->exec($_[1]) || 0);
291 $self->{op
}->{mul
} = sub {
292 return ($self->exec($_[0]) || 0) * ($self->exec($_[1]) || 0);
294 $self->{op
}->{div
} = sub {
295 return ($self->exec($_[0]) || 0) / ($self->exec($_[1]) || 1);
298 $self->{op
}->{env
} = sub {
299 return $ENV{$self->exec($_[0])};
302 $self->{xh
}->{arch
} = 'Unix';
312 my $c = $self->compile($src);
314 return $self->exec($c, @_);
321 my $self = bless { @_ }, $class;
323 $self->{path
} ||= [];
325 return $self->init();