You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.5 KiB
131 lines
4.5 KiB
import swc from "npm:@swc/wasm@1.3.35";
|
|
import { resolveNode } from "../index.ts";
|
|
|
|
export default function IfStatement(
|
|
node: swc.Node,
|
|
variablesTable: VariablesTableType,
|
|
variableModificationThings: VariablesThingType,
|
|
prc: { count: number },
|
|
mainInstructions: string[],
|
|
) {
|
|
const typedNode = node as swc.IfStatement;
|
|
switch (typedNode.test.type) {
|
|
case "BinaryExpression": {
|
|
const typedExpr = typedNode.test as swc.BinaryExpression;
|
|
let addr1: number;
|
|
let addr2: number;
|
|
switch (typedExpr.left.type) {
|
|
case "Identifier": {
|
|
const typedValue = typedExpr.left;
|
|
const variableInfo = variablesTable[typedValue.value];
|
|
if (variableInfo.memoryType === "string") {
|
|
// TODO: implement string comparisons
|
|
throw new Error(
|
|
"i would rather pipi in pampers than implement string comparisons",
|
|
);
|
|
}
|
|
addr1 = variableInfo.memoryLocation;
|
|
break;
|
|
}
|
|
default:
|
|
throw new Error(
|
|
`left side comparison not implemented for node type ${typedExpr.left.type}`,
|
|
);
|
|
}
|
|
switch (typedExpr.right.type) {
|
|
case "Identifier": {
|
|
const typedValue = typedExpr.right;
|
|
const variableInfo = variablesTable[typedValue.value];
|
|
if (variableInfo.memoryType === "string") {
|
|
// TODO: implement string comparisons
|
|
throw new Error(
|
|
"i would rather pipi in pampers than implement string comparisons",
|
|
);
|
|
}
|
|
addr2 = variableInfo.memoryLocation;
|
|
break;
|
|
}
|
|
case "BooleanLiteral": {
|
|
const value = typedExpr.right.value;
|
|
mainInstructions.push(
|
|
`LOL ${value ? "01" : "00"} 00F2`,
|
|
);
|
|
addr2 = 0x00F2;
|
|
prc.count += 4;
|
|
break;
|
|
}
|
|
default:
|
|
throw new Error(
|
|
`right side comparison not implemented for node type ${typedExpr.right.type}`,
|
|
);
|
|
}
|
|
let ourInsts = [`${(() => {
|
|
switch (typedExpr.operator) {
|
|
case "!=":
|
|
case "!==":
|
|
return "JEA";
|
|
case "==":
|
|
case "===":
|
|
return "JNA";
|
|
default:
|
|
throw new Error(
|
|
`if statement comparison operator ${typedExpr.operator} unimplemented`,
|
|
);
|
|
}
|
|
})} TODO${
|
|
(
|
|
typedExpr.operator === "<" ||
|
|
typedExpr.operator === ">"
|
|
)
|
|
? ` ${
|
|
addr1.toString(16).padStart(
|
|
4,
|
|
"0",
|
|
)
|
|
} ${
|
|
addr2.toString(16).padStart(
|
|
4,
|
|
"0",
|
|
)
|
|
}`
|
|
: ""
|
|
}`];
|
|
prc.count += 7;
|
|
const oldPRC = prc;
|
|
for (
|
|
const node of (typedNode.consequent as swc.BlockStatement)
|
|
.stmts
|
|
) {
|
|
ourInsts = ourInsts.concat(
|
|
resolveNode(
|
|
node,
|
|
prc,
|
|
variableModificationThings,
|
|
variablesTable,
|
|
),
|
|
);
|
|
}
|
|
ourInsts[0] = ourInsts[0].replace(
|
|
"TODO",
|
|
prc.count.toString(16).padStart(
|
|
4,
|
|
"0",
|
|
),
|
|
);
|
|
|
|
ourInsts.push("NOP");
|
|
prc.count++;
|
|
|
|
for (const inst of ourInsts) {
|
|
mainInstructions.push(inst);
|
|
}
|
|
|
|
break;
|
|
}
|
|
default:
|
|
throw new Error(
|
|
`if statement test with node type ${typedNode.test.type} not implemented`,
|
|
);
|
|
}
|
|
}
|