123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- // +build !appengine
- package main
- import (
- "bufio"
- "flag"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "strings"
- "github.com/mattn/anko/core"
- "github.com/mattn/anko/env"
- _ "github.com/mattn/anko/packages"
- "github.com/mattn/anko/parser"
- "github.com/mattn/anko/vm"
- )
- const version = "0.1.8"
- var (
- flagExecute string
- file string
- args []string
- e *env.Env
- )
- func main() {
- var exitCode int
- parseFlags()
- setupEnv()
- if flagExecute != "" || flag.NArg() > 0 {
- exitCode = runNonInteractive()
- } else {
- exitCode = runInteractive()
- }
- os.Exit(exitCode)
- }
- func parseFlags() {
- flagVersion := flag.Bool("v", false, "prints out the version and then exits")
- flag.StringVar(&flagExecute, "e", "", "execute the Anko code")
- flag.Parse()
- if *flagVersion {
- fmt.Println(version)
- os.Exit(0)
- }
- if flagExecute != "" || flag.NArg() < 1 {
- args = flag.Args()
- return
- }
- file = flag.Arg(0)
- args = flag.Args()[1:]
- }
- func setupEnv() {
- e = env.NewEnv()
- e.Define("args", args)
- core.Import(e)
- }
- func runNonInteractive() int {
- var source string
- if flagExecute != "" {
- source = flagExecute
- } else {
- sourceBytes, err := ioutil.ReadFile(file)
- if err != nil {
- fmt.Println("ReadFile error:", err)
- return 2
- }
- source = string(sourceBytes)
- }
- _, err := vm.Execute(e, nil, source)
- if err != nil {
- fmt.Println("Execute error:", err)
- return 4
- }
- return 0
- }
- func runInteractive() int {
- var following bool
- var source string
- scanner := bufio.NewScanner(os.Stdin)
- parser.EnableErrorVerbose()
- for {
- if following {
- source += "\n"
- fmt.Print(" ")
- } else {
- fmt.Print("> ")
- }
- if !scanner.Scan() {
- break
- }
- source += scanner.Text()
- if source == "" {
- continue
- }
- if source == "quit()" {
- break
- }
- stmts, err := parser.ParseSrc(source)
- if e, ok := err.(*parser.Error); ok {
- es := e.Error()
- if strings.HasPrefix(es, "syntax error: unexpected") {
- if strings.HasPrefix(es, "syntax error: unexpected $end,") {
- following = true
- continue
- }
- } else {
- if e.Pos.Column == len(source) && !e.Fatal {
- fmt.Fprintln(os.Stderr, e)
- following = true
- continue
- }
- if e.Error() == "unexpected EOF" {
- following = true
- continue
- }
- }
- }
- following = false
- source = ""
- var v interface{}
- if err == nil {
- v, err = vm.Run(e, nil, stmts)
- }
- if err != nil {
- if e, ok := err.(*vm.Error); ok {
- fmt.Fprintf(os.Stderr, "%d:%d %s\n", e.Pos.Line, e.Pos.Column, err)
- } else if e, ok := err.(*parser.Error); ok {
- fmt.Fprintf(os.Stderr, "%d:%d %s\n", e.Pos.Line, e.Pos.Column, err)
- } else {
- fmt.Fprintln(os.Stderr, err)
- }
- continue
- }
- fmt.Printf("%#v\n", v)
- }
- if err := scanner.Err(); err != nil {
- if err != io.EOF {
- fmt.Fprintln(os.Stderr, "ReadString error:", err)
- return 12
- }
- }
- return 0
- }
|