Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Dec 7, 2024
1 parent a233384 commit 0db54bc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
20 changes: 2 additions & 18 deletions core/src/main/scala/org/bykn/bosatsu/MatchlessToValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,7 @@ object MatchlessToValue {
val resFn = loop(res)
val capScoped = caps.map(loop).toVector
Dynamic { scope =>
val initMutKeys = scope.muts.keySet
val valuesInScope = capScoped.map(scoped => scoped(scope))
// these can't change the keyset
require(initMutKeys == scope.muts.keySet)
// now we ignore the scope after reading from it
val scope1 = Scope.capture(valuesInScope, scope.extra)

Expand All @@ -368,10 +365,7 @@ object MatchlessToValue {
val resFn = loop(res)
val capScoped = caps.map(loop).toVector
Dynamic { scope =>
val initMutKeys = scope.muts.keySet
val valuesInScope = capScoped.map(scoped => scoped(scope))
// these can't change the keyset
require(initMutKeys == scope.muts.keySet)

lazy val scope1: Scope = Scope
.capture(valuesInScope)
Expand All @@ -395,20 +389,10 @@ object MatchlessToValue {
// has failed
Dynamic { (scope: Scope) =>
var c = condF(scope)
def printOnFail[A](a: => A, msg: => String): A = {
try a
catch {
case t: Throwable =>
println(msg)
throw t
}
}
while(c) {
//println(s"loop iteration: ${scope.debugString}")
printOnFail(effectF(scope), s"failed in effect:\n\n$effect\n\n${scope.debugString}")
c = printOnFail(condF(scope), "failed in check")
effectF(scope)
c = condF(scope)
}

scope.muts(result.ident).get()
}
case Global(p, n) =>
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/scala/org/bykn/bosatsu/codegen/clang/ClangGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,17 @@ object ClangGen {
.flatMapN { (c, thenC, elseC) =>
Code.ValueLike.ifThenElseV(c, thenC, elseC)(newLocalName)
}
case Always.SetChain(setmuts, result) =>
(
setmuts.traverse { case (LocalAnonMut(mut), v) =>
for {
name <- getAnon(mut)
vl <- innerToValue(v)
} yield (name := vl)
}, innerToValue(result)
).mapN { (assigns, result) =>
Code.Statements(assigns) +: result
}
case Always(cond, thenExpr) =>
boolToValue(cond).flatMap { bv =>
bv.discardValue match {
Expand Down
35 changes: 23 additions & 12 deletions core/src/main/scala/org/bykn/bosatsu/codegen/python/PythonGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1713,19 +1713,17 @@ object PythonGen {
} yield block.withValue(defName)
}
case WhileExpr(cond, effect, res) =>
(boolExpr(cond, slotName), loop(effect, slotName), loop(res, slotName))
.flatMapN { (cond, effect, res) =>
Env.newAssignableVar.map { c =>
Code.block(
c := cond,
Code.While(c,
Code.block(
Code.always(effect),
c := cond
)
(boolExpr(cond, slotName), loop(effect, slotName), loop(res, slotName), Env.newAssignableVar)
.mapN { (cond, effect, res, c) =>
Code.block(
c := cond,
Code.While(c,
Code.block(
Code.always(effect),
c := cond
)
).withValue(res)
}
)
).withValue(res)
}
case PredefExternal((fn, arity)) =>
// make a lambda
Expand Down Expand Up @@ -1838,6 +1836,19 @@ object PythonGen {
Env.ifElse(ifs, elseV)
}.flatten

case Always.SetChain(setmuts, result) =>
(
setmuts.traverse { case (LocalAnonMut(mut), v) =>
Env.nameForAnon(mut).product(loop(v, slotName))
}, loop(result, slotName)
).mapN { (assigns, result) =>
Code.blockFromList(
assigns.toList.map { case (mut, v) =>
mut := v
}
)
.withValue(result)
}
case Always(cond, expr) =>
(boolExpr(cond, slotName).map(Code.always), loop(expr, slotName))
.mapN(_.withValue(_))
Expand Down

0 comments on commit 0db54bc

Please sign in to comment.