small changes
Some checks failed
Build / build (push) Failing after 5m28s
Publish Image / publish (push) Failing after 25s
Test / build (push) Failing after 5m23s

This commit is contained in:
Chuck Smith
2024-03-29 08:18:24 -04:00
parent fb0cebaf56
commit f65d7bfb1c
3 changed files with 97 additions and 0 deletions

View File

@@ -8,13 +8,45 @@ import (
"math"
"monkey/internal/code"
"monkey/internal/object"
"strings"
)
func Indent(text, ident string) string {
if text[len(text)-1:] == "\n" {
result := ""
for _, j := range strings.Split(text[:len(text)-1], "\n") {
result += ident + j + "\n"
}
return result
}
result := ""
for _, j := range strings.Split(strings.TrimRight(text, "\n"), "\n") {
result += ident + j + "\n"
}
return result[:len(result)-1]
}
type Bytecode struct {
Instructions code.Instructions
Constants []object.Object
}
func (b *Bytecode) String() string {
var s strings.Builder
s.WriteString("Constants:\n")
for i, c := range b.Constants {
s.WriteString(fmt.Sprintf("%02d %s\n", i, c))
if cf, ok := c.(*object.CompiledFunction); ok {
s.WriteString(fmt.Sprintf(" Instructions:\n%s\n", Indent(cf.Instructions.String(), " ")))
}
}
s.WriteString(fmt.Sprintf("Instructions:\n%s\n", b.Instructions))
return s.String()
}
type encoder struct {
bytes.Buffer
}