Example #1
0
func testInputFile(sourceFilePath string, t *testing.T) {
	sourceDirPart, sourceFileNamePart := filepath.Split(sourceFilePath)
	parts := strings.Split(sourceFileNamePart, ".")
	testName := parts[0]

	outputFilePath := sourceDirPart + testName + ".out"

	input, errIn := util.ReadFile(sourceFilePath)
	if errIn != nil {
		t.Errorf("Error reading file <" + sourceFilePath + ">: " + errIn.Error())
		return
	}

	expectedRaw, errOut := util.ReadFile(outputFilePath)
	if errOut != nil {
		t.Errorf("Error reading file <" + outputFilePath + ">: " + errOut.Error())
		return
	}

	// Remove any carriage return line endings from .out file
	expectedWithUntrimmed := strings.Replace(expectedRaw, "\r", "", -1)
	expected := strings.TrimSpace(expectedWithUntrimmed)

	nodes, errors := parser.Parse(input, sourceFilePath)
	if errors.Len() != 0 {
		verify(t, sourceFilePath, input, expected, errors.String())
	} else {
		e := interpreter.NewTopLevelMapEnv()

		var outputBuffer bytes.Buffer

		dummyReadLine := func() string {
			return "text from dummy read line"
		}

		var result ast.Node
		var evalError error
		for _, n := range nodes {
			result, evalError = interpreter.Eval(e, n, &outputBuffer, dummyReadLine)
			if evalError != nil {
				break
			}
		}

		actual := (&outputBuffer).String()

		if evalError == nil {
			//DEBUG fmt.Printf("RESULT(%v): %v\n", sourceFilePath, result)
			if result != nil {
				actual = actual + result.String()
			}
		} else {
			actual = actual + evalError.Error()
		}
		verify(t, sourceFilePath, input, expected, actual)
	}
}
Example #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)
		}
	}
}