Enchanced math parser, again.

This commit is contained in:
Andrea Cavalli 2017-06-12 12:35:10 +02:00
parent 8bc727e823
commit b194f19092
3 changed files with 60 additions and 3 deletions

View File

@ -18,6 +18,7 @@ import org.warp.picalculator.math.parser.features.FeatureSum;
import org.warp.picalculator.math.parser.features.FeatureVariable;
import org.warp.picalculator.math.parser.features.interfaces.Feature;
import org.warp.picalculator.math.parser.steps.JoinNumberAndVariables;
import org.warp.picalculator.math.parser.steps.AddImplicitMultiplications;
import org.warp.picalculator.math.parser.steps.FixMultiplicationsAndDivisions;
import org.warp.picalculator.math.parser.steps.FixSingleFunctionArgs;
import org.warp.picalculator.math.parser.steps.FixSumsAndSubtractions;
@ -71,9 +72,16 @@ public class MathParser {
new FixSingleFunctionArgs(),
new FixMultiplicationsAndDivisions(),
new FixSumsAndSubtractions(),
new AddImplicitMultiplications(context),
};
boolean lastLoopDidSomething;
Function lastElement;
Utils.out.print(Utils.OUTPUTLEVEL_DEBUG_MAX, "\tStatus: ");
for (Function f : functionsList) {
Utils.out.print(Utils.OUTPUTLEVEL_DEBUG_MAX, f.toString());
}
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MAX);
for (MathParserStep step : steps) {
Utils.out.println(2, "Stack fixing step \""+step.getStepName()+"\"");
@ -158,9 +166,8 @@ public class MathParser {
break;
}
//TODO: Temporary solution. In near future Variables will be distint objects and they will have a color. So they will be no longer a BlockChar/FeatureChar
for (char var : MathematicalSymbols.variables) {
if ( featureChar == var) {
if (featureChar == var) {
result = new FeatureVariable(featureChar, V_TYPE.UNKNOWN);
break;
}

View File

@ -0,0 +1,50 @@
package org.warp.picalculator.math.parser.steps;
import org.warp.picalculator.IntegerObj;
import org.warp.picalculator.math.Function;
import org.warp.picalculator.math.FunctionSingle;
import org.warp.picalculator.math.MathContext;
import org.warp.picalculator.math.functions.Multiplication;
import org.warp.picalculator.math.functions.Number;
import org.warp.picalculator.math.functions.Variable;
import org.warp.picalculator.math.parser.MathParserStep;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
public class AddImplicitMultiplications implements MathParserStep {
private MathContext context;
public AddImplicitMultiplications(MathContext context) {
this.context = context;
}
@Override
public boolean eval(IntegerObj curIndex, Function lastFunction, Function currentFunction, ObjectArrayList<Function> functionsList) {
if (currentFunction instanceof FunctionSingle) {
if (lastFunction instanceof Function) {
functionsList.set(curIndex.i, new Multiplication(context, currentFunction, lastFunction));
functionsList.remove(curIndex.i + 1);
return true;
}
} else if (currentFunction instanceof Function) {
if (lastFunction instanceof FunctionSingle) {
functionsList.set(curIndex.i, new Multiplication(context, currentFunction, lastFunction));
functionsList.remove(curIndex.i + 1);
return true;
}
}
return false;
}
@Override
public boolean requiresReversedIteration() {
return true;
}
@Override
public String getStepName() {
return "Add implicit multiplications before and after Single Functions";
}
}

View File

@ -21,7 +21,7 @@ public class JoinNumberAndVariables implements MathParserStep {
@Override
public boolean eval(IntegerObj curIndex, Function lastFunction, Function currentFunction, ObjectArrayList<Function> functionsList) {
if (currentFunction instanceof Number | currentFunction instanceof Variable) {
if (lastFunction instanceof Variable) {
if (lastFunction instanceof Variable | lastFunction instanceof Number | lastFunction instanceof Multiplication) {
final Function var = lastFunction;
final Function numb = currentFunction;
functionsList.set(curIndex.i, new Multiplication(context, numb, var));