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

import org.zwobble.couscous.ast.Operator;
import org.zwobble.couscous.interpreter.NoSuchField;
import org.zwobble.couscous.values.BooleanValue;
import org.zwobble.couscous.values.PrimitiveValue;
import org.zwobble.couscous.values.PrimitiveValues;

import java.util.Optional;

import static org.zwobble.couscous.util.ExtraLists.list;

public final class BooleanInterpreterValue implements InterpreterValue {
    public static final BooleanInterpreterValue TRUE = new BooleanInterpreterValue(true);
    public static final BooleanInterpreterValue FALSE = new BooleanInterpreterValue(false);

    public static BooleanInterpreterValue of(final boolean value) {
        return value ? TRUE : FALSE;
    }

    private static final ConcreteType TYPE = ConcreteType.builder(BooleanInterpreterValue.class, "boolean")
        
        .method("negate", list(), (environment, arguments) ->
            of(!arguments.getReceiver().getValue()))

        .method(Operator.BOOLEAN_AND.getMethodName(), list(BooleanValue.REF), (environment, arguments) -> {
            BooleanInterpreterValue right = (BooleanInterpreterValue)arguments.get(0);
            return of(arguments.getReceiver().getValue() && right.getValue());
        })

        .method(Operator.BOOLEAN_OR.getMethodName(), list(BooleanValue.REF), (environment, arguments) -> {
            BooleanInterpreterValue right = (BooleanInterpreterValue)arguments.get(0);
            return of(arguments.getReceiver().getValue() || right.getValue());
        })

        .build();

    private final boolean value;

    private BooleanInterpreterValue(final boolean value) {
        this.value = value;
    }

    public boolean getValue() {
        return 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);
    }
    
    @java.lang.Override
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (!(o instanceof BooleanInterpreterValue)) return false;
        final BooleanInterpreterValue other = (BooleanInterpreterValue)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() ? 79 : 97);
        return result;
    }
    
    @java.lang.Override
    public java.lang.String toString() {
        return "BooleanInterpreterValue(value=" + this.getValue() + ")";
    }
}