2019-01-09 10:17:42 +03:00
|
|
|
package tengo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/d5/tengo/compiler"
|
|
|
|
"github.com/d5/tengo/parser"
|
2019-01-11 12:16:34 +03:00
|
|
|
"github.com/d5/tengo/source"
|
2019-01-09 10:17:42 +03:00
|
|
|
"github.com/d5/tengo/vm"
|
|
|
|
)
|
|
|
|
|
2019-01-10 00:49:50 +03:00
|
|
|
func Compile(input []byte, filename string) (*compiler.Bytecode, error) {
|
2019-01-11 12:16:34 +03:00
|
|
|
fileSet := source.NewFileSet()
|
2019-01-09 10:17:42 +03:00
|
|
|
|
2019-01-10 00:49:50 +03:00
|
|
|
p := parser.NewParser(fileSet.AddFile(filename, -1, len(input)), input, nil)
|
2019-01-09 10:17:42 +03:00
|
|
|
file, err := p.ParseFile()
|
|
|
|
if err != nil {
|
2019-01-10 00:49:50 +03:00
|
|
|
return nil, err
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c := compiler.NewCompiler(nil, nil)
|
|
|
|
if err := c.Compile(file); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Bytecode(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Run(b *compiler.Bytecode) error {
|
|
|
|
v := vm.NewVM(b, nil)
|
|
|
|
|
|
|
|
return v.Run()
|
|
|
|
}
|