summaryrefslogtreecommitdiff
path: root/checker/src
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2024-08-22 22:46:49 +0800
committerGravatar Uko Kokņevičs2024-08-22 22:46:49 +0800
commit1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec (patch)
tree5beb2c0175517deea3254ea1dc164f24aab10ad9 /checker/src
parentFix bug in checker that made it not execute and made ArgSpec less likely to c... (diff)
downloadorang-1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec.tar.gz
orang-1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec.tar.xz
orang-1ddcda7bd2b7148f4c6ba4f23791786c64d1fbec.zip
Get rid of that Scope mess
Diffstat (limited to 'checker/src')
-rw-r--r--checker/src/main/java/lv/enes/orang/checker/Checker.java39
1 files changed, 16 insertions, 23 deletions
diff --git a/checker/src/main/java/lv/enes/orang/checker/Checker.java b/checker/src/main/java/lv/enes/orang/checker/Checker.java
index 14604b8..ee0914f 100644
--- a/checker/src/main/java/lv/enes/orang/checker/Checker.java
+++ b/checker/src/main/java/lv/enes/orang/checker/Checker.java
@@ -1,27 +1,17 @@
1package lv.enes.orang.checker; 1package lv.enes.orang.checker;
2 2
3import lv.enes.orang.ast.*; 3import lv.enes.orang.ast.*;
4import lv.enes.orang.core.ImmutableScope;
5import lv.enes.orang.core.Scope;
6 4
7import java.util.HashMap; 5import java.util.*;
8import java.util.Map;
9 6
10public class Checker implements ExpressionVisitor<Void, CheckerException>, StatementVisitor<Checker, CheckerException> { 7public class Checker implements ExpressionVisitor<Void, CheckerException>, StatementVisitor<Checker, CheckerException> {
11 private final Scope<Boolean> definitions; 8 private final Set<String> definitions;
12 9
13 public static <E> Checker of(Map<String, E> builtins) { 10 public static <E> Checker of(Map<String, E> builtins) {
14 var boolMap = builtins.keySet() 11 return new Checker(Collections.unmodifiableSet(builtins.keySet()));
15 .stream()
16 .<Map<String, Boolean>>collect(
17 HashMap::new,
18 (map, elem) -> map.put(elem, true),
19 Map::putAll
20 );
21 return new Checker(ImmutableScope.of(boolMap));
22 } 12 }
23 13
24 private Checker(Scope<Boolean> definitions) { 14 private Checker(Set<String> definitions) {
25 this.definitions = definitions; 15 this.definitions = definitions;
26 } 16 }
27 17
@@ -55,10 +45,13 @@ public class Checker implements ExpressionVisitor<Void, CheckerException>, State
55 45
56 @Override 46 @Override
57 public Checker visitDefinition(Definition def) throws CheckerException { 47 public Checker visitDefinition(Definition def) throws CheckerException {
58 if (definitions.hasDefinition(def.name())) { 48 if (definitions.contains(def.name())) {
59 throw new CheckerException(STR."Top-level definition '\{def.name()}' redefined!"); 49 throw new CheckerException(STR."Top-level definition '\{def.name()}' redefined!");
60 } 50 }
61 var ch = new Checker(ImmutableScope.of(definitions, def.name(), true)); 51
52 var newScope = new HashSet<>(definitions);
53 newScope.add(def.name());
54 var ch = new Checker(newScope);
62 ch.visit(def.body()); 55 ch.visit(def.body());
63 return ch; 56 return ch;
64 } 57 }
@@ -79,13 +72,13 @@ public class Checker implements ExpressionVisitor<Void, CheckerException>, State
79 72
80 @Override 73 @Override
81 public Void visitFnExpression(FnExpression expr) throws CheckerException { 74 public Void visitFnExpression(FnExpression expr) throws CheckerException {
82 var args = new HashMap<String, Boolean>(); 75 var newScope = new HashSet<>(definitions);
83 for (var arg : expr.args()) { 76 for (var arg : expr.args()) {
84 if (arg instanceof ArgSpec.Named(String name)) { 77 if (arg instanceof ArgSpec.Named(String name)) {
85 args.put(name, true); 78 newScope.add(name);
86 } 79 }
87 } 80 }
88 new Checker(ImmutableScope.of(definitions, args)).visit(expr.body()); 81 new Checker(newScope).visit(expr.body());
89 return null; 82 return null;
90 } 83 }
91 84
@@ -105,11 +98,11 @@ public class Checker implements ExpressionVisitor<Void, CheckerException>, State
105 98
106 @Override 99 @Override
107 public Void visitLetInExpression(LetInExpression expr) throws CheckerException { 100 public Void visitLetInExpression(LetInExpression expr) throws CheckerException {
108 var locals = new HashMap<String, Boolean>(); 101 var newScope = new HashSet<>(definitions);
109 for (var local : expr.bindings()) { 102 for (var local : expr.bindings()) {
110 locals.put(local.name(), true); 103 newScope.add(local.name());
111 } 104 }
112 new Checker(ImmutableScope.of(definitions, locals)).visit(expr.body()); 105 new Checker(newScope).visit(expr.body());
113 return null; 106 return null;
114 } 107 }
115 108
@@ -136,7 +129,7 @@ public class Checker implements ExpressionVisitor<Void, CheckerException>, State
136 129
137 @Override 130 @Override
138 public Void visitVariable(VariableExpression expr) throws CheckerException { 131 public Void visitVariable(VariableExpression expr) throws CheckerException {
139 if (!definitions.hasDefinition(expr.name())) { 132 if (!definitions.contains(expr.name())) {
140 throw new CheckerException(STR."Variable named '\{expr.name()}' not defined!"); 133 throw new CheckerException(STR."Variable named '\{expr.name()}' not defined!");
141 } 134 }
142 return null; 135 return null;