Skip to content

Commit

Permalink
integrate wat codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Aug 23, 2024
1 parent 32c5e0d commit 5e33660
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { NoInfer } from "../../wazum/dist/utils";
import { Node } from "../ast/nodes/Node";
import { BranchStatement as BranchStatement } from "../ast/nodes/BranchStatement";
import { BranchToStatement } from "../ast/nodes/BranchToStatement";
import { IfStatement } from "../ast/nodes/IfStatement";

let offset: number = 0;
export class Generator {
Expand Down Expand Up @@ -70,6 +71,8 @@ export class Generator {
body.push(this.parseReturnStatement(stmt));
else if (stmt instanceof BranchStatement)
body.push(this.parseBranch(stmt));
else if (stmt instanceof IfStatement)
body.push(this.parseIfStmt(stmt));
else
throw new Error(
"Could not parse body of FunctionDeclaration to wasm equivalent!",
Expand Down Expand Up @@ -98,6 +101,12 @@ export class Generator {
w.branch("a"),
]);
}
parseIfStmt(node: IfStatement): w.BranchIf {
return w.branchIf(
"block",
w.constant("i32", 1)
)
}
parseVariable(node: VariableDeclaration): w.LocalSet {
if (node.value instanceof StringLiteral) {
const value = this.parseStringLiteral(node.value as StringLiteral);
Expand Down
7 changes: 6 additions & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { writeFileSync } from "fs";
import { Parser } from "./parser";
import { Tokenizer } from "./tokenizer";
import { Transpile } from "./transpiler/transpiler";
import { Generator } from "./generator";

const start = Date.now();
const tokenizer = new Tokenizer(`
Expand All @@ -21,9 +22,13 @@ fn main(a: i32, b: i32) -> i32 {
console.dir(tokenizer.getAll(), { depth: 10 });
const parser = new Parser(tokenizer, "test.zp");
const program = parser.parseSource();
console.dir(program, { depth: 10 });
console.dir(program.topLevelStatements, { depth: 1 });

const transpiled = Transpile.from(program);
console.log("Transpiled:\n" + transpiled);

const generator = new Generator();
generator.parseFn(program.topLevelStatements[1])
console.log("WAT:\n" + generator.toWat());

writeFileSync("./test.ts", transpiled);

0 comments on commit 5e33660

Please sign in to comment.