small changes
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user