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";
|
2022-11-21 08:57:44 +11:00
|
|
|
import { executor, ProgramMemory } from "./executor";
|
2022-11-14 14:04:23 +11:00
|
|
|
|
|
|
|
describe("test", () => {
|
|
|
|
it("test assigning two variables, the second summing with the first", () => {
|
|
|
|
const code = `const myVar = 5
|
|
|
|
const newVar = myVar + 1`;
|
2022-11-21 08:57:44 +11:00
|
|
|
const { root } = exe(code);
|
|
|
|
expect(root.myVar).toBe(5);
|
|
|
|
expect(root.newVar).toBe(6);
|
2022-11-14 14:04:23 +11:00
|
|
|
});
|
|
|
|
it("test assigning a var with a string", () => {
|
2022-11-20 09:41:21 +11:00
|
|
|
const code = `const myVar = "a str"`;
|
2022-11-21 08:57:44 +11:00
|
|
|
const { root } = exe(code);
|
|
|
|
expect(root.myVar).toBe("a str");
|
2022-11-14 14:04:23 +11:00
|
|
|
});
|
|
|
|
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"
|
|
|
|
);
|
2022-11-21 08:57:44 +11:00
|
|
|
const { root } = exe(code);
|
|
|
|
expect(root.myVar).toBe("a str another str");
|
2022-11-14 14:04:23 +11:00
|
|
|
});
|
|
|
|
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
|
|
|
};
|
2022-11-21 08:57:44 +11:00
|
|
|
const { root } = executor(abstractSyntaxTree(lexer(code)), {
|
2022-11-20 09:41:21 +11:00
|
|
|
root: programMemoryOverride,
|
2022-11-21 08:57:44 +11:00
|
|
|
_sketch: [],
|
2022-11-20 09:41:21 +11:00
|
|
|
});
|
2022-11-21 08:57:44 +11:00
|
|
|
expect(root.myVar).toBe("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 = () => {}", () => {
|
2022-11-21 08:57:44 +11:00
|
|
|
const { root } = exe(
|
2022-11-20 09:41:21 +11:00
|
|
|
[
|
|
|
|
"fn funcN = (a, b) => {",
|
|
|
|
" return a + b",
|
|
|
|
"}",
|
2022-11-20 09:56:18 +11:00
|
|
|
"const theVar = 60",
|
|
|
|
"const magicNum = funcN(9, theVar)",
|
2022-11-20 09:41:21 +11:00
|
|
|
].join("\n")
|
|
|
|
);
|
2022-11-21 08:57:44 +11:00
|
|
|
expect(root.theVar).toBe(60);
|
|
|
|
expect(root.magicNum).toBe(69);
|
|
|
|
});
|
|
|
|
it("sketch declaration", () => {
|
|
|
|
let code = `sketch mySketch {
|
|
|
|
path myPath = lineTo(0,1)
|
|
|
|
lineTo(1,1)
|
|
|
|
path rightPath = lineTo(1,0)
|
|
|
|
close()
|
|
|
|
}
|
2022-11-21 09:16:24 +11:00
|
|
|
show(mySketch)
|
2022-11-21 08:57:44 +11:00
|
|
|
`;
|
2022-11-21 09:16:24 +11:00
|
|
|
const { root, return: _return } = exe(code);
|
2022-11-21 08:57:44 +11:00
|
|
|
expect(root.mySketch.map(({ previousPath, ...rest }: any) => rest)).toEqual(
|
|
|
|
[
|
|
|
|
{ type: "base", from: [0, 0] },
|
|
|
|
{ type: "toPoint", to: [0, 1], name: "myPath" },
|
|
|
|
{ type: "toPoint", to: [1, 1] },
|
|
|
|
{ type: "toPoint", to: [1, 0], name: "rightPath" },
|
|
|
|
{ type: "close", firstPath: { type: "base", from: [0, 0] } },
|
|
|
|
]
|
|
|
|
);
|
|
|
|
expect(root.mySketch[0]).toEqual(root.mySketch[4].firstPath);
|
2022-11-21 09:16:24 +11:00
|
|
|
// hmm not sure what handle the "show" function
|
|
|
|
expect(_return).toEqual([
|
|
|
|
{
|
|
|
|
type: "Identifier",
|
|
|
|
start: 108,
|
|
|
|
end: 116,
|
|
|
|
name: "mySketch",
|
|
|
|
},
|
|
|
|
]);
|
2022-11-20 09:41:21 +11:00
|
|
|
});
|
2022-11-14 14:04:23 +11:00
|
|
|
});
|
|
|
|
|
|
|
|
// helpers
|
|
|
|
|
2022-11-20 09:41:21 +11:00
|
|
|
function exe(
|
|
|
|
code: string,
|
2022-11-21 08:57:44 +11:00
|
|
|
programMemory: ProgramMemory = { root: {}, _sketch: [] }
|
2022-11-20 09:41:21 +11:00
|
|
|
) {
|
|
|
|
const tokens = lexer(code);
|
|
|
|
const ast = abstractSyntaxTree(tokens);
|
|
|
|
return executor(ast, programMemory);
|
|
|
|
}
|