示例#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

		var result ast.Node
		var evalError error
		for _, n := range nodes {
			result, evalError = interpreter.Eval(e, n, &outputBuffer)
			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)
	}
}
示例#2
0
文件: main.go 项目: onlyafly/piquant
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 piquant:\n")
		flag.PrintDefaults()
		return
	}

	// Setup liner

	line := liner.NewLiner()
	defer line.Close()
	openLinerHistory(line)
	configureLiner(line)

	// Initialize

	topLevelEnv := interpreter.NewTopLevelMapEnv()

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

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

	// Loading of files

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

	// 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 == ":quit":
			return
		case strings.HasPrefix(input, ":inspect "):
			withoutInspectPrefix := strings.Split(input, ":inspect ")[1]
			if result, err := interpreter.ParseEval(topLevelEnv, withoutInspectPrefix, "REPL"); err == nil {
				inspect(result)
			} else {
				fmt.Println(err.Error())
			}
		default:
			interpreter.ParseEvalPrint(topLevelEnv, input, "REPL", true)
		}
	}
}