示例#1
0
func main() {
	world := eval.NewWorld()
	var fset = token.NewFileSet()
	r := bufio.NewReader(os.Stdin)
	for {
		print("# ")
		line, err := r.ReadString('\n')
		if err != nil {
			break
		}
		code, err := world.Compile(fset, line+";")
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		ret, err := code.Run()
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			continue
		}
		if ret != nil {
			println(ret.String())
		}
	}
}
示例#2
0
func main() {
	world := eval.NewWorld()
	var fset = token.NewFileSet()
	r := bufio.NewReader(os.Stdin)
	for {
		func() {
			defer func() {
				recover()
			}()
			fmt.Print("# ")
			line, err := r.ReadString('\n')
			if err != nil {
				return
			}
			code, err := world.Compile(fset, line+"\n")
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				return
			}
			ret, err := code.Run()
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				return
			}
			if ret != nil {
				fmt.Println(ret.String())
			}
		}()
	}
}
示例#3
0
文件: config.go 项目: remogatto/goam
// Reads the specified config file
func readConfig(config *config_file_t) error {
	var err error

	configCurrent_mutex.Lock()
	{
		if *flag_debug {
			println("read config:", config.path)
		}

		currentConfig = config

		w := eval.NewWorld()
		defineConstants(w)
		defineFunctions(w)
		err = loadAndRunScript(w, config.path)

		currentConfig = nil
	}
	configCurrent_mutex.Unlock()

	if err != nil {
		err = errors.New(config.Path() + ": " + err.Error())
	}

	return err
}
func main() {
	w := eval.NewWorld()
	fset := token.NewFileSet()

	code, err := w.Compile(fset, "1 + 2")
	if err != nil {
		fmt.Println("Compile error")
		return
	}

	val, err := code.Run()
	if err != nil {
		fmt.Println("Run time error")
		return
	}
	fmt.Println("Return value:", val) //prints, well, 3

}
示例#5
0
文件: eval.go 项目: Bobberino/musings
func main() {
	flag.Parse()
	w := eval.NewWorld()
	if *filename != "" {
		data, err := ioutil.ReadFile(*filename)
		if err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
		file, err := parser.ParseFile(fset, *filename, data, 0)
		if err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
		files := []*ast.File{file}
		code, err := w.CompilePackage(fset, files, "main")
		if err != nil {
			if list, ok := err.(scanner.ErrorList); ok {
				for _, e := range list {
					fmt.Println(e.Error())
				}
			} else {
				fmt.Println(err.Error())
			}
			os.Exit(1)
		}
		code, err = w.Compile(fset, "main()")
		if err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
		_, err = code.Run()
		if err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
		os.Exit(0)
	}

	r := bufio.NewReader(os.Stdin)
	for {
		print("; ")
		line, err := r.ReadString('\n')
		if err != nil {
			break
		}
		code, err := w.Compile(fset, line)
		if err != nil {
			println(err.Error())
			continue
		}
		v, err := code.Run()
		if err != nil {
			println(err.Error())
			continue
		}
		if v != nil {
			println(v.String())
		}
	}
}
示例#6
0
文件: main.go 项目: abi/dotfiles
func main() {
	defer atexit()

	flag.Parse()
	w := eval.NewWorld()
	if *filename != "" {
		data, err := ioutil.ReadFile(*filename)
		if err != nil {
			fmt.Println(err.String())
			os.Exit(1)
		}
		file, err := parser.ParseFile(fset, *filename, data, 0)
		if err != nil {
			fmt.Println(err.String())
			os.Exit(1)
		}
		files := []*ast.File{file}
		code, err := w.CompilePackage(fset, files, "main")
		if err != nil {
			if list, ok := err.(scanner.ErrorList); ok {
				for _, e := range list {
					fmt.Println(e.String())
				}
			} else {
				fmt.Println(err.String())
			}
			os.Exit(1)
		}
		code, err = w.Compile(fset, "main()")
		if err != nil {
			fmt.Println(err.String())
			os.Exit(1)
		}
		_, err = code.Run()
		if err != nil {
			fmt.Println(err.String())
			os.Exit(1)
		}
		os.Exit(0)
	}

	var ierr os.Error = nil // previous interpreter error
	ps1 := "igo> "
	ps2 := "...  "
	prompt := &ps1
	codelet := ""
	// initialize main package
	{
		codelet := "package main\n"
		f, err := parser.ParseFile(fset, "input", codelet, 0)
		code, err := w.CompilePackage(fset, []*ast.File{f}, "main")
		if err == nil {
			code.Run()
		}
	}
	for {
		line := readline.ReadLine(prompt)
		if line == nil {
			break //os.Exit(0)
		}
		if *line == "" || *line == ";" {
			// no more input
			prompt = &ps1
		}
		codelet += *line
		readline.AddHistory(codelet)
		code, err := w.Compile(fset, codelet+";")
		if err != nil {
			if ierr != nil && prompt == &ps1 {
				fmt.Println(err.String())
				fmt.Printf("(error %T)\n", err)
				// reset state
				codelet = ""
				ierr = nil
				prompt = &ps1
				continue
			}
			// maybe multi-line command ?
			prompt = &ps2
			ierr = err
			codelet += "\n"
			continue
		}
		v, err := code.Run()
		if err != nil {
			fmt.Println(err.String())
			fmt.Printf("(error %T)\n", err)
			codelet = ""
			continue
		}
		if v != nil {
			fmt.Println(v.String())
		}
		//	resetstate:
		// reset state
		codelet = ""
		ierr = nil
	}
}