Skip to content

Commit

Permalink
Make sub_definitions work the way they should in the example compiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcurtis committed Jul 19, 2010
1 parent a12ad27 commit 1f22fac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
43 changes: 24 additions & 19 deletions src/Squaak/Actions.pm
Expand Up @@ -21,28 +21,14 @@ method statementlist($/) {
}

method stat_or_def($/) {
make $<statement>.ast;
}

method statement:sym<assignment>($/) {
my $lhs := $<primary>.ast;
my $rhs := $<expression>.ast;
$lhs.lvalue(1);
make PAST::Op.new($lhs, $rhs, :pasttype<bind>, :node($/));
}

method statement:sym<if>($/) {
my $cond := $<expression>.ast;
my $past := PAST::Op.new( $cond, $<then>.ast,
:pasttype('if'),
:node($/) );
if $<else> {
$past.push($<else>[0].ast);
if $<statement> {
make $<statement>.ast;
} else { # Must be a def
make $<sub_definition>.ast;
}
make $past;
}

method statement:sym<sub>($/) {
method sub_definition($/) {
our $?BLOCK;
our @?BLOCK;
my $past := $<parameters>.ast;
Expand Down Expand Up @@ -84,6 +70,25 @@ method parameters($/) {
make $past;
}


method statement:sym<assignment>($/) {
my $lhs := $<primary>.ast;
my $rhs := $<expression>.ast;
$lhs.lvalue(1);
make PAST::Op.new($lhs, $rhs, :pasttype<bind>, :node($/));
}

method statement:sym<if>($/) {
my $cond := $<expression>.ast;
my $past := PAST::Op.new( $cond, $<then>.ast,
:pasttype('if'),
:node($/) );
if $<else> {
$past.push($<else>[0].ast);
}
make $past;
}

method statement:sym<throw>($/) {
make PAST::Op.new( $<expression>.ast,
:pirop('throw'),
Expand Down
21 changes: 11 additions & 10 deletions src/Squaak/Grammar.pm
Expand Up @@ -32,6 +32,17 @@ rule statementlist {

rule stat_or_def {
| <statement>
| <sub_definition>
}

rule sub_definition {
'sub' <identifier> <parameters>
<statement>*
'end'
}

rule parameters {
'(' [<identifier> ** ',']? ')'
}

proto rule statement { <...> }
Expand All @@ -50,16 +61,6 @@ rule statement:sym<if> {
'end'
}

rule statement:sym<sub> {
<sym> <identifier> <parameters>
<statement>*
'end'
}

rule parameters {
'(' [<identifier> ** ',']? ')'
}

rule statement:sym<throw> {
<sym> <expression>
}
Expand Down

0 comments on commit 1f22fac

Please sign in to comment.