1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 07:50:09 -08:00

Fix several bugs and register operations as operations.

This commit is contained in:
Danila Fedorin 2017-07-27 14:15:45 -07:00
parent 5c9c718283
commit 381c88f7a0
3 changed files with 12 additions and 10 deletions

View File

@ -3,7 +3,7 @@ package org.nwapw.abacus.function;
/** /**
* A class that represents a single infix operator. * A class that represents a single infix operator.
*/ */
public abstract class Operator { public class Operator {
/** /**
* The associativity of the operator. * The associativity of the operator.

View File

@ -1,6 +1,8 @@
package org.nwapw.abacus.plugin; package org.nwapw.abacus.plugin;
import org.nwapw.abacus.function.Function; import org.nwapw.abacus.function.Function;
import org.nwapw.abacus.function.Operator;
import org.nwapw.abacus.function.OperatorAssociativity;
import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NaiveNumber;
import org.nwapw.abacus.number.NumberInterface; import org.nwapw.abacus.number.NumberInterface;
@ -21,7 +23,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
public void onEnable() { public void onEnable() {
registerFunction("+", new Function() { registerOperator("\\+", new Operator(OperatorAssociativity.LEFT, 0, new Function() {
@Override @Override
protected boolean matchesParams(NumberInterface[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length >= 1; return params.length >= 1;
@ -35,9 +37,9 @@ public class StandardPlugin extends Plugin {
} }
return sum; return sum;
} }
}); }));
registerFunction("-", new Function() { registerOperator("-", new Operator(OperatorAssociativity.LEFT, 0, new Function() {
@Override @Override
protected boolean matchesParams(NumberInterface[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length == 2; return params.length == 2;
@ -47,9 +49,9 @@ public class StandardPlugin extends Plugin {
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
return params[0].subtract(params[1]); return params[0].subtract(params[1]);
} }
}); }));
registerFunction("*", new Function() { registerOperator("\\*", new Operator(OperatorAssociativity.LEFT, 1, new Function() {
@Override @Override
protected boolean matchesParams(NumberInterface[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length >= 1; return params.length >= 1;
@ -63,9 +65,9 @@ public class StandardPlugin extends Plugin {
} }
return product; return product;
} }
}); }));
registerFunction("/", new Function() { registerOperator("/", new Operator(OperatorAssociativity.LEFT, 1, new Function() {
@Override @Override
protected boolean matchesParams(NumberInterface[] params) { protected boolean matchesParams(NumberInterface[] params) {
return params.length == 2; return params.length == 2;
@ -75,7 +77,7 @@ public class StandardPlugin extends Plugin {
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
return params[0].divide(params[1]); return params[0].divide(params[1]);
} }
}); }));
registerFunction("!", new Function() { registerFunction("!", new Function() {
//private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>(); //private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();

View File

@ -30,7 +30,7 @@ public class NumberReducer implements Reducer<NumberInterface> {
} else if(node instanceof OpNode){ } else if(node instanceof OpNode){
NumberInterface left = (NumberInterface) children[0]; NumberInterface left = (NumberInterface) children[0];
NumberInterface right = (NumberInterface) children[1]; NumberInterface right = (NumberInterface) children[1];
Function function = manager.functionFor(((OpNode) node).getOperation()); Function function = manager.operatorFor(((OpNode) node).getOperation()).getFunction();
if(function == null) return null; if(function == null) return null;
return function.apply(left, right); return function.apply(left, right);
} else if(node instanceof FunctionNode){ } else if(node instanceof FunctionNode){