summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/case.hpp
blob: 005bb475c6532c05048fe9cfe9d24adddca259bd (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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/conversion.hpp>

namespace mbgl {
namespace style {
namespace expression {

class TypedCase : public TypedExpression {
public:
    using Case = std::pair<std::unique_ptr<TypedExpression>, std::unique_ptr<TypedExpression>>;
    
    TypedCase(std::vector<Case> cases_,
              std::unique_ptr<TypedExpression> otherwise_
    ) : TypedExpression(otherwise_->getType()),
        cases(std::move(cases_)),
        otherwise(std::move(otherwise_))
    {}
    
    bool isFeatureConstant() const override {
        if (!otherwise->isFeatureConstant()) { return false; }
        for (const auto& pair : cases) {
            if (!pair.first->isFeatureConstant() || !pair.second->isFeatureConstant()) {
                return false;
            }
        }
        return true;
    }

    bool isZoomConstant() const override {
        if (!otherwise->isZoomConstant()) { return false; }
        for (const auto& pair : cases) {
            if (!pair.first->isFeatureConstant() || !pair.second->isZoomConstant()) {
                return false;
            }
        }
        return true;
    }
    
    EvaluationResult evaluate(const EvaluationParameters& params) const override {
        for (const auto& casePair : cases) {
            const auto& condition = casePair.first->template evaluate<bool>(params);
            if (!condition) {
                return condition.error();
            }
            if (*condition) {
                return casePair.second->evaluate(params);
            }
        }
        
        return otherwise->evaluate(params);
    }

private:
    std::unique_ptr<TypedExpression> input;
    std::vector<Case> cases;
    std::unique_ptr<TypedExpression> otherwise;
};

class UntypedCase : public UntypedExpression {
public:
    using Cases = std::vector<std::pair<std::unique_ptr<UntypedExpression>, std::unique_ptr<UntypedExpression>>>;
    
    UntypedCase(std::string key,
                 Cases cases_,
                 std::unique_ptr<UntypedExpression> otherwise_)
    :   UntypedExpression(key),
        cases(std::move(cases_)),
        otherwise(std::move(otherwise_))
    {}
    
    template <class V>
    static ParseResult parse(const V& value, const ParsingContext& ctx) {
        using namespace mbgl::style::conversion;
        
        assert(isArray(value));
        auto length = arrayLength(value);
        if (length < 4) {
            return CompileError {
                "Expected at least 3 arguments, but found only " + std::to_string(length - 1) + ".",
                ctx.key()
            };
        }
        
        // Expect even-length array: ["case", 2 * (n pairs)..., otherwise]
        if (length % 2 != 0) {
            return CompileError {
                "Missing final output value for \"case\" expression.",
                ctx.key()
            };
        }
        
        auto otherwise = parseExpression(arrayMember(value, length - 1), ParsingContext(ctx, {length - 1}, {"case"}));
        if (otherwise.template is<CompileError>()) {
            return otherwise;
        }
        
        Cases cases;
        for (size_t i = 1; i + 1 < length; i += 2) {
            auto condition = parseExpression(arrayMember(value, i), ParsingContext(ctx, {i}, {"case"}));
            if (condition.template is<CompileError>()) {
                return condition.template get<CompileError>();
            }

            auto output = parseExpression(arrayMember(value, i + 1), ParsingContext(ctx, {i + 1}, {"case"}));
            if (output.template is<CompileError>()) {
                return output.template get<CompileError>();
            }

            cases.push_back(std::make_pair(
                std::move(condition.template get<std::unique_ptr<UntypedExpression>>()),
                std::move(output.template get<std::unique_ptr<UntypedExpression>>()))
            );
        }
        
        return std::make_unique<UntypedCase>(ctx.key(),
                                             std::move(cases),
                                             std::move(otherwise.template get<std::unique_ptr<UntypedExpression>>()));
    }
    
    TypecheckResult typecheck(std::vector<CompileError>& errors) const override {
        auto checkedOtherwise = otherwise->typecheck(errors);
        if (!checkedOtherwise) {
            return TypecheckResult();
        }

        optional<type::Type> outputType;
        std::vector<TypedCase::Case> checkedCases;
        for (const auto& pair : cases) {
            auto condition = pair.first->typecheck(errors);
            if (!condition) continue;
            auto error = matchType(type::Boolean, (*condition)->getType());
            if (error) {
                errors.push_back({ *error, pair.first->getKey() });
            }

            auto output = pair.second->typecheck(errors);
            if (!output) continue;
            if (!outputType) {
                outputType = (*output)->getType();
            } else {
                error = matchType(*outputType, (*output)->getType());
                if (error) {
                    errors.push_back({ *error, pair.second->getKey() });
                }
            }
            
            if (errors.size() == 0) {
                checkedCases.emplace_back(std::move(*condition), std::move(*output));
            }
        }
        
        auto error = matchType(*outputType, (*checkedOtherwise)->getType());
        if (error) {
            errors.push_back({ *error, otherwise->getKey() });
        }
        
        if (errors.size() > 0) {
            return TypecheckResult();
        }

        return TypecheckResult(std::make_unique<TypedCase>(std::move(checkedCases), std::move(*checkedOtherwise)));
    };
    
private:
    Cases cases;
    std::unique_ptr<UntypedExpression> otherwise;
};

} // namespace expression
} // namespace style
} // namespace mbgl