A data structure for tracking symbols in a compiler or interpreter.
var SymbolTable = ;//create the root scopevar s0 = ;//define some symbolss0;s0;//now enter a sub-scope that inherits s0var s1 = s0;//define/override symbols in the new scopes1;s1;//s1 inherited "a"s1;//-> 1//s1 changed "b"s1;//-> 200//but s0 "b" was not toucheds0;//-> 2//s1 has "c", but s0 does nots1;//-> trues0;//-> false
s = SymbolTable()
Creates a new symbol table.
Returns true if the symbol is in scope.
value = s.get(symbol)
Returns the value associated with that symbol in scope. Returns undefined if not found.
Set the value associated with symbol. This can be anything you want. It can be another data structure if you need to store symbol values and type information for example.
Remove a symbol from a scope
s2 = s.push()
Return a new scope that inherits the current scope.
Create a stack of symbol tables/scopes where you always work with the current scope at the top of the stack. It has the same API as the symbol table except you push/pop the scope.
var SymbolTableStack = ;var s = ;s;s;s;s;//-> 2s;s;//-> 1
has, get, set, unset
These all work the same as the symbol table. However, it will always be the scope at the top of the stack.
table = s.push()
Creates a new table/scope and pushes it on top of the stack. It also returns the new table.
table = s.pop()
Pops the current table/scope off the stack.
i = s.height()
Get the stack height (initially it's 1)
i = s.getItsHeight(symbol)
What was the stack height when this symbol was set/defined? Returns undefined if the symbol is not found.