The compiler almost work.
[artemus.git] / Artemus5.pm
blobc2d69ec9f5ee6abdfa69fc039b9de3cec0bb71d4
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 if ($$seq =~ s/^"(([^"\\]*(\\.[^"\\]*)*))"\s*//) {
44 # double quoted string
45 my $str = $1;
47 # replace usual escaped characters
48 $str =~ s/\\n/\n/g;
49 $str =~ s/\\r/\r/g;
50 $str =~ s/\\t/\t/g;
51 $str =~ s/\\"/\"/g;
52 $str =~ s/\\\\/\\/g;
54 push(@ret, [ '"', $str ]);
56 elsif ($$seq =~ s/^'(([^'\\]*(\\.[^'\\]*)*))'\s*//) {
57 # single quoted string
58 my $str = $1;
60 $str =~ s/\\'/\'/g;
61 $str =~ s/\\\\/\\/g;
63 push(@ret, [ '"', $str ]);
65 elsif ($$seq =~ s/^(\d+(\.\d+)?)\s*//) {
66 # number
67 push(@ret, [ '"', $1 ]);
69 elsif ($$seq =~ /^\{\s*/) {
70 # another code sequence
71 push(@ret, $self->compile_c($seq));
73 elsif ($$seq =~ s/^\}\s*//) {
74 # end of sequence
75 last;
77 elsif ($$seq =~ s/^%([^\s\{]+)\s*//) {
78 # external hash value
79 push(@ret, [ '%', $1 ]);
81 elsif ($$seq =~ s/^(\$\d+)\s*//) {
82 # argument
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) {
90 push(@ret, $1);
92 # the rest will be args for this one
93 next;
96 push(@ret, [ $1 ]);
98 else {
99 die "Syntax error near $$seq";
102 # if arrived here with only one instruction,
103 # we're over
104 if (scalar(@ret) == 1) {
105 return $ret[0];
109 return [ @ret ];
113 sub compile {
114 my $self = shift;
115 my $str = shift;
117 # was this code already compiled?
118 if (exists($self->{pc}->{$str})) {
119 return $self->{pc}->{$str};
122 # joiner opcode
123 my @ret = ( '?' );
125 # split by the Artemus5 marks
126 my @stream = split(/(<\{|\}>)/, $str);
128 # alternate between literal strings and Artemus5 code
129 while (@stream) {
130 my $p = shift(@stream);
132 if ($p eq '<{') {
133 $p = shift(@stream);
134 push(@ret, $self->compile_c(\$p));
135 shift(@stream);
137 else {
138 push(@ret, [ '"', $p ]);
142 my $ret = [ @ret ];
144 return $self->{pc}->{$str} = $ret;
148 sub code {
149 my $self = shift;
150 my $op = shift;
152 if (!exists($self->{op}->{$op})) {
153 my $c = undef;
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)) {
159 $c = join('', <F>);
160 close F;
162 last;
166 if (defined($c)) {
167 $self->{op}->{$op} = $self->compile($c);
171 return $self->{op}->{$op};
175 sub exec {
176 my $self = shift;
177 my $prg = shift;
178 my $ret = '';
180 # stream of Artemus5 code
181 my @stream = @{$prg};
183 # pick opcode
184 my $op = shift(@stream);
186 # pick code
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($_); }
196 @stream ]);
198 $ret = $self->exec($c);
200 # drop stack
201 pop(@{$self->{stack}});
203 else {
204 die "Opcode not found: $op";
207 return $ret;
211 sub init {
212 my $self = shift;
214 $self->{stack} = [ [] ];
216 $self->{op}->{VERSION} = [ '"', $Artemus5::VERSION ];
218 $self->{op}->{VERSION_STR} = [
219 '?', 'Artemus ', [ 'VERSION' ]
222 # literal
223 $self->{op}->{'"'} = sub {
224 return $_[0] || '';
227 # argument
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]) ];
248 return '';
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 {
273 my $ret = '';
275 if ($self->exec($_[0])) {
276 $ret = $self->exec($_[1]);
278 elsif (scalar(@_) == 3) {
279 $ret = $self->exec($_[2]);
282 $ret;
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';
304 return $self;
308 sub process {
309 my $self = shift;
310 my $src = shift;
312 my $c = $self->compile($src);
314 return $self->exec($c, @_);
318 sub new {
319 my $class = shift;
321 my $self = bless { @_ }, $class;
323 $self->{path} ||= [];
325 return $self->init();
329 __END__