xgo/script/script.go

106 lines
2.3 KiB
Go
Raw Normal View History

2019-01-09 23:39:40 +03:00
package script
2019-01-09 10:17:42 +03:00
import (
"fmt"
"github.com/d5/tengo/compiler"
"github.com/d5/tengo/objects"
"github.com/d5/tengo/parser"
"github.com/d5/tengo/source"
2019-01-09 10:17:42 +03:00
"github.com/d5/tengo/vm"
)
2019-01-09 23:39:40 +03:00
// Script can simplify compilation and execution of embedded scripts.
2019-01-09 10:17:42 +03:00
type Script struct {
2019-01-09 23:39:40 +03:00
variables map[string]*Variable
2019-01-09 10:17:42 +03:00
input []byte
}
2019-01-09 23:39:40 +03:00
// New creates a Script instance with an input script.
func New(input []byte) *Script {
2019-01-09 10:17:42 +03:00
return &Script{
2019-01-09 23:39:40 +03:00
variables: make(map[string]*Variable),
2019-01-09 10:17:42 +03:00
input: input,
}
}
2019-01-09 23:39:40 +03:00
// Add adds a new variable or updates an existing variable to the script.
2019-01-09 10:17:42 +03:00
func (s *Script) Add(name string, value interface{}) error {
2019-01-09 23:39:40 +03:00
obj, err := interfaceToObject(value)
2019-01-09 10:17:42 +03:00
if err != nil {
return err
}
2019-01-09 23:39:40 +03:00
s.variables[name] = &Variable{
name: name,
value: &obj,
}
2019-01-09 10:17:42 +03:00
return nil
}
2019-01-09 23:39:40 +03:00
// Remove removes (undefines) an existing variable for the script.
// It returns false if the variable name is not defined.
2019-01-09 10:17:42 +03:00
func (s *Script) Remove(name string) bool {
if _, ok := s.variables[name]; !ok {
return false
}
delete(s.variables, name)
return true
}
2019-01-09 23:39:40 +03:00
// Compile compiles the script with all the defined variables, and, returns Compiled object.
func (s *Script) Compile() (*Compiled, error) {
2019-01-09 10:17:42 +03:00
symbolTable, globals := s.prepCompile()
fileSet := source.NewFileSet()
2019-01-09 10:17:42 +03:00
p := parser.NewParser(fileSet.AddFile("", -1, len(s.input)), s.input, nil)
file, err := p.ParseFile()
if err != nil {
return nil, fmt.Errorf("parse error: %s", err.Error())
}
c := compiler.NewCompiler(symbolTable, nil)
if err := c.Compile(file); err != nil {
return nil, err
}
2019-01-09 23:39:40 +03:00
return &Compiled{
2019-01-09 10:17:42 +03:00
symbolTable: symbolTable,
2019-01-09 23:39:40 +03:00
machine: vm.NewVM(c.Bytecode(), globals),
2019-01-09 10:17:42 +03:00
}, nil
}
func (s *Script) prepCompile() (symbolTable *compiler.SymbolTable, globals []*objects.Object) {
var names []string
for name := range s.variables {
names = append(names, name)
}
symbolTable = compiler.NewSymbolTable()
globals = make([]*objects.Object, len(names), len(names))
for idx, name := range names {
symbol := symbolTable.Define(name)
if symbol.Index != idx {
panic(fmt.Errorf("wrong symbol index: %d != %d", idx, symbol.Index))
}
2019-01-09 23:39:40 +03:00
globals[symbol.Index] = s.variables[name].value
2019-01-09 10:17:42 +03:00
}
return
}
2019-01-09 23:39:40 +03:00
func (s *Script) copyVariables() map[string]*Variable {
vars := make(map[string]*Variable)
for n, v := range s.variables {
vars[n] = v
2019-01-09 10:17:42 +03:00
}
2019-01-09 23:39:40 +03:00
return vars
2019-01-09 10:17:42 +03:00
}