From f816bbde62043092041a5ec6184e16eceac3a7c9 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sun, 20 Oct 2024 14:23:02 +0200 Subject: [PATCH] add `round`, `abs`, `int`, etc. to scheduler shunting yard --- src/shuntingYard.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/shuntingYard.hpp b/src/shuntingYard.hpp index e2124db3d..3448fa1fc 100644 --- a/src/shuntingYard.hpp +++ b/src/shuntingYard.hpp @@ -72,6 +72,27 @@ std::deque exprToTokens(const std::string & expr) { if (*p == '\0') { --p; } + } else if (strncmp(p, "int", 3) == 0) { + p += 2; + tokens.emplace_back(Token::Type::Unary, "i", 5); + } else if (strncmp(p, "round", 5) == 0) { + p += 4; + tokens.emplace_back(Token::Type::Unary, "r", 5); + } else if (strncmp(p, "abs", 3) == 0) { + p += 2; + tokens.emplace_back(Token::Type::Unary, "a", 5); + } else if (strncmp(p, "log", 3) == 0) { + p += 2; + tokens.emplace_back(Token::Type::Unary, "l", 5); + } else if (strncmp(p, "exp", 3) == 0) { + p += 2; + tokens.emplace_back(Token::Type::Unary, "e", 5); + } else if (strncmp(p, "sqrt", 4) == 0) { + p += 3; + tokens.emplace_back(Token::Type::Unary, "s", 5); + } else if (strncmp(p, "pow", 3) == 0) { + p += 2; + tokens.emplace_back(Token::Type::Unary, "p", 5); } else if (*p >= 'a' && *p <= 'z') { const auto * b = p; while ((*p >= 'a' && *p <= 'z') || (*p == '_')) { @@ -448,6 +469,27 @@ std::string calculate(const std::string & expr) { } stack.push_back(to_logic(rhs) == 0 ? "1" : "0"); break; + case 'i': + stack.push_back(to_string(std::stoi(rhs))); + break; + case 'r': + stack.push_back(to_string(std::round(std::stod(rhs)))); + break; + case 'a': + stack.push_back(to_string(std::abs(std::stod(rhs)))); + break; + case 'e': + stack.push_back(to_string(std::exp(std::stod(rhs)))); + break; + case 'l': + stack.push_back(to_string(std::log(std::stod(rhs)))); + break; + case 's': + stack.push_back(to_string(std::sqrt(std::stod(rhs)))); + break; + case 'p': + stack.push_back(to_string(std::pow(std::stod(rhs), 2))); + break; } } break; case Token::Type::Compare: {