add compile-run to "tengo" tool
This commit is contained in:
parent
b802132c94
commit
7289d767d4
1 changed files with 25 additions and 0 deletions
|
@ -14,6 +14,10 @@ import (
|
||||||
"github.com/d5/tengo/runtime"
|
"github.com/d5/tengo/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sourceFileExt = ".tengo"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
compile bool
|
compile bool
|
||||||
outputFile = flag.String("o", "", "Output file")
|
outputFile = flag.String("o", "", "Output file")
|
||||||
|
@ -43,6 +47,11 @@ func main() {
|
||||||
_, _ = fmt.Fprintln(os.Stderr, err.Error())
|
_, _ = fmt.Fprintln(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
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 {
|
} else {
|
||||||
if err := doRun(inputData, inputFile, *outputFile); err != nil {
|
if err := doRun(inputData, inputFile, *outputFile); err != nil {
|
||||||
_, _ = fmt.Fprintln(os.Stderr, err.Error())
|
_, _ = fmt.Fprintln(os.Stderr, err.Error())
|
||||||
|
@ -106,6 +115,22 @@ func doCompile(data []byte, inputFile, outputFile string) (err error) {
|
||||||
return
|
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) {
|
func doRun(data []byte, _, _ string) (err error) {
|
||||||
bytecode := &compiler.Bytecode{}
|
bytecode := &compiler.Bytecode{}
|
||||||
err = bytecode.Decode(bytes.NewReader(data))
|
err = bytecode.Decode(bytes.NewReader(data))
|
||||||
|
|
Loading…
Reference in a new issue