Refactor executor to make it able to call recursively

and Add variable declarator using callExpression AST test
This commit is contained in:
Kurt Hutten IrevDev
2022-11-20 09:41:21 +11:00
parent 0d92dd4a3c
commit 082730bb2e
4 changed files with 230 additions and 56 deletions

View File

@ -1,58 +1,64 @@
import { Program, BinaryPart } from "./abstractSyntaxTree";
import { Program, BinaryPart, BinaryExpression } from "./abstractSyntaxTree";
export const executor = (ast: Program, rootOverride: {[key: string]: any} = {}): any => {
const programMemory: { [key: string]: any } = {
interface ProgramMemory {
root: { [key: string]: any };
return?: any;
}
export const executor = (
node: Program,
programMemory: ProgramMemory = { root: {} }
): any => {
const _programMemory: ProgramMemory = {
root: {
...rootOverride
...programMemory.root,
},
return: programMemory.return,
};
const { body } = ast;
const { body } = node;
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;
_programMemory.root[variableName] = declaration.init.value;
} else if (declaration.init.type === "BinaryExpression") {
const getVal = (part: BinaryPart) => {
if (part.type === "Literal") {
return part.value;
} else if (part.type === "Identifier") {
return programMemory.root[part.name];
}
};
const left = getVal(declaration.init.left);
const right = getVal(declaration.init.right);
programMemory.root[variableName] = left + right;
_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: { [key: string]: any } = {
_programMemory.root[declaration.id.name] = (...args: any[]) => {
const fnMemory: ProgramMemory = {
root: {
...programMemory.root,
..._programMemory.root,
},
};
if(args.length > fnInit.params.length) {
throw new Error(`Too many arguments passed to function ${declaration.id.name}`)
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}`)
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.root);
}
} else if(declaration.init.type === "CallExpression") {
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") {
if (arg.type === "Literal") {
return arg.value;
} else if(arg.type === "Identifier") {
return programMemory.root[arg.name];
} else if (arg.type === "Identifier") {
return _programMemory.root[arg.name];
}
})
programMemory.root[variableName] = programMemory.root[fnName](...fnArgs);
});
_programMemory.root[variableName] = _programMemory.root[fnName](
...fnArgs
);
}
});
} else if (statement.type === "ExpressionStatement") {
@ -63,14 +69,33 @@ export const executor = (ast: Program, rootOverride: {[key: string]: any} = {}):
if (arg.type === "Literal") {
return arg.value;
} else if (arg.type === "Identifier") {
return programMemory.root[arg.name];
return _programMemory.root[arg.name];
}
});
programMemory.root[functionName](...args);
_programMemory.root[functionName](...args);
}
} else if (statement.type === "ReturnStatement") {
if(statement.argument.type === "BinaryExpression") {
const returnValue = getBinaryExpressionResult(statement.argument, _programMemory);
_programMemory.return = returnValue;
}
} else if(statement.type === "ReturnStatement") {
console.log("statement",statement)
}
});
return programMemory;
return _programMemory;
};
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;
}