mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 15:30:09 -08:00
Switch Abacus to returning an EvaluationResult with the context.
This commit is contained in:
parent
782669a32b
commit
ef1890f24d
|
@ -11,6 +11,7 @@ import org.nwapw.abacus.parsing.TreeBuilder
|
||||||
import org.nwapw.abacus.plugin.NumberImplementation
|
import org.nwapw.abacus.plugin.NumberImplementation
|
||||||
import org.nwapw.abacus.plugin.PluginManager
|
import org.nwapw.abacus.plugin.PluginManager
|
||||||
import org.nwapw.abacus.plugin.StandardPlugin
|
import org.nwapw.abacus.plugin.StandardPlugin
|
||||||
|
import org.nwapw.abacus.tree.EvaluationResult
|
||||||
import org.nwapw.abacus.tree.NumberReducer
|
import org.nwapw.abacus.tree.NumberReducer
|
||||||
import org.nwapw.abacus.tree.TreeNode
|
import org.nwapw.abacus.tree.TreeNode
|
||||||
import org.nwapw.abacus.variables.VariableDatabase
|
import org.nwapw.abacus.variables.VariableDatabase
|
||||||
|
@ -63,6 +64,9 @@ class Abacus(val configuration: Configuration) {
|
||||||
pluginManager.addListener(promotionManager)
|
pluginManager.addListener(promotionManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reloads the Abacus core.
|
||||||
|
*/
|
||||||
fun reload(){
|
fun reload(){
|
||||||
pluginManager.reload()
|
pluginManager.reload()
|
||||||
with(mutableContext) {
|
with(mutableContext) {
|
||||||
|
@ -71,6 +75,14 @@ class Abacus(val configuration: Configuration) {
|
||||||
clearDefinitions()
|
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
|
* Parses a string into a tree structure using the main
|
||||||
* tree builder.
|
* tree builder.
|
||||||
|
@ -86,7 +98,10 @@ class Abacus(val configuration: Configuration) {
|
||||||
* @param tree the tree to reduce, must not be null.
|
* @param tree the tree to reduce, must not be null.
|
||||||
* @return the resulting number, or null of the reduction failed.
|
* @return the resulting number, or null of the reduction failed.
|
||||||
*/
|
*/
|
||||||
fun evaluateTree(tree: TreeNode): NumberInterface? =
|
fun evaluateTree(tree: TreeNode): EvaluationResult {
|
||||||
tree.reduce(NumberReducer(this, context))
|
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);
|
TreeNode parsedTree = abacus.parseString(input);
|
||||||
Assert.assertNotNull(parsedTree);
|
Assert.assertNotNull(parsedTree);
|
||||||
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
Assert.assertEquals(parsedTree.toString(), parseOutput);
|
||||||
NumberInterface result = abacus.evaluateTree(parsedTree);
|
NumberInterface result = abacus.evaluateTree(parsedTree).getValue();
|
||||||
Assert.assertNotNull(result);
|
Assert.assertNotNull(result);
|
||||||
Assert.assertTrue(result.toString().startsWith(output));
|
Assert.assertTrue(result.toString().startsWith(output));
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ import org.nwapw.abacus.plugin.ClassFinder;
|
||||||
import org.nwapw.abacus.plugin.PluginListener;
|
import org.nwapw.abacus.plugin.PluginListener;
|
||||||
import org.nwapw.abacus.plugin.PluginManager;
|
import org.nwapw.abacus.plugin.PluginManager;
|
||||||
import org.nwapw.abacus.plugin.StandardPlugin;
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||||
|
import org.nwapw.abacus.tree.EvaluationResult;
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -144,12 +145,14 @@ public class AbacusController implements PluginListener {
|
||||||
if (constructedTree == null) {
|
if (constructedTree == null) {
|
||||||
return ERR_SYNTAX;
|
return ERR_SYNTAX;
|
||||||
}
|
}
|
||||||
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
EvaluationResult result = abacus.evaluateTree(constructedTree);
|
||||||
|
NumberInterface evaluatedNumber = result.getValue();
|
||||||
if (evaluatedNumber == null) {
|
if (evaluatedNumber == null) {
|
||||||
return ERR_EVAL;
|
return ERR_EVAL;
|
||||||
}
|
}
|
||||||
String resultingString = evaluatedNumber.toString();
|
String resultingString = evaluatedNumber.toString();
|
||||||
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), resultingString));
|
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), resultingString));
|
||||||
|
abacus.applyToContext(result.getResultingContext());
|
||||||
return resultingString;
|
return resultingString;
|
||||||
} catch (ComputationInterruptedException exception) {
|
} catch (ComputationInterruptedException exception) {
|
||||||
return ERR_STOP;
|
return ERR_STOP;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user