blob: f0ecbd1d32342a8f72c87c14070edcb2670458be (
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
37
38
39
40
41
42
43
44
45
46
47
48
|
package org.zwobble.couscous.interpreter.values;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.zwobble.couscous.interpreter.NoSuchField;
import org.zwobble.couscous.interpreter.UnexpectedValueType;
import org.zwobble.couscous.values.PrimitiveValue;
import lombok.val;
public class ObjectInterpreterValue implements InterpreterValue {
private final ConcreteType type;
private final Map<String, InterpreterValue> fields;
public ObjectInterpreterValue(ConcreteType type) {
this.type = type;
this.fields = new HashMap<>();
}
@Override
public ConcreteType getType() {
return type;
}
@Override
public Optional<PrimitiveValue> toPrimitiveValue() {
return Optional.empty();
}
@Override
public InterpreterValue getField(String fieldName) {
if (fields.containsKey(fieldName)) {
return fields.get(fieldName);
} else {
throw new UnsupportedOperationException();
}
}
public void setField(String fieldName, InterpreterValue value) {
val field = type.getField(fieldName).orElseThrow(() -> new NoSuchField(fieldName));
if (!field.getType().equals(value.getType().getName())) {
throw new UnexpectedValueType(field.getType(), value.getType().getName());
}
fields.put(fieldName, value);
}
}
|