Skip to content

Commit

Permalink
Adjust naming
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirlogachev committed Sep 27, 2024
1 parent 6690b38 commit b46f309
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ContractCompiler(version: StdLibVersion) extends ExpressionCompiler(versio
.getOrElse(List.empty)
unionInCallableErrs <- checkCallableUnions(af, annotationsWithErr._1.toList.flatten)
compiledBody <- local {
modify[Id, CompilerContext, CompilationError](cc => cc.copy(varDefs = cc.varDefs ++ annotationBindings)).flatMap(_ =>
modify[Id, CompilerContext, CompilationError](ctx => ctx.copy(varDefs = ctx.varDefs ++ annotationBindings)).flatMap(_ =>
compileFunc(af.f.position, af.f, saveExprContext, annotationBindings.map(_._1), allowIllFormedStrings)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class ExpressionCompiler(val version: StdLibVersion) {
.handleError()
compiledFuncBody <- local {
val newArgs: VariableTypes = argTypesWithErr._1.getOrElse(List.empty).toMap
modify[Id, CompilerContext, CompilationError](cc => cc.copy(varDefs = cc.varDefs ++ newArgs))
modify[Id, CompilerContext, CompilationError](ctx1 => ctx1.copy(varDefs = ctx1.varDefs ++ newArgs))
.flatMap(_ => compileExprWithCtx(func.expr, saveExprContext, allowIllFormedStrings))
}

Expand All @@ -368,10 +368,10 @@ class ExpressionCompiler(val version: StdLibVersion) {
}

protected def updateCtx(letName: String, letType: Types.FINAL, p: Pos): CompileM[Unit] =
modify[Id, CompilerContext, CompilationError](cc => cc.copy(varDefs = cc.varDefs + (letName -> VariableInfo(p, letType))))
modify[Id, CompilerContext, CompilationError](ctx => ctx.copy(varDefs = ctx.varDefs + (letName -> VariableInfo(p, letType))))

protected def updateCtx(funcName: String, typeSig: FunctionTypeSignature, p: Pos): CompileM[Unit] =
modify[Id, CompilerContext, CompilationError](cc => cc.copy(functionDefs = cc.functionDefs + (funcName -> FunctionInfo(p, List(typeSig)))))
modify[Id, CompilerContext, CompilationError](ctx => ctx.copy(functionDefs = ctx.functionDefs + (funcName -> FunctionInfo(p, List(typeSig)))))

private def compileLetBlock(
p: Pos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ object ScriptEstimatorV2 extends ScriptEstimator {
local {
val letResult = (false, evalExpr(let.value))
for {
_ <- update(ec => ec.copy(letDefs = ec.letDefs.updated(let.name, letResult)))
_ <- update(ctx => ctx.copy(letDefs = ctx.letDefs.updated(let.name, letResult)))
r <- evalExpr(inner)
} yield r + 5
}
Expand All @@ -60,15 +60,15 @@ object ScriptEstimatorV2 extends ScriptEstimator {
local {
for {
_ <- checkFuncCtx(func)
_ <- update(ec => ec.copy(userFuncs = ec.userFuncs + (FunctionHeader.User(func.name) -> func)))
_ <- update(ctx => ctx.copy(userFuncs = ctx.userFuncs + (FunctionHeader.User(func.name) -> func)))
r <- evalExpr(inner)
} yield r + 5
}

private def checkFuncCtx(func: FUNC): EvalM[Unit] =
local {
for {
_ <- update(ec => ec.copy(letDefs = ec.letDefs ++ func.args.map((_, (true, const(0)))).toMap))
_ <- update(ctx => ctx.copy(letDefs = ctx.letDefs ++ func.args.map((_, (true, const(0)))).toMap))
_ <- evalExpr(func.body)
} yield ()
}
Expand All @@ -84,7 +84,7 @@ object ScriptEstimatorV2 extends ScriptEstimator {
} yield r + 2

private def setRefEvaluated(key: String, lzy: EvalM[Long]): EvalM[Long] =
update(ec => ec.copy(letDefs = ec.letDefs.updated(key, (true, lzy))))
update(ctx => ctx.copy(letDefs = ctx.letDefs.updated(key, (true, lzy))))
.flatMap(_ => lzy)

private def evalGetter(expr: EXPR): EvalM[Long] =
Expand All @@ -104,23 +104,23 @@ object ScriptEstimatorV2 extends ScriptEstimator {
for {
argsComplexity <- evalFuncArgs(args)
ctx <- get[Id, EstimatorContext, EstimationError]
_ <- update(ec => ec.copy(letDefs = ec.letDefs ++ ctx.overlappedRefs))
_ <- update(ctx1 => ctx1.copy(letDefs = ctx1.letDefs ++ ctx.overlappedRefs))
overlapped = func.args.flatMap(arg => ctx.letDefs.get(arg).map((arg, _))).toMap
ctxArgs = func.args.map((_, (false, const(1)))).toMap
_ <- update(ec =>
ec.copy(
letDefs = ec.letDefs ++ ctxArgs,
overlappedRefs = ec.overlappedRefs ++ overlapped
_ <- update(ctx1 =>
ctx1.copy(
letDefs = ctx1.letDefs ++ ctxArgs,
overlappedRefs = ctx1.overlappedRefs ++ overlapped
)
)

bodyComplexity <- evalExpr(func.body).map(_ + func.args.size * 5)
evaluatedCtx <- get[Id, EstimatorContext, EstimationError]
overlappedChanges = overlapped.map { case ref @ (name, _) => evaluatedCtx.letDefs.get(name).map((name, _)).getOrElse(ref) }
_ <- update(ec =>
ec.copy(
letDefs = ec.letDefs -- ctxArgs.keys ++ overlapped,
overlappedRefs = ec.overlappedRefs ++ overlappedChanges
_ <- update(ctx1 =>
ctx1.copy(
letDefs = ctx1.letDefs -- ctxArgs.keys ++ overlapped,
overlappedRefs = ctx1.overlappedRefs ++ overlappedChanges
)
)
} yield bodyComplexity + argsComplexity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:
letCosts <- usedRefs.toSeq.traverse { ref =>
local {
for {
_ <- update(ec => ec.copy(funcs = startCtx.funcs))
_ <- update(ctx1 => ctx1.copy(funcs = startCtx.funcs))
cost <- ctx.globalLetEvals.getOrElse(ref, zero)
} yield cost
}
Expand Down Expand Up @@ -138,9 +138,9 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:
} yield ()

private def handleUsedRefs(name: String, cost: Long, startCtx: EstimatorContext, refsUsedInBody: Set[String]): EvalM[Unit] =
update(ec =>
ec.copy(
funcs = ec.funcs + (User(name) -> (Coeval.now(cost), refsUsedInBody)),
update(ctx =>
ctx.copy(
funcs = ctx.funcs + (User(name) -> (Coeval.now(cost), refsUsedInBody)),
usedRefs = startCtx.usedRefs
)
)
Expand All @@ -158,7 +158,7 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:
if (activeFuncArgs.contains(key) && letFixes)
const(overheadCost)
else
update(ec => ec.copy(usedRefs = ec.usedRefs + key)).map(_ => overheadCost)
update(ctx => ctx.copy(usedRefs = ctx.usedRefs + key)).map(_ => overheadCost)

private def evalGetter(expr: EXPR, activeFuncArgs: Set[String]): EvalM[Long] =
evalExpr(expr, activeFuncArgs).flatMap(sum(_, overheadCost))
Expand All @@ -180,10 +180,10 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:
} yield result

private def setFuncToCtx(header: FunctionHeader, bodyCost: Coeval[Long], bodyUsedRefs: Set[EstimationError]): EvalM[Unit] =
update(ec =>
ec.copy(
funcs = ec.funcs + (header -> (bodyCost, Set())),
usedRefs = ec.usedRefs ++ bodyUsedRefs
update(ctx =>
ctx.copy(
funcs = ctx.funcs + (header -> (bodyCost, Set())),
usedRefs = ctx.usedRefs ++ bodyUsedRefs
)
)

Expand All @@ -207,9 +207,9 @@ case class ScriptEstimatorV3(fixOverflow: Boolean, overhead: Boolean, letFixes:
): EvalM[Long] =
for {
startCtx <- get[Id, EstimatorContext, EstimationError]
_ <- ctxFuncsOpt.fold(doNothing.void)(ctxFuncs => update(ec => ec.copy(funcs = ctxFuncs)))
_ <- ctxFuncsOpt.fold(doNothing.void)(ctxFuncs => update(ctx => ctx.copy(funcs = ctxFuncs)))
cost <- evalExpr(expr, activeFuncArgs)
_ <- update(ec => ec.copy(funcs = startCtx.funcs))
_ <- update(ctx => ctx.copy(funcs = startCtx.funcs))
} yield cost

private def withUsedRefs[A](eval: EvalM[A]): EvalM[(A, Set[String])] =
Expand Down

0 comments on commit b46f309

Please sign in to comment.