示例#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))
	}
}
示例#2
0
文件: main.go 项目: uiureo/jack
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))
	}
}
示例#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")
	}
}
示例#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")
	}
}
示例#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
}
示例#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))
	}
}
示例#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))
	}
}
示例#8
0
文件: main_test.go 项目: uiureo/jack
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)
	}
}
示例#9
0
func parse(source string) *Node {
	node, _ := ParseStatements(tokenizer.Tokenize(source))
	return node
}