Skip to content

Commit f931afd

Browse files
committedNov 21, 2013
Fix block args merging into sexps
1 parent 5e0f9c7 commit f931afd

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed
 

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ class ReturnNode < Base
148148
children :value
149149

150150
def return_val
151-
expr_or_nil value
151+
if value.nil?
152+
expr(s(:nil))
153+
elsif children.size > 1
154+
expr(s(:array, *children))
155+
else
156+
expr(value)
157+
end
152158
end
153159

154160
def return_in_iter?

Diff for: ‎lib/opal/parser.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def value(tok)
5656
end
5757

5858
def source(tok)
59-
tok[1]
59+
tok ? tok[1] : nil
6060
end
6161

6262
def new_nil(tok)
@@ -154,19 +154,18 @@ def new_compstmt(block)
154154

155155
def new_body(compstmt, res, els, ens)
156156
s = compstmt || s(:block)
157-
s.line = compstmt.line if compstmt
158157

159158
if res
160-
s = s(:rescue, s)
159+
s = s(:rescue, [s])
161160
res.each { |r| s << r }
162161
s << els if els
163162
end
164163

165-
ens ? s(:ensure, s, ens) : s
164+
ens ? s(:ensure, [s, ens]) : s
166165
end
167166

168167
def new_def(kw, recv, name, args, body, end_tok)
169-
body = s(:block, body) if body.type != :block
168+
body = s(:block, [body]) if body.type != :block
170169
body << s(:nil) if body.size == 1
171170

172171
s(:def, [recv, value(name).to_sym, args, body], source(kw))
@@ -276,13 +275,13 @@ def new_block_args(norm, opt, rest, block)
276275

277276
if opt
278277
opt[1..-1].each do |_opt|
279-
res << s(:lasgn, _opt[1])
278+
res << s(:lasgn, [_opt[1]])
280279
end
281280
end
282281

283282
if rest
284283
r = rest.to_s[1..-1].to_sym
285-
res << s(:splat, s(:lasgn, r))
284+
res << new_splat(nil, s(:lasgn, [r]))
286285
scope.add_local r
287286
end
288287

@@ -330,6 +329,10 @@ def add_block_pass(arglist, block)
330329
arglist
331330
end
332331

332+
def new_block_pass(amper_tok, val)
333+
s(:block_pass, [val], source(amper_tok))
334+
end
335+
333336
def new_splat(tok, value)
334337
s(:splat, [value], source(tok))
335338
end

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

+12-13
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

+12-13
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ rule
7373

7474
bodystmt: compstmt opt_rescue opt_else opt_ensure
7575
{
76-
result = new_body val[0], val[1], val[2], val[3]
76+
result = new_body(val[0], val[1], val[2], val[3])
7777
}
7878

7979
compstmt: stmts opt_terms
@@ -198,9 +198,7 @@ rule
198198
| block_command
199199
| kRETURN call_args
200200
{
201-
args = val[1]
202-
args = args[1] if args.size == 2
203-
result = new_return(val[0], args)
201+
result = new_return(val[0], val[1])
204202
}
205203
| kBREAK call_args
206204
{
@@ -627,7 +625,7 @@ rule
627625

628626
call_args: command
629627
{
630-
result = s(:array, val[0])
628+
result = [val[0]]
631629
}
632630
| args opt_block_arg
633631
{
@@ -636,17 +634,17 @@ rule
636634
}
637635
| assocs opt_block_arg
638636
{
639-
result = s(:arglist, s(:hash, *val[0]))
637+
result = [new_hash(nil, val[0], nil)]
640638
add_block_pass result, val[1]
641639
}
642640
| args tCOMMA assocs opt_block_arg
643641
{
644642
result = val[0]
645-
result << s(:hash, *val[2])
643+
result << new_hash(nil, val[2], nil)
646644
}
647645
| block_arg
648646
{
649-
result = s(:arglist)
647+
result = []
650648
add_block_pass result, val[0]
651649
}
652650

@@ -674,7 +672,7 @@ rule
674672

675673
block_arg: tAMPER arg_value
676674
{
677-
result = s(:block_pass, val[1])
675+
result = new_block_pass(val[0], val[1])
678676
}
679677

680678
opt_block_arg: tCOMMA block_arg
@@ -985,7 +983,7 @@ rule
985983

986984
f_block_optarg: f_block_opt
987985
{
988-
result = s(:block, val[0])
986+
result = s(:block, [val[0]])
989987
}
990988
| f_block_optarg tCOMMA f_block_opt
991989
{
@@ -995,7 +993,8 @@ rule
995993

996994
f_block_opt: tIDENTIFIER tEQL primary_value
997995
{
998-
result = new_assign new_assignable(s(:identifier, val[0].intern)), val[2]
996+
result = new_assign(new_assignable(new_ident(
997+
val[0])), val[1], val[2])
999998
}
1000999

10011000
opt_block_var: none
@@ -1157,7 +1156,7 @@ opt_block_args_tail: tCOMMA block_args_tail
11571156
{
11581157
exc = val[1] || s(:array)
11591158
exc << new_assign(val[2], s(:gvar, '$!'.intern)) if val[2]
1160-
result = [s(:resbody, exc, val[4])]
1159+
result = [s(:resbody, [exc, val[4]])]
11611160
result.push val[5].first if val[5]
11621161
}
11631162
| # none
@@ -1167,7 +1166,7 @@ opt_block_args_tail: tCOMMA block_args_tail
11671166

11681167
exc_list: arg_value
11691168
{
1170-
result = s(:array, val[0])
1169+
result = s(:array, [val[0]])
11711170
}
11721171
| mrhs
11731172
| none

0 commit comments

Comments
 (0)
Please sign in to comment.