Skip to content

Commit 73a6816

Browse files
committed
Make staged interpreter a run test
1 parent b916c14 commit 73a6816

File tree

4 files changed

+78
-37
lines changed

4 files changed

+78
-37
lines changed

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ class CompilationTests extends ParallelTesting {
196196
compileFilesInDir("../tests/run", defaultOptions) +
197197
compileFilesInDir("../tests/run-no-optimise", defaultOptions) +
198198
compileFile("../tests/run-special/quote-run.scala", defaultRunWithCompilerOptions) +
199-
compileFile("../tests/run-special/quote-run-2.scala", defaultRunWithCompilerOptions)
199+
compileFile("../tests/run-special/quote-run-2.scala", defaultRunWithCompilerOptions) +
200+
compileFile("../tests/run-special/quote-run-staged-interpreter.scala", defaultRunWithCompilerOptions)
200201
}.checkRuns()
201202

202203
// Generic java signatures tests ---------------------------------------------

tests/pos/quote-stagedInterpreter.scala

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
{
3+
def $anonfun(x: Int): Int =
4+
{
5+
2.+(x).+(4)
6+
}
7+
closure($anonfun)
8+
}
9+
}
10+
6
11+
8
12+
9
13+
---
14+
2.+(3).+(4)
15+
9
16+
---
17+
{
18+
val y: Int = 3
19+
2.+(y).+(4)
20+
}
21+
9
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import scala.quoted._
2+
import dotty.tools.dotc.quoted.Runners._
3+
4+
enum Exp {
5+
case Num(n: Int)
6+
case Plus(e1: Exp, e2: Exp)
7+
case Var(x: String)
8+
case Let(x: String, e: Exp, in: Exp)
9+
}
10+
11+
object Test {
12+
import Exp._
13+
14+
def compile(e: Exp, env: Map[String, Expr[Int]], keepLets: Boolean): Expr[Int] = {
15+
def compileImpl(e: Exp, env: Map[String, Expr[Int]]): Expr[Int] = e match {
16+
case Num(n) => n
17+
case Plus(e1, e2) => '(~compileImpl(e1, env) + ~compileImpl(e2, env))
18+
case Var(x) => env(x)
19+
case Let(x, e, body) =>
20+
if (keepLets)
21+
'{ val y = ~compileImpl(e, env); ~compileImpl(body, env + (x -> '(y))) }
22+
else
23+
compileImpl(body, env + (x -> compileImpl(e, env)))
24+
}
25+
compileImpl(e, env)
26+
}
27+
28+
29+
def main(args: Array[String]): Unit = {
30+
val exp = Plus(Plus(Num(2), Var("x")), Num(4))
31+
val letExp = Let("x", Num(3), exp)
32+
33+
val res1 = '{ (x: Int) => ~compile(exp, Map("x" -> '(x)), false) }
34+
35+
36+
println(res1.show)
37+
38+
val fn = res1.run
39+
println(fn(0))
40+
println(fn(2))
41+
println(fn(3))
42+
43+
println("---")
44+
45+
val res2 = compile(letExp, Map(), false)
46+
println(res2.show)
47+
println(res2.run)
48+
49+
println("---")
50+
51+
val res3 = compile(letExp, Map(), true)
52+
println(res3.show)
53+
println(res3.run)
54+
}
55+
}

0 commit comments

Comments
 (0)