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

@ -326,4 +326,128 @@ describe("testing function declaration", () => {
}, },
]); ]);
}); });
test("call expression assignment", () => {
const tokens = lexer(
`fn funcN = (a, b) => { return a + b }
const myVar = funcN(1, 2)`);
const { body } = abstractSyntaxTree(tokens);
expect(body).toEqual([
{
"type": "VariableDeclaration",
"start": 0,
"end": 37,
"kind": "fn",
"declarations": [
{
"type": "VariableDeclarator",
"start": 3,
"end": 37,
"id": {
"type": "Identifier",
"start": 3,
"end": 8,
"name": "funcN"
},
"init": {
"type": "FunctionExpression",
"start": 11,
"end": 37,
"id": null,
"params": [
{
"type": "Identifier",
"start": 12,
"end": 13,
"name": "a"
},
{
"type": "Identifier",
"start": 15,
"end": 16,
"name": "b"
}
],
"body": {
"type": "BlockStatement",
"start": 21,
"end": 37,
"body": [
{
"type": "ReturnStatement",
"start": 23,
"end": 35,
"argument": {
"type": "BinaryExpression",
"start": 30,
"end": 35,
"left": {
"type": "Identifier",
"start": 30,
"end": 31,
"name": "a"
},
"operator": "+",
"right": {
"type": "Identifier",
"start": 34,
"end": 35,
"name": "b"
}
}
}
]
}
}
}
]
},
{
"type": "VariableDeclaration",
"start": 38,
"end": 63,
"kind": "const",
"declarations": [
{
"type": "VariableDeclarator",
"start": 44,
"end": 63,
"id": {
"type": "Identifier",
"start": 44,
"end": 49,
"name": "myVar"
},
"init": {
"type": "CallExpression",
"start": 52,
"end": 63,
"callee": {
"type": "Identifier",
"start": 52,
"end": 57,
"name": "funcN"
},
"arguments": [
{
"type": "Literal",
"start": 58,
"end": 59,
"value": 1,
"raw": "1"
},
{
"type": "Literal",
"start": 61,
"end": 62,
"value": 2,
"raw": "2"
}
],
"optional": false
}
}
]
}
])
});
}); });

View File

@ -389,7 +389,7 @@ function makeLiteral(tokens: Token[], index: number): Literal {
}; };
} }
interface BinaryExpression extends GeneralStatement { export interface BinaryExpression extends GeneralStatement {
type: "BinaryExpression"; type: "BinaryExpression";
operator: string; operator: string;
left: BinaryPart; left: BinaryPart;

View File

@ -1,4 +1,4 @@
import fs from "node:fs" import fs from "node:fs";
import { abstractSyntaxTree } from "./abstractSyntaxTree"; import { abstractSyntaxTree } from "./abstractSyntaxTree";
import { lexer } from "./tokeniser"; import { lexer } from "./tokeniser";
@ -8,9 +8,7 @@ describe("test", () => {
it("test assigning two variables, the second summing with the first", () => { it("test assigning two variables, the second summing with the first", () => {
const code = `const myVar = 5 const code = `const myVar = 5
const newVar = myVar + 1`; const newVar = myVar + 1`;
const tokens = lexer(code); const programMemory = exe(code);
const ast = abstractSyntaxTree(tokens);
const programMemory = executor(ast);
expect(withoutStdFns(programMemory)).toEqual({ expect(withoutStdFns(programMemory)).toEqual({
root: { root: {
myVar: 5, myVar: 5,
@ -19,10 +17,8 @@ const newVar = myVar + 1`;
}); });
}); });
it("test assigning a var with a string", () => { it("test assigning a var with a string", () => {
const code = `const myVar = "a str"` const code = `const myVar = "a str"`;
const tokens = lexer(code); const programMemory = exe(code);
const ast = abstractSyntaxTree(tokens);
const programMemory = executor(ast);
expect(withoutStdFns(programMemory)).toEqual({ expect(withoutStdFns(programMemory)).toEqual({
root: { root: {
myVar: "a str", myVar: "a str",
@ -30,10 +26,11 @@ const newVar = myVar + 1`;
}); });
}); });
it("test assigning a var by cont concatenating two strings string", () => { it("test assigning a var by cont concatenating two strings string", () => {
const code = fs.readFileSync("./src/lang/testExamples/variableDeclaration.cado", "utf-8"); const code = fs.readFileSync(
const tokens = lexer(code); "./src/lang/testExamples/variableDeclaration.cado",
const ast = abstractSyntaxTree(tokens); "utf-8"
const programMemory = executor(ast); );
const programMemory = exe(code);
expect(withoutStdFns(programMemory)).toEqual({ expect(withoutStdFns(programMemory)).toEqual({
root: { root: {
myVar: "a str another str", myVar: "a str another str",
@ -43,23 +40,51 @@ const newVar = myVar + 1`;
it("test with function call", () => { it("test with function call", () => {
const code = ` const code = `
const myVar = "hello" const myVar = "hello"
log(5, myVar)` log(5, myVar)`;
const programMemoryOverride = { const programMemoryOverride = {
log: jest.fn(), log: jest.fn(),
} };
const programMemory = executor(abstractSyntaxTree(lexer(code)), programMemoryOverride); const programMemory = executor(abstractSyntaxTree(lexer(code)), {
root: programMemoryOverride,
});
expect(withoutStdFns(programMemory)).toEqual({ expect(withoutStdFns(programMemory)).toEqual({
root: { myVar: "hello" }, root: { myVar: "hello" },
}); });
expect(programMemoryOverride.log).toHaveBeenCalledWith(5, "hello"); expect(programMemoryOverride.log).toHaveBeenCalledWith(5, "hello");
}) });
it("fn funcN = () => {}", () => {
const programMemory = exe(
[
"fn funcN = (a, b) => {",
" return a + b",
"}",
"const theVar = 5",
"const myVar = funcN(1, theVar)",
].join("\n")
);
expect(withoutStdFns(programMemory, ["funcN"])).toEqual({
root: { theVar: 5, myVar: 6 },
});
});
}); });
// helpers // helpers
function withoutStdFns(obj: any) { function exe(
code: string,
programMemory: { root: { [key: string]: any }; return?: any } = { root: {} }
) {
const tokens = lexer(code);
const ast = abstractSyntaxTree(tokens);
return executor(ast, programMemory);
}
function withoutStdFns(obj: any, toDelete: string[] = []) {
const newRoot = { ...obj.root }; const newRoot = { ...obj.root };
const newObj = { ...obj, root: newRoot }; const newObj = { ...obj, root: newRoot };
delete newObj.root.log; delete newObj.root.log;
toDelete.forEach((key) => {
delete newObj.root[key];
});
return newObj; return newObj;
} }

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