initial version of "tengo" CLI tool

This commit is contained in:
Daniel Kang 2019-01-09 13:49:50 -08:00
parent cce71f0cd5
commit 928a728f78
2 changed files with 138 additions and 5 deletions

135
cmd/tengo/main.go Normal file
View file

@ -0,0 +1,135 @@
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/d5/tengo"
"github.com/d5/tengo/compiler"
"github.com/d5/tengo/vm"
)
var (
compile bool
outputFile = flag.String("o", "", "Output file")
)
func init() {
flag.BoolVar(&compile, "compile", false, "Compile input file")
flag.BoolVar(&compile, "c", false, "Compile input file")
flag.Parse()
}
func main() {
inputFile := flag.Arg(0)
if inputFile == "" {
doHelp()
os.Exit(2)
}
inputData, err := ioutil.ReadFile(inputFile)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error reading input file: %s", err.Error())
os.Exit(1)
}
if compile {
if err := doCompile(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())
os.Exit(1)
}
}
}
func doHelp() {
fmt.Println("Tengo is a tool to compile Tengo source code or execute compiled Tengo binary.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println()
fmt.Println(" tengo [flags] input-file")
fmt.Println()
fmt.Println("Flags:")
fmt.Println()
fmt.Println(" -compile/-c compile source file")
fmt.Println(" -o output file")
fmt.Println()
fmt.Println("Examples:")
fmt.Println()
fmt.Println(" tengo -c -o program.out program.tengo")
fmt.Println(" : Compile program.tengo and write compiled binary to program.out file")
fmt.Println()
fmt.Println(" tengo program.out")
fmt.Println(" : Execute compiled binary program.out")
fmt.Println()
fmt.Println()
}
func doCompile(data []byte, inputFile, outputFile string) (err error) {
bytecode, err := tengo.Compile(data, filepath.Base(inputFile))
if err != nil {
return
}
if outputFile == "" {
outputFile = basename(inputFile) + ".out"
}
out, err := os.OpenFile(outputFile, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return
}
defer func() {
if err != nil {
_ = out.Close()
} else {
err = out.Close()
}
}()
err = bytecode.Encode(out)
if err != nil {
return
}
fmt.Println(outputFile)
return
}
func doRun(data []byte, _, _ string) (err error) {
bytecode := &compiler.Bytecode{}
err = bytecode.Decode(bytes.NewReader(data))
if err != nil {
return
}
machine := vm.NewVM(bytecode, nil)
err = machine.Run()
if err != nil {
return
}
return
}
func basename(s string) string {
s = filepath.Base(s)
n := strings.LastIndexByte(s, '.')
if n > 0 {
return s[:n]
}
return s
}

View file

@ -1,21 +1,19 @@
package tengo
import (
"fmt"
"github.com/d5/tengo/compiler"
"github.com/d5/tengo/parser"
"github.com/d5/tengo/scanner"
"github.com/d5/tengo/vm"
)
func Compile(input []byte) (*compiler.Bytecode, error) {
func Compile(input []byte, filename string) (*compiler.Bytecode, error) {
fileSet := scanner.NewFileSet()
p := parser.NewParser(fileSet.AddFile("", -1, len(input)), input, nil)
p := parser.NewParser(fileSet.AddFile(filename, -1, len(input)), input, nil)
file, err := p.ParseFile()
if err != nil {
return nil, fmt.Errorf("parse error: %s", err.Error())
return nil, err
}
c := compiler.NewCompiler(nil, nil)