From 1bcb03f0cc7e87faad9a16f11def97ff7907ab94 Mon Sep 17 00:00:00 2001 From: niansa Date: Thu, 4 Feb 2021 13:45:05 +0100 Subject: [PATCH] Implemented basic functions --- include/pilang.hpp | 3 ++- main.cpp | 10 ++++++---- modules/std.cpp | 8 ++++---- modules/stdio.cpp | 2 +- pilang.cpp | 23 ++++++++++++++--------- 5 files changed, 27 insertions(+), 19 deletions(-) diff --git a/include/pilang.hpp b/include/pilang.hpp index 0bdca18..9da2200 100644 --- a/include/pilang.hpp +++ b/include/pilang.hpp @@ -13,6 +13,7 @@ namespace Pilang3 { namespace exceptions { class langException : public std::exception {}; + class emptyExpr : public langException {}; class BadInteger : public langException {}; class BadArgs : public langException {}; class NoSuchCommand : public langException {}; @@ -48,7 +49,7 @@ namespace Pilang3 { using Cmdargs = std::vector; using Cmdfnc = std::function; - using Cmddesc = std::tuple, bool>; + using Cmddesc = std::tuple, bool>; extern std::unordered_map builtinCmds; diff --git a/main.cpp b/main.cpp index 595137b..15b2209 100644 --- a/main.cpp +++ b/main.cpp @@ -13,11 +13,13 @@ int main() { // CLI loop while (true) { std::getline(std::cin, line); - //try { + try { Evaluation evaluation(env, line); evaluation.execute(env); - //} catch (exceptions::langException& e) { - // std::cout << "E: Language exception: " << &e << std::endl; - //} + } catch (exceptions::emptyExpr&) { + // Do nothing + } catch (exceptions::langException& e) { + std::cout << "E: Language exception: " << &e << std::endl; + } } } diff --git a/modules/std.cpp b/modules/std.cpp index a053755..2531b6c 100644 --- a/modules/std.cpp +++ b/modules/std.cpp @@ -40,10 +40,10 @@ public: } Std() { - Pilang3::builtinCmds["return"] = {retval, 1, {Variable::id_any}, false}; - Pilang3::builtinCmds["set"] = {set, 2, {Variable::id_string, Variable::id_any}, false}; - Pilang3::builtinCmds["get"] = {get, 1, {Variable::id_string}, false}; - Pilang3::builtinCmds["concat"] = {concat, 0, {}, true}; + Pilang3::builtinCmds["return"] = {retval, {Variable::id_any}, false}; + Pilang3::builtinCmds["set"] = {set, {Variable::id_string, Variable::id_any}, false}; + Pilang3::builtinCmds["get"] = {get, {Variable::id_string}, false}; + Pilang3::builtinCmds["concat"] = {concat, {}, true}; } }; diff --git a/modules/stdio.cpp b/modules/stdio.cpp index b86d765..7c2af6e 100644 --- a/modules/stdio.cpp +++ b/modules/stdio.cpp @@ -14,7 +14,7 @@ public: } StdIO() { - Pilang3::builtinCmds["print"] = {print, 1, {Variable::id_string}, false}; + Pilang3::builtinCmds["print"] = {print, {Variable::id_string}, false}; } }; diff --git a/pilang.cpp b/pilang.cpp index 98248dc..0cd9580 100644 --- a/pilang.cpp +++ b/pilang.cpp @@ -10,22 +10,26 @@ #include "pilang.hpp" +static inline bool is_sep(const char character) { + return character == ' ' or character == '\t' or character == '\n'; +} + namespace Pilang3 { - std::unordered_map, bool>> builtinCmds; + std::unordered_map builtinCmds; Evaluation::Evaluation(SharedEnvironment env, const std::string& expression, const bool autoeval) { // Count off trailing spaces ssize_t tspaces = 0; for (const auto& character : expression) { - if (character == ' ' or character == '\t') { + if (is_sep(character)) { tspaces++; } else { break; } } // Parse arguments if any - if (not expression.empty()) { + if (not expression.empty() and static_cast(expression.size()) != tspaces) { Variable thisarg; bool in_command_name = true; bool newarg = false; @@ -52,7 +56,7 @@ namespace Pilang3 { // Start new argument else if (newarg) { // Skip spaces - if (character == ' ' or character == ',' or character == '\n' or character == ';') continue; + if (is_sep(character)) continue; // Guess type by first character newarg = false; if (std::isdigit(character) or character == '-' or character == '+') { @@ -80,7 +84,7 @@ namespace Pilang3 { switch (thisarg.type) { case Variable::id_integer: { // Integer - if (character == ' ') continue; + if (is_sep(character)) continue; else if (maybe_end_of_arg) { // End argument try { @@ -180,20 +184,21 @@ namespace Pilang3 { return std::get(res->second->data)->execute(env); }; } else { - uint16_t argslen; std::vector argstypes; bool anymoreopts; - std::tie(command, argslen, argstypes, anymoreopts) = res->second; + std::tie(command, argstypes, anymoreopts) = res->second; // Check args - if ((arguments.size() > argslen and not anymoreopts) or (arguments.size() < argslen)) { + if ((arguments.size() > argstypes.size() and not anymoreopts) or (arguments.size() < argstypes.size())) { throw exceptions::BadArgs(); } - for (uint16_t argidx = 0; argidx != argslen; argidx++) { + for (uint16_t argidx = 0; argidx != argstypes.size(); argidx++) { if (argstypes[argidx] != Variable::id_any and argstypes[argidx] != arguments[argidx].type) { throw exceptions::BadArgs(); } } } + } else { + throw exceptions::emptyExpr(); } }