aboutsummaryrefslogtreecommitdiffstats
path: root/main/core/runtime/Scope.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/core/runtime/Scope.java')
-rw-r--r--main/core/runtime/Scope.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/main/core/runtime/Scope.java b/main/core/runtime/Scope.java
new file mode 100644
index 0000000..a45cdd7
--- /dev/null
+++ b/main/core/runtime/Scope.java
@@ -0,0 +1,48 @@
+package com.jantuomi.interpreter.main.core.runtime;
+
+import com.jantuomi.interpreter.main.core.parser.datatype.DataContainer;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Created by jan on 20.6.2016.
+ */
+public class Scope {
+ private Scope parent = null;
+
+ private Map<String, DataContainer> variables = new HashMap<>();
+ private Map<String, Function> functions = new HashMap<>();
+
+ public void addVariable(String symbol) {
+ variables.put(symbol, null);
+ }
+
+ public void setVariableValue(String symbol, DataContainer value) {
+ variables.replace(symbol, value);
+ }
+
+ public DataContainer resolveSymbol(String symbol, List<DataContainer> params) {
+ if (functions.containsKey(symbol)) {
+ return functions.get(symbol).evaluate(params);
+ }
+ if (variables.containsKey(symbol)) {
+ return variables.get(symbol);
+ }
+ else if (parent != null) {
+ return parent.resolveSymbol(symbol, params);
+ }
+ else {
+ return null;
+ }
+ }
+
+ public void setParent(Scope parent) {
+ this.parent = parent;
+ }
+
+ public void addFunction(String symbol, Function func) {
+ functions.put(symbol, func);
+ }
+}