Class: Oryx::SymbolTable

Inherits:
Object
  • Object
show all
Defined in:
lib/oryx/symbol_table.rb

Instance Method Summary collapse

Constructor Details

#initializeSymbolTable

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_scopeObject



33
34
35
# File 'lib/oryx/symbol_table.rb', line 33

def current_scope
  values.length - 1
end

#enter_scopeObject



9
10
11
# File 'lib/oryx/symbol_table.rb', line 9

def enter_scope
  values.push Hash.new
end

#exit_scopeObject

Raises:



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

Raises:



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