From 6acc61b26fda95d65e53a1c5b17b19406c340266 Mon Sep 17 00:00:00 2001 From: niansa Date: Wed, 10 Mar 2021 09:16:46 +0100 Subject: [PATCH] Minor fixups to get ready for implementing conditions --- include/pilang.hpp | 18 +++++++++++ modules/std.cpp | 1 + pilang.cpp | 75 ++++++++++++++++++++++++++++++---------------- 3 files changed, 68 insertions(+), 26 deletions(-) diff --git a/include/pilang.hpp b/include/pilang.hpp index b66a44a..b39ee72 100644 --- a/include/pilang.hpp +++ b/include/pilang.hpp @@ -26,6 +26,7 @@ namespace Pilang3 { class Environment; class Evaluation; class Function; + class Condition; struct Variable; using SharedEnvironment = std::shared_ptr; using SharedVariable = std::shared_ptr; @@ -40,6 +41,7 @@ namespace Pilang3 { id_reference, id_evaluation, id_function, + id_condition [[maybe_unused]], id_type [[maybe_unused]], id_retval [[maybe_unused]], id_any [[maybe_unused]], @@ -95,4 +97,20 @@ namespace Pilang3 { Variable execute(SharedEnvironment env, Cmdargs& args); }; + + class Condition { + public: + enum { + // Condition special types + id_equals, + id_notequals, + id_biggerthan, + id_smallerthan + }; + + ssize_t exprlen = -1; + std::vector arr; + + Condition(const std::string& expression); + }; }; diff --git a/modules/std.cpp b/modules/std.cpp index 0e5025d..c316c8e 100644 --- a/modules/std.cpp +++ b/modules/std.cpp @@ -58,6 +58,7 @@ public: case Variable::id_string: fres << std::get(arg.data); break; case Variable::id_reference: fres << "REF<" << std::get(arg.data) << '>'; break; case Variable::id_type: fres << std::get(arg.data); break; + case Variable::id_null: fres << "(null)"; break; default: fres << "'; break; } } diff --git a/pilang.cpp b/pilang.cpp index 42c1e2a..df004f4 100644 --- a/pilang.cpp +++ b/pilang.cpp @@ -18,6 +18,16 @@ static inline bool is_sep(const char character) { namespace Pilang3 { std::unordered_map builtinCmds; + Condition::Condition(const std::string& expression) { + + for (auto characterit = expression.begin(); characterit < expression.end(); characterit++) { + if (*characterit == ',') { + exprlen = std::distance(expression.begin(), characterit); + break; + } + } + } + Evaluation::Evaluation(SharedEnvironment env, const std::string& expression, const bool autoeval, const bool autoderef) { // Count off trailing spaces ssize_t tspaces = 0; @@ -34,6 +44,7 @@ namespace Pilang3 { if (not expression.empty() and static_cast(expression.size()) != tspaces) { Variable thisarg; bool in_command_name = true; + bool in_string = false; bool newarg = false; bool escapenext = false; std::string cache; @@ -67,6 +78,7 @@ namespace Pilang3 { goto parsechar; } else if (character == '"') { // String + in_string = true; thisarg.type = Variable::id_string; } else if (character == '&') { // Expression @@ -75,6 +87,10 @@ namespace Pilang3 { // Function thisarg.type = Variable::id_function; goto parsechar; + } else if (character == '!') { + // Function + thisarg.type = Variable::id_condition; + goto parsechar; } else { // Reference thisarg.type = Variable::id_reference; @@ -85,33 +101,35 @@ namespace Pilang3 { bool maybe_end_of_arg = character == ',' or character == ';'; // Do whatever (depends on type) switch (thisarg.type) { - case Variable::id_integer: { - // Integer - if (is_sep(character)) continue; - else if (maybe_end_of_arg) { - // End argument - try { - thisarg.data = integer_type(std::stoi(cache)); - } catch (std::invalid_argument&) { - throw exceptions::BadInteger(); - } - newarg = true; - } else { - cache.push_back(character); + case Variable::id_integer: { + // Integer + if (is_sep(character)) continue; + else if (maybe_end_of_arg) { + // End argument + try { + thisarg.data = integer_type(std::stoi(cache)); + } catch (std::invalid_argument&) { + throw exceptions::BadInteger(); } - } break; - case Variable::id_string: { - // String - if (character == '\\' and not escapenext) { - escapenext = true; - } else if (character == '"' and not escapenext) { - thisarg.data = cache; - newarg = true; - character = *(++characterit); - } else { - cache.push_back(character); - } - } break; + newarg = true; + } else { + cache.push_back(character); + } + } break; + case Variable::id_string: { + // String + if (character == '"' and not escapenext) { + std::get(thisarg.data).append(cache); + cache.clear(); + in_string = not in_string; + } else if (in_string and character == '\\' and not escapenext) { + escapenext = true; + } else if (in_string) { + cache.push_back(character); + } else if (maybe_end_of_arg) { + newarg = true; + } + } break; case Variable::id_reference: { // Reference if (maybe_end_of_arg) { @@ -204,7 +222,12 @@ namespace Pilang3 { thisarg.data = thisfunc; newarg = true; } break; + case Variable::id_condition: { + //TODO + } break; } + // Check if argument might be a condition + //TODO // Actually end argument if required if (newarg) { arguments.push_back(thisarg);