@@ -57,8 +57,9 @@ grammar Rubyish::Grammar is HLL::Grammar {
57
57
:my $ * CLASS_BLOCK := $ * CUR_BLOCK ;
58
58
:my $ * IN_TEMPLATE := 0;
59
59
:my $ * IN_PARENS := 0;
60
- :my % * SYM ;
61
- :my % * CLASS_SYMS ;
60
+ :my % * SYM ; # symbols in current scope
61
+ :my % * SYM-GBL ; # globals and package variables
62
+ :my % * SYM-CLASS ; # class-inherited methods
62
63
63
64
^ ~ $ <stmtlist >
64
65
|| <.panic (' Syntax error' )>
@@ -93,7 +94,13 @@ grammar Rubyish::Grammar is HLL::Grammar {
93
94
:my $ * CUR_BLOCK := QAST::Block . new(QAST::Stmts . new());
94
95
<operation > {
95
96
$ * DEF := ~ $ < operation > ;
96
- % * SYM {$ * DEF } := $ * IN_CLASS ?? ' method' !! ' func' ;
97
+ if $ * IN_CLASS {
98
+ % * SYM {$ * DEF } := ' method' ;
99
+ % * SYM <self > := ' var' ;
100
+ }
101
+ else {
102
+ % * SYM {$ * DEF } := ' func' ;
103
+ }
97
104
}
98
105
['(' ~ ')' <signature >? ]? <separator >?
99
106
<stmtlist >
@@ -130,7 +137,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
130
137
[ '<' <super = .ident > { inherit-syms(~ $ < super > ) } ]?
131
138
<separator >
132
139
<stmtlist > {
133
- % * CLASS_SYMS {~ $ < ident > } := % * SYM ;
140
+ % * SYM-CLASS {~ $ < ident > } := % * SYM ;
134
141
}
135
142
}
136
143
@@ -203,9 +210,12 @@ grammar Rubyish::Grammar is HLL::Grammar {
203
210
token var {
204
211
:my $ * MAYBE_DECL := 0;
205
212
\+?
206
- [$ < sigil > =[ \$ | \@ ] | <pkg = .ident >'::' <? before <[ A..Z ] >> | <!keyword > ]
207
- <ident ><! before [\!| \? | <hs >\(]>
208
- [ <? before <hs > <bind - op > { $ * MAYBE_DECL := 1 }> || <?> ]
213
+ $ < var > =[[$ < sigil > =[ \$ | \@ ] | <pkg = .ident >'::' <? before <[ A..Z ] >> | <!keyword > ] <ident >]
214
+ <! before [\!| \? | <hs >\(]>
215
+ [ <? before <hs > <bind - op > { $ * MAYBE_DECL := 1 }>
216
+ || <?{ variable(~ $ < var > ) || ~ $ < sigil > eq ' @' }>
217
+ || <!{ callable(~ $ < var > ) }> <.panic (" undeclared variable: $ < var > " )>
218
+ ]
209
219
}
210
220
211
221
token term :sym <var > { <var > }
@@ -445,13 +455,19 @@ grammar Rubyish::Grammar is HLL::Grammar {
445
455
446
456
# Functions
447
457
sub callable ($ op ) {
448
- my $ type := % * SYM {$ op } || (% builtins {$ op } && ' func' );
458
+ my $ type := % * SYM {$ op } // (% builtins {$ op } && ' func' );
449
459
450
460
$ type && ($ type eq ' func' || $ type eq ' method' );
451
461
}
452
462
463
+ sub variable ($ op ) {
464
+ my $ type := % * SYM {$ op } // % * SYM-GBL {$ op };
465
+
466
+ $ type && ($ type eq ' var' );
467
+ }
468
+
453
469
sub inherit-syms ($ class ) {
454
- if my % syms := % * CLASS_SYMS {$ class } {
470
+ if my % syms := % * SYM-CLASS {$ class } {
455
471
% * SYM {$ _ } := % syms {$ _ }
456
472
for % syms ;
457
473
}
@@ -579,8 +595,6 @@ class Rubyish::Actions is HLL::Actions {
579
595
make $ < call-args > . ast;
580
596
}
581
597
582
- my $ tmpsym := 0 ;
583
-
584
598
method term :sym <new >($/ ) {
585
599
586
600
my $ tmp-obj := ' $new-obj$' ;
@@ -672,6 +686,9 @@ class Rubyish::Actions is HLL::Actions {
672
686
673
687
if $ sigil eq ' $' || $ ns {
674
688
$ block := $ * TOP_BLOCK ;
689
+ % * SYM-GBL {$ name } := ' var' ;
690
+ % * SYM {~ $ < ident > } := ' var'
691
+ if $ ns ;
675
692
}
676
693
elsif ! $ sigil {
677
694
$ block := $ * CUR_BLOCK ;
0 commit comments