1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-17 08:03:09 -08:00

Add the ability for plugins to access variables, and add the operators.

This commit is contained in:
Danila Fedorin 2017-09-02 00:20:18 -07:00
parent 28802cfed3
commit 585cabc478
3 changed files with 60 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package org.nwapw.abacus.plugin;
import org.nwapw.abacus.function.*;
import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.variables.VariableDatabase;
/**
* A plugin class that can be externally implemented and loaded via the
@ -219,4 +220,12 @@ public abstract class Plugin {
*/
public abstract void onDisable();
/**
* Get the variable database.
* @return the variable database.
*/
public final VariableDatabase getVariableDatabase(){
return manager.getVariableDatabase();
}
}

View File

@ -3,6 +3,7 @@ package org.nwapw.abacus.plugin;
import org.nwapw.abacus.Abacus;
import org.nwapw.abacus.function.*;
import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.variables.VariableDatabase;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
@ -421,4 +422,12 @@ public class PluginManager {
public Set<Class<?>> getLoadedPluginClasses() {
return loadedPluginClasses;
}
/**
* Gets the variable database.
* @return the database.
*/
public VariableDatabase getVariableDatabase(){
return abacus.getVariableDatabase();
}
}

View File

@ -1,9 +1,14 @@
package org.nwapw.abacus.plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.nwapw.abacus.function.*;
import org.nwapw.abacus.number.NaiveNumber;
import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.number.PreciseNumber;
import org.nwapw.abacus.tree.Reducer;
import org.nwapw.abacus.tree.TreeNode;
import org.nwapw.abacus.tree.VariableNode;
import java.util.ArrayList;
import java.util.HashMap;
@ -14,6 +19,40 @@ import java.util.HashMap;
*/
public class StandardPlugin extends Plugin {
/**
* The set operator.
*/
public final TreeValueOperator opSet = new TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) {
@Override
public boolean matchesParams(NumberImplementation implementation, TreeNode[] params) {
return params.length == 2 && params[0] instanceof VariableNode;
}
@Override
public NumberInterface applyWithReducerInternal(NumberImplementation implementation, Reducer<? extends NumberInterface> reducer, TreeNode[] params) {
String assignTo = ((VariableNode) params[0]).getVariable();
NumberInterface value = params[1].reduce(reducer);
getVariableDatabase().getVariables().put(assignTo, value);
return value;
}
};
/**
* The define operator.
*/
public final TreeValueOperator opDefine = new TreeValueOperator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0) {
@Override
public boolean matchesParams(NumberImplementation implementation, TreeNode[] params) {
return params.length == 2 && params[0] instanceof VariableNode;
}
@Nullable
@Override
public NumberInterface applyWithReducerInternal(NumberImplementation implementation, Reducer<? extends NumberInterface> reducer, TreeNode[] params) {
String assignTo = ((VariableNode) params[0]).getVariable();
getVariableDatabase().getDefinitions().put(assignTo, params[1]);
return params[1].reduce(reducer);
}
};
/**
* The addition operator, +
*/
@ -712,6 +751,9 @@ public class StandardPlugin extends Plugin {
registerOperator("^", OP_CARET);
registerOperator("!", OP_FACTORIAL);
registerTreeValueOperator("=", opSet);
registerTreeValueOperator(":=", opDefine);
registerOperator("nPr", OP_NPR);
registerOperator("nCr", OP_NCR);