1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-31 19:15:20 +00:00

Rename the context class.

This commit is contained in:
2017-09-06 22:54:21 -07:00
parent ef1890f24d
commit 059226a4d4
10 changed files with 110 additions and 109 deletions

View File

@@ -1,20 +1,17 @@
package org.nwapw.abacus
import org.nwapw.abacus.config.Configuration
import org.nwapw.abacus.context.MutableReductionContext
import org.nwapw.abacus.context.ReductionContext
import org.nwapw.abacus.number.NumberInterface
import org.nwapw.abacus.context.MutableEvaluationContext
import org.nwapw.abacus.context.EvaluationContext
import org.nwapw.abacus.number.PromotionManager
import org.nwapw.abacus.parsing.LexerTokenizer
import org.nwapw.abacus.parsing.ShuntingYardParser
import org.nwapw.abacus.parsing.TreeBuilder
import org.nwapw.abacus.plugin.NumberImplementation
import org.nwapw.abacus.plugin.PluginManager
import org.nwapw.abacus.plugin.StandardPlugin
import org.nwapw.abacus.tree.EvaluationResult
import org.nwapw.abacus.tree.NumberReducer
import org.nwapw.abacus.tree.TreeNode
import org.nwapw.abacus.variables.VariableDatabase
/**
* Core class to handle all mathematics.
@@ -51,11 +48,11 @@ class Abacus(val configuration: Configuration) {
/**
* The hidden, mutable implementation of the context.
*/
private val mutableContext = MutableReductionContext(numberImplementation = StandardPlugin.IMPLEMENTATION_NAIVE)
private val mutableContext = MutableEvaluationContext(numberImplementation = StandardPlugin.IMPLEMENTATION_NAIVE)
/**
* The base context from which calculations are started.
*/
val context: ReductionContext
val context: EvaluationContext
get() = mutableContext
init {
@@ -80,7 +77,7 @@ class Abacus(val configuration: Configuration) {
* variables and the like.
* @param context the context to apply.
*/
fun applyToContext(context: ReductionContext){
fun applyToContext(context: EvaluationContext){
mutableContext.apply(context)
}
/**
@@ -92,13 +89,23 @@ class Abacus(val configuration: Configuration) {
*/
fun parseString(input: String): TreeNode? = treeBuilder.fromString(input)
/**
* Evaluates the given tree using the main
* number reducer.
* Evaluates the given tree.
*
* @param tree the tree to reduce, must not be null.
* @return the resulting number, or null of the reduction failed.
* @return the evaluation result.
*/
fun evaluateTree(tree: TreeNode): EvaluationResult {
return evaluateTreeWithContext(tree, context.mutableSubInstance())
}
/**
* Evaluates the given tree using a different context than
* the default one.
*
* @param tree the tree to reduce, must not be null.
* @param context the context to use for the evaluation.
* @return the evaluation result.
*/
fun evaluateTreeWithContext(tree: TreeNode, context: MutableEvaluationContext): EvaluationResult {
val newReducer = NumberReducer(this, context)
val evaluationValue = tree.reduce(newReducer)
return EvaluationResult(evaluationValue, newReducer.context)

View File

@@ -3,7 +3,7 @@ package org.nwapw.abacus.context
import kotlin.reflect.KProperty
/**
* A delegate to accumulate a collection of elements in a [ReductionContext] hierarchy.
* A delegate to accumulate a collection of elements in a [EvaluationContext] hierarchy.
*
* ChainAccumulateDelegate is similar to the [ChainSearchDelegate], however, it operates only on collections.
* Instead of returning the most recent collection, it merges them into a [Set].
@@ -11,11 +11,11 @@ import kotlin.reflect.KProperty
* @param T the type of element in the collection.
* @property valueGetter the getter used to access the collection from the context.
*/
class ChainAccumulateDelegate<out T>(private val valueGetter: ReductionContext.() -> Collection<T>) {
class ChainAccumulateDelegate<out T>(private val valueGetter: EvaluationContext.() -> Collection<T>) {
operator fun getValue(selfRef: Any, property: KProperty<*>): Set<T> {
val set = mutableSetOf<T>()
var currentRef: ReductionContext = selfRef as? ReductionContext ?: return set
var currentRef: EvaluationContext = selfRef as? EvaluationContext ?: return set
while(true) {
set.addAll(currentRef.valueGetter())
currentRef = currentRef.parent ?: break

View File

@@ -3,9 +3,9 @@ package org.nwapw.abacus.context
import kotlin.reflect.KProperty
/**
* A delegate to search a hierarchy made up of [ReductionContext].
* A delegate to search a hierarchy made up of [EvaluationContext].
*
* ChainSearchDelegate is a variable delegate written specifically for use in [ReductionContext], because
* ChainSearchDelegate is a variable delegate written specifically for use in [EvaluationContext], because
* of its hierarchical structure. Variables not found in the current context are searched
* for in its parent, which continues recursively until the context being examined has no parent.
* This class assists that logic, which is commonly re-used with different variable types, by calling
@@ -14,10 +14,10 @@ import kotlin.reflect.KProperty
* @param V the type of the property to search recursively.
* @property valueGetter the getter lambda to access the value from the context.
*/
class ChainSearchDelegate<out V>(private val valueGetter: ReductionContext.() -> V?) {
class ChainSearchDelegate<out V>(private val valueGetter: EvaluationContext.() -> V?) {
operator fun getValue(selfRef: Any, property: KProperty<*>): V? {
var currentRef = selfRef as? ReductionContext ?: return null
var currentRef = selfRef as? EvaluationContext ?: return null
var returnedValue = currentRef.valueGetter()
while (returnedValue == null) {
currentRef = currentRef.parent ?: break

View File

@@ -15,9 +15,9 @@ import org.nwapw.abacus.tree.TreeNode
* @property numberImplementation the implementation for numbers of this context.
* @property reducer the reducer used by this context.
*/
open class ReductionContext(val parent: ReductionContext? = null,
open val numberImplementation: NumberImplementation? = null,
open val reducer: Reducer<NumberInterface>? = null) {
open class EvaluationContext(val parent: EvaluationContext? = null,
open val numberImplementation: NumberImplementation? = null,
open val reducer: Reducer<NumberInterface>? = null) {
/**
* The map of variables in this context.
@@ -66,7 +66,7 @@ open class ReductionContext(val parent: ReductionContext? = null,
* Create a new child instance of this context that is mutable.
* @return the new child instance.
*/
fun mutableSubInstance(): MutableReductionContext = MutableReductionContext(this)
fun mutableSubInstance(): MutableEvaluationContext = MutableEvaluationContext(this)
/**
* Gets a variable stored in this context.

View File

@@ -11,10 +11,10 @@ import org.nwapw.abacus.tree.TreeNode
* @param numberImplementation the number implementation used in this context.
* @param reducer the reducer used in this context
*/
class MutableReductionContext(parent: ReductionContext? = null,
numberImplementation: NumberImplementation? = null,
reducer: Reducer<NumberInterface>? = null) :
ReductionContext(parent, numberImplementation, reducer) {
class MutableEvaluationContext(parent: EvaluationContext? = null,
numberImplementation: NumberImplementation? = null,
reducer: Reducer<NumberInterface>? = null) :
EvaluationContext(parent, numberImplementation, reducer) {
override var numberImplementation: NumberImplementation? = super.numberImplementation
override var reducer: Reducer<NumberInterface>? = super.reducer
@@ -23,7 +23,7 @@ class MutableReductionContext(parent: ReductionContext? = null,
* Writes data stored in the [other] context over data stored in this one.
* @param other the context from which to copy data.
*/
fun apply(other: ReductionContext) {
fun apply(other: EvaluationContext) {
if(other.numberImplementation != null) numberImplementation = other.numberImplementation
if(other.reducer != null) reducer = other.reducer
for(name in other.variables) {

View File

@@ -1,6 +1,6 @@
package org.nwapw.abacus.function.applicable
import org.nwapw.abacus.context.MutableReductionContext
import org.nwapw.abacus.context.MutableEvaluationContext
import org.nwapw.abacus.function.DomainException
/**
@@ -18,7 +18,7 @@ interface Applicable<in T : Any, out O : Any> {
* @param params the parameter array to verify for compatibility.
* @return whether the array can be used with applyInternal.
*/
fun matchesParams(context: MutableReductionContext, params: Array<out T>): Boolean
fun matchesParams(context: MutableEvaluationContext, params: Array<out T>): Boolean
/**
* Applies the applicable object to the given parameters,
@@ -26,7 +26,7 @@ interface Applicable<in T : Any, out O : Any> {
* @param params the parameters to apply to.
* @return the result of the application.
*/
fun applyInternal(context: MutableReductionContext, params: Array<out T>): O
fun applyInternal(context: MutableEvaluationContext, params: Array<out T>): O
/**
* If the parameters can be used with this applicable, returns
@@ -35,7 +35,7 @@ interface Applicable<in T : Any, out O : Any> {
* @param params the parameters to apply to.
* @return the result of the operation, or null if parameters do not match.
*/
fun apply(context: MutableReductionContext, vararg params: T): O {
fun apply(context: MutableEvaluationContext, vararg params: T): O {
if (!matchesParams(context, params)) throw DomainException()
return applyInternal(context, params)
}

View File

@@ -1,7 +1,6 @@
package org.nwapw.abacus.tree
import org.nwapw.abacus.context.MutableReductionContext
import org.nwapw.abacus.context.MutableEvaluationContext
import org.nwapw.abacus.number.NumberInterface
import org.nwapw.abacus.plugin.NumberImplementation
data class EvaluationResult(val value: NumberInterface?, val resultingContext: MutableReductionContext)
data class EvaluationResult(val value: NumberInterface?, val resultingContext: MutableEvaluationContext)

View File

@@ -1,12 +1,10 @@
package org.nwapw.abacus.tree
import org.nwapw.abacus.Abacus
import org.nwapw.abacus.context.MutableReductionContext
import org.nwapw.abacus.context.ReductionContext
import org.nwapw.abacus.function.NumberFunction
import org.nwapw.abacus.context.EvaluationContext
import org.nwapw.abacus.number.NumberInterface
class NumberReducer(val abacus: Abacus, context: ReductionContext) : Reducer<NumberInterface> {
class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer<NumberInterface> {
val context = context.mutableSubInstance()