aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/interpreter/values/BoxedIntegerInterpreterValue.java
blob: 3ffa2442a847a1370309bab05c61e3557611a88b (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
package org.zwobble.couscous.interpreter.values;

import org.zwobble.couscous.ast.Operator;
import org.zwobble.couscous.values.IntegerValue;
import org.zwobble.couscous.values.ObjectValues;
import org.zwobble.couscous.values.StringValue;

import static java.util.Arrays.asList;

public class BoxedIntegerInterpreterValue {
    public static final ConcreteType TYPE = ConcreteType.classBuilder("java.lang.Integer")
        .field("value", IntegerValue.REF)

        .staticMethod("parseInt", asList(StringValue.REF),
            (environment, arguments) -> {
                StringInterpreterValue value = (StringInterpreterValue)arguments.get(0);
                return new IntegerInterpreterValue(Integer.parseInt(value.getValue()));
            })

        .method(Operator.EQUALS.getMethodName(), asList(ObjectValues.OBJECT),
            (environment, arguments) -> {
                InterpreterValue right = arguments.getPositionalArguments().get(0);
                if (right.getType().getName().equals(ObjectValues.BOXED_INT)) {
                    IntegerInterpreterValue leftValue = (IntegerInterpreterValue)arguments.getReceiver().getField("value");
                    IntegerInterpreterValue rightValue = (IntegerInterpreterValue)right.getField("value");
                    return new BooleanInterpreterValue(leftValue.getValue() == rightValue.getValue());
                } else {
                    return new BooleanInterpreterValue(false);
                }
            })
        .build();
}