Skip to content

Commit

Permalink
Refactored local variable store.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford committed Oct 5, 2009
1 parent 8171427 commit 03affd5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
20 changes: 2 additions & 18 deletions lib/compiler-ng/ast/definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def bytecode(g)
end

class ClosedScope < Node
include CompilerNG::LocalVariables

attr_accessor :body

def new_description(g)
Expand All @@ -98,24 +100,6 @@ def children
[@body]
end

def local_count
variables.size
end

def local_names
names = Array.new local_count
variables.each_pair { |name, var| names[var.slot] = name }
names
end

def variables
@variables ||= {}
end

def allocate_slot
variables.size
end

# A nested scope is looking up a local variable. If the variable exists
# in our local variables hash, return a nested reference to it.
def search_local(name)
Expand Down
26 changes: 2 additions & 24 deletions lib/compiler-ng/ast/sends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ def bytecode(g)
end

class Iter < Node
include CompilerNG::LocalVariables

attr_accessor :parent, :arguments, :body

def initialize(line, arguments, body)
Expand All @@ -293,24 +295,6 @@ def module?
false
end

def local_count
variables.size
end

def local_names
names = Array.new local_count
variables.each_pair { |name, var| names[var.slot] = name }
names
end

def variables
@variables ||= {}
end

def allocate_slot
variables.size
end

def nest_scope(scope)
scope.parent = self
end
Expand Down Expand Up @@ -502,12 +486,6 @@ def bytecode(g)
end

class For < Iter
def initialize(line, arguments, body)
@line = line
@arguments = IterArguments.new line, arguments
@body = body || Nil.new(line)
end

def variables
@parent.variables
end
Expand Down
20 changes: 20 additions & 0 deletions lib/compiler-ng/locals.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
module Rubinius
class CompilerNG
module LocalVariables
def variables
@variables ||= {}
end

def local_count
variables.size
end

def local_names
names = Array.new local_count
variables.each_pair { |name, var| names[var.slot] = name }
names
end

def allocate_slot
variables.size
end
end

class LocalVariable
attr_reader :slot

Expand Down

0 comments on commit 03affd5

Please sign in to comment.