Re: how to print vm commands in correct order
Posted by
cadet1620 on
URL: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/how-to-print-vm-commands-in-correct-order-tp4030962p4030970.html
kraftwerk1611 wrote
However I am not sure how to avoid global variables in situations like when a function a() calls b(), then b() calls c(), and then c() calls d(). And there is suppose a subroutine symbol table that is defined in function a() and also used in function d() but functions b() and c() dont use it.
During compilation, functions are completely independent. None of the arguments or local variables for function a() affect function b().
class MyClass {
field ...
static ...
function
§ void a() {
var a, b, c;
...
}
function
§ void b() {
var x, y, z, a, b, c;
...
}
}
When compiling, when the tokenizer gets to position
§ ('function' has been parsed and compileSubroutine() has been called) you need to call SymboolTable.startSubroutine().
SymboolTable.startSubroutine() throws away all symbol information for the previous subroutine, keeping only the symbol information for the class's fields and statics.
This is why the SymbolTable is recommended to be implemented using two hash tables (Python dictionaries). SymboolTable.startSubroutine() can simply dispose of the subroutine hash table and allocate a new hash table and set localIndex back to 0.
--Mark