mirror of
https://github.com/Noratrieb/riverdelta.git
synced 2026-01-15 00:45:04 +01:00
Add assignments
This commit is contained in:
parent
6d2a2fe474
commit
0bf9aed35e
6 changed files with 113 additions and 32 deletions
39
src/lower.ts
39
src/lower.ts
|
|
@ -272,6 +272,36 @@ function lowerExpr(fcx: FuncContext, instrs: wasm.Instr[], expr: Expr) {
|
|||
|
||||
break;
|
||||
}
|
||||
case "assign": {
|
||||
lowerExpr(fcx, instrs, expr.rhs);
|
||||
const { lhs } = expr;
|
||||
switch (lhs.kind) {
|
||||
case "ident": {
|
||||
const res = lhs.value.res!;
|
||||
|
||||
switch (res.kind) {
|
||||
case "local": {
|
||||
const location =
|
||||
fcx.varLocations[fcx.varLocations.length - 1 - res.index];
|
||||
storeVariable(instrs, location);
|
||||
break;
|
||||
}
|
||||
case "item": {
|
||||
throw new Error("cannot store to item");
|
||||
}
|
||||
case "builtin": {
|
||||
throw new Error("cannot store to builtin");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw new Error("invalid lhs side of assignment");
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "block": {
|
||||
const prevVarLocationLengths = fcx.varLocations.length;
|
||||
|
||||
|
|
@ -610,6 +640,15 @@ function loadVariable(instrs: wasm.Instr[], loc: VarLocation) {
|
|||
});
|
||||
}
|
||||
|
||||
function storeVariable(instrs: wasm.Instr[], loc: VarLocation) {
|
||||
// Stores are just like loads, just the other way around.
|
||||
const types = loc.types.map((_, i) => i);
|
||||
types.reverse();
|
||||
types.forEach((i) => {
|
||||
instrs.push({ kind: "local.set", imm: loc.localIdx + i });
|
||||
});
|
||||
}
|
||||
|
||||
function computeAbi(ty: TyFn): FnAbi {
|
||||
function argRetAbi(param: Ty): ArgRetAbi {
|
||||
switch (param.kind) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue