Exemple #1
0
func TestSimpleDeclStmt(t *testing.T) {
	input := `var c = 9`
	parser := Parse("TestSimpleDeclStmt", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.DeclStmt{
			Decl: &ast.GenDecl{
				Tok: lex.Token{Typ: lex.VAR, Val: "var"},
				Spec: &ast.ValueSpec{
					Name:  &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "c"}},
					Value: &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "9"}},
				},
			},
		},
	}
	expected := &ast.File{
		List: stmtList,
	}

	if !ast.Equals(parser.File, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #2
0
func TestCompositeParen(t *testing.T) {
	input := `(( 4 ) + ( 4 ))`
	parser := Parse("TestCompositeParen", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.ExprStmt{
			X: &ast.ParenExpr{
				Lparen: lex.Token{Typ: lex.LEFTPAREN, Val: "("},
				Rparen: lex.Token{Typ: lex.RIGHTPAREN, Val: ")"},
				X: &ast.BinaryExpr{
					X: &ast.ParenExpr{
						Lparen: lex.Token{Typ: lex.LEFTPAREN, Val: "("},
						Rparen: lex.Token{Typ: lex.RIGHTPAREN, Val: ")"},
						X:      &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "4"}},
					},
					Op: lex.Token{Typ: lex.ADD, Val: "+"},
					Y: &ast.ParenExpr{
						Lparen: lex.Token{Typ: lex.LEFTPAREN, Val: "("},
						Rparen: lex.Token{Typ: lex.RIGHTPAREN, Val: ")"},
						X:      &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "4"}},
					},
				},
			},
		},
	}
	expected := &ast.File{
		List: stmtList,
	}
	if !ast.Equals(parser.File, expected) {
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #3
0
func TestSimpleAssignStmt(t *testing.T) {
	input := `c = 9`
	parser := Parse("TestSimpleAssignStmt", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.AssignStmt{
			Lhs: &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "c"}},
			Tok: lex.Token{Typ: lex.ASSIGN, Val: "="},
			Rhs: &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "9"}},
		},
	}
	expected := &ast.File{
		List: stmtList,
	}

	// if !ast.Equals(parser.File.List[0].(*ast.AssignStmt).Rhs, expected.List[0].(*ast.AssignStmt).Rhs) {
	// 	t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	// }

	if !ast.Equals(parser.File, expected) {
		//t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #4
0
func TestMultyAssignStmt(t *testing.T) {
	input := `a = true;b=10;c=b`
	parser := Parse("TestSimpleAssignStmt", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.AssignStmt{
			Lhs: &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "a"}},
			Tok: lex.Token{Typ: lex.ASSIGN, Val: "="},
			Rhs: &ast.BasicLit{Tok: lex.Token{Typ: lex.BOOL, Val: "true"}},
		},
		&ast.AssignStmt{
			Lhs: &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "b"}},
			Tok: lex.Token{Typ: lex.ASSIGN, Val: "="},
			Rhs: &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "10"}},
		},
		&ast.AssignStmt{
			Lhs: &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "c"}},
			Tok: lex.Token{Typ: lex.ASSIGN, Val: "="},
			Rhs: &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "b"}},
		},
	}
	expected := &ast.File{
		List: stmtList,
	}

	if !ast.Equals(parser.File, expected) {
		//t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #5
0
func TestSimpleBoolExpr(t *testing.T) {
	input :=
		`true || false && test`
	parser := Parse("TestSimpleBoolExpr", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.ExprStmt{X: &ast.BinaryExpr{
			X:  &ast.BasicLit{Tok: lex.Token{Typ: lex.BOOL, Val: "true"}},
			Op: lex.Token{Typ: lex.LOR, Val: "||"},
			Y: &ast.BinaryExpr{
				X:  &ast.BasicLit{Tok: lex.Token{Typ: lex.BOOL, Val: "false"}},
				Op: lex.Token{Typ: lex.LAND, Val: "&&"},
				Y:  &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "test"}},
			},
		}},
	}
	expected := &ast.File{
		List: stmtList,
	}
	if !ast.Equals(parser.File, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #6
0
func TestMultiLineParenExpr(t *testing.T) {
	input :=
		`((10)
*1
/9)`
	parser := Parse("TestMultiLineParenExpr", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.ExprStmt{X: &ast.ParenExpr{
			Lparen: lex.Token{Typ: lex.LEFTPAREN, Val: "("},
			Rparen: lex.Token{Typ: lex.RIGHTPAREN, Val: ")"},
			X: &ast.BinaryExpr{
				X: &ast.BinaryExpr{
					X: &ast.ParenExpr{
						Lparen: lex.Token{Typ: lex.LEFTPAREN, Val: "("},
						Rparen: lex.Token{Typ: lex.RIGHTPAREN, Val: ")"},
						X:      &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "10"}},
					},
					Op: lex.Token{Typ: lex.MUL, Val: "*"},
					Y:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "1"}},
				},
				Op: lex.Token{Typ: lex.QUO, Val: "/"},
				Y:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "9"}}},
		}},
	}
	expected := &ast.File{
		List: stmtList,
	}
	if !ast.Equals(parser.File, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #7
0
func TestAssociativityADDSUB(t *testing.T) {
	input := `7-4+2`
	parser := Parse("TestAssociativityADDSUB", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.ExprStmt{
			X: &ast.BinaryExpr{
				X: &ast.BinaryExpr{
					X:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "7"}},
					Op: lex.Token{Typ: lex.SUB, Val: "-"},
					Y:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "4"}},
				},
				Op: lex.Token{Typ: lex.ADD, Val: "+"},
				Y:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "2"}},
			},
		},
	}
	expected := &ast.File{
		List: stmtList,
	}
	if !ast.Equals(parser.File, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #8
0
func TestSimpleBinaryAdd(t *testing.T) {
	input := `4+4`
	parser := Parse("TestSimpleBinaryAdd", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.ExprStmt{
			X: &ast.BinaryExpr{
				X:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "4"}},
				Op: lex.Token{Typ: lex.ADD, Val: "+"},
				Y: &ast.BasicLit{
					Tok: lex.Token{
						Typ: lex.INT,
						Val: "4",
					},
				},
			},
		},
	}
	expected := &ast.File{
		List: stmtList,
	}
	if !ast.Equals(parser.File, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #9
0
func TestMultiExprSemiColonSeperated(t *testing.T) {
	input :=
		`
4+(4);4*4;((100)/90);-aTest;
`
	parser := Parse("TestMultiExprSemiColonSeperated", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.ExprStmt{X: &ast.BinaryExpr{
			X:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "4"}},
			Op: lex.Token{Typ: lex.ADD, Val: "+"},
			Y: &ast.ParenExpr{Lparen: lex.Token{Typ: lex.LEFTPAREN, Val: "("},
				X: &ast.BasicLit{
					Tok: lex.Token{
						Typ: lex.INT,
						Val: "4",
					},
				},
				Rparen: lex.Token{Typ: lex.RIGHTPAREN, Val: ")"},
			},
		}},
		&ast.ExprStmt{X: &ast.BinaryExpr{
			X:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "4"}},
			Op: lex.Token{Typ: lex.MUL, Val: "*"},
			Y:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "4"}},
		}},
		&ast.ExprStmt{X: &ast.ParenExpr{
			Lparen: lex.Token{Typ: lex.LEFTPAREN, Val: "("},
			Rparen: lex.Token{Typ: lex.RIGHTPAREN, Val: ")"},
			X: &ast.BinaryExpr{
				X: &ast.ParenExpr{
					Lparen: lex.Token{Typ: lex.LEFTPAREN, Val: "("},
					Rparen: lex.Token{Typ: lex.RIGHTPAREN, Val: ")"},
					X:      &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "100"}},
				},
				Op: lex.Token{Typ: lex.QUO, Val: "/"},
				Y:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "90"}},
			},
		}},
		&ast.ExprStmt{X: &ast.UnaryExpr{Op: lex.Token{Typ: lex.SUB, Val: "-"},
			X: &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "aTest"}},
		}},
	}
	expected := &ast.File{
		List: stmtList,
	}
	if !ast.Equals(parser.File, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #10
0
func TestSimpleUnary(t *testing.T) {
	input := `-2`
	parser := Parse("TestSimpleUnary", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.ExprStmt{
			X: &ast.UnaryExpr{
				Op: lex.Token{Typ: lex.SUB, Val: "-"},
				X:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "2"}},
			},
		},
	}
	expected := &ast.File{
		List: stmtList,
	}
	if !ast.Equals(parser.File, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}
Exemple #11
0
func TestSimpleArithmeticPrecedence(t *testing.T) {
	// a+b*2-3/4%a = (a + (b*2)) - ((3/4)%a)
	input := `a+b*2-3/4%a`
	parser := Parse("TestSimpleArithmeticPrecedence", input)

	output := parser.File
	stmtList := []ast.Stmt{
		&ast.ExprStmt{
			X: &ast.BinaryExpr{
				X: &ast.BinaryExpr{
					X:  &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "a"}},
					Op: lex.Token{Typ: lex.ADD, Val: "+"},
					Y: &ast.BinaryExpr{
						X:  &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "b"}},
						Op: lex.Token{Typ: lex.MUL, Val: "*"},
						Y:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "2"}},
					},
				},
				Op: lex.Token{Typ: lex.SUB, Val: "-"},
				Y: &ast.BinaryExpr{
					X: &ast.BinaryExpr{
						X:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "3"}},
						Op: lex.Token{Typ: lex.QUO, Val: "/"},
						Y:  &ast.BasicLit{Tok: lex.Token{Typ: lex.INT, Val: "4"}},
					},
					Op: lex.Token{Typ: lex.REM, Val: "%"},
					Y:  &ast.Ident{Tok: lex.Token{Typ: lex.IDENTIFIER, Val: "a"}},
				},
			},
		},
	}
	expected := &ast.File{
		List: stmtList,
	}
	if !ast.Equals(parser.File, expected) {
		// t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", spew.Sdump(expected), spew.Sdump(output))
		t.Errorf("\nExpected:\n%s\n\nGot:\n%s\n", expected.String(), output.String())
	}
}