diff --git a/compiler/ast.ts b/compiler/ast.ts index 38bfe0a..4a88117 100644 --- a/compiler/ast.ts +++ b/compiler/ast.ts @@ -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 "()"; + })(); + 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 "()"; + })(); + const { line } = expr.pos; + return `${expr.kind.type}:${line}${body}`; +} diff --git a/compiler/lowerer.ts b/compiler/lowerer.ts index f2b8f7c..53a4077 100644 --- a/compiler/lowerer.ts +++ b/compiler/lowerer.ts @@ -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); diff --git a/compiler/parser.ts b/compiler/parser.ts index 90c1f1a..757b85e 100644 --- a/compiler/parser.ts +++ b/compiler/parser.ts @@ -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(";")) {