Skip to content

Commit

Permalink
Be more discriminating about arguments when checking AST transforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford committed Nov 11, 2009
1 parent f9fccb7 commit bf832d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/compiler/ast/node.rb
Expand Up @@ -24,8 +24,14 @@ def self.match_send?(node, receiver, method, name)
end

def self.match_arguments?(arguments, count)
(arguments and arguments.body.size == count) or
(arguments.nil? and count == 0)
case arguments
when ArrayLiteral
arguments.body.size == count
when nil
count == 0
else
false
end
end

def initialize(line)
Expand Down
3 changes: 2 additions & 1 deletion lib/compiler/ast/transforms.rb
Expand Up @@ -123,7 +123,8 @@ class SendFastMath < SendWithArguments
}

def self.match?(line, receiver, name, arguments, privately)
if op = Operators[name] and arguments.body.size == 1
return false unless op = Operators[name]
if match_arguments? arguments, 1
node = new line, receiver, name, arguments
node.operator = op
node
Expand Down
20 changes: 20 additions & 0 deletions spec/compiler/plugins/fastmath_spec.rb
Expand Up @@ -15,6 +15,26 @@
end
end

relates "1.+(*a)" do
compile do |g|
g.push 1
g.push :self
g.send :a, 0, true
g.cast_array
g.push :nil
g.send_with_splat :+, 0, false, false
end

compile :fastmath do |g|
g.push 1
g.push :self
g.send :a, 0, true
g.cast_array
g.push :nil
g.send_with_splat :+, 0, false, false
end
end

relates "1 - 1" do
compile do |g|
g.push 1
Expand Down

0 comments on commit bf832d0

Please sign in to comment.