Skip to content

Commit

Permalink
Fix issues with overriding OS classes (#449)
Browse files Browse the repository at this point in the history
* Fix issues with overriding OS classes
  • Loading branch information
netalondon authored Sep 16, 2024
1 parent 9d8b768 commit e51d586
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 56 deletions.
29 changes: 21 additions & 8 deletions simulator/src/jack/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ import {
isPrimitive,
} from "../languages/jack.js";
import { Segment } from "../languages/vm.js";
import { VM_BUILTINS } from "../vm/builtins.js";
import {
VM_BUILTINS,
makeInterface,
overridesOsCorrectly,
} from "../vm/builtins.js";
import { validateSubroutine } from "./controlFlow.js";

const osClasses = new Set([
Expand Down Expand Up @@ -291,11 +295,25 @@ export class Compiler {
}
}

compileSubroutineDec(subroutine: Subroutine) {
validateSubroutineDec(subroutine: Subroutine) {
this.validateReturnType(
subroutine.returnType.value,
subroutine.returnType.span,
);

if (isOsClass(this.className)) {
const builtin = VM_BUILTINS[`${this.className}.${subroutine.name.value}`];

if (builtin && !overridesOsCorrectly(this.className, subroutine)) {
throw createError(
`OS subroutine ${this.className}.${subroutine.name.value} must follow the interface ${makeInterface(subroutine.name.value, builtin)})`,
);
}
}
}

compileSubroutineDec(subroutine: Subroutine) {
this.validateSubroutineDec(subroutine);
switch (subroutine.type) {
case "method":
this.compileMethod(subroutine);
Expand Down Expand Up @@ -426,15 +444,10 @@ export class Compiler {
}
this.validateArgNum(
`${className}.${subroutineName}`,
isMethod ? builtin.nArgs - 1 : builtin.nArgs,
builtin.args.length,
call,
);
return;
} else if (isOsClass(className)) {
throw createError(
`Class ${className} doesn't contain a subroutine ${subroutineName}`,
call.span,
);
} else if (this.classes[className]) {
for (const subroutine of this.classes[className].subroutines) {
if (subroutine.name.value == subroutineName) {
Expand Down
Loading

0 comments on commit e51d586

Please sign in to comment.