clean up some leftover cruft
parent
b00a08ea88
commit
b2b83a8e5b
|
@ -56,22 +56,11 @@ struct ident;
|
||||||
struct any_value;
|
struct any_value;
|
||||||
struct global_var;
|
struct global_var;
|
||||||
|
|
||||||
using hook_func = callable<void, state &>;
|
using hook_func = callable<void, state &>;
|
||||||
using var_cb_func = callable<void, state &, ident &>;
|
using command_func = callable<
|
||||||
using command_func = callable<
|
|
||||||
void, state &, std::span<any_value>, any_value &
|
void, state &, std::span<any_value>, any_value &
|
||||||
>;
|
>;
|
||||||
|
|
||||||
enum {
|
|
||||||
IDENT_FLAG_PERSIST = 1 << 0,
|
|
||||||
IDENT_FLAG_OVERRIDE = 1 << 1,
|
|
||||||
IDENT_FLAG_HEX = 1 << 2,
|
|
||||||
IDENT_FLAG_READONLY = 1 << 3,
|
|
||||||
IDENT_FLAG_OVERRIDDEN = 1 << 4,
|
|
||||||
IDENT_FLAG_UNKNOWN = 1 << 5,
|
|
||||||
IDENT_FLAG_ARG = 1 << 6
|
|
||||||
};
|
|
||||||
|
|
||||||
struct internal_state;
|
struct internal_state;
|
||||||
struct thread_state;
|
struct thread_state;
|
||||||
struct ident_impl;
|
struct ident_impl;
|
||||||
|
@ -314,8 +303,6 @@ enum class loop_state {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LIBCUBESCRIPT_EXPORT state {
|
struct LIBCUBESCRIPT_EXPORT state {
|
||||||
int identflags = 0;
|
|
||||||
|
|
||||||
state();
|
state();
|
||||||
state(alloc_func func, void *data);
|
state(alloc_func func, void *data);
|
||||||
virtual ~state();
|
virtual ~state();
|
||||||
|
@ -336,7 +323,6 @@ struct LIBCUBESCRIPT_EXPORT state {
|
||||||
|
|
||||||
void swap(state &s) {
|
void swap(state &s) {
|
||||||
std::swap(p_tstate, s.p_tstate);
|
std::swap(p_tstate, s.p_tstate);
|
||||||
std::swap(identflags, s.identflags);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
state new_thread();
|
state new_thread();
|
||||||
|
@ -352,55 +338,17 @@ struct LIBCUBESCRIPT_EXPORT state {
|
||||||
|
|
||||||
void init_libs(int libs = LIB_ALL);
|
void init_libs(int libs = LIB_ALL);
|
||||||
|
|
||||||
void clear_override(ident &id);
|
|
||||||
void clear_overrides();
|
|
||||||
|
|
||||||
ident *new_ident(std::string_view name, int flags = IDENT_FLAG_UNKNOWN);
|
|
||||||
|
|
||||||
template<typename F>
|
|
||||||
integer_var *new_ivar(
|
integer_var *new_ivar(
|
||||||
std::string_view name, integer_type m, integer_type x, integer_type v,
|
std::string_view n, integer_type v, bool read_only = false
|
||||||
F &&f, int flags = 0
|
);
|
||||||
) {
|
|
||||||
return new_ivar(
|
|
||||||
name, m, x, v,
|
|
||||||
var_cb_func{std::forward<F>(f), callable_alloc, this}, flags
|
|
||||||
);
|
|
||||||
}
|
|
||||||
integer_var *new_ivar(
|
|
||||||
std::string_view name, integer_type m, integer_type x, integer_type v
|
|
||||||
) {
|
|
||||||
return new_ivar(name, m, x, v, var_cb_func{}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename F>
|
|
||||||
float_var *new_fvar(
|
float_var *new_fvar(
|
||||||
std::string_view name, float_type m, float_type x, float_type v,
|
std::string_view n, float_type v, bool read_only = false
|
||||||
F &&f, int flags = 0
|
);
|
||||||
) {
|
|
||||||
return new_fvar(
|
|
||||||
name, m, x, v,
|
|
||||||
var_cb_func{std::forward<F>(f), callable_alloc, this}, flags
|
|
||||||
);
|
|
||||||
}
|
|
||||||
float_var *new_fvar(
|
|
||||||
std::string_view name, float_type m, float_type x, float_type v
|
|
||||||
) {
|
|
||||||
return new_fvar(name, m, x, v, var_cb_func{}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename F>
|
|
||||||
string_var *new_svar(
|
string_var *new_svar(
|
||||||
std::string_view name, std::string_view v, F &&f, int flags = 0
|
std::string_view n, std::string_view v, bool read_only = false
|
||||||
) {
|
);
|
||||||
return new_svar(
|
|
||||||
name, v,
|
ident *new_ident(std::string_view name, int flags);
|
||||||
var_cb_func{std::forward<F>(f), callable_alloc, this}, flags
|
|
||||||
);
|
|
||||||
}
|
|
||||||
string_var *new_svar(std::string_view name, std::string_view v) {
|
|
||||||
return new_svar(name, v, var_cb_func{}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
command *new_command(
|
command *new_command(
|
||||||
|
@ -419,9 +367,6 @@ struct LIBCUBESCRIPT_EXPORT state {
|
||||||
std::span<ident *> get_idents();
|
std::span<ident *> get_idents();
|
||||||
std::span<ident const *> get_idents() const;
|
std::span<ident const *> get_idents() const;
|
||||||
|
|
||||||
void reset_var(std::string_view name);
|
|
||||||
void touch_var(std::string_view name);
|
|
||||||
|
|
||||||
void run(bcode_ref const &code, any_value &ret);
|
void run(bcode_ref const &code, any_value &ret);
|
||||||
void run(std::string_view code, any_value &ret);
|
void run(std::string_view code, any_value &ret);
|
||||||
void run(std::string_view code, any_value &ret, std::string_view source);
|
void run(std::string_view code, any_value &ret, std::string_view source);
|
||||||
|
@ -454,18 +399,6 @@ struct LIBCUBESCRIPT_EXPORT state {
|
||||||
private:
|
private:
|
||||||
hook_func set_call_hook(hook_func func);
|
hook_func set_call_hook(hook_func func);
|
||||||
|
|
||||||
integer_var *new_ivar(
|
|
||||||
std::string_view n, integer_type m, integer_type x, integer_type v,
|
|
||||||
var_cb_func f, int flags
|
|
||||||
);
|
|
||||||
float_var *new_fvar(
|
|
||||||
std::string_view n, float_type m, float_type x, float_type v,
|
|
||||||
var_cb_func f, int flags
|
|
||||||
);
|
|
||||||
string_var *new_svar(
|
|
||||||
std::string_view n, std::string_view v, var_cb_func f, int flags
|
|
||||||
);
|
|
||||||
|
|
||||||
command *new_command(
|
command *new_command(
|
||||||
std::string_view name, std::string_view args, command_func func
|
std::string_view name, std::string_view args, command_func func
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include <cubescript/cubescript.hh>
|
#include <cubescript/cubescript.hh>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include "cs_gen.hh"
|
#include "cs_gen.hh"
|
||||||
#include "cs_thread.hh"
|
#include "cs_thread.hh"
|
||||||
|
|
||||||
|
@ -85,7 +87,10 @@ LIBCUBESCRIPT_EXPORT stack_state error::save_stack(thread_state &ts) {
|
||||||
integer_var *dalias = static_cast<integer_var *>(
|
integer_var *dalias = static_cast<integer_var *>(
|
||||||
ts.istate->identmap[ID_IDX_DBGALIAS]
|
ts.istate->identmap[ID_IDX_DBGALIAS]
|
||||||
);
|
);
|
||||||
if (!dalias->get_value()) {
|
auto dval = std::clamp(
|
||||||
|
dalias->get_value(), integer_type(0), integer_type(1000)
|
||||||
|
);
|
||||||
|
if (!dval) {
|
||||||
return stack_state{ts, nullptr, !!ts.callstack};
|
return stack_state{ts, nullptr, !!ts.callstack};
|
||||||
}
|
}
|
||||||
int total = 0, depth = 0;
|
int total = 0, depth = 0;
|
||||||
|
@ -96,13 +101,13 @@ LIBCUBESCRIPT_EXPORT stack_state error::save_stack(thread_state &ts) {
|
||||||
return stack_state{ts, nullptr, false};
|
return stack_state{ts, nullptr, false};
|
||||||
}
|
}
|
||||||
stack_state::node *st = ts.istate->create_array<stack_state::node>(
|
stack_state::node *st = ts.istate->create_array<stack_state::node>(
|
||||||
std::min(total, dalias->get_value())
|
std::min(total, dval)
|
||||||
);
|
);
|
||||||
stack_state::node *ret = st, *nd = st;
|
stack_state::node *ret = st, *nd = st;
|
||||||
++st;
|
++st;
|
||||||
for (ident_link *l = ts.callstack; l; l = l->next) {
|
for (ident_link *l = ts.callstack; l; l = l->next) {
|
||||||
++depth;
|
++depth;
|
||||||
if (depth < dalias->get_value()) {
|
if (depth < dval) {
|
||||||
nd->id = l->id;
|
nd->id = l->id;
|
||||||
nd->index = total - depth + 1;
|
nd->index = total - depth + 1;
|
||||||
if (!l->next) {
|
if (!l->next) {
|
||||||
|
@ -117,7 +122,7 @@ LIBCUBESCRIPT_EXPORT stack_state error::save_stack(thread_state &ts) {
|
||||||
nd->next = nullptr;
|
nd->next = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return stack_state{ts, ret, total > dalias->get_value()};
|
return stack_state{ts, ret, total > dval};
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace cubescript */
|
} /* namespace cubescript */
|
||||||
|
|
|
@ -18,54 +18,21 @@ bool ident_is_callable(ident const *id) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var_impl::var_impl(
|
var_impl::var_impl(
|
||||||
ident_type tp, string_ref name, var_cb_func f, int fl
|
ident_type tp, string_ref name, int fl
|
||||||
):
|
):
|
||||||
ident_impl{tp, name, fl}, cb_var{std::move(f)}
|
ident_impl{tp, name, fl}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void var_impl::changed(state &cs) {
|
ivar_impl::ivar_impl(string_ref name, integer_type v, int fl):
|
||||||
if (cb_var) {
|
var_impl{ident_type::IVAR, name, fl}, p_storage{v}
|
||||||
switch (p_type) {
|
|
||||||
case ID_IVAR:
|
|
||||||
cb_var(cs, *static_cast<ivar_impl *>(this));
|
|
||||||
break;
|
|
||||||
case ID_FVAR:
|
|
||||||
cb_var(cs, *static_cast<fvar_impl *>(this));
|
|
||||||
break;
|
|
||||||
case ID_SVAR:
|
|
||||||
cb_var(cs, *static_cast<svar_impl *>(this));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ivar_impl::ivar_impl(
|
|
||||||
string_ref name, integer_type m, integer_type x, integer_type v, var_cb_func f, int fl
|
|
||||||
):
|
|
||||||
var_impl{
|
|
||||||
ident_type::IVAR, name, std::move(f),
|
|
||||||
fl | ((m > x) ? IDENT_FLAG_READONLY : 0)
|
|
||||||
},
|
|
||||||
p_storage{v}, p_minval{m}, p_maxval{x}, p_overrideval{0}
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
fvar_impl::fvar_impl(
|
fvar_impl::fvar_impl(string_ref name, float_type v, int fl):
|
||||||
string_ref name, float_type m, float_type x, float_type v, var_cb_func f, int fl
|
var_impl{ident_type::FVAR, name, fl}, p_storage{v}
|
||||||
):
|
|
||||||
var_impl{
|
|
||||||
ident_type::FVAR, name, std::move(f),
|
|
||||||
fl | ((m > x) ? IDENT_FLAG_READONLY : 0)
|
|
||||||
},
|
|
||||||
p_storage{v}, p_minval{m}, p_maxval{x}, p_overrideval{0}
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
svar_impl::svar_impl(
|
svar_impl::svar_impl(string_ref name, string_ref v, int fl):
|
||||||
string_ref name, string_ref v, string_ref ov, var_cb_func f, int fl
|
var_impl{ident_type::SVAR, name, fl}, p_storage{v}
|
||||||
):
|
|
||||||
var_impl{ident_type::SVAR, name, std::move(f), fl},
|
|
||||||
p_storage{v}, p_overrideval{ov}
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
alias_impl::alias_impl(
|
alias_impl::alias_impl(
|
||||||
|
@ -153,11 +120,9 @@ void alias_stack::set_arg(alias *a, thread_state &ts, any_value &v) {
|
||||||
node->val_s = std::move(v);
|
node->val_s = std::move(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
void alias_stack::set_alias(alias *a, thread_state &ts, any_value &v) {
|
void alias_stack::set_alias(alias *, thread_state &, any_value &v) {
|
||||||
node->val_s = std::move(v);
|
node->val_s = std::move(v);
|
||||||
node->code = bcode_ref{};
|
node->code = bcode_ref{};
|
||||||
auto *ai = static_cast<alias_impl *>(a);
|
|
||||||
ai->p_flags = (ai->p_flags & ts.pstate->identflags) | ts.pstate->identflags;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* public interface */
|
/* public interface */
|
||||||
|
|
|
@ -16,6 +16,12 @@ enum {
|
||||||
ID_NOT, ID_AND, ID_OR
|
ID_NOT, ID_AND, ID_OR
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum {
|
||||||
|
IDENT_FLAG_UNKNOWN = 1 << 0,
|
||||||
|
IDENT_FLAG_ARG = 1 << 1,
|
||||||
|
IDENT_FLAG_READONLY = 1 << 2
|
||||||
|
};
|
||||||
|
|
||||||
struct ident_stack {
|
struct ident_stack {
|
||||||
any_value val_s;
|
any_value val_s;
|
||||||
bcode_ref code;
|
bcode_ref code;
|
||||||
|
@ -64,38 +70,25 @@ struct ident_impl {
|
||||||
bool ident_is_callable(ident const *id);
|
bool ident_is_callable(ident const *id);
|
||||||
|
|
||||||
struct var_impl: ident_impl {
|
struct var_impl: ident_impl {
|
||||||
var_impl(
|
var_impl(ident_type tp, string_ref name, int flags);
|
||||||
ident_type tp, string_ref name, var_cb_func func, int flags = 0
|
|
||||||
);
|
|
||||||
|
|
||||||
var_cb_func cb_var;
|
|
||||||
|
|
||||||
void changed(state &cs);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ivar_impl: var_impl, integer_var {
|
struct ivar_impl: var_impl, integer_var {
|
||||||
ivar_impl(
|
ivar_impl(string_ref n, integer_type v, int flags);
|
||||||
string_ref n, integer_type m, integer_type x, integer_type v, var_cb_func f, int flags
|
|
||||||
);
|
|
||||||
|
|
||||||
integer_type p_storage, p_minval, p_maxval, p_overrideval;
|
integer_type p_storage;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fvar_impl: var_impl, float_var {
|
struct fvar_impl: var_impl, float_var {
|
||||||
fvar_impl(
|
fvar_impl(string_ref n, float_type v, int flags);
|
||||||
string_ref n, float_type m, float_type x, float_type v,
|
|
||||||
var_cb_func f, int flags
|
|
||||||
);
|
|
||||||
|
|
||||||
float_type p_storage, p_minval, p_maxval, p_overrideval;
|
float_type p_storage;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct svar_impl: var_impl, string_var {
|
struct svar_impl: var_impl, string_var {
|
||||||
svar_impl(
|
svar_impl(string_ref n, string_ref v, int flags);
|
||||||
string_ref n, string_ref v, string_ref ov, var_cb_func f, int flags
|
|
||||||
);
|
|
||||||
|
|
||||||
string_ref p_storage, p_overrideval;
|
string_ref p_storage;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct alias_impl: ident_impl, alias {
|
struct alias_impl: ident_impl, alias {
|
||||||
|
|
|
@ -83,12 +83,12 @@ state::state(alloc_func func, void *data) {
|
||||||
throw internal_error{"invalid dummy index"};
|
throw internal_error{"invalid dummy index"};
|
||||||
}
|
}
|
||||||
|
|
||||||
id = new_ivar("numargs", MAX_ARGUMENTS, 0, 0);
|
id = new_ivar("numargs", 0, true);
|
||||||
if (id->get_index() != ID_IDX_NUMARGS) {
|
if (id->get_index() != ID_IDX_NUMARGS) {
|
||||||
throw internal_error{"invalid numargs index"};
|
throw internal_error{"invalid numargs index"};
|
||||||
}
|
}
|
||||||
|
|
||||||
id = new_ivar("dbgalias", 0, 1000, 4);
|
id = new_ivar("dbgalias", 4);
|
||||||
if (id->get_index() != ID_IDX_DBGALIAS) {
|
if (id->get_index() != ID_IDX_DBGALIAS) {
|
||||||
throw internal_error{"invalid dbgalias index"};
|
throw internal_error{"invalid dbgalias index"};
|
||||||
}
|
}
|
||||||
|
@ -289,45 +289,38 @@ LIBCUBESCRIPT_EXPORT std::span<ident const *> state::get_idents() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
LIBCUBESCRIPT_EXPORT integer_var *state::new_ivar(
|
LIBCUBESCRIPT_EXPORT integer_var *state::new_ivar(
|
||||||
std::string_view n, integer_type m, integer_type x, integer_type v,
|
std::string_view n, integer_type v, bool read_only
|
||||||
var_cb_func f, int flags
|
|
||||||
) {
|
) {
|
||||||
auto *iv = p_tstate->istate->create<ivar_impl>(
|
auto *iv = p_tstate->istate->create<ivar_impl>(
|
||||||
string_ref{p_tstate->istate, n}, m, x, v, std::move(f), flags
|
string_ref{p_tstate->istate, n}, v,
|
||||||
|
read_only ? IDENT_FLAG_READONLY : 0
|
||||||
);
|
);
|
||||||
add_ident(iv, iv);
|
add_ident(iv, iv);
|
||||||
return iv;
|
return iv;
|
||||||
}
|
}
|
||||||
|
|
||||||
LIBCUBESCRIPT_EXPORT float_var *state::new_fvar(
|
LIBCUBESCRIPT_EXPORT float_var *state::new_fvar(
|
||||||
std::string_view n, float_type m, float_type x, float_type v,
|
std::string_view n, float_type v, bool read_only
|
||||||
var_cb_func f, int flags
|
|
||||||
) {
|
) {
|
||||||
auto *fv = p_tstate->istate->create<fvar_impl>(
|
auto *fv = p_tstate->istate->create<fvar_impl>(
|
||||||
string_ref{p_tstate->istate, n}, m, x, v, std::move(f), flags
|
string_ref{p_tstate->istate, n}, v,
|
||||||
|
read_only ? IDENT_FLAG_READONLY : 0
|
||||||
);
|
);
|
||||||
add_ident(fv, fv);
|
add_ident(fv, fv);
|
||||||
return fv;
|
return fv;
|
||||||
}
|
}
|
||||||
|
|
||||||
LIBCUBESCRIPT_EXPORT string_var *state::new_svar(
|
LIBCUBESCRIPT_EXPORT string_var *state::new_svar(
|
||||||
std::string_view n, std::string_view v, var_cb_func f, int flags
|
std::string_view n, std::string_view v, bool read_only
|
||||||
) {
|
) {
|
||||||
auto *sv = p_tstate->istate->create<svar_impl>(
|
auto *sv = p_tstate->istate->create<svar_impl>(
|
||||||
string_ref{p_tstate->istate, n}, string_ref{p_tstate->istate, v},
|
string_ref{p_tstate->istate, n}, string_ref{p_tstate->istate, v},
|
||||||
string_ref{p_tstate->istate, ""}, std::move(f), flags
|
read_only ? IDENT_FLAG_READONLY : 0
|
||||||
);
|
);
|
||||||
add_ident(sv, sv);
|
add_ident(sv, sv);
|
||||||
return sv;
|
return sv;
|
||||||
}
|
}
|
||||||
|
|
||||||
LIBCUBESCRIPT_EXPORT void state::touch_var(std::string_view name) {
|
|
||||||
ident *id = get_ident(name);
|
|
||||||
if (id && id->is_var()) {
|
|
||||||
static_cast<var_impl *>(id->p_impl)->changed(*this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LIBCUBESCRIPT_EXPORT void state::set_alias(
|
LIBCUBESCRIPT_EXPORT void state::set_alias(
|
||||||
std::string_view name, any_value v
|
std::string_view name, any_value v
|
||||||
) {
|
) {
|
||||||
|
@ -361,7 +354,7 @@ LIBCUBESCRIPT_EXPORT void state::set_alias(
|
||||||
throw error{*this, "cannot alias invalid name '%s'", name.data()};
|
throw error{*this, "cannot alias invalid name '%s'", name.data()};
|
||||||
} else {
|
} else {
|
||||||
auto *a = p_tstate->istate->create<alias_impl>(
|
auto *a = p_tstate->istate->create<alias_impl>(
|
||||||
*this, string_ref{p_tstate->istate, name}, std::move(v), identflags
|
*this, string_ref{p_tstate->istate, name}, std::move(v), 0
|
||||||
);
|
);
|
||||||
add_ident(a, a);
|
add_ident(a, a);
|
||||||
}
|
}
|
||||||
|
|
|
@ -226,8 +226,6 @@ bool exec_alias(
|
||||||
}
|
}
|
||||||
auto oldargs = anargs->get_value();
|
auto oldargs = anargs->get_value();
|
||||||
anargs->set_value(integer_type(callargs));
|
anargs->set_value(integer_type(callargs));
|
||||||
int oldflags = ts.pstate->identflags;
|
|
||||||
ts.pstate->identflags |= a->get_flags()&IDENT_FLAG_OVERRIDDEN;
|
|
||||||
ident_link aliaslink = {a, ts.callstack, uargs};
|
ident_link aliaslink = {a, ts.callstack, uargs};
|
||||||
ts.callstack = &aliaslink;
|
ts.callstack = &aliaslink;
|
||||||
if (!aast.node->code) {
|
if (!aast.node->code) {
|
||||||
|
@ -242,7 +240,6 @@ bool exec_alias(
|
||||||
bcode_ref coderef = aast.node->code;
|
bcode_ref coderef = aast.node->code;
|
||||||
auto cleanup = [&]() {
|
auto cleanup = [&]() {
|
||||||
ts.callstack = aliaslink.next;
|
ts.callstack = aliaslink.next;
|
||||||
ts.pstate->identflags = oldflags;
|
|
||||||
auto amask = aliaslink.usedargs;
|
auto amask = aliaslink.usedargs;
|
||||||
for (std::size_t i = 0; i < callargs; i++) {
|
for (std::size_t i = 0; i < callargs; i++) {
|
||||||
ts.get_astack(
|
ts.get_astack(
|
||||||
|
|
Loading…
Reference in New Issue