aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/interpreter/Environment.java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-11 21:33:25 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-11 21:33:25 +0000
commita34a00ba19f6ffb61762da4629b53b8a633f9d17 (patch)
tree6ae34a92c0dfe9a24df0b7e90da2da263a61f708 /src/main/java/org/zwobble/couscous/interpreter/Environment.java
parent6d02b3e3e6f8da9837a46efda4733bdd89e38aaf (diff)
Implement static constructor and fields in interpreter
Diffstat (limited to 'src/main/java/org/zwobble/couscous/interpreter/Environment.java')
-rw-r--r--src/main/java/org/zwobble/couscous/interpreter/Environment.java24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/main/java/org/zwobble/couscous/interpreter/Environment.java b/src/main/java/org/zwobble/couscous/interpreter/Environment.java
index e57f8e5..daee3ed 100644
--- a/src/main/java/org/zwobble/couscous/interpreter/Environment.java
+++ b/src/main/java/org/zwobble/couscous/interpreter/Environment.java
@@ -1,24 +1,29 @@
package org.zwobble.couscous.interpreter;
-import java.util.Map;
-import java.util.Optional;
import org.zwobble.couscous.ast.TypeName;
import org.zwobble.couscous.ast.VariableDeclaration;
import org.zwobble.couscous.ast.VariableNode;
import org.zwobble.couscous.ast.identifiers.Identifier;
import org.zwobble.couscous.interpreter.errors.UnboundVariable;
import org.zwobble.couscous.interpreter.errors.VariableNotInScope;
-import org.zwobble.couscous.interpreter.values.ConcreteType;
import org.zwobble.couscous.interpreter.values.InterpreterValue;
+import org.zwobble.couscous.interpreter.values.StaticReceiverValue;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+
import static java.util.stream.Collectors.toMap;
public class Environment {
- private final Project project;
+ private final Map<TypeName, StaticReceiverValue> types;
private final Optional<InterpreterValue> thisValue;
private final Map<Identifier, VariableEntry> stackFrame;
-
+ private final Project project;
+
public Environment(Project project, Optional<InterpreterValue> thisValue, Map<VariableDeclaration, Optional<InterpreterValue>> stackFrame) {
this.project = project;
+ this.types = new HashMap<>();
this.thisValue = thisValue;
this.stackFrame = stackFrame.entrySet().stream()
.collect(toMap(
@@ -46,8 +51,13 @@ public class Environment {
put(variable.getDeclaration().getId(), value);
}
- public ConcreteType findClass(TypeName className) {
- return project.findClass(className);
+ public StaticReceiverValue findClass(TypeName className) {
+ if (!types.containsKey(className)) {
+ StaticReceiverValue value = new StaticReceiverValue(project.findClass(className));
+ types.put(className, value);
+ value.callStaticConstructor(this);
+ }
+ return types.get(className);
}
public Environment withStackFrame(Optional<InterpreterValue> thisValue, Map<VariableDeclaration, Optional<InterpreterValue>> stackFrame) {