Skip to content

Commit dbb7c81

Browse files
committed
Add Let to staged interpreter test
1 parent 22d7429 commit dbb7c81

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

tests/pos/stagedInterpreter.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@ enum Exp {
44
case Num(n: Int)
55
case Plus(e1: Exp, e2: Exp)
66
case Var(x: String)
7+
case Let(x: String, e: Exp, in: Exp)
78
}
89

910
object Test {
1011
import Exp._
1112

13+
val keepLets = true
14+
1215
val exp = Plus(Plus(Num(2), Var("x")), Num(4))
1316

17+
val letExp = Let("x", Num(3), exp)
18+
1419
def compile(e: Exp, env: Map[String, Expr[Int]]): Expr[Int] = e match {
1520
case Num(n) => n
1621
case Plus(e1, e2) => '(~compile(e1, env) + ~compile(e2, env))
1722
case Var(x) => env(x)
23+
case Let(x, e, body) =>
24+
if (keepLets)
25+
'{ val y = ~compile(e, env); ~compile(body, env + (x -> '(y))) }
26+
else
27+
compile(body, env + (x -> compile(e, env)))
1828
}
1929

20-
val res = (x: Int) => ~compile(exp, Map("x" -> '(x)))
30+
val res1 = (x: Int) => ~compile(exp, Map("x" -> '(x)))
2131

32+
val res2 = compile(letExp, Map())
2233
}

0 commit comments

Comments
 (0)