Skip to content

Commit 5e0f9c7

Browse files
committedNov 21, 2013
Handle a lot more sexp constructions
1 parent 08db404 commit 5e0f9c7

File tree

5 files changed

+1901
-1884
lines changed

5 files changed

+1901
-1884
lines changed
 

Diff for: ‎lib/opal/nodes/module.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def compile
3131
end
3232

3333
def name_and_base
34-
if Symbol === cid or String === cid
35-
[cid.to_s, 'self']
34+
if cid.type == :const
35+
[cid[1].to_s, 'self']
3636
elsif cid.type == :colon2
3737
[cid[2].to_s, expr(cid[1])]
3838
elsif cid.type == :colon3

Diff for: ‎lib/opal/parser.rb

+96-38
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ def new_const(tok)
111111
s(:const, [value(tok).to_sym], source(tok))
112112
end
113113

114+
def new_colon2(lhs, tok, name)
115+
s(:colon2, [lhs, value(name).to_sym], source(tok))
116+
end
117+
118+
def new_colon3(tok, name)
119+
s(:colon3, [value(name).to_sym], source(name))
120+
end
121+
114122
def new_sym(tok)
115123
s(:sym, [value(tok).to_sym], source(tok))
116124
end
@@ -119,6 +127,14 @@ def new_alias(kw, new, old)
119127
s(:alias, [new, old], source(kw))
120128
end
121129

130+
def new_break(kw, args=[])
131+
s(:break, args, source(kw))
132+
end
133+
134+
def new_return(kw, args=[])
135+
s(:return, args, source(kw))
136+
end
137+
122138
def new_block(stmt = nil)
123139
s = s(:block, [], nil)
124140
s << stmt if stmt
@@ -149,32 +165,29 @@ def new_body(compstmt, res, els, ens)
149165
ens ? s(:ensure, s, ens) : s
150166
end
151167

152-
def new_def(line, recv, name, args, body)
168+
def new_def(kw, recv, name, args, body, end_tok)
153169
body = s(:block, body) if body.type != :block
154170
body << s(:nil) if body.size == 1
155-
args.line = line
156-
s = s(:def, recv, name.to_sym, args, body)
157-
s.line = line
158-
s.end_line = @lexer.line
159-
s
171+
172+
s(:def, [recv, value(name).to_sym, args, body], source(kw))
160173
end
161174

162175
def new_class(start, path, sup, body, endt)
163176
s(:class, [path, sup, body], source(start))
164177
end
165178

166-
def new_sclass(expr, body)
167-
s(:sclass, expr, body)
179+
def new_sclass(kw, expr, body, end_tok)
180+
s(:sclass, [expr, body], source(kw))
168181
end
169182

170-
def new_module(path, body)
171-
s(:module, path, body)
183+
def new_module(kw, path, body, end_tok)
184+
s(:module, [path, body], source(kw))
172185
end
173186

174187
def new_iter(args, body)
188+
args ||= [nil]
175189
s = s(:iter, args)
176190
s << body if body
177-
s.end_line = @lexer.line
178191
s
179192
end
180193

@@ -199,8 +212,24 @@ def new_array(start, args, finish)
199212
s(:array, args, source(start))
200213
end
201214

215+
def new_hash(open, assocs, close)
216+
s(:hash, [*assocs], source(open))
217+
end
218+
219+
def new_not(kw, expr)
220+
s(:not, [expr], source(kw))
221+
end
222+
223+
def new_paren(open, expr, close)
224+
if expr.nil?
225+
s(:paren, [s(:nil, [], source(open))], source(open))
226+
else
227+
s(:paren, [expr], source(open))
228+
end
229+
end
230+
202231
def new_args(norm, opt, rest, block)
203-
res = s(:args)
232+
res = s(:args, [])
204233

205234
if norm
206235
norm.each do |arg|
@@ -232,13 +261,13 @@ def new_args(norm, opt, rest, block)
232261
end
233262

234263
def new_block_args(norm, opt, rest, block)
235-
res = s(:array)
264+
res = []
236265

237266
if norm
238267
norm.each do |arg|
239268
if arg.is_a? Symbol
240269
scope.add_local arg
241-
res << s(:lasgn, arg)
270+
res << s(:lasgn, [arg])
242271
else
243272
res << arg
244273
end
@@ -265,26 +294,48 @@ def new_block_args(norm, opt, rest, block)
265294

266295
res << opt if opt
267296

268-
args = res.size == 2 && norm ? res[1] : s(:masgn, res)
269-
270-
if args.type == :array
271-
s(:masgn, args)
272-
else
273-
args
274-
end
297+
res.size == 1 && norm ? [res[0]] : [s(:masgn, [s(:array, res)])]
275298
end
276299

277300
def new_call(recv, meth, args = [])
278301
s(:call, [recv, value(meth).to_sym, s(:arglist, args)], source(meth))
279302
end
280303

304+
def new_binary_call(recv, meth, arg)
305+
new_call(recv, meth, [arg])
306+
end
307+
308+
def new_unary_call(op, recv)
309+
new_call(recv, op, [])
310+
end
311+
312+
def new_and(lhs, tok, rhs)
313+
s(:and, [lhs, rhs], source(tok))
314+
end
315+
316+
def new_or(lhs, tok, rhs)
317+
s(:or, [lhs, rhs], source(tok))
318+
end
319+
320+
def new_irange(beg, op, finish)
321+
s(:irange, [beg, finish], source(op))
322+
end
323+
324+
def new_erange(beg, op, finish)
325+
s(:erange, [beg, finish], source(op))
326+
end
327+
281328
def add_block_pass(arglist, block)
282329
arglist << block if block
283330
arglist
284331
end
285332

333+
def new_splat(tok, value)
334+
s(:splat, [value], source(tok))
335+
end
336+
286337
def new_op_asgn(op, lhs, rhs)
287-
case op
338+
case value(op).to_sym
288339
when :"||"
289340
result = s(:op_asgn_or, new_gettable(lhs))
290341
result << (lhs << rhs)
@@ -301,6 +352,10 @@ def new_op_asgn(op, lhs, rhs)
301352
result
302353
end
303354

355+
def new_op_asgn1(lhs, args, op, rhs)
356+
s(:op_asgn1, [lhs, args, op, rhs], source(op))
357+
end
358+
304359
def new_assign(lhs, tok, rhs)
305360
case lhs.type
306361
when :iasgn, :cdecl, :lasgn, :gasgn, :cvdecl, :nth_ref
@@ -345,13 +400,13 @@ def new_assignable(ref)
345400
def new_gettable(ref)
346401
res = case ref.type
347402
when :lasgn
348-
s(:lvar, ref[1])
403+
s(:lvar, [ref[1]])
349404
when :iasgn
350-
s(:ivar, ref[1])
405+
s(:ivar, [ref[1]])
351406
when :gasgn
352-
s(:gvar, ref[1])
407+
s(:gvar, [ref[1]])
353408
when :cvdecl
354-
s(:cvar, ref[1])
409+
s(:cvar, [ref[1]])
355410
else
356411
raise "Bad new_gettable ref: #{ref.type}"
357412
end
@@ -385,14 +440,9 @@ def new_var_ref(ref)
385440
end
386441
end
387442

388-
def new_super(args)
389-
args = (args || s(:arglist))
390-
391-
if args.type == :array
392-
args.type = :arglist
393-
end
394-
395-
s(:super, args)
443+
def new_super(kw, args)
444+
args ||= []
445+
s(:super, args, source(kw))
396446
end
397447

398448
def new_yield(args)
@@ -424,9 +474,13 @@ def new_dsym(str)
424474
str
425475
end
426476

477+
def new_evstr(str)
478+
s(:evstr, [str])
479+
end
480+
427481
def new_str(str)
428482
# cover empty strings
429-
return s(:str, "") unless str
483+
return s(:str, [""]) unless str
430484
# catch s(:str, "", other_str)
431485
if str.size == 3 and str[1] == "" and str.type == :str
432486
return str[2]
@@ -436,7 +490,7 @@ def new_str(str)
436490
str
437491
# top level evstr should be a dstr
438492
elsif str.type == :evstr
439-
s(:dstr, "", str)
493+
s(:dstr, ["", str])
440494
else
441495
str
442496
end
@@ -460,14 +514,18 @@ def str_append(str, str2)
460514
return str unless str2
461515

462516
if str.type == :evstr
463-
str = s(:dstr, "", str)
517+
str = s(:dstr, ["", str])
464518
elsif str.type == :str
465-
str = s(:dstr, str[1])
519+
str = s(:dstr, [str[1]])
466520
else
467521
#puts str.type
468522
end
469523
str << str2
470524
str
471525
end
526+
527+
def new_str_content(tok)
528+
s(:str, [value(tok)], source(tok))
529+
end
472530
end
473531
end

Diff for: ‎lib/opal/parser/grammar.rb

+1,704-1,728
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎lib/opal/parser/grammar.y

+98-115
Large diffs are not rendered by default.

Diff for: ‎lib/opal/parser/lexer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ def yylex
960960
end
961961

962962
@lex_state = :expr_beg
963-
return [sign, sign]
963+
return [sign, matched]
964964

965965
elsif scan(/\?/)
966966
if end?

0 commit comments

Comments
 (0)
Please sign in to comment.