finalwq
This commit is contained in:
@@ -38,4 +38,20 @@ class CompilerTest extends TimedSuite {
|
||||
testCompiler(LetRec(Nil, Prim("-", List(Lit(10), Lit(2)))), 8)
|
||||
}
|
||||
|
||||
test("extra_credit_fn_as_var") {
|
||||
testCompiler(LetRec(Nil,Let("x",UnknownType,Ref("putchar"),Let("x$1",UnknownType,App(Ref("x"),List(Lit(97))),Lit(69)))), 69)
|
||||
}
|
||||
|
||||
test("extra_credit_char_parse_toInt_toChar") {
|
||||
testCompiler(LetRec(Nil,Let("x$1",UnitType,App(Ref("putchar"),List(App(Ref("toInt"),List(Lit('s'))))),Let("x$2",UnitType,App(Ref("putchar"),List(App(Ref("toInt"),List(Lit('s'))))),Let("x$3",UnitType,App(Ref("putchar"),List(App(Ref("toInt"),List(Lit('i'))))),Lit(96))))), 96)
|
||||
testCompiler(LetRec(List(),App(Ref("toInt"),List(App(Ref("toChar"),List(Lit(420)))))), 164) // Int->Char narrow
|
||||
}
|
||||
|
||||
test("func_decl_call") {
|
||||
val pos = Position(0,0,0,0,0,0)
|
||||
//def f(x:Int, y:Int, z:Char) = (x - y)*20 + 10 * toInt(z); f(6, 9, '0') == 420
|
||||
testCompiler(LetRec(List(FunDef("f",List(Arg("x",IntType,pos), Arg("y",IntType,Position(0,0,0,0,0,0)), Arg("z",CharType,Position(0,0,0,0,0,0))),IntType,Prim("+",List(Prim("*",List(Prim("-",List(Ref("x"), Ref("y"))), Lit(20))), Prim("*",List(Lit(10), App(Ref("toInt"),List(Ref("z"))))))))),App(Ref("f"),List(Lit(6), Lit(9), Lit('0')))), 420)
|
||||
//def f(x:Int) = 1 + x; val y = f(1); val z = f(2); y + z
|
||||
testCompiler(LetRec(List(FunDef("f",List(Arg("x",IntType,pos)),IntType,Prim("+",List(Lit(1), Ref("x"))))),Let("y",IntType,App(Ref("f"),List(Lit(1))),Let("z",IntType,App(Ref("f"),List(Lit(2))),Prim("+",List(Ref("y"), Ref("z")))))), 5)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,4 +17,20 @@ class InterpretTest extends TimedSuite {
|
||||
testInterpreter(Prim("-", List(Lit(10), Lit(2))), Cst(8))
|
||||
}
|
||||
|
||||
// test("extra_credit_fn_as_var") {
|
||||
// testInterpreter(LetRec(Nil,Let("x",UnknownType,Ref("putchar"),Let("x$1",UnknownType,App(Ref("x"),List(Lit(97))),Lit(69)))), Cst(69))
|
||||
// }
|
||||
|
||||
// test("extra_credit_char_parse_toInt_toChar") {
|
||||
// testInterpreter(LetRec(Nil,Let("x$1",UnitType,App(Ref("putchar"),List(App(Ref("toInt"),List(Lit('s'))))),Let("x$2",UnitType,App(Ref("putchar"),List(App(Ref("toInt"),List(Lit('s'))))),Let("x$3",UnitType,App(Ref("putchar"),List(App(Ref("toInt"),List(Lit('i'))))),Lit(96))))), Cst(96))
|
||||
// testInterpreter(LetRec(List(),App(Ref("toInt"),List(App(Ref("toChar"),List(Lit(420)))))), Cst(164)) // Int->Char narrow
|
||||
// }
|
||||
|
||||
// test("func_decl_call") {
|
||||
// val pos = Position(0,0,0,0,0,0)
|
||||
// //def f(x:Int, y:Int, z:Char) = (x - y)*20 + 10 * toInt(z); f(6, 9, '0') == 420
|
||||
// testInterpreter(LetRec(List(FunDef("f",List(Arg("x",IntType,pos), Arg("y",IntType,Position(0,0,0,0,0,0)), Arg("z",CharType,Position(0,0,0,0,0,0))),IntType,Prim("+",List(Prim("*",List(Prim("-",List(Ref("x"), Ref("y"))), Lit(20))), Prim("*",List(Lit(10), App(Ref("toInt"),List(Ref("z"))))))))),App(Ref("f"),List(Lit(6), Lit(9), Lit('0')))), Cst(420))
|
||||
// //def f(x:Int) = 1 + x; val y = f(1); val z = f(2); y + z
|
||||
// testInterpreter(LetRec(List(FunDef("f",List(Arg("x",IntType,pos)),IntType,Prim("+",List(Lit(1), Ref("x"))))),Let("y",IntType,App(Ref("f"),List(Lit(1))),Let("z",IntType,App(Ref("f"),List(Lit(2))),Prim("+",List(Ref("y"), Ref("z")))))), Cst(5))
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -30,4 +30,5 @@ class ParserTest extends TimedSuite {
|
||||
test("ParseOptionalType") {
|
||||
testBaseParser("val x = 1; 2", Let("x", UnknownType, Lit(1), Lit(2)))
|
||||
}
|
||||
//see examples/hello.scala and pow.scala for more comprehensive tests
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user