Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.81 KB | None | 0 0
  1. package miniscala
  2.  
  3. import miniscala.Ast._
  4. import miniscala.Interpreter._
  5. import miniscala.TypeChecker.{TypeEnv, TypeError, typeCheck}
  6. import miniscala.parser.Parser.parse
  7.  
  8. object Test {
  9.  
  10.   def main(args: Array[String]): Unit = {
  11.     test("{ def f(x: Int): Int = x; f(2) }", IntVal(2), IntType())
  12.     testFail("{ def f(x: Int): Int = x; f(2, 3) }")
  13.     testFail("{ def f(x: Int): Int = x; f(true) }")
  14.     testTypeFail("{ def f(x: Int): Int = false; 2}")
  15.     testVal("{def f(x: Int): Int = false; 2}", IntVal(2))
  16.     testValFail("{def f(x: Int): Int = false; f(2)}")
  17.     // Mutual recursion
  18.     test(
  19.       """{ def even(x: Int): Boolean =
  20.        |    if (x == 0) true else odd(x-1);
  21.        |  def odd(x: Int): Boolean =
  22.        |    if (x == 0) false else even(x-1);
  23.        |  even(5) }""".stripMargin, BoolVal(false), BoolType())
  24.     // Static scoping:
  25.     test(
  26.       """{ val x = 2;
  27.        |  { def p(): Int = x * 2;
  28.        |    { def q(): Int = p();
  29.        |      { val x = 5;
  30.        |        { def p(): Boolean = true;
  31.        |          q() } } } } }""".stripMargin, IntVal(4), IntType())
  32.  
  33.     test("(i: Int) => i", ClosureVal(List(FunParam("i", Some(IntType()))), None, VarExp("i"), Map()), FunType(List(IntType()), IntType()))
  34.     test("((i: Int) => i * 2)(2)", IntVal(4), IntType())
  35.     test("{ val f = (i: Int) => i * 2; val i = 6; f(2) }", IntVal(4), IntType())
  36.     test("(() => ((i: Int) => i))()", ClosureVal(List(FunParam("i", Some(IntType()))), None, VarExp("i"), Map()), FunType(List(IntType()), IntType()))
  37.     test(
  38.       """{ val x = 2;
  39.        |  def f(i: Int, g: Int => Int): Boolean = g(i) == 2;
  40.        |  f(4, (i: Int) => i / 2) }
  41.      """.stripMargin, BoolVal(true), BoolType())
  42.     test("((f: Int => Int => Int) => f(2)(3))((i: Int) => ((j: Int) => i * j))", IntVal(6), IntType())
  43.     testFail("((i: Int) => i * 2)(false)")
  44.     testFail("(() => 5)(1)")
  45.     testFail("{ val x = 12; x(2) }")
  46.     testVal("((i) => i * 2)(2)", IntVal(4))
  47.     testTypeFail("((i) => i * 2)")
  48.   }
  49.  
  50.   def test(prg: String, rval: Val, rtype: Type) = {
  51.     testVal(prg, rval)
  52.     testType(prg, rtype)
  53.   }
  54.  
  55.   def testFail(prg: String) = {
  56.     testValFail(prg)
  57.     testTypeFail(prg)
  58.   }
  59.  
  60.   def testVal(prg: String, value: Val, env: Env = Map[Id, Val]()) = {
  61.     assert(eval(parse(prg), env) == value)
  62.   }
  63.  
  64.   def testType(prg: String, out: Type, tenv: TypeEnv = Map[Id, Type]()) = {
  65.     assert(typeCheck(parse(prg), tenv) == out)
  66.   }
  67.  
  68.   def testValFail(prg: String) = {
  69.     try {
  70.       eval(parse(prg), Map[Id, Val]())
  71.       assert(false)
  72.     } catch {
  73.       case _: InterpreterError => assert(true)
  74.     }
  75.   }
  76.  
  77.   def testTypeFail(prg: String) = {
  78.     try {
  79.       typeCheck(parse(prg), Map[Id, Type]())
  80.       assert(false)
  81.     } catch {
  82.       case _: TypeError => assert(true)
  83.     }
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement