mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-10 01:05:20 +00:00
Switch Abacus to returning an EvaluationResult with the context.
This commit is contained in:
@@ -11,6 +11,7 @@ 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
|
||||
@@ -63,6 +64,9 @@ class Abacus(val configuration: Configuration) {
|
||||
pluginManager.addListener(promotionManager)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the Abacus core.
|
||||
*/
|
||||
fun reload(){
|
||||
pluginManager.reload()
|
||||
with(mutableContext) {
|
||||
@@ -71,6 +75,14 @@ class Abacus(val configuration: Configuration) {
|
||||
clearDefinitions()
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Merges the current context with the provided one, updating
|
||||
* variables and the like.
|
||||
* @param context the context to apply.
|
||||
*/
|
||||
fun applyToContext(context: ReductionContext){
|
||||
mutableContext.apply(context)
|
||||
}
|
||||
/**
|
||||
* Parses a string into a tree structure using the main
|
||||
* tree builder.
|
||||
@@ -86,7 +98,10 @@ class Abacus(val configuration: Configuration) {
|
||||
* @param tree the tree to reduce, must not be null.
|
||||
* @return the resulting number, or null of the reduction failed.
|
||||
*/
|
||||
fun evaluateTree(tree: TreeNode): NumberInterface? =
|
||||
tree.reduce(NumberReducer(this, context))
|
||||
fun evaluateTree(tree: TreeNode): EvaluationResult {
|
||||
val newReducer = NumberReducer(this, context)
|
||||
val evaluationValue = tree.reduce(newReducer)
|
||||
return EvaluationResult(evaluationValue, newReducer.context)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.nwapw.abacus.tree
|
||||
|
||||
import org.nwapw.abacus.context.MutableReductionContext
|
||||
import org.nwapw.abacus.number.NumberInterface
|
||||
import org.nwapw.abacus.plugin.NumberImplementation
|
||||
|
||||
data class EvaluationResult(val value: NumberInterface?, val resultingContext: MutableReductionContext)
|
||||
@@ -24,7 +24,7 @@ public class CalculationTests {
|
||||
TreeNode parsedTree = abacus.parseString(input);
|
||||
Assert.assertNotNull(parsedTree);
|
||||
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
||||
NumberInterface result = abacus.evaluateTree(parsedTree);
|
||||
NumberInterface result = abacus.evaluateTree(parsedTree).getValue();
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertTrue(result.toString().startsWith(output));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user