xgo/compiler/bytecode.go

56 lines
1.2 KiB
Go
Raw Normal View History

2019-01-09 10:17:42 +03:00
package compiler
import (
"encoding/gob"
"io"
"github.com/d5/tengo/objects"
)
// Bytecode is a compiled instructions and constants.
2019-01-09 10:17:42 +03:00
type Bytecode struct {
Instructions []byte
Constants []objects.Object
}
// Decode reads Bytecode data from the reader.
2019-01-09 10:17:42 +03:00
func (b *Bytecode) Decode(r io.Reader) error {
dec := gob.NewDecoder(r)
if err := dec.Decode(&b.Instructions); err != nil {
return err
}
return dec.Decode(&b.Constants)
2019-01-09 10:17:42 +03:00
}
// Encode writes Bytecode data to the writer.
2019-01-09 10:17:42 +03:00
func (b *Bytecode) Encode(w io.Writer) error {
enc := gob.NewEncoder(w)
if err := enc.Encode(b.Instructions); err != nil {
return err
}
return enc.Encode(b.Constants)
2019-01-09 10:17:42 +03:00
}
func init() {
gob.Register(&objects.Int{})
gob.Register(&objects.Float{})
gob.Register(&objects.String{})
gob.Register(&objects.Bool{})
gob.Register(&objects.Char{})
gob.Register(&objects.Array{})
gob.Register(&objects.Map{})
gob.Register(&objects.CompiledFunction{})
gob.Register(&objects.Undefined{})
2019-01-18 12:43:46 +03:00
gob.Register(&objects.Error{})
gob.Register(&objects.ImmutableMap{})
gob.Register(&objects.Bytes{})
gob.Register(&objects.StringIterator{})
gob.Register(&objects.MapIterator{})
gob.Register(&objects.ImmutableMapIterator{})
gob.Register(&objects.ArrayIterator{})
2019-01-09 10:17:42 +03:00
}