libcubescript/src/cs_ident.hh

120 lines
2.9 KiB
C++
Raw Permalink Normal View History

#ifndef LIBCUBESCRIPT_ALIAS_HH
#define LIBCUBESCRIPT_ALIAS_HH
#include <cubescript/cubescript.hh>
#include <bitset>
2021-03-23 23:32:25 +01:00
namespace cubescript {
static constexpr std::size_t MAX_ARGUMENTS = 32;
using argset = std::bitset<MAX_ARGUMENTS>;
enum {
ID_UNKNOWN = -1, ID_VAR, ID_COMMAND, ID_ALIAS,
ID_LOCAL, ID_DO, ID_DOARGS, ID_IF, ID_BREAK, ID_CONTINUE, ID_RESULT,
ID_NOT, ID_AND, ID_OR
};
2021-04-03 03:37:58 +02:00
enum {
IDENT_FLAG_UNKNOWN = 1 << 0,
IDENT_FLAG_ARG = 1 << 1,
IDENT_FLAG_READONLY = 1 << 2,
IDENT_FLAG_OVERRIDE = 1 << 3,
IDENT_FLAG_OVERRIDDEN = 1 << 4,
IDENT_FLAG_PERSIST = 1 << 5
2021-04-03 03:37:58 +02:00
};
struct ident_stack {
any_value val_s;
bcode_ref code;
ident_stack *next;
ident_stack(): val_s{}, code{}, next{nullptr} {}
};
struct alias_stack {
ident_stack *node = nullptr;
int flags = 0;
2021-04-02 23:50:35 +02:00
void push(ident_stack &st);
void pop();
void set_arg(alias *a, thread_state &ts, any_value &v);
void set_alias(alias *a, thread_state &ts, any_value &v);
};
2021-03-23 23:29:32 +01:00
struct ident_impl {
ident_impl() = delete;
ident_impl(ident_impl const &) = delete;
ident_impl(ident_impl &&) = delete;
/* trigger destructors for all inherited members properly */
2021-03-23 23:29:32 +01:00
virtual ~ident_impl() {};
2021-03-23 23:29:32 +01:00
ident_impl &operator=(ident_impl const &) = delete;
ident_impl &operator=(ident_impl &&) = delete;
2021-03-23 23:29:32 +01:00
ident_impl(ident_type tp, string_ref name, int flags = 0);
2021-03-23 23:29:32 +01:00
string_ref p_name;
/* represents the ident_type above, but internally it has a wider
* variety of values, so it's an int here (maps to an internal enum)
*/
int p_type, p_flags;
int p_index = -1;
};
bool ident_is_callable(ident const *id);
struct var_impl: ident_impl, builtin_var {
var_impl(string_ref name, int flags);
2021-04-04 02:44:35 +02:00
command *get_setter(thread_state &ts) const;
any_value p_storage{};
any_value p_override{};
};
void var_changed(thread_state &ts, builtin_var &id, any_value &oldval);
2021-03-23 23:29:32 +01:00
struct alias_impl: ident_impl, alias {
alias_impl(state &cs, string_ref n, string_ref a, int flags);
alias_impl(state &cs, string_ref n, std::string_view a, int flags);
alias_impl(state &cs, string_ref n, integer_type a, int flags);
alias_impl(state &cs, string_ref n, float_type a, int flags);
alias_impl(state &cs, string_ref n, int flags);
alias_impl(state &cs, string_ref n, any_value v, int flags);
ident_stack p_initial;
};
2021-03-23 23:29:32 +01:00
struct command_impl: ident_impl, command {
command_impl(
string_ref name, string_ref args, int numargs, command_func func
);
2021-05-02 22:44:38 +02:00
void call(
thread_state &ts, span_type<any_value> args, any_value &ret
) const;
2021-03-23 23:29:32 +01:00
string_ref p_cargs;
command_func p_cb_cftv;
int p_numargs;
};
2021-05-02 22:44:38 +02:00
bool ident_is_used_arg(ident const *id, thread_state &ts);
struct ident_p {
ident_p(ident &id): ip{&id} {}
ident_impl &impl() { return *ip->p_impl; }
void impl(ident_impl *impl) { ip->p_impl = impl; }
ident *ip;
};
2021-03-23 23:32:25 +01:00
} /* namespace cubescript */
#endif