[core] Implement Expression::serialize()

Issue #10714
- Each expression stores its operator as a string, and default serialization is [operator, serialize(child1), ...]
- Custom implementations of `serialize` for Expression types that don't follow the pattern
- expression::Value -> mbgl::Value converter
- node_expression bindings to expose `serialize`
This commit is contained in:
Chris Loer
2018-02-08 15:23:29 -08:00
committed by Chris Loer
parent 341eb7645f
commit 8635dab4c3
30 changed files with 262 additions and 40 deletions

View File

@@ -33,6 +33,9 @@ public:
std::vector<optional<Value>> possibleOutputs() const override {
return input->possibleOutputs();
}
mbgl::Value serialize() const override;
std::string getOperator() const override { return "array"; }
private:
std::unique_ptr<Expression> input;

View File

@@ -26,6 +26,8 @@ public:
bool operator==(const Expression& e) const override;
std::vector<optional<Value>> possibleOutputs() const override;
std::string getOperator() const override;
private:
std::vector<std::unique_ptr<Expression>> inputs;

View File

@@ -31,6 +31,8 @@ public:
std::vector<optional<Value>> possibleOutputs() const override {
return { nullopt };
}
std::string getOperator() const override { return "at"; }
private:
std::unique_ptr<Expression> index;

View File

@@ -23,6 +23,7 @@ public:
bool operator==(const Expression& e) const override;
std::vector<optional<Value>> possibleOutputs() const override;
std::string getOperator() const override { return "any"; }
private:
std::vector<std::unique_ptr<Expression>> inputs;
};
@@ -41,6 +42,7 @@ public:
bool operator==(const Expression& e) const override;
std::vector<optional<Value>> possibleOutputs() const override;
std::string getOperator() const override { return "all"; }
private:
std::vector<std::unique_ptr<Expression>> inputs;
};

View File

@@ -28,6 +28,7 @@ public:
std::vector<optional<Value>> possibleOutputs() const override;
std::string getOperator() const override { return "case"; }
private:
std::vector<Branch> branches;
std::unique_ptr<Expression> otherwise;

View File

@@ -38,6 +38,7 @@ public:
return args.at(i).get();
}
std::string getOperator() const override { return "coalesce"; }
private:
Args args;
};

View File

@@ -28,6 +28,7 @@ public:
std::vector<optional<Value>> possibleOutputs() const override;
std::string getOperator() const override;
private:
EvaluationResult (*coerceSingleValue) (const Value& v);
std::vector<std::unique_ptr<Expression>> inputs;

View File

@@ -40,14 +40,16 @@ namespace detail {
// each CompoundExpression definition's type::Type data from the type of its
// "evaluate" function.
struct SignatureBase {
SignatureBase(type::Type result_, variant<std::vector<type::Type>, VarargsType> params_) :
SignatureBase(type::Type result_, variant<std::vector<type::Type>, VarargsType> params_, std::string name_) :
result(std::move(result_)),
params(std::move(params_))
params(std::move(params_)),
name(std::move(name_))
{}
virtual ~SignatureBase() = default;
virtual std::unique_ptr<Expression> makeExpression(const std::string& name, std::vector<std::unique_ptr<Expression>>) const = 0;
virtual std::unique_ptr<Expression> makeExpression(std::vector<std::unique_ptr<Expression>>) const = 0;
type::Type result;
variant<std::vector<type::Type>, VarargsType> params;
std::string name;
};
} // namespace detail
@@ -111,6 +113,10 @@ public:
}
return false;
}
std::string getOperator() const override {
return signature.name;
}
private:
Signature signature;
@@ -128,8 +134,7 @@ struct CompoundExpressionRegistry {
ParseResult parseCompoundExpression(const std::string name, const mbgl::style::conversion::Convertible& value, ParsingContext& ctx);
ParseResult createCompoundExpression(const std::string& name,
const CompoundExpressionRegistry::Definition& definition,
ParseResult createCompoundExpression(const CompoundExpressionRegistry::Definition& definition,
std::vector<std::unique_ptr<Expression>> args,
ParsingContext& ctx);

View File

@@ -21,6 +21,7 @@ public:
EvaluationResult evaluate(const EvaluationContext&) const override;
std::vector<optional<Value>> possibleOutputs() const override;
std::string getOperator() const override { return negate ? "!=" : "=="; }
private:
std::unique_ptr<Expression> lhs;
std::unique_ptr<Expression> rhs;

View File

@@ -135,6 +135,17 @@ public:
* complete set of outputs is statically undecidable.
*/
virtual std::vector<optional<Value>> possibleOutputs() const = 0;
virtual mbgl::Value serialize() const {
std::vector<mbgl::Value> serialized;
serialized.emplace_back(getOperator());
eachChild([&](const Expression &child) {
serialized.emplace_back(child.serialize());
});
return serialized;
};
virtual std::string getOperator() const = 0;
protected:
template <typename T>

View File

@@ -185,6 +185,9 @@ public:
}
return false;
}
mbgl::Value serialize() const override;
std::string getOperator() const override { return "interpolate"; }
};
} // namespace expression

View File

@@ -39,6 +39,8 @@ public:
return result.get();
}
mbgl::Value serialize() const override;
std::string getOperator() const override { return "let"; }
private:
Bindings bindings;
std::unique_ptr<Expression> result;
@@ -66,6 +68,8 @@ public:
std::vector<optional<Value>> possibleOutputs() const override;
mbgl::Value serialize() const override;
std::string getOperator() const override { return "var"; }
private:
std::string name;
std::shared_ptr<Expression> value;

View File

@@ -12,8 +12,16 @@ namespace expression {
class Literal : public Expression {
public:
Literal(Value value_) : Expression(typeOf(value_)), value(value_) {}
Literal(type::Array type_, std::vector<Value> value_) : Expression(type_), value(value_) {}
Literal(Value value_)
: Expression(typeOf(value_))
, value(value_)
{}
Literal(type::Array type_, std::vector<Value> value_)
: Expression(type_)
, value(value_)
{}
EvaluationResult evaluate(const EvaluationContext&) const override {
return value;
}
@@ -33,6 +41,8 @@ public:
return {{ value }};
}
mbgl::Value serialize() const override;
std::string getOperator() const override { return "literal"; }
private:
Value value;
};

View File

@@ -32,7 +32,9 @@ public:
bool operator==(const Expression& e) const override;
std::vector<optional<Value>> possibleOutputs() const override;
mbgl::Value serialize() const override;
std::string getOperator() const override { return "match"; }
private:
std::unique_ptr<Expression> input;
Branches branches;

View File

@@ -38,6 +38,8 @@ public:
static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx);
mbgl::Value serialize() const override;
std::string getOperator() const override { return "step"; }
private:
const std::unique_ptr<Expression> input;
const std::map<double, std::unique_ptr<Expression>> stops;

View File

@@ -110,6 +110,7 @@ struct ValueConverter<float> {
template<>
struct ValueConverter<mbgl::Value> {
static Value toExpressionValue(const mbgl::Value& value);
static mbgl::Value fromExpressionValue(const Value& value);
};
template <typename T, std::size_t N>

View File

@@ -49,6 +49,7 @@ public:
return {};
}
std::string getOperator() const override { return "error"; }
private:
std::string message;
};