bit of early blockstatement impl

master
Drake 1 year ago
parent 1a0984a060
commit 31fd9a4382

@ -5,140 +5,153 @@ import VariableDeclaration from "./nodes/VariableDeclaration.ts";
import ExpressionStatement from "./nodes/ExpressionStatement.ts";
import IfStatement from "./nodes/IfStatement.ts";
import WhileStatement from "./nodes/WhileStatement.ts";
import BlockStatement from "./nodes/BlockStatement.ts";
const { args } = await new Command()
.name("lrisscript")
.version("0.0.1")
.description(
"Compiles LrisScript (goofy subset of JS) to LASM (LRIS text representation)",
)
.arguments("<input:string> [output:string]")
.parse(Deno.args);
.name("lrisscript")
.version("0.0.1")
.description(
"Compiles LrisScript (goofy subset of JS) to LASM (LRIS text representation)",
)
.arguments("<input:string> [output:string]")
.parse(Deno.args);
let ast: swc.Module;
if (args[0].endsWith(".ts")) {
ast = await swc.parse(await Deno.readTextFile(args[0]), {
syntax: "typescript",
});
ast = await swc.parse(await Deno.readTextFile(args[0]), {
syntax: "typescript",
});
} else {
ast = await swc.parse(await Deno.readTextFile(args[0]), {
syntax: "ecmascript",
});
ast = await swc.parse(await Deno.readTextFile(args[0]), {
syntax: "ecmascript",
});
}
const variablesTable: VariablesTableType = {};
const variableHoles: {
memoryLocation: number;
memorySize: number;
memoryLocation: number;
memorySize: number;
}[] = []; // 😳
// deno-lint-ignore prefer-const
let variableMemoryOffset = {
offset: 0x011F,
offset: 0x011F,
};
function getVariableMemoryOffset(varSize: number) {
for (const memoryHole of variableHoles) {
if (varSize <= memoryHole.memorySize) {
memoryHole.memorySize -= varSize;
return memoryHole.memoryLocation;
}
for (const memoryHole of variableHoles) {
if (varSize <= memoryHole.memorySize) {
memoryHole.memorySize -= varSize;
return memoryHole.memoryLocation;
}
const oldMemoryOffset = variableMemoryOffset.offset;
variableMemoryOffset.offset += varSize;
return oldMemoryOffset;
}
const oldMemoryOffset = variableMemoryOffset.offset;
variableMemoryOffset.offset += varSize;
return oldMemoryOffset;
}
// FIXME: zero out old memory here
function freeVariable(variableName: string) {
const variableInfo = variablesTable[variableName];
delete variablesTable[variableName];
variableHoles.push({
memoryLocation: variableInfo.memoryLocation,
memorySize: variableInfo.memorySize,
});
function freeVariable(
variableName: string,
) {
const variableInfo = variablesTable[variableName];
delete variablesTable[variableName];
variableHoles.push({
memoryLocation: variableInfo.memoryLocation,
memorySize: variableInfo.memorySize,
});
}
function resolveNode(
node: swc.Node,
prc: { count: number },
variableModificationThings: VariablesThingType,
variablesTable: VariablesTableType,
node: swc.Node,
prc: { count: number },
variableModificationThings: VariablesThingType,
variablesTable: VariablesTableType,
) {
// deno-lint-ignore prefer-const
let instructions: string[] = [];
switch (node.type) {
case "VariableDeclaration": {
VariableDeclaration(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
case "ExpressionStatement": {
ExpressionStatement(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
case "IfStatement": {
IfStatement(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
case "WhileStatement": {
WhileStatement(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
default:
throw new Error(`Unimplemented node ${node.type}`);
// deno-lint-ignore prefer-const
let instructions: string[] = [];
switch (node.type) {
case "VariableDeclaration": {
VariableDeclaration(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
case "ExpressionStatement": {
ExpressionStatement(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
case "IfStatement": {
IfStatement(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
case "WhileStatement": {
WhileStatement(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
case "BlockStatement": {
BlockStatement(
node,
variablesTable,
variableModificationThings,
prc,
instructions,
);
break;
}
return instructions;
default:
throw new Error(`Unimplemented node ${node.type}`);
}
return instructions;
}
export { resolveNode };
// deno-lint-ignore prefer-const
let prc = {
// i love java's crypt for only doing references in objects
count: 0,
// i love java's crypt for only doing references in objects
count: 0,
};
let outputInstructions: string[] = [];
for (const node of ast.body) {
outputInstructions = outputInstructions.concat(
resolveNode(
node,
prc,
{
getVariableMemoryOffset,
freeVariable,
},
variablesTable,
),
);
console.log(variablesTable);
console.log(prc.count);
outputInstructions = outputInstructions.concat(
resolveNode(
node,
prc,
{
getVariableMemoryOffset,
freeVariable,
},
variablesTable,
),
);
console.log(variablesTable);
console.log(prc.count);
}
outputInstructions.push("HLT");
if (args[1]) {
await Deno.writeTextFile(`./${args[1]}`, outputInstructions.join("\n"));
await Deno.writeTextFile(`./${args[1]}`, outputInstructions.join("\n"));
} else {
console.log(outputInstructions.join("\n"));
console.log(outputInstructions.join("\n"));
}

@ -0,0 +1,33 @@
import swc from "npm:@swc/wasm@1.3.35";
import { resolveNode } from "../index.ts";
export default function BlockStatement(
node: swc.Node,
variablesTable: VariablesTableType,
variableModificationThings: VariablesThingType,
prc: { count: number },
instructions: string[],
) {
const typedNode = node as swc.BlockStatement;
const oldVariableTable = { ...variablesTable };
for (
const node of typedNode.stmts
) {
instructions = instructions.concat(
resolveNode(
node,
prc,
variableModificationThings,
variablesTable,
),
);
}
for (const key of Object.keys(variablesTable)) {
if (!oldVariableTable[key]) {
variableModificationThings.freeVariable(key);
}
}
}
Loading…
Cancel
Save