diff --git a/src/cs_ident.cc b/src/cs_ident.cc index 0b3dcd8..a3025ed 100644 --- a/src/cs_ident.cc +++ b/src/cs_ident.cc @@ -5,6 +5,16 @@ namespace cscript { +cs_ident_impl::cs_ident_impl(cs_ident_type tp, cs_strref nm, int fl): + p_name{nm}, p_type{int(tp)}, p_flags{fl} +{} + +cs_var_impl::cs_var_impl( + cs_ident_type tp, cs_strref name, cs_var_cb f, int fl +): + cs_ident_impl{tp, name, fl}, cb_var{std::move(f)} +{} + void cs_var_impl::changed(cs_state &cs) { if (cb_var) { switch (p_type) { @@ -23,6 +33,79 @@ void cs_var_impl::changed(cs_state &cs) { } } +cs_ivar_impl::cs_ivar_impl( + cs_strref name, cs_int m, cs_int x, cs_int v, cs_var_cb f, int fl +): + cs_var_impl{ + cs_ident_type::IVAR, name, std::move(f), + fl | ((m > x) ? CS_IDF_READONLY : 0) + }, + p_storage{v}, p_minval{m}, p_maxval{x}, p_overrideval{0} +{} + +cs_fvar_impl::cs_fvar_impl( + cs_strref name, cs_float m, cs_float x, cs_float v, cs_var_cb f, int fl +): + cs_var_impl{ + cs_ident_type::FVAR, name, std::move(f), + fl | ((m > x) ? CS_IDF_READONLY : 0) + }, + p_storage{v}, p_minval{m}, p_maxval{x}, p_overrideval{0} +{} + +cs_svar_impl::cs_svar_impl( + cs_strref name, cs_strref v, cs_strref ov, cs_var_cb f, int fl +): + cs_var_impl{cs_ident_type::SVAR, name, std::move(f), fl}, + p_storage{v}, p_overrideval{ov} +{} + +cs_alias_impl::cs_alias_impl( + cs_state &cs, cs_strref name, cs_strref a, int fl +): + cs_ident_impl{cs_ident_type::ALIAS, name, fl}, + p_acode{nullptr}, p_astack{nullptr}, p_val{cs} +{ + p_val.set_str(a); +} + +cs_alias_impl::cs_alias_impl( + cs_state &cs, cs_strref name, std::string_view a, int fl +): + cs_ident_impl{cs_ident_type::ALIAS, name, fl}, + p_acode{nullptr}, p_astack{nullptr}, p_val{cs} +{ + p_val.set_str(a); +} + +cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, cs_int a, int fl): + cs_ident_impl{cs_ident_type::ALIAS, name, fl}, + p_acode{nullptr}, p_astack{nullptr}, p_val{cs} +{ + p_val.set_int(a); +} + +cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, cs_float a, int fl): + cs_ident_impl{cs_ident_type::ALIAS, name, fl}, + p_acode{nullptr}, p_astack{nullptr}, p_val{cs} +{ + p_val.set_float(a); +} + +cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, int fl): + cs_ident_impl{cs_ident_type::ALIAS, name, fl}, + p_acode{nullptr}, p_astack{nullptr}, p_val{cs} +{ + p_val.set_none(); +} + +cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, cs_value v, int fl): + cs_ident_impl{cs_ident_type::ALIAS, name, fl}, + p_acode{nullptr}, p_astack{nullptr}, p_val{cs} +{ + p_val = v; +} + void cs_alias_impl::push_arg(cs_value &v, cs_ident_stack &st, bool um) { if (p_astack == &st) { /* prevent cycles and unnecessary code elsewhere */ @@ -104,6 +187,13 @@ cs_bcode *cs_alias_impl::compile_code(cs_state &cs) { return p_acode; } +cs_command_impl::cs_command_impl( + cs_strref name, cs_strref args, int nargs, cs_command_cb f +): + cs_ident_impl{cs_ident_type::COMMAND, name, 0}, + p_cargs{args}, p_cb_cftv{std::move(f)}, p_numargs{nargs} +{} + bool ident_is_used_arg(cs_ident *id, cs_state &cs) { if (!cs.p_callstack) { return true; @@ -111,4 +201,174 @@ bool ident_is_used_arg(cs_ident *id, cs_state &cs) { return cs.p_callstack->usedargs & (1 << id->get_index()); } +/* public interface */ + +LIBCUBESCRIPT_EXPORT bool cs_ident::is_alias() const { + return get_type() == cs_ident_type::ALIAS; +} + +LIBCUBESCRIPT_EXPORT cs_alias *cs_ident::get_alias() { + if (!is_alias()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT cs_alias const *cs_ident::get_alias() const { + if (!is_alias()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT bool cs_ident::is_command() const { + return get_type() == cs_ident_type::COMMAND; +} + +LIBCUBESCRIPT_EXPORT cs_command *cs_ident::get_command() { + if (!is_command()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT cs_command const *cs_ident::get_command() const { + if (!is_command()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT bool cs_ident::is_special() const { + return get_type() == cs_ident_type::SPECIAL; +} + +LIBCUBESCRIPT_EXPORT bool cs_ident::is_var() const { + switch (get_type()) { + case cs_ident_type::IVAR: + case cs_ident_type::FVAR: + case cs_ident_type::SVAR: + return true; + default: + break; + } + return false; +} + +LIBCUBESCRIPT_EXPORT cs_var *cs_ident::get_var() { + if (!is_var()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT cs_var const *cs_ident::get_var() const { + if (!is_var()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT bool cs_ident::is_ivar() const { + return get_type() == cs_ident_type::IVAR; +} + +LIBCUBESCRIPT_EXPORT cs_ivar *cs_ident::get_ivar() { + if (!is_ivar()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT cs_ivar const *cs_ident::get_ivar() const { + if (!is_ivar()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT bool cs_ident::is_fvar() const { + return get_type() == cs_ident_type::FVAR; +} + +LIBCUBESCRIPT_EXPORT cs_fvar *cs_ident::get_fvar() { + if (!is_fvar()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT cs_fvar const *cs_ident::get_fvar() const { + if (!is_fvar()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT bool cs_ident::is_svar() const { + return get_type() == cs_ident_type::SVAR; +} + +LIBCUBESCRIPT_EXPORT cs_svar *cs_ident::get_svar() { + if (!is_svar()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT cs_svar const *cs_ident::get_svar() const { + if (!is_svar()) { + return nullptr; + } + return static_cast(this); +} + +LIBCUBESCRIPT_EXPORT cs_int cs_ivar::get_val_min() const { + return static_cast(this)->p_minval; +} + +LIBCUBESCRIPT_EXPORT cs_int cs_ivar::get_val_max() const { + return static_cast(this)->p_maxval; +} + +LIBCUBESCRIPT_EXPORT cs_int cs_ivar::get_value() const { + return static_cast(this)->p_storage; +} + +LIBCUBESCRIPT_EXPORT void cs_ivar::set_value(cs_int val) { + static_cast(this)->p_storage = val; +} + +LIBCUBESCRIPT_EXPORT cs_float cs_fvar::get_val_min() const { + return static_cast(this)->p_minval; +} + +LIBCUBESCRIPT_EXPORT cs_float cs_fvar::get_val_max() const { + return static_cast(this)->p_maxval; +} + +LIBCUBESCRIPT_EXPORT cs_float cs_fvar::get_value() const { + return static_cast(this)->p_storage; +} + +LIBCUBESCRIPT_EXPORT void cs_fvar::set_value(cs_float val) { + static_cast(this)->p_storage = val; +} + +LIBCUBESCRIPT_EXPORT cs_strref cs_svar::get_value() const { + return static_cast(this)->p_storage; +} + +LIBCUBESCRIPT_EXPORT void cs_svar::set_value(cs_strref val) { + static_cast(this)->p_storage = val; +} + +LIBCUBESCRIPT_EXPORT std::string_view cs_command::get_args() const { + return static_cast(this)->p_cargs; +} + +LIBCUBESCRIPT_EXPORT int cs_command::get_num_args() const { + return static_cast(this)->p_numargs; +} + } /* namespace cscript */ diff --git a/src/cubescript.cc b/src/cubescript.cc index 6fc39ff..d017c90 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -20,232 +20,6 @@ bool cs_check_num(std::string_view s) { } } -cs_ident_impl::cs_ident_impl(cs_ident_type tp, cs_strref nm, int fl): - p_name(nm), p_type(int(tp)), p_flags(fl) -{} - -cs_var_impl::cs_var_impl(cs_ident_type tp, cs_strref name, cs_var_cb f, int fl): - cs_ident_impl(tp, name, fl), cb_var(std::move(f)) -{} - -cs_ivar_impl::cs_ivar_impl( - cs_strref name, cs_int m, cs_int x, cs_int v, cs_var_cb f, int fl -): - cs_var_impl(cs_ident_type::IVAR, name, std::move(f), fl | ((m > x) ? CS_IDF_READONLY : 0)), - p_storage(v), p_minval(m), p_maxval(x), p_overrideval(0) -{} - -cs_fvar_impl::cs_fvar_impl( - cs_strref name, cs_float m, cs_float x, cs_float v, cs_var_cb f, int fl -): - cs_var_impl(cs_ident_type::FVAR, name, std::move(f), fl | ((m > x) ? CS_IDF_READONLY : 0)), - p_storage(v), p_minval(m), p_maxval(x), p_overrideval(0) -{} - -cs_svar_impl::cs_svar_impl(cs_strref name, cs_strref v, cs_strref ov, cs_var_cb f, int fl): - cs_var_impl(cs_ident_type::SVAR, name, std::move(f), fl), - p_storage{v}, p_overrideval{ov} -{} - -cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, cs_strref a, int fl): - cs_ident_impl(cs_ident_type::ALIAS, name, fl), - p_acode(nullptr), p_astack(nullptr), p_val{cs} -{ - p_val.set_str(a); -} -cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, std::string_view a, int fl): - cs_ident_impl(cs_ident_type::ALIAS, name, fl), - p_acode(nullptr), p_astack(nullptr), p_val{cs} -{ - p_val.set_str(a); -} -cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, cs_int a, int fl): - cs_ident_impl(cs_ident_type::ALIAS, name, fl), - p_acode(nullptr), p_astack(nullptr), p_val{cs} -{ - p_val.set_int(a); -} -cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, cs_float a, int fl): - cs_ident_impl(cs_ident_type::ALIAS, name, fl), - p_acode(nullptr), p_astack(nullptr), p_val{cs} -{ - p_val.set_float(a); -} -cs_alias_impl::cs_alias_impl(cs_state &cs, cs_strref name, int fl): - cs_ident_impl(cs_ident_type::ALIAS, name, fl), - p_acode(nullptr), p_astack(nullptr), p_val{cs} -{ - p_val.set_none(); -} -/* FIXME: use cs rather than val's cs */ -cs_alias_impl::cs_alias_impl(cs_state &, cs_strref name, cs_value v, int fl): - cs_ident_impl(cs_ident_type::ALIAS, name, fl), - p_acode(nullptr), p_astack(nullptr), p_val(v) -{} - -cs_command_impl::cs_command_impl( - cs_strref name, cs_strref args, int nargs, cs_command_cb f -): - cs_ident_impl(cs_ident_type::COMMAND, name, 0), - p_cargs(args), p_cb_cftv(std::move(f)), p_numargs(nargs) -{} - -bool cs_ident::is_alias() const { - return get_type() == cs_ident_type::ALIAS; -} - -cs_alias *cs_ident::get_alias() { - if (!is_alias()) { - return nullptr; - } - return static_cast(this); -} - -cs_alias const *cs_ident::get_alias() const { - if (!is_alias()) { - return nullptr; - } - return static_cast(this); -} - -bool cs_ident::is_command() const { - return get_type() == cs_ident_type::COMMAND; -} - -cs_command *cs_ident::get_command() { - if (!is_command()) { - return nullptr; - } - return static_cast(this); -} - -cs_command const *cs_ident::get_command() const { - if (!is_command()) { - return nullptr; - } - return static_cast(this); -} - -bool cs_ident::is_special() const { - return get_type() == cs_ident_type::SPECIAL; -} - -bool cs_ident::is_var() const { - cs_ident_type tp = get_type(); - return (tp >= cs_ident_type::IVAR) && (tp <= cs_ident_type::SVAR); -} - -cs_var *cs_ident::get_var() { - if (!is_var()) { - return nullptr; - } - return static_cast(this); -} - -cs_var const *cs_ident::get_var() const { - if (!is_var()) { - return nullptr; - } - return static_cast(this); -} - -bool cs_ident::is_ivar() const { - return get_type() == cs_ident_type::IVAR; -} - -cs_ivar *cs_ident::get_ivar() { - if (!is_ivar()) { - return nullptr; - } - return static_cast(this); -} - -cs_ivar const *cs_ident::get_ivar() const { - if (!is_ivar()) { - return nullptr; - } - return static_cast(this); -} - -bool cs_ident::is_fvar() const { - return get_type() == cs_ident_type::FVAR; -} - -cs_fvar *cs_ident::get_fvar() { - if (!is_fvar()) { - return nullptr; - } - return static_cast(this); -} - -cs_fvar const *cs_ident::get_fvar() const { - if (!is_fvar()) { - return nullptr; - } - return static_cast(this); -} - -bool cs_ident::is_svar() const { - return get_type() == cs_ident_type::SVAR; -} - -cs_svar *cs_ident::get_svar() { - if (!is_svar()) { - return nullptr; - } - return static_cast(this); -} - -cs_svar const *cs_ident::get_svar() const { - if (!is_svar()) { - return nullptr; - } - return static_cast(this); -} - -cs_int cs_ivar::get_val_min() const { - return static_cast(this)->p_minval; -} -cs_int cs_ivar::get_val_max() const { - return static_cast(this)->p_maxval; -} - -cs_int cs_ivar::get_value() const { - return static_cast(this)->p_storage; -} -void cs_ivar::set_value(cs_int val) { - static_cast(this)->p_storage = val; -} - -cs_float cs_fvar::get_val_min() const { - return static_cast(this)->p_minval; -} -cs_float cs_fvar::get_val_max() const { - return static_cast(this)->p_maxval; -} - -cs_float cs_fvar::get_value() const { - return static_cast(this)->p_storage; -} -void cs_fvar::set_value(cs_float val) { - static_cast(this)->p_storage = val; -} - -cs_strref cs_svar::get_value() const { - return static_cast(this)->p_storage; -} -void cs_svar::set_value(cs_strref val) { - static_cast(this)->p_storage = val; -} - -std::string_view cs_command::get_args() const { - return static_cast(this)->p_cargs; -} - -int cs_command::get_num_args() const { - return static_cast(this)->p_numargs; -} - void cs_init_lib_base(cs_state &cs); static void *cs_default_alloc(void *, void *p, size_t, size_t ns) {