diff options
| author | 2024-08-22 22:46:49 +0800 | |
|---|---|---|
| committer | 2024-08-22 22:46:49 +0800 | |
| commit | 1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec (patch) | |
| tree | 5beb2c0175517deea3254ea1dc164f24aab10ad9 /core/src/main | |
| parent | Fix bug in checker that made it not execute and made ArgSpec less likely to c... (diff) | |
| download | orang-1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec.tar.gz orang-1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec.tar.xz orang-1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec.zip | |
Get rid of that Scope mess
Diffstat (limited to 'core/src/main')
| -rw-r--r-- | core/src/main/java/lv/enes/orang/core/ImmutableScope.java | 47 | ||||
| -rw-r--r-- | core/src/main/java/lv/enes/orang/core/MutableScope.java | 26 | ||||
| -rw-r--r-- | core/src/main/java/lv/enes/orang/core/Scope.java | 31 |
3 files changed, 0 insertions, 104 deletions
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 @@ | |||
| 1 | package lv.enes.orang.core; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.Map; | ||
| 5 | |||
| 6 | public class ImmutableScope<E> extends Scope<E> { | ||
| 7 | public static <E> ImmutableScope<E> of(Map<String, E> builtins) { | ||
| 8 | return new ImmutableScope<>(null, new HashMap<>(builtins)); | ||
| 9 | } | ||
| 10 | |||
| 11 | public static <E> ImmutableScope<E> of(Scope<E> parent) { | ||
| 12 | if (parent instanceof ImmutableScope<E> imm) { | ||
| 13 | return imm; | ||
| 14 | } | ||
| 15 | return ImmutableScope.of(parent, Map.of()); | ||
| 16 | } | ||
| 17 | |||
| 18 | public static <E> ImmutableScope<E> of(Scope<E> parent, String key, E value) { | ||
| 19 | return ImmutableScope.of(parent, Map.of(key, value)); | ||
| 20 | } | ||
| 21 | |||
| 22 | public static <E> ImmutableScope<E> of(Scope<E> parent, Map<String, E> definitions) { | ||
| 23 | return new ImmutableScope<>(parent, definitions).maybeFlattened(); | ||
| 24 | } | ||
| 25 | |||
| 26 | protected ImmutableScope(Scope<E> parent, Map<String, E> definitions) { | ||
| 27 | super(parent, Map.copyOf(definitions)); | ||
| 28 | } | ||
| 29 | |||
| 30 | public ImmutableScope<E> maybeFlattened() { | ||
| 31 | if (depth > MAX_DEPTH) { | ||
| 32 | return flattened(); | ||
| 33 | } | ||
| 34 | return this; | ||
| 35 | } | ||
| 36 | |||
| 37 | private ImmutableScope<E> flattened() { | ||
| 38 | if (parent instanceof ImmutableScope<E> immParent) { | ||
| 39 | var flatParent = immParent.flattened(); | ||
| 40 | var newDefs = new HashMap<>(flatParent.definitions); | ||
| 41 | newDefs.putAll(definitions); | ||
| 42 | return new ImmutableScope<>(flatParent.parent, newDefs); | ||
| 43 | } | ||
| 44 | |||
| 45 | return this; | ||
| 46 | } | ||
| 47 | } | ||
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 @@ | |||
| 1 | package lv.enes.orang.core; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.Map; | ||
| 5 | |||
| 6 | public class MutableScope<E> extends Scope<E> { | ||
| 7 | public static <E> MutableScope<E> of(Map<String, E> builtins) { | ||
| 8 | return new MutableScope<>(null, new HashMap<>(builtins)); | ||
| 9 | } | ||
| 10 | |||
| 11 | public static <E> MutableScope<E> of(Scope<E> parent) { | ||
| 12 | return new MutableScope<>(parent, Map.of()); | ||
| 13 | } | ||
| 14 | |||
| 15 | public static <E> MutableScope<E> of(Scope<E> parent, String name, E value) { | ||
| 16 | return new MutableScope<>(parent, Map.of(name, value)); | ||
| 17 | } | ||
| 18 | |||
| 19 | protected MutableScope(Scope<E> parent, Map<String, E> definitions) { | ||
| 20 | super(parent, new HashMap<>(definitions)); | ||
| 21 | } | ||
| 22 | |||
| 23 | public void setDefinition(String key, E value) { | ||
| 24 | definitions.put(key, value); | ||
| 25 | } | ||
| 26 | } | ||
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 @@ | |||
| 1 | package lv.enes.orang.core; | ||
| 2 | |||
| 3 | import java.util.Map; | ||
| 4 | |||
| 5 | public abstract class Scope<E> { | ||
| 6 | public static final int MAX_DEPTH = 4; | ||
| 7 | |||
| 8 | protected final Scope<E> parent; | ||
| 9 | protected final Map<String, E> definitions; | ||
| 10 | protected final int depth; | ||
| 11 | |||
| 12 | protected Scope(Scope<E> parent, Map<String, E> definitions) { | ||
| 13 | this.parent = parent; | ||
| 14 | this.definitions = definitions; | ||
| 15 | this.depth = parent == null ? 0 : parent.depth + 1; | ||
| 16 | } | ||
| 17 | |||
| 18 | public E getDefinition(String name) throws OrangRuntimeException { | ||
| 19 | if (definitions.containsKey(name)) { | ||
| 20 | return definitions.get(name); | ||
| 21 | } else if (parent != null) { | ||
| 22 | return parent.getDefinition(name); | ||
| 23 | } else { | ||
| 24 | throw new OrangRuntimeException(STR."Value named \{name} is not defined!"); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | public boolean hasDefinition(String name) { | ||
| 29 | return definitions.containsKey(name) || parent != null && parent.hasDefinition(name); | ||
| 30 | } | ||
| 31 | } | ||