Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Variables

  • Loading branch information...
commit cf576485e3c78bf1319ee4a0398636810b8df713 1 parent ae7dca0
Brian Gernhardt authored
Showing with 41 additions and 1 deletion.
  1. +32 −0 cish/src/cish/Actions.pm
  2. +9 −1 cish/src/cish/Grammar.pm
View
32 cish/src/cish/Actions.pm
@@ -35,6 +35,8 @@ method block($/) {
method simple($/) {
if $<builtin> {
make $<builtin>.ast
+ } elsif $<decl_list> {
+ make $<decl_list>.ast
} elsif $<EXPR> {
make $<EXPR>.ast
} else {
@@ -68,12 +70,42 @@ method control:sym<if>($/) {
make $past;
}
+method decl($/) {
+ my $name := ~$<ident>;
+ my $BLOCK := @BLOCKS[0];
+ if $BLOCK.symbol($name) {
+ $/.CURSOR.panic("Redeclaration of variable ", $name);
+ }
+
+ my $past := PAST::Var.new(
+ :name($name), :scope<lexical>, :isdecl(1), :lvalue(1), :node($/)
+ );
+
+ $past.viviself( $<EXPR> ?? $<EXPR>[0].ast !! PAST::Val.new( :value(0) ) );
+
+ $BLOCK.symbol($name, :scope<lexical>);
+
+ $BLOCK.push($past);
+
+ make PAST::Var.new(:name($name));
+}
+
+method decl_list($/) {
+ my $past := PAST::Stmts.new( :node($/) );
+ for $<decl> { $past.push( $_.ast ); }
+ make $past;
+}
+
method term:sym<integer>($/) {
make PAST::Val.new( :value($<integer>.ast) );
}
method term:sym<quote>($/) { make $<quote>.ast; }
+method term:sym<variable>($/) {
+ make PAST::Var.new( :name(~$/) );
+}
+
method quote:sym<'>($/) { make $<quote_EXPR>.ast; }
method quote:sym<">($/) { make $<quote_EXPR>.ast; }
View
10 cish/src/cish/Grammar.pm
@@ -36,13 +36,14 @@ rule control:sym<if> {
rule block {
'{'
- <.begin_block>
+ <.begin_block> :
<statement>*
'}'
}
rule simple {
| <builtin>
+ | <decl_list>
| <EXPR>
| <?>
}
@@ -51,10 +52,14 @@ proto token builtin { <...> }
rule builtin:sym<say> { <sym> [ <EXPR> ] ** ',' }
rule builtin:sym<print> { <sym> [ <EXPR> ] ** ',' }
+rule decl { <ident> [ '=' <EXPR> ]? }
+rule decl_list { 'int' <decl> ** ',' }
+
## Terms
token term:sym<integer> { <integer> }
token term:sym<quote> { <quote> }
+token term:sym<variable> { <ident> }
proto token quote { <...> }
token quote:sym<'> { <?[']> <quote_EXPR: ':q'> } #'
@@ -70,6 +75,7 @@ INIT {
cish::Grammar.O(':prec<r>, :assoc<left>', '%and');
cish::Grammar.O(':prec<q>, :assoc<left>', '%or');
cish::Grammar.O(':prec<p>, :assoc<right>', '%ternary');
+ cish::Grammar.O(':prec<o>, :assoc<right>', '%assignment');
}
token circumfix:sym<( )> { '(' <.ws> <EXPR> ')' }
@@ -98,3 +104,5 @@ token infix:sym<? :> {
'?' <EXPR('p')> ':'
<O('%ternary, :pasttype<if>, :reducecheck<ternary>')>
}
+
+token infix:sym<=> { <sym> <O('%assignment, :pirop<assign>')> }
Please sign in to comment.
Something went wrong with that request. Please try again.