From bc35c4f5cb981405050957833ff6d87733032d5c Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 7 Jun 2022 10:18:32 +0000 Subject: Marginally better bytecode builder line API --- pkg/lang/vm/code/builder.go | 38 ++++++++++++++++++++++++++++++++++---- pkg/lang/vm/text/compiler.go | 6 ++---- 2 files changed, 36 insertions(+), 8 deletions(-) (limited to 'pkg/lang/vm') diff --git a/pkg/lang/vm/code/builder.go b/pkg/lang/vm/code/builder.go index 3aa6b6b..51820fb 100644 --- a/pkg/lang/vm/code/builder.go +++ b/pkg/lang/vm/code/builder.go @@ -8,12 +8,17 @@ import ( type Builder struct { code []byte debugInfo DebugInfo + + currentLine int + lineStart int } func NewBuilder() Builder { return Builder{ - code: make([]byte, 0, 64), - debugInfo: NewDebugInfo("unknown file"), + code: make([]byte, 0, 64), + debugInfo: NewDebugInfo("unknown file"), + lineStart: -1, + currentLine: -1, } } @@ -39,8 +44,33 @@ func (b *Builder) AppendRaw(raw Raw) { b.code = append(b.code, raw...) } -func (b *Builder) AppendLine(line int) { - b.debugInfo.AppendLine(len(b.code)-1, line) +func (b *Builder) StartLine(line int) { + if b.lineStart != -1 { + panic("line already started") + } + + b.currentLine = line + b.lineStart = len(b.code) +} + +func (b *Builder) EndLine() { + defer func() { + b.currentLine = -1 + b.lineStart = -1 + }() + + if b.lineStart == -1 { + panic("line not started") + } + + if b.lineStart == len(b.code) { + println("line empty") + return + } + + println("line start", b.lineStart) + + b.debugInfo.AppendLine(len(b.code)-1, b.currentLine) } func (b *Builder) SetInt(at int, x int64) { diff --git a/pkg/lang/vm/text/compiler.go b/pkg/lang/vm/text/compiler.go index 68889eb..28bc9a7 100644 --- a/pkg/lang/vm/text/compiler.go +++ b/pkg/lang/vm/text/compiler.go @@ -47,11 +47,9 @@ func (cpl *Compiler) Compile() (code.Code, error) { cpl.codePos += len(line) + builder.StartLine(cpl.src.Loc().Row - 1) builder.AppendRaw(line) - - if len(line) > 0 { - builder.AppendLine(cpl.src.Loc().Row - 1) - } + builder.EndLine() } if err := cpl.linkLabels(&builder); err != nil { -- cgit 1.4.1