|
|
@ -42,13 +42,67 @@ class ParserTest extends TimedSuite {
|
|
|
|
assert(ast == res, "Invalid result")
|
|
|
|
assert(ast == res, "Invalid result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*test("SingleDigit") {
|
|
|
|
test("SingleDigit") {
|
|
|
|
testGenericPrecedence("1", Lit(1))
|
|
|
|
testGenericPrecedence("1", Lit(1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test("GenericPrecedence") {
|
|
|
|
test("GenericPrecedence") {
|
|
|
|
testGenericPrecedence("2-4*3", Prim("-", Lit(2), Prim("*", Lit(4), Lit(3))))
|
|
|
|
testGenericPrecedence("2-4*3", Prim("-", Lit(2), Prim("*", Lit(4), Lit(3))))
|
|
|
|
}*/
|
|
|
|
testGenericPrecedence("1+ -2", Prim("+",Lit(1),Unary("-",Lit(2))))
|
|
|
|
|
|
|
|
testGenericPrecedence("1+2*3/4", Prim("+",Lit(1),Prim("/",Prim("*",Lit(2),Lit(3)),Lit(4))))
|
|
|
|
|
|
|
|
testGenericPrecedence("1+(-1)", Prim("+",Lit(1), Unary("-",Lit(1))))
|
|
|
|
|
|
|
|
testGenericPrecedence("(1-2*3/4-5)*6",
|
|
|
|
|
|
|
|
Prim("*",Prim("-",Prim("-",Lit(1),Prim("/",Prim("*",Lit(2),Lit(3)),Lit(4))),Lit(5)),Lit(6)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test("testLetParser") {
|
|
|
|
|
|
|
|
testLetParser("val x=1;x", Let("x", Lit(1), Ref("x")))
|
|
|
|
|
|
|
|
testLetParser("val x = 1; val y = 2; x - y",
|
|
|
|
|
|
|
|
Let("x", Lit(1), Let("y", Lit(2), Prim("-", Ref("x"), Ref("y")))))
|
|
|
|
|
|
|
|
testLetParser("val x = 1; val y = 2; x*3 - y",
|
|
|
|
|
|
|
|
Let("x", Lit(1), Let("y", Lit(2), Prim("-", Prim("*", Ref("x"), Lit(3)), Ref("y")))))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test("testBranchParser") {
|
|
|
|
|
|
|
|
testBranchParser("if(1==2)3 else 4", If(Cond("==",Lit(1), Lit(2)), Lit(3), Lit(4)))
|
|
|
|
|
|
|
|
testBranchParser("val x = 5; if (x == 5) x else 5",
|
|
|
|
|
|
|
|
Let("x",Lit(5),If(Cond("==",Ref("x"),Lit(5)),Ref("x"),Lit(5))))
|
|
|
|
|
|
|
|
testBranchParser("if(3>5){1}else{2}", If(Cond(">", Lit(3), Lit(5)), Lit(1), Lit(2)))
|
|
|
|
|
|
|
|
testBranchParser("if(3>5){if(9<0){1}else{2}}else{4}",
|
|
|
|
|
|
|
|
If(Cond(">", Lit(3), Lit(5)), If(Cond("<", Lit(9), Lit(0)), Lit(1), Lit(2)), Lit(4)))
|
|
|
|
|
|
|
|
assertThrows[Exception] {
|
|
|
|
|
|
|
|
testBranchParser("if(1>2)1", Lit(0))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test("testVariableParser"){
|
|
|
|
|
|
|
|
testVariableParser("var x = 1; var y = 2; x = y = 3",
|
|
|
|
|
|
|
|
VarDec("x", Lit(1), VarDec("y", Lit(2), VarAssign("x", VarAssign("y", Lit(3))))))
|
|
|
|
|
|
|
|
assertThrows[Exception] {
|
|
|
|
|
|
|
|
testVariableParser("var x = 1 1", Lit(0))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Loop Parser
|
|
|
|
|
|
|
|
test("testLoopParser"){
|
|
|
|
|
|
|
|
testLoopParser("var x = 1; var y = 1; while(x <= 0) {val t = x + y; if(t > y) x = t else y = (-x - 1)};x",
|
|
|
|
|
|
|
|
VarDec("x", Lit(1),
|
|
|
|
|
|
|
|
VarDec("y", Lit(1),
|
|
|
|
|
|
|
|
While(Cond("<=", Ref("x"), Lit(0)),
|
|
|
|
|
|
|
|
Let("t", Prim("+", Ref("x"), Ref("y")),
|
|
|
|
|
|
|
|
If(Cond(">", Ref("t"), Ref("y")),
|
|
|
|
|
|
|
|
VarAssign("x", Ref("t")),
|
|
|
|
|
|
|
|
VarAssign("y",Prim("-", Unary("-", Ref("x")), Lit(1))))),
|
|
|
|
|
|
|
|
Ref("x")))))
|
|
|
|
|
|
|
|
assertThrows[Exception] {
|
|
|
|
|
|
|
|
testLoopParser("while(1){};", Lit(0))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThrows[Exception] {
|
|
|
|
|
|
|
|
testLoopParser("while(1 == 1) { val x = 2; }", Lit(0))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
testLoopParser("while(1 == 1) { val x = 2; 2 }; 1", While(Cond("==", Lit(1), Lit(1)), Let("x", Lit(2), Lit(2)), Lit(1)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test("1") {
|
|
|
|
test("1") {
|
|
|
|
testGenericPrecedence(s"${(1 << 31) - 1}", Lit({
|
|
|
|
testGenericPrecedence(s"${(1 << 31) - 1}", Lit({
|
|
|
|
(1 << 31) - 1
|
|
|
|
(1 << 31) - 1
|
|
|
@ -86,12 +140,12 @@ class ParserTest extends TimedSuite {
|
|
|
|
Prim("*", Prim("*", Prim("*", Lit(2), Lit(9)), Lit(5)), Lit(3)),
|
|
|
|
Prim("*", Prim("*", Prim("*", Lit(2), Lit(9)), Lit(5)), Lit(3)),
|
|
|
|
Prim("/", Prim("/", Lit(18), Lit(6)), Lit(3))))
|
|
|
|
Prim("/", Prim("/", Lit(18), Lit(6)), Lit(3))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*test("rt") {
|
|
|
|
test("rt") {
|
|
|
|
testGenericPrecedence("18/6/3",Prim("/",Prim("/",Lit(18),Lit(6)),Lit(3)))
|
|
|
|
testGenericPrecedence("18/6/3",Prim("/",Prim("/",Lit(18),Lit(6)),Lit(3)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test("go"){
|
|
|
|
test("go"){
|
|
|
|
testGenericPrecedence("2*9*5*3",Prim("*",Prim("*",Prim("*",Lit(2),Lit(9)),Lit(5)), Lit(3)))
|
|
|
|
testGenericPrecedence("2*9*5*3",Prim("*",Prim("*",Prim("*",Lit(2),Lit(9)),Lit(5)), Lit(3)))
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
test("8") {
|
|
|
|
test("8") {
|
|
|
|
0
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|