blob: fc082a5f94fb049f28b950999ff99715ecd3aefe (
plain)
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
|
package org.zwobble.couscous.interpreter.values;
import org.zwobble.couscous.interpreter.InterpreterTypes;
import org.zwobble.couscous.interpreter.errors.NoSuchField;
import org.zwobble.couscous.interpreter.errors.UnboundField;
import java.util.HashMap;
import java.util.Map;
public class InterpreterFields {
// TODO: distinguish static and non-static fields
private final ConcreteType type;
private final Map<String, InterpreterValue> fields;
public InterpreterFields(ConcreteType type) {
this.type = type;
this.fields = new HashMap<>();
}
public InterpreterValue getField(String fieldName) {
type.getField(fieldName).orElseThrow(() -> new NoSuchField(fieldName));
if (fields.containsKey(fieldName)) {
return fields.get(fieldName);
} else {
throw new UnboundField(fieldName);
}
}
public void setField(String fieldName, InterpreterValue value) {
FieldValue field = type.getField(fieldName)
.orElseThrow(() -> new NoSuchField(fieldName));
InterpreterTypes.checkIsInstance(field.getType(), value);
fields.put(fieldName, value);
}
}
|