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

		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)
	}
}
Example #2
0
func loadFile(fileName string, env interpreter.Env) {
	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, fileName, false)
		}
	}
}
Example #3
0
func primLoad(e Env, head ast.Node, args []ast.Node) ast.Node {
	arg := args[0]
	switch val := arg.(type) {
	case *ast.Str:
		fileName := val.Value

		if len(fileName) > 0 {
			content, err := util.ReadFile(fileName)
			if err != nil {
				panicEvalError(
					arg,
					fmt.Sprintf("Error while loading file <%v>: %v\n", fileName, err.Error()))
			} else {
				ParseEvalPrint(e, content, fileName, false)
			}
		}

		return &ast.Nil{}
	}

	panicEvalError(arg, "Argument to 'load' not a string: "+arg.String())
	return nil
}