Skip to content

Commit

Permalink
Clean up common code between defn and defs
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 21, 2013
1 parent c91d3f0 commit 9724209
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 54 deletions.
6 changes: 3 additions & 3 deletions lib/opal/grammar.rb

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

4 changes: 2 additions & 2 deletions lib/opal/grammar.y
Expand Up @@ -940,7 +940,7 @@ primary:
}
f_arglist bodystmt END
{
result = new_defn val[2], val[1], val[3], val[4]
result = new_def val[2], nil, val[1], val[3], val[4]
pop_scope
}
| DEF singleton dot_or_colon
Expand All @@ -954,7 +954,7 @@ primary:
}
f_arglist bodystmt END
{
result = new_defs val[5], val[1], val[4], val[6], val[7]
result = new_def val[5], val[1], val[4], val[6], val[7]
pop_scope
}
| BREAK
Expand Down
13 changes: 2 additions & 11 deletions lib/opal/grammar_helpers.rb
Expand Up @@ -31,22 +31,13 @@ def new_body(compstmt, res, els, ens)
ens ? s(:ensure, s, ens) : s
end

def new_defn(line, name, args, body)
def new_def(line, recv, name, args, body)
body = s(:block, body) if body.type != :block
scope = s(:scope, body)
body << s(:nil) if body.size == 1
scope.line = body.line
args.line = line
s = s(:defn, name.to_sym, args, scope)
s.line = line
s.end_line = @line
s
end

def new_defs(line, recv, name, args, body)
scope = s(:scope, body)
scope.line = body.line
s = s(:defs, recv, name.to_sym, args, scope)
s = s(:def, recv, name.to_sym, args, scope)
s.line = line
s.end_line = @line
s
Expand Down
18 changes: 3 additions & 15 deletions lib/opal/parser.rb
Expand Up @@ -1074,21 +1074,9 @@ def process_undef(sexp, level)
f("delete #{ @scope.proto }#{ mid_to_jsid sexp[0][1].to_s }", sexp)
end

# s(:defn, mid, s(:args), s(:scope))
def process_defn(sexp, level)
mid, args, stmts = sexp

js_def nil, mid, args, stmts, sexp.line, sexp.end_line, sexp, level
end

# s(:defs, recv, mid, s(:args), s(:scope))
def process_defs(sexp, level)
recv, mid, args, stmts = sexp

js_def recv, mid, args, stmts, sexp.line, sexp.end_line, sexp, level
end

def js_def(recvr, mid, args, stmts, line, end_line, sexp, level)
# s(:def, recv, mid, s(:args), s(:scope))
def process_def(sexp, level)
recvr, mid, args, stmts = sexp
jsid = mid_to_jsid mid.to_s

recvr = process(recvr) if recvr
Expand Down
34 changes: 17 additions & 17 deletions spec/parser/def_spec.rb
Expand Up @@ -2,8 +2,8 @@

describe "The def keyword" do
describe "for normal definitions" do
it "should return s(:defn)" do
opal_parse("def a; end").should == [:defn, :a, [:args], [:scope, [:block, [:nil]]]]
it "should return s(:def)" do
opal_parse("def a; end").should == [:def, nil, :a, [:args], [:scope, [:block, [:nil]]]]
end

it "adds s(:nil) on an empty body" do
Expand All @@ -12,49 +12,49 @@
end

describe "for singleton definitions" do
it "should return s(:defs)" do
opal_parse("def self.a; end").should == [:defs, [:self], :a, [:args], [:scope, [:block]]]
it "should return s(:def)" do
opal_parse("def self.a; end").should == [:def, [:self], :a, [:args], [:scope, [:block, [:nil]]]]
end

it "does not add s(:nil) on an empty body" do
opal_parse("def self.foo; end").last.should == [:scope, [:block]]
it "adds s(:nil) on an empty body" do
opal_parse("def self.foo; end").last.should == [:scope, [:block, [:nil]]]
end
end

describe "with normal args" do
it "should list all args" do
opal_parse("def foo(a); end")[2].should == [:args, :a]
opal_parse("def foo(a, b); end")[2].should == [:args, :a, :b]
opal_parse("def foo(a, b, c); end")[2].should == [:args, :a, :b, :c]
opal_parse("def foo(a); end")[3].should == [:args, :a]
opal_parse("def foo(a, b); end")[3].should == [:args, :a, :b]
opal_parse("def foo(a, b, c); end")[3].should == [:args, :a, :b, :c]
end
end

describe "with opt args" do
it "should list all opt args as well as block with each lasgn" do
opal_parse("def foo(a = 1); end")[2].should == [:args, :a, [:block, [:lasgn, :a, [:int, 1]]]]
opal_parse("def foo(a = 1, b = 2); end")[2].should == [:args, :a, :b, [:block, [:lasgn, :a, [:int, 1]], [:lasgn, :b, [:int, 2]]]]
opal_parse("def foo(a = 1); end")[3].should == [:args, :a, [:block, [:lasgn, :a, [:int, 1]]]]
opal_parse("def foo(a = 1, b = 2); end")[3].should == [:args, :a, :b, [:block, [:lasgn, :a, [:int, 1]], [:lasgn, :b, [:int, 2]]]]
end

it "should list lasgn block after all other args" do
opal_parse("def foo(a, b = 1); end")[2].should == [:args, :a, :b, [:block, [:lasgn, :b, [:int, 1]]]]
opal_parse("def foo(b = 1, *c); end")[2].should == [:args, :b, :"*c", [:block, [:lasgn, :b, [:int, 1]]]]
opal_parse("def foo(b = 1, &block); end")[2].should == [:args, :b, :"&block", [:block, [:lasgn, :b, [:int, 1]]]]
opal_parse("def foo(a, b = 1); end")[3].should == [:args, :a, :b, [:block, [:lasgn, :b, [:int, 1]]]]
opal_parse("def foo(b = 1, *c); end")[3].should == [:args, :b, :"*c", [:block, [:lasgn, :b, [:int, 1]]]]
opal_parse("def foo(b = 1, &block); end")[3].should == [:args, :b, :"&block", [:block, [:lasgn, :b, [:int, 1]]]]
end
end

describe "with rest args" do
it "should list rest args in place as a symbol with '*' prefix" do
opal_parse("def foo(*a); end")[2].should == [:args, :"*a"]
opal_parse("def foo(*a); end")[3].should == [:args, :"*a"]
end

it "uses '*' as an arg name for rest args without a name" do
opal_parse("def foo(*); end")[2].should == [:args, :"*"]
opal_parse("def foo(*); end")[3].should == [:args, :"*"]
end
end

describe "with block arg" do
it "should list block argument with the '&' prefix" do
opal_parse("def foo(&a); end")[2].should == [:args, :"&a"]
opal_parse("def foo(&a); end")[3].should == [:args, :"&a"]
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/parser/lvar_spec.rb
Expand Up @@ -15,24 +15,24 @@

describe "inside a def" do
it "should created by a norm arg" do
opal_parse("def a(b); b; end").should == [:defn, :a, [:args, :b], [:scope, [:block, [:lvar, :b]]]]
opal_parse("def a(b, c); c; end").should == [:defn, :a, [:args, :b, :c], [:scope, [:block, [:lvar, :c]]]]
opal_parse("def a(b); b; end").should == [:def, nil, :a, [:args, :b], [:scope, [:block, [:lvar, :b]]]]
opal_parse("def a(b, c); c; end").should == [:def, nil, :a, [:args, :b, :c], [:scope, [:block, [:lvar, :c]]]]
end

it "should be created by an opt arg" do
opal_parse("def a(b=10); b; end").should == [:defn, :a, [:args, :b, [:block, [:lasgn, :b, [:int, 10]]]], [:scope, [:block, [:lvar, :b]]]]
opal_parse("def a(b=10); b; end").should == [:def, nil, :a, [:args, :b, [:block, [:lasgn, :b, [:int, 10]]]], [:scope, [:block, [:lvar, :b]]]]
end

it "should be created by a rest arg" do
opal_parse("def a(*b); b; end").should == [:defn, :a, [:args, :"*b"], [:scope, [:block, [:lvar, :b]]]]
opal_parse("def a(*b); b; end").should == [:def, nil, :a, [:args, :"*b"], [:scope, [:block, [:lvar, :b]]]]
end

it "should be created by a block arg" do
opal_parse("def a(&b); b; end").should == [:defn, :a, [:args, :"&b"], [:scope, [:block, [:lvar, :b]]]]
opal_parse("def a(&b); b; end").should == [:def, nil, :a, [:args, :"&b"], [:scope, [:block, [:lvar, :b]]]]
end

it "should not be created from locals outside the def" do
opal_parse("a = 10; def b; a; end").should == [:block, [:lasgn, :a, [:int, 10]], [:defn, :b, [:args], [:scope, [:block, [:call, nil, :a, [:arglist]]]]]]
opal_parse("a = 10; def b; a; end").should == [:block, [:lasgn, :a, [:int, 10]], [:def, nil, :b, [:args], [:scope, [:block, [:call, nil, :a, [:arglist]]]]]]
end
end
end

0 comments on commit 9724209

Please sign in to comment.