2022-11-20 09:41:21 +11:00
|
|
|
import { Program, BinaryPart, BinaryExpression } from "./abstractSyntaxTree";
|
2022-11-21 08:57:44 +11:00
|
|
|
import {Path, sketchFns } from "./sketch";
|
2022-11-14 14:04:23 +11:00
|
|
|
|
2022-11-21 08:57:44 +11:00
|
|
|
export interface ProgramMemory {
|
2022-11-20 09:41:21 +11:00
|
|
|
root: { [key: string]: any };
|
|
|
|
return?: any;
|
2022-11-21 08:57:44 +11:00
|
|
|
_sketch: Path[];
|
2022-11-20 09:41:21 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
export const executor = (
|
|
|
|
node: Program,
|
2022-11-21 08:57:44 +11:00
|
|
|
programMemory: ProgramMemory = { root: {}, _sketch: [] },
|
2022-11-21 09:16:24 +11:00
|
|
|
options: { bodyType: "root" | "sketch" | "block" } = { bodyType: "root" }
|
2022-11-20 09:41:21 +11:00
|
|
|
): any => {
|
|
|
|
const _programMemory: ProgramMemory = {
|
2022-11-14 14:04:23 +11:00
|
|
|
root: {
|
2022-11-20 09:41:21 +11:00
|
|
|
...programMemory.root,
|
2022-11-14 14:04:23 +11:00
|
|
|
},
|
2022-11-21 08:57:44 +11:00
|
|
|
_sketch: [],
|
2022-11-20 09:41:21 +11:00
|
|
|
return: programMemory.return,
|
2022-11-14 14:04:23 +11:00
|
|
|
};
|
2022-11-20 09:41:21 +11:00
|
|
|
const { body } = node;
|
2022-11-14 14:04:23 +11:00
|
|
|
body.forEach((statement) => {
|
|
|
|
if (statement.type === "VariableDeclaration") {
|
|
|
|
statement.declarations.forEach((declaration) => {
|
|
|
|
const variableName = declaration.id.name;
|
|
|
|
if (declaration.init.type === "Literal") {
|
2022-11-20 09:41:21 +11:00
|
|
|
_programMemory.root[variableName] = declaration.init.value;
|
2022-11-14 14:04:23 +11:00
|
|
|
} else if (declaration.init.type === "BinaryExpression") {
|
2022-11-21 08:57:44 +11:00
|
|
|
_programMemory.root[variableName] = getBinaryExpressionResult(
|
|
|
|
declaration.init,
|
|
|
|
_programMemory
|
|
|
|
);
|
|
|
|
} else if (declaration.init.type === "SketchExpression") {
|
|
|
|
const sketchInit = declaration.init;
|
|
|
|
const fnMemory: ProgramMemory = {
|
|
|
|
root: {
|
|
|
|
..._programMemory.root,
|
|
|
|
},
|
|
|
|
_sketch: [],
|
|
|
|
};
|
|
|
|
const { _sketch } = executor(sketchInit.body, fnMemory, {
|
|
|
|
bodyType: "sketch",
|
|
|
|
});
|
|
|
|
_programMemory.root[variableName] = _sketch;
|
2022-11-20 08:18:45 +11:00
|
|
|
} else if (declaration.init.type === "FunctionExpression") {
|
|
|
|
const fnInit = declaration.init;
|
2022-11-20 09:41:21 +11:00
|
|
|
|
|
|
|
_programMemory.root[declaration.id.name] = (...args: any[]) => {
|
|
|
|
const fnMemory: ProgramMemory = {
|
2022-11-20 08:18:45 +11:00
|
|
|
root: {
|
2022-11-20 09:41:21 +11:00
|
|
|
..._programMemory.root,
|
2022-11-20 08:18:45 +11:00
|
|
|
},
|
2022-11-21 08:57:44 +11:00
|
|
|
_sketch: [],
|
2022-11-20 08:18:45 +11:00
|
|
|
};
|
2022-11-20 09:41:21 +11:00
|
|
|
if (args.length > fnInit.params.length) {
|
|
|
|
throw new Error(
|
|
|
|
`Too many arguments passed to function ${declaration.id.name}`
|
|
|
|
);
|
2022-11-20 08:18:45 +11:00
|
|
|
} else if (args.length < fnInit.params.length) {
|
2022-11-20 09:41:21 +11:00
|
|
|
throw new Error(
|
|
|
|
`Too few arguments passed to function ${declaration.id.name}`
|
|
|
|
);
|
2022-11-20 08:18:45 +11:00
|
|
|
}
|
|
|
|
fnInit.params.forEach((param, index) => {
|
|
|
|
fnMemory.root[param.name] = args[index];
|
|
|
|
});
|
2022-11-21 09:16:24 +11:00
|
|
|
return executor(fnInit.body, fnMemory, {bodyType: 'block'}).return;
|
2022-11-20 09:41:21 +11:00
|
|
|
};
|
|
|
|
} else if (declaration.init.type === "CallExpression") {
|
2022-11-20 08:18:45 +11:00
|
|
|
const fnName = declaration.init.callee.name;
|
|
|
|
const fnArgs = declaration.init.arguments.map((arg) => {
|
2022-11-20 09:41:21 +11:00
|
|
|
if (arg.type === "Literal") {
|
2022-11-20 08:18:45 +11:00
|
|
|
return arg.value;
|
2022-11-20 09:41:21 +11:00
|
|
|
} else if (arg.type === "Identifier") {
|
|
|
|
return _programMemory.root[arg.name];
|
2022-11-20 08:18:45 +11:00
|
|
|
}
|
2022-11-20 09:41:21 +11:00
|
|
|
});
|
2022-11-21 08:57:44 +11:00
|
|
|
if ("lineTo" === fnName || "close" === fnName) {
|
|
|
|
if (options.bodyType !== "sketch") {
|
|
|
|
throw new Error(
|
|
|
|
`Cannot call ${fnName} outside of a sketch declaration`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const result = sketchFns[fnName](
|
|
|
|
_programMemory,
|
|
|
|
variableName,
|
2022-11-23 21:28:38 +11:00
|
|
|
[declaration.start, declaration.end],
|
2022-11-21 08:57:44 +11:00
|
|
|
...fnArgs
|
|
|
|
);
|
|
|
|
_programMemory._sketch = result.programMemory._sketch;
|
|
|
|
_programMemory.root[variableName] = result.currentPath;
|
|
|
|
} else {
|
|
|
|
_programMemory.root[variableName] = _programMemory.root[fnName](
|
|
|
|
...fnArgs
|
|
|
|
);
|
|
|
|
}
|
2022-11-14 14:04:23 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (statement.type === "ExpressionStatement") {
|
|
|
|
const expression = statement.expression;
|
|
|
|
if (expression.type === "CallExpression") {
|
|
|
|
const functionName = expression.callee.name;
|
|
|
|
const args = expression.arguments.map((arg) => {
|
|
|
|
if (arg.type === "Literal") {
|
|
|
|
return arg.value;
|
|
|
|
} else if (arg.type === "Identifier") {
|
2022-11-20 09:41:21 +11:00
|
|
|
return _programMemory.root[arg.name];
|
2022-11-14 14:04:23 +11:00
|
|
|
}
|
|
|
|
});
|
2022-11-21 08:57:44 +11:00
|
|
|
if ("lineTo" === functionName || "close" === functionName) {
|
|
|
|
if (options.bodyType !== "sketch") {
|
|
|
|
throw new Error(
|
|
|
|
`Cannot call ${functionName} outside of a sketch declaration`
|
|
|
|
);
|
|
|
|
}
|
2022-11-23 21:28:38 +11:00
|
|
|
const result = sketchFns[functionName](_programMemory, "", [statement.start, statement.end], ...args);
|
2022-11-21 08:57:44 +11:00
|
|
|
_programMemory._sketch = [...result.programMemory._sketch];
|
2022-11-21 09:16:24 +11:00
|
|
|
} else if("show" === functionName) {
|
|
|
|
if (options.bodyType !== "root") {
|
|
|
|
throw new Error(
|
|
|
|
`Cannot call ${functionName} outside of a root`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_programMemory.return = expression.arguments;
|
|
|
|
|
2022-11-21 08:57:44 +11:00
|
|
|
} else {
|
|
|
|
_programMemory.root[functionName](...args);
|
|
|
|
}
|
2022-11-20 09:41:21 +11:00
|
|
|
}
|
|
|
|
} else if (statement.type === "ReturnStatement") {
|
2022-11-21 08:57:44 +11:00
|
|
|
if (statement.argument.type === "BinaryExpression") {
|
|
|
|
_programMemory.return = getBinaryExpressionResult(
|
|
|
|
statement.argument,
|
|
|
|
_programMemory
|
|
|
|
);
|
2022-11-14 14:04:23 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-11-20 09:41:21 +11:00
|
|
|
return _programMemory;
|
2022-11-14 14:04:23 +11:00
|
|
|
};
|
2022-11-20 09:41:21 +11:00
|
|
|
|
|
|
|
function getBinaryExpressionResult(
|
|
|
|
expression: BinaryExpression,
|
|
|
|
programMemory: ProgramMemory
|
|
|
|
) {
|
|
|
|
const getVal = (part: BinaryPart) => {
|
|
|
|
if (part.type === "Literal") {
|
|
|
|
return part.value;
|
|
|
|
} else if (part.type === "Identifier") {
|
|
|
|
return programMemory.root[part.name];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const left = getVal(expression.left);
|
|
|
|
const right = getVal(expression.right);
|
|
|
|
return left + right;
|
|
|
|
}
|