From 8eb0da6d3731af0ccef044452496cf5bfed9c9db Mon Sep 17 00:00:00 2001 From: Angel Ortega Date: Fri, 18 Sep 2009 09:06:24 +0200 Subject: [PATCH] Another rewrite: everything works but compile_c(). --- Artemus5.pm | 67 ++++++++++++++++++++++++++++--------------------------------- art5 | 22 +++++++++++++------- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/Artemus5.pm b/Artemus5.pm index 60a97a5..af13c48 100644 --- a/Artemus5.pm +++ b/Artemus5.pm @@ -109,6 +109,7 @@ sub compile { return $self->{pc}->{$str}; } + # joiner opcode my @ret = ( '?' ); # split by the Artemus5 marks @@ -134,16 +135,17 @@ sub compile { } -sub script { +sub code { my $self = shift; - my $sc = shift; + my $op = shift; - if (!exists($self->{sc}->{$sc})) { + if (!exists($self->{op}->{$op})) { my $c = undef; - # try to load and compile from the path + # try to resolve it by loading + # and compiling it from the path foreach my $p (@{$self->{path}}) { - if (open(F, $p . '/' . $sc)) { + if (open(F, $p . '/' . $op)) { $c = join('', ); close F; @@ -151,21 +153,19 @@ sub script { } } - if (!defined($c)) { - die "Undefined script: $sc"; + if (defined($c)) { + $self->{op}->{$op} = $self->compile($c); } - - $c = $self->compile($c); - $self->{sc}->{$sc} = $c; } - return $self->{sc}->{$sc}; + return $self->{op}->{$op}; } sub exec { my $self = shift; my $prg = shift; + my $ret = ''; # stream of Artemus5 code my @stream = @{$prg}; @@ -173,12 +173,28 @@ sub exec { # pick opcode my $op = shift(@stream); - # and execute - if (my $c = $self->{op}->{$op}) { - return $c->(@stream); + # pick code + my $c = $self->code($op); + + if (ref($c) eq 'CODE') { + $ret = $c->(@stream); + } + elsif (ref($c) eq 'ARRAY') { + # push the arguments to the stack + push(@{$self->{stack}}, + [ map { $self->exec($_); } + @stream ]); + + $ret = $self->exec($c); + + # drop stack + pop(@{$self->{stack}}); + } + else { + die "Opcode not found: $op"; } - die "Opcode not found: $op"; + return $ret; } @@ -198,27 +214,6 @@ sub init { return $_[0] || ''; }; - # subroutine frame - $self->{op}->{'()'} = sub { - my $code = shift; - - # pick code's arguments - my @args = @{$code}; - shift @args; - - # push the results to the stack - push(@{$self->{stack}}, - [ map { $self->exec($_); } - @args ]); - - my $ret = $self->exec($code); - - # drop stack - pop(@{$self->{stack}}); - - return $ret; - }; - # argument $self->{op}->{'$'} = sub { return $self->{stack}->[-1]->[$_[0]] || ''; diff --git a/art5 b/art5 index 3ee1392..566496b 100644 --- a/art5 +++ b/art5 @@ -13,7 +13,12 @@ my $a = Artemus5->new(); my $url = [ '?', [ '"', ''], + [ '"', ';offset='], + [ 'or', + [ '$', 2 ], + [ '"', 0 ], + ], + [ '"', '>'], [ 'or', [ '$', 1 ], [ '"', 'Main index' ], @@ -24,19 +29,22 @@ my $url = [ '?', $a->{op}->{url} = $url; # navigator -# <{if $2 -# {? "<< -# }}> +# "><<" +# } +#}> # \n<{url 'LOGIN' 'Login page'}>\n<{url 'INDEX'}> my $p = [ '?', [ '"', "\n" ], - [ '()', [ 'url', [ '"', 'LOGIN'], ['"', 'Login page'] ] ], + [ 'url', [ '"', 'LOGIN'], ['"', 'Login page'], ['"', 10 ]], [ '"', "\n" ], - [ '()', [ 'url', [ '"', 'INDEX'] ] ], + [ 'url', [ '"', 'INDEX'] ], [ '"', "\n" ] ]; -- 2.11.4.GIT