Permalink
Browse files

Selection and Repetition

  • Loading branch information...
1 parent 0cfde50 commit af7256b03e499e002629a19c5f834af03b1033c1 Brian Gernhardt committed Jan 25, 2011
Showing with 23 additions and 1 deletion.
  1. +16 −0 cish/src/cish/Actions.pm
  2. +7 −1 cish/src/cish/Grammar.pm
View
@@ -15,6 +15,8 @@ method TOP($/) {
method statement($/) {
if $<block> {
make $<block>.ast;
+ } elsif $<control> {
+ make $<control>.ast;
} else {
make $<simple>.ast;
}
@@ -46,6 +48,20 @@ method builtin:sym<print>($/) {
make $past;
}
+method control:sym<while>($/) {
+ make PAST::Op.new(
+ $<EXPR>.ast,
+ $<statement>.ast,
+ :pasttype<while>, :node($/)
+ );
+}
+
+method control:sym<if>($/) {
+ my $past := PAST::Op.new( $<EXPR>.ast, :pasttype<if>, :node($/) );
+ for $<statement> { $past.push( $_.ast ); }
+ make $past;
+}
+
method term:sym<integer>($/) {
make PAST::Val.new( :value($<integer>.ast) );
}
View
@@ -22,7 +22,13 @@ token ws {
## Statements
rule statement {
- <block> | <simple> ';'
+ <block> | <control> | <simple> ';'
+}
+
+proto token control { <...> }
+rule control:sym<while> { <sym> '(' <EXPR> ')' <statement> }
+rule control:sym<if> {
+ <sym> '(' <EXPR> ')' <statement> [ 'else' : <statement> ]?
}
rule block { '{' <statement>* '}' }

0 comments on commit af7256b

Please sign in to comment.