1 # Copyright (C) 2008, The Perl Foundation.
6 lolcode::Grammar::Actions - ast transformations for lolcode
8 This file contains the methods that are used by the parse grammar
9 to build the PAST representation of an lolcode program.
10 Each method below corresponds to a rule in F<src/parser/grammar.pg>,
11 and is invoked at the point where C<{*}> appears in the rule,
12 with the current match object as the first argument. If the
13 line containing C<{*}> also has a C<#= key> comment, then the
14 value of the comment is passed as the second argument to the method.
18 class lolcode::Grammar::Actions;
21 my $block := $( $<block> );
22 my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'), :isdecl(1));
28 method statement ($/, $key) {
29 if (($key eq 'expression')&&($<expression><tokens>[0]<identifier> ne 'VISIBLE')) {
30 my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'));
31 my $past := PAST::Op.new( :pasttype('bind'), :node( $/ ) );
33 $past.push( $( $<expression> ) );
37 make $( $/{$key} ); # For now
46 my $name := ~$<variable><identifier>;
48 my $var := PAST::Var.new( :name( $name ),
53 my $scope := 'lexical';
55 if ~$<scope>[0] eq 'FARAWAY' {
61 unless $?BLOCK.symbol($name) {
62 $?BLOCK.symbol($name, :scope($scope));
68 # XXX Someone clever needs to refactor this into C<assign>
69 my $past := PAST::Op.new( :pasttype('bind'), :node( $/ ) );
71 $past.push( $( $<expression>[0] ) );
80 my $past := PAST::Op.new( :pasttype('bind'), :node( $/ ) );
81 $past.push( $( $<variable> ) );
82 $past.push( $( $<expression> ) );
86 method function($/,$key) {
89 our $?BLOCK_SIGNATURE;
91 $arglist := PAST::Stmts.new();
92 # if there are any parameters, get the PAST for each of them and
93 # adjust the scope to parameter.
95 my $param := PAST::Var.new(:name(~$_<identifier>), :scope('parameter'), :node($($_)));
97 $arglist.push($param);
99 $?BLOCK_SIGNATURE := $arglist;
101 elsif $key eq 'block' {
102 my $block := $( $<block> );
103 $block.blocktype('declaration');
104 $?BLOCK.symbol(~$<variable><identifier>, :arity($block.arity()));
107 my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'), :isdecl(1));
108 $block[1].unshift($it);
110 $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'));
112 $block.name(~$<variable><identifier>);
114 #my $past := PAST::Op.new( :pasttype('bind'), :node( $/ ) );
115 #$($<variable>).isdecl(1);
116 #$past.push( $( $<variable> ) );
117 #$past.push( $block );
126 my $count := +$<expression> - 1;
127 my $expr := $( $<expression>[$count] );
128 my $then := $( $<block>[$count] );
129 $then.blocktype('immediate');
130 my $past := PAST::Op.new( $expr, $then,
135 my $else := $( $<else>[0] );
136 $else.blocktype('immediate');
139 while ($count != 0) {
140 $count := $count - 1;
141 $expr := $( $<expression>[$count] );
142 $then := $( $<block>[$count] );
143 $then.blocktype('immediate');
144 $past := PAST::Op.new( $expr, $then, $past,
149 $expr := $past.shift();
150 my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'));
151 my $bind := PAST::Op.new( :pasttype('bind'), :node( $/ ) );
154 $past.unshift( $it );
155 my $past := PAST::Stmts.new( $bind, $past, :node( $/ ) );
160 my $count := +$<value> - 1;
161 my $val := $( $<value>[$count] );
162 my $then := $( $<block>[$count] );
163 my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'));
164 my $expr := PAST::Op.new(:pasttype('call'), :name('BOTH SAEM'), $it, $val);
165 $then.blocktype('immediate');
166 my $past := PAST::Op.new( $expr, $then,
171 my $else := $( $<else>[0] );
172 $else.blocktype('immediate');
175 while ($count != 0) {
176 $count := $count - 1;
177 $val := $( $<value>[$count] );
178 $expr := PAST::Op.new(:pasttype('call'), :name('BOTH SAEM'), $it, $val);
179 $then := $( $<block>[$count] );
180 $then.blocktype('immediate');
181 $past := PAST::Op.new( $expr, $then, $past,
186 #$expr := $past.shift();
187 #my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'));
188 #my $bind := PAST::Op.new( :pasttype('bind'), :node( $/ ) );
190 #$bind.push( $expr );
191 #$past.unshift( $it );
192 my $past := PAST::Stmts.new( $past, :node( $/ ) );
196 method block($/,$key) {
200 our $?BLOCK_SIGNATURE;
201 $?BLOCK := PAST::Block.new( PAST::Stmts.new(), :node($/) );
202 @?BLOCK.unshift($?BLOCK);
203 my $iter := $?BLOCK_SIGNATURE.iterator();
206 $?BLOCK.arity($?BLOCK.arity() + 1);
208 $?BLOCK.symbol($_.name(), :scope('lexical'));
211 elsif $key eq 'close' {
212 #my $past := PAST::Block.new( :blocktype('declaration'), :node( $/ ) );
213 my $past := @?BLOCK.shift();
214 $?BLOCK := @?BLOCK[0];
215 my $stmts := PAST::Stmts.new( :node( $/ ) );
217 $stmts.push( $( $_ ) );
224 method value($/, $key) {
229 make PAST::Val.new( :value( ~$/ ), :returns('String'), :node($/) );
232 method expression($/) {
233 my $past := PAST::Op.new( :name('expr_parse'), :pasttype('call'), :node( $/ ) );
235 my $name := ~$_<identifier>;
237 my $foo := lookup($name);
240 if $?BLOCK.symbol($name) && $?BLOCK.symbol($name)<scope> {
241 $past.push(PAST::Var.new(:name($name), :scope($?BLOCK.symbol($name)<scope>)));
244 my $inline := '%r = find_name "' ~ $_<identifier> ~ '"';
245 $past.push(PAST::Op.new( :inline($inline) ));
249 my $inline := '%r = find_name "MKAY"';
250 $past.push(PAST::Op.new( :inline($inline) ));
253 $past.push( $( $_ ) );
260 make PAST::Val.new( :value( ~$/ ), :returns('Integer'), :node($/) );
264 make PAST::Val.new( :value( ~$/ ), :returns('Float'), :node($/) );
268 if (~$/ eq 'FAIL' ) {
269 make PAST::Val.new( :value( 0 ), :returns('Boolean'), :node($/) );
272 make PAST::Val.new( :value( 1 ), :returns('Boolean'), :node($/) );
277 make PAST::Val.new( :value( $($<yarn_literal>) ), :node($/) );
281 method variable ($/) {
282 if ($<identifier> eq 'IT') {
283 make PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'));
289 my $var := PAST::Var.new( :name( $<identifier> ),
294 if $?BLOCK.symbol($<identifier>) {
295 my $scope := '' ~ $?BLOCK.symbol($<identifier>)<scope>;
304 my $sym_table := $_.symbol(~$<identifier>);
307 $scope := '' ~ $sym_table<scope>;
312 $var.scope('package');
326 # cperl-indent-level: 4
329 # vim: expandtab shiftwidth=4: