Skip to content

Commit

Permalink
Remove more lens
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirlogachev committed Sep 27, 2024
1 parent b46f309 commit ac87d73
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.wavesplatform.lang.v1.compiler.Terms.*
import com.wavesplatform.lang.v1.compiler.Types.{CASETYPEREF, NOTHING}
import com.wavesplatform.lang.v1.evaluator.ContextfulNativeFunction.{Extended, Simple}
import com.wavesplatform.lang.v1.evaluator.ctx.*
import com.wavesplatform.lang.v1.evaluator.ctx.EnabledLogEvaluationContext.Lenses
import com.wavesplatform.lang.v1.task.imports.*
import com.wavesplatform.lang.v1.traits.Environment
import com.wavesplatform.lang.*
Expand All @@ -30,16 +29,16 @@ object EvaluatorV1 {
}

class EvaluatorV1[F[_]: Monad, C[_[_]]](implicit ev: Monad[EvalF[F, *]], ev2: Monad[CoevalF[F, *]]) {
private val lenses = new Lenses[F, C]
import lenses.*

private def evalLetBlock(let: LET, inner: EXPR): EvalM[F, C, (EvaluationContext[C, F], EVALUATED)] =
for {
ctx <- get[F, EnabledLogEvaluationContext[C, F], ExecutionError]
blockEvaluation = evalExpr(let.value)
lazyBlock = LazyVal(blockEvaluation.ter(ctx), ctx.l(let.name))
result <- local {
modify[F, EnabledLogEvaluationContext[C, F], ExecutionError](lets.modify(_)(_.updated(let.name, lazyBlock)))
modify[F, EnabledLogEvaluationContext[C, F], ExecutionError](ctx1 =>
ctx1.copy(ec = ctx1.ec.copy(letDefs = ctx1.ec.letDefs.updated(let.name, lazyBlock)))
)
.flatMap(_ => evalExprWithCtx(inner))
}
} yield result
Expand All @@ -49,15 +48,17 @@ class EvaluatorV1[F[_]: Monad, C[_[_]]](implicit ev: Monad[EvalF[F, *]], ev2: Mo
val function = UserFunction(func.name, 0, NOTHING, func.args.map(n => (n, NOTHING))*)(func.body)
.asInstanceOf[UserFunction[C]]
local {
modify[F, EnabledLogEvaluationContext[C, F], ExecutionError](funcs.modify(_)(_.updated(funcHeader, function)))
modify[F, EnabledLogEvaluationContext[C, F], ExecutionError](ctx =>
ctx.copy(ec = ctx.ec.copy(functions = ctx.ec.functions.updated(funcHeader, function)))
)
.flatMap(_ => evalExprWithCtx(inner))
}
}

private def evalRef(key: String): EvalM[F, C, (EvaluationContext[C, F], EVALUATED)] =
for {
ctx <- get[F, EnabledLogEvaluationContext[C, F], ExecutionError]
r <- lets.get(ctx).get(key) match {
r <- ctx.ec.letDefs.get(key) match {
case Some(lzy) => liftTER[F, C, EVALUATED](lzy.value)
case None => raiseError[F, EnabledLogEvaluationContext[C, F], ExecutionError, EVALUATED](s"A definition of '$key' not found")
}
Expand All @@ -83,8 +84,7 @@ class EvaluatorV1[F[_]: Monad, C[_[_]]](implicit ev: Monad[EvalF[F, *]], ev2: Mo
private def evalFunctionCall(header: FunctionHeader, args: List[EXPR]): EvalM[F, C, (EvaluationContext[C, F], EVALUATED)] =
for {
ctx <- get[F, EnabledLogEvaluationContext[C, F], ExecutionError]
result <- funcs
.get(ctx)
result <- ctx.ec.functions
.get(header)
.map {
case func: UserFunction[C] =>
Expand All @@ -94,7 +94,7 @@ class EvaluatorV1[F[_]: Monad, C[_[_]]](implicit ev: Monad[EvalF[F, *]], ev2: Mo
}
local {
val newState: EvalM[F, C, Unit] =
set[F, EnabledLogEvaluationContext[C, F], ExecutionError](lets.set(ctx)(letDefsWithArgs)).map(_.pure[F])
set[F, EnabledLogEvaluationContext[C, F], ExecutionError](ctx.copy(ec = ctx.ec.copy(letDefs = letDefsWithArgs))).map(_.pure[F])
Monad[EvalM[F, C, *]].flatMap(newState)(_ => evalExpr(func.ev(ctx.ec.environment, args)))
}
}: EvalM[F, C, EVALUATED]
Expand All @@ -118,7 +118,7 @@ class EvaluatorV1[F[_]: Monad, C[_[_]]](implicit ev: Monad[EvalF[F, *]], ev2: Mo
// no such function, try data constructor
header match {
case FunctionHeader.User(typeName, _) =>
types.get(ctx).get(typeName).collect { case t @ CASETYPEREF(_, fields, _) =>
ctx.ec.typeDefs.get(typeName).collect { case t @ CASETYPEREF(_, fields, _) =>
args
.traverse[EvalM[F, C, *], EVALUATED](evalExpr)
.map(values => CaseObj(t, fields.map(_._1).zip(values).toMap): EVALUATED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.wavesplatform.lang.v1.compiler.Terms.LET
import com.wavesplatform.lang.v1.compiler.Types.FINAL
import com.wavesplatform.lang.v1.evaluator.Contextful.NoContext
import com.wavesplatform.lang.v1.evaluator.{Contextful, LetExecResult, LetLogCallback}
import shapeless.{Lens, lens}

import java.util

Expand Down Expand Up @@ -51,17 +50,6 @@ case class EnabledLogEvaluationContext[C[_[_]], F[_]: Monad](l: LetLogCallback[F
loggedLets.computeIfAbsent(let, _ => l(let.name)(result))
}

object EnabledLogEvaluationContext {
class Lenses[F[_]: Monad, C[_[_]]] {
val types: Lens[EnabledLogEvaluationContext[C, F], Map[String, FINAL]] =
lens[EnabledLogEvaluationContext[C, F]] >> Symbol("ec") >> Symbol("typeDefs")
val lets: Lens[EnabledLogEvaluationContext[C, F], Map[String, LazyVal[F]]] =
lens[EnabledLogEvaluationContext[C, F]] >> Symbol("ec") >> Symbol("letDefs")
val funcs: Lens[EnabledLogEvaluationContext[C, F], Map[FunctionHeader, BaseFunction[C]]] =
lens[EnabledLogEvaluationContext[C, F]] >> Symbol("ec") >> Symbol("functions")
}
}

case class DisabledLogEvaluationContext[C[_[_]], F[_]](ec: EvaluationContext[C, F]) extends LoggedEvaluationContext[C, F] {
override def log(let: LET, result: LetExecResult[F]): Unit = ()
}
Expand Down

0 comments on commit ac87d73

Please sign in to comment.