-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASTDeref.java
More file actions
40 lines (35 loc) · 1.21 KB
/
Copy pathASTDeref.java
File metadata and controls
40 lines (35 loc) · 1.21 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 ASTDeref implements ASTNode {
ASTNode exp;
public ASTDeref(ASTNode exp) {
this.exp = exp;
}
@Override
public IValue eval(Environment<IValue> e) throws InterpreterError
{
IValue v = exp.eval(e);
if (v instanceof MemoryManager.VAddress addr) {
try {
return ASTLet.mem.memrd(addr);
} catch (MemoryPanicError ex) {
throw new InterpreterError("mut: " + ex.getMessage());
}
} else if (v instanceof VCell cell) {
return cell.getval();
}
throw new InterpreterError("! operator: reference or addr expected, " + v + " found.");
}
@Override
public ASTType typecheck(Environment<ASTType> env) throws TypeCheckError, InterpreterError {
ASTType t = exp.typecheck(env);
if (t instanceof ASTTRef ref) {
return ASTTypeDef.unfold(ref.getType(), env);
} else if (t instanceof ASTTAddr addr) {
return ASTTypeDef.unfold(addr.getType(), env);
}
throw new TypeCheckError("Deref: expected a reference");
}
@Override
public boolean isFreeOutsideLambda(String id) {
return exp.isFreeOutsideLambda(id);
}
}