Skip to content

Commit

Permalink
1. fix bytecode compile bug 2. update CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
d5 committed Jan 30, 2019
1 parent a9aa8dc commit 0e9c523
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
51 changes: 30 additions & 21 deletions cmd/tengo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,35 @@ import (
const (
sourceFileExt = ".tengo"
replPrompt = ">> "
version = "dev"
)

var (
compile bool
showHelp bool
outputFile = flag.String("o", "", "Output file")
compileOutput string
showHelp bool
showVersion bool
)

func init() {
flag.BoolVar(&showHelp, "help", false, "Show help")
flag.BoolVar(&compile, "compile", false, "Compile input file")
flag.BoolVar(&compile, "c", false, "Compile input file")
flag.StringVar(&compileOutput, "o", "", "Compile output file")
flag.BoolVar(&showVersion, "version", false, "Show version")
flag.Parse()
}

func main() {
if showHelp {
doHelp()
os.Exit(2)
} else if showVersion {
fmt.Println(version)
return
}

inputFile := flag.Arg(0)
if inputFile == "" {
// REPL
doRepl(os.Stdin, os.Stdout)
runREPL(os.Stdin, os.Stdout)
return
}

Expand All @@ -56,18 +60,18 @@ func main() {
os.Exit(1)
}

if compile {
if err := doCompile(inputData, inputFile, *outputFile); err != nil {
if compileOutput != "" {
if err := compileOnly(inputData, inputFile, compileOutput); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else if filepath.Ext(inputFile) == sourceFileExt {
if err := doCompileRun(inputData, inputFile, *outputFile); err != nil {
if err := compileAndRun(inputData, inputFile); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} else {
if err := doRun(inputData, inputFile, *outputFile); err != nil {
if err := runCompiled(inputData); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
Expand All @@ -81,27 +85,32 @@ func doHelp() {
fmt.Println()
fmt.Println("Flags:")
fmt.Println()
fmt.Println(" -c/-compile compile the input and produce bytecode file")
fmt.Println(" -o output")
fmt.Println(" -o compile output file")
fmt.Println(" -version show version")
fmt.Println()
fmt.Println("Examples:")
fmt.Println()
fmt.Println(" tengo")
fmt.Println(" : Start Tengo REPL")
fmt.Println()
fmt.Println(" Start Tengo REPL")
fmt.Println()
fmt.Println(" tengo myapp.tengo")
fmt.Println(" : Compile and execute source file (myapp.tengo)")
fmt.Println()
fmt.Println(" tengo -c myapp myapp.tengo")
fmt.Println(" : Compile source file (myapp.tengo) and produce bytecode file (myapp)")
fmt.Println(" Compile and run source file (myapp.tengo)")
fmt.Println(" Source file must have .tengo extension")
fmt.Println()
fmt.Println(" tengo -o myapp myapp.tengo")
fmt.Println()
fmt.Println(" Compile source file (myapp.tengo) into bytecode file (myapp)")
fmt.Println()
fmt.Println(" tengo myapp")
fmt.Println(" : Execute bytecode file (myapp)")
fmt.Println()
fmt.Println(" Run bytecode file (myapp)")
fmt.Println()
fmt.Println()
}

func doCompile(data []byte, inputFile, outputFile string) (err error) {
func compileOnly(data []byte, inputFile, outputFile string) (err error) {
bytecode, err := compileSrc(data, filepath.Base(inputFile))
if err != nil {
return
Expand Down Expand Up @@ -133,7 +142,7 @@ func doCompile(data []byte, inputFile, outputFile string) (err error) {
return
}

func doCompileRun(data []byte, inputFile, _ string) (err error) {
func compileAndRun(data []byte, inputFile string) (err error) {
bytecode, err := compileSrc(data, filepath.Base(inputFile))
if err != nil {
return
Expand All @@ -149,7 +158,7 @@ func doCompileRun(data []byte, inputFile, _ string) (err error) {
return
}

func doRun(data []byte, _, _ string) (err error) {
func runCompiled(data []byte) (err error) {
bytecode := &compiler.Bytecode{}
err = bytecode.Decode(bytes.NewReader(data))
if err != nil {
Expand All @@ -166,7 +175,7 @@ func doRun(data []byte, _, _ string) (err error) {
return
}

func doRepl(in io.Reader, out io.Writer) {
func runREPL(in io.Reader, out io.Writer) {
stdin := bufio.NewScanner(in)

fileSet := source.NewFileSet()
Expand Down
1 change: 1 addition & 0 deletions compiler/bytecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ func init() {
gob.Register(&objects.MapIterator{})
gob.Register(&objects.ArrayIterator{})
gob.Register(&objects.Time{})
gob.Register(&objects.CompiledModule{})
}

0 comments on commit 0e9c523

Please sign in to comment.