File tree Expand file tree Collapse file tree 2 files changed +45
-7
lines changed Expand file tree Collapse file tree 2 files changed +45
-7
lines changed Original file line number Diff line number Diff line change @@ -75,26 +75,36 @@ class ReplCompiler extends Compiler {
75
75
76
76
implicit val ctx : Context = state.context
77
77
78
+ // If trees is of the form `{ def1; def2; def3 }` then `List(def1, def2, def3)`
79
+ val flattened = trees match {
80
+ case List (Block (stats, expr)) =>
81
+ if (expr eq EmptyTree ) stats // happens when expr is not an expression
82
+ else stats :+ expr
83
+ case _ =>
84
+ trees
85
+ }
86
+
78
87
var valIdx = state.valIndex
88
+ val defs = new mutable.ListBuffer [Tree ]
79
89
80
- val defs = trees.flatMap {
90
+ flattened.foreach {
81
91
case expr @ Assign (id : Ident , _) =>
82
92
// special case simple reassignment (e.g. x = 3)
83
93
// in order to print the new value in the REPL
84
94
val assignName = (id.name ++ str.REPL_ASSIGN_SUFFIX ).toTermName
85
95
val assign = ValDef (assignName, TypeTree (), id).withPos(expr.pos)
86
- List ( expr, assign)
96
+ defs += expr += assign
87
97
case expr if expr.isTerm =>
88
98
val resName = (str.REPL_RES_PREFIX + valIdx).toTermName
89
99
valIdx += 1
90
100
val vd = ValDef (resName, TypeTree (), expr).withPos(expr.pos)
91
- vd :: Nil
101
+ defs += vd
92
102
case other =>
93
- other :: Nil
103
+ defs += other
94
104
}
95
105
96
106
Definitions (
97
- defs,
107
+ defs.toList ,
98
108
state.copy(
99
109
objectIndex = state.objectIndex + (if (defs.isEmpty) 0 else 1 ),
100
110
valIndex = valIdx
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ import org.junit.{Ignore, Test}
7
7
8
8
class ReplCompilerTests extends ReplTest {
9
9
10
+ private def lines () = storedOutput().split(EOL ).toList
11
+
10
12
@ Test def compileSingle = fromInitialState { implicit state =>
11
13
run(" def foo: 1 = 1" )
12
14
assertEquals(" def foo: Int(1)" , storedOutput().trim)
@@ -53,7 +55,7 @@ class ReplCompilerTests extends ReplTest {
53
55
" val res1: Int = 20"
54
56
)
55
57
56
- assertEquals(expected, storedOutput().split( EOL ).toList )
58
+ assertEquals(expected, lines() )
57
59
}
58
60
59
61
@ Test def testImportMutable =
@@ -124,6 +126,32 @@ class ReplCompilerTests extends ReplTest {
124
126
)
125
127
126
128
run(source)
127
- assertEquals(expected, storedOutput().split( EOL ).toList )
129
+ assertEquals(expected, lines() )
128
130
}
131
+
132
+ @ Test def topLevelBlock =
133
+ fromInitialState { implicit state =>
134
+ val source =
135
+ """ {
136
+ | def foo = 1
137
+ | case class A(x: Int)
138
+ | foo
139
+ | def bar = 2
140
+ |}""" .stripMargin
141
+
142
+ val expected = List (
143
+ " // defined case class A" ,
144
+ " def foo: Int" ,
145
+ " def bar: Int" ,
146
+ " val res0: Int = 1"
147
+ )
148
+
149
+ val ns = run(source)
150
+ assertEquals(expected, lines())
151
+ ns
152
+ }
153
+ .andThen { implicit state =>
154
+ run(" val a = A(foo + bar)" )
155
+ assertEquals(" val a: A = A(3)" , storedOutput().trim)
156
+ }
129
157
}
You can’t perform that action at this time.
0 commit comments