add Bytecode.FormatInstructions and Bytecode.FormatConstants ()

This commit is contained in:
Daniel 2019-02-10 21:44:22 -08:00 committed by GitHub
parent 9c67444678
commit 9ccc6dd901
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 33 deletions

View file

@ -2,7 +2,9 @@ package compiler
import (
"encoding/gob"
"fmt"
"io"
"reflect"
"github.com/d5/tengo/objects"
)
@ -45,6 +47,35 @@ func (b *Bytecode) Encode(w io.Writer) error {
return enc.Encode(b.Constants)
}
// FormatInstructions returns human readable string representations of
// compiled instructions.
func (b *Bytecode) FormatInstructions() []string {
return FormatInstructions(b.Instructions, 0)
}
// FormatConstants returns human readable string representations of
// compiled constants.
func (b *Bytecode) FormatConstants() (output []string) {
for cidx, cn := range b.Constants {
switch cn := cn.(type) {
case *objects.CompiledFunction:
output = append(output, fmt.Sprintf("[% 3d] (Compiled Function|%p)", cidx, &cn))
for _, l := range FormatInstructions(cn.Instructions, 0) {
output = append(output, fmt.Sprintf(" %s", l))
}
case *objects.CompiledModule:
output = append(output, fmt.Sprintf("[% 3d] (Compiled Module|%p)", cidx, &cn))
for _, l := range FormatInstructions(cn.Instructions, 0) {
output = append(output, fmt.Sprintf(" %s", l))
}
default:
output = append(output, fmt.Sprintf("[% 3d] %s (%s|%p)", cidx, cn, reflect.TypeOf(cn).Elem().Name(), &cn))
}
}
return
}
func cleanupObjects(o objects.Object) objects.Object {
switch o := o.(type) {
case *objects.Bool:

View file

@ -2,7 +2,6 @@ package compiler_test
import (
"fmt"
"reflect"
"strings"
"testing"
@ -985,20 +984,10 @@ func traceCompile(input string, symbols map[string]objects.Object) (res *compile
err = c.Compile(parsed)
{
trace = append(trace, fmt.Sprintf("Compiler Trace:\n%s", strings.Join(tr.Out, "")))
bytecode := c.Bytecode()
var constStr []string
for cidx, cn := range bytecode.Constants {
if cmFn, ok := cn.(*objects.CompiledFunction); ok {
constStr = append(constStr, fmt.Sprintf("[% 3d] (Compiled Function|%p)", cidx, cn))
for _, l := range compiler.FormatInstructions(cmFn.Instructions, 0) {
constStr = append(constStr, fmt.Sprintf(" %s", l))
}
} else {
constStr = append(constStr, fmt.Sprintf("[% 3d] %s (%s|%p)", cidx, cn, reflect.TypeOf(cn).Name(), cn))
}
}
trace = append(trace, fmt.Sprintf("Compiled Constants:\n%s", strings.Join(constStr, "\n")))
trace = append(trace, fmt.Sprintf("Compiled Instructions:\n%s\n", strings.Join(compiler.FormatInstructions(bytecode.Instructions, 0), "\n")))
trace = append(trace, fmt.Sprintf("Compiled Constants:\n%s", strings.Join(bytecode.FormatConstants(), "\n")))
trace = append(trace, fmt.Sprintf("Compiled Instructions:\n%s\n", strings.Join(bytecode.FormatInstructions(), "\n")))
}
if err != nil {
return

View file

@ -245,25 +245,8 @@ func traceCompileRun(file *ast.File, symbols map[string]objects.Object, userModu
}
bytecode := c.Bytecode()
var constStr []string
for cidx, cn := range bytecode.Constants {
switch cn := cn.(type) {
case *objects.CompiledFunction:
constStr = append(constStr, fmt.Sprintf("[% 3d] (Compiled Function|%p)", cidx, &cn))
for _, l := range compiler.FormatInstructions(cn.Instructions, 0) {
constStr = append(constStr, fmt.Sprintf(" %s", l))
}
case *objects.CompiledModule:
constStr = append(constStr, fmt.Sprintf("[% 3d] (Compiled Module|%p)", cidx, &cn))
for _, l := range compiler.FormatInstructions(cn.Instructions, 0) {
constStr = append(constStr, fmt.Sprintf(" %s", l))
}
default:
constStr = append(constStr, fmt.Sprintf("[% 3d] %s (%s|%p)", cidx, cn, reflect.TypeOf(cn).Elem().Name(), &cn))
}
}
trace = append(trace, fmt.Sprintf("\n[Compiled Constants]\n\n%s", strings.Join(constStr, "\n")))
trace = append(trace, fmt.Sprintf("\n[Compiled Instructions]\n\n%s\n", strings.Join(compiler.FormatInstructions(bytecode.Instructions, 0), "\n")))
trace = append(trace, fmt.Sprintf("\n[Compiled Constants]\n\n%s", strings.Join(bytecode.FormatConstants(), "\n")))
trace = append(trace, fmt.Sprintf("\n[Compiled Instructions]\n\n%s\n", strings.Join(bytecode.FormatInstructions(), "\n")))
v = runtime.NewVM(bytecode, globals)