Skip to content

Commit d52f956

Browse files
author
jonas.ong
committed
add lambda expressions
1 parent 7c8b3df commit d52f956

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/wasm-compiler/builderGenerator.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,38 @@ export class BuilderGenerator
470470
);
471471
}
472472

473+
visitLambdaExpr(expr: ExprNS.Lambda): WasmNumeric {
474+
const arity = expr.parameters.length;
475+
const tag = this.userFunctions.length;
476+
this.userFunctions.push([]); // placeholder
477+
478+
// no statements allowed in lambdas, so there won't be any new local declarations
479+
// other than parameters
480+
const newFrame = this.collectDeclarations([], expr.parameters);
481+
482+
if (tag >= 1 << 16)
483+
throw new Error("Tag cannot be above 16-bit integer limit");
484+
if (arity >= 1 << 8)
485+
throw new Error("Arity cannot be above 8-bit integer limit");
486+
if (newFrame.length > 1 << 8)
487+
throw new Error("Environment length cannot be above 8-bit integer limit");
488+
489+
this.environment.push(newFrame);
490+
const body = this.visit(expr.body);
491+
this.environment.pop();
492+
493+
this.userFunctions[tag] = [wasm.return(body)];
494+
495+
return wasm
496+
.call(MAKE_CLOSURE_FX)
497+
.args(
498+
i32.const(tag),
499+
i32.const(arity),
500+
i32.const(newFrame.length),
501+
global.get(CURR_ENV)
502+
);
503+
}
504+
473505
visitCallExpr(expr: ExprNS.Call): WasmRaw {
474506
const callee = this.visit(expr.callee);
475507
const args = expr.args.map((arg) => this.visit(arg));
@@ -548,9 +580,6 @@ ${args.map(
548580
}
549581

550582
// UNIMPLEMENTED PYTHON CONSTRUCTS
551-
visitLambdaExpr(expr: ExprNS.Lambda): WasmNumeric {
552-
throw new Error("Method not implemented.");
553-
}
554583
visitMultiLambdaExpr(expr: ExprNS.MultiLambda): WasmNumeric {
555584
throw new Error("Method not implemented.");
556585
}

0 commit comments

Comments
 (0)