mirror of
https://gitlab.com/niansa/pilang3.git
synced 2025-03-06 20:49:20 +01:00
79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
#include <string>
|
|
#include <vector>
|
|
#include <unordered_map>
|
|
#include <variant>
|
|
#include <exception>
|
|
#include <memory>
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace Pilang3 {
|
|
using integer_type = int64_t;
|
|
|
|
namespace exceptions {
|
|
class langException : public std::exception {};
|
|
class BadInteger : public langException {};
|
|
class BadArgs : public langException {};
|
|
class NoSuchCommand : public langException {};
|
|
class NoSuchVariable : public langException {};
|
|
class UnexpectedEndOfExpression : public langException {};
|
|
}
|
|
|
|
class Environment;
|
|
class Evaluation;
|
|
class Function;
|
|
struct Variable;
|
|
using SharedEnvironment = std::shared_ptr<Environment>;
|
|
using SharedVariable = std::shared_ptr<Variable>;
|
|
using SharedEvaluation = std::shared_ptr<Evaluation>;
|
|
using SharedFunction = std::shared_ptr<Function>;
|
|
|
|
struct Variable {
|
|
enum Type {
|
|
id_string,
|
|
id_integer,
|
|
id_reference,
|
|
id_evaluation,
|
|
id_function,
|
|
id_type [[maybe_unused]],
|
|
id_retval [[maybe_unused]],
|
|
id_any [[maybe_unused]],
|
|
id_null [[maybe_unused]]
|
|
} type;
|
|
|
|
using Data = std::variant<std::string, integer_type, SharedVariable, SharedEvaluation, SharedFunction, Type>;
|
|
Data data;
|
|
};
|
|
|
|
using Cmdargs = std::vector<Variable>;
|
|
using Cmdfnc = std::function<Variable (SharedEnvironment, Cmdargs&)>;
|
|
using Cmddesc = std::tuple<Cmdfnc, uint16_t, std::vector<Variable::Type>, bool>;
|
|
|
|
extern std::unordered_map<std::string, Cmddesc> builtinCmds;
|
|
|
|
class Environment {
|
|
public:
|
|
std::unordered_map<std::string, SharedVariable> globalVars;
|
|
void *anybuf = nullptr;
|
|
};
|
|
|
|
class Evaluation {
|
|
public:
|
|
Cmdargs arguments;
|
|
Cmdfnc command = nullptr;
|
|
std::string command_name;
|
|
ssize_t exprlen = -1;
|
|
|
|
Evaluation(SharedEnvironment env, const std::string& expression, const bool autoeval = true);
|
|
|
|
Variable execute(SharedEnvironment env);
|
|
};
|
|
|
|
class Function {
|
|
public:
|
|
std::vector<SharedEvaluation> evalChain;
|
|
|
|
Variable execute(SharedEnvironment env);
|
|
};
|
|
};
|