diff --git a/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt b/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt index 99214a7..23e6f8e 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/Abacus.kt @@ -48,6 +48,10 @@ class Abacus(val configuration: Configuration) { * The promotion manager used to convert between number implementations. */ val promotionManager = PromotionManager(this) + /** + * The database of variable definitions. + */ + val variableDatabase = VariableDatabase(this) /** * The number implementation used by default. */ @@ -63,6 +67,7 @@ class Abacus(val configuration: Configuration) { pluginManager.addListener(tokenizer) pluginManager.addListener(parser) pluginManager.addListener(promotionManager) + pluginManager.addListener(variableDatabase) } /** diff --git a/core/src/main/kotlin/org/nwapw/abacus/variables/VariableDatabase.kt b/core/src/main/kotlin/org/nwapw/abacus/variables/VariableDatabase.kt new file mode 100644 index 0000000..ab477e2 --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/variables/VariableDatabase.kt @@ -0,0 +1,79 @@ +package org.nwapw.abacus.variables + +import org.nwapw.abacus.Abacus +import org.nwapw.abacus.number.NumberInterface +import org.nwapw.abacus.plugin.PluginListener +import org.nwapw.abacus.plugin.PluginManager +import javax.swing.tree.TreeNode + +/** + * A database for variables and definition. + * + * The variable database is used to keep track of + * variables and definitions throughout the calculator. + * + * @property abacus the Abacus instance. + */ +class VariableDatabase(val abacus: Abacus): PluginListener { + + /** + * The variables that are stored in the database. + */ + private val variables = mutableMapOf() + /** + * The definitions that are stored in the database. + */ + private val definitions = mutableMapOf() + + /** + * 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 onUnload(manager: PluginManager?) { + variables.clear() + definitions.clear() + } + +} \ No newline at end of file