From 388bf5c33dc958ac5de5b23393d3ea6767090a19 Mon Sep 17 00:00:00 2001 From: Eddie CooRo <1300100eddiecooro@gmail.com> Date: Mon, 14 Oct 2019 13:10:27 +0330 Subject: [PATCH] Minor fixes in compiler --- 11/CompilationEngine.js | 4 +++- 11/SymbolTable.js | 14 +++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/11/CompilationEngine.js b/11/CompilationEngine.js index c264b15..c7a597e 100755 --- a/11/CompilationEngine.js +++ b/11/CompilationEngine.js @@ -648,7 +648,9 @@ class CompilationEngine { // Nothing is needed } - this.compileSubroutineBody(); + if (this.tokenizer.symbol() !== '}') { + this.compileSubroutineBody(); + } this._extractSymbol('}'); this.vmWriter.writeLabel(this.endOfCurrentSubroutine); diff --git a/11/SymbolTable.js b/11/SymbolTable.js index c667f2d..23ba2b9 100644 --- a/11/SymbolTable.js +++ b/11/SymbolTable.js @@ -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]) => @@ -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; } }