Skip to content

Commit 7c8b3df

Browse files
author
jonas.ong
committed
add if statement
1 parent 015a3b0 commit 7c8b3df

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/wasm-compiler/builderGenerator.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,21 @@ ${args.map(
532532
return wasm.nop();
533533
}
534534

535+
visitIfStmt(stmt: StmtNS.If): WasmInstruction {
536+
const condition = this.visit(stmt.condition);
537+
const body = stmt.body.map((b) => this.visit(b));
538+
const elseBody = stmt.elseBlock?.map((e) => this.visit(e));
539+
540+
return elseBody
541+
? wasm
542+
.if(i32.wrap_i64(wasm.call(BOOLISE_FX).args(condition)))
543+
.then(...body)
544+
.else(...elseBody)
545+
: wasm
546+
.if(i32.wrap_i64(wasm.call(BOOLISE_FX).args(condition)))
547+
.then(...body);
548+
}
549+
535550
// UNIMPLEMENTED PYTHON CONSTRUCTS
536551
visitLambdaExpr(expr: ExprNS.Lambda): WasmNumeric {
537552
throw new Error("Method not implemented.");
@@ -566,9 +581,6 @@ ${args.map(
566581
visitAssertStmt(stmt: StmtNS.Assert): WasmInstruction {
567582
throw new Error("Method not implemented.");
568583
}
569-
visitIfStmt(stmt: StmtNS.If): WasmInstruction {
570-
throw new Error("Method not implemented.");
571-
}
572584
visitWhileStmt(stmt: StmtNS.While): WasmInstruction {
573585
throw new Error("Method not implemented.");
574586
}

src/wasm-compiler/constants.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const ERROR_MAP = {
1919
LOG_UNKNOWN_TYPE: [1, "Calling log on an unknown runtime type."],
2020
ARITH_OP_UNKNOWN_TYPE: [2, "Calling an arithmetic operation on an unsupported runtime type."],
2121
COMPLEX_COMPARISON: [3, "Using an unsupported comparison operator on complex type."],
22-
COMPARE_OP_UNKNOWN_TYPE: [4, "Calling a comparison operation on an unsupported runtime type."],
22+
COMPARE_OP_UNKNOWN_TYPE: [4, "Calling a comparison operation on unsupported operands."],
2323
CALL_NOT_FX: [5, "Calling a non-function value."],
2424
FUNC_WRONG_ARITY: [6, "Calling function with wrong number of arguments."],
2525
UNBOUND: [7, "Accessing an unbound value."],
@@ -624,7 +624,17 @@ export const COMPARISON_OP_FX = wasm
624624
)
625625
),
626626

627-
// else, unreachable
627+
// else, default to not equal
628+
wasm
629+
.if(i32.eq(local.get("$op"), i32.const(COMPARISON_OP_TAG.EQ)))
630+
.then(wasm.return(wasm.call(MAKE_BOOL_FX).args(i32.const(0))))
631+
.else(
632+
wasm
633+
.if(i32.eq(local.get("$op"), i32.const(COMPARISON_OP_TAG.NEQ)))
634+
.then(wasm.return(wasm.call(MAKE_BOOL_FX).args(i32.const(1))))
635+
),
636+
637+
// other operators: unreachable
628638
wasm.call("$_log_error").args(i32.const(ERROR_MAP.COMPARE_OP_UNKNOWN_TYPE[0])),
629639
wasm.unreachable()
630640
);

0 commit comments

Comments
 (0)