-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTTypeDef.java
More file actions
40 lines (34 loc) · 1.06 KB
/
Copy pathASTTypeDef.java
File metadata and controls
40 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class ASTTypeDef implements ASTNode {
HashMap<String,ASTType> ltd;
ASTNode body;
public ASTTypeDef(HashMap<String,ASTType> ltdp, ASTNode b) {
ltd = ltdp;
body = b;
}
@Override
public IValue eval(Environment<IValue> env) throws InterpreterError {
return body.eval(env);
}
@Override
public ASTType typecheck(Environment<ASTType> e) throws TypeCheckError, InterpreterError {
e = e.beginScope();
for (String id : ltd.keySet()) {
e.assoc(id, ltd.get(id));
}
ASTType returnType = body.typecheck(e);
e.endScope();
return returnType;
}
static public ASTType unfold(ASTType t, Environment<ASTType> e) throws InterpreterError {
Set<String> seen = new HashSet<>();
while (t instanceof ASTTId tid) {
if (seen.contains(tid.toStr())) return t;
seen.add(tid.toStr());
t = e.find(tid.toStr());
}
return t;
}
}