Class: SyntaxTree::YARV::LocalTable

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/local_table.rb

Overview

This represents every local variable associated with an instruction sequence. There are two kinds of locals: plain locals that are what you expect, and block proxy locals, which represent local variables associated with blocks that were passed into the current instruction sequence.

Defined Under Namespace

Classes: BlockLocal, Lookup, PlainLocal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocalTable

Returns a new instance of LocalTable.



43
44
45
# File 'lib/syntax_tree/yarv/local_table.rb', line 43

def initialize
  @locals = []
end

Instance Attribute Details

#localsObject (readonly)

Returns the value of attribute locals.



41
42
43
# File 'lib/syntax_tree/yarv/local_table.rb', line 41

def locals
  @locals
end

Instance Method Details

#block(name) ⇒ Object

Add a BlockLocal to the local table.



73
74
75
# File 'lib/syntax_tree/yarv/local_table.rb', line 73

def block(name)
  locals << BlockLocal.new(name) unless has?(name)
end

#empty?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/syntax_tree/yarv/local_table.rb', line 47

def empty?
  locals.empty?
end

#find(name, level = 0) ⇒ Object



51
52
53
54
# File 'lib/syntax_tree/yarv/local_table.rb', line 51

def find(name, level = 0)
  index = locals.index { |local| local.name == name }
  Lookup.new(locals[index], index, level) if index
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/syntax_tree/yarv/local_table.rb', line 56

def has?(name)
  locals.any? { |local| local.name == name }
end

#name_at(index) ⇒ Object



64
65
66
# File 'lib/syntax_tree/yarv/local_table.rb', line 64

def name_at(index)
  locals[index].name
end

#namesObject



60
61
62
# File 'lib/syntax_tree/yarv/local_table.rb', line 60

def names
  locals.map(&:name)
end

#offset(index) ⇒ Object

This is the offset from the top of the stack where this local variable lives.



84
85
86
# File 'lib/syntax_tree/yarv/local_table.rb', line 84

def offset(index)
  size - (index - 3) - 1
end

#plain(name) ⇒ Object

Add a PlainLocal to the local table.



78
79
80
# File 'lib/syntax_tree/yarv/local_table.rb', line 78

def plain(name)
  locals << PlainLocal.new(name) unless has?(name)
end

#sizeObject



68
69
70
# File 'lib/syntax_tree/yarv/local_table.rb', line 68

def size
  locals.length
end