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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
package org.zwobble.couscous.interpreter.values;
import java.util.Optional;
import java.util.function.BiFunction;
import org.zwobble.couscous.ast.Operator;
import org.zwobble.couscous.interpreter.*;
import org.zwobble.couscous.values.IntegerValue;
import org.zwobble.couscous.values.ObjectValues;
import org.zwobble.couscous.values.PrimitiveValue;
import org.zwobble.couscous.values.PrimitiveValues;
import static java.util.Arrays.asList;
public final class IntegerInterpreterValue implements InterpreterValue {
public static final ConcreteType TYPE = ConcreteType.builder(IntegerInterpreterValue.class, IntegerValue.REF)
.method(Operator.ADD.getMethodName(), asList(IntegerValue.REF),
infixReturningInteger((left, right) -> left + right))
.method(Operator.SUBTRACT.getMethodName(), asList(IntegerValue.REF),
infixReturningInteger((left, right) -> left - right))
.method(Operator.MULTIPLY.getMethodName(), asList(IntegerValue.REF),
infixReturningInteger((left, right) -> left * right))
.method(Operator.DIVIDE.getMethodName(), asList(IntegerValue.REF),
infixReturningInteger((left, right) -> left / right))
.method(Operator.MOD.getMethodName(), asList(IntegerValue.REF),
infixReturningInteger((left, right) -> left % right))
.method(Operator.EQUALS.getMethodName(), asList(ObjectValues.OBJECT),
(environment, arguments) -> {
InterpreterValue right = arguments.getPositionalArguments().get(0);
if (right instanceof IntegerInterpreterValue) {
return integerEquals(arguments, ((IntegerInterpreterValue)right));
} else {
return new BooleanInterpreterValue(false);
}
})
.method(Operator.EQUALS.getMethodName(), asList(IntegerValue.REF),
(environment, arguments) -> {
IntegerInterpreterValue right = (IntegerInterpreterValue)arguments.getPositionalArguments().get(0);
return integerEquals(arguments, right);
})
.method(Operator.NOT_EQUALS.getMethodName(), asList(IntegerValue.REF),
infixReturningBoolean((left, right) -> !left.equals(right)))
.method(Operator.GREATER_THAN.getMethodName(), asList(IntegerValue.REF),
infixReturningBoolean((left, right) -> left > right))
.method(Operator.GREATER_THAN_OR_EQUAL.getMethodName(), asList(IntegerValue.REF),
infixReturningBoolean((left, right) -> left >= right))
.method(Operator.LESS_THAN.getMethodName(), asList(IntegerValue.REF),
infixReturningBoolean((left, right) -> left < right))
.method(Operator.LESS_THAN_OR_EQUAL.getMethodName(), asList(IntegerValue.REF),
infixReturningBoolean((left, right) -> left <= right))
.build();
private static BooleanInterpreterValue integerEquals(MethodCallArguments<IntegerInterpreterValue> arguments, IntegerInterpreterValue right) {
return new BooleanInterpreterValue(arguments.getReceiver().getValue() == right.getValue());
}
private static
BiFunction<Environment, MethodCallArguments<IntegerInterpreterValue>, InterpreterValue>
infixReturningInteger(BiFunction<Integer, Integer, Integer> func) {
return infix((left, right) -> new IntegerInterpreterValue(func.apply(left, right)));
}
private static
BiFunction<Environment, MethodCallArguments<IntegerInterpreterValue>, InterpreterValue>
infixReturningBoolean(BiFunction<Integer, Integer, Boolean> func) {
return infix((left, right) -> new BooleanInterpreterValue(func.apply(left, right)));
}
private static
BiFunction<Environment, MethodCallArguments<IntegerInterpreterValue>, InterpreterValue>
infix(BiFunction<Integer, Integer, InterpreterValue> func) {
return (environment, arguments) -> {
IntegerInterpreterValue left = arguments.getReceiver();
IntegerInterpreterValue right = (IntegerInterpreterValue) arguments.get(0);
return func.apply(left.getValue(), right.getValue());
};
}
private final int value;
@Override
public ConcreteType getType() {
return TYPE;
}
@Override
public Optional<PrimitiveValue> toPrimitiveValue() {
return Optional.of(PrimitiveValues.value(value));
}
@Override
public InterpreterValue getField(String fieldName) {
throw new NoSuchField(fieldName);
}
@Override
public void setField(String fieldName, InterpreterValue value) {
throw new NoSuchField(fieldName);
}
public IntegerInterpreterValue(final int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
public InterpreterValue box() {
ObjectInterpreterValue box = new ObjectInterpreterValue(BoxedIntegerInterpreterValue.TYPE);
box.setField("value", this);
return box;
}
@java.lang.Override
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof IntegerInterpreterValue)) return false;
final IntegerInterpreterValue other = (IntegerInterpreterValue)o;
if (this.getValue() != other.getValue()) return false;
return true;
}
@java.lang.Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.getValue();
return result;
}
@java.lang.Override
public java.lang.String toString() {
return "IntegerInterpreterValue(value=" + this.getValue() + ")";
}
}
|