From 81ba92018c8e481390ab097b12867fc3759013eb Mon Sep 17 00:00:00 2001 From: Jorge Acereda Date: Fri, 15 Nov 2024 19:19:32 +0100 Subject: [PATCH] Lame PoC for source-level debugging --- gx.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gx.go b/gx.go index 21d9742..c9a88e2 100644 --- a/gx.go +++ b/gx.go @@ -532,6 +532,15 @@ func (c *Compiler) genFuncDecl(decl *ast.FuncDecl) string { return result } +func (c *Compiler) writePos(node ast.Node) { + c.write("\n#line ") + p := c.fileSet.Position(node.Pos()) + c.write(strconv.Itoa(p.Line)) + c.write(" \"") + c.write(strings.Replace(p.Filename, "\\", "/", -1)) + c.write("\"\n") +} + // // Expressions // @@ -1113,6 +1122,7 @@ func (c *Compiler) writeDeclStmt(declStmt *ast.DeclStmt) { } func (c *Compiler) writeStmt(stmt ast.Stmt) { + c.writePos(stmt) switch stmt := stmt.(type) { case *ast.ExprStmt: c.writeExprStmt(stmt) @@ -1626,6 +1636,7 @@ func (c *Compiler) compile() { c.write("//\n// Function definitions\n//\n") for _, funcDecl := range funcDecls { if funcDecl.Body != nil { + c.writePos(funcDecl) c.write("\n") c.write(c.genFuncDecl(funcDecl)) c.write(" ")