Class: RubyLexer::SymbolTable
- Inherits:
-
Object
- Object
- RubyLexer::SymbolTable
- Defined in:
- lib/rubylexer/symboltable.rb
Instance Method Summary collapse
- #[](name) ⇒ Object (also: #===)
- #[]=(name, val) ⇒ Object
- #__locals_lists ⇒ Object
- #deep_copy ⇒ Object
- #end_block ⇒ Object
-
#initialize ⇒ SymbolTable
constructor
A new instance of SymbolTable.
- #names ⇒ Object
- #start_block ⇒ Object
Constructor Details
#initialize ⇒ SymbolTable
Returns a new instance of SymbolTable.
22 23 24 25 26 |
# File 'lib/rubylexer/symboltable.rb', line 22 def initialize #note: below Stack means Array (used as a stack) @symbols={} #Hash of String to Stack of Object(user-defined) @locals_lists=[{}] #Stack of Hash of String to Boolean end |
Instance Method Details
#[](name) ⇒ Object Also known as: ===
74 75 76 77 |
# File 'lib/rubylexer/symboltable.rb', line 74 def [](name) assert @locals_lists.last (stack=@symbols[name]) and stack.last end |
#[]=(name, val) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rubylexer/symboltable.rb', line 81 def []=(name, val) assert @locals_lists.last if @locals_lists.last and @locals_lists.last[name] #already defined in this block @symbols[name][-1]=val #overwrite current value else stack=(@symbols[name] ||= []) stack.push val @locals_lists.last[name]=true end assert @locals_lists.last return val end |
#__locals_lists ⇒ Object
70 71 72 |
# File 'lib/rubylexer/symboltable.rb', line 70 def __locals_lists @locals_lists end |
#deep_copy ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubylexer/symboltable.rb', line 45 def deep_copy result=SymbolTable.allocate new_symbols={} @symbols.each_pair{|str,stack| new_symbols[str.clone]=stack.map{|elem| elem.clone rescue elem } } new_locals_lists=[] @locals_lists.each{|hash| new_locals_lists.push({}) hash.each_pair{|str,bool| new_locals_lists.last[str.dup]=bool } } new_locals_lists.push({}) if new_locals_lists.empty? result.instance_eval{ @symbols=new_symbols @locals_lists=new_locals_lists } return result end |
#end_block ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rubylexer/symboltable.rb', line 34 def end_block assert @locals_lists.last list=@locals_lists.pop list or raise "unbalanced end block" list.each_key {|sym| @symbols[sym].pop @symbols[sym].empty? and @symbols.delete sym } assert @locals_lists.last end |
#names ⇒ Object
66 67 68 |
# File 'lib/rubylexer/symboltable.rb', line 66 def names @symbols.keys end |
#start_block ⇒ Object
28 29 30 31 32 |
# File 'lib/rubylexer/symboltable.rb', line 28 def start_block assert @locals_lists.last @locals_lists.push({}) assert @locals_lists.last end |