Esempio n. 1
0
func loadFile(fileName string, env interpreter.Env, standardReadLine func() string) {
	if len(fileName) > 0 {
		content, err := util.ReadFile(fileName)
		if err != nil {
			fmt.Printf("Error while loading file <%v>: %v\n", fileName, err.Error())
		} else {
			interpreter.ParseEvalPrint(env, content, standardReadLine, fileName, false)
		}
	}
}
Esempio n. 2
0
func main() {

	startupFileName := flag.String("l", "", "load a file at startup")
	showHelp := flag.Bool("help", false, "show the help")
	flag.Parse()
	exeFileName := flag.Arg(0)

	if showHelp != nil && *showHelp {
		fmt.Printf("Usage of vamos:\n")
		flag.PrintDefaults()
		return
	}

	// Setup liner

	line := startLiner()
	defer line.Close()

	standardReadLine := func() string {
		stopLiner(line) // Liner must be stopped to turn off raw mode...
		reader := bufio.NewReader(os.Stdin)
		text, _ := reader.ReadString('\n')
		line = startLiner() // ...and then turn it back on.
		return text
	}

	// Initialize

	topLevelEnv := interpreter.NewTopLevelMapEnv()

	if len(exeFileName) != 0 {
		loadFile("prelude.v", topLevelEnv, standardReadLine)
		loadFile(exeFileName, topLevelEnv, standardReadLine)
		return
	}

	fmt.Printf("Vamos %s (%s)\n", version, versionDate)
	loadFile("prelude.v", topLevelEnv, standardReadLine)
	fmt.Printf("(Press Ctrl+C or type :quit to exit)\n\n")

	// Loading of files

	if startupFileName != nil {
		loadFile(*startupFileName, topLevelEnv, standardReadLine)
	}

	// REPL

	for {
		input, err := line.Prompt("> ")

		if err != nil {
			if err.Error() == "prompt aborted" {
				fmt.Printf("Quiting...\n")
			} else {
				fmt.Printf("Prompt error: %s\n", err)
			}
			return
		}

		line.AppendHistory(input)
		writeLinerHistory(line)

		switch {
		case input == ":test":
			fmt.Print("Test Prompt: ")
			fmt.Println(standardReadLine())
		case input == ":quit":
			return
		case strings.HasPrefix(input, ":inspect "):
			withoutInspectPrefix := strings.Split(input, ":inspect ")[1]
			if result, err := interpreter.ParseEval(topLevelEnv, withoutInspectPrefix, standardReadLine, "REPL"); err == nil {
				inspect(result)
			} else {
				fmt.Println(err.Error())
			}
		default:
			interpreter.ParseEvalPrint(topLevelEnv, input, standardReadLine, "REPL", true)
		}
	}
}