From 059226a4d489fab7e08aedc260b439afb46f70de Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 6 Sep 2017 22:54:21 -0700 Subject: [PATCH] Rename the context class. --- .../nwapw/abacus/plugin/StandardPlugin.java | 123 +++++++++--------- .../main/kotlin/org/nwapw/abacus/Abacus.kt | 29 +++-- .../abacus/context/ChainAccumulateDelegate.kt | 6 +- .../abacus/context/ChainSearchDelegate.kt | 8 +- ...ductionContext.kt => EvaluationContext.kt} | 8 +- ...Context.kt => MutableEvaluationContext.kt} | 10 +- .../abacus/function/applicable/Applicable.kt | 8 +- .../org/nwapw/abacus/tree/EvaluationResult.kt | 5 +- .../org/nwapw/abacus/tree/NumberReducer.kt | 6 +- .../nwapw/abacus/tests/TokenizerTests.java | 16 +-- 10 files changed, 110 insertions(+), 109 deletions(-) rename core/src/main/kotlin/org/nwapw/abacus/context/{ReductionContext.kt => EvaluationContext.kt} (88%) rename core/src/main/kotlin/org/nwapw/abacus/context/{MutableReductionContext.kt => MutableEvaluationContext.kt} (85%) diff --git a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index 5e7069e..e144bbc 100755 --- a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -1,11 +1,10 @@ package org.nwapw.abacus.plugin; -import org.nwapw.abacus.context.MutableReductionContext; +import org.nwapw.abacus.context.MutableEvaluationContext; import org.nwapw.abacus.function.*; import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.PreciseNumber; -import org.nwapw.abacus.tree.Reducer; import org.nwapw.abacus.tree.TreeNode; import org.nwapw.abacus.tree.VariableNode; @@ -23,12 +22,12 @@ public class StandardPlugin extends Plugin { */ public final TreeValueOperator opSet = new TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { @Override - public boolean matchesParams(MutableReductionContext context, TreeNode[] params) { + public boolean matchesParams(MutableEvaluationContext context, TreeNode[] params) { return params.length == 2 && params[0] instanceof VariableNode; } @Override - public NumberInterface applyInternal(MutableReductionContext context, TreeNode[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, TreeNode[] params) { String assignTo = ((VariableNode) params[0]).getVariable(); NumberInterface value = params[1].reduce(context.getReducer()); if(value == null) throw new EvaluationException(); @@ -41,12 +40,12 @@ public class StandardPlugin extends Plugin { */ public final TreeValueOperator opDefine = new TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { @Override - public boolean matchesParams(MutableReductionContext context, TreeNode[] params) { + public boolean matchesParams(MutableEvaluationContext context, TreeNode[] params) { return params.length == 2 && params[0] instanceof VariableNode; } @Override - public NumberInterface applyInternal(MutableReductionContext context, TreeNode[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, TreeNode[] params) { String assignTo = ((VariableNode) params[0]).getVariable(); context.setDefinition(assignTo, params[1]); NumberInterface value = params[1].reduce(context.getReducer()); @@ -59,12 +58,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_ADD = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 2; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return params[0].add(params[1]); } }; @@ -73,12 +72,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_SUBTRACT = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 2; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return params[0].subtract(params[1]); } @@ -88,12 +87,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_NEGATE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return params[0].negate(); } }; @@ -102,12 +101,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_MULTIPLY = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 2; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return params[0].multiply(params[1]); } }; @@ -136,7 +135,7 @@ public class StandardPlugin extends Plugin { @Override public NumberInterface instanceForPi() { - MutableReductionContext dummyContext = new MutableReductionContext(null, this, null); + MutableEvaluationContext dummyContext = new MutableEvaluationContext(null, this, null); NumberInterface C = FUNCTION_SQRT.apply(dummyContext, new PreciseNumber("10005")).multiply(new PreciseNumber("426880")); NumberInterface M = PreciseNumber.ONE; NumberInterface L = new PreciseNumber("13591409"); @@ -167,12 +166,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_DIVIDE = new NumberOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 1) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 2 && params[1].compareTo(context.getInheritedNumberImplementation().instanceForString(Integer.toString(0))) != 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return params[0].divide(params[1]); } }; @@ -182,14 +181,14 @@ public class StandardPlugin extends Plugin { public static final NumberOperator OP_FACTORIAL = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0) { //private HashMap, ArrayList> storedList = new HashMap, ArrayList>(); @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1 && params[0].fractionalPart().compareTo(context.getInheritedNumberImplementation().instanceForString("0")) == 0 && params[0].signum() >= 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberImplementation implementation = context.getInheritedNumberImplementation(); if (params[0].signum() == 0) { return implementation.instanceForString("1"); @@ -214,13 +213,13 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_NPR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 2 && params[0].fractionalPart().signum() == 0 && params[1].fractionalPart().signum() == 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberImplementation implementation = context.getInheritedNumberImplementation(); if (params[0].compareTo(params[1]) < 0 || params[0].signum() < 0 || @@ -245,13 +244,13 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_NCR = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 2 && params[0].fractionalPart().signum() == 0 && params[1].fractionalPart().signum() == 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return OP_NPR.apply(context, params).divide(OP_FACTORIAL.apply(context, params[1])); } }; @@ -260,12 +259,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_ABS = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return params[0].multiply(context.getInheritedNumberImplementation().instanceForString(Integer.toString(params[0].signum()))); } }; @@ -274,12 +273,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_LN = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1 && params[0].compareTo(context.getInheritedNumberImplementation().instanceForString("0")) > 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberImplementation implementation = context.getInheritedNumberImplementation(); NumberInterface param = params[0]; NumberInterface one = implementation.instanceForString("1"); @@ -310,7 +309,7 @@ public class StandardPlugin extends Plugin { * @param x value at which the series is evaluated. 0 < x < 2. (x=2 is convergent but impractical.) * @return the partial sum. */ - private NumberInterface getLogPartialSum(MutableReductionContext context, NumberInterface x) { + private NumberInterface getLogPartialSum(MutableEvaluationContext context, NumberInterface x) { NumberImplementation implementation = context.getInheritedNumberImplementation(); NumberInterface maxError = x.getMaxError(); x = x.subtract(implementation.instanceForString("1")); //Terms used are for log(x+1). @@ -355,12 +354,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_RAND_INT = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return context.getInheritedNumberImplementation().instanceForString(Long.toString(Math.round(Math.random() * params[0].floor().intValue()))); } }; @@ -369,7 +368,7 @@ public class StandardPlugin extends Plugin { */ public static final NumberOperator OP_CARET = new NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { NumberInterface zero = context.getInheritedNumberImplementation().instanceForString("0"); return params.length == 2 && !(params[0].compareTo(zero) == 0 @@ -378,7 +377,7 @@ public class StandardPlugin extends Plugin { } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberImplementation implementation = context.getInheritedNumberImplementation(); NumberInterface zero = implementation.instanceForString("0"); if (params[0].compareTo(zero) == 0) @@ -400,12 +399,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_SQRT = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return OP_CARET.apply(context, params[0], context.getInheritedNumberImplementation().instanceForString(".5")); } }; @@ -415,12 +414,12 @@ public class StandardPlugin extends Plugin { */ public static final NumberFunction FUNCTION_EXP = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberImplementation implementation = context.getInheritedNumberImplementation(); NumberInterface maxError = params[0].getMaxError(); int n = 0; @@ -453,12 +452,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionSin = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberImplementation implementation = context.getInheritedNumberImplementation(); NumberInterface pi = piFor(params[0].getClass()); NumberInterface twoPi = pi.multiply(implementation.instanceForString("2")); @@ -478,12 +477,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionCos = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return functionSin.apply(context, piFor(params[0].getClass()).divide(context.getInheritedNumberImplementation().instanceForString("2")) .subtract(params[0])); } @@ -493,12 +492,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionTan = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return functionSin.apply(context, params[0]).divide(functionCos.apply(context, params[0])); } }; @@ -507,12 +506,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionSec = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return context.getInheritedNumberImplementation().instanceForString("1").divide(functionCos.apply(context, params[0])); } }; @@ -521,12 +520,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionCsc = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return context.getInheritedNumberImplementation().instanceForString("1").divide(functionSin.apply(context, params[0])); } }; @@ -535,12 +534,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionCot = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return functionCos.apply(context, params[0]).divide(functionSin.apply(context, params[0])); } }; @@ -550,13 +549,13 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArcsin = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1 && FUNCTION_ABS.apply(context, params[0]).compareTo(context.getInheritedNumberImplementation().instanceForString("1")) <= 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberImplementation implementation = context.getInheritedNumberImplementation(); if (FUNCTION_ABS.apply(context, params[0]).compareTo(implementation.instanceForString(".8")) >= 0) { NumberInterface[] newParams = {FUNCTION_SQRT.apply(context, implementation.instanceForString("1").subtract(params[0].multiply(params[0])))}; @@ -584,12 +583,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArccos = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1 && FUNCTION_ABS.apply(context, params[0]).compareTo(context.getInheritedNumberImplementation().instanceForString("1")) <= 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return piFor(params[0].getClass()).divide(context.getInheritedNumberImplementation().instanceForString("2")) .subtract(functionArcsin.apply(context, params)); } @@ -600,12 +599,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArccsc = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1 && FUNCTION_ABS.apply(context, params[0]).compareTo(context.getInheritedNumberImplementation().instanceForString("1")) >= 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberInterface[] reciprocalParamArr = {context.getInheritedNumberImplementation().instanceForString("1").divide(params[0])}; return functionArcsin.apply(context, reciprocalParamArr); } @@ -616,12 +615,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArcsec = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1 && FUNCTION_ABS.apply(context, params[0]).compareTo(context.getInheritedNumberImplementation().instanceForString("1")) >= 0; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberInterface[] reciprocalParamArr = {context.getInheritedNumberImplementation().instanceForString("1").divide(params[0])}; return functionArccos.apply(context, reciprocalParamArr); } @@ -632,12 +631,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArctan = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { NumberImplementation implementation = context.getInheritedNumberImplementation(); if (params[0].signum() == -1) { NumberInterface[] negatedParams = {params[0].negate()}; @@ -674,12 +673,12 @@ public class StandardPlugin extends Plugin { */ public final NumberFunction functionArccot = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 1; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return piFor(params[0].getClass()).divide(context.getInheritedNumberImplementation().instanceForString("2")) .subtract(functionArctan.apply(context, params)); } @@ -719,7 +718,7 @@ public class StandardPlugin extends Plugin { * @param x where the series is evaluated. * @return the value of the series */ - private static NumberInterface sinTaylor(MutableReductionContext context, NumberInterface x) { + private static NumberInterface sinTaylor(MutableEvaluationContext context, NumberInterface x) { NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x; NumberInterface maxError = x.getMaxError(); int n = 1; @@ -738,7 +737,7 @@ public class StandardPlugin extends Plugin { * @param phi an angle (in radians). * @return theta in [0, 2pi) that differs from phi by a multiple of 2pi. */ - private static NumberInterface getSmallAngle(MutableReductionContext context, NumberInterface phi, NumberInterface pi) { + private static NumberInterface getSmallAngle(MutableEvaluationContext context, NumberInterface phi, NumberInterface pi) { NumberInterface twoPi = pi.multiply(context.getInheritedNumberImplementation().instanceForString("2")); NumberInterface theta = FUNCTION_ABS.apply(context, phi).subtract(twoPi .multiply(FUNCTION_ABS.apply(context, phi).divide(twoPi).floor())); //Now theta is in [0, 2pi). diff --git a/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt b/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt index c0dfe12..0363646 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt @@ -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) diff --git a/core/src/main/kotlin/org/nwapw/abacus/context/ChainAccumulateDelegate.kt b/core/src/main/kotlin/org/nwapw/abacus/context/ChainAccumulateDelegate.kt index e2f1017..bc03d44 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/context/ChainAccumulateDelegate.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/context/ChainAccumulateDelegate.kt @@ -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(private val valueGetter: ReductionContext.() -> Collection) { +class ChainAccumulateDelegate(private val valueGetter: EvaluationContext.() -> Collection) { operator fun getValue(selfRef: Any, property: KProperty<*>): Set { val set = mutableSetOf() - 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 diff --git a/core/src/main/kotlin/org/nwapw/abacus/context/ChainSearchDelegate.kt b/core/src/main/kotlin/org/nwapw/abacus/context/ChainSearchDelegate.kt index f69725f..b1245b9 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/context/ChainSearchDelegate.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/context/ChainSearchDelegate.kt @@ -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(private val valueGetter: ReductionContext.() -> V?) { +class ChainSearchDelegate(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 diff --git a/core/src/main/kotlin/org/nwapw/abacus/context/ReductionContext.kt b/core/src/main/kotlin/org/nwapw/abacus/context/EvaluationContext.kt similarity index 88% rename from core/src/main/kotlin/org/nwapw/abacus/context/ReductionContext.kt rename to core/src/main/kotlin/org/nwapw/abacus/context/EvaluationContext.kt index 957e067..d4fdadd 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/context/ReductionContext.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/context/EvaluationContext.kt @@ -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? = null) { +open class EvaluationContext(val parent: EvaluationContext? = null, + open val numberImplementation: NumberImplementation? = null, + open val reducer: Reducer? = 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. diff --git a/core/src/main/kotlin/org/nwapw/abacus/context/MutableReductionContext.kt b/core/src/main/kotlin/org/nwapw/abacus/context/MutableEvaluationContext.kt similarity index 85% rename from core/src/main/kotlin/org/nwapw/abacus/context/MutableReductionContext.kt rename to core/src/main/kotlin/org/nwapw/abacus/context/MutableEvaluationContext.kt index fa96c55..684b69c 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/context/MutableReductionContext.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/context/MutableEvaluationContext.kt @@ -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? = null) : - ReductionContext(parent, numberImplementation, reducer) { +class MutableEvaluationContext(parent: EvaluationContext? = null, + numberImplementation: NumberImplementation? = null, + reducer: Reducer? = null) : + EvaluationContext(parent, numberImplementation, reducer) { override var numberImplementation: NumberImplementation? = super.numberImplementation override var reducer: Reducer? = 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) { diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt index 3aff900..8d824a8 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/function/applicable/Applicable.kt @@ -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 { * @param params the parameter array to verify for compatibility. * @return whether the array can be used with applyInternal. */ - fun matchesParams(context: MutableReductionContext, params: Array): Boolean + fun matchesParams(context: MutableEvaluationContext, params: Array): Boolean /** * Applies the applicable object to the given parameters, @@ -26,7 +26,7 @@ interface Applicable { * @param params the parameters to apply to. * @return the result of the application. */ - fun applyInternal(context: MutableReductionContext, params: Array): O + fun applyInternal(context: MutableEvaluationContext, params: Array): O /** * If the parameters can be used with this applicable, returns @@ -35,7 +35,7 @@ interface Applicable { * @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) } diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/EvaluationResult.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/EvaluationResult.kt index 89bde63..e94c9d6 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/EvaluationResult.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/EvaluationResult.kt @@ -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) \ No newline at end of file +data class EvaluationResult(val value: NumberInterface?, val resultingContext: MutableEvaluationContext) \ No newline at end of file diff --git a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt index bc62039..c1a1da8 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/tree/NumberReducer.kt @@ -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 { +class NumberReducer(val abacus: Abacus, context: EvaluationContext) : Reducer { val context = context.mutableSubInstance() diff --git a/core/src/test/java/org/nwapw/abacus/tests/TokenizerTests.java b/core/src/test/java/org/nwapw/abacus/tests/TokenizerTests.java index 90272c4..d22f94d 100644 --- a/core/src/test/java/org/nwapw/abacus/tests/TokenizerTests.java +++ b/core/src/test/java/org/nwapw/abacus/tests/TokenizerTests.java @@ -1,17 +1,15 @@ package org.nwapw.abacus.tests; -import org.jetbrains.annotations.NotNull; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.nwapw.abacus.Abacus; import org.nwapw.abacus.config.Configuration; -import org.nwapw.abacus.context.MutableReductionContext; +import org.nwapw.abacus.context.MutableEvaluationContext; import org.nwapw.abacus.function.*; import org.nwapw.abacus.lexing.pattern.Match; import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.parsing.LexerTokenizer; -import org.nwapw.abacus.plugin.NumberImplementation; import org.nwapw.abacus.plugin.Plugin; import org.nwapw.abacus.tree.TokenType; @@ -23,12 +21,12 @@ public class TokenizerTests { private static LexerTokenizer lexerTokenizer = new LexerTokenizer(); private static NumberFunction subtractFunction = new NumberFunction() { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return params.length == 2; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return params[0].subtract(params[1]); } }; @@ -39,12 +37,12 @@ public class TokenizerTests { 0) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return true; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return subtractFunction.apply(context, params); } }); @@ -52,12 +50,12 @@ public class TokenizerTests { 0) { @Override - public boolean matchesParams(MutableReductionContext context, NumberInterface[] params) { + public boolean matchesParams(MutableEvaluationContext context, NumberInterface[] params) { return true; } @Override - public NumberInterface applyInternal(MutableReductionContext context, NumberInterface[] params) { + public NumberInterface applyInternal(MutableEvaluationContext context, NumberInterface[] params) { return subtractFunction.apply(context, params); } });