From b2a20226d3465a2b9a1234e7b72d955f0833c632 Mon Sep 17 00:00:00 2001 From: Riley Jones Date: Wed, 9 Aug 2017 09:44:51 -0700 Subject: [PATCH] add variables --- .../org/nwapw/abacus/number/Variable.java | 102 ++++++++++++++++++ .../nwapw/abacus/plugin/VariablePlugin.java | 52 +++++++++ .../org/nwapw/abacus/tree/VariableNode.java | 24 +++++ 3 files changed, 178 insertions(+) create mode 100644 src/main/java/org/nwapw/abacus/number/Variable.java create mode 100644 src/main/java/org/nwapw/abacus/plugin/VariablePlugin.java create mode 100644 src/main/java/org/nwapw/abacus/tree/VariableNode.java diff --git a/src/main/java/org/nwapw/abacus/number/Variable.java b/src/main/java/org/nwapw/abacus/number/Variable.java new file mode 100644 index 0000000..30b0f65 --- /dev/null +++ b/src/main/java/org/nwapw/abacus/number/Variable.java @@ -0,0 +1,102 @@ +package org.nwapw.abacus.number; + +public class Variable extends NumberInterface{ + public NumberInterface value; + public String variable; + + public Variable(NumberInterface value,String variable){ + this.value = value; + this.variable = variable; + } + public String getVariable(){ + return variable; + } + + @Override + public NumberInterface number(){ + return value.number(); + } + + @Override + public Class getClassVal(){ + return value.getClassVal(); + } + + @Override + public int getMaxPrecision() { + return value.getMaxPrecision(); + } + + @Override + protected NumberInterface multiplyInternal(NumberInterface multiplier) { + value = value.promoteToInternal(multiplier.number().getClass()); + return value.multiplyInternal(multiplier.number()); + } + + @Override + protected NumberInterface divideInternal(NumberInterface divisor) { + value = value.promoteToInternal(divisor.number().getClass()); + return value.divideInternal(divisor.number()); + } + + @Override + protected NumberInterface addInternal(NumberInterface summand) { + value = value.promoteToInternal(summand.number().getClass()); + return value.addInternal(summand.number()); + } + + @Override + protected NumberInterface subtractInternal(NumberInterface subtrahend) { + value = value.promoteToInternal(subtrahend.number().getClass()); + return value.subtractInternal(subtrahend.number()); + } + + @Override + protected NumberInterface negateInternal() { + return value.negateInternal(); + } + + @Override + protected NumberInterface intPowInternal(int exponent) { + return value.intPowInternal(exponent); + } + + @Override + public int compareTo(NumberInterface number) { + value = value.promoteToInternal(number.number().getClass()); + return value.compareTo(number.number()); + } + + @Override + public int signum() { + return value.signum(); + } + + @Override + protected NumberInterface ceilingInternal() { + return value.ceilingInternal(); + } + + @Override + protected NumberInterface floorInternal() { + return value.floorInternal(); + } + + @Override + protected NumberInterface fractionalPartInternal() { + return value.fractionalPartInternal(); + } + + @Override + public int intValue() { + return value.intValue(); + } + + @Override + protected NumberInterface promoteToInternal(Class toClass) { + return value.promoteToInternal(toClass); + } + public String toString(){ + return value.toString(); + } +} \ No newline at end of file diff --git a/src/main/java/org/nwapw/abacus/plugin/VariablePlugin.java b/src/main/java/org/nwapw/abacus/plugin/VariablePlugin.java new file mode 100644 index 0000000..ef04032 --- /dev/null +++ b/src/main/java/org/nwapw/abacus/plugin/VariablePlugin.java @@ -0,0 +1,52 @@ +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.function.OperatorType; +import org.nwapw.abacus.number.NumberInterface; +import org.nwapw.abacus.number.Variable; + +import java.lang.reflect.Method; +import java.util.HashMap; + +public class VariablePlugin extends Plugin { + private HashMap variableMap; + public final Operator OP_EQUALS = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, -1, new Function() { + //private HashMap, ArrayList> storedList = new HashMap, ArrayList>(); + @Override + protected boolean matchesParams(NumberInterface[] params) { + return params.length == 2; + } + + @Override + protected NumberInterface applyInternal(NumberInterface[] params) { + //System.out.println((char)Double.parseDouble(params[1].toString())); + //System.out.println(params[0].toString()); + if (params[0] instanceof Variable){ + variableMap.put(((Variable) params[0]).getVariable(), params[1]); + } + return params[1]; + } + }); + public NumberInterface getValue(String variable){ + return variableMap.get(variable); + } + public VariablePlugin(PluginManager manager) { + super(manager); + //variables = new ArrayList<>(); + variableMap=new HashMap<>(); + } + + + @Override + public void onEnable(){ + //variables = new ArrayList<>(); + variableMap=new HashMap<>(); + registerOperator("=",OP_EQUALS); + } + @Override + public void onDisable(){ + + } +}; diff --git a/src/main/java/org/nwapw/abacus/tree/VariableNode.java b/src/main/java/org/nwapw/abacus/tree/VariableNode.java new file mode 100644 index 0000000..5b5d0b6 --- /dev/null +++ b/src/main/java/org/nwapw/abacus/tree/VariableNode.java @@ -0,0 +1,24 @@ +package org.nwapw.abacus.tree; + +import org.nwapw.abacus.number.NumberInterface; + +public class VariableNode extends TreeNode{ + private String variable; + public VariableNode() { + variable = null; + } + public VariableNode(String name){ + this.variable = name; + } + public String getVariable() { + return variable; + } + @Override + public T reduce(Reducer reducer) { + return reducer.reduceNode(this); + } + @Override + public String toString() { + return variable; + } +}