Exemple #1
0
func TestParseClass(t *testing.T) {
	root, tokens := parseClass(tokenizer.Tokenize(`
		class Main {
			function void main() {
				return;
			}
		}
	`))

	if root.Name != "class" {
		t.Errorf("expect node `<class>`, but got:\n%v", root.ToXML())
	}

	node, i := root.Find(&Node{Name: "keyword", Value: "class"})
	if !(node != nil && i == 0) {
		t.Errorf("expect node to have class keyword, but got:\n%v", root.ToXML())
	}

	if node, _ := root.Find(&Node{Name: "subroutineDec"}); node == nil {
		t.Errorf("expect node to have subroutineDec, but got:\n%v", root.ToXML())
	}

	if len(tokens) > 0 {
		t.Errorf("expect len(tokens) == 0, but actual: %v", len(tokens))
	}
}
Exemple #2
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))
	}
}
Exemple #3
0
func TestParseLetStatementWithArrayIndex(t *testing.T) {
	node, tokens := parseLetStatement(tokenizer.Tokenize(`let a[2]="foo";`))

	if node.Name != "letStatement" {
		t.Errorf("expect: letStatement, actual: %v", node.ToXML())
	}

	if len(tokens) > 0 {
		t.Error("parse failed")
	}
}
Exemple #4
0
func TestParseVarDec(t *testing.T) {
	node, tokens := parseVarDec(tokenizer.Tokenize(`var int i, sum;`))

	if node.Name != "varDec" {
		t.Errorf("expect Name:`varDec` but actual: %v", node.Name)
	}

	if len(tokens) > 0 {
		t.Error("parse fails")
	}
}
Exemple #5
0
func testParseTermSuccess(t *testing.T, source string) (*Node, []*tokenizer.Token) {
	root, tokens := parseTerm(tokenizer.Tokenize(source))

	if len(tokens) > 0 {
		t.Errorf("`%s`: expect len(tokens) == 0, but actual: %v", source, len(tokens))
	}

	if root.Name != "term" {
		t.Errorf("`%s`: expect node to be term, but actual: %s", source, root.ToXML())
	}

	return root, tokens
}
Exemple #6
0
func TestParseClassWithField(t *testing.T) {
	root, tokens := parseClass(tokenizer.Tokenize(`
		class Main {
			field int x, y;
			static int size;

			function void main() {
				return;
			}
		}
	`))

	if node, _ := root.Find(&Node{Name: "classVarDec"}); node == nil {
		t.Errorf("expect node to have classVarDec, but got:\n%v", root.ToXML())
	}

	if len(tokens) > 0 {
		t.Errorf("expect len(tokens) == 0, but actual: %v", len(tokens))
	}
}
Exemple #7
0
func TestParseClassWithMethod(t *testing.T) {
	root, tokens := parseClass(tokenizer.Tokenize(`
		class Foo {
			constructor Foo new() {
				return;
			}

			method boolean bar() {
				return true;
			}
		}
	`))

	if node, _ := root.Find(&Node{Name: "subroutineDec"}); node == nil {
		t.Errorf("expect node to have classVarDec, but got:\n%v", root.ToXML())
	}

	if len(tokens) > 0 {
		t.Errorf("expect len(tokens) == 0, but actual: %v", len(tokens))
	}
}
Exemple #8
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)
	}
}
Exemple #9
0
func parse(source string) *Node {
	node, _ := ParseStatements(tokenizer.Tokenize(source))
	return node
}