mirror of
https://github.com/DanilaFe/abacus
synced 2025-01-09 23:58:09 -08:00
add variables
This commit is contained in:
parent
d67d498625
commit
b2a20226d3
102
src/main/java/org/nwapw/abacus/number/Variable.java
Normal file
102
src/main/java/org/nwapw/abacus/number/Variable.java
Normal file
@ -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<? extends NumberInterface> 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<? extends NumberInterface> toClass) {
|
||||||
|
return value.promoteToInternal(toClass);
|
||||||
|
}
|
||||||
|
public String toString(){
|
||||||
|
return value.toString();
|
||||||
|
}
|
||||||
|
}
|
52
src/main/java/org/nwapw/abacus/plugin/VariablePlugin.java
Normal file
52
src/main/java/org/nwapw/abacus/plugin/VariablePlugin.java
Normal file
@ -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<String,NumberInterface> variableMap;
|
||||||
|
public final Operator OP_EQUALS = new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, -1, new Function() {
|
||||||
|
//private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
||||||
|
@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(){
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
24
src/main/java/org/nwapw/abacus/tree/VariableNode.java
Normal file
24
src/main/java/org/nwapw/abacus/tree/VariableNode.java
Normal file
@ -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> T reduce(Reducer<T> reducer) {
|
||||||
|
return reducer.reduceNode(this);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return variable;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user