-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTFun.java
More file actions
44 lines (34 loc) · 1.34 KB
/
Copy pathASTFun.java
File metadata and controls
44 lines (34 loc) · 1.34 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
44
public class ASTFun implements ASTNode {
private final String param;
private final ASTType paramType;
private ASTNode body;
public ASTFun(String param, ASTType paramType, ASTNode body) {
this.param = param;
this.paramType = paramType;
this.body = body;
}
public void setBody(ASTNode b) {
body = b;
}
@Override
public IValue eval(Environment<IValue> e) throws InterpreterError {
return new VClosure(param, body, e);
}
@Override
public ASTType typecheck(Environment<ASTType> e) throws TypeCheckError, InterpreterError {
if (paramType == null) {
throw new TypeCheckError("fn: missing type annotation for parameter '" + param + "'");
}
ASTType resolvedParamType = ASTTypeDef.unfold(paramType, e);
Environment<ASTType> newEnv = e.beginScope();
newEnv.assoc(param, resolvedParamType);
ASTType bodyType = body.typecheck(newEnv);
if (bodyType instanceof ASTTAddr addrType) {
if (addrType.getDepth() > e.getDepth()) {
throw new TypeCheckError("Illegal escape of stack address: pointer to a local variable outlives the function scope!");
}
}
newEnv.endScope();
return new ASTTArrow(resolvedParamType, bodyType);
}
}