add compile-run to "tengo" tool

This commit is contained in:
Daniel Kang 2019-01-12 00:19:08 -08:00
parent b802132c94
commit 7289d767d4

View file

@ -14,6 +14,10 @@ import (
"github.com/d5/tengo/runtime"
)
const (
sourceFileExt = ".tengo"
)
var (
compile bool
outputFile = flag.String("o", "", "Output file")
@ -43,6 +47,11 @@ func main() {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else if filepath.Ext(inputFile) == sourceFileExt {
if err := doCompileRun(inputData, inputFile, *outputFile); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else {
if err := doRun(inputData, inputFile, *outputFile); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
@ -106,6 +115,22 @@ func doCompile(data []byte, inputFile, outputFile string) (err error) {
return
}
func doCompileRun(data []byte, inputFile, _ string) (err error) {
bytecode, err := tengo.Compile(data, filepath.Base(inputFile))
if err != nil {
return
}
machine := runtime.NewVM(bytecode, nil)
err = machine.Run()
if err != nil {
return
}
return
}
func doRun(data []byte, _, _ string) (err error) {
bytecode := &compiler.Bytecode{}
err = bytecode.Decode(bytes.NewReader(data))