-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTAnd.java
More file actions
41 lines (31 loc) · 1.35 KB
/
Copy pathASTAnd.java
File metadata and controls
41 lines (31 loc) · 1.35 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
public class ASTAnd implements ASTNode {
ASTNode lhs, rhs;
public ASTAnd(ASTNode l, ASTNode r) {
lhs = l;
rhs = r;
}
@Override
public IValue eval(Environment<IValue> e) throws InterpreterError {
IValue v1 = lhs.eval(e);
if (!(v1 instanceof VBool))
throw new InterpreterError("illegal types to && operator");
if (!((VBool)v1).getval()) // short-circuit
return new VBool(false);
IValue v2 = rhs.eval(e);
if (!(v2 instanceof VBool))
throw new InterpreterError("illegal types to && operator");
return new VBool(((VBool)v2).getval());
}
@Override
public ASTType typecheck(Environment<ASTType> e) throws TypeCheckError, InterpreterError {
ASTType lhst = lhs.typecheck(e);
ASTType rhst = rhs.typecheck(e);
if (!(lhst instanceof ASTTBool) || !(rhst instanceof ASTTBool))
throw new TypeCheckError("&&: arg type not bool");
return ASTTBool.tbool;
}
@Override
public boolean isFreeOutsideLambda(String id) {
return lhs.isFreeOutsideLambda(id) || rhs.isFreeOutsideLambda(id);
}
}