Skip to content

Commit

Permalink
debug free variables
Browse files Browse the repository at this point in the history
  • Loading branch information
b-studios committed Nov 24, 2023
1 parent 3d53d57 commit a1afa49
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions effekt/shared/src/main/scala/effekt/core/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ object freeVariables {
def bound(t: BlockParam): Variables = Variables.block(t.id)

def bound(d: Definition): Variables = d match {
case Definition.Def(id, block) => Variables.value(id)
case Definition.Let(id, binding) => Variables.block(id)
case Definition.Def(id, block) => Variables.block(id)
case Definition.Let(id, binding) => Variables.value(id)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package js

import effekt.context.Context
import effekt.context.assertions.*
import effekt.core.{given, *}

import effekt.symbols.{ Symbol, Module, Wildcard }
import effekt.core.{ *, given }
import effekt.core.freeVariables.Variables
import effekt.symbols.{ Module, Symbol, Wildcard }


/**
Expand Down Expand Up @@ -87,12 +87,13 @@ object TransformerDS {
def Return[T](t: T): Bind[T] = k => List(k(t))
def Bind[T](b: Bind[T]): Bind[T] = b

def entrypoint(s: List[js.Stmt]): List[js.Stmt] = List(js.Try(s, JSName("k"), Nil))

// List(js.ExprStmt(js.MethodCall($effekt, JSName("entrypoint"), js.Lambda(
// Nil, js.MaybeBlock(s)
// ))))

def entrypoint(result: Id, vars: Variables)(s: List[js.Stmt]): List[js.Stmt] = List(js.Try(s, JSName("k"), List(RawStmt(
s""" /*
| * result: ${uniqueName(result)}
| * free value variables: ${vars.values.map(uniqueName).mkString(", ")}
| * free block variables: ${vars.blocks.map(uniqueName).mkString(", ")}
| */
|""".stripMargin))))

def toJS(s: core.Stmt)(using DeclarationContext, Context): Bind[js.Expr] = s match {

Expand Down Expand Up @@ -124,7 +125,8 @@ object TransformerDS {
// (sw :: stmts, ret)

case Val(Wildcard(), binding, body) =>
Bind { k => entrypoint { toJS(binding)(x => js.ExprStmt(x)) } ++ toJS(body)(k) }
val free = freeVariables.free(body)
Bind { k => entrypoint(Wildcard(), free) { toJS(binding)(x => js.ExprStmt(x)) } ++ toJS(body)(k) }

// this is the whole reason for the Bind monad
// [[ val x = bind; body ]](k) =
Expand All @@ -133,7 +135,8 @@ object TransformerDS {
// [[body]](k)
case Val(id, binding, body) =>
Bind { k =>
js.Let(nameDef(id), Undefined) :: entrypoint { toJS(binding)(x => js.Assign(nameRef(id), x)) } ::: toJS(body)(k)
val free = freeVariables.free(body) -- Variables.value(id)
js.Let(nameDef(id), Undefined) :: entrypoint(id, free) { toJS(binding)(x => js.Assign(nameRef(id), x)) } ::: toJS(body)(k)
}

case Var(id, init, cap, body) =>
Expand Down

0 comments on commit a1afa49

Please sign in to comment.