2022-11-20 09:41:21 +11:00
|
|
|
import fs from "node:fs";
|
2022-11-14 14:04:23 +11:00
|
|
|
|
|
|
|
import { abstractSyntaxTree } from "./abstractSyntaxTree";
|
|
|
|
import { lexer } from "./tokeniser";
|
|
|
|
import { executor } from "./executor";
|
|
|
|
|
|
|
|
describe("test", () => {
|
|
|
|
it("test assigning two variables, the second summing with the first", () => {
|
|
|
|
const code = `const myVar = 5
|
|
|
|
const newVar = myVar + 1`;
|
2022-11-20 09:41:21 +11:00
|
|
|
const programMemory = exe(code);
|
2022-11-14 14:04:23 +11:00
|
|
|
expect(withoutStdFns(programMemory)).toEqual({
|
|
|
|
root: {
|
|
|
|
myVar: 5,
|
|
|
|
newVar: 6,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("test assigning a var with a string", () => {
|
2022-11-20 09:41:21 +11:00
|
|
|
const code = `const myVar = "a str"`;
|
|
|
|
const programMemory = exe(code);
|
2022-11-14 14:04:23 +11:00
|
|
|
expect(withoutStdFns(programMemory)).toEqual({
|
|
|
|
root: {
|
|
|
|
myVar: "a str",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("test assigning a var by cont concatenating two strings string", () => {
|
2022-11-20 09:41:21 +11:00
|
|
|
const code = fs.readFileSync(
|
|
|
|
"./src/lang/testExamples/variableDeclaration.cado",
|
|
|
|
"utf-8"
|
|
|
|
);
|
|
|
|
const programMemory = exe(code);
|
2022-11-14 14:04:23 +11:00
|
|
|
expect(withoutStdFns(programMemory)).toEqual({
|
|
|
|
root: {
|
|
|
|
myVar: "a str another str",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("test with function call", () => {
|
|
|
|
const code = `
|
|
|
|
const myVar = "hello"
|
2022-11-20 09:41:21 +11:00
|
|
|
log(5, myVar)`;
|
2022-11-14 14:04:23 +11:00
|
|
|
const programMemoryOverride = {
|
|
|
|
log: jest.fn(),
|
2022-11-20 09:41:21 +11:00
|
|
|
};
|
|
|
|
const programMemory = executor(abstractSyntaxTree(lexer(code)), {
|
|
|
|
root: programMemoryOverride,
|
|
|
|
});
|
2022-11-14 14:04:23 +11:00
|
|
|
expect(withoutStdFns(programMemory)).toEqual({
|
2022-11-20 09:41:21 +11:00
|
|
|
root: { myVar: "hello" },
|
2022-11-14 14:04:23 +11:00
|
|
|
});
|
|
|
|
expect(programMemoryOverride.log).toHaveBeenCalledWith(5, "hello");
|
2022-11-20 09:41:21 +11:00
|
|
|
});
|
|
|
|
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 },
|
|
|
|
});
|
|
|
|
});
|
2022-11-14 14:04:23 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
// helpers
|
|
|
|
|
2022-11-20 09:41:21 +11:00
|
|
|
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[] = []) {
|
2022-11-14 14:04:23 +11:00
|
|
|
const newRoot = { ...obj.root };
|
|
|
|
const newObj = { ...obj, root: newRoot };
|
|
|
|
delete newObj.root.log;
|
2022-11-20 09:41:21 +11:00
|
|
|
toDelete.forEach((key) => {
|
|
|
|
delete newObj.root[key];
|
|
|
|
});
|
2022-11-14 14:04:23 +11:00
|
|
|
return newObj;
|
2022-11-20 09:41:21 +11:00
|
|
|
}
|