Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
happytomatoe committed Oct 20, 2024
1 parent 8b71fdb commit b573d00
Show file tree
Hide file tree
Showing 12 changed files with 4,154 additions and 3,767 deletions.
2 changes: 1 addition & 1 deletion simulator/src/jack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Next command gives us ability to regenerate parser and lexer after we've changed

```
npm run gen
```
```
9 changes: 6 additions & 3 deletions simulator/src/jack/anltr.compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { GlobalSymbolTableListener } from "./listener/global.symbol.listener.js";
import { CustomErrorListener } from "./listener/error.listener.js";
import { ValidatorListener } from "./listener/validator.listener.js";
Expand Down Expand Up @@ -110,7 +109,9 @@ export class JackCompiler {
tree: ProgramContext,
filename?: string,
): ProgramContext | JackCompilerError[] {
if (Object.keys(this.globalSymbolTableListener.globalSymbolTable).length == 0) {
if (
Object.keys(this.globalSymbolTableListener.globalSymbolTable).length == 0
) {
throw new Error(
"Please populate global symbol table using parserAndBind method",
);
Expand All @@ -134,7 +135,9 @@ export class JackCompiler {
return errors;
}
const validateTree = treeOrErrors as ProgramContext;
const vmWriter = new VMWriter(this.globalSymbolTableListener.globalSymbolTable);
const vmWriter = new VMWriter(
this.globalSymbolTableListener.globalSymbolTable,
);
ParseTreeWalker.DEFAULT.walk(vmWriter, validateTree);
return vmWriter.result;
}
Expand Down
59 changes: 32 additions & 27 deletions simulator/src/jack/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const terminalNodeToSpan = (node: TerminalNode): Span => {
export const ruleContextToSpan = (ctx: ParserRuleContext): Span => {
const start = assertExists(
ctx.start,
" Cannot find start token when creating an error"
" Cannot find start token when creating an error",
);
return {
line: start.line,
Expand Down Expand Up @@ -59,7 +59,7 @@ export interface JackCompilerError {
export function makeJackCompilerError(
type: JackCompilerErrorType,
span: Span,
msg: string
msg: string,
): JackCompilerError {
return {
type,
Expand All @@ -72,83 +72,84 @@ export const ConstructorMushReturnThisError = (span: Span) =>
makeJackCompilerError(
"ConstructorMushReturnThisError",
span,
`A constructor must return 'this'`
`A constructor must return 'this'`,
);

export const DuplicatedClassError = (span: Span, className: string) =>
makeJackCompilerError(
"DuplicatedClassError",
span,
`Class ${className} is already defined.`
`Class ${className} is already defined.`,
);

export const DuplicatedSubroutineError = (span: Span, subroutineName: string) =>
makeJackCompilerError(
"DuplicatedSubroutineError",
span,
`Subroutine ${subroutineName} is already defined.`
`Subroutine ${subroutineName} is already defined.`,
);

export const DuplicatedVariableError = (span: Span, variableName: string) =>
makeJackCompilerError(
"DuplicatedVariableError",
span,
`Duplicated local variable, field, argument or static variable ${variableName}`
`Duplicated local variable, field, argument or static variable ${variableName}`,
);

export const FieldCantBeReferencedInFunctionError = (span: Span) =>
makeJackCompilerError(
"FieldCantBeReferencedInFunctionError",
span,
`Field can't be referenced in a function`
`Field can't be referenced in a function`,
);

export const FilenameDoesntMatchClassNameError = (
span: Span,
filename: string,
className: string
className: string,
) =>
makeJackCompilerError(
"FilenameDoesntMatchClassNameError",
span,
`Class name ${className} doesn't match file name ${filename}`
`Class name ${className} doesn't match file name ${filename}`,
);

export const FunctionCalledAsMethodError = (span: Span, subroutineId: string) =>
makeJackCompilerError(
"FunctionCalledAsMethodError",
span,
`Function or constructor ${subroutineId} was called as a method`
`Function or constructor ${subroutineId} was called as a method`,
);

export const IncorrectConstructorReturnTypeError = (span: Span) =>
makeJackCompilerError(
"IncorrectConstructorReturnTypeError",
span,
`The return type of a constructor must be of the class type`
`The return type of a constructor must be of the class type`,
);

export const IncorrectParamsNumberInSubroutineCallError = (
span: Span,
subroutineName: string,
expectedParamsCount: number,
actualParamsCount: number
actualParamsCount: number,
) =>
makeJackCompilerError(
"IncorrectParamsNumberInSubroutineCallError",
span,
`Expected ${expectedParamsCount} arguments, but got ${actualParamsCount}`);
`Expected ${expectedParamsCount} arguments, but got ${actualParamsCount}`,
);

export const IntLiteralIsOutOfRangeError = (
span: Span,
value: number,
min: number,
max: number
max: number,
) =>
makeJackCompilerError(
"IntLiteralIsOutOfRangeError",
span,
`Integer constant(${value}) is out of range. Min value is ${min} and max value is ${max}`
`Integer constant(${value}) is out of range. Min value is ${min} and max value is ${max}`,
);

export const LexerOrParserError = (span: Span, msg: string) =>
Expand All @@ -158,56 +159,56 @@ export const MethodCalledAsFunctionError = (span: Span, subroutineId: string) =>
makeJackCompilerError(
"MethodCalledAsFunctionError",
span,
`Method ${subroutineId} was called as a function/constructor`
`Method ${subroutineId} was called as a function/constructor`,
);

export const NonVoidFunctionNoReturnError = (span: Span) =>
makeJackCompilerError(
"NonVoidFunctionNoReturnError",
span,
`A non void subroutine must return a value`
`A non void subroutine must return a value`,
);

export const SubroutineNotAllPathsReturnError = (
span: Span,
subroutineName: string
subroutineName: string,
) =>
makeJackCompilerError(
"SubroutineNotAllPathsReturnError",
span,
`Subroutine ${subroutineName}: not all code paths return a value`
`Subroutine ${subroutineName}: not all code paths return a value`,
);

export const ThisCantBeReferencedInFunctionError = (span: Span) =>
makeJackCompilerError(
"ThisCantBeReferencedInFunctionError",
span,
`this can't be referenced in a function`
`this can't be referenced in a function`,
);

export const UndeclaredVariableError = (span: Span, variableName: string) =>
makeJackCompilerError(
"UndeclaredVariableError",
span,
`Undeclared variable ${variableName}`
`Undeclared variable ${variableName}`,
);

export const UnknownClassError = (span: Span, className: string) =>
makeJackCompilerError(
"UnknownClassError",
span,
`Class ${className} doesn't exist`
`Class ${className} doesn't exist`,
);

export const UnknownSubroutineCallError = (
span: Span,
subroutineName: string,
className?: string
className?: string,
) =>
makeJackCompilerError(
"UnknownSubroutineCallError",
span,
`Can't find subroutine '${subroutineName} in '${className ?? "(unknown)"}`
`Can't find subroutine '${subroutineName} in '${className ?? "(unknown)"}`,
);

export const UnreachableCodeError = (span: Span) =>
Expand All @@ -217,12 +218,16 @@ export const VoidSubroutineReturnsValueError = (span: Span) =>
makeJackCompilerError(
"VoidSubroutineReturnsValueError",
span,
"Cannot return a value from a void subroutine"
"Cannot return a value from a void subroutine",
);

export const WrongLiteralTypeError = (span: Span, expectedTypeName: string, actualTypeName:string) =>
export const WrongLiteralTypeError = (
span: Span,
expectedTypeName: string,
actualTypeName: string,
) =>
makeJackCompilerError(
"WrongLiteralTypeError",
span,
`Cannot assign ${actualTypeName} type to ${expectedTypeName}`
`Cannot assign ${actualTypeName} type to ${expectedTypeName}`,
);
Loading

0 comments on commit b573d00

Please sign in to comment.