mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 13:06:30 +00:00
refactor annotation lowering code
This commit is contained in:
parent
483a5081e3
commit
5216c3bef0
@ -116,6 +116,30 @@ export class Lowerer {
|
|||||||
this.program.add(Ops.Jump, this.breakStack.at(-1)!);
|
this.program.add(Ops.Jump, this.breakStack.at(-1)!);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private lowerBuiltinAnno(annoArgs: Expr[]) {
|
||||||
|
if (annoArgs.length !== 1) {
|
||||||
|
throw new Error("invalid # of arguments to builtin annotation");
|
||||||
|
}
|
||||||
|
const anno = annoArgs[0];
|
||||||
|
if (anno.kind.type !== "ident") {
|
||||||
|
throw new Error(
|
||||||
|
`unexpected argument type '${anno.kind.type}' expected 'ident'`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const value = anno.kind.value;
|
||||||
|
switch (value) {
|
||||||
|
case "print": {
|
||||||
|
this.program.add(Ops.Builtin, Builtins.Print);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
throw new Error(
|
||||||
|
`unrecognized builtin '${value}'`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private lowerFnStmt(stmt: Stmt) {
|
private lowerFnStmt(stmt: Stmt) {
|
||||||
if (stmt.kind.type !== "fn") {
|
if (stmt.kind.type !== "fn") {
|
||||||
throw new Error();
|
throw new Error();
|
||||||
@ -135,24 +159,7 @@ export class Lowerer {
|
|||||||
this.locals.allocSym(ident);
|
this.locals.allocSym(ident);
|
||||||
}
|
}
|
||||||
if (stmt.kind.anno?.ident === "builtin") {
|
if (stmt.kind.anno?.ident === "builtin") {
|
||||||
const anno = stmt.kind.anno.values[0];
|
this.lowerBuiltinAnno(stmt.kind.anno.values);
|
||||||
if (!anno) {
|
|
||||||
throw new Error("builtin annotation without ident");
|
|
||||||
}
|
|
||||||
if (anno.kind.type !== "ident") {
|
|
||||||
throw new Error("builtin annotation without ident");
|
|
||||||
}
|
|
||||||
const value = anno.kind.value;
|
|
||||||
switch (value) {
|
|
||||||
case "print": {
|
|
||||||
this.program.add(Ops.LoadLocal, 0);
|
|
||||||
this.program.add(Ops.Builtin, Builtins.Print);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
throw new Error("unrecognized builtin");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.lowerExpr(stmt.kind.body);
|
this.lowerExpr(stmt.kind.body);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user