Skip to content

Commit

Permalink
Fix block args merging into sexps
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Nov 21, 2013
1 parent 5e0f9c7 commit f931afd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
8 changes: 7 additions & 1 deletion lib/opal/nodes/logic.rb
Expand Up @@ -148,7 +148,13 @@ class ReturnNode < Base
children :value

def return_val
expr_or_nil value
if value.nil?
expr(s(:nil))
elsif children.size > 1
expr(s(:array, *children))
else
expr(value)
end
end

def return_in_iter?
Expand Down
17 changes: 10 additions & 7 deletions lib/opal/parser.rb
Expand Up @@ -56,7 +56,7 @@ def value(tok)
end

def source(tok)
tok[1]
tok ? tok[1] : nil
end

def new_nil(tok)
Expand Down Expand Up @@ -154,19 +154,18 @@ def new_compstmt(block)

def new_body(compstmt, res, els, ens)
s = compstmt || s(:block)
s.line = compstmt.line if compstmt

if res
s = s(:rescue, s)
s = s(:rescue, [s])
res.each { |r| s << r }
s << els if els
end

ens ? s(:ensure, s, ens) : s
ens ? s(:ensure, [s, ens]) : s
end

def new_def(kw, recv, name, args, body, end_tok)
body = s(:block, body) if body.type != :block
body = s(:block, [body]) if body.type != :block
body << s(:nil) if body.size == 1

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

if opt
opt[1..-1].each do |_opt|
res << s(:lasgn, _opt[1])
res << s(:lasgn, [_opt[1]])
end
end

if rest
r = rest.to_s[1..-1].to_sym
res << s(:splat, s(:lasgn, r))
res << new_splat(nil, s(:lasgn, [r]))
scope.add_local r
end

Expand Down Expand Up @@ -330,6 +329,10 @@ def add_block_pass(arglist, block)
arglist
end

def new_block_pass(amper_tok, val)
s(:block_pass, [val], source(amper_tok))
end

def new_splat(tok, value)
s(:splat, [value], source(tok))
end
Expand Down
25 changes: 12 additions & 13 deletions lib/opal/parser/grammar.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions lib/opal/parser/grammar.y
Expand Up @@ -73,7 +73,7 @@ rule

bodystmt: compstmt opt_rescue opt_else opt_ensure
{
result = new_body val[0], val[1], val[2], val[3]
result = new_body(val[0], val[1], val[2], val[3])
}

compstmt: stmts opt_terms
Expand Down Expand Up @@ -198,9 +198,7 @@ rule
| block_command
| kRETURN call_args
{
args = val[1]
args = args[1] if args.size == 2
result = new_return(val[0], args)
result = new_return(val[0], val[1])
}
| kBREAK call_args
{
Expand Down Expand Up @@ -627,7 +625,7 @@ rule

call_args: command
{
result = s(:array, val[0])
result = [val[0]]
}
| args opt_block_arg
{
Expand All @@ -636,17 +634,17 @@ rule
}
| assocs opt_block_arg
{
result = s(:arglist, s(:hash, *val[0]))
result = [new_hash(nil, val[0], nil)]
add_block_pass result, val[1]
}
| args tCOMMA assocs opt_block_arg
{
result = val[0]
result << s(:hash, *val[2])
result << new_hash(nil, val[2], nil)
}
| block_arg
{
result = s(:arglist)
result = []
add_block_pass result, val[0]
}

Expand Down Expand Up @@ -674,7 +672,7 @@ rule

block_arg: tAMPER arg_value
{
result = s(:block_pass, val[1])
result = new_block_pass(val[0], val[1])
}

opt_block_arg: tCOMMA block_arg
Expand Down Expand Up @@ -985,7 +983,7 @@ rule

f_block_optarg: f_block_opt
{
result = s(:block, val[0])
result = s(:block, [val[0]])
}
| f_block_optarg tCOMMA f_block_opt
{
Expand All @@ -995,7 +993,8 @@ rule

f_block_opt: tIDENTIFIER tEQL primary_value
{
result = new_assign new_assignable(s(:identifier, val[0].intern)), val[2]
result = new_assign(new_assignable(new_ident(
val[0])), val[1], val[2])
}

opt_block_var: none
Expand Down Expand Up @@ -1157,7 +1156,7 @@ opt_block_args_tail: tCOMMA block_args_tail
{
exc = val[1] || s(:array)
exc << new_assign(val[2], s(:gvar, '$!'.intern)) if val[2]
result = [s(:resbody, exc, val[4])]
result = [s(:resbody, [exc, val[4]])]
result.push val[5].first if val[5]
}
| # none
Expand All @@ -1167,7 +1166,7 @@ opt_block_args_tail: tCOMMA block_args_tail

exc_list: arg_value
{
result = s(:array, val[0])
result = s(:array, [val[0]])
}
| mrhs
| none
Expand Down

0 comments on commit f931afd

Please sign in to comment.