diff options
| author | Mel <einebeere@gmail.com> | 2022-06-07 10:18:32 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-07 10:18:32 +0000 |
| commit | bc35c4f5cb981405050957833ff6d87733032d5c (patch) | |
| tree | 067ba06ca67100f483bd1054bcd7a7904c61ef3f /pkg/lang/vm | |
| parent | 144f49d64e3fc11d334900e90d30c8620ca2991b (diff) | |
| download | jinx-bc35c4f5cb981405050957833ff6d87733032d5c.tar.zst jinx-bc35c4f5cb981405050957833ff6d87733032d5c.zip | |
Marginally better bytecode builder line API
Diffstat (limited to 'pkg/lang/vm')
| -rw-r--r-- | pkg/lang/vm/code/builder.go | 38 | ||||
| -rw-r--r-- | pkg/lang/vm/text/compiler.go | 6 |
2 files changed, 36 insertions, 8 deletions
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 { |
