inital basical executor
This commit is contained in:
@ -370,6 +370,9 @@ export const abstractSyntaxTree = (tokens: Token[]): Program => {
|
||||
if (typeof token === "undefined") {
|
||||
console.log("probably should throw");
|
||||
}
|
||||
if (token.type === 'whitespace') {
|
||||
return startTree(tokens, tokenIndex + 1, previousBody);
|
||||
}
|
||||
if (token.type === "word" && token.value === "const") {
|
||||
const { declaration, lastIndex } = makeVariableDeclaration(
|
||||
tokens,
|
||||
|
65
src/lang/executor.test.ts
Normal file
65
src/lang/executor.test.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import fs from "node:fs"
|
||||
|
||||
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`;
|
||||
const tokens = lexer(code);
|
||||
const ast = abstractSyntaxTree(tokens);
|
||||
const programMemory = executor(ast);
|
||||
expect(withoutStdFns(programMemory)).toEqual({
|
||||
root: {
|
||||
myVar: 5,
|
||||
newVar: 6,
|
||||
},
|
||||
});
|
||||
});
|
||||
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);
|
||||
expect(withoutStdFns(programMemory)).toEqual({
|
||||
root: {
|
||||
myVar: "a str",
|
||||
},
|
||||
});
|
||||
});
|
||||
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);
|
||||
expect(withoutStdFns(programMemory)).toEqual({
|
||||
root: {
|
||||
myVar: "a str another str",
|
||||
},
|
||||
});
|
||||
});
|
||||
it("test with function call", () => {
|
||||
const code = `
|
||||
const myVar = "hello"
|
||||
log(5, myVar)`
|
||||
const programMemoryOverride = {
|
||||
log: jest.fn(),
|
||||
}
|
||||
const programMemory = executor(abstractSyntaxTree(lexer(code)), programMemoryOverride);
|
||||
expect(withoutStdFns(programMemory)).toEqual({
|
||||
root: {myVar: "hello"},
|
||||
});
|
||||
expect(programMemoryOverride.log).toHaveBeenCalledWith(5, "hello");
|
||||
})
|
||||
});
|
||||
|
||||
// helpers
|
||||
|
||||
function withoutStdFns(obj: any) {
|
||||
const newRoot = { ...obj.root };
|
||||
const newObj = { ...obj, root: newRoot };
|
||||
delete newObj.root.log;
|
||||
return newObj;
|
||||
}
|
45
src/lang/executor.ts
Normal file
45
src/lang/executor.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { Program, BinaryPart } from "./abstractSyntaxTree";
|
||||
|
||||
export const executor = (ast: Program, rootOverride: {[key: string]: any} = {}): any => {
|
||||
const programMemory: { [key: string]: any } = {
|
||||
root: {
|
||||
...rootOverride
|
||||
},
|
||||
};
|
||||
const { body } = ast;
|
||||
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;
|
||||
} 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;
|
||||
}
|
||||
});
|
||||
} else if (statement.type === "ExpressionStatement") {
|
||||
const expression = statement.expression;
|
||||
if (expression.type === "CallExpression") {
|
||||
const functionName = expression.callee.name;
|
||||
const args = expression.arguments.map((arg) => {
|
||||
if (arg.type === "Literal") {
|
||||
return arg.value;
|
||||
} else if (arg.type === "Identifier") {
|
||||
return programMemory.root[arg.name];
|
||||
}
|
||||
});
|
||||
programMemory.root[functionName](...args);
|
||||
}
|
||||
}
|
||||
});
|
||||
return programMemory;
|
||||
};
|
Reference in New Issue
Block a user