Skip to content

Commit

Permalink
evaluate blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
iusildra committed Jul 3, 2023
1 parent 01db315 commit 13e1dd6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RuntimeDefaultEvaluator(val frame: JdiFrame, val logger: Logger) extends R
case branching: IfTree => evaluateIf(branching)
case staticMethod: StaticMethodTree => invokeStatic(staticMethod)
case outer: OuterTree => evaluateOuter(outer)
case UnitTree => Safe(JdiValue(frame.thread.virtualMachine.mirrorOfVoid, frame.thread))
}

/* -------------------------------------------------------------------------- */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class RuntimeDefaultValidator(val frame: JdiFrame, val logger: Logger) extends R
case select: Term.Select => validateSelect(select)
case branch: Term.If => validateIf(branch)
case instance: Term.New => validateNew(instance)
case block: Term.Block => validateBlock(block)
case _ => Recoverable("Expression not supported at runtime")
}

Expand All @@ -53,6 +54,15 @@ class RuntimeDefaultValidator(val frame: JdiFrame, val logger: Logger) extends R
case _ => validate(expression)
}

/* -------------------------------------------------------------------------- */
/* Block validation */
/* -------------------------------------------------------------------------- */
def validateBlock(block: Term.Block): Validation[RuntimeEvaluableTree] =
block.stats.foldLeft(Valid(UnitTree): Validation[RuntimeEvaluableTree]) {
case (Valid(_), stat) => validate(stat)
case (err: Invalid, _) => err
}

/* -------------------------------------------------------------------------- */
/* Literal validation */
/* -------------------------------------------------------------------------- */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected[internal] object RuntimeEvaluatorExtractors {
case OuterModuleTree(module) => unapply(module)
case IfTree(p, t, f, _) => unapply(p) || unapply(t) || unapply(f)
case _: MethodTree | _: NewInstanceTree => true
case _: LiteralTree | _: LocalVarTree | _: PreEvaluatedTree | _: ThisTree => false
case _: LiteralTree | _: LocalVarTree | _: PreEvaluatedTree | _: ThisTree | UnitTree => false
case _: StaticFieldTree | _: ClassTree | _: TopLevelModuleTree => false
case _: PrimitiveBinaryOpTree | _: PrimitiveUnaryOpTree | _: ArrayElemTree => false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ sealed trait OuterTree extends RuntimeEvaluableTree
/* -------------------------------------------------------------------------- */
/* Simple trees */
/* -------------------------------------------------------------------------- */
case object UnitTree extends RuntimeEvaluableTree {
override def `type`: Type = ???
override def prettyPrint(depth: Int): String = "UnitTree()"
}
case class LiteralTree private (
value: Safe[Any],
`type`: Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object RuntimeEvaluatorEnvironments {
| private val lapinou = "lapinou"
| def bar(str: String, int: Int): String = str + int
| def keepTheRabbit: String = lapinou
| def bar(i: Int) = s"bar ${i}"
|}
|
|object Foo { val foofoo = "foofoo" }
Expand Down Expand Up @@ -792,6 +793,15 @@ abstract class RuntimeEvaluatorTests(val scalaVersion: ScalaVersion) extends Deb
Evaluation.success("test(test)", "test -1")
)
}

test("Should evaluate blocks") {
implicit val debuggee = field
check(
Breakpoint(8),
Evaluation.success("{ 1+1; 2+2; lapin}", "lapin"),
Evaluation.success("f1.bar { 1+1; 2+2 }", "bar 4")
)
}
}

/* -------------------------------------------------------------------------- */
Expand Down

0 comments on commit 13e1dd6

Please sign in to comment.