Beispiel #1
0
func TestParseVar(t *testing.T) {
	types := []Type{FILE, DECL, IDENT, IDENT, VAR, IDENT, IDENT}
	src := "(decl main int (var a int))"
	file := token.NewFile("var.calc", 1, len(src))
	f := parse.ParseFile(file, "var.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))

	types = []Type{FILE, DECL, IDENT, IDENT, VAR, IDENT, IDENT, ASSIGN,
		IDENT, BASIC}
	src = "(decl main int (var (= a 5) int))"
	file = token.NewFile("var2.calc", 1, len(src))
	f = parse.ParseFile(file, "var2.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))

	types = []Type{FILE, DECL, IDENT, IDENT, VAR, IDENT, ASSIGN, IDENT, BASIC}
	src = "(decl main int (var (= a 5)))"
	file = token.NewFile("var3.calc", 1, len(src))
	f = parse.ParseFile(file, "var3.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))
}
Beispiel #2
0
func TestParseIf(t *testing.T) {
	types := []Type{FILE, DECL, IDENT, IDENT, IF, IDENT, IDENT, BASIC}
	src := "(decl main int (if true int 3))"
	file := token.NewFile("if.calc", 1, len(src))
	f := parse.ParseFile(file, "if.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))

	types = []Type{FILE, DECL, IDENT, IDENT, IF, BINARY, IDENT, IDENT, IDENT,
		IDENT, LIST, BINARY, IDENT, BASIC, IDENT}
	src = "(decl main int (if (< a b) int a ((+ b 1) b)))"
	file = token.NewFile("if2.calc", 1, len(src))
	f = parse.ParseFile(file, "if2.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))

	types = []Type{FILE, DECL, IDENT, IDENT, IF, BINARY, IDENT, IDENT, LIST,
		ASSIGN, IDENT, IDENT}
	src = "(decl main int (if (< a b) ((= a b))))"
	file = token.NewFile("if3.calc", 1, len(src))
	f = parse.ParseFile(file, "if3.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))
}
Beispiel #3
0
func TestParseDecl(t *testing.T) {
	types := []Type{FILE, DECL, IDENT, IDENT, IDENT}
	src := "(decl func int a)"
	file := token.NewFile("decl1.calc", 1, len(src))
	f := parse.ParseFile(file, "decl1.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))

	types = []Type{FILE, DECL, IDENT, IDENT, BINARY, BASIC, BASIC}
	src = "(decl five int (+ 2 3))"
	file = token.NewFile("decl2.calc", 1, len(src))
	f = parse.ParseFile(file, "decl1.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))

	types = []Type{FILE, DECL, IDENT, IDENT, IDENT, IDENT, BINARY,
		IDENT, IDENT}
	src = "(decl add(a b int) int (+ a b))"
	file = token.NewFile("decl3.calc", 1, len(src))
	f = parse.ParseFile(file, "decl2.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))
}
Beispiel #4
0
func TestParseFileBasic(t *testing.T) {
	types := []Type{FILE, DECL, IDENT, IDENT, BINARY, BASIC, BASIC}
	src := "(decl main int (+ 1 2))"
	file := token.NewFile("basicdec", 1, len(src))
	f := parse.ParseFile(file, "basicdec", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))
}
Beispiel #5
0
func TestParseNested(t *testing.T) {
	var types = []Type{FILE, DECL, IDENT, IDENT, BINARY, BINARY, BASIC, BASIC,
		BASIC, BINARY, BASIC, BASIC}
	src := ";comment\n(decl main int (+ (/ 9 3) 5 (- 3 1)))"
	file := token.NewFile("nested.calc", 1, len(src))
	f := parse.ParseFile(file, "nested.calc", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))
}
Beispiel #6
0
func TestParseCall(t *testing.T) {
	types := []Type{FILE, DECL, IDENT, IDENT, CALL, IDENT, BASIC, BASIC,
		BASIC, BASIC}
	src := "(decl main int (add 1 2 3 4))"
	file := token.NewFile("call1", 1, len(src))
	f := parse.ParseFile(file, "call1", "(decl main int (add 1 2 3 4))")
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))

	types = []Type{FILE, DECL, IDENT, IDENT, CALL, IDENT}
	src = "(decl main int (nothing))"
	file = token.NewFile("call1", 1, len(src))
	f = parse.ParseFile(file, "call1", src)
	if f == nil {
		t.Fatal("Failed to parse")
	}
	ast.Walk(f, nodeTest(types, t))
}
Beispiel #7
0
func checkTest(t *testing.T, test Test, n ast.Node, err error) {
	if err != nil {
		t.Logf("error: %s", err)
	}
	if n == nil && len(test.types) != 0 {
		t.Logf("%s: expr is nil, expected %d types", test.name, len(test.types))
		t.Fail()
	}

	if test.pass {
		ast.Walk(n, &Tester{types: test.types, t: t})
	}
	if t.Failed() {
		t.Logf("%s: %#v", test.name, n)
		t.Fatalf("%s: failed parsing expression: %s", test.name, test.src)
	}
}