Skip to content

Commit

Permalink
Minor fixes in compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiecooro committed Oct 14, 2019
1 parent 54b53bc commit 388bf5c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion 11/CompilationEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,9 @@ class CompilationEngine {
// Nothing is needed
}

this.compileSubroutineBody();
if (this.tokenizer.symbol() !== '}') {
this.compileSubroutineBody();
}
this._extractSymbol('}');

this.vmWriter.writeLabel(this.endOfCurrentSubroutine);
Expand Down
14 changes: 11 additions & 3 deletions 11/SymbolTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ class SymbolTable {
this.symbols = {};
}

safeGet(name) {
let symbol = this.symbols[name];
if (!symbol) {
throw new Error('Symbol with name: ' + name + ' is not defined.');
}
return symbol;
}

startSubroutine() {
this.symbols = Object.entries(this.symbols).reduce(
(newSymbols, [k, s]) =>
Expand Down Expand Up @@ -33,15 +41,15 @@ class SymbolTable {
}

kindOf(name) {
return this.symbols[name].kind;
return this.safeGet(name).kind;
}

typeOf(name) {
return this.symbols[name].type;
return this.safeGet(name).type;
}

indexOf(name) {
return this.symbols[name].id;
return this.safeGet(name).id;
}
}

Expand Down

0 comments on commit 388bf5c

Please sign in to comment.