Skip to content

Commit

Permalink
Fix finding free variables
Browse files Browse the repository at this point in the history
  • Loading branch information
phischu committed Jan 10, 2025
1 parent 3cbf9cf commit cdea340
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,14 @@ object Transformer {

case machine.Switch(value, clauses, default) =>
emit(Comment(s"switch ${value.name}, ${clauses.length} clauses"))
shareValues(List(value), clauses.flatMap(freeVariables).toSet)
val freeInClauses = clauses.flatMap(freeVariables) ++ default.map(freeVariables).getOrElse(Set.empty)
shareValues(List(value), freeInClauses.toSet)

val tagName = freshName("tag")
val objectName = freshName("fields")
emit(ExtractValue(tagName, transform(value), 0))
emit(ExtractValue(objectName, transform(value), 1))

val freeInClauses = clauses.flatMap(freeVariables)

val stack = getStack()
def labelClause(clause: machine.Clause): String = {
implicit val BC = BlockContext()
Expand Down
2 changes: 1 addition & 1 deletion effekt/shared/src/main/scala/effekt/machine/Analysis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def freeVariables(statement: Statement): Set[Variable] =
case Construct(name, tag, values, rest) =>
Set.from(values) ++ (freeVariables(rest) -- Set(name))
case Switch(value, clauses, default: Option[Clause]) =>
Set(value) ++ clauses.flatMap { case (tag, branch) => freeVariables(branch) } ++ default.map(freeVariables).getOrElse(Set.empty)
Set(value) ++ clauses.flatMap(freeVariables) ++ default.map(freeVariables).getOrElse(Set.empty)
case New(name, clauses, rest) =>
freeVariables(clauses) ++ (freeVariables(rest) -- Set(name))
case Invoke(value, tag, values) =>
Expand Down

0 comments on commit cdea340

Please sign in to comment.