Files
modeling-app/src/lang/executor.ts

102 lines
3.5 KiB
TypeScript
Raw Normal View History

import { Program, BinaryPart, BinaryExpression } from "./abstractSyntaxTree";
2022-11-14 14:04:23 +11:00
interface ProgramMemory {
root: { [key: string]: any };
return?: any;
}
export const executor = (
node: Program,
programMemory: ProgramMemory = { root: {} }
): any => {
const _programMemory: ProgramMemory = {
2022-11-14 14:04:23 +11:00
root: {
...programMemory.root,
2022-11-14 14:04:23 +11:00
},
return: programMemory.return,
2022-11-14 14:04:23 +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") {
_programMemory.root[variableName] = declaration.init.value;
2022-11-14 14:04:23 +11:00
} else if (declaration.init.type === "BinaryExpression") {
_programMemory.root[variableName] = getBinaryExpressionResult(declaration.init, _programMemory);
} else if (declaration.init.type === "FunctionExpression") {
const fnInit = declaration.init;
_programMemory.root[declaration.id.name] = (...args: any[]) => {
const fnMemory: ProgramMemory = {
root: {
..._programMemory.root,
},
};
if (args.length > fnInit.params.length) {
throw new Error(
`Too many arguments passed to function ${declaration.id.name}`
);
} else if (args.length < fnInit.params.length) {
throw new Error(
`Too few arguments passed to function ${declaration.id.name}`
);
}
fnInit.params.forEach((param, index) => {
fnMemory.root[param.name] = args[index];
});
return executor(fnInit.body, fnMemory).return;
};
} else if (declaration.init.type === "CallExpression") {
const fnName = declaration.init.callee.name;
const fnArgs = declaration.init.arguments.map((arg) => {
if (arg.type === "Literal") {
return arg.value;
} else if (arg.type === "Identifier") {
return _programMemory.root[arg.name];
}
});
_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") {
return _programMemory.root[arg.name];
2022-11-14 14:04:23 +11:00
}
});
_programMemory.root[functionName](...args);
}
} else if (statement.type === "ReturnStatement") {
if(statement.argument.type === "BinaryExpression") {
const returnValue = getBinaryExpressionResult(statement.argument, _programMemory);
_programMemory.return = returnValue;
2022-11-14 14:04:23 +11:00
}
}
});
return _programMemory;
2022-11-14 14:04:23 +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;
}