1
- # -*- coding: iso-8859-1 -*-
2
1
use NQPHLL;
3
2
4
3
# Ruby subset extended from the `rubyish` example, as introduced in the
@@ -45,7 +44,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
45
44
token template-nibble :sym <stray-tag > { [<.tmpl - unesc >| <.tmpl - hdr >] <.panic (" Stray tag, e.g. '%>' or '<?rbi?>'" )> }
46
45
token template-nibble :sym <literal > { [<! before [<tmpl - esc >| '#{' | $ ]> . ]+ }
47
46
48
- token tmpl-hdr {'<?rbi?>' \h * \n ? <? {$ * IN_TEMPLATE := 1 }> }
47
+ token tmpl-hdr {'<?rbi?>' \h * \n ? { $ * IN_TEMPLATE := 1 } }
49
48
token tmpl-esc {\h * '<%'
50
49
[<?{ $ * IN_TEMPLATE }> || <.panic (' Template directive precedes "<?rbi?>"' )>]
51
50
}
@@ -70,7 +69,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
70
69
71
70
'def' ~ 'end' <defbody >
72
71
73
- <? {% * SYM := self . hcopy(% sym-save ); 1 }>
72
+ { % * SYM := self . hcopy(% sym-save )}
74
73
}
75
74
76
75
rule defbody {
@@ -97,13 +96,15 @@ grammar Rubyish::Grammar is HLL::Grammar {
97
96
98
97
[<sym > \h + ] ~ [\h * 'end' ] <classbody >
99
98
100
- <? {% * SYM := self . hcopy(% sym-save );1 }>
99
+ { % * SYM := self . hcopy(% sym-save );1 }
101
100
}
102
101
103
102
rule classbody {
104
103
:my $ * CUR_BLOCK := QAST::Block . new(QAST::Stmts . new());
105
104
:my $ * CLASS_BLOCK := $ * CUR_BLOCK ;
106
- <ident > <separator >
105
+
106
+ <ident > { $ * CLASS_BLOCK . name (~ $ < ident > ) }
107
+ <separator >
107
108
<stmtlist >
108
109
}
109
110
@@ -112,7 +113,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
112
113
113
114
token code-block {:s <hs >
114
115
:my $ * CUR_BLOCK := QAST::Block . new(QAST::Stmts . new());
115
- <closure >
116
+ <closure >
116
117
}
117
118
118
119
token term :sym <call > {
@@ -154,7 +155,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
154
155
[ <? before \h * '=' [\w | \h + || <.EXPR >] { $ * MAYBE_DECL := 1 }> || <?> ]
155
156
}
156
157
157
- token term :sym <var > { <var > }
158
+ token term :sym <var > { <var > }
158
159
159
160
token term :sym <value > { \+? <value > }
160
161
@@ -187,8 +188,8 @@ grammar Rubyish::Grammar is HLL::Grammar {
187
188
<EXPR > *%% <comma >
188
189
}
189
190
190
- token value :sym <integer > { \+? \ d+ }
191
- token value :sym <float > { \+? \ d* '.' \d + }
191
+ token value :sym <integer > { \d + }
192
+ token value :sym <float > { \d * '.' \d + }
192
193
token value :sym <array > {'[' ~ ']' <paren - list > }
193
194
token value :sym <hash > {'{' ~ '}' <paren - list > }
194
195
token value :sym <nil > { <sym > }
@@ -364,8 +365,8 @@ grammar Rubyish::Grammar is HLL::Grammar {
364
365
token term :sym <lambda > {:s
365
366
:my $ * CUR_BLOCK := QAST::Block . new(QAST::Stmts . new());
366
367
['lambda' <closure >
367
- | '->' <closure = .closure2 >
368
- ]
368
+ | '->' <closure = .closure2 >
369
+ ]
369
370
}
370
371
371
372
method builtin-init () {
@@ -543,9 +544,10 @@ class Rubyish::Actions is HLL::Actions {
543
544
544
545
if $ sigil eq ' @' && $ * IN_CLASS {
545
546
# instance variable, bound to self
546
- make QAST ::Var. new ( : scope(' attribute' ),
547
+ my $ package-name := $ * CLASS_BLOCK . name ;
548
+ make QAST ::Var. new ( : name($ name ), : scope(' attribute' ),
547
549
QAST ::Var. new ( : name(' self' ), : scope(' lexical' )),
548
- QAST ::SVal. new ( : value($ name ) )
550
+ QAST ::SVal. new ( : value($ package- name ) )
549
551
);
550
552
}
551
553
else {
@@ -645,9 +647,9 @@ class Rubyish::Actions is HLL::Actions {
645
647
}
646
648
647
649
for @ params {
648
- $ * CUR_BLOCK [0 ]. push ($ _ );
649
- $ * CUR_BLOCK . symbol($ _ . name , : declared(1 ));
650
- }
650
+ $ * CUR_BLOCK [0 ]. push ($ _ );
651
+ $ * CUR_BLOCK . symbol($ _ . name , : declared(1 ));
652
+ }
651
653
}
652
654
653
655
method stmt :sym <class >($/ ) {
@@ -656,14 +658,18 @@ class Rubyish::Actions is HLL::Actions {
656
658
# Generate code to create the class.
657
659
my $ class_stmts := QAST ::Stmts. new ( $ body_block );
658
660
my $ ins_name := ' ::' ~ $ < classbody >< ident > ;
659
- $ class_stmts . push (QAST ::Op. new (
660
- : op(' bind' ),
661
- QAST ::Var. new ( : name($ ins_name ), : scope(' lexical' ), : decl(' var' ) ),
662
- QAST ::Op. new (
661
+
662
+ my $ new_type := QAST ::Op. new (
663
663
: op(' callmethod' ), : name(' new_type' ),
664
664
QAST ::WVal. new ( : value(RubyishClassHOW) ),
665
- QAST ::SVal. new ( : value(~ $ < classbody >< ident > ), : named(' name' ) ) )
666
- ));
665
+ QAST ::SVal. new ( : value(~ $ < classbody >< ident > ), : named(' name' ) ),
666
+ );
667
+
668
+ $ class_stmts . push (QAST ::Op. new (
669
+ : op(' bind' ),
670
+ QAST ::Var. new ( : name($ ins_name ), : scope(' lexical' ), : decl(' var' ) ),
671
+ $ new_type ,
672
+ ));
667
673
668
674
# Add methods.
669
675
my $ class_var := QAST ::Var. new ( : name($ ins_name ), : scope(' lexical' ) );
0 commit comments