#include #include #include #include #include #include #include #include namespace Pilang3 { using integer_type = int64_t; namespace exceptions { class langException : public std::exception {public: const char *what() const noexcept {return "Runtime error";}}; class emptyExpr : public langException {public: const char *what() const noexcept {return "Empty expression";}}; class BadInteger : public langException {public: const char *what() const noexcept {return "Bad integer";}}; class BadArgs : public langException {public: const char *what() const noexcept {return "Bad arguments";}}; class NoSuchCommand : public langException {public: const char *what() const noexcept {return "No such command";}}; class NoSuchVariable : public langException {public: const char *what() const noexcept {return "No such variable";}}; class UnexpectedEndOfExpression : public langException {public: const char *what() const noexcept {return "Unexpected end of expression";}}; class exit : public langException {public: int code; const char *what() const noexcept {return "Interpreter exit";} exit(int code) {this->code = code;}}; } class Environment; class Evaluation; class Function; class Condition; struct Variable; using SharedEnvironment = std::shared_ptr; using SharedVariable = std::shared_ptr; using SharedEvaluation = std::shared_ptr; using SharedFunction = std::shared_ptr; using SharedEvaluationWBool = std::tuple; struct Variable { enum Type { id_string, id_integer, id_reference, id_evaluation, id_function, id_condition [[maybe_unused]], id_type [[maybe_unused]], id_retval [[maybe_unused]], id_any [[maybe_unused]], id_null [[maybe_unused]] } type; using Data = std::variant; Data data; }; using Cmdargs = std::vector; using Cmdfnc = std::function; using Cmddesc = std::tuple, bool>; extern std::unordered_map builtinCmds; class Environment { public: using Scope = std::unordered_map; std::stack variableScope; Scope *globalScope; void *anybuf = nullptr; Environment() { variableScope.push({}); globalScope = &variableScope.top(); } Scope &currScope() { return variableScope.top(); } }; class Evaluation { public: Cmdargs arguments; std::tuple command = {"", {nullptr, {}, false}}; std::string command_name; ssize_t exprlen = -1; Evaluation(SharedEnvironment env, const std::string& expression, const bool autoeval = true, const bool autoderef = true); void derefer(SharedEnvironment env); Variable execute(SharedEnvironment env); }; class Function { public: std::vector evalChain; std::vector argumentNames; bool new_scope = false; 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); }; };