From a34a00ba19f6ffb61762da4629b53b8a633f9d17 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 11 Feb 2016 21:33:25 +0000 Subject: Implement static constructor and fields in interpreter --- .../zwobble/couscous/interpreter/Environment.java | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'src/main/java/org/zwobble/couscous/interpreter/Environment.java') 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 types; private final Optional thisValue; private final Map stackFrame; - + private final Project project; + public Environment(Project project, Optional thisValue, Map> 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 thisValue, Map> stackFrame) { -- cgit v1.2.3