-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTWhile.java
More file actions
40 lines (35 loc) · 1.13 KB
/
Copy pathASTWhile.java
File metadata and controls
40 lines (35 loc) · 1.13 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
public class ASTWhile implements ASTNode {
ASTNode cond, body;
public ASTWhile(ASTNode cond, ASTNode body) {
this.cond = cond;
this.body = body;
}
@Override
public IValue eval(Environment<IValue> e) throws InterpreterError
{
while (true) {
IValue condVal = cond.eval(e);
if (condVal instanceof VBool) {
if (!((VBool)condVal).getval()) {
return new VBool(false);
}
body.eval(e);
} else {
throw new InterpreterError("condition of while must be a boolean");
}
}
}
@Override
public ASTType typecheck(Environment<ASTType> e) throws TypeCheckError, InterpreterError {
ASTType condType = cond.typecheck(e);
if (!(condType instanceof ASTTBool)) {
throw new TypeCheckError("while: condition not bool");
}
body.typecheck(e);
return ASTTBool.tbool;
}
@Override
public boolean isFreeOutsideLambda(String id) {
return cond.isFreeOutsideLambda(id) || body.isFreeOutsideLambda(id);
}
}