Switch Abacus to returning an EvaluationResult with the context.

This commit is contained in:
Danila Fedorin 2017-09-06 22:22:15 -07:00
parent 782669a32b
commit ef1890f24d
4 changed files with 29 additions and 4 deletions

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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));
}

View File

@ -20,6 +20,7 @@ import org.nwapw.abacus.plugin.ClassFinder;
import org.nwapw.abacus.plugin.PluginListener;
import org.nwapw.abacus.plugin.PluginManager;
import org.nwapw.abacus.plugin.StandardPlugin;
import org.nwapw.abacus.tree.EvaluationResult;
import org.nwapw.abacus.tree.TreeNode;
import java.io.File;
@ -144,12 +145,14 @@ public class AbacusController implements PluginListener {
if (constructedTree == null) {
return ERR_SYNTAX;
}
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
EvaluationResult result = abacus.evaluateTree(constructedTree);
NumberInterface evaluatedNumber = result.getValue();
if (evaluatedNumber == null) {
return ERR_EVAL;
}
String resultingString = evaluatedNumber.toString();
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), resultingString));
abacus.applyToContext(result.getResultingContext());
return resultingString;
} catch (ComputationInterruptedException exception) {
return ERR_STOP;