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