2021-03-22 22:33:01 +01:00
|
|
|
#ifndef LIBCUBESCRIPT_ALIAS_HH
|
|
|
|
#define LIBCUBESCRIPT_ALIAS_HH
|
|
|
|
|
|
|
|
#include <cubescript/cubescript.hh>
|
|
|
|
|
2021-03-28 23:46:08 +02:00
|
|
|
#include <bitset>
|
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
namespace cubescript {
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-03-30 02:08:25 +02:00
|
|
|
static constexpr std::size_t MAX_ARGUMENTS = 32;
|
2021-03-28 23:46:08 +02:00
|
|
|
using argset = std::bitset<MAX_ARGUMENTS>;
|
|
|
|
|
2021-03-22 22:33:01 +01:00
|
|
|
enum {
|
|
|
|
ID_UNKNOWN = -1, ID_IVAR, ID_FVAR, ID_SVAR, 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 {
|
2021-04-04 01:08:30 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-03-30 00:31:11 +02:00
|
|
|
struct ident_stack {
|
|
|
|
any_value val_s;
|
2021-04-02 02:11:38 +02:00
|
|
|
bcode_ref code;
|
2021-03-30 00:31:11 +02:00
|
|
|
ident_stack *next;
|
2021-04-02 02:11:38 +02:00
|
|
|
ident_stack(state &cs): val_s{cs}, code{}, next{nullptr} {}
|
2021-03-30 00:31:11 +02:00
|
|
|
};
|
|
|
|
|
2021-04-02 05:47:49 +02:00
|
|
|
struct alias_stack {
|
2021-04-04 01:08:30 +02:00
|
|
|
ident_stack *node = nullptr;
|
|
|
|
int flags = 0;
|
2021-04-02 05:47:49 +02:00
|
|
|
|
2021-04-02 23:50:35 +02:00
|
|
|
void push(ident_stack &st);
|
|
|
|
void pop();
|
|
|
|
|
2021-04-02 05:47:49 +02:00
|
|
|
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_link {
|
|
|
|
ident *id;
|
|
|
|
ident_link *next;
|
2021-03-28 23:46:08 +02:00
|
|
|
std::bitset<MAX_ARGUMENTS> usedargs;
|
2021-03-22 22:33:01 +01:00
|
|
|
};
|
|
|
|
|
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;
|
2021-03-22 22:33:01 +01:00
|
|
|
|
|
|
|
/* trigger destructors for all inherited members properly */
|
2021-03-23 23:29:32 +01:00
|
|
|
virtual ~ident_impl() {};
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
ident_impl &operator=(ident_impl const &) = delete;
|
|
|
|
ident_impl &operator=(ident_impl &&) = delete;
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
ident_impl(ident_type tp, string_ref name, int flags = 0);
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
string_ref p_name;
|
|
|
|
/* represents the ident_type above, but internally it has a wider
|
2021-03-22 22:33:01 +01:00
|
|
|
* variety of values, so it's an int here (maps to an internal enum)
|
|
|
|
*/
|
|
|
|
int p_type, p_flags;
|
|
|
|
|
|
|
|
int p_index = -1;
|
|
|
|
};
|
|
|
|
|
2021-03-27 00:26:59 +01:00
|
|
|
bool ident_is_callable(ident const *id);
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
struct var_impl: ident_impl {
|
2021-04-03 03:37:58 +02:00
|
|
|
var_impl(ident_type tp, string_ref name, int flags);
|
2021-04-04 02:44:35 +02:00
|
|
|
|
|
|
|
virtual void save_val() = 0;
|
2021-03-22 22:33:01 +01:00
|
|
|
};
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
struct ivar_impl: var_impl, integer_var {
|
2021-04-03 03:37:58 +02:00
|
|
|
ivar_impl(string_ref n, integer_type v, int flags);
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-04-04 02:44:35 +02:00
|
|
|
void save_val();
|
|
|
|
|
2021-04-03 03:37:58 +02:00
|
|
|
integer_type p_storage;
|
2021-04-04 01:08:30 +02:00
|
|
|
integer_type p_override;
|
2021-03-22 22:33:01 +01:00
|
|
|
};
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
struct fvar_impl: var_impl, float_var {
|
2021-04-03 03:37:58 +02:00
|
|
|
fvar_impl(string_ref n, float_type v, int flags);
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-04-04 02:44:35 +02:00
|
|
|
void save_val();
|
|
|
|
|
2021-04-03 03:37:58 +02:00
|
|
|
float_type p_storage;
|
2021-04-04 01:08:30 +02:00
|
|
|
float_type p_override;
|
2021-03-22 22:33:01 +01:00
|
|
|
};
|
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
struct svar_impl: var_impl, string_var {
|
2021-04-03 03:37:58 +02:00
|
|
|
svar_impl(string_ref n, string_ref v, int flags);
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-04-04 02:44:35 +02:00
|
|
|
void save_val();
|
|
|
|
|
2021-04-03 03:37:58 +02:00
|
|
|
string_ref p_storage;
|
2021-04-04 01:08:30 +02:00
|
|
|
string_ref p_override;
|
2021-03-22 22:33:01 +01:00
|
|
|
};
|
|
|
|
|
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);
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-04-01 03:40:36 +02:00
|
|
|
ident_stack p_initial;
|
2021-03-22 22:33:01 +01:00
|
|
|
};
|
|
|
|
|
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-03-22 22:33:01 +01:00
|
|
|
);
|
|
|
|
|
2021-03-30 00:28:06 +02:00
|
|
|
void call(state &cs, std::span<any_value> args, any_value &ret);
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-03-23 23:29:32 +01:00
|
|
|
string_ref p_cargs;
|
|
|
|
command_func p_cb_cftv;
|
2021-03-22 22:33:01 +01:00
|
|
|
int p_numargs;
|
|
|
|
};
|
|
|
|
|
2021-03-26 02:29:54 +01:00
|
|
|
bool ident_is_used_arg(ident *id, thread_state &ts);
|
2021-03-22 22:33:01 +01:00
|
|
|
|
2021-03-23 23:32:25 +01:00
|
|
|
} /* namespace cubescript */
|
2021-03-22 22:33:01 +01:00
|
|
|
|
|
|
|
#endif
|