Skip to content

Commit e9f1bdd

Browse files
committed
[examples/rubyish] add more comments
1 parent 5a65ffe commit e9f1bdd

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

examples/rubyish/rubyish.nqp

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class RubyishClassHOW {
1010
has $!isa;
1111
has %!methods;
1212

13+
#| define a new class
1314
method new_type(:$name!, :$isa?) {
1415
nqp::die("duplicate class definition: $name")
1516
if %CLASSES{ $name };
@@ -20,18 +21,20 @@ class RubyishClassHOW {
2021
nqp::newtype($obj, 'HashAttrStore');
2122
}
2223

24+
#| add a named method to a class
2325
method add_method($obj, $name, $code) {
2426
nqp::die("This class already has a method named " ~ $name)
2527
if nqp::existskey(%!methods, $name);
2628

2729
%!methods{$name} := $code;
2830
}
2931

32+
#| find a named method in a class or its parents
33+
#| a '^' prefix, skips the current class, starting at the parent
3034
method find_method($obj, $name) {
3135
my $method;
3236

3337
if nqp::substr($name, 0, 1) eq '^' {
34-
# '^' prefix indicates a superclass lookup
3538
$name := nqp::substr($name, 1);
3639
}
3740
else {
@@ -53,10 +56,10 @@ grammar Rubyish::Grammar is HLL::Grammar {
5356

5457
token TOP {
5558
:my $*CUR_BLOCK := QAST::Block.new(QAST::Stmts.new());
56-
:my $*TOP_BLOCK := $*CUR_BLOCK;
57-
:my $*CLASS_BLOCK := $*CUR_BLOCK;
58-
:my $*IN_TEMPLATE := 0;
59-
:my $*IN_PARENS := 0;
59+
:my $*TOP_BLOCK := $*CUR_BLOCK; # global top-level block
60+
:my $*CLASS_BLOCK := $*CUR_BLOCK; # out class block
61+
:my $*IN_TEMPLATE := 0; # true, if in a template
62+
:my $*IN_PARENS := 0; # true, if in a parentheised list (signature etc)
6063
:my %*SYM; # symbols in current scope
6164
:my %*SYM-GBL; # globals and package variables
6265
:my %*SYM-CLASS; # class-inherited methods
@@ -68,10 +71,12 @@ grammar Rubyish::Grammar is HLL::Grammar {
6871
rule separator { ';' | \n <!after continuation> }
6972
token continuation { \\ \n }
7073

74+
#| a list of statements and/or template expressions
7175
rule stmtlist {
7276
[ <stmt=.stmtish>? ] *%% [<.separator>|<stmt=.template-chunk>]
7377
}
7478

79+
#| a single statement, plus optional modifier
7580
token stmtish {:s<hs>
7681
<stmt> [ <modifier> <EXPR>]?
7782
}
@@ -80,6 +85,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
8085

8186
proto token stmt {*}
8287

88+
#| function, or method definition
8389
token stmt:sym<def> {:s
8490
:my %*inner-sym := nqp::clone(%*SYM);
8591
:my $*DEF;
@@ -95,10 +101,12 @@ grammar Rubyish::Grammar is HLL::Grammar {
95101
<operation> {
96102
$*DEF := ~$<operation>;
97103
if $*IN_CLASS {
104+
# if we're in a class, we're defining a method ...
98105
%*SYM{$*DEF} := 'method';
99106
%*SYM<self> := 'var';
100107
}
101108
else {
109+
# ... otherwise it's a function
102110
%*SYM{$*DEF} := 'func';
103111
}
104112
}
@@ -108,6 +116,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
108116

109117
rule comma { [','|'=>'] }
110118

119+
#| a signature; for a method, function or closure
111120
rule signature {
112121
:my $*IN_PARENS := 1;
113122
[ <param> | '*' <slurpy=.param> | '&' <func=.param> ] +% ','
@@ -119,6 +128,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
119128
}
120129
}
121130

131+
#| a class definition
122132
token stmt:sym<class> {
123133
:my $*IN_CLASS := 1;
124134
:my @*METHODS;
@@ -148,7 +158,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
148158
<closure>
149159
}
150160

151-
our %builtins;
161+
our %builtins; #| functions that map directly to nqp ops
152162
BEGIN {
153163
%builtins := nqp::hash(
154164
'abort', 'die',
@@ -162,13 +172,15 @@ grammar Rubyish::Grammar is HLL::Grammar {
162172
);
163173
}
164174

175+
#| a call to a function or method
165176
token term:sym<call> {
166177
<!keyword>
167178
<operation> ['(' ~ ')' <call-args=.paren-args>? <code-block>?
168179
|:s<hs> <?{callable(~$<operation>)}> <call-args>?
169180
]
170181
}
171182

183+
#| a call to the super-method (aka callsame)
172184
token term:sym<super> {
173185
'super' ['(' ~ ')' <call-args=.paren-args>? <code-block>?
174186
|:s <call-args>?
@@ -179,10 +191,12 @@ grammar Rubyish::Grammar is HLL::Grammar {
179191
]
180192
}
181193

194+
#| call to an nqp operation
182195
token term:sym<nqp-op> {
183196
'nqp::'<ident> ['(' ~ ')' <call-args=.paren-args>? | <call-args>? ]
184197
}
185198

199+
#| quoted words, e.g.: %w<a b c>
186200
token term:sym<quote-words> {
187201
\%w <?before [.]> <quote_EXPR: ':q', ':w'>
188202
}
@@ -206,6 +220,8 @@ grammar Rubyish::Grammar is HLL::Grammar {
206220
['new' \h+ <ident> | <ident> '.' 'new'] ['(' ~ ')' <call-args=.paren-args>?]?
207221
}
208222

223+
# process a variable name, e.g.: localvar $global @attr Pkg::Var
224+
# the first reference to a local or global variable must be an assignment
209225
token var {
210226
:my $*MAYBE_DECL := 0;
211227
\+?
@@ -226,26 +242,34 @@ grammar Rubyish::Grammar is HLL::Grammar {
226242
proto token value {*}
227243
token value:sym<string> {<strings>}
228244
token strings {:s<hs> <string> <strings>? }
229-
token string { <?[']> <quote_EXPR: ':q'>
230-
| <?["]> <quote_EXPR: ':qq'>
231-
| \%[ q <?before [.]> <quote_EXPR: ':q'>
232-
| Q <?before [.]> <quote_EXPR: ':qq'>
245+
token string { <?[']> <quote_EXPR: ':q'> # 'non-interpolating'
246+
| <?["]> <quote_EXPR: ':qq'> # "interpolating#{42}"
247+
| \%[ q <?before [.]> <quote_EXPR: ':q'> # %q<non-interpolating>
248+
| Q <?before [.]> <quote_EXPR: ':qq'> # %Q<interpolating#{42}>
233249
]
234250
}
235251

236252
token value:sym<heredoc> {'<<'<heredoc>}
237253

238254
proto token heredoc {*}
255+
#| non-interpolating heredoc
239256
token heredoc:sym<literal> {[$<marker>=<.ident> | \' $<marker>=<- [\' \n]>+? \' ]\n
240257
$<text>=.*?
241258
\n$<marker>$$
242259
}
243260

244-
token heredoc-line {\n? [<!before ['#{']> \N]+ | \n }
261+
#| interpolating heredoc
245262
token heredoc:sym<interp> {\" $<marker>=<- [\" \n]>+? \"\n
246263
[<text=.interp> | <text=.heredoc-line> ]*?
247264
\n$<marker>$$
248265
}
266+
token heredoc-line {\n? [<!before ['#{']> \N]+ | \n }
267+
268+
#| Interpolation
269+
token interp { '#{' ~ '}' [ [:s<hs> <stmtlist> ]
270+
|| <panic('string interpolation error')> ]
271+
}
272+
token quote_escape:sym<#{ }> { <?quotemod_check('s')> <interp> }
249273

250274
token paren-list {
251275
:my $*IN_PARENS := 1;
@@ -260,12 +284,6 @@ grammar Rubyish::Grammar is HLL::Grammar {
260284
token value:sym<true> { <sym> }
261285
token value:sym<false> { <sym> }
262286

263-
# Interpolation
264-
token interp { '#{' ~ '}' [ [:s<hs> <stmtlist> ]
265-
|| <panic('string interpolation error')> ]
266-
}
267-
token quote_escape:sym<#{ }> { <?quotemod_check('s')> <interp> }
268-
269287
# Reserved words.
270288
token keyword {
271289
[ BEGIN | class | ensure | nil | new | when

0 commit comments

Comments
 (0)