mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 15:30:09 -08:00
Remove the additional methods from the VariableDatabase.
This commit is contained in:
parent
428df8bfd3
commit
28802cfed3
|
@ -8,6 +8,7 @@ import org.nwapw.abacus.function.TreeValueOperator;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.number.PromotionManager;
|
import org.nwapw.abacus.number.PromotionManager;
|
||||||
import org.nwapw.abacus.number.PromotionResult;
|
import org.nwapw.abacus.number.PromotionResult;
|
||||||
|
import org.nwapw.abacus.variables.VariableDatabase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A reducer implementation that turns a tree into a single number.
|
* A reducer implementation that turns a tree into a single number.
|
||||||
|
@ -35,7 +36,13 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||||
if (node instanceof NumberNode) {
|
if (node instanceof NumberNode) {
|
||||||
return abacus.getNumberImplementation().instanceForString(((NumberNode) node).getNumber());
|
return abacus.getNumberImplementation().instanceForString(((NumberNode) node).getNumber());
|
||||||
} else if (node instanceof VariableNode) {
|
} else if (node instanceof VariableNode) {
|
||||||
return abacus.getVariableDatabase().getVariableValue(((VariableNode) node).getVariable());
|
VariableDatabase database = abacus.getVariableDatabase();
|
||||||
|
String name = ((VariableNode) node).getVariable();
|
||||||
|
NumberInterface variable = database.getVariables().get(name);
|
||||||
|
if(variable != null) return variable;
|
||||||
|
TreeNode definition = database.getDefinitions().get(name);
|
||||||
|
if(definition != null) return definition.reduce(this);
|
||||||
|
return abacus.getNumberImplementation().instanceForString("0");
|
||||||
} else if (node instanceof NumberBinaryNode) {
|
} else if (node instanceof NumberBinaryNode) {
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
|
|
|
@ -4,7 +4,7 @@ import org.nwapw.abacus.Abacus
|
||||||
import org.nwapw.abacus.number.NumberInterface
|
import org.nwapw.abacus.number.NumberInterface
|
||||||
import org.nwapw.abacus.plugin.PluginListener
|
import org.nwapw.abacus.plugin.PluginListener
|
||||||
import org.nwapw.abacus.plugin.PluginManager
|
import org.nwapw.abacus.plugin.PluginManager
|
||||||
import javax.swing.tree.TreeNode
|
import org.nwapw.abacus.tree.TreeNode
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A database for variables and definition.
|
* A database for variables and definition.
|
||||||
|
@ -19,53 +19,11 @@ class VariableDatabase(val abacus: Abacus): PluginListener {
|
||||||
/**
|
/**
|
||||||
* The variables that are stored in the database.
|
* The variables that are stored in the database.
|
||||||
*/
|
*/
|
||||||
private val variables = mutableMapOf<String, NumberInterface>()
|
val variables = mutableMapOf<String, NumberInterface>()
|
||||||
/**
|
/**
|
||||||
* The definitions that are stored in the database.
|
* The definitions that are stored in the database.
|
||||||
*/
|
*/
|
||||||
private val definitions = mutableMapOf<String, TreeNode>()
|
val definitions = mutableMapOf<String, TreeNode>()
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores a variable in the database.
|
|
||||||
*
|
|
||||||
* @param name the name of the variable.
|
|
||||||
* @param value the new value of the variable.
|
|
||||||
*/
|
|
||||||
fun storeVariable(name: String, value: NumberInterface) {
|
|
||||||
variables[name] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stores a definition in the database
|
|
||||||
*
|
|
||||||
* @param name the name of the definition.
|
|
||||||
* @param value the new value of the definition.
|
|
||||||
*/
|
|
||||||
fun storeDefinition(name: String, value: TreeNode) {
|
|
||||||
definitions[name] = value
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the value of the variable, or 0 if
|
|
||||||
* it is not defined.
|
|
||||||
*
|
|
||||||
* @param name the name of the variable.
|
|
||||||
* @return the value of the variable.
|
|
||||||
*/
|
|
||||||
fun getVariableValue(name: String): NumberInterface {
|
|
||||||
return variables[name] ?:
|
|
||||||
abacus.numberImplementation.instanceForString("0")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the definition.
|
|
||||||
*
|
|
||||||
* @param name the name of the definition.
|
|
||||||
* @return the value of the definition, or null if one doesn't exist.
|
|
||||||
*/
|
|
||||||
fun getDefinition(name: String): TreeNode? {
|
|
||||||
return definitions[name]
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onLoad(manager: PluginManager?) {
|
override fun onLoad(manager: PluginManager?) {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user