Beispiel #1
0
func main() {
	if len(os.Args) < 2 {
		fmt.Fprintln(os.Stderr, "no files given")
		os.Exit(1)
	}

	parseMode := os.Args[1] == "parse"

	filename := os.Args[1]
	if parseMode {
		filename = os.Args[2]
	}

	data, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(1)
	}

	tokens := tokenizer.Tokenize(string(data))
	tree := parser.Parse(tokens)

	if parseMode {
		fmt.Print(tree.ToXML())
	} else {
		fmt.Print(compiler.Compile(tree))
	}
}
Beispiel #2
0
func testMainOutput(t *testing.T, jackFile string) {
	xmlFile := regexp.MustCompile(`\.jack$`).ReplaceAllString(jackFile, ".xml")

	name := strings.Split(filepath.Base(jackFile), ".")[0]

	code, err := ioutil.ReadFile(jackFile)
	if err != nil {
		t.Error(err)
		return
	}

	parserOutput := parser.Parse(tokenizer.Tokenize(string(code))).ToXML()

	file, _ := ioutil.TempFile("", "")
	file.Write([]byte(parserOutput))

	output, err := exec.Command(toolPath, file.Name(), xmlFile).CombinedOutput()

	if err != nil {
		t.Errorf("%s: %s %v", name, output, err)
	}
}