diff options
| author | 2024-08-19 01:15:51 +0800 | |
|---|---|---|
| committer | 2024-08-19 01:15:51 +0800 | |
| commit | ce423971e6ea3b4126806eea4b6874daee9f07b2 (patch) | |
| tree | e412ed023e405f7bb7ddc915f14ffa07469aceec /core/src/main/java | |
| parent | Changed the grammar to allow bare top-level expressions only in REPL. (diff) | |
| download | orang-ce423971e6ea3b4126806eea4b6874daee9f07b2.tar.gz orang-ce423971e6ea3b4126806eea4b6874daee9f07b2.tar.xz orang-ce423971e6ea3b4126806eea4b6874daee9f07b2.zip | |
Added a checker module.
NOTE: I think I should look at guava or commons for some sort of sealable Map instead of the current Scope :D
Diffstat (limited to 'core/src/main/java')
4 files changed, 115 insertions, 0 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 new file mode 100644 index 0000000..5120b08 --- /dev/null +++ b/core/src/main/java/lv/enes/orang/core/ImmutableScope.java | |||
| @@ -0,0 +1,47 @@ | |||
| 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 new file mode 100644 index 0000000..8d8b455 --- /dev/null +++ b/core/src/main/java/lv/enes/orang/core/MutableScope.java | |||
| @@ -0,0 +1,26 @@ | |||
| 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/OrangRuntimeException.java b/core/src/main/java/lv/enes/orang/core/OrangRuntimeException.java new file mode 100644 index 0000000..6ce5f70 --- /dev/null +++ b/core/src/main/java/lv/enes/orang/core/OrangRuntimeException.java | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | package lv.enes.orang.core; | ||
| 2 | |||
| 3 | public class OrangRuntimeException extends OrangException { | ||
| 4 | public OrangRuntimeException(String message) { | ||
| 5 | super(message); | ||
| 6 | } | ||
| 7 | |||
| 8 | public OrangRuntimeException(Throwable cause) { | ||
| 9 | super(cause); | ||
| 10 | } | ||
| 11 | } | ||
diff --git a/core/src/main/java/lv/enes/orang/core/Scope.java b/core/src/main/java/lv/enes/orang/core/Scope.java new file mode 100644 index 0000000..0f4d23a --- /dev/null +++ b/core/src/main/java/lv/enes/orang/core/Scope.java | |||
| @@ -0,0 +1,31 @@ | |||
| 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 | } | ||