Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
happytomatoe committed Oct 3, 2024
1 parent af250f2 commit cf2c04c
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 81 deletions.
6 changes: 0 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
],
"outFiles": ["${workspaceFolder}/extension/build/test/**/*.js"],
"preLaunchTask": "${defaultBuildTask}"
}, {
"type": "chrome",
"request": "launch",
"name": "Test web ide",
"url": "http://localhost:3000/web-ide",
"webRoot": "${workspaceFolder}"
}
]
}
49 changes: 0 additions & 49 deletions .vscode/numbered-bookmarks.json

This file was deleted.

1 change: 0 additions & 1 deletion simulator/src/jack/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ interface Range {
max: number;
}
export const intRange = { min: -32768, max: 32767 } as Range;
//TODO: should we convert this to symbols?
const builtInFunctionsToArgCount: Record<string, number> = {
"Array.dispose": 0,
//TODO: what is this?
Expand Down
36 changes: 15 additions & 21 deletions simulator/src/jack/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
import { BinderListener } from "./listener/binder.listener.js";
import { CustomErrorListener } from "./listener/error.listener.js";
import { ValidatorListener } from "./listener/validator.listener.js";
import { JackCompilerError, LexerOrParserError, Span } from "./error.js";
import { JackCompilerError, LexerOrParserError } from "./error.js";
import { VMWriter } from "./listener/vm.writer.listener.js";
import JackParser, { ProgramContext } from "./generated/JackParser.js";
import { CharStreams, CommonTokenStream, ParseTreeWalker } from "antlr4";
import JackLexer from "./generated/JackLexer.js";
import { CompilationError, createError } from "../languages/base.js";
import { CompilationError } from "../languages/base.js";

export function compile(
files: Record<string, string>,
): Record<string, string | CompilationError> {
try {
return _compile(files)
} catch (err) {
const result: Record<string, CompilationError> = {};
console.error(err);
if (typeof err === "string") {
const result: Record<string, CompilationError> = {};
const error: CompilationError = createError(err.toUpperCase())
for (const name in Object.keys(files)) {
result[name] = error;
}
return result;
} else if (err instanceof Error) {
const result: Record<string, CompilationError> = {};
const error: CompilationError = createError(err.message)
for (const name in Object.keys(files)) {
result[name] = error;
}
return result;
const keys = Object.keys(files);
for (const name of keys) {
result[name] = { message: "Something went wrong while compiling files. Please create a bug report" } as CompilationError;
}
return result;
}
console.error("Should've returned from catch or try");
return {};
}
function _compile(files: Record<string, string>,
): Record<string, string | CompilationError> {
Expand All @@ -49,8 +38,7 @@ function _compile(files: Record<string, string>,
for (const [name, content] of Object.entries(files)) {
const treeOrErrors = compiler.parserAndBind(content);
if (Array.isArray(treeOrErrors)) {
const s = treeOrErrors[0].span
errors[name] = { message: `Line ${s.line}: ${treeOrErrors[0].msg}`, span: { start: s.start, end: s.end, line: 3 } as Span } as CompilationError;
errors[name] = toCompilerError(treeOrErrors);
}
trees[name] = treeOrErrors as ProgramContext;
}
Expand All @@ -64,13 +52,19 @@ function _compile(files: Record<string, string>,
for (const [name, tree] of Object.entries(trees)) {
const compiledOrErrors = compiler.compile(tree);
if (Array.isArray(compiledOrErrors)) {
result[name] = { message: compiledOrErrors[0].msg, span: compiledOrErrors[0].span } as CompilationError;
result[name] = toCompilerError(compiledOrErrors);
} else {
result[name] = compiledOrErrors;
}
}
return result;
}
function toCompilerError(errors: JackCompilerError[]): CompilationError {
const err = errors[0];
return { message: `Line ${err.span.line}: ${err.msg}`, span: err.span } as CompilationError
}


export class Compiler {
private binder = new BinderListener();
private errorListener = new CustomErrorListener();
Expand Down
1 change: 0 additions & 1 deletion simulator/src/jack/listener/binder.listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import JackParserListener from "../generated/JackParserListener.js";

const primitives = new Set(builtInTypes);
export type Primitive = typeof primitives extends Set<infer S> ? S : never;
/// <reference path="../error.ts" />
/**
* Creates global symbol table that contains built-in functions and found classes and subroutines
*/
Expand Down
3 changes: 0 additions & 3 deletions simulator/src/jack/listener/error.listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { JackCompilerError, LexerOrParserError } from "../error.js";
export class CustomErrorListener extends ErrorListener<any> {
public errors: JackCompilerError[] = [];

/**
* Provides a default instance of {@link ConsoleErrorListener}.
*/
override syntaxError = (
recognizer: Recognizer<any>,
offendingSymbol: any,
Expand Down

0 comments on commit cf2c04c

Please sign in to comment.