@@ -167,6 +167,13 @@ class NQP::Actions is HLL::Actions {
167
167
$ past := QAST ::Op. new ($ ml <cond >. ast, $ past , : op(~ $ ml <sym >), : node($/ ) );
168
168
}
169
169
}
170
+ if $ past <var_initialized > {
171
+ # Variable declared and unconditionally initialized; can strip
172
+ # the added just-to-be-safe initialization of the lexical and
173
+ # just have the var decl.
174
+ my $ decls := $ * W . cur_lexpad()[0 ];
175
+ $ decls . push ($ decls . pop ()[0 ]); # First child of bind node is var decl
176
+ }
170
177
}
171
178
elsif $ < statement > { $ past := $ < statement > . ast; }
172
179
elsif $ < statement_control > { $ past := $ < statement_control > . ast; }
@@ -718,6 +725,11 @@ class NQP::Actions is HLL::Actions {
718
725
$/ . CURSOR. panic(" Redeclaration of symbol " , $ name );
719
726
}
720
727
if $ * SCOPE eq ' has' {
728
+ # Initializer not allowed.
729
+ if $ < initializer > {
730
+ $/ . CURSOR. panic(' Initiailizers not supported on has-scoped variables' );
731
+ }
732
+
721
733
# Locate the type of meta-attribute we need.
722
734
unless nqp ::existskey(% * HOW , $ * PKGDECL ~ ' -attr' ) {
723
735
$/ . CURSOR. panic(" $ * PKGDECL packages do not support attributes" );
@@ -755,6 +767,9 @@ class NQP::Actions is HLL::Actions {
755
767
$ name := ~ $ < variable > ;
756
768
$ past := lexical_package_lookup([$ name ], $/ );
757
769
$ BLOCK . symbol($ name , : scope(' package' ) );
770
+ if $ < initializer > {
771
+ $ past := QAST ::Op. new ( : op(' bind' ), $ past , $ < initializer > . ast );
772
+ }
758
773
}
759
774
else {
760
775
my $ type ;
@@ -779,6 +794,10 @@ class NQP::Actions is HLL::Actions {
779
794
QAST ::Var. new ( : name($ name ), : scope(' lexical' ), : decl(' var' ), : returns($ type ) ),
780
795
$ default
781
796
));
797
+ if $ < initializer > {
798
+ $ past := QAST ::Op. new ( : op(' bind' ), : node($/ ), $ past , $ < initializer > . ast );
799
+ $ past <var_initialized > := 1 ;
800
+ }
782
801
$ BLOCK . symbol($ name , : scope(' lexical' ), : type($ type ) );
783
802
}
784
803
@@ -790,6 +809,10 @@ class NQP::Actions is HLL::Actions {
790
809
make $ past ;
791
810
}
792
811
812
+ method initializer ($/ ) {
813
+ make $ < EXPR > . ast;
814
+ }
815
+
793
816
method routine_declarator :sym <sub >($/ ) { make $ < routine_def > . ast; }
794
817
method routine_declarator :sym <method >($/ ) { make $ < method_def > . ast; }
795
818
0 commit comments