From 585cabc478288fc0a299a3d3bae33d904ca1d6c5 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 2 Sep 2017 00:20:18 -0700 Subject: [PATCH] Add the ability for plugins to access variables, and add the operators. --- .../java/org/nwapw/abacus/plugin/Plugin.java | 9 ++++ .../nwapw/abacus/plugin/PluginManager.java | 9 ++++ .../nwapw/abacus/plugin/StandardPlugin.java | 42 +++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java b/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java index 223d7aa..ecec78a 100644 --- a/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/Plugin.java @@ -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(); + } + } diff --git a/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java b/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java index 0c99c58..9f7e0ab 100644 --- a/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/PluginManager.java @@ -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> getLoadedPluginClasses() { return loadedPluginClasses; } + + /** + * Gets the variable database. + * @return the database. + */ + public VariableDatabase getVariableDatabase(){ + return abacus.getVariableDatabase(); + } } diff --git a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java index 36ac473..b4f919a 100755 --- a/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/core/src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java @@ -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 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 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);