From 7289d767d4eb34e9e7d6510c7e1bc5fbd06d7d1e Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Sat, 12 Jan 2019 00:19:08 -0800 Subject: [PATCH] add compile-run to "tengo" tool --- cmd/tengo/main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cmd/tengo/main.go b/cmd/tengo/main.go index c956782..51b425c 100644 --- a/cmd/tengo/main.go +++ b/cmd/tengo/main.go @@ -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))