summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/expression.hpp
blob: 20fe5be8f86d98132b3475442553a43677876fa8 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#pragma once

#include <array>
#include <vector>
#include <memory>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/style/function/type.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/conversion.hpp>

namespace mbgl {

class GeometryTileFeature;


namespace style {
namespace expression {

using OutputValue = variant<
    float,
    std::string,
    mbgl::Color,
    std::array<float, 2>,
    std::array<float, 4>>;

struct EvaluationError {
    std::string message;
};

struct CompileError {
    std::string message;
    std::string key;
};

class Expression {
public:
    Expression(std::string key_, type::Type type_) : key(key_), type(type_) {}
    virtual ~Expression() {}
    
    virtual optional<OutputValue> evaluate(float, const GeometryTileFeature&, EvaluationError& e) const = 0;
    
    // Exposed for use with pure Feature objects (e.g. beyond the context of tiled map data).
    optional<OutputValue> evaluate(float, const Feature&, EvaluationError&) const;
    
    type::Type getType() const {
        return type;
    }
    
    type::ValueType getResultType() const {
        return type.match(
            [&] (const type::Lambda& lambdaType) {
                return lambdaType.getResult();
            },
            [&] (const auto& t) -> type::ValueType { return t; }
        );
    }
    
    bool isFeatureConstant() {
        return true;
    }
    
    bool isZoomConstant() {
        return true;
    }
    
private:
    std::string key;
    type::Type type;
};

using ParseResult = variant<CompileError, std::unique_ptr<Expression>>;
template <class V>
ParseResult parseExpression(const V& value, const ParsingContext& context);

using namespace mbgl::style::conversion;

class LiteralExpression : public Expression {
public:
    LiteralExpression(std::string key, float value_) : Expression(key, type::Primitive::Number), value(value_) {}
    LiteralExpression(std::string key, const std::string& value_) : Expression(key, type::Primitive::String), value(value_) {}
    LiteralExpression(std::string key, const mbgl::Color& value_) : Expression(key, type::Primitive::Color), value(value_) {}
    LiteralExpression(std::string key) : Expression(key, type::Primitive::Null) {}
    
    optional<OutputValue> evaluate(float, const GeometryTileFeature&, EvaluationError&) const override {
        return value;
    }
    
    template <class V>
    static ParseResult parse(const V& value, const ParsingContext& ctx) {
        if (isUndefined(value))
            return std::make_unique<LiteralExpression>(ctx.key());
        
        if (isObject(value)) {
            return CompileError {ctx.key(), "Unimplemented: object literals"};
        }
        
        if (isArray(value)) {
            return CompileError {ctx.key(), "Unimplemented: array literals"};
        }
        
        optional<mbgl::Value> v = toValue(value);
        assert(v);
        return v->match(
            [&] (std::string s) { return std::make_unique<LiteralExpression>(ctx.key(), s); },
            [&] (bool b) { return std::make_unique<LiteralExpression>(ctx.key(), b); },
            [&] (auto f) {
                auto number = numericValue<float>(f);
                assert(number);
                return std::make_unique<LiteralExpression>(ctx.key(), *number);
            }
        );
    }
    
private:
    optional<OutputValue> value;
};

class LambdaExpression : public Expression {
public:
    using Args = std::vector<std::unique_ptr<Expression>>;

    LambdaExpression(std::string key,
                    std::string name_,
                    Args args_,
                    type::Lambda type) :
        Expression(key, type),
        args(std::move(args_)),
        name(name_)
    {}
    
    template <class V>
    static variant<CompileError, Args> parseArgs(const V& value, const ParsingContext& ctx) {
        assert(isArray(value));
        auto length = arrayLength(value);
        Args args;
        for(size_t i = 1; i < length; i++) {
            const auto& arg = arrayMember(value, i);
            auto parsedArg = parseExpression(arg, ParsingContext(ctx, i, {}));
            if (parsedArg.template is<std::unique_ptr<Expression>>()) {
                args.push_back(std::move(parsedArg.template get<std::unique_ptr<Expression>>()));
            } else {
                return parsedArg.template get<CompileError>();
            }
        }
        return std::move(args);
    }
    
protected:
    Args args;
private:
    std::string name;
};

template <typename T, typename Rfunc>
optional<OutputValue> evaluateBinaryOperator(float z,
                                            const GeometryTileFeature& f,
                                            EvaluationError& e,
                                            const LambdaExpression::Args& args,
                                            optional<T> initial,
                                            Rfunc reduce)
{
    optional<T> memo = initial;
    for(const auto& arg : args) {
        auto argValue = arg->evaluate(z, f, e);
        if (!argValue) return {};
        if (!memo) memo = {argValue->get<T>()};
        else memo = reduce(*memo, argValue->get<T>());
    }
    return {*memo};
}

class PlusExpression : public LambdaExpression {
public:
    PlusExpression(std::string key, Args args) :
        LambdaExpression(key, "+", std::move(args),
                        {type::Primitive::Number, {type::NArgs({type::Primitive::Number})}})
    {}
    
    optional<OutputValue> evaluate(float zoom, const GeometryTileFeature& feature, EvaluationError& error) const override {
        return evaluateBinaryOperator<float>(zoom, feature, error, args,
            {}, [](float memo, float next) { return memo + next; });
    }
    
    template <class V>
    static ParseResult parse(const V& value, const ParsingContext& ctx) {
        auto args = LambdaExpression::parseArgs(value, ctx);
        if (args.template is<LambdaExpression::Args>()) {
            return std::make_unique<PlusExpression>(ctx.key(), std::move(args.template get<Args>()));
        } else {
            return args.template get<CompileError>();
        }
    }
};

class TimesExpression : public LambdaExpression {
public:
    TimesExpression(std::string key, Args args) :
        LambdaExpression(key, "*", std::move(args),
                        {type::Primitive::Number, {type::NArgs({type::Primitive::Number})}})
    {}
    
    optional<OutputValue> evaluate(float zoom, const GeometryTileFeature& feature, EvaluationError& error) const override {
        return evaluateBinaryOperator<float>(zoom, feature, error, args,
            {}, [](float memo, float next) { return memo * next; });
    }
    
    template <class V>
    static ParseResult parse(const V& value, const ParsingContext& ctx) {
        auto args = LambdaExpression::parseArgs(value, ctx);
        if (args.template is<LambdaExpression::Args>()) {
            return std::make_unique<TimesExpression>(ctx.key(), std::move(args.template get<Args>()));
        } else {
            return args.template get<CompileError>();
        }
    }
};

class MinusExpression : public LambdaExpression {
public:
    MinusExpression(std::string key, Args args) :
        LambdaExpression(key, "-", std::move(args),
                        {type::Primitive::Number, {type::Primitive::Number, type::Primitive::Number}})
    {}
    
    optional<OutputValue> evaluate(float zoom, const GeometryTileFeature& feature, EvaluationError& error) const override {
        return evaluateBinaryOperator<float>(zoom, feature, error, args,
            {}, [](float memo, float next) { return memo - next; });
    }
    
    template <class V>
    static ParseResult parse(const V& value, const ParsingContext& ctx) {
        auto args = LambdaExpression::parseArgs(value, ctx);
        if (args.template is<LambdaExpression::Args>()) {
            return std::make_unique<MinusExpression>(ctx.key(), std::move(args.template get<Args>()));
        } else {
            return args.template get<CompileError>();
        }
    }
};

class DivideExpression : public LambdaExpression {
public:
    DivideExpression(std::string key, Args args) :
        LambdaExpression(key, "/", std::move(args),
                        {type::Primitive::Number, {type::Primitive::Number, type::Primitive::Number}})
    {}
    
    optional<OutputValue> evaluate(float zoom, const GeometryTileFeature& feature, EvaluationError& error) const override {
        return evaluateBinaryOperator<float>(zoom, feature, error, args,
            {}, [](float memo, float next) { return memo / next; });
    }
    
    template <class V>
    static ParseResult parse(const V& value, const ParsingContext& ctx) {
        auto args = LambdaExpression::parseArgs(value, ctx);
        if (args.template is<LambdaExpression::Args>()) {
            return std::make_unique<DivideExpression>(ctx.key(), std::move(args.template get<Args>()));
        } else {
            return args.template get<CompileError>();
        }
    }
};



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