Compare commits
2 Commits
e8e9d30f60
...
a6d9604202
Author | SHA1 | Date |
---|---|---|
Drake | a6d9604202 | 1 year ago |
Drake | 129217fbea | 1 year ago |
@ -1,130 +1,127 @@
|
||||
import swc from "npm:@swc/wasm@1.3.35";
|
||||
import { resolveNode } from "../index.ts";
|
||||
import BlockStatement from "./BlockStatement.ts";
|
||||
|
||||
export default function IfStatement(
|
||||
node: swc.Node,
|
||||
variablesTable: VariablesTableType,
|
||||
variableModificationThings: VariablesThingType,
|
||||
prc: { count: number },
|
||||
mainInstructions: string[],
|
||||
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",
|
||||
),
|
||||
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",
|
||||
);
|
||||
|
||||
ourInsts.push("NOP");
|
||||
prc.count++;
|
||||
|
||||
for (const inst of ourInsts) {
|
||||
mainInstructions.push(inst);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
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(
|
||||
`if statement test with node type ${typedNode.test.type} not implemented`,
|
||||
"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${
|
||||
// deno-lint-ignore no-constant-condition
|
||||
(
|
||||
false
|
||||
)
|
||||
? ` ${
|
||||
addr1.toString(16).padStart(
|
||||
4,
|
||||
"0",
|
||||
)
|
||||
} ${
|
||||
addr2.toString(16).padStart(
|
||||
4,
|
||||
"0",
|
||||
)
|
||||
}`
|
||||
: ""}`];
|
||||
prc.count += 7;
|
||||
const oldPRC = prc;
|
||||
ourInsts = ourInsts.concat(
|
||||
...resolveNode(
|
||||
typedNode.consequent,
|
||||
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`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue