-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhirkevich Alexander Y
authored and
Zhirkevich Alexander Y
committed
Jul 24, 2024
1 parent
4f949bc
commit def299f
Showing
26 changed files
with
838 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 117 additions & 5 deletions
122
...in/io/github/alexzhirkevich/compottie/internal/animation/expressions/EvaluationContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,132 @@ | ||
package io.github.alexzhirkevich.compottie.internal.animation.expressions | ||
|
||
internal interface EvaluationContext { | ||
import io.github.alexzhirkevich.compottie.internal.animation.expressions.operations.condition.OpFunction | ||
|
||
|
||
internal enum class VariableScope { | ||
Global, Block | ||
} | ||
|
||
val variables : MutableMap<String, Any> | ||
internal interface EvaluationContext { | ||
|
||
val randomSource : RandomSource | ||
|
||
fun registerFunction(function: OpFunction) | ||
|
||
fun getFunction(name: String) : OpFunction? | ||
|
||
fun getVariable(name : String) : Any? | ||
|
||
fun setVariable(name: String, value : Any, scope: VariableScope) | ||
|
||
fun withScope(extraVariables : Map<String ,Any>, block : (EvaluationContext) -> Any) : Any | ||
|
||
} | ||
|
||
internal class DefaultEvaluatorContext( | ||
override val variables: MutableMap<String, Any> = mutableMapOf(), | ||
override val randomSource: RandomSource = RandomSource(), | ||
) : EvaluationContext { | ||
|
||
val result: Any? get() = variables["\$bm_rt"] | ||
private val globalVariables: MutableMap<String, Any> = mutableMapOf() | ||
|
||
private val blockVariables: MutableMap<String, Any> = mutableMapOf() | ||
|
||
private val functions: MutableMap<String, OpFunction> = mutableMapOf() | ||
|
||
private val child by lazy { | ||
BlockEvaluatorContext(this) | ||
} | ||
|
||
val result: Any? get() = getVariable("\$bm_rt") | ||
|
||
fun reset() { | ||
globalVariables.clear() | ||
blockVariables.clear() | ||
} | ||
|
||
override fun setVariable(name: String, value: Any, scope: VariableScope) { | ||
val map = when (scope) { | ||
VariableScope.Global -> globalVariables | ||
VariableScope.Block -> blockVariables | ||
} | ||
map[name] = value | ||
} | ||
|
||
override fun registerFunction(function: OpFunction) { | ||
functions[function.name] = function | ||
} | ||
|
||
override fun getFunction(name: String): OpFunction? { | ||
return functions[name] | ||
} | ||
|
||
override fun getVariable(name: String): Any? { | ||
return when { | ||
blockVariables.containsKey(name) -> blockVariables[name] | ||
globalVariables.containsKey(name) -> globalVariables[name] | ||
else -> null | ||
} | ||
} | ||
|
||
override fun withScope( | ||
extraVariables: Map<String, Any>, | ||
block: (EvaluationContext) -> Any | ||
) : Any { | ||
child.reset() | ||
extraVariables.forEach { (n, v) -> | ||
child.setVariable(n, v, VariableScope.Block) | ||
} | ||
return block(child) | ||
} | ||
} | ||
|
||
private class BlockEvaluatorContext( | ||
private val parent : EvaluationContext | ||
) : EvaluationContext { | ||
|
||
private val child by lazy { | ||
BlockEvaluatorContext(this) | ||
} | ||
|
||
private val scopeVariables: MutableMap<String, Any> = mutableMapOf() | ||
private val scopeFunctions: MutableMap<String, OpFunction> = mutableMapOf() | ||
|
||
override val randomSource: RandomSource | ||
get() = parent.randomSource | ||
|
||
override fun registerFunction(function: OpFunction) { | ||
scopeFunctions[function.name] = function | ||
} | ||
|
||
override fun getFunction(name: String): OpFunction? { | ||
return scopeFunctions[name] ?: parent.getFunction(name) | ||
} | ||
|
||
override fun getVariable(name: String): Any? { | ||
return if (scopeVariables.containsKey(name)) { | ||
scopeVariables[name] | ||
} else { | ||
parent.getVariable(name) | ||
} | ||
} | ||
|
||
override fun setVariable(name: String, value: Any, scope: VariableScope) { | ||
when (scope) { | ||
VariableScope.Global -> parent.setVariable(name, value, scope) | ||
VariableScope.Block -> scopeVariables[name] = value | ||
} | ||
} | ||
|
||
override fun withScope(extraVariables: Map<String, Any>, block: (EvaluationContext) -> Any) : Any { | ||
child.reset() | ||
extraVariables.forEach { (n, v) -> | ||
child.setVariable(n, v, VariableScope.Block) | ||
} | ||
return block(child) | ||
} | ||
|
||
fun reset() { | ||
variables.clear() | ||
scopeFunctions.clear() | ||
scopeVariables.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.