From 381c88f7a0ededbd21ecbc67015624a3adc41d49 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 27 Jul 2017 14:15:45 -0700 Subject: [PATCH] Fix several bugs and register operations as operations. --- src/org/nwapw/abacus/function/Operator.java | 2 +- .../nwapw/abacus/plugin/StandardPlugin.java | 18 ++++++++++-------- src/org/nwapw/abacus/tree/NumberReducer.java | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/org/nwapw/abacus/function/Operator.java b/src/org/nwapw/abacus/function/Operator.java index bd12495..6d6f7ff 100644 --- a/src/org/nwapw/abacus/function/Operator.java +++ b/src/org/nwapw/abacus/function/Operator.java @@ -3,7 +3,7 @@ package org.nwapw.abacus.function; /** * A class that represents a single infix operator. */ -public abstract class Operator { +public class Operator { /** * The associativity of the operator. diff --git a/src/org/nwapw/abacus/plugin/StandardPlugin.java b/src/org/nwapw/abacus/plugin/StandardPlugin.java index a864ff6..bd410cd 100755 --- a/src/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/org/nwapw/abacus/plugin/StandardPlugin.java @@ -1,6 +1,8 @@ package org.nwapw.abacus.plugin; 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.NumberInterface; @@ -21,7 +23,7 @@ public class StandardPlugin extends Plugin { @Override public void onEnable() { - registerFunction("+", new Function() { + registerOperator("\\+", new Operator(OperatorAssociativity.LEFT, 0, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length >= 1; @@ -35,9 +37,9 @@ public class StandardPlugin extends Plugin { } return sum; } - }); + })); - registerFunction("-", new Function() { + registerOperator("-", new Operator(OperatorAssociativity.LEFT, 0, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2; @@ -47,9 +49,9 @@ public class StandardPlugin extends Plugin { protected NumberInterface applyInternal(NumberInterface[] params) { return params[0].subtract(params[1]); } - }); + })); - registerFunction("*", new Function() { + registerOperator("\\*", new Operator(OperatorAssociativity.LEFT, 1, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length >= 1; @@ -63,9 +65,9 @@ public class StandardPlugin extends Plugin { } return product; } - }); + })); - registerFunction("/", new Function() { + registerOperator("/", new Operator(OperatorAssociativity.LEFT, 1, new Function() { @Override protected boolean matchesParams(NumberInterface[] params) { return params.length == 2; @@ -75,7 +77,7 @@ public class StandardPlugin extends Plugin { protected NumberInterface applyInternal(NumberInterface[] params) { return params[0].divide(params[1]); } - }); + })); registerFunction("!", new Function() { //private HashMap, ArrayList> storedList = new HashMap, ArrayList>(); diff --git a/src/org/nwapw/abacus/tree/NumberReducer.java b/src/org/nwapw/abacus/tree/NumberReducer.java index ab80374..6e4bee8 100644 --- a/src/org/nwapw/abacus/tree/NumberReducer.java +++ b/src/org/nwapw/abacus/tree/NumberReducer.java @@ -30,7 +30,7 @@ public class NumberReducer implements Reducer { } else if(node instanceof OpNode){ NumberInterface left = (NumberInterface) children[0]; 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; return function.apply(left, right); } else if(node instanceof FunctionNode){