diff --git a/simulator/package.json b/simulator/package.json index d6d538b14..cbdc9e86c 100644 --- a/simulator/package.json +++ b/simulator/package.json @@ -38,6 +38,8 @@ "build": "tsc", "test": "jest --verbose", "test-w": "jest --watchAll --verbose", + "test-jack": "npm run test -- -t \"Jack\"", + "test-jack-w": "npm run test-w -- -t \"Jack\"", "gen": "cd src/languages/grammars && antlr4 -Dlanguage=TypeScript JackLexer.g4 JackParser.g4 -o ../../jack/generated" } } diff --git a/simulator/src/jack/builtins.ts b/simulator/src/jack/builtins.ts index cbb14dddd..672dc2f2a 100644 --- a/simulator/src/jack/builtins.ts +++ b/simulator/src/jack/builtins.ts @@ -8,8 +8,7 @@ interface Range { export const intRange = { min: -32768, max: 32767 } as Range; const builtInFunctionsToArgCount: Record = { "Array.dispose": 0, - //TODO: what is this? - // "Array.init": 0, + "Array.init": 0, "Array.new": 1, "Keyboard.init": 0, "Keyboard.keyPressed": 0, @@ -18,19 +17,18 @@ const builtInFunctionsToArgCount: Record = { "Keyboard.readLine": 1, "Math.abs": 1, "Math.divide": 2, - //TODO: what is this ? - // "Math.init": 0, + "Math.init": 0, "Math.max": 2, "Math.min": 2, "Math.multiply": 2, "Math.sqrt": 1, "Memory.alloc": 1, "Memory.deAlloc": 1, - // "Memory.init": 0, + "Memory.init": 0, "Memory.peek": 1, "Memory.poke": 2, "Output.backSpace": 0, - // "Output.init": 0, + "Output.init": 0, "Output.moveCursor": 2, "Output.printChar": 1, "Output.printInt": 1, @@ -41,7 +39,7 @@ const builtInFunctionsToArgCount: Record = { "Screen.drawLine": 4, "Screen.drawPixel": 2, "Screen.drawRectangle": 4, - // "Screen.init": 0, + "Screen.init": 0, "Screen.setColor": 1, "String.appendChar": 1, "String.backSpace": 0, @@ -49,7 +47,7 @@ const builtInFunctionsToArgCount: Record = { "String.dispose": 0, "String.doubleQuote": 0, "String.eraseLastChar": 0, - // "String.init": 0, + "String.init": 0, "String.intValue": 0, "String.length": 0, "String.new": 1, diff --git a/simulator/src/jack/compiler.test.ts b/simulator/src/jack/compiler.test.ts index 1620e4dcb..9be784b70 100644 --- a/simulator/src/jack/compiler.test.ts +++ b/simulator/src/jack/compiler.test.ts @@ -3,7 +3,7 @@ import { getTestResourcePath, testResourceDirs } from "./test.helper"; import path from "path"; import { ProgramContext } from "./generated/JackParser"; import { Compiler } from "./compiler"; -describe("Compiler", () => { +describe("Jack compiler", () => { test("static field", () => { testCompiler( `class A{ diff --git a/simulator/src/jack/listener/binder.listener.test.ts b/simulator/src/jack/listener/binder.listener.test.ts index 09839d282..6c9769ffc 100644 --- a/simulator/src/jack/listener/binder.listener.test.ts +++ b/simulator/src/jack/listener/binder.listener.test.ts @@ -10,7 +10,7 @@ import { parseJackText, } from "../test.helper"; -describe("Binder", () => { +describe("Jack binder", () => { const jestConsole = console; beforeEach(() => { diff --git a/simulator/src/jack/listener/validator.listener.test.ts b/simulator/src/jack/listener/validator.listener.test.ts index ce0ed44f7..33c584360 100644 --- a/simulator/src/jack/listener/validator.listener.test.ts +++ b/simulator/src/jack/listener/validator.listener.test.ts @@ -35,7 +35,7 @@ import fs from "fs"; import { BinderListener } from "./binder.listener"; import path from "path"; import { ProgramContext } from "../generated/JackParser"; -describe("ValidatorListener", () => { +describe("Jack validator listener", () => { const jestConsole = console; beforeEach(() => { global.console = require("console"); @@ -119,7 +119,7 @@ describe("ValidatorListener", () => { }`, UndeclaredVariableError, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.b": genericSymbol(SubroutineType.Function, 1), "Main.a": genericSymbol(SubroutineType.Function, 1), }, @@ -468,7 +468,7 @@ describe("ValidatorListener", () => { }`, IncorrectParamsNumberInSubroutineCallError, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 2), "Main.b": genericSymbol(SubroutineType.Function, 2), }, @@ -494,7 +494,7 @@ describe("ValidatorListener", () => { }`, undefined, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.new": genericSymbol(SubroutineType.Constructor, 0), "Main.a": genericSymbol(SubroutineType.Function, 0), "Main.b": genericSymbol(SubroutineType.Method, 0), @@ -515,7 +515,7 @@ describe("ValidatorListener", () => { }`, undefined, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Method, 0), "Main.b": genericSymbol(SubroutineType.Method, 0), }, @@ -539,7 +539,7 @@ describe("ValidatorListener", () => { }`, MethodCalledAsFunctionError, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.b": genericSymbol(SubroutineType.Function, 0), "Main.c": genericSymbol(SubroutineType.Method, 0), }, @@ -559,7 +559,7 @@ describe("ValidatorListener", () => { }`, FunctionCalledAsMethodError, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.b": genericSymbol(SubroutineType.Function, 0), "Main.c": genericSymbol(SubroutineType.Function, 0), }, @@ -575,7 +575,7 @@ describe("ValidatorListener", () => { }`, IncorrectConstructorReturnType, { - Main: genericSymbol(), + "Main": genericSymbol(), D: genericSymbol(), }, ); @@ -593,7 +593,7 @@ describe("ValidatorListener", () => { }`, UnreachableCodeError, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.new": genericSymbol(SubroutineType.Constructor, 0), }, ); @@ -608,7 +608,7 @@ describe("ValidatorListener", () => { }`, ConstructorMushReturnThis, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.new": genericSymbol(SubroutineType.Constructor, 0), }, ); @@ -625,7 +625,7 @@ describe("ValidatorListener", () => { }`, WrongLiteralTypeError, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 0), String: genericSymbol(), }, @@ -643,7 +643,7 @@ describe("ValidatorListener", () => { }`, WrongLiteralTypeError, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 0), }, ); @@ -661,7 +661,7 @@ describe("ValidatorListener", () => { }`, WrongLiteralTypeError, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 0), }, ); @@ -678,7 +678,7 @@ describe("ValidatorListener", () => { }`, IntLiteralOverflow, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 0), }, ); @@ -695,7 +695,7 @@ describe("ValidatorListener", () => { }`, IntLiteralOverflow, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 0), }, ); @@ -712,7 +712,7 @@ describe("ValidatorListener", () => { }`, FieldCantBeReferencedInFunction, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 0), }, ); @@ -729,7 +729,7 @@ describe("ValidatorListener", () => { }`, undefined, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 0), }, ); @@ -746,7 +746,7 @@ describe("ValidatorListener", () => { }`, ThisCantBeReferencedInFunction, { - Main: genericSymbol(), + "Main": genericSymbol(), "Main.a": genericSymbol(SubroutineType.Function, 0), }, ); @@ -794,9 +794,6 @@ function testValidator( new ValidatorListener(globalSymbolTable), ); if (expectedError) { - // if (validator.cfgNode) { - // validator.cfgNode.print() - // } if (validator.errors.length > 1) { console.error("Errors", validator.errors); } @@ -806,8 +803,8 @@ function testValidator( } catch (e) { throw new Error( `Expected error ${expectedError.name} but got '` + - validator.errors.join(",") + - "'", + validator.errors.join(",") + + "'", ); } } else { @@ -816,13 +813,9 @@ function testValidator( "Didn't expect any errors but got " + validator.errors.join("\n"), ); } - // if (expectedLocalSymbolTable != undefined) { - // expect(validator.localSymbolTable).toEqual(expectedLocalSymbolTable) - // } } -//TODO: add validation for assgining from void function call -//TODO: Add rule to forbid var use before assignment + /** * TODO: * Ideas for improvement - @@ -831,4 +824,6 @@ function testValidator( * - Expected subroutine name in call * - a numeric value is illegal here when using non numeric vars * - validate function call - when using literal in call validate the type + * add validation for assigning from void function call + * add rule to forbid var use before assignment */ diff --git a/simulator/src/jack/parser.test.ts b/simulator/src/jack/parser.test.ts index 73e227899..3de130d1c 100644 --- a/simulator/src/jack/parser.test.ts +++ b/simulator/src/jack/parser.test.ts @@ -10,7 +10,7 @@ import { } from "./test.helper"; import { BinderListener } from "./listener/binder.listener"; -describe("Parser", () => { +describe("Jack parser", () => { const jestConsole = console; beforeEach(() => { global.console = require("console"); diff --git a/simulator/src/jack/symbol.table.test.ts b/simulator/src/jack/symbol.table.test.ts index 97ca4b804..021c21c58 100644 --- a/simulator/src/jack/symbol.table.test.ts +++ b/simulator/src/jack/symbol.table.test.ts @@ -5,7 +5,7 @@ import { SubroutineScope, } from "./symbol"; -describe("LocalSymbolTable", () => { +describe("Jack local symbol table", () => { const jestConsole = console; beforeEach(() => {