# Copyright (C) 2008, The Perl Foundation. # $Id: actions.pm 29034 2008-07-03 19:58:18Z tene $ =begin comments lolcode::Grammar::Actions - ast transformations for lolcode This file contains the methods that are used by the parse grammar to build the PAST representation of an lolcode program. Each method below corresponds to a rule in F, and is invoked at the point where C<{*}> appears in the rule, with the current match object as the first argument. If the line containing C<{*}> also has a C<#= key> comment, then the value of the comment is passed as the second argument to the method. =end comments class lolcode::Grammar::Actions; method TOP($/) { my $block := $( $ ); my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'), :isdecl(1)); $block.unshift($it); make $block; } method statement ($/, $key) { if (($key eq 'expression')&&($[0] ne 'VISIBLE')) { my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef')); my $past := PAST::Op.new( :pasttype('bind'), :node( $/ ) ); $past.push( $it ); $past.push( $( $ ) ); make $past; } else { make $( $/{$key} ); # For now } } method declare($/) { our $?BLOCK; our @?BLOCK; my $name := ~$; my $var := PAST::Var.new( :name( $name ), :viviself('Undef'), :node( $/ ) ); my $scope := 'lexical'; if $[0] { if ~$[0] eq 'FARAWAY' { $scope := 'package'; } } $var.scope(~$scope); unless $?BLOCK.symbol($name) { $?BLOCK.symbol($name, :scope($scope)); $var.isdecl(1); } if ($) { $var.isdecl(1); # XXX Someone clever needs to refactor this into C my $past := PAST::Op.new( :pasttype('bind'), :node( $/ ) ); $past.push( $var ); $past.push( $( $[0] ) ); make $past; } else { make $var; } } method assign($/) { my $past := PAST::Op.new( :pasttype('bind'), :node( $/ ) ); $past.push( $( $ ) ); $past.push( $( $ ) ); make $past; } method function($/,$key) { our $?BLOCK; if $key eq 'params' { our $?BLOCK_SIGNATURE; my $arglist; $arglist := PAST::Stmts.new(); # if there are any parameters, get the PAST for each of them and # adjust the scope to parameter. for $ { my $param := PAST::Var.new(:name(~$_), :scope('parameter'), :node($($_))); $param.isdecl(1); $arglist.push($param); } $?BLOCK_SIGNATURE := $arglist; } elsif $key eq 'block' { my $block := $( $ ); $block.blocktype('declaration'); $?BLOCK.symbol(~$, :arity($block.arity())); my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef'), :isdecl(1)); $block[1].unshift($it); $it := PAST::Var.new( :name( 'IT' ), :scope('lexical')); $block[1].push($it); $block.name(~$); make $block; #my $past := PAST::Op.new( :pasttype('bind'), :node( $/ ) ); #$($).isdecl(1); #$past.push( $( $ ) ); #$past.push( $block ); #make $past; } } method ifthen($/) { my $count := +$ - 1; my $expr := $( $[$count] ); my $then := $( $[$count] ); $then.blocktype('immediate'); my $past := PAST::Op.new( $expr, $then, :pasttype('if'), :node( $/ ) ); if ( $ ) { my $else := $( $[0] ); $else.blocktype('immediate'); $past.push( $else ); } while ($count != 0) { $count := $count - 1; $expr := $( $[$count] ); $then := $( $[$count] ); $then.blocktype('immediate'); $past := PAST::Op.new( $expr, $then, $past, :pasttype('if'), :node( $/ ) ); } $expr := $past.shift(); my $it := PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef')); my $bind := PAST::Op.new( :pasttype('bind'), :node( $/ ) ); $bind.push( $it ); $bind.push( $expr ); $past.unshift( $it ); my $past := PAST::Stmts.new( $bind, $past, :node( $/ ) ); make $past; } method block($/,$key) { our $?BLOCK; our @?BLOCK; if $key eq 'open' { our $?BLOCK_SIGNATURE; $?BLOCK := PAST::Block.new( PAST::Stmts.new(), :node($/) ); @?BLOCK.unshift($?BLOCK); my $iter := $?BLOCK_SIGNATURE.iterator(); $?BLOCK.arity(0); for $iter { $?BLOCK.arity($?BLOCK.arity() + 1); $?BLOCK[0].push($_); $?BLOCK.symbol($_.name(), :scope('lexical')); } } elsif $key eq 'close' { #my $past := PAST::Block.new( :blocktype('declaration'), :node( $/ ) ); my $past := @?BLOCK.shift(); $?BLOCK := @?BLOCK[0]; my $stmts := PAST::Stmts.new( :node( $/ ) ); for $ { $stmts.push( $( $_ ) ); } $past.push($stmts); make $past; } } method value($/, $key) { make $( $/{$key} ); } method bang($/) { make PAST::Val.new( :value( ~$/ ), :returns('String'), :node($/) ); } method expression($/) { my $past := PAST::Op.new( :name('expr_parse'), :pasttype('call'), :node( $/ ) ); for $ { my $name := ~$_; if($_) { my $foo := lookup($name); our $?BLOCK; our @?BLOCK; if $?BLOCK.symbol($name) && $?BLOCK.symbol($name) { $past.push(PAST::Var.new(:name($name), :scope($?BLOCK.symbol($name)))); } else { my $inline := '%r = find_name "' ~ $_ ~ '"'; $past.push(PAST::Op.new( :inline($inline) )); } } elsif($_ eq "MKAY"){ my $inline := '%r = find_name "MKAY"'; $past.push(PAST::Op.new( :inline($inline) )); } else { $past.push( $( $_ ) ); } } make $past; } method integer($/) { make PAST::Val.new( :value( ~$/ ), :returns('Integer'), :node($/) ); } method float($/) { make PAST::Val.new( :value( ~$/ ), :returns('Float'), :node($/) ); } method boolean($/) { if (~$/ eq 'FAIL' ) { make PAST::Val.new( :value( 0 ), :returns('Boolean'), :node($/) ); } else { make PAST::Val.new( :value( 1 ), :returns('Boolean'), :node($/) ); } } method quote($/) { make PAST::Val.new( :value( $($) ), :node($/) ); } method variable ($/) { if ($ eq 'IT') { make PAST::Var.new( :name( 'IT' ), :scope('lexical'), :viviself('Undef')); } else { our $?BLOCK; our @?BLOCK; my $var := PAST::Var.new( :name( $ ), :scope('lexical'), :viviself('Undef'), :node( $/ ) ); if $?BLOCK.symbol($) { my $scope := '' ~ $?BLOCK.symbol($); $var.scope(~$scope); } else { our @?BLOCK; my $exists := 0; my $scope; for @?BLOCK { if $_ { my $sym_table := $_.symbol(~$); if $sym_table { $exists := 1; $scope := '' ~ $sym_table; } } } if $exists == 0 { $var.scope('package'); } else { $var.scope($scope); } } make $var; } } # Local Variables: # mode: cperl # cperl-indent-level: 4 # fill-column: 100 # End: # vim: expandtab shiftwidth=4: