Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
happytomatoe committed Oct 4, 2024
1 parent 6bd99b8 commit d67729e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 40 deletions.
2 changes: 2 additions & 0 deletions simulator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
14 changes: 6 additions & 8 deletions simulator/src/jack/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ interface Range {
export const intRange = { min: -32768, max: 32767 } as Range;
const builtInFunctionsToArgCount: Record<string, number> = {
"Array.dispose": 0,
//TODO: what is this?
// "Array.init": 0,
"Array.init": 0,
"Array.new": 1,
"Keyboard.init": 0,
"Keyboard.keyPressed": 0,
Expand All @@ -18,19 +17,18 @@ const builtInFunctionsToArgCount: Record<string, number> = {
"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,
Expand All @@ -41,15 +39,15 @@ const builtInFunctionsToArgCount: Record<string, number> = {
"Screen.drawLine": 4,
"Screen.drawPixel": 2,
"Screen.drawRectangle": 4,
// "Screen.init": 0,
"Screen.init": 0,
"Screen.setColor": 1,
"String.appendChar": 1,
"String.backSpace": 0,
"String.charAt": 2,
"String.dispose": 0,
"String.doubleQuote": 0,
"String.eraseLastChar": 0,
// "String.init": 0,
"String.init": 0,
"String.intValue": 0,
"String.length": 0,
"String.new": 1,
Expand Down
2 changes: 1 addition & 1 deletion simulator/src/jack/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion simulator/src/jack/listener/binder.listener.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
parseJackText,
} from "../test.helper";

describe("Binder", () => {
describe("Jack binder", () => {
const jestConsole = console;

beforeEach(() => {
Expand Down
51 changes: 23 additions & 28 deletions simulator/src/jack/listener/validator.listener.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -119,7 +119,7 @@ describe("ValidatorListener", () => {
}`,
UndeclaredVariableError,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.b": genericSymbol(SubroutineType.Function, 1),
"Main.a": genericSymbol(SubroutineType.Function, 1),
},
Expand Down Expand Up @@ -468,7 +468,7 @@ describe("ValidatorListener", () => {
}`,
IncorrectParamsNumberInSubroutineCallError,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 2),
"Main.b": genericSymbol(SubroutineType.Function, 2),
},
Expand All @@ -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),
Expand All @@ -515,7 +515,7 @@ describe("ValidatorListener", () => {
}`,
undefined,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Method, 0),
"Main.b": genericSymbol(SubroutineType.Method, 0),
},
Expand All @@ -539,7 +539,7 @@ describe("ValidatorListener", () => {
}`,
MethodCalledAsFunctionError,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.b": genericSymbol(SubroutineType.Function, 0),
"Main.c": genericSymbol(SubroutineType.Method, 0),
},
Expand All @@ -559,7 +559,7 @@ describe("ValidatorListener", () => {
}`,
FunctionCalledAsMethodError,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.b": genericSymbol(SubroutineType.Function, 0),
"Main.c": genericSymbol(SubroutineType.Function, 0),
},
Expand All @@ -575,7 +575,7 @@ describe("ValidatorListener", () => {
}`,
IncorrectConstructorReturnType,
{
Main: genericSymbol(),
"Main": genericSymbol(),
D: genericSymbol(),
},
);
Expand All @@ -593,7 +593,7 @@ describe("ValidatorListener", () => {
}`,
UnreachableCodeError,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.new": genericSymbol(SubroutineType.Constructor, 0),
},
);
Expand All @@ -608,7 +608,7 @@ describe("ValidatorListener", () => {
}`,
ConstructorMushReturnThis,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.new": genericSymbol(SubroutineType.Constructor, 0),
},
);
Expand All @@ -625,7 +625,7 @@ describe("ValidatorListener", () => {
}`,
WrongLiteralTypeError,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 0),
String: genericSymbol(),
},
Expand All @@ -643,7 +643,7 @@ describe("ValidatorListener", () => {
}`,
WrongLiteralTypeError,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 0),
},
);
Expand All @@ -661,7 +661,7 @@ describe("ValidatorListener", () => {
}`,
WrongLiteralTypeError,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 0),
},
);
Expand All @@ -678,7 +678,7 @@ describe("ValidatorListener", () => {
}`,
IntLiteralOverflow,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 0),
},
);
Expand All @@ -695,7 +695,7 @@ describe("ValidatorListener", () => {
}`,
IntLiteralOverflow,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 0),
},
);
Expand All @@ -712,7 +712,7 @@ describe("ValidatorListener", () => {
}`,
FieldCantBeReferencedInFunction,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 0),
},
);
Expand All @@ -729,7 +729,7 @@ describe("ValidatorListener", () => {
}`,
undefined,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 0),
},
);
Expand All @@ -746,7 +746,7 @@ describe("ValidatorListener", () => {
}`,
ThisCantBeReferencedInFunction,
{
Main: genericSymbol(),
"Main": genericSymbol(),
"Main.a": genericSymbol(SubroutineType.Function, 0),
},
);
Expand Down Expand Up @@ -794,9 +794,6 @@ function testValidator<T extends { name: string }>(
new ValidatorListener(globalSymbolTable),
);
if (expectedError) {
// if (validator.cfgNode) {
// validator.cfgNode.print()
// }
if (validator.errors.length > 1) {
console.error("Errors", validator.errors);
}
Expand All @@ -806,8 +803,8 @@ function testValidator<T extends { name: string }>(
} catch (e) {
throw new Error(
`Expected error ${expectedError.name} but got '` +
validator.errors.join(",") +
"'",
validator.errors.join(",") +
"'",
);
}
} else {
Expand All @@ -816,13 +813,9 @@ function testValidator<T extends { name: string }>(
"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 -
Expand All @@ -831,4 +824,6 @@ function testValidator<T extends { name: string }>(
* - 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
*/
2 changes: 1 addition & 1 deletion simulator/src/jack/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion simulator/src/jack/symbol.table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SubroutineScope,
} from "./symbol";

describe("LocalSymbolTable", () => {
describe("Jack local symbol table", () => {
const jestConsole = console;

beforeEach(() => {
Expand Down

0 comments on commit d67729e

Please sign in to comment.