This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Improve typing for !=, == expressions
- Loading branch information
1 parent
43a9bd3
commit a13027c
Showing
6 changed files
with
119 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <mbgl/style/expression/expression.hpp> | ||
#include <mbgl/style/expression/parsing_context.hpp> | ||
#include <mbgl/style/conversion.hpp> | ||
|
||
#include <memory> | ||
|
||
namespace mbgl { | ||
namespace style { | ||
namespace expression { | ||
|
||
class Equals : public Expression { | ||
public: | ||
Equals(std::unique_ptr<Expression> lhs, std::unique_ptr<Expression> rhs, bool negate); | ||
|
||
static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&); | ||
|
||
void eachChild(const std::function<void(const Expression&)>& visit) const override; | ||
bool operator==(const Expression&) const override; | ||
EvaluationResult evaluate(const EvaluationContext&) const override; | ||
|
||
private: | ||
std::unique_ptr<Expression> lhs; | ||
std::unique_ptr<Expression> rhs; | ||
bool negate; | ||
}; | ||
|
||
} // namespace expression | ||
} // namespace style | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <mbgl/style/expression/equals.hpp> | ||
|
||
namespace mbgl { | ||
namespace style { | ||
namespace expression { | ||
|
||
Equals::Equals(std::unique_ptr<Expression> lhs_, std::unique_ptr<Expression> rhs_, bool negate_) | ||
: Expression(type::Boolean), | ||
lhs(std::move(lhs_)), | ||
rhs(std::move(rhs_)), | ||
negate(negate_) { | ||
} | ||
|
||
EvaluationResult Equals::evaluate(const EvaluationContext& params) const { | ||
EvaluationResult lhsResult = lhs->evaluate(params); | ||
if (!lhsResult) return lhsResult; | ||
|
||
EvaluationResult rhsResult = rhs->evaluate(params); | ||
if (!rhsResult) return lhsResult; | ||
|
||
bool result = *lhsResult == *rhsResult; | ||
if (negate) { | ||
result = !result; | ||
} | ||
return result; | ||
} | ||
|
||
void Equals::eachChild(const std::function<void(const Expression&)>& visit) const { | ||
visit(*lhs); | ||
visit(*rhs); | ||
} | ||
|
||
bool Equals::operator==(const Expression& e) const { | ||
if (auto eq = dynamic_cast<const Equals*>(&e)) { | ||
return eq->negate == negate && *eq->lhs == *lhs && *eq->rhs == *rhs; | ||
} | ||
return false; | ||
} | ||
|
||
static bool isComparableType(const type::Type& type) { | ||
return type == type::String || | ||
type == type::Number || | ||
type == type::Boolean || | ||
type == type::Null; | ||
} | ||
|
||
using namespace mbgl::style::conversion; | ||
ParseResult Equals::parse(const Convertible& value, ParsingContext& ctx) { | ||
std::size_t length = arrayLength(value); | ||
|
||
if (length != 3) { | ||
ctx.error("Expected two arguments."); | ||
return ParseResult(); | ||
} | ||
|
||
bool negate = toString(arrayMember(value, 0)) == std::string("!="); | ||
|
||
ParseResult lhs = ctx.parse(arrayMember(value, 1), 1, {type::Value}); | ||
if (!lhs) return ParseResult(); | ||
|
||
ParseResult rhs = ctx.parse(arrayMember(value, 2), 2, {type::Value}); | ||
if (!rhs) return ParseResult(); | ||
|
||
type::Type lhsType = (*lhs)->getType(); | ||
type::Type rhsType = (*rhs)->getType(); | ||
|
||
if (!isComparableType(lhsType) && !isComparableType(rhsType)) { | ||
ctx.error("Expected at least one argument to be a string, number, boolean, or null, but found (" + | ||
toString(lhsType) + ", " + toString(rhsType) + ") instead."); | ||
return ParseResult(); | ||
} | ||
|
||
if (lhsType != rhsType && lhsType != type::Value && rhsType != type::Value) { | ||
ctx.error("Cannot compare " + toString(lhsType) + " and " + toString(rhsType) + "."); | ||
return ParseResult(); | ||
} | ||
|
||
return ParseResult(std::make_unique<Equals>(std::move(*lhs), std::move(*rhs), negate)); | ||
} | ||
|
||
} // namespace expression | ||
} // namespace style | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters