correct assign source maps

This commit is contained in:
SimonFJ20 2024-12-13 12:12:16 +01:00
parent fccced6174
commit 2d0e401bf3
3 changed files with 43 additions and 8 deletions

View File

@ -119,3 +119,33 @@ export type Anno = {
values: Expr[];
pos: Pos;
};
export function stmtToString(stmt: Stmt): string {
const body = (() => {
switch (stmt.kind.type) {
case "assign":
return `{ subject: ${exprToString(stmt.kind.subject)}, value: ${
exprToString(stmt.kind.value)
} }`;
}
return "(<not implemented>)";
})();
const { line } = stmt.pos;
return `${stmt.kind.type}:${line}${body}`;
}
export function exprToString(expr: Expr): string {
const body = (() => {
switch (expr.kind.type) {
case "binary":
return `(${
exprToString(expr.kind.left)
} ${expr.kind.binaryType} ${exprToString(expr.kind.right)})`;
case "sym":
return `(${expr.kind.ident})`;
}
return "(<not implemented>)";
})();
const { line } = expr.pos;
return `${expr.kind.type}:${line}${body}`;
}

View File

@ -1,5 +1,5 @@
import { Builtins } from "./arch.ts";
import { Expr, Stmt } from "./ast.ts";
import { Expr, Stmt, stmtToString } from "./ast.ts";
import { LocalLeaf, Locals, LocalsFnRoot } from "./lowerer_locals.ts";
import { Ops } from "./mod.ts";
import { Assembler, Label } from "./assembler.ts";
@ -68,7 +68,6 @@ export class Lowerer {
}
private lowerStmt(stmt: Stmt) {
this.addSourceMap(stmt.pos);
switch (stmt.kind.type) {
case "error":
break;
@ -406,17 +405,17 @@ export class Lowerer {
if (expr.kind.type !== "loop") {
throw new Error();
}
const contineLabel = this.program.makeLabel();
const continueLabel = this.program.makeLabel();
const breakLabel = this.program.makeLabel();
this.breakStack.push(breakLabel);
this.program.setLabel(contineLabel);
this.program.setLabel(continueLabel);
this.addSourceMap(expr.kind.body.pos);
this.lowerExpr(expr.kind.body);
this.program.add(Ops.Pop);
this.addClearingSourceMap();
this.program.add(Ops.PushPtr, contineLabel);
this.program.add(Ops.PushPtr, continueLabel);
this.program.add(Ops.Jump);
this.program.setLabel(breakLabel);
if (expr.vtype!.type === "null") {
@ -433,9 +432,15 @@ export class Lowerer {
this.locals = new LocalLeaf(this.locals);
this.scoutFnHeaders(expr.kind.stmts);
for (const stmt of expr.kind.stmts) {
console.log(`sm for stmt ${stmt.kind.type} ${stmt.pos.line}`);
if (stmt.kind.type === "assign") {
console.log(` - ${stmtToString(stmt)}`);
}
this.addSourceMap(stmt.pos);
this.lowerStmt(stmt);
}
if (expr.kind.expr) {
this.addSourceMap(expr.kind.expr.pos);
this.lowerExpr(expr.kind.expr);
} else {
this.program.add(Ops.PushNull);

View File

@ -107,10 +107,10 @@ export class Parser {
stmts.push(this.parseSingleLineBlockStmt());
stmts.push(this.parseFn());
} else if (this.test("{") || this.test("if") || this.test("loop")) {
let expr = this.parseMultiLineBlockExpr();
const expr = this.parseMultiLineBlockExpr();
if (this.test("}")) {
this.step();
return this.expr({ type: "block", stmts, expr }, pos);
return this.expr({ type: "block", stmts, expr }, expr.pos);
}
stmts.push(this.stmt({ type: "expr", expr }, expr.pos));
} else {
@ -122,7 +122,7 @@ export class Parser {
stmts.push(
this.stmt(
{ type: "assign", subject: expr, value },
pos,
expr.pos,
),
);
} else if (this.test(";")) {