diff --git a/cmd/repl/main.go b/cmd/repl/main.go deleted file mode 100644 index 2bce704..0000000 --- a/cmd/repl/main.go +++ /dev/null @@ -1,111 +0,0 @@ -package main - -import ( - "bufio" - "fmt" - "io" - "os" - "os/user" - - "github.com/d5/tengo/compiler" - "github.com/d5/tengo/compiler/ast" - "github.com/d5/tengo/compiler/parser" - "github.com/d5/tengo/compiler/source" - "github.com/d5/tengo/objects" - "github.com/d5/tengo/runtime" -) - -const ( - Prompt = ">> " -) - -func main() { - currentUser, err := user.Current() - if err != nil { - panic(err) - } - - fmt.Printf("Hello, %s! Welcome to the Tengo Language!\n", currentUser.Name) - - startRepl(os.Stdin, os.Stdout) -} - -func startRepl(in io.Reader, out io.Writer) { - stdin := bufio.NewScanner(in) - - fileSet := source.NewFileSet() - globals := make([]*objects.Object, runtime.GlobalsSize) - symbolTable := compiler.NewSymbolTable() - - for { - _, _ = fmt.Fprintf(out, Prompt) - - scanned := stdin.Scan() - if !scanned { - return - } - - line := stdin.Text() - - file, err := parser.ParseFile(fileSet.AddFile("test", -1, len(line)), []byte(line), nil) - if err != nil { - _, _ = fmt.Fprintf(out, "error: %s\n", err.Error()) - continue - } - - file = addPrints(file) - - c := compiler.NewCompiler(symbolTable, nil) - if err := c.Compile(file); err != nil { - _, _ = fmt.Fprintf(out, "Compilation error:\n %s\n", err.Error()) - continue - } - - machine := runtime.NewVM(c.Bytecode(), globals) - if err != nil { - _, _ = fmt.Fprintf(out, "VM error:\n %s\n", err.Error()) - continue - } - if err := machine.Run(); err != nil { - _, _ = fmt.Fprintf(out, "Execution error:\n %s\n", err.Error()) - continue - } - } -} - -func addPrints(file *ast.File) *ast.File { - var stmts []ast.Stmt - for _, s := range file.Stmts { - switch s := s.(type) { - case *ast.ExprStmt: - stmts = append(stmts, &ast.ExprStmt{ - Expr: &ast.CallExpr{ - Func: &ast.Ident{ - Name: "print", - }, - Args: []ast.Expr{s.Expr}, - }, - }) - - case *ast.AssignStmt: - stmts = append(stmts, s) - - stmts = append(stmts, &ast.ExprStmt{ - Expr: &ast.CallExpr{ - Func: &ast.Ident{ - Name: "print", - }, - Args: s.Lhs, - }, - }) - - default: - stmts = append(stmts, s) - } - } - - return &ast.File{ - InputFile: file.InputFile, - Stmts: stmts, - } -} diff --git a/cmd/tengo/main.go b/cmd/tengo/main.go index 51b425c..1928dd7 100644 --- a/cmd/tengo/main.go +++ b/cmd/tengo/main.go @@ -1,9 +1,11 @@ package main import ( + "bufio" "bytes" "flag" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -11,31 +13,44 @@ import ( "github.com/d5/tengo" "github.com/d5/tengo/compiler" + "github.com/d5/tengo/compiler/ast" + "github.com/d5/tengo/compiler/parser" + "github.com/d5/tengo/compiler/source" + "github.com/d5/tengo/objects" "github.com/d5/tengo/runtime" ) const ( sourceFileExt = ".tengo" + replPrompt = ">> " ) var ( compile bool + showHelp bool outputFile = flag.String("o", "", "Output file") ) 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.Parse() } func main() { - inputFile := flag.Arg(0) - if inputFile == "" { + if showHelp { doHelp() os.Exit(2) } + inputFile := flag.Arg(0) + if inputFile == "" { + // REPL + doRepl(os.Stdin, os.Stdout) + return + } + inputData, err := ioutil.ReadFile(inputFile) if err != nil { _, _ = fmt.Fprintf(os.Stderr, "Error reading input file: %s", err.Error()) @@ -65,7 +80,7 @@ func doHelp() { fmt.Println() fmt.Println("Usage:") fmt.Println() - fmt.Println(" tengo [flags] input-file") + fmt.Println(" tengo [flags] [input-file]") fmt.Println() fmt.Println("Flags:") fmt.Println() @@ -74,11 +89,17 @@ func doHelp() { 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(" tengo") + fmt.Println(" : Start Tengo REPL") fmt.Println() - fmt.Println(" tengo program.out") - fmt.Println(" : Execute compiled binary program.out") + fmt.Println(" tengo myapp.tengo") + fmt.Println(" : Compile and execute a Tengo code (myapp.tengo)") + fmt.Println() + fmt.Println(" tengo -c -o myapp myapp.tengo") + fmt.Println(" : Compile a Tengo code (myapp.tengo) into compiled binary (myapp)") + fmt.Println() + fmt.Println(" tengo myapp") + fmt.Println(" : Execute compiled binary (myapp)") fmt.Println() fmt.Println() } @@ -148,6 +169,86 @@ func doRun(data []byte, _, _ string) (err error) { return } +func doRepl(in io.Reader, out io.Writer) { + stdin := bufio.NewScanner(in) + + fileSet := source.NewFileSet() + globals := make([]*objects.Object, runtime.GlobalsSize) + symbolTable := compiler.NewSymbolTable() + + for { + _, _ = fmt.Fprintf(out, replPrompt) + + scanned := stdin.Scan() + if !scanned { + return + } + + line := stdin.Text() + + file, err := parser.ParseFile(fileSet.AddFile("test", -1, len(line)), []byte(line), nil) + if err != nil { + _, _ = fmt.Fprintf(out, "error: %s\n", err.Error()) + continue + } + + file = addPrints(file) + + c := compiler.NewCompiler(symbolTable, nil) + if err := c.Compile(file); err != nil { + _, _ = fmt.Fprintf(out, "Compilation error:\n %s\n", err.Error()) + continue + } + + machine := runtime.NewVM(c.Bytecode(), globals) + if err != nil { + _, _ = fmt.Fprintf(out, "VM error:\n %s\n", err.Error()) + continue + } + if err := machine.Run(); err != nil { + _, _ = fmt.Fprintf(out, "Execution error:\n %s\n", err.Error()) + continue + } + } +} + +func addPrints(file *ast.File) *ast.File { + var stmts []ast.Stmt + for _, s := range file.Stmts { + switch s := s.(type) { + case *ast.ExprStmt: + stmts = append(stmts, &ast.ExprStmt{ + Expr: &ast.CallExpr{ + Func: &ast.Ident{ + Name: "print", + }, + Args: []ast.Expr{s.Expr}, + }, + }) + + case *ast.AssignStmt: + stmts = append(stmts, s) + + stmts = append(stmts, &ast.ExprStmt{ + Expr: &ast.CallExpr{ + Func: &ast.Ident{ + Name: "print", + }, + Args: s.Lhs, + }, + }) + + default: + stmts = append(stmts, s) + } + } + + return &ast.File{ + InputFile: file.InputFile, + Stmts: stmts, + } +} + func basename(s string) string { s = filepath.Base(s)