Skip to content

Commit

Permalink
Fixed svalue, splat bytecode.
Browse files Browse the repository at this point in the history
Some compiler specs fail, but they are broken and need to be
fixed. As a consequence of wrapping splat nodes in array nodes
in the rewriter, literals of the form [*[something]] were
causing the bytecode sequence to be emitted:

  make_array 0     # yup, create an empty array
  push something   # ie push each elt of the literal array
  make_array size  # size of the inner literal array
  send :+          # put all the inner elts in the empty array

This is totally unnecessary since

  [] + [something] == [something]

The specs will be updated once the compiler changes are made
active.
  • Loading branch information
Brian Ford committed Aug 12, 2009
1 parent df65c19 commit 5af4228
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
36 changes: 27 additions & 9 deletions lib/melbourne/nodes.rb
Expand Up @@ -554,11 +554,12 @@ def bytecode(g)
end
end

class SplatArgument < Node
# TODO: rename to Splat and fix Splat superclass
class SplatValue < Node
attr_accessor :value

def self.from(p, value)
node = SplatArgument.new p.compiler
node = SplatValue.new p.compiler
node.value = value
node
end
Expand All @@ -578,12 +579,12 @@ class ActualArguments < Node

def self.from(p, arguments)
node = ActualArguments.new p.compiler
if arguments.kind_of? Splat
node.splat = SplatArgument.from p, arguments.value
if arguments.kind_of? SplatValue
node.splat = arguments
node.array = []
elsif arguments.kind_of? ConcatArgs
node.array = arguments.array.body
node.splat = SplatArgument.from p, arguments.rest
node.splat = SplatValue.from p, arguments.rest
else
node.array = arguments.body
end
Expand Down Expand Up @@ -1996,7 +1997,7 @@ def bytecode(g)
class PushArgs < DynamicArguments
def self.from(p, splat, value)
node = PushArgs.new p.compiler
node.array = splat.child
node.array = splat.value
node.item = value
node
end
Expand Down Expand Up @@ -2302,11 +2303,13 @@ def self.from(p, expr)
node
end

def children
[@value]
end

def bytecode(g)
g.make_array 0
@value.bytecode(g)
g.cast_array
g.send :+, 1
g.cast_array unless @value.kind_of? ArrayLiteral
end
end

Expand Down Expand Up @@ -2348,6 +2351,21 @@ def self.from(p, expr)
end

def bytecode(g)
@value.bytecode(g)
if @value.kind_of? SplatValue
done = g.new_label

g.dup
g.send :size, 0
g.push 1
g.send :>, 1
g.git done

g.push 0
g.send :at, 1

done.set!
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/melbourne/processor.rb
Expand Up @@ -379,7 +379,7 @@ def process_self(line)
end

def process_splat(line, expr)
AST::Splat.from self, expr
AST::SplatValue.from self, expr
end

def process_str(line, str)
Expand Down

0 comments on commit 5af4228

Please sign in to comment.