-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTApp.java
More file actions
43 lines (35 loc) · 1.27 KB
/
Copy pathASTApp.java
File metadata and controls
43 lines (35 loc) · 1.27 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
41
42
43
public class ASTApp implements ASTNode {
ASTNode lhs, rhs;
public ASTApp(ASTNode l, ASTNode r) {
lhs = l;
rhs = r;
}
@Override
public IValue eval(Environment<IValue> e) throws InterpreterError
{
IValue fun = lhs.eval(e);
if (fun instanceof VClosure clo) {
IValue arg = rhs.eval(e);
return clo.call(arg);
} else {
throw new InterpreterError("app: closure expected, found " + fun);
}
}
@Override
public ASTType typecheck(Environment<ASTType> e) throws TypeCheckError, InterpreterError {
ASTType funType = lhs.typecheck(e);
ASTType argType = rhs.typecheck(e);
if (!(funType instanceof ASTTArrow arrow)) {
throw new TypeCheckError("app: not a function, got " + funType.toStr());
}
// C <: A
if (!argType.subtypeOf(arrow.getDom(), e)) {
throw new TypeCheckError("app: argument type mismatch: expected " + arrow.getDom().toStr() + " but got " + argType.toStr());
}
return ASTTypeDef.unfold(arrow.getCodom(), e);
}
@Override
public boolean isFreeOutsideLambda(String id) {
return lhs.isFreeOutsideLambda(id) || rhs.isFreeOutsideLambda(id);
}
}