From dc472601bb31430561d66469b81e95b88fe6ec6b Mon Sep 17 00:00:00 2001 From: niansa Date: Wed, 10 Mar 2021 10:03:48 +0100 Subject: [PATCH] Implemented just the very basics of conditions --- include/pilang.hpp | 18 +++++++++- pilang.cpp | 83 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/include/pilang.hpp b/include/pilang.hpp index b39ee72..f5b71a0 100644 --- a/include/pilang.hpp +++ b/include/pilang.hpp @@ -34,6 +34,22 @@ namespace Pilang3 { using SharedFunction = std::shared_ptr; using SharedEvaluationWBool = std::tuple; + namespace condition { + enum type { + _none, + equals, + not_equals, + biggerthan, + smallerthan, + biggerthanorequals, + smallerthanorequals, + ncpp_and, + ncpp_or, + _end + }; + using array = std::vector; + } + struct Variable { enum Type { id_string, @@ -48,7 +64,7 @@ namespace Pilang3 { id_null [[maybe_unused]] } type; - using Data = std::variant; + using Data = std::variant; Data data; }; diff --git a/pilang.cpp b/pilang.cpp index df004f4..49f2076 100644 --- a/pilang.cpp +++ b/pilang.cpp @@ -45,8 +45,10 @@ namespace Pilang3 { Variable thisarg; bool in_command_name = true; bool in_string = false; + bool is_condition = false; bool newarg = false; bool escapenext = false; + condition::array condition_cache; std::string cache; for (auto characterit = expression.begin() + tspaces; characterit < expression.end(); characterit++) { char character = *characterit; @@ -88,8 +90,9 @@ namespace Pilang3 { thisarg.type = Variable::id_function; goto parsechar; } else if (character == '!') { - // Function - thisarg.type = Variable::id_condition; + // Condition + is_condition = true; + thisarg.type = Variable::id_null; goto parsechar; } else { // Reference @@ -222,20 +225,76 @@ namespace Pilang3 { thisarg.data = thisfunc; newarg = true; } break; - case Variable::id_condition: { - //TODO - } break; } - // Check if argument might be a condition - //TODO + if (not (in_string or newarg)) { + // Check if argument might be a condition + if (character == '!' or character == '=') { + is_condition = true; + } + // Continue building condition + if (is_condition) { + uint64_t cond_extends = condition::_none; + // Equals + if (character == '=' and *(characterit + 1) == '=') { + cond_extends = condition::equals; + } + // Not equals + else if (character == '!' and *(characterit + 1) == '=') { + cond_extends = condition::not_equals; + } + // Bigger than or equals + else if (character == '>' and *(characterit + 1) == '=') { + cond_extends = condition::biggerthanorequals; + } + // Smaller than or equals + else if (character == '<' and *(characterit + 1) == '=') { + cond_extends = condition::smallerthanorequals; + } + // Bigger than + else if (character == '>') { + cond_extends = condition::biggerthan; + } + // Smaller than + else if (character == '<') { + cond_extends = condition::smallerthan; + } + // And + else if (character == '&' and *(characterit + 1) == '&') { + cond_extends = condition::ncpp_and; + } + // Or + else if (character == '|' and *(characterit + 1) == '|') { + cond_extends = condition::ncpp_or; + } + // Extend + if (cond_extends != condition::_none) { + condition_cache.push_back(new Variable(thisarg)); + condition_cache.push_back(reinterpret_cast(cond_extends)); + newarg = true; + // Note: some conditional strings have just one character + if (cond_extends != condition::biggerthan and cond_extends != condition::smallerthan) { + characterit += 1; + } + } + } + } else if (newarg and is_condition) { + // End condition + condition_cache.push_back(new Variable(thisarg)); + is_condition = false; + thisarg.type = Variable::id_condition; + thisarg.data = condition_cache; + condition_cache.clear(); + } // Actually end argument if required if (newarg) { - arguments.push_back(thisarg); cache.clear(); - // Check if end of command was reached - if (character == ';') { - exprlen = std::distance(expression.begin(), characterit); - break; + if (not is_condition) { + arguments.push_back(thisarg); + // Check if end of command was reached + if (character == ';') { + exprlen = std::distance(expression.begin(), characterit); + break; + } } } }