Skip to content

Commit b8d3495

Browse files
committed
[examples/rubyish] added "undeclared variable" error message
1 parent 2e21ba6 commit b8d3495

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

examples/rubyish/rubyish.nqp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ grammar Rubyish::Grammar is HLL::Grammar {
5757
:my $*CLASS_BLOCK := $*CUR_BLOCK;
5858
:my $*IN_TEMPLATE := 0;
5959
: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
6263

6364
^ ~ $ <stmtlist>
6465
|| <.panic('Syntax error')>
@@ -93,7 +94,13 @@ grammar Rubyish::Grammar is HLL::Grammar {
9394
:my $*CUR_BLOCK := QAST::Block.new(QAST::Stmts.new());
9495
<operation> {
9596
$*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+
}
97104
}
98105
['(' ~ ')' <signature>?]? <separator>?
99106
<stmtlist>
@@ -130,7 +137,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
130137
[ '<' <super=.ident> { inherit-syms(~$<super>) } ]?
131138
<separator>
132139
<stmtlist> {
133-
%*CLASS_SYMS{~$<ident>} := %*SYM;
140+
%*SYM-CLASS{~$<ident>} := %*SYM;
134141
}
135142
}
136143

@@ -203,9 +210,12 @@ grammar Rubyish::Grammar is HLL::Grammar {
203210
token var {
204211
:my $*MAYBE_DECL := 0;
205212
\+?
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+
]
209219
}
210220

211221
token term:sym<var> { <var> }
@@ -445,13 +455,19 @@ grammar Rubyish::Grammar is HLL::Grammar {
445455

446456
# Functions
447457
sub callable($op) {
448-
my $type := %*SYM{$op} || (%builtins{$op} && 'func');
458+
my $type := %*SYM{$op} // (%builtins{$op} && 'func');
449459

450460
$type && ($type eq 'func' || $type eq 'method');
451461
}
452462

463+
sub variable($op) {
464+
my $type := %*SYM{$op} // %*SYM-GBL{$op};
465+
466+
$type && ($type eq 'var');
467+
}
468+
453469
sub inherit-syms($class) {
454-
if my %syms := %*CLASS_SYMS{$class} {
470+
if my %syms := %*SYM-CLASS{$class} {
455471
%*SYM{$_} := %syms{$_}
456472
for %syms;
457473
}
@@ -579,8 +595,6 @@ class Rubyish::Actions is HLL::Actions {
579595
make $<call-args>.ast;
580596
}
581597

582-
my $tmpsym := 0;
583-
584598
method term:sym<new>($/) {
585599

586600
my $tmp-obj := '$new-obj$';
@@ -672,6 +686,9 @@ class Rubyish::Actions is HLL::Actions {
672686

673687
if $sigil eq '$' || $ns {
674688
$block := $*TOP_BLOCK;
689+
%*SYM-GBL{$name} := 'var';
690+
%*SYM{~$<ident>} := 'var'
691+
if $ns;
675692
}
676693
elsif !$sigil {
677694
$block := $*CUR_BLOCK;

examples/rubyish/t/scoping.t

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class DerivedClass < BaseClass
2121
end
2222

2323
class DisjointClass
24+
Y = 42;
2425
def set_inst1(v); @i1 = v; end
2526
def set_inst2(v); @i2 = v; end
2627
def get_inst1; @i1; end

0 commit comments

Comments
 (0)