1 # Copyright (C) 2008, The Perl Foundation.
6 cardinal::Grammar::Actions - ast transformations for cardinal
8 This file contains the methods that are used by the parse grammar
9 to build the PAST representation of an cardinal 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 cardinal::Grammar::Actions;
21 my $past := $( $<comp_stmt> );
22 $past.blocktype('declaration');
23 $past.pirflags(':load');
26 if defined( $?INIT ) {
34 $?INIT.blocktype('declaration');
35 $?INIT.pirflags(':init :load');
36 $past.unshift( $?INIT );
37 $?INIT := PAST::Block.new(); # For the next eval.
43 method comp_stmt($/,$key) {
46 our $?BLOCK_SIGNATURED;
48 if $?BLOCK_SIGNATURED {
49 $?BLOCK := $?BLOCK_SIGNATURED;
50 $?BLOCK_SIGNATURED := 0;
53 $?BLOCK := PAST::Block.new( PAST::Stmts.new(), :node($/));
55 @?BLOCK.unshift($?BLOCK);
58 my $past := @?BLOCK.shift();
59 $?BLOCK := @?BLOCK[0];
60 $past.push( $( $<stmts> ) );
66 my $past := PAST::Stmts.new( :node($/) );
73 method basic_stmt($/, $key) {
78 my $past := $( $<basic_stmt> );
80 my $modifier := $( $_ );
81 $modifier.push($past);
89 if $<sym> eq 'until' {
90 ## there is no :pasttype('until'); this is called repeat_until
91 $op := 'repeat_until';
94 ## if, while and unless are valid :pasttypes.
97 make PAST::Op.new( $( $<expr> ), :pasttype($op), :node($/) );
102 my $past := $( $<arg> );
104 $past := PAST::Op.new( $past, :pirop('n_not'), :node($/) );
108 if ~$<op>[0] eq 'and' { $op := 'if'; }
109 else { $op := 'unless'; }
110 $past := PAST::Op.new( $past, $( $<expr>[0] ), :pasttype($op), :node($/) );
115 method return_stmt($/) {
116 my $past := $($<call_args>);
117 $past.pasttype('inline');
118 $past.inline(' .return(%0)');
122 ## not entirely sure what alias does, but this is a guess...
124 my $fname := $<fname>[0];
125 my $alias := $<fname>[1];
126 make PAST::Op.new( $alias, $fname, :pasttype('bind'), :node($/) );
130 my $past := $( $<comp_stmt> );
131 my $sub := PAST::Compiler.compile( $past );
133 ## XXX what to do here? empty block? stolen from rakudo.
134 make PAST::Block.new( :node($/) );
138 my $past := PAST::Block.new( $( $<comp_stmt> ), :node($/) );
139 $past.blocktype('declaration');
140 my $sub := PAST::Compiler.compile( $past );
141 PIR q< $P0 = get_hll_global ['cardinal'], '@?END_BLOCKS' >;
142 PIR q< $P1 = find_lex '$sub' >;
143 PIR q< push $P0, $P1 >;
147 method indexed_assignment($/) {
148 my $key := $( $<key> );
149 my $rhs := $( $<rhs> );
150 my $primary := $( $<basic_primary> );
152 my $past := PAST::Op.new( :name('[]='), :pasttype('callmethod'), :node($/) );
154 $past.push( $primary );
160 method member_assignment($/) {
161 my $rhs := $( $<rhs> );
162 my $primary := $( $<basic_primary> );
164 my $past := PAST::Op.new( :name(~$<key><ident> ~ '='), :pasttype('callmethod'), :node($/) );
166 $past.push( $primary );
171 method assignment($/) {
172 my $lhs := $( $<mlhs> );
174 my $name := $lhs.name();
175 unless $?BLOCK.symbol(~$name) {
181 my $sym_table := $_.symbol(~$name);
184 $scope := '' ~ $sym_table<scope>;
189 if $exists == 0 && defined($?CLASS) {
190 my $block := $?CLASS[0];
191 my $sym_table := $block.symbol(~$name);
194 $scope := '' ~ $sym_table<scope>;
201 $?BLOCK.symbol(~$name, :scope($scope));
205 my $rhs := $( $<mrhs> );
206 make PAST::Op.new( $lhs, $rhs, :pasttype('bind'), :lvalue(1), :node($/) );
209 method mlhs($/, $key) {
213 method lhs($/, $key) {
217 method member_variable($/) {
218 make $( $<primary> );
225 $args := $( $<args>[0] );
228 my $past := PAST::Op.new( :name('[]'), :pasttype('callmethod'), :node($/) );
230 $past.push( $args.shift() );
236 method variable($/, $key) {
238 if $key eq 'varname' {
239 $past := $( $/<varname> );
241 elsif $key eq 'self' {
242 $past := PAST::Op.new(:inline('%r = self'));
244 elsif $key eq 'nil' {
245 $past := PAST::Var.new(:scope('package'), :name('nil'));
250 method varname($/, $key) {
251 my $past := $( $/{$key} );
252 if is_a_sub(~$/) { # unary sub
253 $past := PAST::Op.new( :pasttype('call'), :node($/), $past );
261 my $toplevel := @?BLOCK[0];
262 $toplevel.symbol(~$/, :scope('package'), :namespace(@namespace));
263 make PAST::Var.new( :name(~$/), :scope('package'), :namespace(@namespace), :viviself('Undef'), :node($/) );
266 method instance_variable($/) {
270 my $past := PAST::Var.new( :name($name), :scope('attribute'), :viviself('Undef'), :node($/) );
271 my $block := $?CLASS[0];
272 unless $block.symbol(~$/) {
276 :name('!keyword_has'),
281 PAST::Val.new( :value($name) )
285 $block.symbol(~$name, :scope('attribute'));
286 $?BLOCK.symbol(~$name, :scope('attribute'));
291 method class_variable($/) {
295 my $past := PAST::Var.new( :name($name), :scope('package'), :viviself('Undef'), :node($/) );
296 my $block := $?CLASS[0];
297 unless $block.symbol(~$/) {
298 $block.symbol(~$name, :scope('package'));
299 $?BLOCK.symbol(~$name, :scope('package'));
304 method local_variable($/) {
306 my $past := PAST::Var.new( :name(~$/), :node($/), :viviself('Undef') );
307 if $?BLOCK.symbol($<ident>) {
308 my $scope := '' ~ $?BLOCK.symbol($<ident>)<scope>;
309 $past.scope(~$scope);
317 my $sym_table := $_.symbol(~$<ident>);
320 $scope := '' ~ $sym_table<scope>;
325 $past.scope('package');
337 my $past := $( $<local_variable> );
341 method constant_variable($/) {
344 if $name eq 'Array' { $name := "CardinalArray"; }
345 elsif $name eq 'Hash' { $name := "CardinalHash"; }
346 elsif $name eq 'String' { $name := "CardinalString"; }
347 my $past := PAST::Var.new( :name($name), :scope('package'), :node($/), :viviself('Undef'), :namespace( @a ) );
353 my $cond := +$<expr> - 1;
354 my $comp := $( $<comp_stmt>[$cond] );
355 $comp.blocktype('immediate');
356 my $past := PAST::Op.new( $( $<expr>[$cond] ),
362 my $else := $( $<else>[0] ) ;
363 $else.blocktype('immediate');
368 $comp := $( $<comp_stmt>[$cond] );
369 $comp.blocktype('immediate');
370 $past := PAST::Op.new( $( $<expr>[$cond] ),
380 method unless_stmt($/) {
381 my $cond := $( $<expr> );
382 my $body := $( $<comp_stmt> );
383 $body.blocktype('immediate');
384 my $past := PAST::Op.new( $cond, $body, :pasttype('unless'), :node($/) );
386 $past.push( $( $<else>[0] ) );
392 make $( $<comp_stmt> );
396 make $( $<comp_stmt> );
399 method while_stmt($/) {
400 my $cond := $( $<expr> );
401 my $body := $( $<comp_stmt> );
402 $body.blocktype('immediate');
403 make PAST::Op.new( $cond, $body, :pasttype(~$<sym>), :node($/) );
406 method for_stmt($/) {
407 my $list := $( $<expr> );
408 my $body := $( $<comp_stmt> );
409 my $var := $( $<variable> );
410 $body.blocktype('declaration');
411 $var.scope('parameter');
414 make PAST::Op.new( $list, $body, :pasttype('for'), :node($/) );
417 method control_command($/,$key) {
418 $/.panic("next, break, and redo aren't implemented yet");
422 my $past := $( $<comp_stmt> );
423 my $name := $( $<module_identifier> );
424 $past.namespace( $name.name() );
425 $past.blocktype('declaration');
429 method begin_end($/) {
430 my $past := $( $<comp_stmt> );
431 # XXX handle resque and ensure clauses
435 method classdef($/,$key) {
440 my $name := ~$<module_identifier><ident>;
442 my $decl := PAST::Stmts.new();
452 :name('!keyword_class'),
453 PAST::Val.new( :value($name) )
457 @?CLASS.unshift( $?CLASS );
459 $?CLASS.unshift( PAST::Block.new() );
462 my $block := $( $<comp_stmt> );
463 $block.namespace($name);
464 $block.blocktype('declaration');
465 $block.pirflags(':init :load');
469 :pasttype('callmethod'),
473 :name('!CARDINALMETA'),
474 :namespace('CardinalObject')
481 :value('CardinalObject'),
482 :named( PAST::Val.new( :value('parent') ) )
487 unless defined( $?INIT ) {
488 $?INIT := PAST::Block.new();
491 if $_.WHAT() eq 'Block' {
499 # Restore outer class.
501 $?CLASS := @?CLASS.shift();
504 $?CLASS := @?CLASS[0];
512 method functiondef($/) {
513 my $past := $( $<comp_stmt> );
514 my $name := $<fname>;
515 my $arity := +$past[0]<arity>;
516 #my $args := $( $<argdecl> );
521 $?BLOCK.symbol(~$name, :scope('package'), :arity($arity));
522 if defined($?CLASS) {
523 $past.pirflags(':method');
528 method sig_identifier($/) {
529 my $past := $($<identifier>);
530 if +$<default> == 1 {
531 $past.viviself( $( $<default>[0] ) );
536 method block_signature($/) {
537 my $params := PAST::Stmts.new( :node($/) );
538 my $past := PAST::Block.new($params, :blocktype('declaration'));
539 for $<sig_identifier> {
540 my $parameter := $( $_ );
541 $past.symbol($parameter.name(), :scope('lexical'));
542 $parameter.scope('parameter');
543 $params.push($parameter);
546 my $slurp := $( $<slurpy_param>[0] || $<slurpy_param> );
547 $past.symbol($slurp.name(), :scope('lexical'));
548 $params.push( $slurp );
552 my $block := $( $<block_param>[0] );
553 $past.symbol($block.name(), :scope('lexical'));
554 $params.push($block);
556 $params.arity( +$<sig_identifier> + +$<block_param> );
557 our $?BLOCK_SIGNATURED := $past;
561 method slurpy_param($/) {
562 my $past := $( $<identifier> );
564 $past.scope('parameter');
568 method block_param($/) {
569 my $past := $( $<identifier> );
570 $past.scope('parameter');
574 method identifier($/) {
575 make PAST::Var.new( :name(~$<ident>), :node($/) );
578 method module_identifier($/) {
579 make PAST::Var.new( :name(~$/), :scope('package'), :node($/) );
586 method methodcall($/) {
587 my $op := $<operation>;
590 $past := $( $<call_args>[0] );
593 $past := PAST::Op.new();
596 $past.pasttype('callmethod');
599 $past.push( $( $<do_block>[0] ) );
606 method do_block($/) {
607 my $past := $( $<comp_stmt> );
611 method super_call($/) {
612 my $past := $( $<call_args> );
613 ## how to invoke super.xxx ?
617 method operation($/) {
618 make $( $<identifier> );
621 method call_args($/) {
624 $past := $( $<args> );
627 $past := PAST::Op.new( :pasttype('call'), :node($/) );
630 $past.push( $( $<do_block>[0] ) );
636 my $past := PAST::Op.new( :pasttype('call'), :node($/) );
643 method basic_primary($/, $key) {
648 my $past := $( $<basic_primary> );
650 # XXX check this out:
651 for $<post_primary_expr> {
652 my $postexpr := $( $_ );
653 $postexpr.unshift($past);
659 method post_primary_expr($/, $key) {
663 method scope_identifier($/) {
664 make $( $<identifier> );
665 # XXX handle :: operator.
668 method literal($/, $key) {
669 my $past := $( $/{$key} );
673 method pcomp_stmt($/) {
674 make $( $<comp_stmt> );
677 method quote_string($/) {
678 make $( $<quote_expression> );
682 make $( $<quote_expression> );
688 $list := $( $<args>[0] );
692 $list := PAST::Op.new( :name('list'), :node($/) );
699 my $hash := PAST::Op.new( :name('hash'), :node($/) );
701 my $items := $( $<assocs>[0] );
710 my $assoc := PAST::Stmts.new(:node($/));
719 my $past := PAST::Op.new(:name('list'), :node($/));
720 $past.push( $( $<arg>[0] ) );
721 $past.push( $( $<arg>[1] ) );
726 make PAST::Val.new( :value( ~$/ ), :returns('Float'), :node($/) );
730 make PAST::Val.new( :value( ~$/ ), :returns('CardinalInteger'), :node($/) );
734 make PAST::Val.new( :value( ~$<string_literal> ), :returns('CardinalString'), :node($/) );
738 make $($<quote_expression>);
741 method quote_expression($/, $key) {
743 if $key eq 'quote_regex' {
745 $past := PAST::Block.new(
747 :compiler('PGE::Perl6Regex'),
749 :blocktype('declaration'),
753 elsif $key eq 'quote_concat' {
754 if +$<quote_concat> == 1 {
755 $past := $( $<quote_concat>[0] );
758 $past := PAST::Op.new(
763 for $<quote_concat> {
772 method quote_concat($/) {
773 my $terms := +$<quote_term>;
775 my $past := $( $<quote_term>[0] );
776 while ($count != $terms) {
777 $past := PAST::Op.new(
779 $( $<quote_term>[$count] ),
783 $count := $count + 1;
789 method quote_term($/, $key) {
791 if ($key eq 'literal') {
792 $past := PAST::Val.new(
793 :value( ~$<quote_literal> ),
794 :returns('CardinalString'), :node($/)
797 elsif ($key eq 'variable') {
798 $past := $( $<variable> );
800 elsif ($key eq 'circumfix') {
801 $past := $( $<circumfix> );
802 if $past.WHAT() eq 'Block' {
803 $past.blocktype('immediate');
809 method arg($/, $key) {
810 ## Handle the operator table
816 my $past := PAST::Op.new( :name($<type>),
817 :pasttype($<top><pasttype>),
818 :pirop($<top><pirop>),
819 :lvalue($<top><lvalue>),
829 sub is_a_sub($name) {
832 if $?BLOCK.symbol(~$name) {
833 if defined($?BLOCK.symbol(~$name)<arity>) {
842 my $sym_table := $_.symbol(~$name);
844 if defined($sym_table<arity>) {
853 my $lex := lex_lookup($name);
854 if $lex && ~lookup_class($lex) eq 'Sub' { return(1); }
860 # cperl-indent-level: 4
863 # vim: expandtab shiftwidth=4: