2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
2013-06-14 03:09:55 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2013-06-14 03:09:55 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2016-06-26 03:34:09 +00:00
|
|
|
#include <memory>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <string>
|
2013-06-14 03:09:55 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-10-12 00:48:38 +00:00
|
|
|
#include "InputCommon/ControlReference/ExpressionParser.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
using namespace ciface::Core;
|
|
|
|
|
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
namespace ExpressionParser
|
|
|
|
{
|
|
|
|
enum TokenType
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
TOK_DISCARD,
|
|
|
|
TOK_INVALID,
|
|
|
|
TOK_EOF,
|
|
|
|
TOK_LPAREN,
|
|
|
|
TOK_RPAREN,
|
|
|
|
TOK_AND,
|
|
|
|
TOK_OR,
|
|
|
|
TOK_NOT,
|
|
|
|
TOK_ADD,
|
|
|
|
TOK_CONTROL,
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline std::string OpName(TokenType op)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_AND:
|
|
|
|
return "And";
|
|
|
|
case TOK_OR:
|
|
|
|
return "Or";
|
|
|
|
case TOK_NOT:
|
|
|
|
return "Not";
|
|
|
|
case TOK_ADD:
|
|
|
|
return "Add";
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return "";
|
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Token
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
TokenType type;
|
|
|
|
ControlQualifier qualifier;
|
|
|
|
|
|
|
|
Token(TokenType type_) : type(type_) {}
|
|
|
|
Token(TokenType type_, ControlQualifier qualifier_) : type(type_), qualifier(qualifier_) {}
|
2017-02-26 07:04:16 +00:00
|
|
|
operator std::string() const
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TOK_DISCARD:
|
|
|
|
return "Discard";
|
|
|
|
case TOK_EOF:
|
|
|
|
return "EOF";
|
|
|
|
case TOK_LPAREN:
|
|
|
|
return "(";
|
|
|
|
case TOK_RPAREN:
|
|
|
|
return ")";
|
|
|
|
case TOK_AND:
|
|
|
|
return "&";
|
|
|
|
case TOK_OR:
|
|
|
|
return "|";
|
|
|
|
case TOK_NOT:
|
|
|
|
return "!";
|
|
|
|
case TOK_ADD:
|
|
|
|
return "+";
|
|
|
|
case TOK_CONTROL:
|
|
|
|
return "Device(" + (std::string)qualifier + ")";
|
|
|
|
case TOK_INVALID:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Invalid";
|
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
class Lexer
|
|
|
|
{
|
2013-06-14 03:09:55 +00:00
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string expr;
|
|
|
|
std::string::iterator it;
|
|
|
|
|
|
|
|
Lexer(const std::string& expr_) : expr(expr_) { it = expr.begin(); }
|
|
|
|
bool FetchBacktickString(std::string& value, char otherDelim = 0)
|
|
|
|
{
|
|
|
|
value = "";
|
|
|
|
while (it != expr.end())
|
|
|
|
{
|
|
|
|
char c = *it;
|
|
|
|
++it;
|
|
|
|
if (c == '`')
|
|
|
|
return false;
|
|
|
|
if (c > 0 && c == otherDelim)
|
|
|
|
return true;
|
|
|
|
value += c;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token GetFullyQualifiedControl()
|
|
|
|
{
|
|
|
|
ControlQualifier qualifier;
|
|
|
|
std::string value;
|
|
|
|
|
|
|
|
if (FetchBacktickString(value, ':'))
|
|
|
|
{
|
|
|
|
// Found colon, this is the device name
|
|
|
|
qualifier.has_device = true;
|
|
|
|
qualifier.device_qualifier.FromString(value);
|
|
|
|
FetchBacktickString(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
qualifier.control_name = value;
|
|
|
|
|
|
|
|
return Token(TOK_CONTROL, qualifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
Token GetBarewordsControl(char c)
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
name += c;
|
|
|
|
|
|
|
|
while (it != expr.end())
|
|
|
|
{
|
|
|
|
c = *it;
|
|
|
|
if (!isalpha(c))
|
|
|
|
break;
|
|
|
|
name += c;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlQualifier qualifier;
|
|
|
|
qualifier.control_name = name;
|
|
|
|
return Token(TOK_CONTROL, qualifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
Token NextToken()
|
|
|
|
{
|
|
|
|
if (it == expr.end())
|
|
|
|
return Token(TOK_EOF);
|
|
|
|
|
|
|
|
char c = *it++;
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
return Token(TOK_DISCARD);
|
|
|
|
case '(':
|
|
|
|
return Token(TOK_LPAREN);
|
|
|
|
case ')':
|
|
|
|
return Token(TOK_RPAREN);
|
|
|
|
case '&':
|
|
|
|
return Token(TOK_AND);
|
|
|
|
case '|':
|
|
|
|
return Token(TOK_OR);
|
|
|
|
case '!':
|
|
|
|
return Token(TOK_NOT);
|
|
|
|
case '+':
|
|
|
|
return Token(TOK_ADD);
|
|
|
|
case '`':
|
|
|
|
return GetFullyQualifiedControl();
|
|
|
|
default:
|
|
|
|
if (isalpha(c))
|
|
|
|
return GetBarewordsControl(c);
|
|
|
|
else
|
|
|
|
return Token(TOK_INVALID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus Tokenize(std::vector<Token>& tokens)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
Token tok = NextToken();
|
|
|
|
|
|
|
|
if (tok.type == TOK_DISCARD)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (tok.type == TOK_INVALID)
|
|
|
|
{
|
|
|
|
tokens.clear();
|
2017-02-28 09:58:03 +00:00
|
|
|
return ParseStatus::SyntaxError;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tokens.push_back(tok);
|
|
|
|
|
|
|
|
if (tok.type == TOK_EOF)
|
|
|
|
break;
|
|
|
|
}
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ExpressionNode
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
virtual ~ExpressionNode() {}
|
2017-02-26 07:04:16 +00:00
|
|
|
virtual ControlState GetValue() const { return 0; }
|
2016-06-24 08:43:46 +00:00
|
|
|
virtual void SetValue(ControlState state) {}
|
2017-02-26 07:04:16 +00:00
|
|
|
virtual int CountNumControls() const { return 0; }
|
|
|
|
virtual operator std::string() const { return ""; }
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
2015-02-25 23:41:59 +00:00
|
|
|
class DummyExpression : public ExpressionNode
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
std::string name;
|
2015-02-25 23:41:59 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
DummyExpression(const std::string& name_) : name(name_) {}
|
2017-02-26 07:04:16 +00:00
|
|
|
ControlState GetValue() const override { return 0.0; }
|
2016-06-24 08:43:46 +00:00
|
|
|
void SetValue(ControlState value) override {}
|
2017-02-26 07:04:16 +00:00
|
|
|
int CountNumControls() const override { return 0; }
|
|
|
|
operator std::string() const override { return "`" + name + "`"; }
|
2015-02-25 23:41:59 +00:00
|
|
|
};
|
|
|
|
|
2013-06-14 03:09:55 +00:00
|
|
|
class ControlExpression : public ExpressionNode
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
ControlQualifier qualifier;
|
|
|
|
Device::Control* control;
|
|
|
|
|
2016-07-14 15:45:59 +00:00
|
|
|
ControlExpression(ControlQualifier qualifier_, std::shared_ptr<Device> device,
|
|
|
|
Device::Control* control_)
|
|
|
|
: qualifier(qualifier_), control(control_), m_device(device)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
ControlState GetValue() const override { return control->ToInput()->GetState(); }
|
2016-10-12 00:48:38 +00:00
|
|
|
void SetValue(ControlState value) override { control->ToOutput()->SetState(value); }
|
2017-02-26 07:04:16 +00:00
|
|
|
int CountNumControls() const override { return 1; }
|
|
|
|
operator std::string() const override { return "`" + (std::string)qualifier + "`"; }
|
2016-07-14 15:45:59 +00:00
|
|
|
private:
|
|
|
|
std::shared_ptr<Device> m_device;
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BinaryExpression : public ExpressionNode
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
TokenType op;
|
|
|
|
ExpressionNode* lhs;
|
|
|
|
ExpressionNode* rhs;
|
|
|
|
|
|
|
|
BinaryExpression(TokenType op_, ExpressionNode* lhs_, ExpressionNode* rhs_)
|
|
|
|
: op(op_), lhs(lhs_), rhs(rhs_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~BinaryExpression()
|
|
|
|
{
|
|
|
|
delete lhs;
|
|
|
|
delete rhs;
|
|
|
|
}
|
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
ControlState GetValue() const override
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
ControlState lhsValue = lhs->GetValue();
|
|
|
|
ControlState rhsValue = rhs->GetValue();
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_AND:
|
|
|
|
return std::min(lhsValue, rhsValue);
|
|
|
|
case TOK_OR:
|
|
|
|
return std::max(lhsValue, rhsValue);
|
|
|
|
case TOK_ADD:
|
|
|
|
return std::min(lhsValue + rhsValue, 1.0);
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValue(ControlState value) override
|
|
|
|
{
|
|
|
|
// Don't do anything special with the op we have.
|
|
|
|
// Treat "A & B" the same as "A | B".
|
|
|
|
lhs->SetValue(value);
|
|
|
|
rhs->SetValue(value);
|
|
|
|
}
|
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
int CountNumControls() const override
|
|
|
|
{
|
|
|
|
return lhs->CountNumControls() + rhs->CountNumControls();
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::string() const override
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
return OpName(op) + "(" + (std::string)(*lhs) + ", " + (std::string)(*rhs) + ")";
|
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class UnaryExpression : public ExpressionNode
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
TokenType op;
|
|
|
|
ExpressionNode* inner;
|
|
|
|
|
|
|
|
UnaryExpression(TokenType op_, ExpressionNode* inner_) : op(op_), inner(inner_) {}
|
|
|
|
virtual ~UnaryExpression() { delete inner; }
|
2017-02-26 07:04:16 +00:00
|
|
|
ControlState GetValue() const override
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
ControlState value = inner->GetValue();
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_NOT:
|
|
|
|
return 1.0 - value;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValue(ControlState value) override
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case TOK_NOT:
|
|
|
|
inner->SetValue(1.0 - value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
int CountNumControls() const override { return inner->CountNumControls(); }
|
|
|
|
operator std::string() const override { return OpName(op) + "(" + (std::string)(*inner) + ")"; }
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
std::shared_ptr<Device> ControlFinder::FindDevice(ControlQualifier qualifier) const
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (qualifier.has_device)
|
|
|
|
return container.FindDevice(qualifier.device_qualifier);
|
|
|
|
else
|
|
|
|
return container.FindDevice(default_device);
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
Device::Control* ControlFinder::FindControl(ControlQualifier qualifier) const
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2016-06-25 19:46:39 +00:00
|
|
|
const std::shared_ptr<Device> device = FindDevice(qualifier);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!device)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (is_input)
|
|
|
|
return device->FindInput(qualifier.control_name);
|
|
|
|
else
|
|
|
|
return device->FindOutput(qualifier.control_name);
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class Parser
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
Parser(std::vector<Token> tokens_, ControlFinder& finder_) : tokens(tokens_), finder(finder_)
|
|
|
|
{
|
|
|
|
m_it = tokens.begin();
|
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus Parse(Expression** expr_out)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
ExpressionNode* node;
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus status = Toplevel(&node);
|
2017-04-02 10:13:12 +00:00
|
|
|
if (status != ParseStatus::Successful)
|
2016-06-24 08:43:46 +00:00
|
|
|
return status;
|
|
|
|
|
|
|
|
*expr_out = new Expression(node);
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
|
|
|
|
private:
|
2016-06-24 08:43:46 +00:00
|
|
|
std::vector<Token> tokens;
|
|
|
|
std::vector<Token>::iterator m_it;
|
|
|
|
ControlFinder& finder;
|
|
|
|
|
|
|
|
Token Chew() { return *m_it++; }
|
|
|
|
Token Peek() { return *m_it; }
|
|
|
|
bool Expects(TokenType type)
|
|
|
|
{
|
|
|
|
Token tok = Chew();
|
|
|
|
return tok.type == type;
|
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus Atom(ExpressionNode** expr_out)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
Token tok = Chew();
|
|
|
|
switch (tok.type)
|
|
|
|
{
|
|
|
|
case TOK_CONTROL:
|
|
|
|
{
|
2016-07-14 15:45:59 +00:00
|
|
|
std::shared_ptr<Device> device = finder.FindDevice(tok.qualifier);
|
2016-06-24 08:43:46 +00:00
|
|
|
Device::Control* control = finder.FindControl(tok.qualifier);
|
|
|
|
if (control == nullptr)
|
|
|
|
{
|
|
|
|
*expr_out = new DummyExpression(tok.qualifier);
|
2017-02-28 09:58:03 +00:00
|
|
|
return ParseStatus::NoDevice;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 15:45:59 +00:00
|
|
|
*expr_out = new ControlExpression(tok.qualifier, device, control);
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
case TOK_LPAREN:
|
|
|
|
return Paren(expr_out);
|
|
|
|
default:
|
2017-02-28 09:58:03 +00:00
|
|
|
return ParseStatus::SyntaxError;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsUnaryExpression(TokenType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TOK_NOT:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus Unary(ExpressionNode** expr_out)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
if (IsUnaryExpression(Peek().type))
|
|
|
|
{
|
|
|
|
Token tok = Chew();
|
|
|
|
ExpressionNode* atom_expr;
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus status = Atom(&atom_expr);
|
|
|
|
if (status == ParseStatus::SyntaxError)
|
2016-06-24 08:43:46 +00:00
|
|
|
return status;
|
|
|
|
*expr_out = new UnaryExpression(tok.type, atom_expr);
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Atom(expr_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsBinaryToken(TokenType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TOK_AND:
|
|
|
|
case TOK_OR:
|
|
|
|
case TOK_ADD:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus Binary(ExpressionNode** expr_out)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus status = Unary(expr_out);
|
|
|
|
if (status == ParseStatus::SyntaxError)
|
2016-06-24 08:43:46 +00:00
|
|
|
return status;
|
|
|
|
|
|
|
|
while (IsBinaryToken(Peek().type))
|
|
|
|
{
|
|
|
|
Token tok = Chew();
|
|
|
|
ExpressionNode* unary_expr;
|
2016-07-13 14:51:19 +00:00
|
|
|
status = Unary(&unary_expr);
|
2017-02-28 09:58:03 +00:00
|
|
|
if (status == ParseStatus::SyntaxError)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
delete *expr_out;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
*expr_out = new BinaryExpression(tok.type, *expr_out, unary_expr);
|
|
|
|
}
|
|
|
|
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus Paren(ExpressionNode** expr_out)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus status;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// lparen already chewed
|
2017-04-02 10:13:12 +00:00
|
|
|
if ((status = Toplevel(expr_out)) != ParseStatus::Successful)
|
2016-06-24 08:43:46 +00:00
|
|
|
return status;
|
|
|
|
|
|
|
|
if (!Expects(TOK_RPAREN))
|
|
|
|
{
|
|
|
|
delete *expr_out;
|
2017-02-28 09:58:03 +00:00
|
|
|
return ParseStatus::SyntaxError;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus Toplevel(ExpressionNode** expr_out) { return Binary(expr_out); }
|
2013-06-14 03:09:55 +00:00
|
|
|
};
|
|
|
|
|
2017-02-26 07:04:16 +00:00
|
|
|
ControlState Expression::GetValue() const
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return node->GetValue();
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Expression::SetValue(ControlState value)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
node->SetValue(value);
|
2013-06-27 00:19:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Expression::Expression(ExpressionNode* node_)
|
2013-06-27 00:19:23 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
node = node_;
|
|
|
|
num_controls = node->CountNumControls();
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Expression::~Expression()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
delete node;
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
static ParseStatus ParseExpressionInner(const std::string& str, ControlFinder& finder,
|
|
|
|
Expression** expr_out)
|
2013-06-14 03:09:55 +00:00
|
|
|
{
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus status;
|
2016-06-24 08:43:46 +00:00
|
|
|
Expression* expr;
|
|
|
|
*expr_out = nullptr;
|
|
|
|
|
|
|
|
if (str == "")
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
Lexer l(str);
|
|
|
|
std::vector<Token> tokens;
|
|
|
|
status = l.Tokenize(tokens);
|
2017-04-02 10:13:12 +00:00
|
|
|
if (status != ParseStatus::Successful)
|
2016-06-24 08:43:46 +00:00
|
|
|
return status;
|
|
|
|
|
|
|
|
Parser p(tokens, finder);
|
|
|
|
status = p.Parse(&expr);
|
2017-04-02 10:13:12 +00:00
|
|
|
if (status != ParseStatus::Successful)
|
2016-06-24 08:43:46 +00:00
|
|
|
return status;
|
|
|
|
|
|
|
|
*expr_out = expr;
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 09:58:03 +00:00
|
|
|
ParseStatus ParseExpression(const std::string& str, ControlFinder& finder, Expression** expr_out)
|
2013-06-27 00:19:23 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Add compatibility with old simple expressions, which are simple
|
|
|
|
// barewords control names.
|
2013-06-27 00:19:23 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ControlQualifier qualifier;
|
|
|
|
qualifier.control_name = str;
|
|
|
|
qualifier.has_device = false;
|
2013-06-27 00:19:23 +00:00
|
|
|
|
2016-07-14 15:45:59 +00:00
|
|
|
std::shared_ptr<Device> device = finder.FindDevice(qualifier);
|
2016-06-24 08:43:46 +00:00
|
|
|
Device::Control* control = finder.FindControl(qualifier);
|
|
|
|
if (control)
|
|
|
|
{
|
2016-07-14 15:45:59 +00:00
|
|
|
*expr_out = new Expression(new ControlExpression(qualifier, device, control));
|
2017-04-02 10:13:12 +00:00
|
|
|
return ParseStatus::Successful;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2013-06-27 00:19:23 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return ParseExpressionInner(str, finder, expr_out);
|
2013-06-27 00:19:23 +00:00
|
|
|
}
|
2013-06-14 03:09:55 +00:00
|
|
|
}
|
|
|
|
}
|