Skip to content

Commit

Permalink
Add support for parsing default arguments and storing them in the par…
Browse files Browse the repository at this point in the history
…se tree
  • Loading branch information
vidarh committed Apr 10, 2014
1 parent c9e5162 commit 7bca9f3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
22 changes: 11 additions & 11 deletions features/parser.feature
Expand Up @@ -95,17 +95,17 @@ Feature: Parser
Then the parse tree should become <tree>

Examples:
| expr | tree | notes |
| "def foo(bar=nil); end" | [:do, [:defm, :foo, [:bar], []]] | Default value for arguments |
| "def foo(bar = nil); end" | [:do, [:defm, :foo, [:bar], []]] | Default value for arguments - with whitespace |
| "def foo(bar = []); end" | [:do, [:defm, :foo, [:bar], []]] | Default value for arguments - with whitespace |
| "def foo(&bar);end " | [:do, [:defm, :foo, [[:bar,:block]], []]] | Block as named argument |
| "def foo(a = :b, c = :d);end; " | [:do, [:defm, :foo, [:a,:c], []]] | Second argument following argument with initializer |
| "def foo(a = :b, &bar);end; " | [:do, [:defm, :foo, [:a,[:bar,:block]], []]] | Second argument following argument with initializer |
| "def self.foo;end;" | [:do, [:defm, [:self,:foo], [], []]] | Class method etc. |
| "def *(other_array); end;" | [:do, [:defm, :*, [:other_array], []]] | *-Operator overloading |
| "def foo=(bar);end;" | [:do, [:defm, :foo=, [:bar], []]] | setter |
| "def == bar; end" | [:do, [:defm, :==, [:bar], []]] | Handle operator method name |
| expr | tree | notes |
| "def foo(bar=nil); end" | [:do, [:defm, :foo, [[:bar, :default, :nil]], []]] | Default value for arguments |
| "def foo(bar = nil); end" | [:do, [:defm, :foo, [[:bar, :default, :nil]], []]] | Default value for arguments - with whitespace |
| "def foo(bar = []); end" | [:do, [:defm, :foo, [[:bar, :default, [:array]]], []]] | Default value for arguments - with whitespace |
| "def foo(&bar);end " | [:do, [:defm, :foo, [[:bar,:block]], []]] | Block as named argument |
| "def foo(a = :b, c = :d);end; " | [:do, [:defm, :foo, [[:a,:default, :":b"],[:c,:default, :":d"]], []]] | Second argument following argument with initializer |
| "def foo(a = :b, &bar);end; " | [:do, [:defm, :foo, [[:a,:default, :":b"],[:bar,:block]], []]] | Second argument following argument with initializer |
| "def self.foo;end;" | [:do, [:defm, [:self,:foo], [], []]] | Class method etc. |
| "def *(other_array); end;" | [:do, [:defm, :*, [:other_array], []]] | *-Operator overloading |
| "def foo=(bar);end;" | [:do, [:defm, :foo=, [:bar], []]] | setter |
| "def == bar; end" | [:do, [:defm, :==, [:bar], []]] | Handle operator method name |

@class
Scenario Outline: Classes
Expand Down
9 changes: 6 additions & 3 deletions parser.rb
Expand Up @@ -29,14 +29,17 @@ def parse_arglist
end

nolfws
default = nil
if expect("=")
nolfws
@shunting.parse([","])
# FIXME: Store
default = @shunting.parse([","])
end

if prefix then args = [[name.to_sym, prefix == "*" ? :rest : :block]]
else args = [name.to_sym]
elsif default
args = [[name.to_sym, :default, default]]
else
args = [name.to_sym]
end
nolfws
expect(",") or return args
Expand Down
2 changes: 1 addition & 1 deletion transform.rb
Expand Up @@ -204,7 +204,7 @@ def rewrite_env_vars(exp, env)

def rewrite_let_env(exp)
exp.depth_first(:defm) do |e|
args = Set[*e[2]]
args = Set[*e[2].collect{|a| a.kind_of?(Array) ? a[0] : a}]
scopes = [args.dup] # We don't want "args" above to get updated

# We use this to assign registers
Expand Down

0 comments on commit 7bca9f3

Please sign in to comment.