From f90fcd431f6c03daf23694851369691951cacc83 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Tue, 17 Dec 2024 10:24:18 +0100 Subject: [PATCH] source map else --- compiler/ast.ts | 2 +- compiler/compiler.ts | 2 +- compiler/lowerer.ts | 2 +- compiler/parser.ts | 5 +++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/compiler/ast.ts b/compiler/ast.ts index 4159b96..aeb278d 100644 --- a/compiler/ast.ts +++ b/compiler/ast.ts @@ -45,7 +45,7 @@ export type ExprKind = | { type: "call"; subject: Expr; args: Expr[] } | { type: "unary"; unaryType: UnaryType; subject: Expr } | { type: "binary"; binaryType: BinaryType; left: Expr; right: Expr } - | { type: "if"; cond: Expr; truthy: Expr; falsy?: Expr } + | { type: "if"; cond: Expr; truthy: Expr; falsy?: Expr; elsePos?: Pos } | { type: "bool"; value: boolean } | { type: "null" } | { type: "loop"; body: Expr } diff --git a/compiler/compiler.ts b/compiler/compiler.ts index ec8666e..2b44225 100644 --- a/compiler/compiler.ts +++ b/compiler/compiler.ts @@ -47,7 +47,7 @@ export class Compiler { const lowerer = new Lowerer(lexer.currentPos()); lowerer.lower(ast); - lowerer.printProgram(); + // lowerer.printProgram(); const { program, fnNames } = lowerer.finish(); return { program, fnNames }; diff --git a/compiler/lowerer.ts b/compiler/lowerer.ts index 95c8fc3..aa54231 100644 --- a/compiler/lowerer.ts +++ b/compiler/lowerer.ts @@ -490,7 +490,7 @@ export class Lowerer { this.program.setLabel(falseLabel); if (expr.kind.falsy) { - this.addSourceMap(expr.kind.falsy.pos); + this.addSourceMap(expr.kind.elsePos!); this.lowerExpr(expr.kind.falsy); } else { this.program.add(Ops.PushNull); diff --git a/compiler/parser.ts b/compiler/parser.ts index 04f7e46..6904f7e 100644 --- a/compiler/parser.ts +++ b/compiler/parser.ts @@ -456,17 +456,18 @@ export class Parser { if (!this.test("else")) { return this.expr({ type: "if", cond, truthy }, pos); } + const elsePos = this.pos(); this.step(); if (this.test("if")) { const falsy = this.parseIf(); - return this.expr({ type: "if", cond, truthy, falsy }, pos); + return this.expr({ type: "if", cond, truthy, falsy, elsePos }, pos); } if (!this.test("{")) { this.report("expected block"); return this.expr({ type: "error" }, pos); } const falsy = this.parseBlock(); - return this.expr({ type: "if", cond, truthy, falsy }, pos); + return this.expr({ type: "if", cond, truthy, falsy, elsePos }, pos); } private parseBinary(): Expr {