From 7e74961eb150f28a56405403335f4904f888e058 Mon Sep 17 00:00:00 2001 From: Michael M Date: Wed, 7 Jun 2017 17:30:07 -0700 Subject: [PATCH] ExpressionParser: expose ExpressionNode directly --- .../ControlReference/ControlReference.cpp | 2 +- .../ControlReference/ExpressionParser.cpp | 76 +++++-------------- .../ControlReference/ExpressionParser.h | 12 ++- 3 files changed, 26 insertions(+), 64 deletions(-) diff --git a/Source/Core/InputCommon/ControlReference/ControlReference.cpp b/Source/Core/InputCommon/ControlReference/ControlReference.cpp index 92577496c5..09bcdaa59f 100644 --- a/Source/Core/InputCommon/ControlReference/ControlReference.cpp +++ b/Source/Core/InputCommon/ControlReference/ControlReference.cpp @@ -37,7 +37,7 @@ void ControlReference::UpdateReference(const ciface::Core::DeviceContainer& devi int ControlReference::BoundCount() const { if (m_parsed_expression) - return m_parsed_expression->num_controls; + return m_parsed_expression->CountNumControls(); else return 0; } diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp b/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp index d2cef8c917..82829ed818 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.cpp @@ -208,17 +208,7 @@ public: } }; -class ExpressionNode -{ -public: - virtual ~ExpressionNode() {} - virtual ControlState GetValue() const { return 0; } - virtual void SetValue(ControlState state) {} - virtual int CountNumControls() const { return 0; } - virtual operator std::string() const { return ""; } -}; - -class ControlExpression : public ExpressionNode +class ControlExpression : public Expression { public: ControlQualifier qualifier; @@ -242,15 +232,15 @@ public: operator std::string() const override { return "`" + static_cast(qualifier) + "`"; } }; -class BinaryExpression : public ExpressionNode +class BinaryExpression : public Expression { public: TokenType op; - std::unique_ptr lhs; - std::unique_ptr rhs; + std::unique_ptr lhs; + std::unique_ptr rhs; - BinaryExpression(TokenType op_, std::unique_ptr&& lhs_, - std::unique_ptr&& rhs_) + BinaryExpression(TokenType op_, std::unique_ptr&& lhs_, + std::unique_ptr&& rhs_) : op(op_), lhs(std::move(lhs_)), rhs(std::move(rhs_)) { } @@ -292,13 +282,13 @@ public: } }; -class UnaryExpression : public ExpressionNode +class UnaryExpression : public Expression { public: TokenType op; - std::unique_ptr inner; + std::unique_ptr inner; - UnaryExpression(TokenType op_, std::unique_ptr&& inner_) + UnaryExpression(TokenType op_, std::unique_ptr&& inner_) : op(op_), inner(std::move(inner_)) { } @@ -354,13 +344,13 @@ Device::Control* ControlFinder::FindControl(ControlQualifier qualifier) const struct ParseResult { - ParseResult(ParseStatus status_, std::unique_ptr&& expr_ = {}) + ParseResult(ParseStatus status_, std::unique_ptr&& expr_ = {}) : status(status_), expr(std::move(expr_)) { } ParseStatus status; - std::unique_ptr expr; + std::unique_ptr expr; }; class Parser @@ -449,7 +439,7 @@ private: if (result.status == ParseStatus::SyntaxError) return result; - std::unique_ptr expr = std::move(result.expr); + std::unique_ptr expr = std::move(result.expr); while (IsBinaryToken(Peek().type)) { Token tok = Chew(); @@ -484,45 +474,19 @@ private: ParseResult Toplevel() { return Binary(); } }; -ControlState Expression::GetValue() const -{ - return node->GetValue(); -} - -void Expression::SetValue(ControlState value) -{ - node->SetValue(value); -} - -Expression::Expression(std::unique_ptr&& node_) -{ - node = std::move(node_); - if (node) - num_controls = node->CountNumControls(); -} - -Expression::~Expression() -{ -} - -static std::pair> -ParseExpressionInner(const std::string& str, ControlFinder& finder) +static ParseResult ParseExpressionInner(const std::string& str, ControlFinder& finder) { if (StripSpaces(str).empty()) - return std::make_pair(ParseStatus::EmptyExpression, nullptr); + return {ParseStatus::EmptyExpression}; Lexer l(str); std::vector tokens; ParseStatus tokenize_status = l.Tokenize(tokens); if (tokenize_status != ParseStatus::Successful) - return std::make_pair(tokenize_status, nullptr); + return {tokenize_status}; ParseResult result = Parser(tokens, finder).Parse(); - if (result.status != ParseStatus::Successful) - return std::make_pair(result.status, nullptr); - - return std::make_pair(ParseStatus::Successful, - std::make_unique(std::move(result.expr))); + return result; } std::pair> ParseExpression(const std::string& str, @@ -539,12 +503,12 @@ std::pair> ParseExpression(const std::s Device::Control* control = finder.FindControl(qualifier); if (control) { - return std::make_pair(ParseStatus::Successful, - std::make_unique(std::make_unique( - qualifier, std::move(device), control))); + return std::make_pair(ParseStatus::Successful, std::make_unique( + qualifier, std::move(device), control)); } - return ParseExpressionInner(str, finder); + ParseResult result = ParseExpressionInner(str, finder); + return std::make_pair(result.status, std::move(result.expr)); } } } diff --git a/Source/Core/InputCommon/ControlReference/ExpressionParser.h b/Source/Core/InputCommon/ControlReference/ExpressionParser.h index 55bf86b951..8fde647390 100644 --- a/Source/Core/InputCommon/ControlReference/ExpressionParser.h +++ b/Source/Core/InputCommon/ControlReference/ExpressionParser.h @@ -47,16 +47,14 @@ private: bool is_input; }; -class ExpressionNode; class Expression { public: - explicit Expression(std::unique_ptr&& node = {}); - ~Expression(); - ControlState GetValue() const; - void SetValue(ControlState state); - int num_controls; - std::unique_ptr node; + virtual ~Expression() = default; + virtual ControlState GetValue() const = 0; + virtual void SetValue(ControlState state) = 0; + virtual int CountNumControls() const = 0; + virtual operator std::string() const = 0; }; enum class ParseStatus