From 1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Thu, 22 Aug 2024 22:46:49 +0800 Subject: Get rid of that Scope mess --- .../java/lv/enes/orang/core/ImmutableScope.java | 47 ---------------------- .../main/java/lv/enes/orang/core/MutableScope.java | 26 ------------ core/src/main/java/lv/enes/orang/core/Scope.java | 31 -------------- 3 files changed, 104 deletions(-) delete mode 100644 core/src/main/java/lv/enes/orang/core/ImmutableScope.java delete mode 100644 core/src/main/java/lv/enes/orang/core/MutableScope.java delete mode 100644 core/src/main/java/lv/enes/orang/core/Scope.java (limited to 'core/src/main/java/lv/enes') diff --git a/core/src/main/java/lv/enes/orang/core/ImmutableScope.java b/core/src/main/java/lv/enes/orang/core/ImmutableScope.java deleted file mode 100644 index 5120b08..0000000 --- a/core/src/main/java/lv/enes/orang/core/ImmutableScope.java +++ /dev/null @@ -1,47 +0,0 @@ -package lv.enes.orang.core; - -import java.util.HashMap; -import java.util.Map; - -public class ImmutableScope extends Scope { - public static ImmutableScope of(Map builtins) { - return new ImmutableScope<>(null, new HashMap<>(builtins)); - } - - public static ImmutableScope of(Scope parent) { - if (parent instanceof ImmutableScope imm) { - return imm; - } - return ImmutableScope.of(parent, Map.of()); - } - - public static ImmutableScope of(Scope parent, String key, E value) { - return ImmutableScope.of(parent, Map.of(key, value)); - } - - public static ImmutableScope of(Scope parent, Map definitions) { - return new ImmutableScope<>(parent, definitions).maybeFlattened(); - } - - protected ImmutableScope(Scope parent, Map definitions) { - super(parent, Map.copyOf(definitions)); - } - - public ImmutableScope maybeFlattened() { - if (depth > MAX_DEPTH) { - return flattened(); - } - return this; - } - - private ImmutableScope flattened() { - if (parent instanceof ImmutableScope immParent) { - var flatParent = immParent.flattened(); - var newDefs = new HashMap<>(flatParent.definitions); - newDefs.putAll(definitions); - return new ImmutableScope<>(flatParent.parent, newDefs); - } - - return this; - } -} diff --git a/core/src/main/java/lv/enes/orang/core/MutableScope.java b/core/src/main/java/lv/enes/orang/core/MutableScope.java deleted file mode 100644 index 8d8b455..0000000 --- a/core/src/main/java/lv/enes/orang/core/MutableScope.java +++ /dev/null @@ -1,26 +0,0 @@ -package lv.enes.orang.core; - -import java.util.HashMap; -import java.util.Map; - -public class MutableScope extends Scope { - public static MutableScope of(Map builtins) { - return new MutableScope<>(null, new HashMap<>(builtins)); - } - - public static MutableScope of(Scope parent) { - return new MutableScope<>(parent, Map.of()); - } - - public static MutableScope of(Scope parent, String name, E value) { - return new MutableScope<>(parent, Map.of(name, value)); - } - - protected MutableScope(Scope parent, Map definitions) { - super(parent, new HashMap<>(definitions)); - } - - public void setDefinition(String key, E value) { - definitions.put(key, value); - } -} diff --git a/core/src/main/java/lv/enes/orang/core/Scope.java b/core/src/main/java/lv/enes/orang/core/Scope.java deleted file mode 100644 index 0f4d23a..0000000 --- a/core/src/main/java/lv/enes/orang/core/Scope.java +++ /dev/null @@ -1,31 +0,0 @@ -package lv.enes.orang.core; - -import java.util.Map; - -public abstract class Scope { - public static final int MAX_DEPTH = 4; - - protected final Scope parent; - protected final Map definitions; - protected final int depth; - - protected Scope(Scope parent, Map definitions) { - this.parent = parent; - this.definitions = definitions; - this.depth = parent == null ? 0 : parent.depth + 1; - } - - public E getDefinition(String name) throws OrangRuntimeException { - if (definitions.containsKey(name)) { - return definitions.get(name); - } else if (parent != null) { - return parent.getDefinition(name); - } else { - throw new OrangRuntimeException(STR."Value named \{name} is not defined!"); - } - } - - public boolean hasDefinition(String name) { - return definitions.containsKey(name) || parent != null && parent.hasDefinition(name); - } -} -- cgit v1.2.3