Class: Oryx::SymbolTable
- Inherits:
-
Object
- Object
- Oryx::SymbolTable
- Defined in:
- lib/oryx/symbol_table.rb
Instance Method Summary collapse
- #current_scope ⇒ Object
- #enter_scope ⇒ Object
- #exit_scope ⇒ Object
-
#initialize ⇒ SymbolTable
constructor
A new instance of SymbolTable.
- #insert(variable, value = nil) ⇒ Object
- #lookup(variable) ⇒ Object
- #update(variable, value = nil) ⇒ Object
Constructor Details
#initialize ⇒ SymbolTable
Returns a new instance of SymbolTable.
5 6 7 |
# File 'lib/oryx/symbol_table.rb', line 5 def initialize @values = [{}] end |
Instance Method Details
#current_scope ⇒ Object
33 34 35 |
# File 'lib/oryx/symbol_table.rb', line 33 def current_scope values.length - 1 end |
#enter_scope ⇒ Object
9 10 11 |
# File 'lib/oryx/symbol_table.rb', line 9 def enter_scope values.push Hash.new end |
#exit_scope ⇒ Object
13 14 15 16 |
# File 'lib/oryx/symbol_table.rb', line 13 def exit_scope raise SymbolTableError, "Cannot exit global scope." if current_scope == 0 values.pop end |
#insert(variable, value = nil) ⇒ Object
27 28 29 30 31 |
# File 'lib/oryx/symbol_table.rb', line 27 def insert variable, value=nil raise SymbolTableError, "Re-definition of #{variable} not allowed" if values.last.include? variable.to_sym values[current_scope][variable.to_sym] = value end |
#lookup(variable) ⇒ Object
18 19 20 |
# File 'lib/oryx/symbol_table.rb', line 18 def lookup variable return values[scope_level_of variable][variable.to_sym] end |
#update(variable, value = nil) ⇒ Object
22 23 24 25 |
# File 'lib/oryx/symbol_table.rb', line 22 def update variable, value=nil level = scope_level_of variable values[level][variable.to_sym] = value end |