From ef1890f24da6ebd80f64d8b1d6c5249d0c25684b Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 6 Sep 2017 22:22:15 -0700 Subject: [PATCH] Switch Abacus to returning an EvaluationResult with the context. --- .../main/kotlin/org/nwapw/abacus/Abacus.kt | 19 +++++++++++++++++-- .../org/nwapw/abacus/tree/EvaluationResult.kt | 7 +++++++ .../nwapw/abacus/tests/CalculationTests.java | 2 +- .../org/nwapw/abacus/fx/AbacusController.java | 5 ++++- 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 core/src/main/kotlin/org/nwapw/abacus/tree/EvaluationResult.kt diff --git a/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt b/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt index b2fbfa5..c0dfe12 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt @@ -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) + } } \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/EvaluationResult.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/EvaluationResult.kt new file mode 100644 index 0000000..89bde63 --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/EvaluationResult.kt @@ -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) \ No newline at end of file diff --git a/core/src/test/java/org/nwapw/abacus/tests/CalculationTests.java b/core/src/test/java/org/nwapw/abacus/tests/CalculationTests.java index 1818090..0a00289 100755 --- a/core/src/test/java/org/nwapw/abacus/tests/CalculationTests.java +++ b/core/src/test/java/org/nwapw/abacus/tests/CalculationTests.java @@ -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)); } diff --git a/fx/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/fx/src/main/java/org/nwapw/abacus/fx/AbacusController.java index 74527a1..435da06 100644 --- a/fx/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/fx/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -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;