summaryrefslogtreecommitdiff
path: root/core/src/main/java
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2024-08-19 01:15:51 +0800
committerGravatar Uko Kokņevičs2024-08-19 01:15:51 +0800
commitce423971e6ea3b4126806eea4b6874daee9f07b2 (patch)
treee412ed023e405f7bb7ddc915f14ffa07469aceec /core/src/main/java
parentChanged the grammar to allow bare top-level expressions only in REPL. (diff)
downloadorang-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 '')
-rw-r--r--core/src/main/java/lv/enes/orang/core/ImmutableScope.java47
-rw-r--r--core/src/main/java/lv/enes/orang/core/MutableScope.java26
-rw-r--r--core/src/main/java/lv/enes/orang/core/OrangRuntimeException.java (renamed from orang/src/main/java/lv/enes/orang/OrangRuntimeException.java)4
-rw-r--r--core/src/main/java/lv/enes/orang/core/Scope.java (renamed from orang/src/main/java/lv/enes/orang/Scope.java)23
4 files changed, 84 insertions, 16 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 @@
1package lv.enes.orang.core;
2
3import java.util.HashMap;
4import java.util.Map;
5
6public 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 @@
1package lv.enes.orang.core;
2
3import java.util.HashMap;
4import java.util.Map;
5
6public 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/orang/src/main/java/lv/enes/orang/OrangRuntimeException.java b/core/src/main/java/lv/enes/orang/core/OrangRuntimeException.java
index 8a7d575..6ce5f70 100644
--- a/orang/src/main/java/lv/enes/orang/OrangRuntimeException.java
+++ b/core/src/main/java/lv/enes/orang/core/OrangRuntimeException.java
@@ -1,6 +1,4 @@
1package lv.enes.orang; 1package lv.enes.orang.core;
2
3import lv.enes.orang.core.OrangException;
4 2
5public class OrangRuntimeException extends OrangException { 3public class OrangRuntimeException extends OrangException {
6 public OrangRuntimeException(String message) { 4 public OrangRuntimeException(String message) {
diff --git a/orang/src/main/java/lv/enes/orang/Scope.java b/core/src/main/java/lv/enes/orang/core/Scope.java
index 7fe57c7..0f4d23a 100644
--- a/orang/src/main/java/lv/enes/orang/Scope.java
+++ b/core/src/main/java/lv/enes/orang/core/Scope.java
@@ -1,28 +1,21 @@
1package lv.enes.orang; 1package lv.enes.orang.core;
2 2
3import lv.enes.orang.value.Value;
4
5import java.util.HashMap;
6import java.util.Map; 3import java.util.Map;
7 4
8public abstract class Scope { 5public abstract class Scope<E> {
9 public static final int MAX_DEPTH = 4; 6 public static final int MAX_DEPTH = 4;
10 7
11 protected final Scope parent; 8 protected final Scope<E> parent;
12 protected final Map<String, Value> definitions; 9 protected final Map<String, E> definitions;
13 protected final int depth; 10 protected final int depth;
14 11
15 public static MutableScope topLevel() { 12 protected Scope(Scope<E> parent, Map<String, E> definitions) {
16 return new MutableScope(null, new HashMap<>(Builtins.BUILTINS));
17 }
18
19 protected Scope(Scope parent, Map<String, Value> definitions) {
20 this.parent = parent; 13 this.parent = parent;
21 this.definitions = definitions; 14 this.definitions = definitions;
22 this.depth = parent == null ? 0 : parent.depth + 1; 15 this.depth = parent == null ? 0 : parent.depth + 1;
23 } 16 }
24 17
25 public Value getDefinition(String name) throws OrangRuntimeException { 18 public E getDefinition(String name) throws OrangRuntimeException {
26 if (definitions.containsKey(name)) { 19 if (definitions.containsKey(name)) {
27 return definitions.get(name); 20 return definitions.get(name);
28 } else if (parent != null) { 21 } else if (parent != null) {
@@ -31,4 +24,8 @@ public abstract class Scope {
31 throw new OrangRuntimeException(STR."Value named \{name} is not defined!"); 24 throw new OrangRuntimeException(STR."Value named \{name} is not defined!");
32 } 25 }
33 } 26 }
27
28 public boolean hasDefinition(String name) {
29 return definitions.containsKey(name) || parent != null && parent.hasDefinition(name);
30 }
34} 31}