mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 08:49:52 +03:00
system commands case independend, logic checks, small fixes
This commit is contained in:
@@ -56,6 +56,7 @@ class Token {
|
||||
const bool rightAssociative;
|
||||
};
|
||||
|
||||
// find tokens
|
||||
std::deque<Token> exprToTokens(const std::string & expr) {
|
||||
std::deque<Token> tokens;
|
||||
|
||||
@@ -204,7 +205,7 @@ std::deque<Token> exprToTokens(const std::string & expr) {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
|
||||
// sort tokens to RPN form
|
||||
std::deque<Token> shuntingYard(const std::deque<Token> & tokens) {
|
||||
std::deque<Token> queue;
|
||||
std::vector<Token> stack;
|
||||
@@ -303,6 +304,7 @@ std::deque<Token> shuntingYard(const std::deque<Token> & tokens) {
|
||||
return queue;
|
||||
}
|
||||
|
||||
// check if string is a number
|
||||
bool isnum(const std::string & s) {
|
||||
if (s.find_first_not_of("0123456789.") == std::string::npos || (s[0] == '-' && s.find_first_not_of("0123456789.", 1) == std::string::npos)) {
|
||||
return true;
|
||||
@@ -356,6 +358,7 @@ std::string commands(std::string & expr) {
|
||||
return expr;
|
||||
}
|
||||
|
||||
// checks for logic value
|
||||
int to_logic(const std::string & s) {
|
||||
if (s[0] == '1' || s == "on" || s == "ON" || s == "true") {
|
||||
return 1;
|
||||
@@ -363,9 +366,10 @@ int to_logic(const std::string & s) {
|
||||
if (s[0] == '0' || s == "off" || s == "OFF" || s == "false") {
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// number to string
|
||||
std::string to_string(double d) {
|
||||
if (d == static_cast<int>(d)) {
|
||||
return std::to_string(static_cast<int>(d));
|
||||
@@ -373,8 +377,9 @@ std::string to_string(double d) {
|
||||
return std::to_string(d);
|
||||
}
|
||||
|
||||
// RPN calculator
|
||||
std::string compute(const std::string & expr) {
|
||||
auto expr_new = expr; //emsesp::Helpers::toLower(expr);
|
||||
auto expr_new = emsesp::Helpers::toLower(expr);
|
||||
// emsesp::EMSESP::logger().info("calculate: %s", expr_new.c_str());
|
||||
commands(expr_new);
|
||||
// emsesp::EMSESP::logger().info("calculate: %s", expr_new.c_str());
|
||||
@@ -413,6 +418,9 @@ std::string compute(const std::string & expr) {
|
||||
stack.push_back(to_string(-1 * std::stod(rhs)));
|
||||
break;
|
||||
case '!':
|
||||
if (to_logic(rhs) < 0) {
|
||||
return "";
|
||||
}
|
||||
stack.push_back(to_logic(rhs) == 0 ? "1" : "0");
|
||||
break;
|
||||
}
|
||||
@@ -483,6 +491,9 @@ std::string compute(const std::string & expr) {
|
||||
stack.pop_back();
|
||||
const auto lhs = to_logic(stack.back());
|
||||
stack.pop_back();
|
||||
if (rhs < 0 || lhs < 0) {
|
||||
return "";
|
||||
}
|
||||
switch (token.str[0]) {
|
||||
default:
|
||||
return "";
|
||||
|
||||
Reference in New Issue
Block a user