From 45a2b88d584fc8bdee1fbaf0def1d9b55ba26eff Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Tue, 21 Jun 2016 20:07:32 +0300 Subject: Added scopes, variables and functions --- main/core/runtime/Scope.java | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 main/core/runtime/Scope.java (limited to 'main/core/runtime/Scope.java') 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 variables = new HashMap<>(); + private Map 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 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); + } +} -- cgit v1.3