From b9b344cba658604fe1632b34447e73d3fa602391 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Tue, 23 Mar 2021 23:29:32 +0100 Subject: [PATCH] remove cs_ namespace in all API --- include/cubescript/cubescript.hh | 602 ++++++++--------- include/cubescript/cubescript_conf.hh | 10 +- src/cs_bcode.cc | 70 +- src/cs_bcode.hh | 96 +-- src/cs_error.cc | 28 +- src/cs_gen.cc | 640 +++++++++--------- src/cs_ident.cc | 262 ++++---- src/cs_ident.hh | 110 ++-- src/cs_parser.cc | 68 +- src/cs_parser.hh | 4 +- src/cs_state.cc | 443 ++++++------- src/cs_state.hh | 56 +- src/cs_std.hh | 24 +- src/cs_strman.cc | 60 +- src/cs_strman.hh | 28 +- src/cs_val.cc | 344 +++++----- src/cs_vm.cc | 892 +++++++++++++------------- src/cs_vm.hh | 89 ++- src/lib_base.cc | 100 +-- src/lib_list.cc | 216 +++---- src/lib_math.cc | 184 +++--- src/lib_str.cc | 74 +-- tools/edit_fallback.hh | 6 +- tools/edit_linenoise.hh | 10 +- tools/edit_readline.hh | 14 +- tools/repl.cc | 50 +- 26 files changed, 2242 insertions(+), 2238 deletions(-) diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index f122442..cb15111 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -37,16 +37,16 @@ namespace cscript { -static_assert(std::is_integral_v, "cs_int must be integral"); -static_assert(std::is_signed_v, "cs_int must be signed"); -static_assert(std::is_floating_point_v, "cs_float must be floating point"); +static_assert(std::is_integral_v, "integer_type must be integral"); +static_assert(std::is_signed_v, "integer_type must be signed"); +static_assert(std::is_floating_point_v, "float_type must be floating point"); -struct cs_internal_error: std::runtime_error { +struct internal_error: std::runtime_error { using std::runtime_error::runtime_error; }; template -struct cs_callable { +struct callable { private: struct base { base(base const &); @@ -102,7 +102,7 @@ private: static inline bool f_not_null(CR C::*p) { return !!p; } template - static inline bool f_not_null(cs_callable const &f) { return !!f; } + static inline bool f_not_null(callable const &f) { return !!f; } bool small_storage() { return (static_cast(p_func) == &p_stor); @@ -120,11 +120,11 @@ private: } public: - cs_callable() noexcept: p_func{nullptr} {} - cs_callable(std::nullptr_t) noexcept: p_func{nullptr} {} - cs_callable(std::nullptr_t, alloc_f, void *) noexcept: p_func{nullptr} {} + callable() noexcept: p_func{nullptr} {} + callable(std::nullptr_t) noexcept: p_func{nullptr} {} + callable(std::nullptr_t, alloc_f, void *) noexcept: p_func{nullptr} {} - cs_callable(cs_callable &&f) noexcept { + callable(callable &&f) noexcept { if (!f.p_func) { p_func = nullptr; } else if (f.small_storage()) { @@ -137,7 +137,7 @@ public: } template - cs_callable(F func, alloc_f af, void *ud) { + callable(F func, alloc_f af, void *ud) { if (!f_not_null(func)) { return; } @@ -161,9 +161,9 @@ public: } } - cs_callable &operator=(cs_callable const &) = delete; + callable &operator=(callable const &) = delete; - cs_callable &operator=(cs_callable &&f) noexcept { + callable &operator=(callable &&f) noexcept { cleanup(); if (f.p_func == nullptr) { p_func = nullptr; @@ -177,23 +177,23 @@ public: return *this; } - cs_callable &operator=(std::nullptr_t) noexcept { + callable &operator=(std::nullptr_t) noexcept { cleanup(); p_func = nullptr; return *this; } template - cs_callable &operator=(F &&func) { - cs_callable{std::forward(func)}.swap(*this); + callable &operator=(F &&func) { + callable{std::forward(func)}.swap(*this); return *this; } - ~cs_callable() { + ~callable() { cleanup(); } - void swap(cs_callable &f) noexcept { + void swap(callable &f) noexcept { std::aligned_storage_t tmp_stor; if (small_storage() && f.small_storage()) { auto *t = as_base(&tmp_stor); @@ -241,73 +241,73 @@ public: } }; -using cs_alloc_cb = void *(*)(void *, void *, size_t, size_t); +using alloc_func = void *(*)(void *, void *, size_t, size_t); -struct cs_state; -struct cs_ident; -struct cs_value; -struct cs_var; +struct state; +struct ident; +struct any_value; +struct global_var; -using cs_hook_cb = cs_callable; -using cs_var_cb = cs_callable; -using cs_vprint_cb = cs_callable; -using cs_command_cb = cs_callable< - void, cs_state &, std::span, cs_value & +using hook_func = callable; +using var_cb_func = callable; +using var_print_func = callable; +using command_func = callable< + void, state &, std::span, any_value & >; enum { - CS_IDF_PERSIST = 1 << 0, - CS_IDF_OVERRIDE = 1 << 1, - CS_IDF_HEX = 1 << 2, - CS_IDF_READONLY = 1 << 3, - CS_IDF_OVERRIDDEN = 1 << 4, - CS_IDF_UNKNOWN = 1 << 5, - CS_IDF_ARG = 1 << 6 + 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 cs_bcode; -struct cs_shared_state; -struct cs_ident_impl; +struct bcode; +struct internal_state; +struct ident_impl; -struct LIBCUBESCRIPT_EXPORT cs_bcode_ref { - cs_bcode_ref(): +struct LIBCUBESCRIPT_EXPORT bcode_ref { + bcode_ref(): p_code(nullptr) {} - cs_bcode_ref(cs_bcode *v); - cs_bcode_ref(cs_bcode_ref const &v); - cs_bcode_ref(cs_bcode_ref &&v): + bcode_ref(bcode *v); + bcode_ref(bcode_ref const &v); + bcode_ref(bcode_ref &&v): p_code(v.p_code) { v.p_code = nullptr; } - ~cs_bcode_ref(); + ~bcode_ref(); - cs_bcode_ref &operator=(cs_bcode_ref const &v); - cs_bcode_ref &operator=(cs_bcode_ref &&v); + bcode_ref &operator=(bcode_ref const &v); + bcode_ref &operator=(bcode_ref &&v); operator bool() const { return p_code != nullptr; } - operator cs_bcode *() const { return p_code; } + operator bcode *() const { return p_code; } private: - cs_bcode *p_code; + bcode *p_code; }; -LIBCUBESCRIPT_EXPORT bool cs_code_is_empty(cs_bcode *code); +LIBCUBESCRIPT_EXPORT bool code_is_empty(bcode *code); -struct LIBCUBESCRIPT_EXPORT cs_strref { - friend struct cs_value; - friend struct cs_strman; +struct LIBCUBESCRIPT_EXPORT string_ref { + friend struct any_value; + friend struct string_pool; - cs_strref() = delete; - cs_strref(cs_shared_state *cs, std::string_view str); - cs_strref(cs_state &cs, std::string_view str); + string_ref() = delete; + string_ref(internal_state *cs, std::string_view str); + string_ref(state &cs, std::string_view str); - cs_strref(cs_strref const &ref); + string_ref(string_ref const &ref); - ~cs_strref(); + ~string_ref(); - cs_strref &operator=(cs_strref const &ref); + string_ref &operator=(string_ref const &ref); operator std::string_view() const; @@ -322,55 +322,55 @@ struct LIBCUBESCRIPT_EXPORT cs_strref { return std::string_view{*this}.data(); } - bool operator==(cs_strref const &s) const; + bool operator==(string_ref const &s) const; private: /* for internal use only */ - cs_strref(char const *p, cs_shared_state *cs); + string_ref(char const *p, internal_state *cs); - cs_shared_state *p_state; + internal_state *p_state; char const *p_str; }; -enum class cs_value_type { +enum class value_type { NONE = 0, INT, FLOAT, STRING, CODE, IDENT }; -struct LIBCUBESCRIPT_EXPORT cs_value { - cs_value() = delete; - ~cs_value(); +struct LIBCUBESCRIPT_EXPORT any_value { + any_value() = delete; + ~any_value(); - cs_value(cs_state &); - cs_value(cs_shared_state &); + any_value(state &); + any_value(internal_state &); - cs_value(cs_value const &); - cs_value(cs_value &&v); + any_value(any_value const &); + any_value(any_value &&v); - cs_value &operator=(cs_value const &); - cs_value &operator=(cs_value &&); + any_value &operator=(any_value const &); + any_value &operator=(any_value &&); - cs_value_type get_type() const; + value_type get_type() const; - void set_int(cs_int val); - void set_float(cs_float val); + void set_int(integer_type val); + void set_float(float_type val); void set_str(std::string_view val); - void set_str(cs_strref const &val); + void set_str(string_ref const &val); void set_none(); - void set_code(cs_bcode *val); - void set_ident(cs_ident *val); + void set_code(bcode *val); + void set_ident(ident *val); - cs_strref get_str() const; - cs_int get_int() const; - cs_float get_float() const; - cs_bcode *get_code() const; - cs_ident *get_ident() const; - void get_val(cs_value &r) const; + string_ref get_str() const; + integer_type get_int() const; + float_type get_float() const; + bcode *get_code() const; + ident *get_ident() const; + void get_val(any_value &r) const; bool get_bool() const; void force_none(); - cs_float force_float(); - cs_int force_int(); + float_type force_float(); + integer_type force_int(); std::string_view force_str(); bool code_is_empty() const; @@ -378,174 +378,174 @@ struct LIBCUBESCRIPT_EXPORT cs_value { private: template struct stor_t { - cs_shared_state *state; + internal_state *state; T val; }; - cs_shared_state *state() const { + internal_state *get_state() const { return reinterpret_cast const *>(&p_stor)->state; } std::aligned_union_t<1, - stor_t, - stor_t, + stor_t, + stor_t, stor_t, - cs_strref + string_ref > p_stor; - cs_value_type p_type; + value_type p_type; }; -struct cs_ident_stack { - cs_value val_s; - cs_ident_stack *next; +struct ident_stack { + any_value val_s; + ident_stack *next; - cs_ident_stack(cs_state &cs): val_s{cs}, next{nullptr} {} + ident_stack(state &cs): val_s{cs}, next{nullptr} {} }; -struct cs_error; -struct cs_gen_state; +struct error; +struct codegen_state; -enum class cs_ident_type { +enum class ident_type { IVAR = 0, FVAR, SVAR, COMMAND, ALIAS, SPECIAL }; -struct cs_var; -struct cs_ivar; -struct cs_fvar; -struct cs_svar; -struct cs_alias; -struct cs_command; +struct global_var; +struct integer_var; +struct float_var; +struct string_var; +struct alias; +struct command; -struct LIBCUBESCRIPT_EXPORT cs_ident { +struct LIBCUBESCRIPT_EXPORT ident { int get_raw_type() const; - cs_ident_type get_type() const; + ident_type get_type() const; std::string_view get_name() const; int get_flags() const; int get_index() const; bool is_alias() const; - cs_alias *get_alias(); - cs_alias const *get_alias() const; + alias *get_alias(); + alias const *get_alias() const; bool is_command() const; - cs_command *get_command(); - cs_command const *get_command() const; + command *get_command(); + command const *get_command() const; bool is_special() const; bool is_var() const; - cs_var *get_var(); - cs_var const *get_var() const; + global_var *get_var(); + global_var const *get_var() const; bool is_ivar() const; - cs_ivar *get_ivar(); - cs_ivar const *get_ivar() const; + integer_var *get_ivar(); + integer_var const *get_ivar() const; bool is_fvar() const; - cs_fvar *get_fvar(); - cs_fvar const *get_fvar() const; + float_var *get_fvar(); + float_var const *get_fvar() const; bool is_svar() const; - cs_svar *get_svar(); - cs_svar const *get_svar() const; + string_var *get_svar(); + string_var const *get_svar() const; protected: - cs_ident() = default; + ident() = default; private: - friend struct cs_state; + friend struct state; - cs_ident_impl *p_impl{}; + ident_impl *p_impl{}; }; -struct LIBCUBESCRIPT_EXPORT cs_var: cs_ident { +struct LIBCUBESCRIPT_EXPORT global_var: ident { protected: - cs_var() = default; + global_var() = default; }; -struct LIBCUBESCRIPT_EXPORT cs_ivar: cs_var { - cs_int get_val_min() const; - cs_int get_val_max() const; +struct LIBCUBESCRIPT_EXPORT integer_var: global_var { + integer_type get_val_min() const; + integer_type get_val_max() const; - cs_int get_value() const; - void set_value(cs_int val); + integer_type get_value() const; + void set_value(integer_type val); protected: - cs_ivar() = default; + integer_var() = default; }; -struct LIBCUBESCRIPT_EXPORT cs_fvar: cs_var { - cs_float get_val_min() const; - cs_float get_val_max() const; +struct LIBCUBESCRIPT_EXPORT float_var: global_var { + float_type get_val_min() const; + float_type get_val_max() const; - cs_float get_value() const; - void set_value(cs_float val); + float_type get_value() const; + void set_value(float_type val); protected: - cs_fvar() = default; + float_var() = default; }; -struct LIBCUBESCRIPT_EXPORT cs_svar: cs_var { - cs_strref get_value() const; - void set_value(cs_strref val); +struct LIBCUBESCRIPT_EXPORT string_var: global_var { + string_ref get_value() const; + void set_value(string_ref val); protected: - cs_svar() = default; + string_var() = default; }; -struct LIBCUBESCRIPT_EXPORT cs_alias: cs_ident { - cs_value get_value() const; - void get_cval(cs_value &v) const; +struct LIBCUBESCRIPT_EXPORT alias: ident { + any_value get_value() const; + void get_cval(any_value &v) const; protected: - cs_alias() = default; + alias() = default; }; -struct cs_command: cs_ident { +struct command: ident { std::string_view get_args() const; int get_num_args() const; protected: - cs_command() = default; + command() = default; }; -struct cs_ident_link; +struct ident_link; enum { - CS_LIB_MATH = 1 << 0, - CS_LIB_STRING = 1 << 1, - CS_LIB_LIST = 1 << 2, - CS_LIB_ALL = 0b111 + LIB_MATH = 1 << 0, + LIB_STRING = 1 << 1, + LIB_LIST = 1 << 2, + LIB_ALL = 0b111 }; -enum class cs_loop_state { +enum class loop_state { NORMAL = 0, BREAK, CONTINUE }; -struct LIBCUBESCRIPT_EXPORT cs_state { - friend struct cs_error; - friend struct cs_strman; - friend struct cs_strref; - friend struct cs_value; - friend struct cs_gen_state; - friend inline cs_shared_state *cs_get_sstate(cs_state &); +struct LIBCUBESCRIPT_EXPORT state { + friend struct error; + friend struct string_pool; + friend struct string_ref; + friend struct any_value; + friend struct codegen_state; + friend inline internal_state *state_get_internal(state &); - cs_shared_state *p_state; - cs_ident_link *p_callstack = nullptr; + internal_state *p_state; + ident_link *p_callstack = nullptr; int identflags = 0; - cs_state(); - cs_state(cs_alloc_cb func, void *data); - virtual ~cs_state(); + state(); + state(alloc_func func, void *data); + virtual ~state(); - cs_state(cs_state const &) = delete; - cs_state(cs_state &&s) { + state(state const &) = delete; + state(state &&s) { swap(s); } - cs_state &operator=(cs_state const &) = delete; - cs_state &operator=(cs_state &&s) { + state &operator=(state const &) = delete; + state &operator=(state &&s) { swap(s); s.destroy(); return *this; @@ -553,7 +553,7 @@ struct LIBCUBESCRIPT_EXPORT cs_state { void destroy(); - void swap(cs_state &s) { + void swap(state &s) { std::swap(p_state, s.p_state); std::swap(p_callstack, s.p_callstack); std::swap(identflags, s.identflags); @@ -563,217 +563,219 @@ struct LIBCUBESCRIPT_EXPORT cs_state { std::swap(p_callhook, s.p_callhook); } - cs_state new_thread(); + state new_thread(); template - cs_hook_cb set_call_hook(F &&f) { + hook_func set_call_hook(F &&f) { return std::move(set_call_hook( - cs_hook_cb{std::forward(f), callable_alloc, this} + hook_func{std::forward(f), callable_alloc, this} )); } - cs_hook_cb const &get_call_hook() const; - cs_hook_cb &get_call_hook(); + hook_func const &get_call_hook() const; + hook_func &get_call_hook(); template - cs_vprint_cb set_var_printer(F &&f) { + var_print_func set_var_printer(F &&f) { return std::move(set_var_printer( - cs_vprint_cb{std::forward(f), callable_alloc, this} + var_print_func{std::forward(f), callable_alloc, this} )); } - cs_vprint_cb const &get_var_printer() const; + var_print_func const &get_var_printer() const; - void init_libs(int libs = CS_LIB_ALL); + void init_libs(int libs = LIB_ALL); - void clear_override(cs_ident &id); + void clear_override(ident &id); void clear_overrides(); - cs_ident *new_ident(std::string_view name, int flags = CS_IDF_UNKNOWN); - cs_ident *force_ident(cs_value &v); + ident *new_ident(std::string_view name, int flags = IDENT_FLAG_UNKNOWN); + ident *force_ident(any_value &v); template - cs_ivar *new_ivar( - std::string_view name, cs_int m, cs_int x, cs_int v, + integer_var *new_ivar( + std::string_view name, integer_type m, integer_type x, integer_type v, F &&f, int flags = 0 ) { return new_ivar( name, m, x, v, - cs_var_cb{std::forward(f), callable_alloc, this}, flags + var_cb_func{std::forward(f), callable_alloc, this}, flags ); } - cs_ivar *new_ivar(std::string_view name, cs_int m, cs_int x, cs_int v) { - return new_ivar(name, m, x, v, cs_var_cb{}, 0); + 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 - cs_fvar *new_fvar( - std::string_view name, cs_float m, cs_float x, cs_float v, + float_var *new_fvar( + std::string_view name, float_type m, float_type x, float_type v, F &&f, int flags = 0 ) { return new_fvar( name, m, x, v, - cs_var_cb{std::forward(f), callable_alloc, this}, flags + var_cb_func{std::forward(f), callable_alloc, this}, flags ); } - cs_fvar *new_fvar( - std::string_view name, cs_float m, cs_float x, cs_float v + float_var *new_fvar( + std::string_view name, float_type m, float_type x, float_type v ) { - return new_fvar(name, m, x, v, cs_var_cb{}, 0); + return new_fvar(name, m, x, v, var_cb_func{}, 0); } template - cs_svar *new_svar( + string_var *new_svar( std::string_view name, std::string_view v, F &&f, int flags = 0 ) { return new_svar( name, v, - cs_var_cb{std::forward(f), callable_alloc, this}, flags + var_cb_func{std::forward(f), callable_alloc, this}, flags ); } - cs_svar *new_svar(std::string_view name, std::string_view v) { - return new_svar(name, v, cs_var_cb{}, 0); + string_var *new_svar(std::string_view name, std::string_view v) { + return new_svar(name, v, var_cb_func{}, 0); } template - cs_command *new_command( + command *new_command( std::string_view name, std::string_view args, F &&f ) { return new_command( name, args, - cs_command_cb{std::forward(f), callable_alloc, this} + command_func{std::forward(f), callable_alloc, this} ); } - cs_ident *get_ident(std::string_view name); - cs_alias *get_alias(std::string_view name); + ident *get_ident(std::string_view name); + alias *get_alias(std::string_view name); bool have_ident(std::string_view name); - std::span get_idents(); - std::span get_idents() const; + std::span get_idents(); + std::span get_idents() const; void reset_var(std::string_view name); void touch_var(std::string_view name); - void run(cs_bcode *code, cs_value &ret); - void run(std::string_view code, cs_value &ret); - void run(std::string_view code, cs_value &ret, std::string_view source); - void run(cs_ident *id, std::span args, cs_value &ret); + void run(bcode *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(ident *id, std::span args, any_value &ret); - cs_value run(cs_bcode *code); - cs_value run(std::string_view code); - cs_value run(std::string_view code, std::string_view source); - cs_value run(cs_ident *id, std::span args); + any_value run(bcode *code); + any_value run(std::string_view code); + any_value run(std::string_view code, std::string_view source); + any_value run(ident *id, std::span args); - cs_loop_state run_loop(cs_bcode *code, cs_value &ret); - cs_loop_state run_loop(cs_bcode *code); + loop_state run_loop(bcode *code, any_value &ret); + loop_state run_loop(bcode *code); bool is_in_loop() const { return p_inloop; } - void set_alias(std::string_view name, cs_value v); + void set_alias(std::string_view name, any_value v); void set_var_int( - std::string_view name, cs_int v, + std::string_view name, integer_type v, bool dofunc = true, bool doclamp = true ); void set_var_float( - std::string_view name, cs_float v, + std::string_view name, float_type v, bool dofunc = true, bool doclamp = true ); void set_var_str( std::string_view name, std::string_view v, bool dofunc = true ); - void set_var_int_checked(cs_ivar *iv, cs_int v); - void set_var_int_checked(cs_ivar *iv, std::span args); - void set_var_float_checked(cs_fvar *fv, cs_float v); - void set_var_str_checked(cs_svar *fv, std::string_view v); + void set_var_int_checked(integer_var *iv, integer_type v); + void set_var_int_checked(integer_var *iv, std::span args); + void set_var_float_checked(float_var *fv, float_type v); + void set_var_str_checked(string_var *fv, std::string_view v); - std::optional get_var_int(std::string_view name); - std::optional get_var_float(std::string_view name); - std::optional get_var_str(std::string_view name); + std::optional get_var_int(std::string_view name); + std::optional get_var_float(std::string_view name); + std::optional get_var_str(std::string_view name); - std::optional get_var_min_int(std::string_view name); - std::optional get_var_max_int(std::string_view name); + std::optional get_var_min_int(std::string_view name); + std::optional get_var_max_int(std::string_view name); - std::optional get_var_min_float(std::string_view name); - std::optional get_var_max_float(std::string_view name); + std::optional get_var_min_float(std::string_view name); + std::optional get_var_max_float(std::string_view name); - std::optional get_alias_val(std::string_view name); + std::optional get_alias_val(std::string_view name); - void print_var(cs_var const &v) const; + void print_var(global_var const &v) const; private: - cs_hook_cb set_call_hook(cs_hook_cb func); - cs_vprint_cb set_var_printer(cs_vprint_cb func); + hook_func set_call_hook(hook_func func); + var_print_func set_var_printer(var_print_func func); - cs_ivar *new_ivar( - std::string_view n, cs_int m, cs_int x, cs_int v, - cs_var_cb f, int flags + integer_var *new_ivar( + std::string_view n, integer_type m, integer_type x, integer_type v, + var_cb_func f, int flags ); - cs_fvar *new_fvar( - std::string_view n, cs_float m, cs_float x, cs_float v, - cs_var_cb 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 ); - cs_svar *new_svar( - std::string_view n, std::string_view v, cs_var_cb f, int flags + string_var *new_svar( + std::string_view n, std::string_view v, var_cb_func f, int flags ); - cs_command *new_command( - std::string_view name, std::string_view args, cs_command_cb func + command *new_command( + std::string_view name, std::string_view args, command_func func ); static void *callable_alloc( void *data, void *p, std::size_t os, std::size_t ns ) { - return static_cast(data)->alloc(p, os, ns); + return static_cast(data)->alloc(p, os, ns); } - LIBCUBESCRIPT_LOCAL cs_state(cs_shared_state *s); + LIBCUBESCRIPT_LOCAL state(internal_state *s); - cs_ident *add_ident(cs_ident *id, cs_ident_impl *impl); + ident *add_ident(ident *id, ident_impl *impl); void *alloc(void *ptr, size_t olds, size_t news); - cs_gen_state *p_pstate = nullptr; + codegen_state *p_pstate = nullptr; void *p_errbuf = nullptr; int p_inloop = 0; bool p_owner = false; - cs_hook_cb p_callhook; + hook_func p_callhook; }; -struct cs_stack_state_node { - cs_stack_state_node const *next; - cs_ident const *id; +struct stack_state_node { + stack_state_node const *next; + ident const *id; int index; }; -struct cs_stack_state { - cs_stack_state() = delete; - cs_stack_state(cs_state &cs, cs_stack_state_node *nd = nullptr, bool gap = false); - cs_stack_state(cs_stack_state const &) = delete; - cs_stack_state(cs_stack_state &&st); - ~cs_stack_state(); +struct stack_state { + stack_state() = delete; + stack_state(state &cs, stack_state_node *nd = nullptr, bool gap = false); + stack_state(stack_state const &) = delete; + stack_state(stack_state &&st); + ~stack_state(); - cs_stack_state &operator=(cs_stack_state const &) = delete; - cs_stack_state &operator=(cs_stack_state &&); + stack_state &operator=(stack_state const &) = delete; + stack_state &operator=(stack_state &&); - cs_stack_state_node const *get() const; + stack_state_node const *get() const; bool gap() const; private: - cs_state &p_state; - cs_stack_state_node *p_node; + state &p_state; + stack_state_node *p_node; bool p_gap; }; -struct LIBCUBESCRIPT_EXPORT cs_error { - friend struct cs_state; +struct LIBCUBESCRIPT_EXPORT error { + friend struct state; - cs_error() = delete; - cs_error(cs_error const &) = delete; - cs_error(cs_error &&v): + error() = delete; + error(error const &) = delete; + error(error &&v): p_errmsg(v.p_errmsg), p_stack(std::move(v.p_stack)) {} @@ -781,15 +783,15 @@ struct LIBCUBESCRIPT_EXPORT cs_error { return p_errmsg; } - cs_stack_state &get_stack() { + stack_state &get_stack() { return p_stack; } - cs_stack_state const &get_stack() const { + stack_state const &get_stack() const { return p_stack; } - cs_error(cs_state &cs, std::string_view msg): + error(state &cs, std::string_view msg): p_errmsg(), p_stack(cs) { char *sp; @@ -801,7 +803,7 @@ struct LIBCUBESCRIPT_EXPORT cs_error { } template - cs_error(cs_state &cs, std::string_view msg, A const &...args): + error(state &cs, std::string_view msg, A const &...args): p_errmsg(), p_stack(cs) { std::size_t sz = msg.size() + 64; @@ -810,7 +812,7 @@ struct LIBCUBESCRIPT_EXPORT cs_error { buf = request_buf(cs, sz, sp); int written = std::snprintf(buf, sz, msg.data(), args...); if (written <= 0) { - throw cs_internal_error{"format error"}; + throw internal_error{"format error"}; } else if (std::size_t(written) <= sz) { break; } @@ -821,41 +823,41 @@ struct LIBCUBESCRIPT_EXPORT cs_error { } private: - cs_stack_state save_stack(cs_state &cs); - char *request_buf(cs_state &cs, std::size_t bufs, char *&sp); + stack_state save_stack(state &cs); + char *request_buf(state &cs, std::size_t bufs, char *&sp); std::string_view p_errmsg; - cs_stack_state p_stack; + stack_state p_stack; }; -struct LIBCUBESCRIPT_EXPORT cs_stacked_value: cs_value { - cs_stacked_value(cs_state &cs, cs_ident *id = nullptr); - ~cs_stacked_value(); +struct LIBCUBESCRIPT_EXPORT stacked_value: any_value { + stacked_value(state &cs, ident *id = nullptr); + ~stacked_value(); - cs_stacked_value(cs_stacked_value const &) = delete; - cs_stacked_value(cs_stacked_value &&) = delete; + stacked_value(stacked_value const &) = delete; + stacked_value(stacked_value &&) = delete; - cs_stacked_value &operator=(cs_stacked_value const &) = delete; - cs_stacked_value &operator=(cs_stacked_value &&v) = delete; + stacked_value &operator=(stacked_value const &) = delete; + stacked_value &operator=(stacked_value &&v) = delete; - cs_stacked_value &operator=(cs_value const &v); - cs_stacked_value &operator=(cs_value &&v); + stacked_value &operator=(any_value const &v); + stacked_value &operator=(any_value &&v); - bool set_alias(cs_ident *id); - cs_alias *get_alias() const; + bool set_alias(ident *id); + alias *get_alias() const; bool has_alias() const; bool push(); bool pop(); private: - cs_alias *p_a; - cs_ident_stack p_stack; + alias *p_a; + ident_stack p_stack; bool p_pushed; }; -struct LIBCUBESCRIPT_EXPORT cs_list_parser { - cs_list_parser(cs_state &cs, std::string_view s = std::string_view{}): +struct LIBCUBESCRIPT_EXPORT list_parser { + list_parser(state &cs, std::string_view s = std::string_view{}): p_state{&cs}, p_input_beg{s.data()}, p_input_end{s.data() + s.size()} {} @@ -871,7 +873,7 @@ struct LIBCUBESCRIPT_EXPORT cs_list_parser { bool parse(); std::size_t count(); - cs_strref get_item() const; + string_ref get_item() const; std::string_view get_raw_item() const { return p_item; } std::string_view get_quoted_item() const { return p_quoted_item; } @@ -879,7 +881,7 @@ struct LIBCUBESCRIPT_EXPORT cs_list_parser { void skip_until_item(); private: - cs_state *p_state; + state *p_state; char const *p_input_beg, *p_input_end; std::string_view p_item{}; @@ -887,28 +889,28 @@ private: }; -LIBCUBESCRIPT_EXPORT char const *cs_parse_string( - cs_state &cs, std::string_view str, size_t &nlines +LIBCUBESCRIPT_EXPORT char const *parse_string( + state &cs, std::string_view str, size_t &nlines ); -inline char const *cs_parse_string( - cs_state &cs, std::string_view str +inline char const *parse_string( + state &cs, std::string_view str ) { size_t nlines; - return cs_parse_string(cs, str, nlines); + return parse_string(cs, str, nlines); } -LIBCUBESCRIPT_EXPORT char const *cs_parse_word( - cs_state &cs, std::string_view str +LIBCUBESCRIPT_EXPORT char const *parse_word( + state &cs, std::string_view str ); -LIBCUBESCRIPT_EXPORT cs_strref cs_concat_values( - cs_state &cs, std::span vals, +LIBCUBESCRIPT_EXPORT string_ref concat_values( + state &cs, std::span vals, std::string_view sep = std::string_view{} ); template -inline R cs_escape_string(R writer, std::string_view str) { +inline R escape_string(R writer, std::string_view str) { *writer++ = '"'; for (auto c: str) { switch (c) { @@ -925,7 +927,7 @@ inline R cs_escape_string(R writer, std::string_view str) { } template -inline R cs_unescape_string(R writer, std::string_view str) { +inline R unescape_string(R writer, std::string_view str) { for (auto it = str.begin(); it != str.end(); ++it) { if (*it == '^') { ++it; @@ -963,7 +965,7 @@ inline R cs_unescape_string(R writer, std::string_view str) { } template -inline R cs_print_stack(R writer, cs_stack_state const &st) { +inline R print_stack(R writer, stack_state const &st) { char buf[32] = {0}; auto nd = st.get(); while (nd) { diff --git a/include/cubescript/cubescript_conf.hh b/include/cubescript/cubescript_conf.hh index 1e1f970..b8d2928 100644 --- a/include/cubescript/cubescript_conf.hh +++ b/include/cubescript/cubescript_conf.hh @@ -2,12 +2,12 @@ #define LIBCUBESCRIPT_CUBESCRIPT_CONF_HH namespace cscript { - using cs_int = int; - using cs_float = float; + using integer_type = int; + using float_type = float; - constexpr auto const CS_INT_FORMAT = "%d"; - constexpr auto const CS_FLOAT_FORMAT = "%.7g"; - constexpr auto const CS_ROUND_FLOAT_FORMAT = "%.1f"; + constexpr auto const INT_FORMAT = "%d"; + constexpr auto const FLOAT_FORMAT = "%.7g"; + constexpr auto const ROUND_FLOAT_FORMAT = "%.1f"; } /* namespace cscript */ #endif /* LIBCUBESCRIPT_CUBESCRIPT_CONF_HH */ diff --git a/src/cs_bcode.cc b/src/cs_bcode.cc index 965fc7e..9171e02 100644 --- a/src/cs_bcode.cc +++ b/src/cs_bcode.cc @@ -5,29 +5,29 @@ namespace cscript { /* public API impls */ -LIBCUBESCRIPT_EXPORT cs_bcode_ref::cs_bcode_ref(cs_bcode *v): p_code(v) { - bcode_ref(v->get_raw()); +LIBCUBESCRIPT_EXPORT bcode_ref::bcode_ref(bcode *v): p_code(v) { + bcode_addref(v->get_raw()); } -LIBCUBESCRIPT_EXPORT cs_bcode_ref::cs_bcode_ref(cs_bcode_ref const &v): +LIBCUBESCRIPT_EXPORT bcode_ref::bcode_ref(bcode_ref const &v): p_code(v.p_code) { - bcode_ref(p_code->get_raw()); + bcode_addref(p_code->get_raw()); } -LIBCUBESCRIPT_EXPORT cs_bcode_ref::~cs_bcode_ref() { +LIBCUBESCRIPT_EXPORT bcode_ref::~bcode_ref() { bcode_unref(p_code->get_raw()); } -LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=( - cs_bcode_ref const &v +LIBCUBESCRIPT_EXPORT bcode_ref &bcode_ref::operator=( + bcode_ref const &v ) { bcode_unref(p_code->get_raw()); p_code = v.p_code; - bcode_ref(p_code->get_raw()); + bcode_addref(p_code->get_raw()); return *this; } -LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=(cs_bcode_ref &&v) { +LIBCUBESCRIPT_EXPORT bcode_ref &bcode_ref::operator=(bcode_ref &&v) { bcode_unref(p_code->get_raw()); p_code = v.p_code; v.p_code = nullptr; @@ -37,18 +37,18 @@ LIBCUBESCRIPT_EXPORT cs_bcode_ref &cs_bcode_ref::operator=(cs_bcode_ref &&v) { /* private funcs */ struct bcode_hdr { - cs_shared_state *cs; /* needed to construct the allocator */ + internal_state *cs; /* needed to construct the allocator */ std::size_t asize; /* alloc size of the bytecode block */ - cs_bcode bc; /* CS_CODE_START + refcount */ + bcode bc; /* BC_INST_START + refcount */ }; /* returned address is the 'init' member of the header */ -std::uint32_t *bcode_alloc(cs_state &cs, std::size_t sz) { - auto a = cs_allocator{cs}; +std::uint32_t *bcode_alloc(state &cs, std::size_t sz) { + auto a = std_allocator{cs}; std::size_t hdrs = sizeof(bcode_hdr) / sizeof(std::uint32_t); auto p = a.allocate(sz + hdrs - 1); bcode_hdr *hdr = reinterpret_cast(p); - hdr->cs = cs_get_sstate(cs); + hdr->cs = state_get_internal(cs); hdr->asize = sz + hdrs - 1; return p + hdrs - 1; } @@ -57,7 +57,7 @@ std::uint32_t *bcode_alloc(cs_state &cs, std::size_t sz) { static inline void bcode_free(std::uint32_t *bc) { auto *rp = bc + 1 - (sizeof(bcode_hdr) / sizeof(std::uint32_t)); bcode_hdr *hdr = reinterpret_cast(rp); - cs_allocator{hdr->cs}.deallocate(rp, hdr->asize); + std_allocator{hdr->cs}.deallocate(rp, hdr->asize); } void bcode_incr(std::uint32_t *bc) { @@ -71,19 +71,19 @@ void bcode_decr(std::uint32_t *bc) { } } -void bcode_ref(std::uint32_t *code) { +void bcode_addref(std::uint32_t *code) { if (!code) { return; } - if ((*code & CS_CODE_OP_MASK) == CS_CODE_START) { + if ((*code & BC_INST_OP_MASK) == BC_INST_START) { bcode_incr(code); return; } - switch (code[-1]&CS_CODE_OP_MASK) { - case CS_CODE_START: + switch (code[-1]&BC_INST_OP_MASK) { + case BC_INST_START: bcode_incr(&code[-1]); break; - case CS_CODE_OFFSET: + case BC_INST_OFFSET: code -= std::ptrdiff_t(code[-1] >> 8); bcode_incr(code); break; @@ -94,15 +94,15 @@ void bcode_unref(std::uint32_t *code) { if (!code) { return; } - if ((*code & CS_CODE_OP_MASK) == CS_CODE_START) { + if ((*code & BC_INST_OP_MASK) == BC_INST_START) { bcode_decr(code); return; } - switch (code[-1]&CS_CODE_OP_MASK) { - case CS_CODE_START: + switch (code[-1]&BC_INST_OP_MASK) { + case BC_INST_START: bcode_decr(&code[-1]); break; - case CS_CODE_OFFSET: + case BC_INST_OFFSET: code -= std::ptrdiff_t(code[-1] >> 8); bcode_decr(code); break; @@ -111,25 +111,25 @@ void bcode_unref(std::uint32_t *code) { /* empty fallbacks */ -static std::uint32_t emptyrets[CS_VAL_ANY] = { - CS_RET_NULL, CS_RET_INT, CS_RET_FLOAT, CS_RET_STRING +static std::uint32_t emptyrets[VAL_ANY] = { + BC_RET_NULL, BC_RET_INT, BC_RET_FLOAT, BC_RET_STRING }; -empty_block *bcode_init_empty(cs_shared_state *cs) { - auto a = cs_allocator{cs}; - auto *p = a.allocate(CS_VAL_ANY); - for (std::size_t i = 0; i < CS_VAL_ANY; ++i) { - p[i].init.init = CS_CODE_START + 0x100; - p[i].code = CS_CODE_EXIT | emptyrets[i]; +empty_block *bcode_init_empty(internal_state *cs) { + auto a = std_allocator{cs}; + auto *p = a.allocate(VAL_ANY); + for (std::size_t i = 0; i < VAL_ANY; ++i) { + p[i].init.init = BC_INST_START + 0x100; + p[i].code = BC_INST_EXIT | emptyrets[i]; } return p; } -void bcode_free_empty(cs_shared_state *cs, empty_block *empty) { - cs_allocator{cs}.deallocate(empty, CS_VAL_ANY); +void bcode_free_empty(internal_state *cs, empty_block *empty) { + std_allocator{cs}.deallocate(empty, VAL_ANY); }; -cs_bcode *bcode_get_empty(empty_block *empty, std::size_t val) { +bcode *bcode_get_empty(empty_block *empty, std::size_t val) { return &empty[val].init + 1; } diff --git a/src/cs_bcode.hh b/src/cs_bcode.hh index 7ad32e4..39645bb 100644 --- a/src/cs_bcode.hh +++ b/src/cs_bcode.hh @@ -8,7 +8,7 @@ namespace cscript { -struct cs_bcode { +struct bcode { std::uint32_t init; std::uint32_t *get_raw() { @@ -21,72 +21,72 @@ struct cs_bcode { }; enum { - CS_VAL_NULL = 0, CS_VAL_INT, CS_VAL_FLOAT, CS_VAL_STRING, - CS_VAL_ANY, CS_VAL_CODE, CS_VAL_IDENT, CS_VAL_WORD, - CS_VAL_POP, CS_VAL_COND + VAL_NULL = 0, VAL_INT, VAL_FLOAT, VAL_STRING, + VAL_ANY, VAL_CODE, VAL_IDENT, VAL_WORD, + VAL_POP, VAL_COND }; /* instruction: uint32 [length 24][retflag 2][opcode 6] */ enum { - CS_CODE_START = 0, - CS_CODE_OFFSET, - CS_CODE_NULL, CS_CODE_TRUE, CS_CODE_FALSE, CS_CODE_NOT, - CS_CODE_POP, - CS_CODE_ENTER, CS_CODE_ENTER_RESULT, - CS_CODE_EXIT, CS_CODE_RESULT_ARG, - CS_CODE_VAL, CS_CODE_VAL_INT, - CS_CODE_DUP, - CS_CODE_BOOL, - CS_CODE_BLOCK, CS_CODE_EMPTY, - CS_CODE_COMPILE, CS_CODE_COND, - CS_CODE_FORCE, - CS_CODE_RESULT, - CS_CODE_IDENT, CS_CODE_IDENT_U, CS_CODE_IDENT_ARG, - CS_CODE_COM, CS_CODE_COM_C, CS_CODE_COM_V, - CS_CODE_CONC, CS_CODE_CONC_W, CS_CODE_CONC_M, - CS_CODE_SVAR, CS_CODE_SVAR1, - CS_CODE_IVAR, CS_CODE_IVAR1, CS_CODE_IVAR2, CS_CODE_IVAR3, - CS_CODE_FVAR, CS_CODE_FVAR1, - CS_CODE_LOOKUP, CS_CODE_LOOKUP_U, CS_CODE_LOOKUP_ARG, - CS_CODE_LOOKUP_M, CS_CODE_LOOKUP_MU, CS_CODE_LOOKUP_MARG, - CS_CODE_ALIAS, CS_CODE_ALIAS_U, CS_CODE_ALIAS_ARG, - CS_CODE_CALL, CS_CODE_CALL_U, CS_CODE_CALL_ARG, - CS_CODE_PRINT, - CS_CODE_LOCAL, - CS_CODE_DO, CS_CODE_DO_ARGS, - CS_CODE_JUMP, CS_CODE_JUMP_B, CS_CODE_JUMP_RESULT, - CS_CODE_BREAK, + BC_INST_START = 0, + BC_INST_OFFSET, + BC_INST_NULL, BC_INST_TRUE, BC_INST_FALSE, BC_INST_NOT, + BC_INST_POP, + BC_INST_ENTER, BC_INST_ENTER_RESULT, + BC_INST_EXIT, BC_INST_RESULT_ARG, + BC_INST_VAL, BC_INST_VAL_INT, + BC_INST_DUP, + BC_INST_BOOL, + BC_INST_BLOCK, BC_INST_EMPTY, + BC_INST_COMPILE, BC_INST_COND, + BC_INST_FORCE, + BC_INST_RESULT, + BC_INST_IDENT, BC_INST_IDENT_U, BC_INST_IDENT_ARG, + BC_INST_COM, BC_INST_COM_C, BC_INST_COM_V, + BC_INST_CONC, BC_INST_CONC_W, BC_INST_CONC_M, + BC_INST_SVAR, BC_INST_SVAR1, + BC_INST_IVAR, BC_INST_IVAR1, BC_INST_IVAR2, BC_INST_IVAR3, + BC_INST_FVAR, BC_INST_FVAR1, + BC_INST_LOOKUP, BC_INST_LOOKUP_U, BC_INST_LOOKUP_ARG, + BC_INST_LOOKUP_M, BC_INST_LOOKUP_MU, BC_INST_LOOKUP_MARG, + BC_INST_ALIAS, BC_INST_ALIAS_U, BC_INST_ALIAS_ARG, + BC_INST_CALL, BC_INST_CALL_U, BC_INST_CALL_ARG, + BC_INST_PRINT, + BC_INST_LOCAL, + BC_INST_DO, BC_INST_DO_ARGS, + BC_INST_JUMP, BC_INST_JUMP_B, BC_INST_JUMP_RESULT, + BC_INST_BREAK, - CS_CODE_OP_MASK = 0x3F, - CS_CODE_RET = 6, - CS_CODE_RET_MASK = 0xC0, + BC_INST_OP_MASK = 0x3F, + BC_INST_RET = 6, + BC_INST_RET_MASK = 0xC0, /* return type flags */ - CS_RET_NULL = CS_VAL_NULL << CS_CODE_RET, - CS_RET_STRING = CS_VAL_STRING << CS_CODE_RET, - CS_RET_INT = CS_VAL_INT << CS_CODE_RET, - CS_RET_FLOAT = CS_VAL_FLOAT << CS_CODE_RET, + BC_RET_NULL = VAL_NULL << BC_INST_RET, + BC_RET_STRING = VAL_STRING << BC_INST_RET, + BC_RET_INT = VAL_INT << BC_INST_RET, + BC_RET_FLOAT = VAL_FLOAT << BC_INST_RET, - /* CS_CODE_JUMP_B, CS_CODE_JUMP_RESULT */ - CS_CODE_FLAG_TRUE = 1 << CS_CODE_RET, - CS_CODE_FLAG_FALSE = 0 << CS_CODE_RET + /* BC_INST_JUMP_B, BC_INST_JUMP_RESULT */ + BC_INST_FLAG_TRUE = 1 << BC_INST_RET, + BC_INST_FLAG_FALSE = 0 << BC_INST_RET }; -std::uint32_t *bcode_alloc(cs_state &cs, std::size_t sz); +std::uint32_t *bcode_alloc(state &cs, std::size_t sz); void bcode_incr(std::uint32_t *code); void bcode_decr(std::uint32_t *code); -void bcode_ref(std::uint32_t *code); +void bcode_addref(std::uint32_t *code); void bcode_unref(std::uint32_t *code); struct empty_block { - cs_bcode init; + bcode init; std::uint32_t code; }; -empty_block *bcode_init_empty(cs_shared_state *cs); -void bcode_free_empty(cs_shared_state *cs, empty_block *empty); -cs_bcode *bcode_get_empty(empty_block *empty, std::size_t val); +empty_block *bcode_init_empty(internal_state *cs); +void bcode_free_empty(internal_state *cs, empty_block *empty); +bcode *bcode_get_empty(empty_block *empty, std::size_t val); } /* namespace cscript */ diff --git a/src/cs_error.cc b/src/cs_error.cc index 3aec79b..3fb5fab 100644 --- a/src/cs_error.cc +++ b/src/cs_error.cc @@ -4,11 +4,11 @@ namespace cscript { -LIBCUBESCRIPT_EXPORT char *cs_error::request_buf( - cs_state &cs, std::size_t bufs, char *&sp +LIBCUBESCRIPT_EXPORT char *error::request_buf( + state &cs, std::size_t bufs, char *&sp ) { - cs_charbuf &cb = *static_cast(cs.p_errbuf); - cs_gen_state *gs = cs.p_pstate; + charbuf &cb = *static_cast(cs.p_errbuf); + codegen_state *gs = cs.p_pstate; cb.clear(); std::size_t sz = 0; if (gs) { @@ -30,7 +30,7 @@ LIBCUBESCRIPT_EXPORT char *cs_error::request_buf( nsz = std::snprintf(cb.data(), sz, "%zu: ", gs->current_line); } if (nsz <= 0) { - throw cs_internal_error{"format error"}; + throw internal_error{"format error"}; } else if (std::size_t(nsz) < sz) { sz = std::size_t(nsz); break; @@ -43,24 +43,24 @@ LIBCUBESCRIPT_EXPORT char *cs_error::request_buf( return &cb[sz]; } -LIBCUBESCRIPT_EXPORT cs_stack_state cs_error::save_stack(cs_state &cs) { - cs_ivar *dalias = static_cast(cs.p_state->identmap[DbgaliasIdx]); +LIBCUBESCRIPT_EXPORT stack_state error::save_stack(state &cs) { + integer_var *dalias = static_cast(cs.p_state->identmap[DbgaliasIdx]); if (!dalias->get_value()) { - return cs_stack_state(cs, nullptr, !!cs.p_callstack); + return stack_state(cs, nullptr, !!cs.p_callstack); } int total = 0, depth = 0; - for (cs_ident_link *l = cs.p_callstack; l; l = l->next) { + for (ident_link *l = cs.p_callstack; l; l = l->next) { total++; } if (!total) { - return cs_stack_state(cs, nullptr, false); + return stack_state(cs, nullptr, false); } - cs_stack_state_node *st = cs.p_state->create_array( + stack_state_node *st = cs.p_state->create_array( std::min(total, dalias->get_value()) ); - cs_stack_state_node *ret = st, *nd = st; + stack_state_node *ret = st, *nd = st; ++st; - for (cs_ident_link *l = cs.p_callstack; l; l = l->next) { + for (ident_link *l = cs.p_callstack; l; l = l->next) { ++depth; if (depth < dalias->get_value()) { nd->id = l->id; @@ -77,7 +77,7 @@ LIBCUBESCRIPT_EXPORT cs_stack_state cs_error::save_stack(cs_state &cs) { nd->next = nullptr; } } - return cs_stack_state(cs, ret, total > dalias->get_value()); + return stack_state(cs, ret, total > dalias->get_value()); } } /* namespace cscript */ diff --git a/src/cs_gen.cc b/src/cs_gen.cc index 3393389..6429723 100644 --- a/src/cs_gen.cc +++ b/src/cs_gen.cc @@ -10,10 +10,10 @@ namespace cscript { -std::string_view cs_gen_state::get_str() { +std::string_view codegen_state::get_str() { size_t nl; char const *beg = source; - source = cs_parse_string( + source = parse_string( cs, std::string_view{source, std::size_t(send - source)}, nl ); current_line += nl - 1; @@ -21,13 +21,13 @@ std::string_view cs_gen_state::get_str() { return ret.substr(1, ret.size() - 2); } -cs_charbuf cs_gen_state::get_str_dup() { - cs_charbuf buf{cs}; - cs_unescape_string(std::back_inserter(buf), get_str()); +charbuf codegen_state::get_str_dup() { + charbuf buf{cs}; + unescape_string(std::back_inserter(buf), get_str()); return buf; } -std::string_view cs_gen_state::read_macro_name() { +std::string_view codegen_state::read_macro_name() { char const *op = source; char c = current(); if (!isalpha(c) && (c != '_')) { @@ -39,7 +39,7 @@ std::string_view cs_gen_state::read_macro_name() { return std::string_view{op, std::size_t(source - op)}; } -char cs_gen_state::skip_until(std::string_view chars) { +char codegen_state::skip_until(std::string_view chars) { char c = current(); while (c && (chars.find(c) == std::string_view::npos)) { next_char(); @@ -48,7 +48,7 @@ char cs_gen_state::skip_until(std::string_view chars) { return c; } -char cs_gen_state::skip_until(char cf) { +char codegen_state::skip_until(char cf) { char c = current(); while (c && (c != cf)) { next_char(); @@ -57,19 +57,19 @@ char cs_gen_state::skip_until(char cf) { return c; } -static bool cs_is_hspace(char c) { +static bool is_hspace(char c) { return (c == ' ') || (c == '\t') || (c == '\r'); } -void cs_gen_state::skip_comments() { +void codegen_state::skip_comments() { for (;;) { - for (char c = current(); cs_is_hspace(c); c = current()) { + for (char c = current(); is_hspace(c); c = current()) { next_char(); } if (current() == '\\') { char c = current(1); if ((c != '\r') && (c != '\n')) { - throw cs_error(cs, "invalid line break"); + throw error(cs, "invalid line break"); } /* skip backslash */ next_char(); @@ -91,66 +91,66 @@ void cs_gen_state::skip_comments() { } } -std::string_view cs_gen_state::get_word() { +std::string_view codegen_state::get_word() { char const *beg = source; - source = cs_parse_word( + source = parse_word( cs, std::string_view{source, std::size_t(send - source)} ); return std::string_view{beg, std::size_t(source - beg)}; } -static inline int cs_ret_code(int type, int def = 0) { - if (type >= CS_VAL_ANY) { - return (type == CS_VAL_STRING) ? CS_RET_STRING : def; +static inline int ret_code(int type, int def = 0) { + if (type >= VAL_ANY) { + return (type == VAL_STRING) ? BC_RET_STRING : def; } - return type << CS_CODE_RET; + return type << BC_INST_RET; } static void compilestatements( - cs_gen_state &gs, int rettype, int brak = '\0', int prevargs = 0 + codegen_state &gs, int rettype, int brak = '\0', int prevargs = 0 ); static inline std::pair compileblock( - cs_gen_state &gs, std::string_view p, size_t line, - int rettype = CS_RET_NULL, int brak = '\0' + codegen_state &gs, std::string_view p, size_t line, + int rettype = BC_RET_NULL, int brak = '\0' ); -void cs_gen_state::gen_int(std::string_view word) { +void codegen_state::gen_int(std::string_view word) { gen_int(parse_int(word)); } -void cs_gen_state::gen_float(std::string_view word) { +void codegen_state::gen_float(std::string_view word) { gen_float(parse_float(word)); } -void cs_gen_state::gen_value(int wordtype, std::string_view word, int line) { +void codegen_state::gen_value(int wordtype, std::string_view word, int line) { switch (wordtype) { - case CS_VAL_ANY: + case VAL_ANY: if (!word.empty()) { gen_str(word); } else { gen_null(); } break; - case CS_VAL_STRING: + case VAL_STRING: gen_str(word); break; - case CS_VAL_FLOAT: + case VAL_FLOAT: gen_float(word); break; - case CS_VAL_INT: + case VAL_INT: gen_int(word); break; - case CS_VAL_COND: + case VAL_COND: if (!word.empty()) { compileblock(*this, word, line); } else { gen_null(); } break; - case CS_VAL_CODE: + case VAL_CODE: compileblock(*this, word, line); break; - case CS_VAL_IDENT: + case VAL_IDENT: gen_ident(word); break; default: @@ -158,16 +158,16 @@ void cs_gen_state::gen_value(int wordtype, std::string_view word, int line) { } } -static inline void compileblock(cs_gen_state &gs) { - gs.code.push_back(CS_CODE_EMPTY); +static inline void compileblock(codegen_state &gs) { + gs.code.push_back(BC_INST_EMPTY); } static inline std::pair compileblock( - cs_gen_state &gs, std::string_view p, size_t line, int rettype, int brak + codegen_state &gs, std::string_view p, size_t line, int rettype, int brak ) { size_t start = gs.code.size(); - gs.code.push_back(CS_CODE_BLOCK); - gs.code.push_back(CS_CODE_OFFSET | ((start + 2) << 8)); + gs.code.push_back(BC_INST_BLOCK); + gs.code.push_back(BC_INST_OFFSET | ((start + 2) << 8)); size_t retline = line; if (!p.empty()) { char const *op = gs.source, *oe = gs.send; @@ -175,7 +175,7 @@ static inline std::pair compileblock( gs.source = p.data(); gs.send = p.data() + p.size(); gs.current_line = line; - compilestatements(gs, CS_VAL_ANY, brak); + compilestatements(gs, VAL_ANY, brak); p = std::string_view{gs.source, std::size_t(gs.send - gs.source)}; retline = gs.current_line; gs.source = op; @@ -183,25 +183,25 @@ static inline std::pair compileblock( gs.current_line = oldline; } if (gs.code.size() > start + 2) { - gs.code.push_back(CS_CODE_EXIT | rettype); + gs.code.push_back(BC_INST_EXIT | rettype); gs.code[start] |= uint32_t(gs.code.size() - (start + 1)) << 8; } else { gs.code.resize(start); - gs.code.push_back(CS_CODE_EMPTY | rettype); + gs.code.push_back(BC_INST_EMPTY | rettype); } return std::make_pair(p, retline); } -static inline void compileunescapestr(cs_gen_state &gs) { +static inline void compileunescapestr(codegen_state &gs) { auto str = gs.get_str(); - gs.code.push_back(CS_CODE_VAL | CS_RET_STRING); + gs.code.push_back(BC_INST_VAL | BC_RET_STRING); gs.code.reserve( gs.code.size() + str.size() / sizeof(uint32_t) + 1 ); size_t bufs = (gs.code.capacity() - gs.code.size()) * sizeof(uint32_t); - auto alloc = cs_allocator{gs.cs}; + auto alloc = std_allocator{gs.cs}; auto *buf = alloc.allocate(bufs + 1); - char *wbuf = cs_unescape_string(&buf[0], str); + char *wbuf = unescape_string(&buf[0], str); memset( &buf[wbuf - buf], 0, sizeof(uint32_t) - (wbuf - buf) % sizeof(uint32_t) @@ -213,22 +213,22 @@ static inline void compileunescapestr(cs_gen_state &gs) { } static bool compilearg( - cs_gen_state &gs, int wordtype, int prevargs = MaxResults, - cs_charbuf *word = nullptr + codegen_state &gs, int wordtype, int prevargs = MaxResults, + charbuf *word = nullptr ); -static void compilelookup(cs_gen_state &gs, int ltype, int prevargs = MaxResults) { - cs_charbuf lookup{gs.cs}; +static void compilelookup(codegen_state &gs, int ltype, int prevargs = MaxResults) { + charbuf lookup{gs.cs}; gs.next_char(); switch (gs.current()) { case '(': case '[': - if (!compilearg(gs, CS_VAL_STRING, prevargs)) { + if (!compilearg(gs, VAL_STRING, prevargs)) { goto invalid; } break; case '$': - compilelookup(gs, CS_VAL_STRING, prevargs); + compilelookup(gs, VAL_STRING, prevargs); break; case '\"': lookup = gs.get_str_dup(); @@ -239,93 +239,93 @@ static void compilelookup(cs_gen_state &gs, int ltype, int prevargs = MaxResults if (lookup.empty()) goto invalid; lookup.push_back('\0'); lookupid: - cs_ident *id = gs.cs.new_ident(lookup.str_term()); + ident *id = gs.cs.new_ident(lookup.str_term()); if (id) { switch (id->get_type()) { - case cs_ident_type::IVAR: + case ident_type::IVAR: gs.code.push_back( - CS_CODE_IVAR | cs_ret_code(ltype, CS_RET_INT) | + BC_INST_IVAR | ret_code(ltype, BC_RET_INT) | (id->get_index() << 8) ); switch (ltype) { - case CS_VAL_POP: + case VAL_POP: gs.code.pop_back(); break; - case CS_VAL_CODE: - gs.code.push_back(CS_CODE_COMPILE); + case VAL_CODE: + gs.code.push_back(BC_INST_COMPILE); break; - case CS_VAL_IDENT: - gs.code.push_back(CS_CODE_IDENT_U); + case VAL_IDENT: + gs.code.push_back(BC_INST_IDENT_U); break; } return; - case cs_ident_type::FVAR: + case ident_type::FVAR: gs.code.push_back( - CS_CODE_FVAR | cs_ret_code(ltype, CS_RET_FLOAT) | + BC_INST_FVAR | ret_code(ltype, BC_RET_FLOAT) | (id->get_index() << 8) ); switch (ltype) { - case CS_VAL_POP: + case VAL_POP: gs.code.pop_back(); break; - case CS_VAL_CODE: - gs.code.push_back(CS_CODE_COMPILE); + case VAL_CODE: + gs.code.push_back(BC_INST_COMPILE); break; - case CS_VAL_IDENT: - gs.code.push_back(CS_CODE_IDENT_U); + case VAL_IDENT: + gs.code.push_back(BC_INST_IDENT_U); break; } return; - case cs_ident_type::SVAR: + case ident_type::SVAR: switch (ltype) { - case CS_VAL_POP: + case VAL_POP: return; default: gs.code.push_back( - CS_CODE_SVAR | cs_ret_code(ltype, CS_RET_STRING) | + BC_INST_SVAR | ret_code(ltype, BC_RET_STRING) | (id->get_index() << 8) ); break; } goto done; - case cs_ident_type::ALIAS: + case ident_type::ALIAS: switch (ltype) { - case CS_VAL_POP: + case VAL_POP: return; - case CS_VAL_COND: + case VAL_COND: gs.code.push_back( (id->get_index() < MaxArguments - ? CS_CODE_LOOKUP_MARG - : CS_CODE_LOOKUP_M + ? BC_INST_LOOKUP_MARG + : BC_INST_LOOKUP_M ) | (id->get_index() << 8) ); break; - case CS_VAL_CODE: - case CS_VAL_IDENT: + case VAL_CODE: + case VAL_IDENT: gs.code.push_back( (id->get_index() < MaxArguments - ? CS_CODE_LOOKUP_MARG - : CS_CODE_LOOKUP_M - ) | CS_RET_STRING | (id->get_index() << 8) + ? BC_INST_LOOKUP_MARG + : BC_INST_LOOKUP_M + ) | BC_RET_STRING | (id->get_index() << 8) ); break; default: gs.code.push_back( (id->get_index() < MaxArguments - ? CS_CODE_LOOKUP_ARG - : CS_CODE_LOOKUP - ) | cs_ret_code(ltype, CS_RET_STRING) | + ? BC_INST_LOOKUP_ARG + : BC_INST_LOOKUP + ) | ret_code(ltype, BC_RET_STRING) | (id->get_index() << 8) ); break; } goto done; - case cs_ident_type::COMMAND: { - int comtype = CS_CODE_COM, numargs = 0; + case ident_type::COMMAND: { + int comtype = BC_INST_COM, numargs = 0; if (prevargs >= MaxResults) { - gs.code.push_back(CS_CODE_ENTER); + gs.code.push_back(BC_INST_ENTER); } - auto fmt = static_cast(id)->get_args(); + auto fmt = static_cast(id)->get_args(); for (char c: fmt) { switch (c) { case 'S': @@ -341,7 +341,7 @@ lookupid: numargs++; break; case 'b': - gs.gen_int(std::numeric_limits::min()); + gs.gen_int(std::numeric_limits::min()); numargs++; break; case 'f': @@ -349,7 +349,7 @@ lookupid: numargs++; break; case 'F': - gs.code.push_back(CS_CODE_DUP | CS_RET_FLOAT); + gs.code.push_back(BC_INST_DUP | BC_RET_FLOAT); numargs++; break; case 'E': @@ -375,10 +375,10 @@ lookupid: numargs++; break; case 'C': - comtype = CS_CODE_COM_C; + comtype = BC_INST_COM_C; goto compilecomv; case 'V': - comtype = CS_CODE_COM_V; + comtype = BC_INST_COM_V; goto compilecomv; case '1': case '2': @@ -388,25 +388,25 @@ lookupid: } } gs.code.push_back( - comtype | cs_ret_code(ltype) | (id->get_index() << 8) + comtype | ret_code(ltype) | (id->get_index() << 8) ); gs.code.push_back( (prevargs >= MaxResults - ? CS_CODE_EXIT - : CS_CODE_RESULT_ARG - ) | cs_ret_code(ltype) + ? BC_INST_EXIT + : BC_INST_RESULT_ARG + ) | ret_code(ltype) ); goto done; compilecomv: gs.code.push_back( - comtype | cs_ret_code(ltype) | (numargs << 8) | + comtype | ret_code(ltype) | (numargs << 8) | (id->get_index() << 13) ); gs.code.push_back( (prevargs >= MaxResults - ? CS_CODE_EXIT - : CS_CODE_RESULT_ARG - ) | cs_ret_code(ltype) + ? BC_INST_EXIT + : BC_INST_RESULT_ARG + ) | ret_code(ltype) ); goto done; } @@ -419,41 +419,41 @@ lookupid: } } switch (ltype) { - case CS_VAL_COND: - gs.code.push_back(CS_CODE_LOOKUP_MU); + case VAL_COND: + gs.code.push_back(BC_INST_LOOKUP_MU); break; - case CS_VAL_CODE: - case CS_VAL_IDENT: - gs.code.push_back(CS_CODE_LOOKUP_MU | CS_RET_STRING); + case VAL_CODE: + case VAL_IDENT: + gs.code.push_back(BC_INST_LOOKUP_MU | BC_RET_STRING); break; default: - gs.code.push_back(CS_CODE_LOOKUP_U | cs_ret_code(ltype)); + gs.code.push_back(BC_INST_LOOKUP_U | ret_code(ltype)); break; } done: switch (ltype) { - case CS_VAL_POP: - gs.code.push_back(CS_CODE_POP); + case VAL_POP: + gs.code.push_back(BC_INST_POP); break; - case CS_VAL_CODE: - gs.code.push_back(CS_CODE_COMPILE); + case VAL_CODE: + gs.code.push_back(BC_INST_COMPILE); break; - case CS_VAL_COND: - gs.code.push_back(CS_CODE_COND); + case VAL_COND: + gs.code.push_back(BC_INST_COND); break; - case CS_VAL_IDENT: - gs.code.push_back(CS_CODE_IDENT_U); + case VAL_IDENT: + gs.code.push_back(BC_INST_IDENT_U); break; } return; invalid: switch (ltype) { - case CS_VAL_POP: + case VAL_POP: break; - case CS_VAL_NULL: - case CS_VAL_ANY: - case CS_VAL_WORD: - case CS_VAL_COND: + case VAL_NULL: + case VAL_ANY: + case VAL_WORD: + case VAL_COND: gs.gen_null(); break; default: @@ -462,11 +462,11 @@ invalid: } } -static bool compileblockstr(cs_gen_state &gs, char const *str, char const *send) { +static bool compileblockstr(codegen_state &gs, char const *str, char const *send) { int startc = gs.code.size(); - gs.code.push_back(CS_CODE_VAL | CS_RET_STRING); + gs.code.push_back(BC_INST_VAL | BC_RET_STRING); gs.code.reserve(gs.code.size() + (send - str) / sizeof(uint32_t) + 1); - auto alloc = cs_allocator{gs.cs}; + auto alloc = std_allocator{gs.cs}; auto asz = ((send - str) / sizeof(uint32_t) + 1) * sizeof(uint32_t); char *buf = alloc.allocate(asz); int len = 0; @@ -485,7 +485,7 @@ static bool compileblockstr(cs_gen_state &gs, char const *str, char const *send) break; case '\"': { char const *start = str; - str = cs_parse_string( + str = parse_string( gs.cs, std::string_view{str, send} ); memcpy(&buf[len], start, std::size_t(str - start)); @@ -518,19 +518,19 @@ done: return true; } -static bool compileblocksub(cs_gen_state &gs, int prevargs) { - cs_charbuf lookup{gs.cs}; +static bool compileblocksub(codegen_state &gs, int prevargs) { + charbuf lookup{gs.cs}; switch (gs.current()) { case '(': - if (!compilearg(gs, CS_VAL_ANY, prevargs)) { + if (!compilearg(gs, VAL_ANY, prevargs)) { return false; } break; case '[': - if (!compilearg(gs, CS_VAL_STRING, prevargs)) { + if (!compilearg(gs, VAL_STRING, prevargs)) { return false; } - gs.code.push_back(CS_CODE_LOOKUP_MU); + gs.code.push_back(BC_INST_LOOKUP_MU); break; case '\"': lookup = gs.get_str_dup(); @@ -543,23 +543,23 @@ static bool compileblocksub(cs_gen_state &gs, int prevargs) { } lookup.push_back('\0'); lookupid: - cs_ident *id = gs.cs.new_ident(lookup.str_term()); + ident *id = gs.cs.new_ident(lookup.str_term()); if (id) { switch (id->get_type()) { - case cs_ident_type::IVAR: - gs.code.push_back(CS_CODE_IVAR | (id->get_index() << 8)); + case ident_type::IVAR: + gs.code.push_back(BC_INST_IVAR | (id->get_index() << 8)); goto done; - case cs_ident_type::FVAR: - gs.code.push_back(CS_CODE_FVAR | (id->get_index() << 8)); + case ident_type::FVAR: + gs.code.push_back(BC_INST_FVAR | (id->get_index() << 8)); goto done; - case cs_ident_type::SVAR: - gs.code.push_back(CS_CODE_SVAR | (id->get_index() << 8)); + case ident_type::SVAR: + gs.code.push_back(BC_INST_SVAR | (id->get_index() << 8)); goto done; - case cs_ident_type::ALIAS: + case ident_type::ALIAS: gs.code.push_back( (id->get_index() < MaxArguments - ? CS_CODE_LOOKUP_MARG - : CS_CODE_LOOKUP_M + ? BC_INST_LOOKUP_MARG + : BC_INST_LOOKUP_M ) | (id->get_index() << 8) ); goto done; @@ -568,7 +568,7 @@ lookupid: } } gs.gen_str(lookup.str_term()); - gs.code.push_back(CS_CODE_LOOKUP_MU); + gs.code.push_back(BC_INST_LOOKUP_MU); done: break; } @@ -576,14 +576,14 @@ done: return true; } -static void compileblockmain(cs_gen_state &gs, int wordtype, int prevargs) { +static void compileblockmain(codegen_state &gs, int wordtype, int prevargs) { char const *start = gs.source; size_t curline = gs.current_line; int concs = 0; for (int brak = 1; brak;) { switch (gs.skip_until("@\"/[]")) { case '\0': - throw cs_error(gs.cs, "missing \"]\""); + throw error(gs.cs, "missing \"]\""); return; case '\"': gs.get_str(); @@ -612,14 +612,14 @@ static void compileblockmain(cs_gen_state &gs, int wordtype, int prevargs) { if (brak > level) { continue; } else if (brak < level) { - throw cs_error(gs.cs, "too many @s"); + throw error(gs.cs, "too many @s"); return; } if (!concs && prevargs >= MaxResults) { - gs.code.push_back(CS_CODE_ENTER); + gs.code.push_back(BC_INST_ENTER); } if (concs + 2 > MaxArguments) { - gs.code.push_back(CS_CODE_CONC_W | CS_RET_STRING | (concs << 8)); + gs.code.push_back(BC_INST_CONC_W | BC_RET_STRING | (concs << 8)); concs = 1; } if (compileblockstr(gs, start, esc)) { @@ -644,19 +644,19 @@ static void compileblockmain(cs_gen_state &gs, int wordtype, int prevargs) { if (gs.source - 1 > start) { if (!concs) { switch (wordtype) { - case CS_VAL_POP: + case VAL_POP: return; - case CS_VAL_CODE: - case CS_VAL_COND: { + case VAL_CODE: + case VAL_COND: { auto ret = compileblock(gs, std::string_view{ start, std::size_t(gs.send - start) - }, curline, CS_RET_NULL, ']'); + }, curline, BC_RET_NULL, ']'); gs.source = ret.first.data(); gs.send = ret.first.data() + ret.first.size(); gs.current_line = ret.second; return; } - case CS_VAL_IDENT: + case VAL_IDENT: gs.gen_ident(std::string_view{ start, std::size_t((gs.source - 1) - start) }); @@ -670,43 +670,43 @@ static void compileblockmain(cs_gen_state &gs, int wordtype, int prevargs) { } if (concs) { if (prevargs >= MaxResults) { - gs.code.push_back(CS_CODE_CONC_M | cs_ret_code(wordtype) | (concs << 8)); - gs.code.push_back(CS_CODE_EXIT | cs_ret_code(wordtype)); + gs.code.push_back(BC_INST_CONC_M | ret_code(wordtype) | (concs << 8)); + gs.code.push_back(BC_INST_EXIT | ret_code(wordtype)); } else { - gs.code.push_back(CS_CODE_CONC_W | cs_ret_code(wordtype) | (concs << 8)); + gs.code.push_back(BC_INST_CONC_W | ret_code(wordtype) | (concs << 8)); } } switch (wordtype) { - case CS_VAL_POP: + case VAL_POP: if (concs || gs.source - 1 > start) { - gs.code.push_back(CS_CODE_POP); + gs.code.push_back(BC_INST_POP); } break; - case CS_VAL_COND: + case VAL_COND: if (!concs && gs.source - 1 <= start) { gs.gen_null(); } else { - gs.code.push_back(CS_CODE_COND); + gs.code.push_back(BC_INST_COND); } break; - case CS_VAL_CODE: + case VAL_CODE: if (!concs && gs.source - 1 <= start) { compileblock(gs); } else { - gs.code.push_back(CS_CODE_COMPILE); + gs.code.push_back(BC_INST_COMPILE); } break; - case CS_VAL_IDENT: + case VAL_IDENT: if (!concs && gs.source - 1 <= start) { gs.gen_ident(); } else { - gs.code.push_back(CS_CODE_IDENT_U); + gs.code.push_back(BC_INST_IDENT_U); } break; - case CS_VAL_STRING: - case CS_VAL_NULL: - case CS_VAL_ANY: - case CS_VAL_WORD: + case VAL_STRING: + case VAL_NULL: + case VAL_ANY: + case VAL_WORD: if (!concs && gs.source - 1 <= start) { gs.gen_str(); } @@ -716,7 +716,7 @@ static void compileblockmain(cs_gen_state &gs, int wordtype, int prevargs) { if (gs.source - 1 <= start) { gs.gen_value(wordtype); } else { - gs.code.push_back(CS_CODE_FORCE | (wordtype << CS_CODE_RET)); + gs.code.push_back(BC_INST_FORCE | (wordtype << BC_INST_RET)); } } break; @@ -724,16 +724,16 @@ static void compileblockmain(cs_gen_state &gs, int wordtype, int prevargs) { } static bool compilearg( - cs_gen_state &gs, int wordtype, int prevargs, cs_charbuf *word + codegen_state &gs, int wordtype, int prevargs, charbuf *word ) { gs.skip_comments(); switch (gs.current()) { case '\"': switch (wordtype) { - case CS_VAL_POP: + case VAL_POP: gs.get_str(); break; - case CS_VAL_COND: { + case VAL_COND: { size_t line = gs.current_line; auto s = gs.get_str_dup(); if (!s.empty()) { @@ -744,19 +744,19 @@ static bool compilearg( } break; } - case CS_VAL_CODE: { + case VAL_CODE: { auto s = gs.get_str_dup(); s.push_back('\0'); compileblock(gs, s.str_term(), gs.current_line); break; } - case CS_VAL_WORD: + case VAL_WORD: if (word) { *word = std::move(gs.get_str_dup()); } break; - case CS_VAL_ANY: - case CS_VAL_STRING: + case VAL_ANY: + case VAL_STRING: compileunescapestr(gs); break; default: { @@ -774,31 +774,31 @@ static bool compilearg( case '(': gs.next_char(); if (prevargs >= MaxResults) { - gs.code.push_back(CS_CODE_ENTER); - compilestatements(gs, CS_VAL_ANY, ')'); - gs.code.push_back(CS_CODE_EXIT | cs_ret_code(wordtype)); + gs.code.push_back(BC_INST_ENTER); + compilestatements(gs, VAL_ANY, ')'); + gs.code.push_back(BC_INST_EXIT | ret_code(wordtype)); } else { size_t start = gs.code.size(); - compilestatements(gs, CS_VAL_ANY, ')', prevargs); + compilestatements(gs, VAL_ANY, ')', prevargs); if (gs.code.size() > start) { - gs.code.push_back(CS_CODE_RESULT_ARG | cs_ret_code(wordtype)); + gs.code.push_back(BC_INST_RESULT_ARG | ret_code(wordtype)); } else { gs.gen_value(wordtype); return true; } } switch (wordtype) { - case CS_VAL_POP: - gs.code.push_back(CS_CODE_POP); + case VAL_POP: + gs.code.push_back(BC_INST_POP); break; - case CS_VAL_COND: - gs.code.push_back(CS_CODE_COND); + case VAL_COND: + gs.code.push_back(BC_INST_COND); break; - case CS_VAL_CODE: - gs.code.push_back(CS_CODE_COMPILE); + case VAL_CODE: + gs.code.push_back(BC_INST_COMPILE); break; - case CS_VAL_IDENT: - gs.code.push_back(CS_CODE_IDENT_U); + case VAL_IDENT: + gs.code.push_back(BC_INST_IDENT_U); break; } return true; @@ -808,10 +808,10 @@ static bool compilearg( return true; default: switch (wordtype) { - case CS_VAL_POP: { + case VAL_POP: { return !gs.get_word().empty(); } - case CS_VAL_COND: { + case VAL_COND: { size_t line = gs.current_line; auto s = gs.get_word(); if (s.empty()) { @@ -820,7 +820,7 @@ static bool compilearg( compileblock(gs, s, line); return true; } - case CS_VAL_CODE: { + case VAL_CODE: { size_t line = gs.current_line; auto s = gs.get_word(); if (s.empty()) { @@ -829,7 +829,7 @@ static bool compilearg( compileblock(gs, s, line); return true; } - case CS_VAL_WORD: { + case VAL_WORD: { auto w = gs.get_word(); if (word) { word->clear(); @@ -851,16 +851,16 @@ static bool compilearg( } static void compile_cmd( - cs_gen_state &gs, cs_command_impl *id, bool &more, int rettype, int prevargs + codegen_state &gs, command_impl *id, bool &more, int rettype, int prevargs ) { - int comtype = CS_CODE_COM, numargs = 0, fakeargs = 0; + int comtype = BC_INST_COM, numargs = 0, fakeargs = 0; bool rep = false; auto fmt = id->get_args(); for (auto it = fmt.begin(); it != fmt.end(); ++it) { switch (*it) { case 's': /* string */ if (more) { - more = compilearg(gs, CS_VAL_STRING, prevargs + numargs); + more = compilearg(gs, VAL_STRING, prevargs + numargs); } if (!more) { if (rep) { @@ -872,7 +872,7 @@ static void compile_cmd( int numconc = 1; while ((numargs + numconc) < MaxArguments) { more = compilearg( - gs, CS_VAL_STRING, prevargs + numargs + numconc + gs, VAL_STRING, prevargs + numargs + numconc ); if (!more) { break; @@ -880,14 +880,14 @@ static void compile_cmd( numconc++; } if (numconc > 1) { - gs.code.push_back(CS_CODE_CONC | CS_RET_STRING | (numconc << 8)); + gs.code.push_back(BC_INST_CONC | BC_RET_STRING | (numconc << 8)); } } numargs++; break; case 'i': /* integer */ if (more) { - more = compilearg(gs, CS_VAL_INT, prevargs + numargs); + more = compilearg(gs, VAL_INT, prevargs + numargs); } if (!more) { if (rep) { @@ -900,20 +900,20 @@ static void compile_cmd( break; case 'b': /* integer, INT_MIN default */ if (more) { - more = compilearg(gs, CS_VAL_INT, prevargs + numargs); + more = compilearg(gs, VAL_INT, prevargs + numargs); } if (!more) { if (rep) { break; } - gs.gen_int(std::numeric_limits::min()); + gs.gen_int(std::numeric_limits::min()); fakeargs++; } numargs++; break; case 'f': /* float */ if (more) { - more = compilearg(gs, CS_VAL_FLOAT, prevargs + numargs); + more = compilearg(gs, VAL_FLOAT, prevargs + numargs); } if (!more) { if (rep) { @@ -926,13 +926,13 @@ static void compile_cmd( break; case 'F': /* float, prev-argument default */ if (more) { - more = compilearg(gs, CS_VAL_FLOAT, prevargs + numargs); + more = compilearg(gs, VAL_FLOAT, prevargs + numargs); } if (!more) { if (rep) { break; } - gs.code.push_back(CS_CODE_DUP | CS_RET_FLOAT); + gs.code.push_back(BC_INST_DUP | BC_RET_FLOAT); fakeargs++; } numargs++; @@ -940,7 +940,7 @@ static void compile_cmd( case 't': /* any arg */ if (more) { more = compilearg( - gs, CS_VAL_ANY, + gs, VAL_ANY, prevargs + numargs ); } @@ -955,7 +955,7 @@ static void compile_cmd( break; case 'E': /* condition */ if (more) { - more = compilearg(gs, CS_VAL_COND, prevargs + numargs); + more = compilearg(gs, VAL_COND, prevargs + numargs); } if (!more) { if (rep) { @@ -968,7 +968,7 @@ static void compile_cmd( break; case 'e': /* code */ if (more) { - more = compilearg(gs, CS_VAL_CODE, prevargs + numargs); + more = compilearg(gs, VAL_CODE, prevargs + numargs); } if (!more) { if (rep) { @@ -981,7 +981,7 @@ static void compile_cmd( break; case 'r': /* ident */ if (more) { - more = compilearg(gs, CS_VAL_IDENT, prevargs + numargs); + more = compilearg(gs, VAL_IDENT, prevargs + numargs); } if (!more) { if (rep) { @@ -1001,10 +1001,10 @@ static void compile_cmd( numargs++; break; case 'C': /* concatenated string */ - comtype = CS_CODE_COM_C; + comtype = BC_INST_COM_C; if (more) { while (numargs < MaxArguments) { - more = compilearg(gs, CS_VAL_ANY, prevargs + numargs); + more = compilearg(gs, VAL_ANY, prevargs + numargs); if (!more) { break; } @@ -1013,10 +1013,10 @@ static void compile_cmd( } goto compilecomv; case 'V': /* varargs */ - comtype = CS_CODE_COM_V; + comtype = BC_INST_COM_V; if (more) { while (numargs < MaxArguments) { - more = compilearg(gs, CS_VAL_ANY, prevargs + numargs); + more = compilearg(gs, VAL_ANY, prevargs + numargs); if (!more) { break; } @@ -1034,41 +1034,41 @@ static void compile_cmd( rep = true; } else { while (numargs > MaxArguments) { - gs.code.push_back(CS_CODE_POP); + gs.code.push_back(BC_INST_POP); --numargs; } } break; } } - gs.code.push_back(comtype | cs_ret_code(rettype) | (id->get_index() << 8)); + gs.code.push_back(comtype | ret_code(rettype) | (id->get_index() << 8)); return; compilecomv: gs.code.push_back( - comtype | cs_ret_code(rettype) | (numargs << 8) | (id->get_index() << 13) + comtype | ret_code(rettype) | (numargs << 8) | (id->get_index() << 13) ); } -static void compile_alias(cs_gen_state &gs, cs_alias *id, bool &more, int prevargs) { +static void compile_alias(codegen_state &gs, alias *id, bool &more, int prevargs) { int numargs = 0; while (numargs < MaxArguments) { - more = compilearg(gs, CS_VAL_ANY, prevargs + numargs); + more = compilearg(gs, VAL_ANY, prevargs + numargs); if (!more) { break; } ++numargs; } gs.code.push_back( - (id->get_index() < MaxArguments ? CS_CODE_CALL_ARG : CS_CODE_CALL) + (id->get_index() < MaxArguments ? BC_INST_CALL_ARG : BC_INST_CALL) | (numargs << 8) | (id->get_index() << 13) ); } -static void compile_local(cs_gen_state &gs, bool &more, int prevargs) { +static void compile_local(codegen_state &gs, bool &more, int prevargs) { int numargs = 0; if (more) { while (numargs < MaxArguments) { - more = compilearg(gs, CS_VAL_IDENT, prevargs + numargs); + more = compilearg(gs, VAL_IDENT, prevargs + numargs); if (!more) { break; } @@ -1076,107 +1076,107 @@ static void compile_local(cs_gen_state &gs, bool &more, int prevargs) { } } if (more) { - while ((more = compilearg(gs, CS_VAL_POP))); + while ((more = compilearg(gs, VAL_POP))); } - gs.code.push_back(CS_CODE_LOCAL | (numargs << 8)); + gs.code.push_back(BC_INST_LOCAL | (numargs << 8)); } static void compile_do( - cs_gen_state &gs, bool &more, int prevargs, int rettype, int opcode + codegen_state &gs, bool &more, int prevargs, int rettype, int opcode ) { if (more) { - more = compilearg(gs, CS_VAL_CODE, prevargs); + more = compilearg(gs, VAL_CODE, prevargs); } - gs.code.push_back((more ? opcode : CS_CODE_NULL) | cs_ret_code(rettype)); + gs.code.push_back((more ? opcode : BC_INST_NULL) | ret_code(rettype)); } static void compile_if( - cs_gen_state &gs, cs_ident *id, bool &more, int prevargs, int rettype + codegen_state &gs, ident *id, bool &more, int prevargs, int rettype ) { if (more) { - more = compilearg(gs, CS_VAL_ANY, prevargs); + more = compilearg(gs, VAL_ANY, prevargs); } if (!more) { - gs.code.push_back(CS_CODE_NULL | cs_ret_code(rettype)); + gs.code.push_back(BC_INST_NULL | ret_code(rettype)); } else { int start1 = gs.code.size(); - more = compilearg(gs, CS_VAL_CODE, prevargs + 1); + more = compilearg(gs, VAL_CODE, prevargs + 1); if (!more) { - gs.code.push_back(CS_CODE_POP); - gs.code.push_back(CS_CODE_NULL | cs_ret_code(rettype)); + gs.code.push_back(BC_INST_POP); + gs.code.push_back(BC_INST_NULL | ret_code(rettype)); } else { int start2 = gs.code.size(); - more = compilearg(gs, CS_VAL_CODE, prevargs + 2); + more = compilearg(gs, VAL_CODE, prevargs + 2); uint32_t inst1 = gs.code[start1]; - uint32_t op1 = inst1 & ~CS_CODE_RET_MASK; + uint32_t op1 = inst1 & ~BC_INST_RET_MASK; uint32_t len1 = start2 - (start1 + 1); if (!more) { - if (op1 == (CS_CODE_BLOCK | (len1 << 8))) { - gs.code[start1] = (len1 << 8) | CS_CODE_JUMP_B | CS_CODE_FLAG_FALSE; - gs.code[start1 + 1] = CS_CODE_ENTER_RESULT; + if (op1 == (BC_INST_BLOCK | (len1 << 8))) { + gs.code[start1] = (len1 << 8) | BC_INST_JUMP_B | BC_INST_FLAG_FALSE; + gs.code[start1 + 1] = BC_INST_ENTER_RESULT; gs.code[start1 + len1] = ( - gs.code[start1 + len1] & ~CS_CODE_RET_MASK - ) | cs_ret_code(rettype); + gs.code[start1 + len1] & ~BC_INST_RET_MASK + ) | ret_code(rettype); return; } compileblock(gs); } else { uint32_t inst2 = gs.code[start2]; - uint32_t op2 = inst2 & ~CS_CODE_RET_MASK; + uint32_t op2 = inst2 & ~BC_INST_RET_MASK; uint32_t len2 = gs.code.size() - (start2 + 1); - if (op2 == (CS_CODE_BLOCK | (len2 << 8))) { - if (op1 == (CS_CODE_BLOCK | (len1 << 8))) { + if (op2 == (BC_INST_BLOCK | (len2 << 8))) { + if (op1 == (BC_INST_BLOCK | (len1 << 8))) { gs.code[start1] = ((start2 - start1) << 8) - | CS_CODE_JUMP_B | CS_CODE_FLAG_FALSE; - gs.code[start1 + 1] = CS_CODE_ENTER_RESULT; + | BC_INST_JUMP_B | BC_INST_FLAG_FALSE; + gs.code[start1 + 1] = BC_INST_ENTER_RESULT; gs.code[start1 + len1] = ( - gs.code[start1 + len1] & ~CS_CODE_RET_MASK - ) | cs_ret_code(rettype); - gs.code[start2] = (len2 << 8) | CS_CODE_JUMP; - gs.code[start2 + 1] = CS_CODE_ENTER_RESULT; + gs.code[start1 + len1] & ~BC_INST_RET_MASK + ) | ret_code(rettype); + gs.code[start2] = (len2 << 8) | BC_INST_JUMP; + gs.code[start2 + 1] = BC_INST_ENTER_RESULT; gs.code[start2 + len2] = ( - gs.code[start2 + len2] & ~CS_CODE_RET_MASK - ) | cs_ret_code(rettype); + gs.code[start2 + len2] & ~BC_INST_RET_MASK + ) | ret_code(rettype); return; - } else if (op1 == (CS_CODE_EMPTY | (len1 << 8))) { - gs.code[start1] = CS_CODE_NULL | (inst2 & CS_CODE_RET_MASK); - gs.code[start2] = (len2 << 8) | CS_CODE_JUMP_B | CS_CODE_FLAG_TRUE; - gs.code[start2 + 1] = CS_CODE_ENTER_RESULT; + } else if (op1 == (BC_INST_EMPTY | (len1 << 8))) { + gs.code[start1] = BC_INST_NULL | (inst2 & BC_INST_RET_MASK); + gs.code[start2] = (len2 << 8) | BC_INST_JUMP_B | BC_INST_FLAG_TRUE; + gs.code[start2 + 1] = BC_INST_ENTER_RESULT; gs.code[start2 + len2] = ( - gs.code[start2 + len2] & ~CS_CODE_RET_MASK - ) | cs_ret_code(rettype); + gs.code[start2 + len2] & ~BC_INST_RET_MASK + ) | ret_code(rettype); return; } } } - gs.code.push_back(CS_CODE_COM | cs_ret_code(rettype) | (id->get_index() << 8)); + gs.code.push_back(BC_INST_COM | ret_code(rettype) | (id->get_index() << 8)); } } } static void compile_and_or( - cs_gen_state &gs, cs_ident *id, bool &more, int prevargs, int rettype + codegen_state &gs, ident *id, bool &more, int prevargs, int rettype ) { int numargs = 0; if (more) { - more = compilearg(gs, CS_VAL_COND, prevargs); + more = compilearg(gs, VAL_COND, prevargs); } if (!more) { gs.code.push_back( ((id->get_raw_type() == ID_AND) - ? CS_CODE_TRUE : CS_CODE_FALSE) | cs_ret_code(rettype) + ? BC_INST_TRUE : BC_INST_FALSE) | ret_code(rettype) ); } else { numargs++; int start = gs.code.size(), end = start; while (numargs < MaxArguments) { - more = compilearg(gs, CS_VAL_COND, prevargs + numargs); + more = compilearg(gs, VAL_COND, prevargs + numargs); if (!more) { break; } numargs++; - if ((gs.code[end] & ~CS_CODE_RET_MASK) != ( - CS_CODE_BLOCK | (uint32_t(gs.code.size() - (end + 1)) << 8) + if ((gs.code[end] & ~BC_INST_RET_MASK) != ( + BC_INST_BLOCK | (uint32_t(gs.code.size() - (end + 1)) << 8) )) { break; } @@ -1184,42 +1184,42 @@ static void compile_and_or( } if (more) { while (numargs < MaxArguments) { - more = compilearg(gs, CS_VAL_COND, prevargs + numargs); + more = compilearg(gs, VAL_COND, prevargs + numargs); if (!more) { break; } numargs++; } gs.code.push_back( - CS_CODE_COM_V | cs_ret_code(rettype) | + BC_INST_COM_V | ret_code(rettype) | (numargs << 8) | (id->get_index() << 13) ); } else { uint32_t op = (id->get_raw_type() == ID_AND) - ? (CS_CODE_JUMP_RESULT | CS_CODE_FLAG_FALSE) - : (CS_CODE_JUMP_RESULT | CS_CODE_FLAG_TRUE); + ? (BC_INST_JUMP_RESULT | BC_INST_FLAG_FALSE) + : (BC_INST_JUMP_RESULT | BC_INST_FLAG_TRUE); gs.code.push_back(op); end = gs.code.size(); while ((start + 1) < end) { uint32_t len = gs.code[start] >> 8; gs.code[start] = ((end - (start + 1)) << 8) | op; - gs.code[start + 1] = CS_CODE_ENTER; + gs.code[start + 1] = BC_INST_ENTER; gs.code[start + len] = ( - gs.code[start + len] & ~CS_CODE_RET_MASK - ) | cs_ret_code(rettype); + gs.code[start + len] & ~BC_INST_RET_MASK + ) | ret_code(rettype); start += len + 1; } } } } -static void compilestatements(cs_gen_state &gs, int rettype, int brak, int prevargs) { - cs_charbuf idname{gs.cs}; +static void compilestatements(codegen_state &gs, int rettype, int brak, int prevargs) { + charbuf idname{gs.cs}; for (;;) { gs.skip_comments(); idname.clear(); size_t curline = gs.current_line; - bool more = compilearg(gs, CS_VAL_WORD, prevargs, &idname); + bool more = compilearg(gs, VAL_WORD, prevargs, &idname); if (!more) { goto endstatement; } @@ -1240,46 +1240,46 @@ static void compilestatements(cs_gen_state &gs, int rettype, int brak, int preva gs.next_char(); if (!idname.empty()) { idname.push_back('\0'); - cs_ident *id = gs.cs.new_ident(idname.str_term()); + ident *id = gs.cs.new_ident(idname.str_term()); if (id) { switch (id->get_type()) { - case cs_ident_type::ALIAS: - more = compilearg(gs, CS_VAL_ANY, prevargs); + case ident_type::ALIAS: + more = compilearg(gs, VAL_ANY, prevargs); if (!more) { gs.gen_str(); } gs.code.push_back( (id->get_index() < MaxArguments - ? CS_CODE_ALIAS_ARG - : CS_CODE_ALIAS + ? BC_INST_ALIAS_ARG + : BC_INST_ALIAS ) | (id->get_index() << 8) ); goto endstatement; - case cs_ident_type::IVAR: - more = compilearg(gs, CS_VAL_INT, prevargs); + case ident_type::IVAR: + more = compilearg(gs, VAL_INT, prevargs); if (!more) { gs.gen_int(); } gs.code.push_back( - CS_CODE_IVAR1 | (id->get_index() << 8) + BC_INST_IVAR1 | (id->get_index() << 8) ); goto endstatement; - case cs_ident_type::FVAR: - more = compilearg(gs, CS_VAL_FLOAT, prevargs); + case ident_type::FVAR: + more = compilearg(gs, VAL_FLOAT, prevargs); if (!more) { gs.gen_float(); } gs.code.push_back( - CS_CODE_FVAR1 | (id->get_index() << 8) + BC_INST_FVAR1 | (id->get_index() << 8) ); goto endstatement; - case cs_ident_type::SVAR: - more = compilearg(gs, CS_VAL_STRING, prevargs); + case ident_type::SVAR: + more = compilearg(gs, VAL_STRING, prevargs); if (!more) { gs.gen_str(); } gs.code.push_back( - CS_CODE_SVAR1 | (id->get_index() << 8) + BC_INST_SVAR1 | (id->get_index() << 8) ); goto endstatement; default: @@ -1288,11 +1288,11 @@ static void compilestatements(cs_gen_state &gs, int rettype, int brak, int preva } gs.gen_str(idname.str_term()); } - more = compilearg(gs, CS_VAL_ANY); + more = compilearg(gs, VAL_ANY); if (!more) { gs.gen_str(); } - gs.code.push_back(CS_CODE_ALIAS_U); + gs.code.push_back(BC_INST_ALIAS_U); goto endstatement; } } @@ -1300,25 +1300,25 @@ static void compilestatements(cs_gen_state &gs, int rettype, int brak, int preva noid: int numargs = 0; while (numargs < MaxArguments) { - more = compilearg(gs, CS_VAL_ANY, prevargs + numargs); + more = compilearg(gs, VAL_ANY, prevargs + numargs); if (!more) { break; } ++numargs; } - gs.code.push_back(CS_CODE_CALL_U | (numargs << 8)); + gs.code.push_back(BC_INST_CALL_U | (numargs << 8)); } else { idname.push_back('\0'); - cs_ident *id = gs.cs.get_ident(idname.str_term()); + ident *id = gs.cs.get_ident(idname.str_term()); if (!id) { if (is_valid_name(idname.str_term())) { gs.gen_str(idname.str_term()); goto noid; } switch (rettype) { - case CS_VAL_ANY: { + case VAL_ANY: { std::string_view end = idname.str_term(); - cs_int val = parse_int(end, &end); + integer_type val = parse_int(end, &end); if (!end.empty()) { gs.gen_str(idname.str_term()); } else { @@ -1330,17 +1330,17 @@ noid: gs.gen_value(rettype, idname.str_term(), curline); break; } - gs.code.push_back(CS_CODE_RESULT); + gs.code.push_back(BC_INST_RESULT); } else { switch (id->get_raw_type()) { case ID_ALIAS: compile_alias( - gs, static_cast(id), more, prevargs + gs, static_cast(id), more, prevargs ); break; case ID_COMMAND: compile_cmd( - gs, static_cast(id), more, + gs, static_cast(id), more, rettype, prevargs ); break; @@ -1348,35 +1348,35 @@ noid: compile_local(gs, more, prevargs); break; case ID_DO: - compile_do(gs, more, prevargs, rettype, CS_CODE_DO); + compile_do(gs, more, prevargs, rettype, BC_INST_DO); break; case ID_DOARGS: - compile_do(gs, more, prevargs, rettype, CS_CODE_DO_ARGS); + compile_do(gs, more, prevargs, rettype, BC_INST_DO_ARGS); break; case ID_IF: compile_if(gs, id, more, prevargs, rettype); break; case ID_BREAK: - gs.code.push_back(CS_CODE_BREAK | CS_CODE_FLAG_FALSE); + gs.code.push_back(BC_INST_BREAK | BC_INST_FLAG_FALSE); break; case ID_CONTINUE: - gs.code.push_back(CS_CODE_BREAK | CS_CODE_FLAG_TRUE); + gs.code.push_back(BC_INST_BREAK | BC_INST_FLAG_TRUE); break; case ID_RESULT: if (more) { - more = compilearg(gs, CS_VAL_ANY, prevargs); + more = compilearg(gs, VAL_ANY, prevargs); } gs.code.push_back( - (more ? CS_CODE_RESULT : CS_CODE_NULL) | - cs_ret_code(rettype) + (more ? BC_INST_RESULT : BC_INST_NULL) | + ret_code(rettype) ); break; case ID_NOT: if (more) { - more = compilearg(gs, CS_VAL_ANY, prevargs); + more = compilearg(gs, VAL_ANY, prevargs); } gs.code.push_back( - (more ? CS_CODE_NOT : CS_CODE_TRUE) | cs_ret_code(rettype) + (more ? BC_INST_NOT : BC_INST_TRUE) | ret_code(rettype) ); break; case ID_AND: @@ -1384,45 +1384,45 @@ noid: compile_and_or(gs, id, more, prevargs, rettype); break; case ID_IVAR: - if (!(more = compilearg(gs, CS_VAL_INT, prevargs))) { - gs.code.push_back(CS_CODE_PRINT | (id->get_index() << 8)); - } else if (!(id->get_flags() & CS_IDF_HEX) || !( - more = compilearg(gs, CS_VAL_INT, prevargs + 1) + if (!(more = compilearg(gs, VAL_INT, prevargs))) { + gs.code.push_back(BC_INST_PRINT | (id->get_index() << 8)); + } else if (!(id->get_flags() & IDENT_FLAG_HEX) || !( + more = compilearg(gs, VAL_INT, prevargs + 1) )) { - gs.code.push_back(CS_CODE_IVAR1 | (id->get_index() << 8)); + gs.code.push_back(BC_INST_IVAR1 | (id->get_index() << 8)); } else if (!( - more = compilearg(gs, CS_VAL_INT, prevargs + 2) + more = compilearg(gs, VAL_INT, prevargs + 2) )) { - gs.code.push_back(CS_CODE_IVAR2 | (id->get_index() << 8)); + gs.code.push_back(BC_INST_IVAR2 | (id->get_index() << 8)); } else { - gs.code.push_back(CS_CODE_IVAR3 | (id->get_index() << 8)); + gs.code.push_back(BC_INST_IVAR3 | (id->get_index() << 8)); } break; case ID_FVAR: - if (!(more = compilearg(gs, CS_VAL_FLOAT, prevargs))) { - gs.code.push_back(CS_CODE_PRINT | (id->get_index() << 8)); + if (!(more = compilearg(gs, VAL_FLOAT, prevargs))) { + gs.code.push_back(BC_INST_PRINT | (id->get_index() << 8)); } else { - gs.code.push_back(CS_CODE_FVAR1 | (id->get_index() << 8)); + gs.code.push_back(BC_INST_FVAR1 | (id->get_index() << 8)); } break; case ID_SVAR: - if (!(more = compilearg(gs, CS_VAL_STRING, prevargs))) { - gs.code.push_back(CS_CODE_PRINT | (id->get_index() << 8)); + if (!(more = compilearg(gs, VAL_STRING, prevargs))) { + gs.code.push_back(BC_INST_PRINT | (id->get_index() << 8)); } else { int numargs = 0; do { ++numargs; } while (numargs < MaxArguments && ( more = compilearg( - gs, CS_VAL_ANY, prevargs + numargs + gs, VAL_ANY, prevargs + numargs ) )); if (numargs > 1) { gs.code.push_back( - CS_CODE_CONC | CS_RET_STRING | (numargs << 8) + BC_INST_CONC | BC_RET_STRING | (numargs << 8) ); } - gs.code.push_back(CS_CODE_SVAR1 | (id->get_index() << 8)); + gs.code.push_back(BC_INST_SVAR1 | (id->get_index() << 8)); } break; } @@ -1430,12 +1430,12 @@ noid: } endstatement: if (more) { - while (compilearg(gs, CS_VAL_POP)); + while (compilearg(gs, VAL_POP)); } switch (gs.skip_until(")];/\n")) { case '\0': if (gs.current() != brak) { - throw cs_error(gs.cs, "missing \"%c\"", char(brak)); + throw error(gs.cs, "missing \"%c\"", char(brak)); return; } return; @@ -1445,7 +1445,7 @@ endstatement: gs.next_char(); return; } - throw cs_error(gs.cs, "unexpected \"%c\"", gs.current()); + throw error(gs.cs, "unexpected \"%c\"", gs.current()); return; case '/': gs.next_char(); @@ -1460,12 +1460,12 @@ endstatement: } } -void cs_gen_state::gen_main(std::string_view s, int ret_type) { +void codegen_state::gen_main(std::string_view s, int ret_type) { source = s.data(); send = s.data() + s.size(); - code.push_back(CS_CODE_START); - compilestatements(*this, CS_VAL_ANY); - code.push_back(CS_CODE_EXIT | ((ret_type < CS_VAL_ANY) ? (ret_type << CS_CODE_RET) : 0)); + code.push_back(BC_INST_START); + compilestatements(*this, VAL_ANY); + code.push_back(BC_INST_EXIT | ((ret_type < VAL_ANY) ? (ret_type << BC_INST_RET) : 0)); } } /* namespace cscript */ \ No newline at end of file diff --git a/src/cs_ident.cc b/src/cs_ident.cc index 3e24206..66eaa4e 100644 --- a/src/cs_ident.cc +++ b/src/cs_ident.cc @@ -5,27 +5,27 @@ namespace cscript { -cs_ident_impl::cs_ident_impl(cs_ident_type tp, cs_strref nm, int fl): +ident_impl::ident_impl(ident_type tp, string_ref 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 +var_impl::var_impl( + ident_type tp, string_ref name, var_cb_func f, int fl ): - cs_ident_impl{tp, name, fl}, cb_var{std::move(f)} + ident_impl{tp, name, fl}, cb_var{std::move(f)} {} -void cs_var_impl::changed(cs_state &cs) { +void var_impl::changed(state &cs) { if (cb_var) { switch (p_type) { case ID_IVAR: - cb_var(cs, *static_cast(this)); + cb_var(cs, *static_cast(this)); break; case ID_FVAR: - cb_var(cs, *static_cast(this)); + cb_var(cs, *static_cast(this)); break; case ID_SVAR: - cb_var(cs, *static_cast(this)); + cb_var(cs, *static_cast(this)); break; default: break; @@ -33,80 +33,80 @@ 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 +ivar_impl::ivar_impl( + string_ref name, integer_type m, integer_type x, integer_type v, var_cb_func f, int fl ): - cs_var_impl{ - cs_ident_type::IVAR, name, std::move(f), - fl | ((m > x) ? CS_IDF_READONLY : 0) + 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} {} -cs_fvar_impl::cs_fvar_impl( - cs_strref name, cs_float m, cs_float x, cs_float v, cs_var_cb f, int fl +fvar_impl::fvar_impl( + string_ref name, float_type m, float_type x, float_type v, var_cb_func f, int fl ): - cs_var_impl{ - cs_ident_type::FVAR, name, std::move(f), - fl | ((m > x) ? CS_IDF_READONLY : 0) + 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} {} -cs_svar_impl::cs_svar_impl( - cs_strref name, cs_strref v, cs_strref ov, cs_var_cb f, int fl +svar_impl::svar_impl( + string_ref name, string_ref v, string_ref ov, var_cb_func f, int fl ): - cs_var_impl{cs_ident_type::SVAR, name, std::move(f), fl}, + var_impl{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 +alias_impl::alias_impl( + state &cs, string_ref name, string_ref a, int fl ): - cs_ident_impl{cs_ident_type::ALIAS, name, fl}, + ident_impl{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 +alias_impl::alias_impl( + state &cs, string_ref name, std::string_view a, int fl ): - cs_ident_impl{cs_ident_type::ALIAS, name, fl}, + ident_impl{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}, +alias_impl::alias_impl(state &cs, string_ref name, integer_type a, int fl): + ident_impl{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}, +alias_impl::alias_impl(state &cs, string_ref name, float_type a, int fl): + ident_impl{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}, +alias_impl::alias_impl(state &cs, string_ref name, int fl): + ident_impl{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}, +alias_impl::alias_impl(state &cs, string_ref name, any_value v, int fl): + ident_impl{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) { +void alias_impl::push_arg(any_value &v, ident_stack &st, bool um) { if (p_astack == &st) { /* prevent cycles and unnecessary code elsewhere */ p_val = std::move(v); @@ -119,22 +119,22 @@ void cs_alias_impl::push_arg(cs_value &v, cs_ident_stack &st, bool um) { p_val = std::move(v); clean_code(); if (um) { - p_flags &= ~CS_IDF_UNKNOWN; + p_flags &= ~IDENT_FLAG_UNKNOWN; } } -void cs_alias_impl::pop_arg() { +void alias_impl::pop_arg() { if (!p_astack) { return; } - cs_ident_stack *st = p_astack; + ident_stack *st = p_astack; p_val = std::move(p_astack->val_s); clean_code(); p_astack = st->next; } -void cs_alias_impl::undo_arg(cs_ident_stack &st) { - cs_ident_stack *prev = p_astack; +void alias_impl::undo_arg(ident_stack &st) { + ident_stack *prev = p_astack; st.val_s = std::move(p_val); st.next = prev; p_astack = prev->next; @@ -142,15 +142,15 @@ void cs_alias_impl::undo_arg(cs_ident_stack &st) { clean_code(); } -void cs_alias_impl::redo_arg(cs_ident_stack &st) { - cs_ident_stack *prev = st.next; +void alias_impl::redo_arg(ident_stack &st) { + ident_stack *prev = st.next; prev->val_s = std::move(p_val); p_astack = prev; p_val = std::move(st.val_s); clean_code(); } -void cs_alias_impl::set_arg(cs_state &cs, cs_value &v) { +void alias_impl::set_arg(state &cs, any_value &v) { if (ident_is_used_arg(this, cs)) { p_val = std::move(v); clean_code(); @@ -160,41 +160,41 @@ void cs_alias_impl::set_arg(cs_state &cs, cs_value &v) { } } -void cs_alias_impl::set_alias(cs_state &cs, cs_value &v) { +void alias_impl::set_alias(state &cs, any_value &v) { p_val = std::move(v); clean_code(); p_flags = (p_flags & cs.identflags) | cs.identflags; } -void cs_alias_impl::clean_code() { +void alias_impl::clean_code() { if (p_acode) { bcode_decr(p_acode->get_raw()); p_acode = nullptr; } } -cs_bcode *cs_alias_impl::compile_code(cs_state &cs) { +bcode *alias_impl::compile_code(state &cs) { if (!p_acode) { - cs_gen_state gs(cs); + codegen_state gs(cs); gs.code.reserve(64); gs.gen_main(get_value().get_str()); /* i wish i could steal the memory somehow */ uint32_t *code = bcode_alloc(cs, gs.code.size()); memcpy(code, gs.code.data(), gs.code.size() * sizeof(uint32_t)); bcode_incr(code); - p_acode = reinterpret_cast(code); + p_acode = reinterpret_cast(code); } return p_acode; } -cs_command_impl::cs_command_impl( - cs_strref name, cs_strref args, int nargs, cs_command_cb f +command_impl::command_impl( + string_ref name, string_ref args, int nargs, command_func f ): - cs_ident_impl{cs_ident_type::COMMAND, name, 0}, + ident_impl{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) { +bool ident_is_used_arg(ident *id, state &cs) { if (!cs.p_callstack) { return true; } @@ -203,74 +203,74 @@ bool ident_is_used_arg(cs_ident *id, cs_state &cs) { /* public interface */ -LIBCUBESCRIPT_EXPORT int cs_ident::get_raw_type() const { +LIBCUBESCRIPT_EXPORT int ident::get_raw_type() const { return p_impl->p_type; } -LIBCUBESCRIPT_EXPORT cs_ident_type cs_ident::get_type() const { +LIBCUBESCRIPT_EXPORT ident_type ident::get_type() const { if (p_impl->p_type > ID_ALIAS) { - return cs_ident_type::SPECIAL; + return ident_type::SPECIAL; } - return cs_ident_type(p_impl->p_type); + return ident_type(p_impl->p_type); } -LIBCUBESCRIPT_EXPORT std::string_view cs_ident::get_name() const { +LIBCUBESCRIPT_EXPORT std::string_view ident::get_name() const { return p_impl->p_name; } -LIBCUBESCRIPT_EXPORT int cs_ident::get_flags() const { +LIBCUBESCRIPT_EXPORT int ident::get_flags() const { return p_impl->p_flags; } -LIBCUBESCRIPT_EXPORT int cs_ident::get_index() const { +LIBCUBESCRIPT_EXPORT int ident::get_index() const { return p_impl->p_index; } -LIBCUBESCRIPT_EXPORT bool cs_ident::is_alias() const { - return get_type() == cs_ident_type::ALIAS; +LIBCUBESCRIPT_EXPORT bool ident::is_alias() const { + return get_type() == ident_type::ALIAS; } -LIBCUBESCRIPT_EXPORT cs_alias *cs_ident::get_alias() { +LIBCUBESCRIPT_EXPORT alias *ident::get_alias() { if (!is_alias()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT cs_alias const *cs_ident::get_alias() const { +LIBCUBESCRIPT_EXPORT alias const *ident::get_alias() const { if (!is_alias()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT bool cs_ident::is_command() const { - return get_type() == cs_ident_type::COMMAND; +LIBCUBESCRIPT_EXPORT bool ident::is_command() const { + return get_type() == ident_type::COMMAND; } -LIBCUBESCRIPT_EXPORT cs_command *cs_ident::get_command() { +LIBCUBESCRIPT_EXPORT command *ident::get_command() { if (!is_command()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT cs_command const *cs_ident::get_command() const { +LIBCUBESCRIPT_EXPORT command const *ident::get_command() const { if (!is_command()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT bool cs_ident::is_special() const { - return get_type() == cs_ident_type::SPECIAL; +LIBCUBESCRIPT_EXPORT bool ident::is_special() const { + return get_type() == ident_type::SPECIAL; } -LIBCUBESCRIPT_EXPORT bool cs_ident::is_var() const { +LIBCUBESCRIPT_EXPORT bool ident::is_var() const { switch (get_type()) { - case cs_ident_type::IVAR: - case cs_ident_type::FVAR: - case cs_ident_type::SVAR: + case ident_type::IVAR: + case ident_type::FVAR: + case ident_type::SVAR: return true; default: break; @@ -278,128 +278,128 @@ LIBCUBESCRIPT_EXPORT bool cs_ident::is_var() const { return false; } -LIBCUBESCRIPT_EXPORT cs_var *cs_ident::get_var() { +LIBCUBESCRIPT_EXPORT global_var *ident::get_var() { if (!is_var()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT cs_var const *cs_ident::get_var() const { +LIBCUBESCRIPT_EXPORT global_var const *ident::get_var() const { if (!is_var()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT bool cs_ident::is_ivar() const { - return get_type() == cs_ident_type::IVAR; +LIBCUBESCRIPT_EXPORT bool ident::is_ivar() const { + return get_type() == ident_type::IVAR; } -LIBCUBESCRIPT_EXPORT cs_ivar *cs_ident::get_ivar() { +LIBCUBESCRIPT_EXPORT integer_var *ident::get_ivar() { if (!is_ivar()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT cs_ivar const *cs_ident::get_ivar() const { +LIBCUBESCRIPT_EXPORT integer_var const *ident::get_ivar() const { if (!is_ivar()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT bool cs_ident::is_fvar() const { - return get_type() == cs_ident_type::FVAR; +LIBCUBESCRIPT_EXPORT bool ident::is_fvar() const { + return get_type() == ident_type::FVAR; } -LIBCUBESCRIPT_EXPORT cs_fvar *cs_ident::get_fvar() { +LIBCUBESCRIPT_EXPORT float_var *ident::get_fvar() { if (!is_fvar()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT cs_fvar const *cs_ident::get_fvar() const { +LIBCUBESCRIPT_EXPORT float_var const *ident::get_fvar() const { if (!is_fvar()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT bool cs_ident::is_svar() const { - return get_type() == cs_ident_type::SVAR; +LIBCUBESCRIPT_EXPORT bool ident::is_svar() const { + return get_type() == ident_type::SVAR; } -LIBCUBESCRIPT_EXPORT cs_svar *cs_ident::get_svar() { +LIBCUBESCRIPT_EXPORT string_var *ident::get_svar() { if (!is_svar()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT cs_svar const *cs_ident::get_svar() const { +LIBCUBESCRIPT_EXPORT string_var const *ident::get_svar() const { if (!is_svar()) { return nullptr; } - return static_cast(this); + return static_cast(this); } -LIBCUBESCRIPT_EXPORT cs_int cs_ivar::get_val_min() const { - return static_cast(this)->p_minval; +LIBCUBESCRIPT_EXPORT integer_type integer_var::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 integer_type integer_var::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 integer_type integer_var::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 void integer_var::set_value(integer_type 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 float_type float_var::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 float_type float_var::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 float_type float_var::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 void float_var::set_value(float_type val) { + static_cast(this)->p_storage = val; } -LIBCUBESCRIPT_EXPORT cs_strref cs_svar::get_value() const { - return static_cast(this)->p_storage; +LIBCUBESCRIPT_EXPORT string_ref string_var::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 void string_var::set_value(string_ref val) { + static_cast(this)->p_storage = val; } -LIBCUBESCRIPT_EXPORT cs_value cs_alias::get_value() const { - return static_cast(this)->p_val; +LIBCUBESCRIPT_EXPORT any_value alias::get_value() const { + return static_cast(this)->p_val; } -void cs_alias::get_cval(cs_value &v) const { - auto *imp = static_cast(this); +void alias::get_cval(any_value &v) const { + auto *imp = static_cast(this); switch (imp->p_val.get_type()) { - case cs_value_type::STRING: + case value_type::STRING: v = imp->p_val; break; - case cs_value_type::INT: + case value_type::INT: v.set_int(imp->p_val.get_int()); break; - case cs_value_type::FLOAT: + case value_type::FLOAT: v.set_float(imp->p_val.get_float()); break; default: @@ -408,12 +408,12 @@ void cs_alias::get_cval(cs_value &v) const { } } -LIBCUBESCRIPT_EXPORT std::string_view cs_command::get_args() const { - return static_cast(this)->p_cargs; +LIBCUBESCRIPT_EXPORT std::string_view 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; +LIBCUBESCRIPT_EXPORT int command::get_num_args() const { + return static_cast(this)->p_numargs; } } /* namespace cscript */ diff --git a/src/cs_ident.hh b/src/cs_ident.hh index 5f0dc43..a99a186 100644 --- a/src/cs_ident.hh +++ b/src/cs_ident.hh @@ -11,28 +11,28 @@ enum { ID_NOT, ID_AND, ID_OR }; -struct cs_ident_link { - cs_ident *id; - cs_ident_link *next; +struct ident_link { + ident *id; + ident_link *next; int usedargs; - cs_ident_stack *argstack; + ident_stack *argstack; }; -struct cs_ident_impl { - cs_ident_impl() = delete; - cs_ident_impl(cs_ident_impl const &) = delete; - cs_ident_impl(cs_ident_impl &&) = delete; +struct ident_impl { + ident_impl() = delete; + ident_impl(ident_impl const &) = delete; + ident_impl(ident_impl &&) = delete; /* trigger destructors for all inherited members properly */ - virtual ~cs_ident_impl() {}; + virtual ~ident_impl() {}; - cs_ident_impl &operator=(cs_ident_impl const &) = delete; - cs_ident_impl &operator=(cs_ident_impl &&) = delete; + ident_impl &operator=(ident_impl const &) = delete; + ident_impl &operator=(ident_impl &&) = delete; - cs_ident_impl(cs_ident_type tp, cs_strref name, int flags = 0); + ident_impl(ident_type tp, string_ref name, int flags = 0); - cs_strref p_name; - /* represents the cs_ident_type above, but internally it has a wider + 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; @@ -40,79 +40,79 @@ struct cs_ident_impl { int p_index = -1; }; -struct cs_var_impl: cs_ident_impl { - cs_var_impl( - cs_ident_type tp, cs_strref name, cs_var_cb func, int flags = 0 +struct var_impl: ident_impl { + var_impl( + ident_type tp, string_ref name, var_cb_func func, int flags = 0 ); - cs_var_cb cb_var; + var_cb_func cb_var; - void changed(cs_state &cs); + void changed(state &cs); }; -struct cs_ivar_impl: cs_var_impl, cs_ivar { - cs_ivar_impl( - cs_strref n, cs_int m, cs_int x, cs_int v, cs_var_cb f, int flags +struct ivar_impl: var_impl, integer_var { + ivar_impl( + string_ref n, integer_type m, integer_type x, integer_type v, var_cb_func f, int flags ); - cs_int p_storage, p_minval, p_maxval, p_overrideval; + integer_type p_storage, p_minval, p_maxval, p_overrideval; }; -struct cs_fvar_impl: cs_var_impl, cs_fvar { - cs_fvar_impl( - cs_strref n, cs_float m, cs_float x, cs_float v, - cs_var_cb f, int flags +struct fvar_impl: var_impl, float_var { + fvar_impl( + string_ref n, float_type m, float_type x, float_type v, + var_cb_func f, int flags ); - cs_float p_storage, p_minval, p_maxval, p_overrideval; + float_type p_storage, p_minval, p_maxval, p_overrideval; }; -struct cs_svar_impl: cs_var_impl, cs_svar { - cs_svar_impl( - cs_strref n, cs_strref v, cs_strref ov, cs_var_cb f, int flags +struct svar_impl: var_impl, string_var { + svar_impl( + string_ref n, string_ref v, string_ref ov, var_cb_func f, int flags ); - cs_strref p_storage, p_overrideval; + string_ref p_storage, p_overrideval; }; -struct cs_alias_impl: cs_ident_impl, cs_alias { - cs_alias_impl(cs_state &cs, cs_strref n, cs_strref a, int flags); - cs_alias_impl(cs_state &cs, cs_strref n, std::string_view a, int flags); - cs_alias_impl(cs_state &cs, cs_strref n, cs_int a, int flags); - cs_alias_impl(cs_state &cs, cs_strref n, cs_float a, int flags); - cs_alias_impl(cs_state &cs, cs_strref n, int flags); - cs_alias_impl(cs_state &cs, cs_strref n, cs_value v, int flags); +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); - void push_arg(cs_value &v, cs_ident_stack &st, bool um = true); + void push_arg(any_value &v, ident_stack &st, bool um = true); void pop_arg(); - void undo_arg(cs_ident_stack &st); - void redo_arg(cs_ident_stack &st); - void set_arg(cs_state &cs, cs_value &v); - void set_alias(cs_state &cs, cs_value &v); + void undo_arg(ident_stack &st); + void redo_arg(ident_stack &st); + void set_arg(state &cs, any_value &v); + void set_alias(state &cs, any_value &v); void clean_code(); - cs_bcode *compile_code(cs_state &cs); + bcode *compile_code(state &cs); - cs_bcode *p_acode; - cs_ident_stack *p_astack; - cs_value p_val; + bcode *p_acode; + ident_stack *p_astack; + any_value p_val; }; -struct cs_command_impl: cs_ident_impl, cs_command { - cs_command_impl( - cs_strref name, cs_strref args, int numargs, cs_command_cb func +struct command_impl: ident_impl, command { + command_impl( + string_ref name, string_ref args, int numargs, command_func func ); - void call(cs_state &cs, std::span args, cs_value &ret) { + void call(state &cs, std::span args, any_value &ret) { p_cb_cftv(cs, args, ret); } - cs_strref p_cargs; - cs_command_cb p_cb_cftv; + string_ref p_cargs; + command_func p_cb_cftv; int p_numargs; }; -bool ident_is_used_arg(cs_ident *id, cs_state &cs); +bool ident_is_used_arg(ident *id, state &cs); } /* namespace cscript */ diff --git a/src/cs_parser.cc b/src/cs_parser.cc index d54532c..7854348 100644 --- a/src/cs_parser.cc +++ b/src/cs_parser.cc @@ -9,8 +9,8 @@ namespace cscript { /* string/word parsers are also useful to have public */ -LIBCUBESCRIPT_EXPORT char const *cs_parse_string( - cs_state &cs, std::string_view str, size_t &nlines +LIBCUBESCRIPT_EXPORT char const *parse_string( + state &cs, std::string_view str, size_t &nlines ) { size_t nl = 0; nlines = nl; @@ -54,7 +54,7 @@ LIBCUBESCRIPT_EXPORT char const *cs_parse_string( end: nlines = nl; if ((beg == end) || (*beg != '\"')) { - throw cs_error{ + throw error{ cs, "unfinished string '%s'", std::string_view{orig, std::size_t(beg - orig)} }; @@ -62,8 +62,8 @@ end: return ++beg; } -LIBCUBESCRIPT_EXPORT char const *cs_parse_word( - cs_state &cs, std::string_view str +LIBCUBESCRIPT_EXPORT char const *parse_word( + state &cs, std::string_view str ) { char const *it = str.begin(); char const *end = str.end(); @@ -88,20 +88,20 @@ LIBCUBESCRIPT_EXPORT char const *cs_parse_word( break; case '[': ++it; - it = cs_parse_word(cs, std::string_view{ + it = parse_word(cs, std::string_view{ it, std::size_t(end - it) }); if ((it == end) || (*it != ']')) { - throw cs_error{cs, "missing \"]\""}; + throw error{cs, "missing \"]\""}; } break; case '(': ++it; - it = cs_parse_word(cs, std::string_view{ + it = parse_word(cs, std::string_view{ it, std::size_t(end - it) }); if ((it == end) || (*it != ')')) { - throw cs_error{cs, "missing \")\""}; + throw error{cs, "missing \")\""}; } break; case ']': @@ -128,7 +128,7 @@ static inline void p_set_end( *end = std::string_view{nbeg, nend}; } /* this function assumes the input is definitely a hex digit */ -static inline cs_int p_hexd_to_int(char c) { +static inline integer_type p_hexd_to_int(char c) { if (c >= 97) { /* a-f */ return (c - 'a') + 10; } else if (c >= 65) { /* A-F */ @@ -146,17 +146,17 @@ static inline bool p_check_neg(char const *&input) { return neg; } -cs_int parse_int(std::string_view input, std::string_view *endstr) { +integer_type parse_int(std::string_view input, std::string_view *endstr) { char const *beg = input.begin(); char const *end = input.end(); char const *orig = beg; beg = p_skip_white(beg, end); if (beg == end) { p_set_end(orig, end, endstr); - return cs_int(0); + return integer_type(0); } bool neg = p_check_neg(beg); - cs_int ret = 0; + integer_type ret = 0; char const *past = beg; if ((end - beg) >= 2) { std::string_view pfx = std::string_view{beg, 2}; @@ -188,7 +188,7 @@ done: } template -static inline bool p_read_exp(char const *&beg, char const *end, cs_int &fn) { +static inline bool p_read_exp(char const *&beg, char const *end, integer_type &fn) { if (beg == end) { return true; } @@ -202,7 +202,7 @@ static inline bool p_read_exp(char const *&beg, char const *end, cs_int &fn) { if ((beg == end) || !std::isdigit(*beg)) { return false; } - cs_int exp = 0; + integer_type exp = 0; while ((beg != end) && std::isdigit(*beg)) { exp = exp * 10 + (*beg++ - '0'); } @@ -215,9 +215,9 @@ static inline bool p_read_exp(char const *&beg, char const *end, cs_int &fn) { template static inline bool parse_gen_float( - char const *&beg, char const *end, std::string_view *endstr, cs_float &ret + char const *&beg, char const *end, std::string_view *endstr, float_type &ret ) { - auto read_digits = [&beg, end](double r, cs_int &n) { + auto read_digits = [&beg, end](double r, integer_type &n) { while ( (beg != end) && (Hex ? std::isxdigit(*beg) : std::isdigit(*beg)) @@ -232,7 +232,7 @@ static inline bool parse_gen_float( } return r; }; - cs_int wn = 0, fn = 0; + integer_type wn = 0, fn = 0; double r = read_digits(0.0, wn); if ((beg != end) && (*beg == '.')) { ++beg; @@ -247,24 +247,24 @@ static inline bool parse_gen_float( p_set_end(beg, end, endstr); } if (Hex) { - ret = cs_float(ldexp(r, fn * 4)); + ret = float_type(ldexp(r, fn * 4)); } else { - ret = cs_float(r * pow(10, fn)); + ret = float_type(r * pow(10, fn)); } return true; } -cs_float parse_float(std::string_view input, std::string_view *endstr) { +float_type parse_float(std::string_view input, std::string_view *endstr) { char const *beg = input.begin(); char const *end = input.end(); char const *orig = beg; beg = p_skip_white(beg, end); if (beg == end) { p_set_end(orig, end, endstr); - return cs_float(0); + return float_type(0); } bool neg = p_check_neg(beg); - cs_float ret = cs_float(0); + float_type ret = float_type(0); if ((end - beg) >= 2) { std::string_view pfx = std::string_view{beg, 2}; if ((pfx == "0x") || (pfx == "0X")) { @@ -307,7 +307,7 @@ bool is_valid_name(std::string_view s) { /* list parser public implementation */ -LIBCUBESCRIPT_EXPORT bool cs_list_parser::parse() { +LIBCUBESCRIPT_EXPORT bool list_parser::parse() { skip_until_item(); if (p_input_beg == p_input_end) { return false; @@ -315,7 +315,7 @@ LIBCUBESCRIPT_EXPORT bool cs_list_parser::parse() { switch (*p_input_beg) { case '"': { char const *qi = p_input_beg; - p_input_beg = cs_parse_string(*p_state, get_input()); + p_input_beg = parse_string(*p_state, get_input()); p_quoted_item = std::string_view{qi, p_input_beg}; p_item = p_quoted_item.substr(1, p_quoted_item.size() - 2); break; @@ -338,7 +338,7 @@ LIBCUBESCRIPT_EXPORT bool cs_list_parser::parse() { case '"': /* the quote is needed in str parsing */ --p_input_beg; - p_input_beg = cs_parse_string(*p_state, get_input()); + p_input_beg = parse_string(*p_state, get_input()); break; case '/': if ( @@ -375,7 +375,7 @@ endblock: case ']': return false; default: { - char const *e = cs_parse_word(*p_state, get_input()); + char const *e = parse_word(*p_state, get_input()); p_quoted_item = p_item = std::string_view{p_input_beg, e}; p_input_beg = e; break; @@ -388,7 +388,7 @@ endblock: return true; } -LIBCUBESCRIPT_EXPORT std::size_t cs_list_parser::count() { +LIBCUBESCRIPT_EXPORT std::size_t list_parser::count() { size_t ret = 0; while (parse()) { ++ret; @@ -396,16 +396,16 @@ LIBCUBESCRIPT_EXPORT std::size_t cs_list_parser::count() { return ret; } -LIBCUBESCRIPT_EXPORT cs_strref cs_list_parser::get_item() const { +LIBCUBESCRIPT_EXPORT string_ref list_parser::get_item() const { if (!p_quoted_item.empty() && (p_quoted_item.front() == '"')) { - cs_charbuf buf{*p_state}; - cs_unescape_string(std::back_inserter(buf), p_item); - return cs_strref{*p_state, buf.str()}; + charbuf buf{*p_state}; + unescape_string(std::back_inserter(buf), p_item); + return string_ref{*p_state, buf.str()}; } - return cs_strref{*p_state, p_item}; + return string_ref{*p_state, p_item}; } -LIBCUBESCRIPT_EXPORT void cs_list_parser::skip_until_item() { +LIBCUBESCRIPT_EXPORT void list_parser::skip_until_item() { for (;;) { while (p_input_beg != p_input_end) { char c = *p_input_beg; diff --git a/src/cs_parser.hh b/src/cs_parser.hh index e73ebc8..601efc3 100644 --- a/src/cs_parser.hh +++ b/src/cs_parser.hh @@ -7,8 +7,8 @@ namespace cscript { -cs_int parse_int(std::string_view input, std::string_view *end = nullptr); -cs_float parse_float(std::string_view input, std::string_view *end = nullptr); +integer_type parse_int(std::string_view input, std::string_view *end = nullptr); +float_type parse_float(std::string_view input, std::string_view *end = nullptr); bool is_valid_name(std::string_view input); diff --git a/src/cs_state.cc b/src/cs_state.cc index 0724bfc..29c1d0e 100644 --- a/src/cs_state.cc +++ b/src/cs_state.cc @@ -8,21 +8,21 @@ namespace cscript { -cs_shared_state::cs_shared_state(cs_alloc_cb af, void *data): +internal_state::internal_state(alloc_func af, void *data): allocf{af}, aptr{data}, idents{allocator_type{this}}, identmap{allocator_type{this}}, varprintf{}, - strman{create(this)}, + strman{create(this)}, empty{bcode_init_empty(this)} {} -cs_shared_state::~cs_shared_state() { +internal_state::~internal_state() { bcode_free_empty(this, empty); destroy(strman); } -void *cs_shared_state::alloc(void *ptr, size_t os, size_t ns) { +void *internal_state::alloc(void *ptr, size_t os, size_t ns) { void *p = allocf(aptr, ptr, os, ns); if (!p && ns) { throw std::bad_alloc{}; @@ -30,7 +30,7 @@ void *cs_shared_state::alloc(void *ptr, size_t os, size_t ns) { return p; } -static void *cs_default_alloc(void *, void *p, size_t, size_t ns) { +static void *default_alloc(void *, void *p, size_t, size_t ns) { if (!ns) { std::free(p); return nullptr; @@ -38,88 +38,88 @@ static void *cs_default_alloc(void *, void *p, size_t, size_t ns) { return std::realloc(p, ns); } -void cs_init_lib_base(cs_state &cs); -void cs_init_lib_math(cs_state &cs); -void cs_init_lib_string(cs_state &cs); -void cs_init_lib_list(cs_state &cs); +void init_lib_base(state &cs); +void init_lib_math(state &cs); +void init_lib_string(state &cs); +void init_lib_list(state &cs); /* public interfaces */ -cs_state::cs_state(): cs_state{cs_default_alloc, nullptr} {} +state::state(): state{default_alloc, nullptr} {} -cs_state::cs_state(cs_alloc_cb func, void *data): +state::state(alloc_func func, void *data): p_state{nullptr}, p_callhook{} { - cs_command *p; + command *p; if (!func) { - func = cs_default_alloc; + func = default_alloc; } /* allocator is not set up yet, use func directly */ - p_state = static_cast( - func(data, nullptr, 0, sizeof(cs_shared_state)) + p_state = static_cast( + func(data, nullptr, 0, sizeof(internal_state)) ); /* allocator will be set up in the constructor */ - new (p_state) cs_shared_state{func, data}; + new (p_state) internal_state{func, data}; p_owner = true; /* will be used as message storage for errors */ - p_errbuf = p_state->create(*this); + p_errbuf = p_state->create(*this); for (int i = 0; i < MaxArguments; ++i) { char buf[32]; snprintf(buf, sizeof(buf), "arg%d", i + 1); - new_ident(static_cast(buf), CS_IDF_ARG); + new_ident(static_cast(buf), IDENT_FLAG_ARG); } - cs_ident *id = new_ident("//dummy"); + ident *id = new_ident("//dummy"); if (id->get_index() != DummyIdx) { - throw cs_internal_error{"invalid dummy index"}; + throw internal_error{"invalid dummy index"}; } id = new_ivar("numargs", MaxArguments, 0, 0); if (id->get_index() != NumargsIdx) { - throw cs_internal_error{"invalid numargs index"}; + throw internal_error{"invalid numargs index"}; } id = new_ivar("dbgalias", 0, 1000, 4); if (id->get_index() != DbgaliasIdx) { - throw cs_internal_error{"invalid dbgalias index"}; + throw internal_error{"invalid dbgalias index"}; } p = new_command("do", "e", [](auto &cs, auto args, auto &res) { cs.run(args[0].get_code(), res); }); - static_cast(p)->p_type = ID_DO; + static_cast(p)->p_type = ID_DO; p = new_command("doargs", "e", [](auto &cs, auto args, auto &res) { - cs_do_args(cs, [&cs, &res, &args]() { + call_with_args(cs, [&cs, &res, &args]() { cs.run(args[0].get_code(), res); }); }); - static_cast(p)->p_type = ID_DOARGS; + static_cast(p)->p_type = ID_DOARGS; p = new_command("if", "tee", [](auto &cs, auto args, auto &res) { cs.run((args[0].get_bool() ? args[1] : args[2]).get_code(), res); }); - static_cast(p)->p_type = ID_IF; + static_cast(p)->p_type = ID_IF; p = new_command("result", "t", [](auto &, auto args, auto &res) { res = std::move(args[0]); }); - static_cast(p)->p_type = ID_RESULT; + static_cast(p)->p_type = ID_RESULT; p = new_command("!", "t", [](auto &, auto args, auto &res) { res.set_int(!args[0].get_bool()); }); - static_cast(p)->p_type = ID_NOT; + static_cast(p)->p_type = ID_NOT; p = new_command("&&", "E1V", [](auto &cs, auto args, auto &res) { if (args.empty()) { res.set_int(1); } else { for (size_t i = 0; i < args.size(); ++i) { - cs_bcode *code = args[i].get_code(); + bcode *code = args[i].get_code(); if (code) { cs.run(code, res); } else { @@ -131,14 +131,14 @@ cs_state::cs_state(cs_alloc_cb func, void *data): } } }); - static_cast(p)->p_type = ID_AND; + static_cast(p)->p_type = ID_AND; p = new_command("||", "E1V", [](auto &cs, auto args, auto &res) { if (args.empty()) { res.set_int(0); } else { for (size_t i = 0; i < args.size(); ++i) { - cs_bcode *code = args[i].get_code(); + bcode *code = args[i].get_code(); if (code) { cs.run(code, res); } else { @@ -150,134 +150,134 @@ cs_state::cs_state(cs_alloc_cb func, void *data): } } }); - static_cast(p)->p_type = ID_OR; + static_cast(p)->p_type = ID_OR; p = new_command("local", "", nullptr); - static_cast(p)->p_type = ID_LOCAL; + static_cast(p)->p_type = ID_LOCAL; p = new_command("break", "", [](auto &cs, auto, auto &) { if (cs.is_in_loop()) { throw CsBreakException{}; } else { - throw cs_error{cs, "no loop to break"}; + throw error{cs, "no loop to break"}; } }); - static_cast(p)->p_type = ID_BREAK; + static_cast(p)->p_type = ID_BREAK; p = new_command("continue", "", [](auto &cs, auto, auto &) { if (cs.is_in_loop()) { throw CsContinueException{}; } else { - throw cs_error{cs, "no loop to continue"}; + throw error{cs, "no loop to continue"}; } }); - static_cast(p)->p_type = ID_CONTINUE; + static_cast(p)->p_type = ID_CONTINUE; - cs_init_lib_base(*this); + init_lib_base(*this); } -LIBCUBESCRIPT_EXPORT cs_state::~cs_state() { +LIBCUBESCRIPT_EXPORT state::~state() { destroy(); } -LIBCUBESCRIPT_EXPORT void cs_state::destroy() { +LIBCUBESCRIPT_EXPORT void state::destroy() { if (!p_state || !p_owner) { return; } for (auto &p: p_state->idents) { - cs_ident *i = p.second; - cs_alias *a = i->get_alias(); + ident *i = p.second; + alias *a = i->get_alias(); if (a) { a->get_value().force_none(); - static_cast(a)->clean_code(); + static_cast(a)->clean_code(); } p_state->destroy(i->p_impl); } - p_state->destroy(static_cast(p_errbuf)); + p_state->destroy(static_cast(p_errbuf)); p_state->destroy(p_state); } -cs_state::cs_state(cs_shared_state *s): +state::state(internal_state *s): p_state(s), p_owner(false) {} -LIBCUBESCRIPT_EXPORT cs_state cs_state::new_thread() { - return cs_state{p_state}; +LIBCUBESCRIPT_EXPORT state state::new_thread() { + return state{p_state}; } -LIBCUBESCRIPT_EXPORT cs_hook_cb cs_state::set_call_hook(cs_hook_cb func) { +LIBCUBESCRIPT_EXPORT hook_func state::set_call_hook(hook_func func) { auto hk = std::move(p_callhook); p_callhook = std::move(func); return hk; } -LIBCUBESCRIPT_EXPORT cs_hook_cb const &cs_state::get_call_hook() const { +LIBCUBESCRIPT_EXPORT hook_func const &state::get_call_hook() const { return p_callhook; } -LIBCUBESCRIPT_EXPORT cs_hook_cb &cs_state::get_call_hook() { +LIBCUBESCRIPT_EXPORT hook_func &state::get_call_hook() { return p_callhook; } -LIBCUBESCRIPT_EXPORT cs_vprint_cb cs_state::set_var_printer( - cs_vprint_cb func +LIBCUBESCRIPT_EXPORT var_print_func state::set_var_printer( + var_print_func func ) { auto fn = std::move(p_state->varprintf); p_state->varprintf = std::move(func); return fn; } -LIBCUBESCRIPT_EXPORT cs_vprint_cb const &cs_state::get_var_printer() const { +LIBCUBESCRIPT_EXPORT var_print_func const &state::get_var_printer() const { return p_state->varprintf; } -LIBCUBESCRIPT_EXPORT void cs_state::print_var(cs_var const &v) const { +LIBCUBESCRIPT_EXPORT void state::print_var(global_var const &v) const { if (p_state->varprintf) { p_state->varprintf(*this, v); } } -LIBCUBESCRIPT_EXPORT void *cs_state::alloc(void *ptr, size_t os, size_t ns) { +LIBCUBESCRIPT_EXPORT void *state::alloc(void *ptr, size_t os, size_t ns) { return p_state->alloc(ptr, os, ns); } -LIBCUBESCRIPT_EXPORT cs_ident *cs_state::add_ident( - cs_ident *id, cs_ident_impl *impl +LIBCUBESCRIPT_EXPORT ident *state::add_ident( + ident *id, ident_impl *impl ) { if (!id) { return nullptr; } id->p_impl = impl; p_state->idents[id->get_name()] = id; - static_cast(impl)->p_index = p_state->identmap.size(); + static_cast(impl)->p_index = p_state->identmap.size(); p_state->identmap.push_back(id); return p_state->identmap.back(); } -LIBCUBESCRIPT_EXPORT cs_ident *cs_state::new_ident( +LIBCUBESCRIPT_EXPORT ident *state::new_ident( std::string_view name, int flags ) { - cs_ident *id = get_ident(name); + ident *id = get_ident(name); if (!id) { if (!is_valid_name(name)) { - throw cs_error{ + throw error{ *this, "number %s is not a valid identifier name", name.data() }; } - auto *inst = p_state->create( - *this, cs_strref{p_state, name}, flags + auto *inst = p_state->create( + *this, string_ref{p_state, name}, flags ); id = add_ident(inst, inst); } return id; } -LIBCUBESCRIPT_EXPORT cs_ident *cs_state::force_ident(cs_value &v) { +LIBCUBESCRIPT_EXPORT ident *state::force_ident(any_value &v) { switch (v.get_type()) { - case cs_value_type::IDENT: + case value_type::IDENT: return v.get_ident(); - case cs_value_type::STRING: { - cs_ident *id = new_ident(v.get_str()); + case value_type::STRING: { + ident *id = new_ident(v.get_str()); v.set_ident(id); return id; } @@ -288,7 +288,7 @@ LIBCUBESCRIPT_EXPORT cs_ident *cs_state::force_ident(cs_value &v) { return p_state->identmap[DummyIdx]; } -LIBCUBESCRIPT_EXPORT cs_ident *cs_state::get_ident(std::string_view name) { +LIBCUBESCRIPT_EXPORT ident *state::get_ident(std::string_view name) { auto id = p_state->idents.find(name); if (id != p_state->idents.end()) { return id->second; @@ -296,88 +296,89 @@ LIBCUBESCRIPT_EXPORT cs_ident *cs_state::get_ident(std::string_view name) { return nullptr; } -LIBCUBESCRIPT_EXPORT cs_alias *cs_state::get_alias(std::string_view name) { +LIBCUBESCRIPT_EXPORT alias *state::get_alias(std::string_view name) { auto id = get_ident(name); if (!id || !id->is_alias()) { return nullptr; } - return static_cast(id); + return static_cast(id); } -LIBCUBESCRIPT_EXPORT bool cs_state::have_ident(std::string_view name) { +LIBCUBESCRIPT_EXPORT bool state::have_ident(std::string_view name) { return p_state->idents.find(name) != p_state->idents.end(); } -LIBCUBESCRIPT_EXPORT std::span cs_state::get_idents() { - return std::span{ +LIBCUBESCRIPT_EXPORT std::span state::get_idents() { + return std::span{ p_state->identmap.data(), p_state->identmap.size() }; } -LIBCUBESCRIPT_EXPORT std::span cs_state::get_idents() const { - auto ptr = const_cast(p_state->identmap.data()); - return std::span{ptr, p_state->identmap.size()}; +LIBCUBESCRIPT_EXPORT std::span state::get_idents() const { + auto ptr = const_cast(p_state->identmap.data()); + return std::span{ptr, p_state->identmap.size()}; } -LIBCUBESCRIPT_EXPORT cs_ivar *cs_state::new_ivar( - std::string_view n, cs_int m, cs_int x, cs_int v, cs_var_cb f, int flags +LIBCUBESCRIPT_EXPORT integer_var *state::new_ivar( + std::string_view n, integer_type m, integer_type x, integer_type v, + var_cb_func f, int flags ) { - auto *iv = p_state->create( - cs_strref{p_state, n}, m, x, v, std::move(f), flags + auto *iv = p_state->create( + string_ref{p_state, n}, m, x, v, std::move(f), flags ); add_ident(iv, iv); return iv; } -LIBCUBESCRIPT_EXPORT cs_fvar *cs_state::new_fvar( - std::string_view n, cs_float m, cs_float x, cs_float v, - cs_var_cb f, int flags +LIBCUBESCRIPT_EXPORT float_var *state::new_fvar( + std::string_view n, float_type m, float_type x, float_type v, + var_cb_func f, int flags ) { - auto *fv = p_state->create( - cs_strref{p_state, n}, m, x, v, std::move(f), flags + auto *fv = p_state->create( + string_ref{p_state, n}, m, x, v, std::move(f), flags ); add_ident(fv, fv); return fv; } -LIBCUBESCRIPT_EXPORT cs_svar *cs_state::new_svar( - std::string_view n, std::string_view v, cs_var_cb f, int flags +LIBCUBESCRIPT_EXPORT string_var *state::new_svar( + std::string_view n, std::string_view v, var_cb_func f, int flags ) { - auto *sv = p_state->create( - cs_strref{p_state, n}, cs_strref{p_state, v}, - cs_strref{p_state, ""}, std::move(f), flags + auto *sv = p_state->create( + string_ref{p_state, n}, string_ref{p_state, v}, + string_ref{p_state, ""}, std::move(f), flags ); add_ident(sv, sv); return sv; } -LIBCUBESCRIPT_EXPORT void cs_state::reset_var(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT void state::reset_var(std::string_view name) { + ident *id = get_ident(name); if (!id) { - throw cs_error{*this, "variable %s does not exist", name.data()}; + throw error{*this, "variable %s does not exist", name.data()}; } - if (id->get_flags() & CS_IDF_READONLY) { - throw cs_error{*this, "variable %s is read only", name.data()}; + if (id->get_flags() & IDENT_FLAG_READONLY) { + throw error{*this, "variable %s is read only", name.data()}; } clear_override(*id); } -LIBCUBESCRIPT_EXPORT void cs_state::touch_var(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT void state::touch_var(std::string_view name) { + ident *id = get_ident(name); if (id && id->is_var()) { - static_cast(id->p_impl)->changed(*this); + static_cast(id->p_impl)->changed(*this); } } -LIBCUBESCRIPT_EXPORT void cs_state::set_alias( - std::string_view name, cs_value v +LIBCUBESCRIPT_EXPORT void state::set_alias( + std::string_view name, any_value v ) { - cs_ident *id = get_ident(name); + ident *id = get_ident(name); if (id) { switch (id->get_type()) { - case cs_ident_type::ALIAS: { - cs_alias_impl *a = static_cast(id); + case ident_type::ALIAS: { + alias_impl *a = static_cast(id); if (a->get_index() < MaxArguments) { a->set_arg(*this, v); } else { @@ -385,33 +386,33 @@ LIBCUBESCRIPT_EXPORT void cs_state::set_alias( } return; } - case cs_ident_type::IVAR: - set_var_int_checked(static_cast(id), v.get_int()); + case ident_type::IVAR: + set_var_int_checked(static_cast(id), v.get_int()); break; - case cs_ident_type::FVAR: - set_var_float_checked(static_cast(id), v.get_float()); + case ident_type::FVAR: + set_var_float_checked(static_cast(id), v.get_float()); break; - case cs_ident_type::SVAR: - set_var_str_checked(static_cast(id), v.get_str()); + case ident_type::SVAR: + set_var_str_checked(static_cast(id), v.get_str()); break; default: - throw cs_error{ + throw error{ *this, "cannot redefine builtin %s with an alias", id->get_name().data() }; } } else if (!is_valid_name(name)) { - throw cs_error{*this, "cannot alias invalid name '%s'", name.data()}; + throw error{*this, "cannot alias invalid name '%s'", name.data()}; } else { - auto *a = p_state->create( - *this, cs_strref{p_state, name}, std::move(v), identflags + auto *a = p_state->create( + *this, string_ref{p_state, name}, std::move(v), identflags ); add_ident(a, a); } } -LIBCUBESCRIPT_EXPORT cs_command *cs_state::new_command( - std::string_view name, std::string_view args, cs_command_cb func +LIBCUBESCRIPT_EXPORT command *state::new_command( + std::string_view name, std::string_view args, command_func func ) { int nargs = 0; for (auto fmt = args.begin(); fmt != args.end(); ++fmt) { @@ -459,39 +460,39 @@ LIBCUBESCRIPT_EXPORT cs_command *cs_state::new_command( return nullptr; } } - auto *cmd = p_state->create( - cs_strref{p_state, name}, cs_strref{p_state, args}, nargs, + auto *cmd = p_state->create( + string_ref{p_state, name}, string_ref{p_state, args}, nargs, std::move(func) ); add_ident(cmd, cmd); return cmd; } -LIBCUBESCRIPT_EXPORT void cs_state::clear_override(cs_ident &id) { - if (!(id.get_flags() & CS_IDF_OVERRIDDEN)) { +LIBCUBESCRIPT_EXPORT void state::clear_override(ident &id) { + if (!(id.get_flags() & IDENT_FLAG_OVERRIDDEN)) { return; } switch (id.get_type()) { - case cs_ident_type::ALIAS: { - cs_alias_impl &a = static_cast(id); + case ident_type::ALIAS: { + alias_impl &a = static_cast(id); a.clean_code(); a.get_value().set_str(""); break; } - case cs_ident_type::IVAR: { - cs_ivar_impl &iv = static_cast(id); + case ident_type::IVAR: { + ivar_impl &iv = static_cast(id); iv.set_value(iv.p_overrideval); iv.changed(*this); break; } - case cs_ident_type::FVAR: { - cs_fvar_impl &fv = static_cast(id); + case ident_type::FVAR: { + fvar_impl &fv = static_cast(id); fv.set_value(fv.p_overrideval); fv.changed(*this); break; } - case cs_ident_type::SVAR: { - cs_svar_impl &sv = static_cast(id); + case ident_type::SVAR: { + svar_impl &sv = static_cast(id); sv.set_value(sv.p_overrideval); sv.changed(*this); break; @@ -499,44 +500,44 @@ LIBCUBESCRIPT_EXPORT void cs_state::clear_override(cs_ident &id) { default: break; } - id.p_impl->p_flags &= ~CS_IDF_OVERRIDDEN; + id.p_impl->p_flags &= ~IDENT_FLAG_OVERRIDDEN; } -LIBCUBESCRIPT_EXPORT void cs_state::clear_overrides() { +LIBCUBESCRIPT_EXPORT void state::clear_overrides() { for (auto &p: p_state->idents) { clear_override(*(p.second)); } } template -inline void cs_override_var(cs_state &cs, cs_var *v, int &vflags, SF sf) { - if ((cs.identflags & CS_IDF_OVERRIDDEN) || (vflags & CS_IDF_OVERRIDE)) { - if (vflags & CS_IDF_PERSIST) { - throw cs_error{ +inline void override_var(state &cs, global_var *v, int &vflags, SF sf) { + if ((cs.identflags & IDENT_FLAG_OVERRIDDEN) || (vflags & IDENT_FLAG_OVERRIDE)) { + if (vflags & IDENT_FLAG_PERSIST) { + throw error{ cs, "cannot override persistent variable '%s'", v->get_name().data() }; } - if (!(vflags & CS_IDF_OVERRIDDEN)) { + if (!(vflags & IDENT_FLAG_OVERRIDDEN)) { sf(); - vflags |= CS_IDF_OVERRIDDEN; + vflags |= IDENT_FLAG_OVERRIDDEN; } } else { - if (vflags & CS_IDF_OVERRIDDEN) { - vflags &= ~CS_IDF_OVERRIDDEN; + if (vflags & IDENT_FLAG_OVERRIDDEN) { + vflags &= ~IDENT_FLAG_OVERRIDDEN; } } } -LIBCUBESCRIPT_EXPORT void cs_state::set_var_int( - std::string_view name, cs_int v, bool dofunc, bool doclamp +LIBCUBESCRIPT_EXPORT void state::set_var_int( + std::string_view name, integer_type v, bool dofunc, bool doclamp ) { - cs_ident *id = get_ident(name); + ident *id = get_ident(name); if (!id || id->is_ivar()) { return; } - cs_ivar_impl *iv = static_cast(id); - cs_override_var( + ivar_impl *iv = static_cast(id); + override_var( *this, iv, iv->p_flags, [&iv]() { iv->p_overrideval = iv->get_value(); } ); @@ -550,15 +551,15 @@ LIBCUBESCRIPT_EXPORT void cs_state::set_var_int( } } -LIBCUBESCRIPT_EXPORT void cs_state::set_var_float( - std::string_view name, cs_float v, bool dofunc, bool doclamp +LIBCUBESCRIPT_EXPORT void state::set_var_float( + std::string_view name, float_type v, bool dofunc, bool doclamp ) { - cs_ident *id = get_ident(name); + ident *id = get_ident(name); if (!id || id->is_fvar()) { return; } - cs_fvar_impl *fv = static_cast(id); - cs_override_var( + fvar_impl *fv = static_cast(id); + override_var( *this, fv, fv->p_flags, [&fv]() { fv->p_overrideval = fv->get_value(); } ); @@ -572,90 +573,90 @@ LIBCUBESCRIPT_EXPORT void cs_state::set_var_float( } } -LIBCUBESCRIPT_EXPORT void cs_state::set_var_str( +LIBCUBESCRIPT_EXPORT void state::set_var_str( std::string_view name, std::string_view v, bool dofunc ) { - cs_ident *id = get_ident(name); + ident *id = get_ident(name); if (!id || id->is_svar()) { return; } - cs_svar_impl *sv = static_cast(id); - cs_override_var( + svar_impl *sv = static_cast(id); + override_var( *this, sv, sv->p_flags, [&sv]() { sv->p_overrideval = sv->get_value(); } ); - sv->set_value(cs_strref{p_state, v}); + sv->set_value(string_ref{p_state, v}); if (dofunc) { sv->changed(*this); } } -LIBCUBESCRIPT_EXPORT std::optional -cs_state::get_var_int(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT std::optional +state::get_var_int(std::string_view name) { + ident *id = get_ident(name); if (!id || id->is_ivar()) { return std::nullopt; } - return static_cast(id)->get_value(); + return static_cast(id)->get_value(); } -LIBCUBESCRIPT_EXPORT std::optional -cs_state::get_var_float(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT std::optional +state::get_var_float(std::string_view name) { + ident *id = get_ident(name); if (!id || id->is_fvar()) { return std::nullopt; } - return static_cast(id)->get_value(); + return static_cast(id)->get_value(); } -LIBCUBESCRIPT_EXPORT std::optional -cs_state::get_var_str(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT std::optional +state::get_var_str(std::string_view name) { + ident *id = get_ident(name); if (!id || id->is_svar()) { return std::nullopt; } - return cs_strref{p_state, static_cast(id)->get_value()}; + return string_ref{p_state, static_cast(id)->get_value()}; } -LIBCUBESCRIPT_EXPORT std::optional -cs_state::get_var_min_int(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT std::optional +state::get_var_min_int(std::string_view name) { + ident *id = get_ident(name); if (!id || id->is_ivar()) { return std::nullopt; } - return static_cast(id)->get_val_min(); + return static_cast(id)->get_val_min(); } -LIBCUBESCRIPT_EXPORT std::optional -cs_state::get_var_max_int(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT std::optional +state::get_var_max_int(std::string_view name) { + ident *id = get_ident(name); if (!id || id->is_ivar()) { return std::nullopt; } - return static_cast(id)->get_val_max(); + return static_cast(id)->get_val_max(); } -LIBCUBESCRIPT_EXPORT std::optional -cs_state::get_var_min_float(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT std::optional +state::get_var_min_float(std::string_view name) { + ident *id = get_ident(name); if (!id || id->is_fvar()) { return std::nullopt; } - return static_cast(id)->get_val_min(); + return static_cast(id)->get_val_min(); } -LIBCUBESCRIPT_EXPORT std::optional -cs_state::get_var_max_float(std::string_view name) { - cs_ident *id = get_ident(name); +LIBCUBESCRIPT_EXPORT std::optional +state::get_var_max_float(std::string_view name) { + ident *id = get_ident(name); if (!id || id->is_fvar()) { return std::nullopt; } - return static_cast(id)->get_val_max(); + return static_cast(id)->get_val_max(); } -LIBCUBESCRIPT_EXPORT std::optional -cs_state::get_alias_val(std::string_view name) { - cs_alias *a = get_alias(name); +LIBCUBESCRIPT_EXPORT std::optional +state::get_alias_val(std::string_view name) { + alias *a = get_alias(name); if (!a) { return std::nullopt; } @@ -665,7 +666,7 @@ cs_state::get_alias_val(std::string_view name) { return a->get_value().get_str(); } -cs_int cs_clamp_var(cs_state &cs, cs_ivar *iv, cs_int v) { +integer_type clamp_var(state &cs, integer_var *iv, integer_type v) { if (v < iv->get_val_min()) { v = iv->get_val_min(); } else if (v > iv->get_val_max()) { @@ -673,9 +674,9 @@ cs_int cs_clamp_var(cs_state &cs, cs_ivar *iv, cs_int v) { } else { return v; } - throw cs_error{ + throw error{ cs, - (iv->get_flags() & CS_IDF_HEX) + (iv->get_flags() & IDENT_FLAG_HEX) ? ( (iv->get_val_min() <= 255) ? "valid range for '%s' is %d..0x%X" @@ -686,29 +687,31 @@ cs_int cs_clamp_var(cs_state &cs, cs_ivar *iv, cs_int v) { }; } -LIBCUBESCRIPT_EXPORT void cs_state::set_var_int_checked(cs_ivar *iv, cs_int v) { - if (iv->get_flags() & CS_IDF_READONLY) { - throw cs_error{ +LIBCUBESCRIPT_EXPORT void state::set_var_int_checked( + integer_var *iv, integer_type v +) { + if (iv->get_flags() & IDENT_FLAG_READONLY) { + throw error{ *this, "variable '%s' is read only", iv->get_name().data() }; } - cs_ivar_impl *ivp = static_cast(iv); - cs_override_var( + ivar_impl *ivp = static_cast(iv); + override_var( *this, iv, ivp->p_flags, [&ivp]() { ivp->p_overrideval = ivp->p_storage; } ); if ((v < iv->get_val_min()) || (v > iv->get_val_max())) { - v = cs_clamp_var(*this, iv, v); + v = clamp_var(*this, iv, v); } iv->set_value(v); ivp->changed(*this); } -LIBCUBESCRIPT_EXPORT void cs_state::set_var_int_checked( - cs_ivar *iv, std::span args +LIBCUBESCRIPT_EXPORT void state::set_var_int_checked( + integer_var *iv, std::span args ) { - cs_int v = args[0].force_int(); - if ((iv->get_flags() & CS_IDF_HEX) && (args.size() > 1)) { + integer_type v = args[0].force_int(); + if ((iv->get_flags() & IDENT_FLAG_HEX) && (args.size() > 1)) { v = (v << 16) | (args[1].force_int() << 8); if (args.size() > 2) { v |= args[2].force_int(); @@ -717,7 +720,7 @@ LIBCUBESCRIPT_EXPORT void cs_state::set_var_int_checked( set_var_int_checked(iv, v); } -cs_float cs_clamp_fvar(cs_state &cs, cs_fvar *fv, cs_float v) { +float_type clamp_fvar(state &cs, float_var *fv, float_type v) { if (v < fv->get_val_min()) { v = fv->get_val_min(); } else if (v > fv->get_val_max()) { @@ -725,62 +728,62 @@ cs_float cs_clamp_fvar(cs_state &cs, cs_fvar *fv, cs_float v) { } else { return v; } - cs_value vmin{cs}, vmax{cs}; + any_value vmin{cs}, vmax{cs}; vmin.set_float(fv->get_val_min()); vmax.set_float(fv->get_val_max()); - throw cs_error{ + throw error{ cs, "valid range for '%s' is %s..%s", fv->get_name().data(), vmin.force_str(), vmax.force_str() }; return v; } -LIBCUBESCRIPT_EXPORT void cs_state::set_var_float_checked( - cs_fvar *fv, cs_float v +LIBCUBESCRIPT_EXPORT void state::set_var_float_checked( + float_var *fv, float_type v ) { - if (fv->get_flags() & CS_IDF_READONLY) { - throw cs_error{ + if (fv->get_flags() & IDENT_FLAG_READONLY) { + throw error{ *this, "variable '%s' is read only", fv->get_name().data() }; } - cs_fvar_impl *fvp = static_cast(fv); - cs_override_var( + fvar_impl *fvp = static_cast(fv); + override_var( *this, fv, fvp->p_flags, [&fvp]() { fvp->p_overrideval = fvp->p_storage; } ); if ((v < fv->get_val_min()) || (v > fv->get_val_max())) { - v = cs_clamp_fvar(*this, fv, v); + v = clamp_fvar(*this, fv, v); } fv->set_value(v); fvp->changed(*this); } -LIBCUBESCRIPT_EXPORT void cs_state::set_var_str_checked( - cs_svar *sv, std::string_view v +LIBCUBESCRIPT_EXPORT void state::set_var_str_checked( + string_var *sv, std::string_view v ) { - if (sv->get_flags() & CS_IDF_READONLY) { - throw cs_error{ + if (sv->get_flags() & IDENT_FLAG_READONLY) { + throw error{ *this, "variable '%s' is read only", sv->get_name().data() }; } - cs_svar_impl *svp = static_cast(sv); - cs_override_var( + svar_impl *svp = static_cast(sv); + override_var( *this, sv, svp->p_flags, [&svp]() { svp->p_overrideval = svp->p_storage; } ); - sv->set_value(cs_strref{p_state, v}); + sv->set_value(string_ref{p_state, v}); svp->changed(*this); } -LIBCUBESCRIPT_EXPORT void cs_state::init_libs(int libs) { - if (libs & CS_LIB_MATH) { - cs_init_lib_math(*this); +LIBCUBESCRIPT_EXPORT void state::init_libs(int libs) { + if (libs & LIB_MATH) { + init_lib_math(*this); } - if (libs & CS_LIB_STRING) { - cs_init_lib_string(*this); + if (libs & LIB_STRING) { + init_lib_string(*this); } - if (libs & CS_LIB_LIST) { - cs_init_lib_list(*this); + if (libs & LIB_LIST) { + init_lib_list(*this); } } diff --git a/src/cs_state.hh b/src/cs_state.hh index 0b168a3..ecb85bb 100644 --- a/src/cs_state.hh +++ b/src/cs_state.hh @@ -10,55 +10,55 @@ namespace cscript { -struct cs_state; -struct cs_shared_state; -struct cs_strman; +struct state; +struct internal_state; +struct string_pool; template -struct cs_allocator { +struct std_allocator { using value_type = T; - inline cs_allocator(cs_shared_state *s); - inline cs_allocator(cs_state &cs); + inline std_allocator(internal_state *s); + inline std_allocator(state &cs); template - cs_allocator(cs_allocator const &a): state{a.state} {}; + std_allocator(std_allocator const &a): istate{a.istate} {}; inline T *allocate(std::size_t n); inline void deallocate(T *p, std::size_t n); template - bool operator==(cs_allocator const &a) { - return state == a.state; + bool operator==(std_allocator const &a) { + return istate == a.istate; } - cs_shared_state *state; + internal_state *istate; }; -struct cs_shared_state { - using allocator_type = cs_allocator< - std::pair +struct internal_state { + using allocator_type = std_allocator< + std::pair >; - cs_alloc_cb allocf; + alloc_func allocf; void *aptr; std::unordered_map< - std::string_view, cs_ident *, + std::string_view, ident *, std::hash, std::equal_to, allocator_type > idents; - std::vector> identmap; + std::vector> identmap; - cs_vprint_cb varprintf; - cs_strman *strman; + var_print_func varprintf; + string_pool *strman; empty_block *empty; - cs_shared_state() = delete; + internal_state() = delete; - cs_shared_state(cs_alloc_cb af, void *data); + internal_state(alloc_func af, void *data); - ~cs_shared_state(); + ~internal_state(); void *alloc(void *ptr, size_t os, size_t ns); @@ -91,24 +91,24 @@ struct cs_shared_state { } }; -inline cs_shared_state *cs_get_sstate(cs_state &cs) { +inline internal_state *state_get_internal(state &cs) { return cs.p_state; } template -inline cs_allocator::cs_allocator(cs_shared_state *s): state{s} {} +inline std_allocator::std_allocator(internal_state *s): istate{s} {} template -inline cs_allocator::cs_allocator(cs_state &s): state{cs_get_sstate(s)} {} +inline std_allocator::std_allocator(state &s): istate{state_get_internal(s)} {} template -inline T *cs_allocator::allocate(std::size_t n) { - return static_cast(state->alloc(nullptr, 0, n * sizeof(T))); +inline T *std_allocator::allocate(std::size_t n) { + return static_cast(istate->alloc(nullptr, 0, n * sizeof(T))); } template -inline void cs_allocator::deallocate(T *p, std::size_t n) { - state->alloc(p, n, 0); +inline void std_allocator::deallocate(T *p, std::size_t n) { + istate->alloc(p, n, 0); } } /* namespace cscript */ diff --git a/src/cs_std.hh b/src/cs_std.hh index fa803c5..246ad30 100644 --- a/src/cs_std.hh +++ b/src/cs_std.hh @@ -33,15 +33,15 @@ inline void call_with_cleanup(F1 &&dof, F2 &&clf) { /* a simple static array with elements constructed using ctor args */ template -struct cs_valarray { +struct valarray { template - cs_valarray(A &&...args) { + valarray(A &&...args) { for (std::size_t i = 0; i < N; ++i) { new (&stor[i]) T{std::forward(args)...}; } } - ~cs_valarray() { + ~valarray() { for (std::size_t i = 0; i < N; ++i) { reinterpret_cast(&stor[i])->~T(); } @@ -57,11 +57,11 @@ struct cs_valarray { /* a value buffer */ template -struct cs_valbuf { - cs_valbuf() = delete; +struct valbuf { + valbuf() = delete; - cs_valbuf(cs_shared_state *cs): buf{cs_allocator{cs}} {} - cs_valbuf(cs_state &cs): buf{cs_allocator{cs}} {} + valbuf(internal_state *cs): buf{std_allocator{cs}} {} + valbuf(state &cs): buf{std_allocator{cs}} {} using size_type = std::size_t; using value_type = T; @@ -94,17 +94,17 @@ struct cs_valbuf { T *data() { return &buf[0]; } T const *data() const { return &buf[0]; } - std::vector> buf; + std::vector> buf; }; /* specialization of value buffer for bytes */ -struct cs_charbuf: cs_valbuf { - cs_charbuf(cs_shared_state *cs): cs_valbuf{cs} {} - cs_charbuf(cs_state &cs): cs_valbuf{cs} {} +struct charbuf: valbuf { + charbuf(internal_state *cs): valbuf{cs} {} + charbuf(state &cs): valbuf{cs} {} void append(char const *beg, char const *end) { - cs_valbuf::append(beg, end); + valbuf::append(beg, end); } void append(std::string_view v) { diff --git a/src/cs_strman.cc b/src/cs_strman.cc index 0a4d330..bec9bd3 100644 --- a/src/cs_strman.cc +++ b/src/cs_strman.cc @@ -4,18 +4,18 @@ namespace cscript { -struct cs_strref_state { +struct string_ref_state { std::size_t length; std::size_t refcount; }; -inline cs_strref_state *get_ref_state(char const *ptr) { - return const_cast( - reinterpret_cast(ptr) +inline string_ref_state *get_ref_state(char const *ptr) { + return const_cast( + reinterpret_cast(ptr) ) - 1; } -char const *cs_strman::add(std::string_view str) { +char const *string_pool::add(std::string_view str) { auto it = counts.find(str); /* already present: just increment ref */ if (it != counts.end()) { @@ -36,13 +36,13 @@ char const *cs_strman::add(std::string_view str) { return strp; } -char const *cs_strman::ref(char const *ptr) { +char const *string_pool::ref(char const *ptr) { auto *ss = get_ref_state(ptr); ++ss->refcount; return ptr; } -cs_strref cs_strman::steal(char *ptr) { +string_ref string_pool::steal(char *ptr) { auto *ss = get_ref_state(ptr); auto sr = std::string_view{ptr, ss->length}; /* much like add(), but we already have memory */ @@ -51,16 +51,16 @@ cs_strref cs_strman::steal(char *ptr) { auto *st = it->second; if (st) { /* the buffer is superfluous now */ - cstate->alloc(ss, ss->length + sizeof(cs_strref_state) + 1, 0); - return cs_strref{reinterpret_cast(st + 1), cstate}; + cstate->alloc(ss, ss->length + sizeof(string_ref_state) + 1, 0); + return string_ref{reinterpret_cast(st + 1), cstate}; } } - ss->refcount = 0; /* cs_strref will increment it */ + ss->refcount = 0; /* string_ref will increment it */ counts.emplace(sr, ss); - return cs_strref{ptr, cstate}; + return string_ref{ptr, cstate}; } -void cs_strman::unref(char const *ptr) { +void string_pool::unref(char const *ptr) { auto *ss = get_ref_state(ptr); if (!--ss->refcount) { /* refcount zero, so ditch it @@ -70,16 +70,16 @@ void cs_strman::unref(char const *ptr) { auto it = counts.find(sr); if (it == counts.end()) { /* internal error: this should *never* happen */ - throw cs_internal_error{"no refcount"}; + throw internal_error{"no refcount"}; } /* we're freeing the key */ counts.erase(it); /* dealloc */ - cstate->alloc(ss, ss->length + sizeof(cs_strref_state) + 1, 0); + cstate->alloc(ss, ss->length + sizeof(string_ref_state) + 1, 0); } } -char const *cs_strman::find(std::string_view str) const { +char const *string_pool::find(std::string_view str) const { auto it = counts.find(str); if (it == counts.end()) { return nullptr; @@ -87,18 +87,18 @@ char const *cs_strman::find(std::string_view str) const { return reinterpret_cast(it->second + 1); } -std::string_view cs_strman::get(char const *ptr) const { +std::string_view string_pool::get(char const *ptr) const { auto *ss = get_ref_state(ptr); return std::string_view{ptr, ss->length}; } -char *cs_strman::alloc_buf(std::size_t len) const { - auto mem = cstate->alloc(nullptr, 0, len + sizeof(cs_strref_state) + 1); +char *string_pool::alloc_buf(std::size_t len) const { + auto mem = cstate->alloc(nullptr, 0, len + sizeof(string_ref_state) + 1); if (!mem) { - throw cs_internal_error{"allocation failed"}; + throw internal_error{"allocation failed"}; } /* write length and initial refcount */ - auto *sst = static_cast(mem); + auto *sst = static_cast(mem); sst->length = len; sst->refcount = 1; /* pre-terminate */ @@ -112,48 +112,48 @@ char *cs_strman::alloc_buf(std::size_t len) const { /* strref */ -LIBCUBESCRIPT_EXPORT cs_strref::cs_strref( - cs_shared_state *cs, std::string_view str +LIBCUBESCRIPT_EXPORT string_ref::string_ref( + internal_state *cs, std::string_view str ): p_state{cs} { p_str = cs->strman->add(str); } -LIBCUBESCRIPT_EXPORT cs_strref::cs_strref(cs_state &cs, std::string_view str): +LIBCUBESCRIPT_EXPORT string_ref::string_ref(state &cs, std::string_view str): p_state{cs.p_state} { p_str = p_state->strman->add(str); } -LIBCUBESCRIPT_EXPORT cs_strref::cs_strref(cs_strref const &ref): +LIBCUBESCRIPT_EXPORT string_ref::string_ref(string_ref const &ref): p_state{ref.p_state}, p_str{ref.p_str} { p_state->strman->ref(p_str); } -/* this can be used by friends to do quick cs_strref creation */ -LIBCUBESCRIPT_EXPORT cs_strref::cs_strref(char const *p, cs_shared_state *cs): +/* this can be used by friends to do quick string_ref creation */ +LIBCUBESCRIPT_EXPORT string_ref::string_ref(char const *p, internal_state *cs): p_state{cs} { p_str = p_state->strman->ref(p); } -LIBCUBESCRIPT_EXPORT cs_strref::~cs_strref() { +LIBCUBESCRIPT_EXPORT string_ref::~string_ref() { p_state->strman->unref(p_str); } -LIBCUBESCRIPT_EXPORT cs_strref &cs_strref::operator=(cs_strref const &ref) { +LIBCUBESCRIPT_EXPORT string_ref &string_ref::operator=(string_ref const &ref) { p_str = ref.p_str; p_state = ref.p_state; p_state->strman->ref(p_str); return *this; } -LIBCUBESCRIPT_EXPORT cs_strref::operator std::string_view() const { +LIBCUBESCRIPT_EXPORT string_ref::operator std::string_view() const { return p_state->strman->get(p_str); } -LIBCUBESCRIPT_EXPORT bool cs_strref::operator==(cs_strref const &s) const { +LIBCUBESCRIPT_EXPORT bool string_ref::operator==(string_ref const &s) const { return p_str == s.p_str; } diff --git a/src/cs_strman.hh b/src/cs_strman.hh index fd2ac73..d115174 100644 --- a/src/cs_strman.hh +++ b/src/cs_strman.hh @@ -11,7 +11,7 @@ namespace cscript { -struct cs_strref_state; +struct string_ref_state; /* string manager * @@ -29,19 +29,19 @@ struct cs_strref_state; * for now we don't bother... */ -struct cs_strman { - using allocator_type = cs_allocator< - std::pair +struct string_pool { + using allocator_type = std_allocator< + std::pair >; - cs_strman() = delete; - cs_strman(cs_shared_state *cs): cstate{cs}, counts{allocator_type{cs}} {} - ~cs_strman() {} + string_pool() = delete; + string_pool(internal_state *cs): cstate{cs}, counts{allocator_type{cs}} {} + ~string_pool() {} - cs_strman(cs_strman const &) = delete; - cs_strman(cs_strman &&) = delete; + string_pool(string_pool const &) = delete; + string_pool(string_pool &&) = delete; - cs_strman &operator=(cs_strman const &) = delete; - cs_strman &operator=(cs_strman &&) = delete; + string_pool &operator=(string_pool const &) = delete; + string_pool &operator=(string_pool &&) = delete; /* adds a string into the manager using any source, and returns a managed * version; this is "slow" as it has to hash the string and potentially @@ -58,7 +58,7 @@ struct cs_strman { /* this will use the provided memory, assuming it is a fresh string that * is yet to be added; the memory must be allocated with alloc_buf() */ - cs_strref steal(char *ptr); + string_ref steal(char *ptr); /* decrements the reference count and removes it from the system if * that reaches zero; likewise, only safe with pointers that are managed @@ -78,9 +78,9 @@ struct cs_strman { */ char *alloc_buf(std::size_t len) const; - cs_shared_state *cstate; + internal_state *cstate; std::unordered_map< - std::string_view, cs_strref_state *, + std::string_view, string_ref_state *, std::hash, std::equal_to, allocator_type diff --git a/src/cs_val.cc b/src/cs_val.cc index 8336dcf..53e9813 100644 --- a/src/cs_val.cc +++ b/src/cs_val.cc @@ -7,12 +7,12 @@ namespace cscript { -static std::string_view intstr(cs_int v, cs_charbuf &buf) { +static std::string_view intstr(integer_type v, charbuf &buf) { buf.reserve(32); - int n = snprintf(buf.data(), 32, CS_INT_FORMAT, v); + int n = snprintf(buf.data(), 32, INT_FORMAT, v); if (n > 32) { buf.reserve(n + 1); - int nn = snprintf(buf.data(), n + 1, CS_INT_FORMAT, v); + int nn = snprintf(buf.data(), n + 1, INT_FORMAT, v); if ((nn > n) || (nn <= 0)) { n = -1; } else { @@ -20,26 +20,26 @@ static std::string_view intstr(cs_int v, cs_charbuf &buf) { } } if (n <= 0) { - throw cs_internal_error{"format error"}; + throw internal_error{"format error"}; } return std::string_view{buf.data(), std::size_t(n)}; } -static std::string_view floatstr(cs_float v, cs_charbuf &buf) { +static std::string_view floatstr(float_type v, charbuf &buf) { buf.reserve(32); int n; if (v == std::floor(v)) { - n = snprintf(buf.data(), 32, CS_ROUND_FLOAT_FORMAT, v); + n = snprintf(buf.data(), 32, ROUND_FLOAT_FORMAT, v); } else { - n = snprintf(buf.data(), 32, CS_FLOAT_FORMAT, v); + n = snprintf(buf.data(), 32, FLOAT_FORMAT, v); } if (n > 32) { buf.reserve(n + 1); int nn; if (v == std::floor(v)) { - nn = snprintf(buf.data(), n + 1, CS_ROUND_FLOAT_FORMAT, v); + nn = snprintf(buf.data(), n + 1, ROUND_FLOAT_FORMAT, v); } else { - nn = snprintf(buf.data(), n + 1, CS_FLOAT_FORMAT, v); + nn = snprintf(buf.data(), n + 1, FLOAT_FORMAT, v); } if ((nn > n) || (nn <= 0)) { n = -1; @@ -48,14 +48,14 @@ static std::string_view floatstr(cs_float v, cs_charbuf &buf) { } } if (n <= 0) { - throw cs_internal_error{"format error"}; + throw internal_error{"format error"}; } return std::string_view{buf.data(), std::size_t(n)}; } template struct stor_priv_t { - cs_shared_state *state; + internal_state *state; T val; }; @@ -66,12 +66,12 @@ static inline T &csv_get(U &stor) { } template -static inline void csv_cleanup(cs_value_type tv, T &stor) { +static inline void csv_cleanup(value_type tv, T &stor) { switch (tv) { - case cs_value_type::STRING: - reinterpret_cast(&stor)->~cs_strref(); + case value_type::STRING: + reinterpret_cast(&stor)->~string_ref(); break; - case cs_value_type::CODE: { + case value_type::CODE: { bcode_unref(csv_get(stor)); break; } @@ -80,43 +80,43 @@ static inline void csv_cleanup(cs_value_type tv, T &stor) { } } -cs_value::cs_value(cs_state &st): cs_value(*st.p_state) {} +any_value::any_value(state &st): any_value(*st.p_state) {} -cs_value::cs_value(cs_shared_state &st): - p_stor(), p_type(cs_value_type::NONE) +any_value::any_value(internal_state &st): + p_stor(), p_type(value_type::NONE) { reinterpret_cast *>(&p_stor)->state = &st; } -cs_value::~cs_value() { +any_value::~any_value() { csv_cleanup(p_type, p_stor); } -cs_value::cs_value(cs_value const &v): cs_value(*v.state()) { +any_value::any_value(any_value const &v): any_value(*v.get_state()) { *this = v; } -cs_value::cs_value(cs_value &&v): cs_value(*v.state()) { +any_value::any_value(any_value &&v): any_value(*v.get_state()) { *this = std::move(v); } -cs_value &cs_value::operator=(cs_value const &v) { +any_value &any_value::operator=(any_value const &v) { csv_cleanup(p_type, p_stor); - p_type = cs_value_type::NONE; + p_type = value_type::NONE; switch (v.get_type()) { - case cs_value_type::INT: - case cs_value_type::FLOAT: - case cs_value_type::IDENT: + case value_type::INT: + case value_type::FLOAT: + case value_type::IDENT: p_type = v.p_type; p_stor = v.p_stor; break; - case cs_value_type::STRING: - p_type = cs_value_type::STRING; - new (&p_stor) cs_strref{ - *reinterpret_cast(&v.p_stor) + case value_type::STRING: + p_type = value_type::STRING; + new (&p_stor) string_ref{ + *reinterpret_cast(&v.p_stor) }; break; - case cs_value_type::CODE: + case value_type::CODE: set_code(v.get_code()); break; default: @@ -125,78 +125,78 @@ cs_value &cs_value::operator=(cs_value const &v) { return *this; } -cs_value &cs_value::operator=(cs_value &&v) { +any_value &any_value::operator=(any_value &&v) { *this = v; v.set_none(); return *this; } -cs_value_type cs_value::get_type() const { +value_type any_value::get_type() const { return p_type; } -void cs_value::set_int(cs_int val) { +void any_value::set_int(integer_type val) { csv_cleanup(p_type, p_stor); - p_type = cs_value_type::INT; - csv_get(p_stor) = val; + p_type = value_type::INT; + csv_get(p_stor) = val; } -void cs_value::set_float(cs_float val) { +void any_value::set_float(float_type val) { csv_cleanup(p_type, p_stor); - p_type = cs_value_type::FLOAT; - csv_get(p_stor) = val; + p_type = value_type::FLOAT; + csv_get(p_stor) = val; } -void cs_value::set_str(std::string_view val) { +void any_value::set_str(std::string_view val) { csv_cleanup(p_type, p_stor); - new (&p_stor) cs_strref{state(), val}; - p_type = cs_value_type::STRING; + new (&p_stor) string_ref{get_state(), val}; + p_type = value_type::STRING; } -void cs_value::set_str(cs_strref const &val) { +void any_value::set_str(string_ref const &val) { csv_cleanup(p_type, p_stor); - new (&p_stor) cs_strref{val}; - p_type = cs_value_type::STRING; + new (&p_stor) string_ref{val}; + p_type = value_type::STRING; } -void cs_value::set_none() { +void any_value::set_none() { csv_cleanup(p_type, p_stor); - p_type = cs_value_type::NONE; + p_type = value_type::NONE; } -void cs_value::set_code(cs_bcode *val) { +void any_value::set_code(bcode *val) { csv_cleanup(p_type, p_stor); - p_type = cs_value_type::CODE; - bcode_ref(val->get_raw()); - csv_get(p_stor) = val; + p_type = value_type::CODE; + bcode_addref(val->get_raw()); + csv_get(p_stor) = val; } -void cs_value::set_ident(cs_ident *val) { +void any_value::set_ident(ident *val) { csv_cleanup(p_type, p_stor); - p_type = cs_value_type::IDENT; - csv_get(p_stor) = val; + p_type = value_type::IDENT; + csv_get(p_stor) = val; } -void cs_value::force_none() { - if (get_type() == cs_value_type::NONE) { +void any_value::force_none() { + if (get_type() == value_type::NONE) { return; } set_none(); } -cs_float cs_value::force_float() { - cs_float rf = 0.0f; +float_type any_value::force_float() { + float_type rf = 0.0f; switch (get_type()) { - case cs_value_type::INT: - rf = csv_get(p_stor); + case value_type::INT: + rf = csv_get(p_stor); break; - case cs_value_type::STRING: + case value_type::STRING: rf = parse_float( - *reinterpret_cast(&p_stor) + *reinterpret_cast(&p_stor) ); break; - case cs_value_type::FLOAT: - return csv_get(p_stor); + case value_type::FLOAT: + return csv_get(p_stor); default: break; } @@ -204,19 +204,19 @@ cs_float cs_value::force_float() { return rf; } -cs_int cs_value::force_int() { - cs_int ri = 0; +integer_type any_value::force_int() { + integer_type ri = 0; switch (get_type()) { - case cs_value_type::FLOAT: - ri = csv_get(p_stor); + case value_type::FLOAT: + ri = csv_get(p_stor); break; - case cs_value_type::STRING: + case value_type::STRING: ri = parse_int( - *reinterpret_cast(&p_stor) + *reinterpret_cast(&p_stor) ); break; - case cs_value_type::INT: - return csv_get(p_stor); + case value_type::INT: + return csv_get(p_stor); default: break; } @@ -224,35 +224,35 @@ cs_int cs_value::force_int() { return ri; } -std::string_view cs_value::force_str() { - cs_charbuf rs{state()}; +std::string_view any_value::force_str() { + charbuf rs{get_state()}; std::string_view str; switch (get_type()) { - case cs_value_type::FLOAT: - str = floatstr(csv_get(p_stor), rs); + case value_type::FLOAT: + str = floatstr(csv_get(p_stor), rs); break; - case cs_value_type::INT: - str = intstr(csv_get(p_stor), rs); + case value_type::INT: + str = intstr(csv_get(p_stor), rs); break; - case cs_value_type::STRING: - return *reinterpret_cast(&p_stor); + case value_type::STRING: + return *reinterpret_cast(&p_stor); default: str = rs.str(); break; } set_str(str); - return std::string_view(*reinterpret_cast(&p_stor)); + return std::string_view(*reinterpret_cast(&p_stor)); } -cs_int cs_value::get_int() const { +integer_type any_value::get_int() const { switch (get_type()) { - case cs_value_type::FLOAT: - return cs_int(csv_get(p_stor)); - case cs_value_type::INT: - return csv_get(p_stor); - case cs_value_type::STRING: + case value_type::FLOAT: + return integer_type(csv_get(p_stor)); + case value_type::INT: + return csv_get(p_stor); + case value_type::STRING: return parse_int( - *reinterpret_cast(&p_stor) + *reinterpret_cast(&p_stor) ); default: break; @@ -260,15 +260,15 @@ cs_int cs_value::get_int() const { return 0; } -cs_float cs_value::get_float() const { +float_type any_value::get_float() const { switch (get_type()) { - case cs_value_type::FLOAT: - return csv_get(p_stor); - case cs_value_type::INT: - return cs_float(csv_get(p_stor)); - case cs_value_type::STRING: + case value_type::FLOAT: + return csv_get(p_stor); + case value_type::INT: + return float_type(csv_get(p_stor)); + case value_type::STRING: return parse_float( - *reinterpret_cast(&p_stor) + *reinterpret_cast(&p_stor) ); default: break; @@ -276,48 +276,52 @@ cs_float cs_value::get_float() const { return 0.0f; } -cs_bcode *cs_value::get_code() const { - if (get_type() != cs_value_type::CODE) { +bcode *any_value::get_code() const { + if (get_type() != value_type::CODE) { return nullptr; } - return csv_get(p_stor); + return csv_get(p_stor); } -cs_ident *cs_value::get_ident() const { - if (get_type() != cs_value_type::IDENT) { +ident *any_value::get_ident() const { + if (get_type() != value_type::IDENT) { return nullptr; } - return csv_get(p_stor); + return csv_get(p_stor); } -cs_strref cs_value::get_str() const { +string_ref any_value::get_str() const { switch (get_type()) { - case cs_value_type::STRING: - return *reinterpret_cast(&p_stor); - case cs_value_type::INT: { - cs_charbuf rs{state()}; - return cs_strref{state(), intstr(csv_get(p_stor), rs)}; + case value_type::STRING: + return *reinterpret_cast(&p_stor); + case value_type::INT: { + charbuf rs{get_state()}; + return string_ref{ + get_state(), intstr(csv_get(p_stor), rs) + }; } - case cs_value_type::FLOAT: { - cs_charbuf rs{state()}; - return cs_strref{state(), floatstr(csv_get(p_stor), rs)}; + case value_type::FLOAT: { + charbuf rs{get_state()}; + return string_ref{ + get_state(), floatstr(csv_get(p_stor), rs) + }; } default: break; } - return cs_strref{state(), ""}; + return string_ref{get_state(), ""}; } -void cs_value::get_val(cs_value &r) const { +void any_value::get_val(any_value &r) const { switch (get_type()) { - case cs_value_type::STRING: + case value_type::STRING: r = *this; break; - case cs_value_type::INT: - r.set_int(csv_get(p_stor)); + case value_type::INT: + r.set_int(csv_get(p_stor)); break; - case cs_value_type::FLOAT: - r.set_float(csv_get(p_stor)); + case value_type::FLOAT: + r.set_float(csv_get(p_stor)); break; default: r.set_none(); @@ -325,47 +329,43 @@ void cs_value::get_val(cs_value &r) const { } } -LIBCUBESCRIPT_EXPORT bool cs_code_is_empty(cs_bcode *code) { +LIBCUBESCRIPT_EXPORT bool code_is_empty(bcode *code) { if (!code) { return true; } - return (*code->get_raw() & CS_CODE_OP_MASK) == CS_CODE_EXIT; + return (*code->get_raw() & BC_INST_OP_MASK) == BC_INST_EXIT; } -bool cs_value::code_is_empty() const { - if (get_type() != cs_value_type::CODE) { +bool any_value::code_is_empty() const { + if (get_type() != value_type::CODE) { return true; } - return cscript::cs_code_is_empty(csv_get(p_stor)); + return cscript::code_is_empty(csv_get(p_stor)); } -static inline bool cs_get_bool(std::string_view s) { - if (s.empty()) { - return false; - } - std::string_view end = s; - cs_int ival = parse_int(end, &end); - if (end.empty()) { - return !!ival; - } - end = s; - cs_float fval = parse_float(end, &end); - if (end.empty()) { - return !!fval; - } - return true; -} - -bool cs_value::get_bool() const { +bool any_value::get_bool() const { switch (get_type()) { - case cs_value_type::FLOAT: - return csv_get(p_stor) != 0; - case cs_value_type::INT: - return csv_get(p_stor) != 0; - case cs_value_type::STRING: - return cs_get_bool( - *reinterpret_cast(&p_stor) - ); + case value_type::FLOAT: + return csv_get(p_stor) != 0; + case value_type::INT: + return csv_get(p_stor) != 0; + case value_type::STRING: { + std::string_view s = *reinterpret_cast(&p_stor); + if (s.empty()) { + return false; + } + std::string_view end = s; + integer_type ival = parse_int(end, &end); + if (end.empty()) { + return !!ival; + } + end = s; + float_type fval = parse_float(end, &end); + if (end.empty()) { + return !!fval; + } + return true; + } default: return false; } @@ -373,74 +373,74 @@ bool cs_value::get_bool() const { /* stacked value for easy stack management */ -cs_stacked_value::cs_stacked_value(cs_state &cs, cs_ident *id): - cs_value(cs), p_a(nullptr), p_stack{cs}, p_pushed(false) +stacked_value::stacked_value(state &cs, ident *id): + any_value(cs), p_a(nullptr), p_stack{cs}, p_pushed(false) { set_alias(id); } -cs_stacked_value::~cs_stacked_value() { +stacked_value::~stacked_value() { pop(); - static_cast(this)->~cs_value(); + static_cast(this)->~any_value(); } -cs_stacked_value &cs_stacked_value::operator=(cs_value const &v) { - *static_cast(this) = v; +stacked_value &stacked_value::operator=(any_value const &v) { + *static_cast(this) = v; return *this; } -cs_stacked_value &cs_stacked_value::operator=(cs_value &&v) { - *static_cast(this) = std::move(v); +stacked_value &stacked_value::operator=(any_value &&v) { + *static_cast(this) = std::move(v); return *this; } -bool cs_stacked_value::set_alias(cs_ident *id) { +bool stacked_value::set_alias(ident *id) { if (!id || !id->is_alias()) { return false; } - p_a = static_cast(id); + p_a = static_cast(id); return true; } -cs_alias *cs_stacked_value::get_alias() const { +alias *stacked_value::get_alias() const { return p_a; } -bool cs_stacked_value::has_alias() const { +bool stacked_value::has_alias() const { return p_a != nullptr; } -bool cs_stacked_value::push() { +bool stacked_value::push() { if (!p_a) { return false; } - static_cast(p_a)->push_arg(*this, p_stack); + static_cast(p_a)->push_arg(*this, p_stack); p_pushed = true; return true; } -bool cs_stacked_value::pop() { +bool stacked_value::pop() { if (!p_pushed || !p_a) { return false; } - static_cast(p_a)->pop_arg(); + static_cast(p_a)->pop_arg(); p_pushed = false; return true; } /* public utilities */ -LIBCUBESCRIPT_EXPORT cs_strref cs_concat_values( - cs_state &cs, std::span vals, std::string_view sep +LIBCUBESCRIPT_EXPORT string_ref concat_values( + state &cs, std::span vals, std::string_view sep ) { - cs_charbuf buf{cs}; + charbuf buf{cs}; for (std::size_t i = 0; i < vals.size(); ++i) { switch (vals[i].get_type()) { - case cs_value_type::INT: - case cs_value_type::FLOAT: - case cs_value_type::STRING: + case value_type::INT: + case value_type::FLOAT: + case value_type::STRING: std::ranges::copy( - cs_value{vals[i]}.force_str(), std::back_inserter(buf) + any_value{vals[i]}.force_str(), std::back_inserter(buf) ); break; default: @@ -451,7 +451,7 @@ LIBCUBESCRIPT_EXPORT cs_strref cs_concat_values( } std::ranges::copy(sep, std::back_inserter(buf)); } - return cs_strref{cs, buf.str()}; + return string_ref{cs, buf.str()}; } } /* namespace cscript */ diff --git a/src/cs_vm.cc b/src/cs_vm.cc index c625763..0d32675 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -8,45 +8,45 @@ namespace cscript { -static inline bool cs_ident_has_cb(cs_ident *id) { +static inline bool ident_has_cb(ident *id) { if (!id->is_command() && !id->is_special()) { return false; } - return !!static_cast(id)->p_cb_cftv; + return !!static_cast(id)->p_cb_cftv; } -static inline void cs_push_alias(cs_state &cs, cs_ident *id, cs_ident_stack &st) { +static inline void push_alias(state &cs, ident *id, ident_stack &st) { if (id->is_alias() && (id->get_index() >= MaxArguments)) { - cs_value nv{cs}; - static_cast(id)->push_arg(nv, st); + any_value nv{cs}; + static_cast(id)->push_arg(nv, st); } } -static inline void cs_pop_alias(cs_ident *id) { +static inline void pop_alias(ident *id) { if (id->is_alias() && (id->get_index() >= MaxArguments)) { - static_cast(id)->pop_arg(); + static_cast(id)->pop_arg(); } } -cs_stack_state::cs_stack_state(cs_state &cs, cs_stack_state_node *nd, bool gap): +stack_state::stack_state(state &cs, stack_state_node *nd, bool gap): p_state(cs), p_node(nd), p_gap(gap) {} -cs_stack_state::cs_stack_state(cs_stack_state &&st): +stack_state::stack_state(stack_state &&st): p_state(st.p_state), p_node(st.p_node), p_gap(st.p_gap) { st.p_node = nullptr; st.p_gap = false; } -cs_stack_state::~cs_stack_state() { +stack_state::~stack_state() { size_t len = 0; - for (cs_stack_state_node const *nd = p_node; nd; nd = nd->next) { + for (stack_state_node const *nd = p_node; nd; nd = nd->next) { ++len; } p_state.p_state->destroy_array(p_node, len); } -cs_stack_state &cs_stack_state::operator=(cs_stack_state &&st) { +stack_state &stack_state::operator=(stack_state &&st) { p_node = st.p_node; p_gap = st.p_gap; st.p_node = nullptr; @@ -54,32 +54,32 @@ cs_stack_state &cs_stack_state::operator=(cs_stack_state &&st) { return *this; } -cs_stack_state_node const *cs_stack_state::get() const { +stack_state_node const *stack_state::get() const { return p_node; } -bool cs_stack_state::gap() const { +bool stack_state::gap() const { return p_gap; } -static inline uint32_t *forcecode(cs_state &cs, cs_value &v) { +static inline uint32_t *forcecode(state &cs, any_value &v) { auto *code = v.get_code(); if (!code) { - cs_gen_state gs(cs); + codegen_state gs(cs); gs.code.reserve(64); gs.gen_main(v.get_str()); gs.done(); uint32_t *cbuf = bcode_alloc(cs, gs.code.size()); memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t)); - v.set_code(reinterpret_cast(cbuf + 1)); + v.set_code(reinterpret_cast(cbuf + 1)); code = v.get_code(); } return code->get_raw(); } -static inline void forcecond(cs_state &cs, cs_value &v) { +static inline void forcecond(state &cs, any_value &v) { switch (v.get_type()) { - case cs_value_type::STRING: + case value_type::STRING: if (!std::string_view{v.get_str()}.empty()) { forcecode(cs, v); } else { @@ -91,20 +91,20 @@ static inline void forcecond(cs_state &cs, cs_value &v) { } } -static inline void force_arg(cs_value &v, int type) { +static inline void force_arg(any_value &v, int type) { switch (type) { - case CS_RET_STRING: - if (v.get_type() != cs_value_type::STRING) { + case BC_RET_STRING: + if (v.get_type() != value_type::STRING) { v.force_str(); } break; - case CS_RET_INT: - if (v.get_type() != cs_value_type::INT) { + case BC_RET_INT: + if (v.get_type() != value_type::INT) { v.force_int(); } break; - case CS_RET_FLOAT: - if (v.get_type() != cs_value_type::FLOAT) { + case BC_RET_FLOAT: + if (v.get_type() != value_type::FLOAT) { v.force_float(); } break; @@ -112,7 +112,7 @@ static inline void force_arg(cs_value &v, int type) { } static inline void callcommand( - cs_state &cs, cs_command_impl *id, cs_value *args, cs_value &res, + state &cs, command_impl *id, any_value *args, any_value &res, int numargs, bool lookup = false ) { int i = -1, fakeargs = 0; @@ -136,7 +136,7 @@ static inline void callcommand( if (rep) { break; } - args[i].set_int(std::numeric_limits::min()); + args[i].set_int(std::numeric_limits::min()); fakeargs++; } else { args[i].force_int(); @@ -202,7 +202,7 @@ static inline void callcommand( break; } args[i].set_code( - bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_NULL) + bcode_get_empty(state_get_internal(cs)->empty, VAL_NULL) ); fakeargs++; } else { @@ -226,22 +226,22 @@ static inline void callcommand( break; case 'N': i += 1; - args[i].set_int(cs_int(lookup ? -1 : i - fakeargs)); + args[i].set_int(integer_type(lookup ? -1 : i - fakeargs)); break; case 'C': { i = std::max(i + 1, numargs); - cs_value tv{cs}; - tv.set_str(cs_concat_values( + any_value tv{cs}; + tv.set_str(concat_values( cs, std::span{args, std::size_t(i)}, " " )); - static_cast(id)->call( - cs, std::span(&tv, &tv + 1), res + static_cast(id)->call( + cs, std::span(&tv, &tv + 1), res ); return; } case 'V': i = std::max(i + 1, numargs); - static_cast(id)->call( + static_cast(id)->call( cs, std::span{args, std::size_t(i)}, res ); return; @@ -257,33 +257,33 @@ static inline void callcommand( } } ++i; - static_cast(id)->call( - cs, std::span{args, std::size_t(i)}, res + static_cast(id)->call( + cs, std::span{args, std::size_t(i)}, res ); } -static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result); +static uint32_t *runcode(state &cs, uint32_t *code, any_value &result); -static inline void cs_call_alias( - cs_state &cs, cs_alias *a, cs_value *args, cs_value &result, +static inline void call_alias( + state &cs, alias *a, any_value *args, any_value &result, int callargs, int &nargs, int offset, int skip, uint32_t op ) { - cs_ivar *anargs = static_cast(cs.p_state->identmap[NumargsIdx]); - cs_valarray argstack{cs}; + integer_var *anargs = static_cast(cs.p_state->identmap[NumargsIdx]); + valarray argstack{cs}; for(int i = 0; i < callargs; i++) { - static_cast(cs.p_state->identmap[i])->push_arg( + static_cast(cs.p_state->identmap[i])->push_arg( args[offset + i], argstack[i], false ); } int oldargs = anargs->get_value(); anargs->set_value(callargs); int oldflags = cs.identflags; - cs.identflags |= a->get_flags()&CS_IDF_OVERRIDDEN; - cs_ident_link aliaslink = { + cs.identflags |= a->get_flags()&IDENT_FLAG_OVERRIDDEN; + ident_link aliaslink = { a, cs.p_callstack, (1<(a)->compile_code(cs)->get_raw(); + uint32_t *codep = static_cast(a)->compile_code(cs)->get_raw(); bcode_incr(codep); call_with_cleanup([&]() { runcode(cs, codep+1, result); @@ -292,18 +292,18 @@ static inline void cs_call_alias( cs.p_callstack = aliaslink.next; cs.identflags = oldflags; for (int i = 0; i < callargs; i++) { - static_cast(cs.p_state->identmap[i])->pop_arg(); + static_cast(cs.p_state->identmap[i])->pop_arg(); } int argmask = aliaslink.usedargs & int(~0U << callargs); for (; argmask; ++callargs) { if (argmask & (1 << callargs)) { - static_cast( + static_cast( cs.p_state->identmap[callargs] )->pop_arg(); argmask &= ~(1 << callargs); } } - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); anargs->set_value(oldargs); nargs = offset - skip; }); @@ -314,9 +314,9 @@ static thread_local int rundepth = 0; struct RunDepthRef { RunDepthRef() = delete; - RunDepthRef(cs_state &cs) { + RunDepthRef(state &cs) { if (rundepth >= MaxRunDepth) { - throw cs_error(cs, "exceeded recursion limit"); + throw error(cs, "exceeded recursion limit"); } ++rundepth; } @@ -325,67 +325,67 @@ struct RunDepthRef { ~RunDepthRef() { --rundepth; } }; -static inline cs_alias *cs_get_lookup_id(cs_state &cs, uint32_t op) { - cs_ident *id = cs.p_state->identmap[op >> 8]; - if (id->get_flags() & CS_IDF_UNKNOWN) { - throw cs_error(cs, "unknown alias lookup: %s", id->get_name().data()); +static inline alias *get_lookup_id(state &cs, uint32_t op) { + ident *id = cs.p_state->identmap[op >> 8]; + if (id->get_flags() & IDENT_FLAG_UNKNOWN) { + throw error(cs, "unknown alias lookup: %s", id->get_name().data()); } - return static_cast(id); + return static_cast(id); } -static inline cs_alias *cs_get_lookuparg_id(cs_state &cs, uint32_t op) { - cs_ident *id = cs.p_state->identmap[op >> 8]; +static inline alias *get_lookuparg_id(state &cs, uint32_t op) { + ident *id = cs.p_state->identmap[op >> 8]; if (!ident_is_used_arg(id, cs)) { return nullptr; } - return static_cast(id); + return static_cast(id); } -static inline int cs_get_lookupu_type( - cs_state &cs, cs_value &arg, cs_ident *&id, uint32_t op +static inline int get_lookupu_type( + state &cs, any_value &arg, ident *&id, uint32_t op ) { - if (arg.get_type() != cs_value_type::STRING) { + if (arg.get_type() != value_type::STRING) { return -2; /* default case */ } id = cs.get_ident(arg.get_str()); if (id) { switch(id->get_type()) { - case cs_ident_type::ALIAS: - if (id->get_flags() & CS_IDF_UNKNOWN) { + case ident_type::ALIAS: + if (id->get_flags() & IDENT_FLAG_UNKNOWN) { break; } if ((id->get_index() < MaxArguments) && !ident_is_used_arg(id, cs)) { return ID_UNKNOWN; } return ID_ALIAS; - case cs_ident_type::SVAR: + case ident_type::SVAR: return ID_SVAR; - case cs_ident_type::IVAR: + case ident_type::IVAR: return ID_IVAR; - case cs_ident_type::FVAR: + case ident_type::FVAR: return ID_FVAR; - case cs_ident_type::COMMAND: { + case ident_type::COMMAND: { arg.set_none(); - cs_valarray buf{cs}; + valarray buf{cs}; callcommand( - cs, static_cast(id), + cs, static_cast(id), &buf[0], arg, 0, true ); - force_arg(arg, op & CS_CODE_RET_MASK); + force_arg(arg, op & BC_INST_RET_MASK); return -2; /* ignore */ } default: return ID_UNKNOWN; } } - throw cs_error(cs, "unknown alias lookup: %s", arg.get_str().data()); + throw error(cs, "unknown alias lookup: %s", arg.get_str().data()); } -static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { +static uint32_t *runcode(state &cs, uint32_t *code, any_value &result) { result.set_none(); RunDepthRef level{cs}; /* incr and decr on scope exit */ int numargs = 0; - cs_valarray args{cs}; + valarray args{cs}; auto &chook = cs.get_call_hook(); if (chook) { chook(cs); @@ -393,144 +393,144 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { for (;;) { uint32_t op = *code++; switch (op & 0xFF) { - case CS_CODE_START: - case CS_CODE_OFFSET: + case BC_INST_START: + case BC_INST_OFFSET: continue; - case CS_CODE_NULL | CS_RET_NULL: + case BC_INST_NULL | BC_RET_NULL: result.set_none(); continue; - case CS_CODE_NULL | CS_RET_STRING: + case BC_INST_NULL | BC_RET_STRING: result.set_str(""); continue; - case CS_CODE_NULL | CS_RET_INT: + case BC_INST_NULL | BC_RET_INT: result.set_int(0); continue; - case CS_CODE_NULL | CS_RET_FLOAT: + case BC_INST_NULL | BC_RET_FLOAT: result.set_float(0.0f); continue; - case CS_CODE_FALSE | CS_RET_STRING: + case BC_INST_FALSE | BC_RET_STRING: result.set_str("0"); continue; - case CS_CODE_FALSE | CS_RET_NULL: - case CS_CODE_FALSE | CS_RET_INT: + case BC_INST_FALSE | BC_RET_NULL: + case BC_INST_FALSE | BC_RET_INT: result.set_int(0); continue; - case CS_CODE_FALSE | CS_RET_FLOAT: + case BC_INST_FALSE | BC_RET_FLOAT: result.set_float(0.0f); continue; - case CS_CODE_TRUE | CS_RET_STRING: + case BC_INST_TRUE | BC_RET_STRING: result.set_str("1"); continue; - case CS_CODE_TRUE | CS_RET_NULL: - case CS_CODE_TRUE | CS_RET_INT: + case BC_INST_TRUE | BC_RET_NULL: + case BC_INST_TRUE | BC_RET_INT: result.set_int(1); continue; - case CS_CODE_TRUE | CS_RET_FLOAT: + case BC_INST_TRUE | BC_RET_FLOAT: result.set_float(1.0f); continue; - case CS_CODE_NOT | CS_RET_STRING: + case BC_INST_NOT | BC_RET_STRING: --numargs; result.set_str(args[numargs].get_bool() ? "0" : "1"); continue; - case CS_CODE_NOT | CS_RET_NULL: - case CS_CODE_NOT | CS_RET_INT: + case BC_INST_NOT | BC_RET_NULL: + case BC_INST_NOT | BC_RET_INT: --numargs; result.set_int(!args[numargs].get_bool()); continue; - case CS_CODE_NOT | CS_RET_FLOAT: + case BC_INST_NOT | BC_RET_FLOAT: --numargs; - result.set_float(cs_float(!args[numargs].get_bool())); + result.set_float(float_type(!args[numargs].get_bool())); continue; - case CS_CODE_POP: + case BC_INST_POP: numargs -= 1; continue; - case CS_CODE_ENTER: + case BC_INST_ENTER: code = runcode(cs, code, args[numargs++]); continue; - case CS_CODE_ENTER_RESULT: + case BC_INST_ENTER_RESULT: code = runcode(cs, code, result); continue; - case CS_CODE_EXIT | CS_RET_STRING: - case CS_CODE_EXIT | CS_RET_INT: - case CS_CODE_EXIT | CS_RET_FLOAT: - force_arg(result, op & CS_CODE_RET_MASK); + case BC_INST_EXIT | BC_RET_STRING: + case BC_INST_EXIT | BC_RET_INT: + case BC_INST_EXIT | BC_RET_FLOAT: + force_arg(result, op & BC_INST_RET_MASK); /* fallthrough */ - case CS_CODE_EXIT | CS_RET_NULL: + case BC_INST_EXIT | BC_RET_NULL: return code; - case CS_CODE_RESULT_ARG | CS_RET_STRING: - case CS_CODE_RESULT_ARG | CS_RET_INT: - case CS_CODE_RESULT_ARG | CS_RET_FLOAT: - force_arg(result, op & CS_CODE_RET_MASK); + case BC_INST_RESULT_ARG | BC_RET_STRING: + case BC_INST_RESULT_ARG | BC_RET_INT: + case BC_INST_RESULT_ARG | BC_RET_FLOAT: + force_arg(result, op & BC_INST_RET_MASK); /* fallthrough */ - case CS_CODE_RESULT_ARG | CS_RET_NULL: + case BC_INST_RESULT_ARG | BC_RET_NULL: args[numargs++] = std::move(result); continue; - case CS_CODE_PRINT: - cs.print_var(*static_cast(cs.p_state->identmap[op >> 8])); + case BC_INST_PRINT: + cs.print_var(*static_cast(cs.p_state->identmap[op >> 8])); continue; - case CS_CODE_LOCAL: { + case BC_INST_LOCAL: { int numlocals = op >> 8, offset = numargs - numlocals; - cs_valarray locals{cs}; + valarray locals{cs}; for (int i = 0; i < numlocals; ++i) { - cs_push_alias(cs, args[offset + i].get_ident(), locals[i]); + push_alias(cs, args[offset + i].get_ident(), locals[i]); } call_with_cleanup([&]() { code = runcode(cs, code, result); }, [&]() { for (int i = offset; i < numargs; i++) { - cs_pop_alias(args[i].get_ident()); + pop_alias(args[i].get_ident()); } }); return code; } - case CS_CODE_DO_ARGS | CS_RET_NULL: - case CS_CODE_DO_ARGS | CS_RET_STRING: - case CS_CODE_DO_ARGS | CS_RET_INT: - case CS_CODE_DO_ARGS | CS_RET_FLOAT: - cs_do_args(cs, [&]() { + case BC_INST_DO_ARGS | BC_RET_NULL: + case BC_INST_DO_ARGS | BC_RET_STRING: + case BC_INST_DO_ARGS | BC_RET_INT: + case BC_INST_DO_ARGS | BC_RET_FLOAT: + call_with_args(cs, [&]() { cs.run(args[--numargs].get_code(), result); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); }); continue; /* fallthrough */ - case CS_CODE_DO | CS_RET_NULL: - case CS_CODE_DO | CS_RET_STRING: - case CS_CODE_DO | CS_RET_INT: - case CS_CODE_DO | CS_RET_FLOAT: + case BC_INST_DO | BC_RET_NULL: + case BC_INST_DO | BC_RET_STRING: + case BC_INST_DO | BC_RET_INT: + case BC_INST_DO | BC_RET_FLOAT: cs.run(args[--numargs].get_code(), result); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; - case CS_CODE_JUMP: { + case BC_INST_JUMP: { uint32_t len = op >> 8; code += len; continue; } - case CS_CODE_JUMP_B | CS_CODE_FLAG_TRUE: { + case BC_INST_JUMP_B | BC_INST_FLAG_TRUE: { uint32_t len = op >> 8; if (args[--numargs].get_bool()) { code += len; } continue; } - case CS_CODE_JUMP_B | CS_CODE_FLAG_FALSE: { + case BC_INST_JUMP_B | BC_INST_FLAG_FALSE: { uint32_t len = op >> 8; if (!args[--numargs].get_bool()) { code += len; } continue; } - case CS_CODE_JUMP_RESULT | CS_CODE_FLAG_TRUE: { + case BC_INST_JUMP_RESULT | BC_INST_FLAG_TRUE: { uint32_t len = op >> 8; --numargs; - if (args[numargs].get_type() == cs_value_type::CODE) { + if (args[numargs].get_type() == value_type::CODE) { cs.run(args[numargs].get_code(), result); } else { result = std::move(args[numargs]); @@ -540,10 +540,10 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { } continue; } - case CS_CODE_JUMP_RESULT | CS_CODE_FLAG_FALSE: { + case BC_INST_JUMP_RESULT | BC_INST_FLAG_FALSE: { uint32_t len = op >> 8; --numargs; - if (args[numargs].get_type() == cs_value_type::CODE) { + if (args[numargs].get_type() == value_type::CODE) { cs.run(args[numargs].get_code(), result); } else { result = std::move(args[numargs]); @@ -553,22 +553,22 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { } continue; } - case CS_CODE_BREAK | CS_CODE_FLAG_FALSE: + case BC_INST_BREAK | BC_INST_FLAG_FALSE: if (cs.is_in_loop()) { throw CsBreakException(); } else { - throw cs_error(cs, "no loop to break"); + throw error(cs, "no loop to break"); } break; - case CS_CODE_BREAK | CS_CODE_FLAG_TRUE: + case BC_INST_BREAK | BC_INST_FLAG_TRUE: if (cs.is_in_loop()) { throw CsContinueException(); } else { - throw cs_error(cs, "no loop to continue"); + throw error(cs, "no loop to continue"); } break; - case CS_CODE_VAL | CS_RET_STRING: { + case BC_INST_VAL | BC_RET_STRING: { uint32_t len = op >> 8; args[numargs++].set_str(std::string_view{ reinterpret_cast(code), len @@ -576,7 +576,7 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { code += len / sizeof(uint32_t) + 1; continue; } - case CS_CODE_VAL_INT | CS_RET_STRING: { + case BC_INST_VAL_INT | BC_RET_STRING: { char s[4] = { char((op >> 8) & 0xFF), char((op >> 16) & 0xFF), @@ -586,140 +586,140 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { args[numargs++].set_str(static_cast(s)); continue; } - case CS_CODE_VAL | CS_RET_NULL: - case CS_CODE_VAL_INT | CS_RET_NULL: + case BC_INST_VAL | BC_RET_NULL: + case BC_INST_VAL_INT | BC_RET_NULL: args[numargs++].set_none(); continue; - case CS_CODE_VAL | CS_RET_INT: + case BC_INST_VAL | BC_RET_INT: args[numargs++].set_int( - *reinterpret_cast(code) + *reinterpret_cast(code) ); - code += CsTypeStorageSize; + code += CsTypeStorageSize; continue; - case CS_CODE_VAL_INT | CS_RET_INT: - args[numargs++].set_int(cs_int(op) >> 8); + case BC_INST_VAL_INT | BC_RET_INT: + args[numargs++].set_int(integer_type(op) >> 8); continue; - case CS_CODE_VAL | CS_RET_FLOAT: + case BC_INST_VAL | BC_RET_FLOAT: args[numargs++].set_float( - *reinterpret_cast(code) + *reinterpret_cast(code) ); - code += CsTypeStorageSize; + code += CsTypeStorageSize; continue; - case CS_CODE_VAL_INT | CS_RET_FLOAT: - args[numargs++].set_float(cs_float(cs_int(op) >> 8)); + case BC_INST_VAL_INT | BC_RET_FLOAT: + args[numargs++].set_float(float_type(integer_type(op) >> 8)); continue; - case CS_CODE_DUP | CS_RET_NULL: + case BC_INST_DUP | BC_RET_NULL: args[numargs - 1].get_val(args[numargs]); numargs++; continue; - case CS_CODE_DUP | CS_RET_INT: + case BC_INST_DUP | BC_RET_INT: args[numargs].set_int(args[numargs - 1].get_int()); numargs++; continue; - case CS_CODE_DUP | CS_RET_FLOAT: + case BC_INST_DUP | BC_RET_FLOAT: args[numargs].set_float(args[numargs - 1].get_float()); numargs++; continue; - case CS_CODE_DUP | CS_RET_STRING: + case BC_INST_DUP | BC_RET_STRING: args[numargs] = args[numargs - 1]; args[numargs].force_str(); numargs++; continue; - case CS_CODE_FORCE | CS_RET_STRING: + case BC_INST_FORCE | BC_RET_STRING: args[numargs - 1].force_str(); continue; - case CS_CODE_FORCE | CS_RET_INT: + case BC_INST_FORCE | BC_RET_INT: args[numargs - 1].force_int(); continue; - case CS_CODE_FORCE | CS_RET_FLOAT: + case BC_INST_FORCE | BC_RET_FLOAT: args[numargs - 1].force_float(); continue; - case CS_CODE_RESULT | CS_RET_NULL: + case BC_INST_RESULT | BC_RET_NULL: result = std::move(args[--numargs]); continue; - case CS_CODE_RESULT | CS_RET_STRING: - case CS_CODE_RESULT | CS_RET_INT: - case CS_CODE_RESULT | CS_RET_FLOAT: + case BC_INST_RESULT | BC_RET_STRING: + case BC_INST_RESULT | BC_RET_INT: + case BC_INST_RESULT | BC_RET_FLOAT: result = std::move(args[--numargs]); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; - case CS_CODE_EMPTY | CS_RET_NULL: + case BC_INST_EMPTY | BC_RET_NULL: args[numargs++].set_code( - bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_NULL) + bcode_get_empty(state_get_internal(cs)->empty, VAL_NULL) ); break; - case CS_CODE_EMPTY | CS_RET_STRING: + case BC_INST_EMPTY | BC_RET_STRING: args[numargs++].set_code( - bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_STRING) + bcode_get_empty(state_get_internal(cs)->empty, VAL_STRING) ); break; - case CS_CODE_EMPTY | CS_RET_INT: + case BC_INST_EMPTY | BC_RET_INT: args[numargs++].set_code( - bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_INT) + bcode_get_empty(state_get_internal(cs)->empty, VAL_INT) ); break; - case CS_CODE_EMPTY | CS_RET_FLOAT: + case BC_INST_EMPTY | BC_RET_FLOAT: args[numargs++].set_code( - bcode_get_empty(cs_get_sstate(cs)->empty, CS_VAL_FLOAT) + bcode_get_empty(state_get_internal(cs)->empty, VAL_FLOAT) ); break; - case CS_CODE_BLOCK: { + case BC_INST_BLOCK: { uint32_t len = op >> 8; args[numargs++].set_code( - reinterpret_cast(code + 1) + reinterpret_cast(code + 1) ); code += len; continue; } - case CS_CODE_COMPILE: { - cs_value &arg = args[numargs - 1]; - cs_gen_state gs(cs); + case BC_INST_COMPILE: { + any_value &arg = args[numargs - 1]; + codegen_state gs(cs); switch (arg.get_type()) { - case cs_value_type::INT: + case value_type::INT: gs.code.reserve(8); - gs.code.push_back(CS_CODE_START); + gs.code.push_back(BC_INST_START); gs.gen_int(arg.get_int()); - gs.code.push_back(CS_CODE_RESULT); - gs.code.push_back(CS_CODE_EXIT); + gs.code.push_back(BC_INST_RESULT); + gs.code.push_back(BC_INST_EXIT); break; - case cs_value_type::FLOAT: + case value_type::FLOAT: gs.code.reserve(8); - gs.code.push_back(CS_CODE_START); + gs.code.push_back(BC_INST_START); gs.gen_float(arg.get_float()); - gs.code.push_back(CS_CODE_RESULT); - gs.code.push_back(CS_CODE_EXIT); + gs.code.push_back(BC_INST_RESULT); + gs.code.push_back(BC_INST_EXIT); break; - case cs_value_type::STRING: + case value_type::STRING: gs.code.reserve(64); gs.gen_main(arg.get_str()); break; default: gs.code.reserve(8); - gs.code.push_back(CS_CODE_START); + gs.code.push_back(BC_INST_START); gs.gen_null(); - gs.code.push_back(CS_CODE_RESULT); - gs.code.push_back(CS_CODE_EXIT); + gs.code.push_back(BC_INST_RESULT); + gs.code.push_back(BC_INST_EXIT); break; } gs.done(); uint32_t *cbuf = bcode_alloc(gs.cs, gs.code.size()); memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t)); arg.set_code( - reinterpret_cast(cbuf + 1) + reinterpret_cast(cbuf + 1) ); continue; } - case CS_CODE_COND: { - cs_value &arg = args[numargs - 1]; + case BC_INST_COND: { + any_value &arg = args[numargs - 1]; switch (arg.get_type()) { - case cs_value_type::STRING: { + case value_type::STRING: { std::string_view s = arg.get_str(); if (!s.empty()) { - cs_gen_state gs(cs); + codegen_state gs(cs); gs.code.reserve(64); gs.gen_main(s); gs.done(); @@ -728,7 +728,7 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t) ); - arg.set_code(reinterpret_cast(cbuf + 1)); + arg.set_code(reinterpret_cast(cbuf + 1)); } else { arg.force_none(); } @@ -740,16 +740,16 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } - case CS_CODE_IDENT: + case BC_INST_IDENT: args[numargs++].set_ident(cs.p_state->identmap[op >> 8]); continue; - case CS_CODE_IDENT_ARG: { - cs_alias *a = static_cast( + case BC_INST_IDENT_ARG: { + alias *a = static_cast( cs.p_state->identmap[op >> 8] ); if (!ident_is_used_arg(a, cs)) { - cs_value nv{cs}; - static_cast(a)->push_arg( + any_value nv{cs}; + static_cast(a)->push_arg( nv, cs.p_callstack->argstack[a->get_index()], false ); cs.p_callstack->usedargs |= 1 << a->get_index(); @@ -757,15 +757,15 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { args[numargs++].set_ident(a); continue; } - case CS_CODE_IDENT_U: { - cs_value &arg = args[numargs - 1]; - cs_ident *id = cs.p_state->identmap[DummyIdx]; - if (arg.get_type() == cs_value_type::STRING) { + case BC_INST_IDENT_U: { + any_value &arg = args[numargs - 1]; + ident *id = cs.p_state->identmap[DummyIdx]; + if (arg.get_type() == value_type::STRING) { id = cs.new_ident(arg.get_str()); } if ((id->get_index() < MaxArguments) && !ident_is_used_arg(id, cs)) { - cs_value nv{cs}; - static_cast(id)->push_arg( + any_value nv{cs}; + static_cast(id)->push_arg( nv, cs.p_callstack->argstack[id->get_index()], false ); cs.p_callstack->usedargs |= 1 << id->get_index(); @@ -774,23 +774,23 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } - case CS_CODE_LOOKUP_U | CS_RET_STRING: { - cs_ident *id = nullptr; - cs_value &arg = args[numargs - 1]; - switch (cs_get_lookupu_type(cs, arg, id, op)) { + case BC_INST_LOOKUP_U | BC_RET_STRING: { + ident *id = nullptr; + any_value &arg = args[numargs - 1]; + switch (get_lookupu_type(cs, arg, id, op)) { case ID_ALIAS: - arg = static_cast(id)->get_value(); + arg = static_cast(id)->get_value(); arg.force_str(); continue; case ID_SVAR: - arg.set_str(static_cast(id)->get_value()); + arg.set_str(static_cast(id)->get_value()); continue; case ID_IVAR: - arg.set_int(static_cast(id)->get_value()); + arg.set_int(static_cast(id)->get_value()); arg.force_str(); continue; case ID_FVAR: - arg.set_float(static_cast(id)->get_value()); + arg.set_float(static_cast(id)->get_value()); arg.force_str(); continue; case ID_UNKNOWN: @@ -800,12 +800,12 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } } - case CS_CODE_LOOKUP | CS_RET_STRING: - args[numargs] = cs_get_lookup_id(cs, op)->get_value(); + case BC_INST_LOOKUP | BC_RET_STRING: + args[numargs] = get_lookup_id(cs, op)->get_value(); args[numargs++].force_str(); continue; - case CS_CODE_LOOKUP_ARG | CS_RET_STRING: { - cs_alias *a = cs_get_lookuparg_id(cs, op); + case BC_INST_LOOKUP_ARG | BC_RET_STRING: { + alias *a = get_lookuparg_id(cs, op); if (!a) { args[numargs++].set_str(""); } else { @@ -814,26 +814,26 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { } continue; } - case CS_CODE_LOOKUP_U | CS_RET_INT: { - cs_ident *id = nullptr; - cs_value &arg = args[numargs - 1]; - switch (cs_get_lookupu_type(cs, arg, id, op)) { + case BC_INST_LOOKUP_U | BC_RET_INT: { + ident *id = nullptr; + any_value &arg = args[numargs - 1]; + switch (get_lookupu_type(cs, arg, id, op)) { case ID_ALIAS: arg.set_int( - static_cast(id)->get_value().get_int() + static_cast(id)->get_value().get_int() ); continue; case ID_SVAR: arg.set_int(parse_int( - static_cast(id)->get_value() + static_cast(id)->get_value() )); continue; case ID_IVAR: - arg.set_int(static_cast(id)->get_value()); + arg.set_int(static_cast(id)->get_value()); continue; case ID_FVAR: arg.set_int( - cs_int(static_cast(id)->get_value()) + integer_type(static_cast(id)->get_value()) ); continue; case ID_UNKNOWN: @@ -843,13 +843,13 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } } - case CS_CODE_LOOKUP | CS_RET_INT: + case BC_INST_LOOKUP | BC_RET_INT: args[numargs++].set_int( - cs_get_lookup_id(cs, op)->get_value().get_int() + get_lookup_id(cs, op)->get_value().get_int() ); continue; - case CS_CODE_LOOKUP_ARG | CS_RET_INT: { - cs_alias *a = cs_get_lookuparg_id(cs, op); + case BC_INST_LOOKUP_ARG | BC_RET_INT: { + alias *a = get_lookuparg_id(cs, op); if (!a) { args[numargs++].set_int(0); } else { @@ -857,67 +857,67 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { } continue; } - case CS_CODE_LOOKUP_U | CS_RET_FLOAT: { - cs_ident *id = nullptr; - cs_value &arg = args[numargs - 1]; - switch (cs_get_lookupu_type(cs, arg, id, op)) { + case BC_INST_LOOKUP_U | BC_RET_FLOAT: { + ident *id = nullptr; + any_value &arg = args[numargs - 1]; + switch (get_lookupu_type(cs, arg, id, op)) { case ID_ALIAS: arg.set_float( - static_cast(id)->get_value().get_float() + static_cast(id)->get_value().get_float() ); continue; case ID_SVAR: arg.set_float(parse_float( - static_cast(id)->get_value() + static_cast(id)->get_value() )); continue; case ID_IVAR: - arg.set_float(cs_float( - static_cast(id)->get_value() + arg.set_float(float_type( + static_cast(id)->get_value() )); continue; case ID_FVAR: arg.set_float( - static_cast(id)->get_value() + static_cast(id)->get_value() ); continue; case ID_UNKNOWN: - arg.set_float(cs_float(0)); + arg.set_float(float_type(0)); continue; default: continue; } } - case CS_CODE_LOOKUP | CS_RET_FLOAT: + case BC_INST_LOOKUP | BC_RET_FLOAT: args[numargs++].set_float( - cs_get_lookup_id(cs, op)->get_value().get_float() + get_lookup_id(cs, op)->get_value().get_float() ); continue; - case CS_CODE_LOOKUP_ARG | CS_RET_FLOAT: { - cs_alias *a = cs_get_lookuparg_id(cs, op); + case BC_INST_LOOKUP_ARG | BC_RET_FLOAT: { + alias *a = get_lookuparg_id(cs, op); if (!a) { - args[numargs++].set_float(cs_float(0)); + args[numargs++].set_float(float_type(0)); } else { args[numargs++].set_float(a->get_value().get_float()); } continue; } - case CS_CODE_LOOKUP_U | CS_RET_NULL: { - cs_ident *id = nullptr; - cs_value &arg = args[numargs - 1]; - switch (cs_get_lookupu_type(cs, arg, id, op)) { + case BC_INST_LOOKUP_U | BC_RET_NULL: { + ident *id = nullptr; + any_value &arg = args[numargs - 1]; + switch (get_lookupu_type(cs, arg, id, op)) { case ID_ALIAS: - static_cast(id)->get_value().get_val(arg); + static_cast(id)->get_value().get_val(arg); continue; case ID_SVAR: - arg.set_str(static_cast(id)->get_value()); + arg.set_str(static_cast(id)->get_value()); continue; case ID_IVAR: - arg.set_int(static_cast(id)->get_value()); + arg.set_int(static_cast(id)->get_value()); continue; case ID_FVAR: arg.set_float( - static_cast(id)->get_value() + static_cast(id)->get_value() ); continue; case ID_UNKNOWN: @@ -927,11 +927,11 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } } - case CS_CODE_LOOKUP | CS_RET_NULL: - cs_get_lookup_id(cs, op)->get_value().get_val(args[numargs++]); + case BC_INST_LOOKUP | BC_RET_NULL: + get_lookup_id(cs, op)->get_value().get_val(args[numargs++]); continue; - case CS_CODE_LOOKUP_ARG | CS_RET_NULL: { - cs_alias *a = cs_get_lookuparg_id(cs, op); + case BC_INST_LOOKUP_ARG | BC_RET_NULL: { + alias *a = get_lookuparg_id(cs, op); if (!a) { args[numargs++].set_none(); } else { @@ -940,23 +940,23 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } - case CS_CODE_LOOKUP_MU | CS_RET_STRING: { - cs_ident *id = nullptr; - cs_value &arg = args[numargs - 1]; - switch (cs_get_lookupu_type(cs, arg, id, op)) { + case BC_INST_LOOKUP_MU | BC_RET_STRING: { + ident *id = nullptr; + any_value &arg = args[numargs - 1]; + switch (get_lookupu_type(cs, arg, id, op)) { case ID_ALIAS: - arg = static_cast(id)->get_value(); + arg = static_cast(id)->get_value(); arg.force_str(); continue; case ID_SVAR: - arg.set_str(static_cast(id)->get_value()); + arg.set_str(static_cast(id)->get_value()); continue; case ID_IVAR: - arg.set_int(static_cast(id)->get_value()); + arg.set_int(static_cast(id)->get_value()); arg.force_str(); continue; case ID_FVAR: - arg.set_float(static_cast(id)->get_value()); + arg.set_float(static_cast(id)->get_value()); arg.force_str(); continue; case ID_UNKNOWN: @@ -966,12 +966,12 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } } - case CS_CODE_LOOKUP_M | CS_RET_STRING: - args[numargs] = cs_get_lookup_id(cs, op)->get_value(); + case BC_INST_LOOKUP_M | BC_RET_STRING: + args[numargs] = get_lookup_id(cs, op)->get_value(); args[numargs++].force_str(); continue; - case CS_CODE_LOOKUP_MARG | CS_RET_STRING: { - cs_alias *a = cs_get_lookuparg_id(cs, op); + case BC_INST_LOOKUP_MARG | BC_RET_STRING: { + alias *a = get_lookuparg_id(cs, op); if (!a) { args[numargs++].set_str(""); } else { @@ -980,21 +980,21 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { } continue; } - case CS_CODE_LOOKUP_MU | CS_RET_NULL: { - cs_ident *id = nullptr; - cs_value &arg = args[numargs - 1]; - switch (cs_get_lookupu_type(cs, arg, id, op)) { + case BC_INST_LOOKUP_MU | BC_RET_NULL: { + ident *id = nullptr; + any_value &arg = args[numargs - 1]; + switch (get_lookupu_type(cs, arg, id, op)) { case ID_ALIAS: - static_cast(id)->get_cval(arg); + static_cast(id)->get_cval(arg); continue; case ID_SVAR: - arg.set_str(static_cast(id)->get_value()); + arg.set_str(static_cast(id)->get_value()); continue; case ID_IVAR: - arg.set_int(static_cast(id)->get_value()); + arg.set_int(static_cast(id)->get_value()); continue; case ID_FVAR: - arg.set_float(static_cast(id)->get_value()); + arg.set_float(static_cast(id)->get_value()); continue; case ID_UNKNOWN: arg.set_none(); @@ -1003,11 +1003,11 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } } - case CS_CODE_LOOKUP_M | CS_RET_NULL: - cs_get_lookup_id(cs, op)->get_cval(args[numargs++]); + case BC_INST_LOOKUP_M | BC_RET_NULL: + get_lookup_id(cs, op)->get_cval(args[numargs++]); continue; - case CS_CODE_LOOKUP_MARG | CS_RET_NULL: { - cs_alias *a = cs_get_lookuparg_id(cs, op); + case BC_INST_LOOKUP_MARG | BC_RET_NULL: { + alias *a = get_lookuparg_id(cs, op); if (!a) { args[numargs++].set_none(); } else { @@ -1016,284 +1016,284 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { continue; } - case CS_CODE_SVAR | CS_RET_STRING: - case CS_CODE_SVAR | CS_RET_NULL: - args[numargs++].set_str(static_cast( + case BC_INST_SVAR | BC_RET_STRING: + case BC_INST_SVAR | BC_RET_NULL: + args[numargs++].set_str(static_cast( cs.p_state->identmap[op >> 8] )->get_value()); continue; - case CS_CODE_SVAR | CS_RET_INT: - args[numargs++].set_int(parse_int(static_cast( + case BC_INST_SVAR | BC_RET_INT: + args[numargs++].set_int(parse_int(static_cast( cs.p_state->identmap[op >> 8] )->get_value())); continue; - case CS_CODE_SVAR | CS_RET_FLOAT: - args[numargs++].set_float(parse_float(static_cast( + case BC_INST_SVAR | BC_RET_FLOAT: + args[numargs++].set_float(parse_float(static_cast( cs.p_state->identmap[op >> 8] )->get_value())); continue; - case CS_CODE_SVAR1: + case BC_INST_SVAR1: cs.set_var_str_checked( - static_cast(cs.p_state->identmap[op >> 8]), + static_cast(cs.p_state->identmap[op >> 8]), args[--numargs].get_str() ); continue; - case CS_CODE_IVAR | CS_RET_INT: - case CS_CODE_IVAR | CS_RET_NULL: - args[numargs++].set_int(static_cast( + case BC_INST_IVAR | BC_RET_INT: + case BC_INST_IVAR | BC_RET_NULL: + args[numargs++].set_int(static_cast( cs.p_state->identmap[op >> 8] )->get_value()); continue; - case CS_CODE_IVAR | CS_RET_STRING: - args[numargs].set_int(static_cast( + case BC_INST_IVAR | BC_RET_STRING: + args[numargs].set_int(static_cast( cs.p_state->identmap[op >> 8] )->get_value()); args[numargs++].force_str(); continue; - case CS_CODE_IVAR | CS_RET_FLOAT: - args[numargs++].set_float(cs_float(static_cast( + case BC_INST_IVAR | BC_RET_FLOAT: + args[numargs++].set_float(float_type(static_cast( cs.p_state->identmap[op >> 8] )->get_value())); continue; - case CS_CODE_IVAR1: + case BC_INST_IVAR1: cs.set_var_int_checked( - static_cast(cs.p_state->identmap[op >> 8]), + static_cast(cs.p_state->identmap[op >> 8]), args[--numargs].get_int() ); continue; - case CS_CODE_IVAR2: + case BC_INST_IVAR2: numargs -= 2; cs.set_var_int_checked( - static_cast(cs.p_state->identmap[op >> 8]), + static_cast(cs.p_state->identmap[op >> 8]), (args[numargs].get_int() << 16) | (args[numargs + 1].get_int() << 8) ); continue; - case CS_CODE_IVAR3: + case BC_INST_IVAR3: numargs -= 3; cs.set_var_int_checked( - static_cast(cs.p_state->identmap[op >> 8]), + static_cast(cs.p_state->identmap[op >> 8]), (args[numargs].get_int() << 16) | (args[numargs + 1].get_int() << 8) | (args[numargs + 2].get_int())); continue; - case CS_CODE_FVAR | CS_RET_FLOAT: - case CS_CODE_FVAR | CS_RET_NULL: - args[numargs++].set_float(static_cast( + case BC_INST_FVAR | BC_RET_FLOAT: + case BC_INST_FVAR | BC_RET_NULL: + args[numargs++].set_float(static_cast( cs.p_state->identmap[op >> 8] )->get_value()); continue; - case CS_CODE_FVAR | CS_RET_STRING: - args[numargs].set_int(static_cast( + case BC_INST_FVAR | BC_RET_STRING: + args[numargs].set_int(static_cast( cs.p_state->identmap[op >> 8] )->get_value()); args[numargs++].force_str(); continue; - case CS_CODE_FVAR | CS_RET_INT: - args[numargs++].set_int(int(static_cast( + case BC_INST_FVAR | BC_RET_INT: + args[numargs++].set_int(int(static_cast( cs.p_state->identmap[op >> 8] )->get_value())); continue; - case CS_CODE_FVAR1: + case BC_INST_FVAR1: cs.set_var_float_checked( - static_cast(cs.p_state->identmap[op >> 8]), + static_cast(cs.p_state->identmap[op >> 8]), args[--numargs].get_float() ); continue; - case CS_CODE_COM | CS_RET_NULL: - case CS_CODE_COM | CS_RET_STRING: - case CS_CODE_COM | CS_RET_FLOAT: - case CS_CODE_COM | CS_RET_INT: { - cs_command_impl *id = static_cast( + case BC_INST_COM | BC_RET_NULL: + case BC_INST_COM | BC_RET_STRING: + case BC_INST_COM | BC_RET_FLOAT: + case BC_INST_COM | BC_RET_INT: { + command_impl *id = static_cast( cs.p_state->identmap[op >> 8] ); int offset = numargs - id->get_num_args(); result.force_none(); - id->call(cs, std::span{ + id->call(cs, std::span{ &args[0] + offset, std::size_t(id->get_num_args()) }, result); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); numargs = offset; continue; } - case CS_CODE_COM_V | CS_RET_NULL: - case CS_CODE_COM_V | CS_RET_STRING: - case CS_CODE_COM_V | CS_RET_FLOAT: - case CS_CODE_COM_V | CS_RET_INT: { - cs_command_impl *id = static_cast( + case BC_INST_COM_V | BC_RET_NULL: + case BC_INST_COM_V | BC_RET_STRING: + case BC_INST_COM_V | BC_RET_FLOAT: + case BC_INST_COM_V | BC_RET_INT: { + command_impl *id = static_cast( cs.p_state->identmap[op >> 13] ); std::size_t callargs = (op >> 8) & 0x1F, offset = numargs - callargs; result.force_none(); id->call(cs, std::span{&args[offset], callargs}, result); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); numargs = offset; continue; } - case CS_CODE_COM_C | CS_RET_NULL: - case CS_CODE_COM_C | CS_RET_STRING: - case CS_CODE_COM_C | CS_RET_FLOAT: - case CS_CODE_COM_C | CS_RET_INT: { - cs_command_impl *id = static_cast( + case BC_INST_COM_C | BC_RET_NULL: + case BC_INST_COM_C | BC_RET_STRING: + case BC_INST_COM_C | BC_RET_FLOAT: + case BC_INST_COM_C | BC_RET_INT: { + command_impl *id = static_cast( cs.p_state->identmap[op >> 13] ); std::size_t callargs = (op >> 8) & 0x1F, offset = numargs - callargs; result.force_none(); { - cs_value tv{cs}; - tv.set_str(cs_concat_values(cs, std::span{ + any_value tv{cs}; + tv.set_str(concat_values(cs, std::span{ &args[offset], callargs }, " ")); - id->call(cs, std::span{&tv, 1}, result); + id->call(cs, std::span{&tv, 1}, result); } - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); numargs = offset; continue; } - case CS_CODE_CONC | CS_RET_NULL: - case CS_CODE_CONC | CS_RET_STRING: - case CS_CODE_CONC | CS_RET_FLOAT: - case CS_CODE_CONC | CS_RET_INT: - case CS_CODE_CONC_W | CS_RET_NULL: - case CS_CODE_CONC_W | CS_RET_STRING: - case CS_CODE_CONC_W | CS_RET_FLOAT: - case CS_CODE_CONC_W | CS_RET_INT: { + case BC_INST_CONC | BC_RET_NULL: + case BC_INST_CONC | BC_RET_STRING: + case BC_INST_CONC | BC_RET_FLOAT: + case BC_INST_CONC | BC_RET_INT: + case BC_INST_CONC_W | BC_RET_NULL: + case BC_INST_CONC_W | BC_RET_STRING: + case BC_INST_CONC_W | BC_RET_FLOAT: + case BC_INST_CONC_W | BC_RET_INT: { std::size_t numconc = op >> 8; - auto buf = cs_concat_values( + auto buf = concat_values( cs, std::span{&args[numargs - numconc], numconc}, - ((op & CS_CODE_OP_MASK) == CS_CODE_CONC) ? " " : "" + ((op & BC_INST_OP_MASK) == BC_INST_CONC) ? " " : "" ); numargs = numargs - numconc; args[numargs].set_str(buf); - force_arg(args[numargs], op & CS_CODE_RET_MASK); + force_arg(args[numargs], op & BC_INST_RET_MASK); numargs++; continue; } - case CS_CODE_CONC_M | CS_RET_NULL: - case CS_CODE_CONC_M | CS_RET_STRING: - case CS_CODE_CONC_M | CS_RET_FLOAT: - case CS_CODE_CONC_M | CS_RET_INT: { + case BC_INST_CONC_M | BC_RET_NULL: + case BC_INST_CONC_M | BC_RET_STRING: + case BC_INST_CONC_M | BC_RET_FLOAT: + case BC_INST_CONC_M | BC_RET_INT: { std::size_t numconc = op >> 8; - auto buf = cs_concat_values( + auto buf = concat_values( cs, std::span{&args[numargs - numconc], numconc} ); numargs = numargs - numconc; result.set_str(buf); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; } - case CS_CODE_ALIAS: - static_cast( + case BC_INST_ALIAS: + static_cast( cs.p_state->identmap[op >> 8] )->set_alias(cs, args[--numargs]); continue; - case CS_CODE_ALIAS_ARG: - static_cast( + case BC_INST_ALIAS_ARG: + static_cast( cs.p_state->identmap[op >> 8] )->set_arg(cs, args[--numargs]); continue; - case CS_CODE_ALIAS_U: + case BC_INST_ALIAS_U: numargs -= 2; cs.set_alias( args[numargs].get_str(), std::move(args[numargs + 1]) ); continue; - case CS_CODE_CALL | CS_RET_NULL: - case CS_CODE_CALL | CS_RET_STRING: - case CS_CODE_CALL | CS_RET_FLOAT: - case CS_CODE_CALL | CS_RET_INT: { + case BC_INST_CALL | BC_RET_NULL: + case BC_INST_CALL | BC_RET_STRING: + case BC_INST_CALL | BC_RET_FLOAT: + case BC_INST_CALL | BC_RET_INT: { result.force_none(); - cs_ident *id = cs.p_state->identmap[op >> 13]; + ident *id = cs.p_state->identmap[op >> 13]; int callargs = (op >> 8) & 0x1F, offset = numargs - callargs; - if (id->get_flags() & CS_IDF_UNKNOWN) { - force_arg(result, op & CS_CODE_RET_MASK); - throw cs_error( + if (id->get_flags() & IDENT_FLAG_UNKNOWN) { + force_arg(result, op & BC_INST_RET_MASK); + throw error( cs, "unknown command: %s", id->get_name().data() ); } - cs_call_alias( - cs, static_cast(id), &args[0], result, callargs, + call_alias( + cs, static_cast(id), &args[0], result, callargs, numargs, offset, 0, op ); continue; } - case CS_CODE_CALL_ARG | CS_RET_NULL: - case CS_CODE_CALL_ARG | CS_RET_STRING: - case CS_CODE_CALL_ARG | CS_RET_FLOAT: - case CS_CODE_CALL_ARG | CS_RET_INT: { + case BC_INST_CALL_ARG | BC_RET_NULL: + case BC_INST_CALL_ARG | BC_RET_STRING: + case BC_INST_CALL_ARG | BC_RET_FLOAT: + case BC_INST_CALL_ARG | BC_RET_INT: { result.force_none(); - cs_ident *id = cs.p_state->identmap[op >> 13]; + ident *id = cs.p_state->identmap[op >> 13]; int callargs = (op >> 8) & 0x1F, offset = numargs - callargs; if (!ident_is_used_arg(id, cs)) { numargs = offset; - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; } - cs_call_alias( - cs, static_cast(id), &args[0], result, callargs, + call_alias( + cs, static_cast(id), &args[0], result, callargs, numargs, offset, 0, op ); continue; } - case CS_CODE_CALL_U | CS_RET_NULL: - case CS_CODE_CALL_U | CS_RET_STRING: - case CS_CODE_CALL_U | CS_RET_FLOAT: - case CS_CODE_CALL_U | CS_RET_INT: { + case BC_INST_CALL_U | BC_RET_NULL: + case BC_INST_CALL_U | BC_RET_STRING: + case BC_INST_CALL_U | BC_RET_FLOAT: + case BC_INST_CALL_U | BC_RET_INT: { int callargs = op >> 8, offset = numargs - callargs; - cs_value &idarg = args[offset - 1]; - if (idarg.get_type() != cs_value_type::STRING) { + any_value &idarg = args[offset - 1]; + if (idarg.get_type() != value_type::STRING) { litval: result = std::move(idarg); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); numargs = offset - 1; continue; } auto idn = idarg.get_str(); - cs_ident *id = cs.get_ident(idn); + ident *id = cs.get_ident(idn); if (!id) { noid: if (!is_valid_name(idn)) { goto litval; } result.force_none(); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); std::string_view ids{idn}; - throw cs_error( + throw error( cs, "unknown command: %s", ids.data() ); } result.force_none(); switch (id->get_raw_type()) { default: - if (!cs_ident_has_cb(id)) { + if (!ident_has_cb(id)) { numargs = offset - 1; - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; } /* fallthrough */ case ID_COMMAND: callcommand( - cs, static_cast(id), + cs, static_cast(id), &args[offset], result, callargs ); - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); numargs = offset - 1; continue; case ID_LOCAL: { - cs_valarray locals{cs}; + valarray locals{cs}; for (size_t j = 0; j < size_t(callargs); ++j) { - cs_push_alias(cs, cs.force_ident( + push_alias(cs, cs.force_ident( args[offset + j] ), locals[j]); } @@ -1301,61 +1301,61 @@ noid: code = runcode(cs, code, result); }, [&]() { for (size_t j = 0; j < size_t(callargs); ++j) { - cs_pop_alias(args[offset + j].get_ident()); + pop_alias(args[offset + j].get_ident()); } }); return code; } case ID_IVAR: if (callargs <= 0) { - cs.print_var(*static_cast(id)); + cs.print_var(*static_cast(id)); } else { cs.set_var_int_checked( - static_cast(id), + static_cast(id), std::span{&args[offset], std::size_t(callargs)} ); } numargs = offset - 1; - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; case ID_FVAR: if (callargs <= 0) { - cs.print_var(*static_cast(id)); + cs.print_var(*static_cast(id)); } else { cs.set_var_float_checked( - static_cast(id), + static_cast(id), args[offset].force_float() ); } numargs = offset - 1; - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; case ID_SVAR: if (callargs <= 0) { - cs.print_var(*static_cast(id)); + cs.print_var(*static_cast(id)); } else { cs.set_var_str_checked( - static_cast(id), + static_cast(id), args[offset].force_str() ); } numargs = offset - 1; - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; case ID_ALIAS: { - cs_alias *a = static_cast(id); + alias *a = static_cast(id); if ( (a->get_index() < MaxArguments) && !ident_is_used_arg(a, cs) ) { numargs = offset - 1; - force_arg(result, op & CS_CODE_RET_MASK); + force_arg(result, op & BC_INST_RET_MASK); continue; } - if (a->get_value().get_type() == cs_value_type::NONE) { + if (a->get_value().get_type() == value_type::NONE) { goto noid; } - cs_call_alias( + call_alias( cs, a, &args[0], result, callargs, numargs, offset, 1, op ); @@ -1368,18 +1368,18 @@ noid: return code; } -void cs_state::run(cs_bcode *code, cs_value &ret) { +void state::run(bcode *code, any_value &ret) { runcode(*this, reinterpret_cast(code), ret); } -static void cs_run( - cs_state &cs, std::string_view file, std::string_view code, - cs_value &ret +static void do_run( + state &cs, std::string_view file, std::string_view code, + any_value &ret ) { - cs_gen_state gs(cs); + codegen_state gs(cs); gs.src_name = file; gs.code.reserve(64); - gs.gen_main(code, CS_VAL_ANY); + gs.gen_main(code, VAL_ANY); gs.done(); uint32_t *cbuf = bcode_alloc(gs.cs, gs.code.size()); memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(uint32_t)); @@ -1388,82 +1388,82 @@ static void cs_run( bcode_decr(cbuf); } -void cs_state::run(std::string_view code, cs_value &ret) { - cs_run(*this, std::string_view{}, code, ret); +void state::run(std::string_view code, any_value &ret) { + do_run(*this, std::string_view{}, code, ret); } -void cs_state::run( - std::string_view code, cs_value &ret, std::string_view source +void state::run( + std::string_view code, any_value &ret, std::string_view source ) { - cs_run(*this, source, code, ret); + do_run(*this, source, code, ret); } -void cs_state::run(cs_ident *id, std::span args, cs_value &ret) { +void state::run(ident *id, std::span args, any_value &ret) { int nargs = int(args.size()); ret.set_none(); RunDepthRef level{*this}; /* incr and decr on scope exit */ if (id) { switch (id->get_type()) { default: - if (!cs_ident_has_cb(id)) { + if (!ident_has_cb(id)) { break; } /* fallthrough */ - case cs_ident_type::COMMAND: - if (nargs < static_cast(id)->get_num_args()) { - cs_valarray buf{*this}; + case ident_type::COMMAND: + if (nargs < static_cast(id)->get_num_args()) { + valarray buf{*this}; for (std::size_t i = 0; i < args.size(); ++i) { buf[i] = args[i]; } callcommand( - *this, static_cast(id), &buf[0], ret, + *this, static_cast(id), &buf[0], ret, nargs, false ); } else { callcommand( - *this, static_cast(id), &args[0], + *this, static_cast(id), &args[0], ret, nargs, false ); } nargs = 0; break; - case cs_ident_type::IVAR: + case ident_type::IVAR: if (args.empty()) { - print_var(*static_cast(id)); + print_var(*static_cast(id)); } else { - set_var_int_checked(static_cast(id), args); + set_var_int_checked(static_cast(id), args); } break; - case cs_ident_type::FVAR: + case ident_type::FVAR: if (args.empty()) { - print_var(*static_cast(id)); + print_var(*static_cast(id)); } else { set_var_float_checked( - static_cast(id), args[0].force_float() + static_cast(id), args[0].force_float() ); } break; - case cs_ident_type::SVAR: + case ident_type::SVAR: if (args.empty()) { - print_var(*static_cast(id)); + print_var(*static_cast(id)); } else { set_var_str_checked( - static_cast(id), args[0].force_str() + static_cast(id), args[0].force_str() ); } break; - case cs_ident_type::ALIAS: { - cs_alias *a = static_cast(id); + case ident_type::ALIAS: { + alias *a = static_cast(id); if ( (a->get_index() < MaxArguments) && !ident_is_used_arg(a, *this) ) { break; } - if (a->get_value().get_type() == cs_value_type::NONE) { + if (a->get_value().get_type() == value_type::NONE) { break; } - cs_call_alias( - *this, a, &args[0], ret, nargs, nargs, 0, 0, CS_RET_NULL + call_alias( + *this, a, &args[0], ret, nargs, nargs, 0, 0, BC_RET_NULL ); break; } @@ -1471,49 +1471,49 @@ void cs_state::run(cs_ident *id, std::span args, cs_value &ret) { } } -cs_value cs_state::run(cs_bcode *code) { - cs_value ret{*this}; +any_value state::run(bcode *code) { + any_value ret{*this}; run(code, ret); return ret; } -cs_value cs_state::run(std::string_view code) { - cs_value ret{*this}; +any_value state::run(std::string_view code) { + any_value ret{*this}; run(code, ret); return ret; } -cs_value cs_state::run(std::string_view code, std::string_view source) { - cs_value ret{*this}; +any_value state::run(std::string_view code, std::string_view source) { + any_value ret{*this}; run(code, ret, source); return ret; } -cs_value cs_state::run(cs_ident *id, std::span args) { - cs_value ret{*this}; +any_value state::run(ident *id, std::span args) { + any_value ret{*this}; run(id, args, ret); return ret; } -cs_loop_state cs_state::run_loop(cs_bcode *code, cs_value &ret) { +loop_state state::run_loop(bcode *code, any_value &ret) { ++p_inloop; try { run(code, ret); } catch (CsBreakException) { --p_inloop; - return cs_loop_state::BREAK; + return loop_state::BREAK; } catch (CsContinueException) { --p_inloop; - return cs_loop_state::CONTINUE; + return loop_state::CONTINUE; } catch (...) { --p_inloop; throw; } - return cs_loop_state::NORMAL; + return loop_state::NORMAL; } -cs_loop_state cs_state::run_loop(cs_bcode *code) { - cs_value ret{*this}; +loop_state state::run_loop(bcode *code) { + any_value ret{*this}; return run_loop(code, ret); } diff --git a/src/cs_vm.hh b/src/cs_vm.hh index 401cb9c..541fd42 100644 --- a/src/cs_vm.hh +++ b/src/cs_vm.hh @@ -1,5 +1,5 @@ -#ifndef LIBCUBESCRIPT_CS_VM_HH -#define LIBCUBESCRIPT_CS_VM_HH +#ifndef LIBCUBESCRIPT_VM_HH +#define LIBCUBESCRIPT_VM_HH #include "cubescript/cubescript.hh" @@ -21,13 +21,13 @@ static constexpr int DummyIdx = MaxArguments; static constexpr int NumargsIdx = MaxArguments + 1; static constexpr int DbgaliasIdx = MaxArguments + 2; -static const int cs_valtypet[] = { - CS_VAL_NULL, CS_VAL_INT, CS_VAL_FLOAT, CS_VAL_STRING, - CS_VAL_CODE, CS_VAL_IDENT +static const int valtypet[] = { + VAL_NULL, VAL_INT, VAL_FLOAT, VAL_STRING, + VAL_CODE, VAL_IDENT }; -static inline int cs_vtype_to_int(cs_value_type v) { - return cs_valtypet[int(v)]; +static inline int vtype_to_int(value_type v) { + return valtypet[int(v)]; } struct CsBreakException { @@ -40,24 +40,24 @@ template constexpr size_t CsTypeStorageSize = (sizeof(T) - 1) / sizeof(uint32_t) + 1; -struct cs_gen_state { - cs_state &cs; - cs_gen_state *prevps; +struct codegen_state { + state &cs; + codegen_state *prevps; bool parsing = true; - cs_valbuf code; + valbuf code; char const *source, *send; size_t current_line; std::string_view src_name; - cs_gen_state() = delete; - cs_gen_state(cs_state &csr): + codegen_state() = delete; + codegen_state(state &csr): cs{csr}, prevps{csr.p_pstate}, code{cs}, source{}, send{}, current_line{1}, src_name{} { csr.p_pstate = this; } - ~cs_gen_state() { + ~codegen_state() { done(); } @@ -70,13 +70,13 @@ struct cs_gen_state { } std::string_view get_str(); - cs_charbuf get_str_dup(); + charbuf get_str_dup(); std::string_view get_word(); void gen_str(std::string_view word) { if (word.size() <= 3) { - uint32_t op = CS_CODE_VAL_INT | CS_RET_STRING; + uint32_t op = BC_INST_VAL_INT | BC_RET_STRING; for (size_t i = 0; i < word.size(); ++i) { op |= uint32_t( static_cast(word[i]) @@ -85,7 +85,7 @@ struct cs_gen_state { code.push_back(op); return; } - code.push_back(CS_CODE_VAL | CS_RET_STRING | (word.size() << 8)); + code.push_back(BC_INST_VAL | BC_RET_STRING | (word.size() << 8)); auto it = reinterpret_cast(word.data()); code.append(it, it + (word.size() / sizeof(uint32_t))); size_t esz = word.size() % sizeof(uint32_t); @@ -99,50 +99,50 @@ struct cs_gen_state { } void gen_str() { - code.push_back(CS_CODE_VAL_INT | CS_RET_STRING); + code.push_back(BC_INST_VAL_INT | BC_RET_STRING); } void gen_null() { - code.push_back(CS_CODE_VAL_INT | CS_RET_NULL); + code.push_back(BC_INST_VAL_INT | BC_RET_NULL); } - void gen_int(cs_int i = 0) { + void gen_int(integer_type i = 0) { if (i >= -0x800000 && i <= 0x7FFFFF) { - code.push_back(CS_CODE_VAL_INT | CS_RET_INT | (i << 8)); + code.push_back(BC_INST_VAL_INT | BC_RET_INT | (i << 8)); } else { union { - cs_int i; - uint32_t u[CsTypeStorageSize]; + integer_type i; + uint32_t u[CsTypeStorageSize]; } c; c.i = i; - code.push_back(CS_CODE_VAL | CS_RET_INT); - code.append(c.u, c.u + CsTypeStorageSize); + code.push_back(BC_INST_VAL | BC_RET_INT); + code.append(c.u, c.u + CsTypeStorageSize); } } void gen_int(std::string_view word); - void gen_float(cs_float f = 0.0f) { - if (cs_int(f) == f && f >= -0x800000 && f <= 0x7FFFFF) { - code.push_back(CS_CODE_VAL_INT | CS_RET_FLOAT | (cs_int(f) << 8)); + void gen_float(float_type f = 0.0f) { + if (integer_type(f) == f && f >= -0x800000 && f <= 0x7FFFFF) { + code.push_back(BC_INST_VAL_INT | BC_RET_FLOAT | (integer_type(f) << 8)); } else { union { - cs_float f; - uint32_t u[CsTypeStorageSize]; + float_type f; + uint32_t u[CsTypeStorageSize]; } c; c.f = f; - code.push_back(CS_CODE_VAL | CS_RET_FLOAT); - code.append(c.u, c.u + CsTypeStorageSize); + code.push_back(BC_INST_VAL | BC_RET_FLOAT); + code.append(c.u, c.u + CsTypeStorageSize); } } void gen_float(std::string_view word); - void gen_ident(cs_ident *id) { + void gen_ident(ident *id) { code.push_back( ((id->get_index() < MaxArguments) - ? CS_CODE_IDENT_ARG - : CS_CODE_IDENT + ? BC_INST_IDENT_ARG + : BC_INST_IDENT ) | (id->get_index() << 8) ); } @@ -160,7 +160,7 @@ struct cs_gen_state { int line = 0 ); - void gen_main(std::string_view s, int ret_type = CS_VAL_ANY); + void gen_main(std::string_view s, int ret_type = VAL_ANY); void next_char() { if (source == send) { @@ -187,26 +187,23 @@ struct cs_gen_state { void skip_comments(); }; -void bcode_ref(uint32_t *code); -void bcode_unref(uint32_t *code); - template -static void cs_do_args(cs_state &cs, F body) { +static void call_with_args(state &cs, F body) { if (!cs.p_callstack) { body(); return; } - cs_valarray argstack{cs}; + valarray argstack{cs}; int argmask1 = cs.p_callstack->usedargs; for (int i = 0; argmask1; argmask1 >>= 1, ++i) { if (argmask1 & 1) { - static_cast(cs.p_state->identmap[i])->undo_arg( + static_cast(cs.p_state->identmap[i])->undo_arg( argstack[i] ); } } - cs_ident_link *prevstack = cs.p_callstack->next; - cs_ident_link aliaslink = { + ident_link *prevstack = cs.p_callstack->next; + ident_link aliaslink = { cs.p_callstack->id, cs.p_callstack, prevstack ? prevstack->usedargs : ((1 << MaxArguments) - 1), prevstack ? prevstack->argstack : nullptr @@ -220,7 +217,7 @@ static void cs_do_args(cs_state &cs, F body) { int argmask2 = cs.p_callstack->usedargs; for (int i = 0; argmask2; argmask2 >>= 1, ++i) { if (argmask2 & 1) { - static_cast(cs.p_state->identmap[i])->redo_arg( + static_cast(cs.p_state->identmap[i])->redo_arg( argstack[i] ); } @@ -230,4 +227,4 @@ static void cs_do_args(cs_state &cs, F body) { } /* namespace cscript */ -#endif /* LIBCUBESCRIPT_CS_VM_HH */ +#endif /* LIBCUBESCRIPT_VM_HH */ diff --git a/src/lib_base.cc b/src/lib_base.cc index e602c10..c33eb56 100644 --- a/src/lib_base.cc +++ b/src/lib_base.cc @@ -6,22 +6,22 @@ namespace cscript { -static inline void cs_do_loop( - cs_state &cs, cs_ident &id, cs_int offset, cs_int n, cs_int step, - cs_bcode *cond, cs_bcode *body +static inline void do_loop( + state &cs, ident &id, integer_type offset, integer_type n, integer_type step, + bcode *cond, bcode *body ) { - cs_stacked_value idv{cs, &id}; + stacked_value idv{cs, &id}; if (n <= 0 || !idv.has_alias()) { return; } - for (cs_int i = 0; i < n; ++i) { + for (integer_type i = 0; i < n; ++i) { idv.set_int(offset + i * step); idv.push(); if (cond && !cs.run(cond).get_bool()) { break; } switch (cs.run_loop(body)) { - case cs_loop_state::BREAK: + case loop_state::BREAK: goto end; default: /* continue and normal */ break; @@ -31,23 +31,23 @@ end: return; } -static inline void cs_loop_conc( - cs_state &cs, cs_value &res, cs_ident &id, cs_int offset, cs_int n, - cs_int step, cs_bcode *body, bool space +static inline void do_loop_conc( + state &cs, any_value &res, ident &id, integer_type offset, integer_type n, + integer_type step, bcode *body, bool space ) { - cs_stacked_value idv{cs, &id}; + stacked_value idv{cs, &id}; if (n <= 0 || !idv.has_alias()) { return; } - cs_charbuf s{cs}; - for (cs_int i = 0; i < n; ++i) { + charbuf s{cs}; + for (integer_type i = 0; i < n; ++i) { idv.set_int(offset + i * step); idv.push(); - cs_value v{cs}; + any_value v{cs}; switch (cs.run_loop(body, v)) { - case cs_loop_state::BREAK: + case loop_state::BREAK: goto end; - case cs_loop_state::CONTINUE: + case loop_state::CONTINUE: continue; default: break; @@ -61,34 +61,34 @@ end: res.set_str(s.str()); } -void cs_init_lib_base(cs_state &gcs) { +void init_lib_base(state &gcs) { gcs.new_command("error", "s", [](auto &cs, auto args, auto &) { - throw cs_error(cs, args[0].get_str()); + throw error(cs, args[0].get_str()); }); gcs.new_command("pcall", "err", [](auto &cs, auto args, auto &ret) { - cs_alias *cret = args[1].get_ident()->get_alias(), + alias *cret = args[1].get_ident()->get_alias(), *css = args[2].get_ident()->get_alias(); if (!cret || !css) { ret.set_int(0); return; } - cs_value result{cs}, tback{cs}; + any_value result{cs}, tback{cs}; bool rc = true; try { cs.run(args[0].get_code(), result); - } catch (cs_error const &e) { + } catch (error const &e) { result.set_str(e.what()); if (e.get_stack().get()) { - cs_charbuf buf{cs}; - cs_print_stack(std::back_inserter(buf), e.get_stack()); + charbuf buf{cs}; + print_stack(std::back_inserter(buf), e.get_stack()); tback.set_str(buf.str()); } rc = false; } ret.set_int(rc); - static_cast(cret)->set_alias(cs, result); - static_cast(css)->set_alias(cs, tback); + static_cast(cret)->set_alias(cs, result); + static_cast(css)->set_alias(cs, tback); }); gcs.new_command("?", "ttt", [](auto &, auto args, auto &res) { @@ -114,10 +114,10 @@ void cs_init_lib_base(cs_state &gcs) { }); gcs.new_command("case", "ite2V", [](auto &cs, auto args, auto &res) { - cs_int val = args[0].get_int(); + integer_type val = args[0].get_int(); for (size_t i = 1; (i + 1) < args.size(); i += 2) { if ( - (args[i].get_type() == cs_value_type::NONE) || + (args[i].get_type() == value_type::NONE) || (args[i].get_int() == val) ) { cs.run(args[i + 1].get_code(), res); @@ -127,10 +127,10 @@ void cs_init_lib_base(cs_state &gcs) { }); gcs.new_command("casef", "fte2V", [](auto &cs, auto args, auto &res) { - cs_float val = args[0].get_float(); + float_type val = args[0].get_float(); for (size_t i = 1; (i + 1) < args.size(); i += 2) { if ( - (args[i].get_type() == cs_value_type::NONE) || + (args[i].get_type() == value_type::NONE) || (args[i].get_float() == val) ) { cs.run(args[i + 1].get_code(), res); @@ -140,10 +140,10 @@ void cs_init_lib_base(cs_state &gcs) { }); gcs.new_command("cases", "ste2V", [](auto &cs, auto args, auto &res) { - cs_strref val = args[0].get_str(); + string_ref val = args[0].get_str(); for (size_t i = 1; (i + 1) < args.size(); i += 2) { if ( - (args[i].get_type() == cs_value_type::NONE) || + (args[i].get_type() == value_type::NONE) || (args[i].get_str() == val) ) { cs.run(args[i + 1].get_code(), res); @@ -153,7 +153,7 @@ void cs_init_lib_base(cs_state &gcs) { }); gcs.new_command("pushif", "rte", [](auto &cs, auto args, auto &res) { - cs_stacked_value idv{cs, args[0].get_ident()}; + stacked_value idv{cs, args[0].get_ident()}; if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) { return; } @@ -165,66 +165,66 @@ void cs_init_lib_base(cs_state &gcs) { }); gcs.new_command("loop", "rie", [](auto &cs, auto args, auto &) { - cs_do_loop( + do_loop( cs, *args[0].get_ident(), 0, args[1].get_int(), 1, nullptr, args[2].get_code() ); }); gcs.new_command("loop+", "riie", [](auto &cs, auto args, auto &) { - cs_do_loop( + do_loop( cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, nullptr, args[3].get_code() ); }); gcs.new_command("loop*", "riie", [](auto &cs, auto args, auto &) { - cs_do_loop( + do_loop( cs, *args[0].get_ident(), 0, args[1].get_int(), args[2].get_int(), nullptr, args[3].get_code() ); }); gcs.new_command("loop+*", "riiie", [](auto &cs, auto args, auto &) { - cs_do_loop( + do_loop( cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), args[2].get_int(), nullptr, args[4].get_code() ); }); gcs.new_command("loopwhile", "riee", [](auto &cs, auto args, auto &) { - cs_do_loop( + do_loop( cs, *args[0].get_ident(), 0, args[1].get_int(), 1, args[2].get_code(), args[3].get_code() ); }); gcs.new_command("loopwhile+", "riiee", [](auto &cs, auto args, auto &) { - cs_do_loop( + do_loop( cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, args[3].get_code(), args[4].get_code() ); }); gcs.new_command("loopwhile*", "riiee", [](auto &cs, auto args, auto &) { - cs_do_loop( + do_loop( cs, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(), args[3].get_code(), args[4].get_code() ); }); gcs.new_command("loopwhile+*", "riiiee", [](auto &cs, auto args, auto &) { - cs_do_loop( + do_loop( cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), args[2].get_int(), args[4].get_code(), args[5].get_code() ); }); gcs.new_command("while", "ee", [](auto &cs, auto args, auto &) { - cs_bcode *cond = args[0].get_code(), *body = args[1].get_code(); + bcode *cond = args[0].get_code(), *body = args[1].get_code(); while (cs.run(cond).get_bool()) { switch (cs.run_loop(body)) { - case cs_loop_state::BREAK: + case loop_state::BREAK: goto end; default: /* continue and normal */ break; @@ -235,35 +235,35 @@ end: }); gcs.new_command("loopconcat", "rie", [](auto &cs, auto args, auto &res) { - cs_loop_conc( + do_loop_conc( cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1, args[2].get_code(), true ); }); gcs.new_command("loopconcat+", "riie", [](auto &cs, auto args, auto &res) { - cs_loop_conc( + do_loop_conc( cs, res, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, args[3].get_code(), true ); }); gcs.new_command("loopconcat*", "riie", [](auto &cs, auto args, auto &res) { - cs_loop_conc( + do_loop_conc( cs, res, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(), args[3].get_code(), true ); }); gcs.new_command("loopconcat+*", "riiie", [](auto &cs, auto args, auto &res) { - cs_loop_conc( + do_loop_conc( cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), args[2].get_int(), args[4].get_code(), true ); }); gcs.new_command("loopconcatword", "rie", [](auto &cs, auto args, auto &res) { - cs_loop_conc( + do_loop_conc( cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1, args[2].get_code(), false ); @@ -272,7 +272,7 @@ end: gcs.new_command("loopconcatword+", "riie", []( auto &cs, auto args, auto &res ) { - cs_loop_conc( + do_loop_conc( cs, res, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, args[3].get_code(), false ); @@ -281,7 +281,7 @@ end: gcs.new_command("loopconcatword*", "riie", []( auto &cs, auto args, auto &res ) { - cs_loop_conc( + do_loop_conc( cs, res, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(), args[3].get_code(), false ); @@ -290,14 +290,14 @@ end: gcs.new_command("loopconcatword+*", "riiie", []( auto &cs, auto args, auto &res ) { - cs_loop_conc( + do_loop_conc( cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), args[2].get_int(), args[4].get_code(), false ); }); gcs.new_command("push", "rte", [](auto &cs, auto args, auto &res) { - cs_stacked_value idv{cs, args[0].get_ident()}; + stacked_value idv{cs, args[0].get_ident()}; if (!idv.has_alias() || (idv.get_alias()->get_index() < MaxArguments)) { return; } diff --git a/src/lib_list.cc b/src/lib_list.cc index becbc6e..2a6ccf4 100644 --- a/src/lib_list.cc +++ b/src/lib_list.cc @@ -8,36 +8,36 @@ namespace cscript { template -struct cs_arg_val; +struct arg_val; template<> -struct cs_arg_val { - static cs_int get(cs_value &tv) { +struct arg_val { + static integer_type get(any_value &tv) { return tv.get_int(); } }; template<> -struct cs_arg_val { - static cs_float get(cs_value &tv) { +struct arg_val { + static float_type get(any_value &tv) { return tv.get_float(); } }; template<> -struct cs_arg_val { - static std::string_view get(cs_value &tv) { +struct arg_val { + static std::string_view get(any_value &tv) { return tv.get_str(); } }; template -static inline void cs_list_find( - cs_state &cs, std::span args, cs_value &res, F cmp +static inline void list_find( + state &cs, std::span args, any_value &res, F cmp ) { - cs_int n = 0, skip = args[2].get_int(); - T val = cs_arg_val::get(args[1]); - for (cs_list_parser p{cs, args[0].get_str()}; p.parse(); ++n) { + integer_type n = 0, skip = args[2].get_int(); + T val = arg_val::get(args[1]); + for (list_parser p{cs, args[0].get_str()}; p.parse(); ++n) { if (cmp(p, val)) { res.set_int(n); return; @@ -54,11 +54,11 @@ notfound: } template -static inline void cs_list_assoc( - cs_state &cs, std::span args, cs_value &res, F cmp +static inline void list_assoc( + state &cs, std::span args, any_value &res, F cmp ) { - T val = cs_arg_val::get(args[1]); - for (cs_list_parser p{cs, args[0].get_str()}; p.parse();) { + T val = arg_val::get(args[1]); + for (list_parser p{cs, args[0].get_str()}; p.parse();) { if (cmp(p, val)) { if (p.parse()) { res.set_str(p.get_item()); @@ -71,27 +71,27 @@ static inline void cs_list_assoc( } } -static void cs_loop_list_conc( - cs_state &cs, cs_value &res, cs_ident *id, std::string_view list, - cs_bcode *body, bool space +static void loop_list_conc( + state &cs, any_value &res, ident *id, std::string_view list, + bcode *body, bool space ) { - cs_stacked_value idv{cs, id}; + stacked_value idv{cs, id}; if (!idv.has_alias()) { return; } - cs_charbuf r{cs}; + charbuf r{cs}; int n = 0; - for (cs_list_parser p{cs, list}; p.parse(); ++n) { + for (list_parser p{cs, list}; p.parse(); ++n) { idv.set_str(p.get_item()); idv.push(); if (n && space) { r.push_back(' '); } - cs_value v{cs}; + any_value v{cs}; switch (cs.run_loop(body, v)) { - case cs_loop_state::BREAK: + case loop_state::BREAK: goto end; - case cs_loop_state::CONTINUE: + case loop_state::CONTINUE: continue; default: break; @@ -102,11 +102,11 @@ end: res.set_str(r.str()); } -int cs_list_includes( - cs_state &cs, std::string_view list, std::string_view needle +int list_includes( + state &cs, std::string_view list, std::string_view needle ) { int offset = 0; - for (cs_list_parser p{cs, list}; p.parse();) { + for (list_parser p{cs, list}; p.parse();) { if (p.get_raw_item() == needle) { return offset; } @@ -116,20 +116,20 @@ int cs_list_includes( } template -static inline void cs_list_merge( - cs_state &cs, std::span args, cs_value &res, F cmp +static inline void list_merge( + state &cs, std::span args, any_value &res, F cmp ) { std::string_view list = args[0].get_str(); std::string_view elems = args[1].get_str(); - cs_charbuf buf{cs}; + charbuf buf{cs}; if (PushList) { buf.append(list); } if (Swap) { std::swap(list, elems); } - for (cs_list_parser p{cs, list}; p.parse();) { - if (cmp(cs_list_includes(cs, elems, p.get_raw_item()), 0)) { + for (list_parser p{cs, list}; p.parse();) { + if (cmp(list_includes(cs, elems, p.get_raw_item()), 0)) { if (!buf.empty()) { buf.push_back(' '); } @@ -139,11 +139,11 @@ static inline void cs_list_merge( res.set_str(buf.str()); } -static void cs_init_lib_list_sort(cs_state &cs); +static void init_lib_list_sort(state &cs); -void cs_init_lib_list(cs_state &gcs) { +void init_lib_list(state &gcs) { gcs.new_command("listlen", "s", [](auto &cs, auto args, auto &res) { - res.set_int(cs_int(cs_list_parser{cs, args[0].get_str()}.count())); + res.set_int(integer_type(list_parser{cs, args[0].get_str()}.count())); }); gcs.new_command("at", "si1V", [](auto &cs, auto args, auto &res) { @@ -155,10 +155,10 @@ void cs_init_lib_list(cs_state &gcs) { return; } auto str = args[0].get_str(); - cs_list_parser p{cs, str}; + list_parser p{cs, str}; for (size_t i = 1; i < args.size(); ++i) { p.set_input(str); - cs_int pos = args[i].get_int(); + integer_type pos = args[i].get_int(); for (; pos > 0; --pos) { if (!p.parse()) { break; @@ -172,15 +172,15 @@ void cs_init_lib_list(cs_state &gcs) { }); gcs.new_command("sublist", "siiN", [](auto &cs, auto args, auto &res) { - cs_int skip = args[1].get_int(), + integer_type skip = args[1].get_int(), count = args[2].get_int(), numargs = args[3].get_int(); - cs_int offset = std::max(skip, cs_int(0)), - len = (numargs >= 3) ? std::max(count, cs_int(0)) : -1; + integer_type offset = std::max(skip, integer_type(0)), + len = (numargs >= 3) ? std::max(count, integer_type(0)) : -1; - cs_list_parser p{cs, args[0].get_str()}; - for (cs_int i = 0; i < offset; ++i) { + list_parser p{cs, args[0].get_str()}; + for (integer_type i = 0; i < offset; ++i) { if (!p.parse()) break; } if (len < 0) { @@ -204,19 +204,19 @@ void cs_init_lib_list(cs_state &gcs) { }); gcs.new_command("listfind", "rse", [](auto &cs, auto args, auto &res) { - cs_stacked_value idv{cs, args[0].get_ident()}; + stacked_value idv{cs, args[0].get_ident()}; if (!idv.has_alias()) { res.set_int(-1); return; } auto body = args[2].get_code(); int n = -1; - for (cs_list_parser p{cs, args[1].get_str()}; p.parse();) { + for (list_parser p{cs, args[1].get_str()}; p.parse();) { ++n; idv.set_str(p.get_raw_item()); idv.push(); if (cs.run(body).get_bool()) { - res.set_int(cs_int(n)); + res.set_int(integer_type(n)); return; } } @@ -224,13 +224,13 @@ void cs_init_lib_list(cs_state &gcs) { }); gcs.new_command("listassoc", "rse", [](auto &cs, auto args, auto &res) { - cs_stacked_value idv{cs, args[0].get_ident()}; + stacked_value idv{cs, args[0].get_ident()}; if (!idv.has_alias()) { return; } auto body = args[2].get_code(); int n = -1; - for (cs_list_parser p{cs, args[1].get_str()}; p.parse();) { + for (list_parser p{cs, args[1].get_str()}; p.parse();) { ++n; idv.set_str(p.get_raw_item()); idv.push(); @@ -247,61 +247,61 @@ void cs_init_lib_list(cs_state &gcs) { }); gcs.new_command("listfind=", "i", [](auto &cs, auto args, auto &res) { - cs_list_find( - cs, args, res, [](cs_list_parser const &p, cs_int val) { + list_find( + cs, args, res, [](list_parser const &p, integer_type val) { return parse_int(p.get_raw_item()) == val; } ); }); gcs.new_command("listfind=f", "f", [](auto &cs, auto args, auto &res) { - cs_list_find( - cs, args, res, [](cs_list_parser const &p, cs_float val) { + list_find( + cs, args, res, [](list_parser const &p, float_type val) { return parse_float(p.get_raw_item()) == val; } ); }); gcs.new_command("listfind=s", "s", [](auto &cs, auto args, auto &res) { - cs_list_find( - cs, args, res, [](cs_list_parser const &p, std::string_view val) { + list_find( + cs, args, res, [](list_parser const &p, std::string_view val) { return p.get_raw_item() == val; } ); }); gcs.new_command("listassoc=", "i", [](auto &cs, auto args, auto &res) { - cs_list_assoc( - cs, args, res, [](cs_list_parser const &p, cs_int val) { + list_assoc( + cs, args, res, [](list_parser const &p, integer_type val) { return parse_int(p.get_raw_item()) == val; } ); }); gcs.new_command("listassoc=f", "f", [](auto &cs, auto args, auto &res) { - cs_list_assoc( - cs, args, res, [](cs_list_parser const &p, cs_float val) { + list_assoc( + cs, args, res, [](list_parser const &p, float_type val) { return parse_float(p.get_raw_item()) == val; } ); }); gcs.new_command("listassoc=s", "s", [](auto &cs, auto args, auto &res) { - cs_list_assoc( - cs, args, res, [](cs_list_parser const &p, std::string_view val) { + list_assoc( + cs, args, res, [](list_parser const &p, std::string_view val) { return p.get_raw_item() == val; } ); }); gcs.new_command("looplist", "rse", [](auto &cs, auto args, auto &) { - cs_stacked_value idv{cs, args[0].get_ident()}; + stacked_value idv{cs, args[0].get_ident()}; if (!idv.has_alias()) { return; } auto body = args[2].get_code(); int n = 0; - for (cs_list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { + for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { idv.set_str(p.get_item()); idv.push(); switch (cs.run_loop(body)) { - case cs_loop_state::BREAK: + case loop_state::BREAK: goto end; default: /* continue and normal */ break; @@ -312,14 +312,14 @@ end: }); gcs.new_command("looplist2", "rrse", [](auto &cs, auto args, auto &) { - cs_stacked_value idv1{cs, args[0].get_ident()}; - cs_stacked_value idv2{cs, args[1].get_ident()}; + stacked_value idv1{cs, args[0].get_ident()}; + stacked_value idv2{cs, args[1].get_ident()}; if (!idv1.has_alias() || !idv2.has_alias()) { return; } auto body = args[3].get_code(); int n = 0; - for (cs_list_parser p{cs, args[2].get_str()}; p.parse(); n += 2) { + for (list_parser p{cs, args[2].get_str()}; p.parse(); n += 2) { idv1.set_str(p.get_item()); if (p.parse()) { idv2.set_str(p.get_item()); @@ -329,7 +329,7 @@ end: idv1.push(); idv2.push(); switch (cs.run_loop(body)) { - case cs_loop_state::BREAK: + case loop_state::BREAK: goto end; default: /* continue and normal */ break; @@ -340,15 +340,15 @@ end: }); gcs.new_command("looplist3", "rrrse", [](auto &cs, auto args, auto &) { - cs_stacked_value idv1{cs, args[0].get_ident()}; - cs_stacked_value idv2{cs, args[1].get_ident()}; - cs_stacked_value idv3{cs, args[2].get_ident()}; + stacked_value idv1{cs, args[0].get_ident()}; + stacked_value idv2{cs, args[1].get_ident()}; + stacked_value idv3{cs, args[2].get_ident()}; if (!idv1.has_alias() || !idv2.has_alias() || !idv3.has_alias()) { return; } auto body = args[4].get_code(); int n = 0; - for (cs_list_parser p{cs, args[3].get_str()}; p.parse(); n += 3) { + for (list_parser p{cs, args[3].get_str()}; p.parse(); n += 3) { idv1.set_str(p.get_item()); if (p.parse()) { idv2.set_str(p.get_item()); @@ -364,7 +364,7 @@ end: idv2.push(); idv3.push(); switch (cs.run_loop(body)) { - case cs_loop_state::BREAK: + case loop_state::BREAK: goto end; default: /* continue and normal */ break; @@ -375,7 +375,7 @@ end: }); gcs.new_command("looplistconcat", "rse", [](auto &cs, auto args, auto &res) { - cs_loop_list_conc( + loop_list_conc( cs, res, args[0].get_ident(), args[1].get_str(), args[2].get_code(), true ); @@ -384,21 +384,21 @@ end: gcs.new_command("looplistconcatword", "rse", []( auto &cs, auto args, auto &res ) { - cs_loop_list_conc( + loop_list_conc( cs, res, args[0].get_ident(), args[1].get_str(), args[2].get_code(), false ); }); gcs.new_command("listfilter", "rse", [](auto &cs, auto args, auto &res) { - cs_stacked_value idv{cs, args[0].get_ident()}; + stacked_value idv{cs, args[0].get_ident()}; if (!idv.has_alias()) { return; } auto body = args[2].get_code(); - cs_charbuf r{cs}; + charbuf r{cs}; int n = 0; - for (cs_list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { + for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { idv.set_str(p.get_raw_item()); idv.push(); if (cs.run(body).get_bool()) { @@ -412,13 +412,13 @@ end: }); gcs.new_command("listcount", "rse", [](auto &cs, auto args, auto &res) { - cs_stacked_value idv{cs, args[0].get_ident()}; + stacked_value idv{cs, args[0].get_ident()}; if (!idv.has_alias()) { return; } auto body = args[2].get_code(); int n = 0, r = 0; - for (cs_list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { + for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { idv.set_str(p.get_raw_item()); idv.push(); if (cs.run(body).get_bool()) { @@ -429,16 +429,16 @@ end: }); gcs.new_command("prettylist", "ss", [](auto &cs, auto args, auto &res) { - cs_charbuf buf{cs}; + charbuf buf{cs}; std::string_view s = args[0].get_str(); std::string_view conj = args[1].get_str(); - cs_list_parser p{cs, s}; + list_parser p{cs, s}; size_t len = p.count(); size_t n = 0; for (p.set_input(s); p.parse(); ++n) { auto qi = p.get_quoted_item(); if (!qi.empty() && (qi.front() == '"')) { - cs_unescape_string(std::back_inserter(buf), p.get_raw_item()); + unescape_string(std::back_inserter(buf), p.get_raw_item()); } else { buf.append(p.get_raw_item()); } @@ -458,35 +458,35 @@ end: gcs.new_command("indexof", "ss", [](auto &cs, auto args, auto &res) { res.set_int( - cs_list_includes(cs, args[0].get_str(), args[1].get_str()) + list_includes(cs, args[0].get_str(), args[1].get_str()) ); }); gcs.new_command("listdel", "ss", [](auto &cs, auto args, auto &res) { - cs_list_merge(cs, args, res, std::less()); + list_merge(cs, args, res, std::less()); }); gcs.new_command("listintersect", "ss", [](auto &cs, auto args, auto &res) { - cs_list_merge(cs, args, res, std::greater_equal()); + list_merge(cs, args, res, std::greater_equal()); }); gcs.new_command("listunion", "ss", [](auto &cs, auto args, auto &res) { - cs_list_merge(cs, args, res, std::less()); + list_merge(cs, args, res, std::less()); }); gcs.new_command("listsplice", "ssii", [](auto &cs, auto args, auto &res) { - cs_int offset = std::max(args[2].get_int(), cs_int(0)); - cs_int len = std::max(args[3].get_int(), cs_int(0)); + integer_type offset = std::max(args[2].get_int(), integer_type(0)); + integer_type len = std::max(args[3].get_int(), integer_type(0)); std::string_view s = args[0].get_str(); std::string_view vals = args[1].get_str(); char const *list = s.data(); - cs_list_parser p{cs, s}; - for (cs_int i = 0; i < offset; ++i) { + list_parser p{cs, s}; + for (integer_type i = 0; i < offset; ++i) { if (!p.parse()) { break; } } std::string_view quote = p.get_quoted_item(); char const *qend = !quote.empty() ? "e[quote.size()] : list; - cs_charbuf buf{cs}; + charbuf buf{cs}; if (qend > list) { buf.append(list, qend); } @@ -496,7 +496,7 @@ end: } buf.append(vals); } - for (cs_int i = 0; i < len; ++i) { + for (integer_type i = 0; i < len; ++i) { if (!p.parse()) { break; } @@ -518,7 +518,7 @@ end: res.set_str(buf.str()); }); - cs_init_lib_list_sort(gcs); + init_lib_list_sort(gcs); } struct ListSortItem { @@ -527,9 +527,9 @@ struct ListSortItem { }; struct ListSortFun { - cs_state &cs; - cs_stacked_value &xv, &yv; - cs_bcode *body; + state &cs; + stacked_value &xv, &yv; + bcode *body; bool operator()(ListSortItem const &xval, ListSortItem const &yval) { xv.set_str(xval.str); @@ -540,20 +540,20 @@ struct ListSortFun { } }; -static void cs_list_sort( - cs_state &cs, cs_value &res, std::string_view list, - cs_ident *x, cs_ident *y, cs_bcode *body, cs_bcode *unique +static void list_sort( + state &cs, any_value &res, std::string_view list, + ident *x, ident *y, bcode *body, bcode *unique ) { if (x == y || !x->is_alias() || !y->is_alias()) { return; } - cs_alias *xa = static_cast(x), *ya = static_cast(y); + alias *xa = static_cast(x), *ya = static_cast(y); - cs_valbuf items{cs}; + valbuf items{cs}; size_t total = 0; - for (cs_list_parser p{cs, list}; p.parse();) { + for (list_parser p{cs, list}; p.parse();) { ListSortItem item = { p.get_raw_item(), p.get_quoted_item() }; items.push_back(item); total += item.quote.size(); @@ -564,7 +564,7 @@ static void cs_list_sort( return; } - cs_stacked_value xval{cs, xa}, yval{cs, ya}; + stacked_value xval{cs, xa}, yval{cs, ya}; xval.set_none(); yval.set_none(); xval.push(); @@ -575,7 +575,7 @@ static void cs_list_sort( if (body) { ListSortFun f = { cs, xval, yval, body }; std::sort(items.buf.begin(), items.buf.end(), f); - if (!cs_code_is_empty(unique)) { + if (!code_is_empty(unique)) { f.body = unique; totaluniq = items[0].quote.size(); nuniq = 1; @@ -612,7 +612,7 @@ static void cs_list_sort( xval.pop(); yval.pop(); - cs_charbuf sorted{cs}; + charbuf sorted{cs}; sorted.reserve(totaluniq + std::max(nuniq - 1, size_t(0))); for (size_t i = 0; i < items.size(); ++i) { ListSortItem &item = items[i]; @@ -627,15 +627,15 @@ static void cs_list_sort( res.set_str(sorted.str()); } -static void cs_init_lib_list_sort(cs_state &gcs) { +static void init_lib_list_sort(state &gcs) { gcs.new_command("sortlist", "srree", [](auto &cs, auto args, auto &res) { - cs_list_sort( + list_sort( cs, res, args[0].get_str(), args[1].get_ident(), args[2].get_ident(), args[3].get_code(), args[4].get_code() ); }); gcs.new_command("uniquelist", "srre", [](auto &cs, auto args, auto &res) { - cs_list_sort( + list_sort( cs, res, args[0].get_str(), args[1].get_ident(), args[2].get_ident(), nullptr, args[3].get_code() ); diff --git a/src/lib_math.cc b/src/lib_math.cc index a27ba18..2033523 100644 --- a/src/lib_math.cc +++ b/src/lib_math.cc @@ -8,71 +8,71 @@ namespace cscript { -static constexpr cs_float PI = 3.14159265358979f; -static constexpr cs_float RAD = PI / 180.0f; +static constexpr float_type PI = 3.14159265358979f; +static constexpr float_type RAD = PI / 180.0f; template -struct cs_math_val; +struct math_val; template<> -struct cs_math_val { - static cs_int get(cs_value &tv) { +struct math_val { + static integer_type get(any_value &tv) { return tv.get_int(); } - static void set(cs_value &res, cs_int val) { + static void set(any_value &res, integer_type val) { res.set_int(val); } }; template<> -struct cs_math_val { - static cs_float get(cs_value &tv) { +struct math_val { + static float_type get(any_value &tv) { return tv.get_float(); } - static void set(cs_value &res, cs_float val) { + static void set(any_value &res, float_type val) { res.set_float(val); } }; template -struct cs_math_noop { +struct math_noop { T operator()(T arg) { return arg; } }; template -static inline void cs_mathop( - std::span args, cs_value &res, T initval, +static inline void math_op( + std::span args, any_value &res, T initval, F1 binop, F2 unop ) { T val; if (args.size() >= 2) { - val = binop(cs_math_val::get(args[0]), cs_math_val::get(args[1])); + val = binop(math_val::get(args[0]), math_val::get(args[1])); for (size_t i = 2; i < args.size(); ++i) { - val = binop(val, cs_math_val::get(args[i])); + val = binop(val, math_val::get(args[i])); } } else { - val = unop(!args.empty() ? cs_math_val::get(args[0]) : initval); + val = unop(!args.empty() ? math_val::get(args[0]) : initval); } - cs_math_val::set(res, val); + math_val::set(res, val); } template -static inline void cs_cmpop(std::span args, cs_value &res, F cmp) { +static inline void cmp_op(std::span args, any_value &res, F cmp) { bool val; if (args.size() >= 2) { - val = cmp(cs_math_val::get(args[0]), cs_math_val::get(args[1])); + val = cmp(math_val::get(args[0]), math_val::get(args[1])); for (size_t i = 2; (i < args.size()) && val; ++i) { - val = cmp(cs_math_val::get(args[i - 1]), cs_math_val::get(args[i])); + val = cmp(math_val::get(args[i - 1]), math_val::get(args[i])); } } else { - val = cmp(!args.empty() ? cs_math_val::get(args[0]) : T(0), T(0)); + val = cmp(!args.empty() ? math_val::get(args[0]) : T(0), T(0)); } - res.set_int(cs_int(val)); + res.set_int(integer_type(val)); } -void cs_init_lib_math(cs_state &cs) { +void init_lib_math(state &cs) { cs.new_command("sin", "f", [](auto &, auto args, auto &res) { res.set_float(std::sin(args[0].get_float() * RAD)); }); @@ -114,28 +114,28 @@ void cs_init_lib_math(cs_state &cs) { }); cs.new_command("min", "i1V", [](auto &, auto args, auto &res) { - cs_int v = (!args.empty() ? args[0].get_int() : 0); + integer_type v = (!args.empty() ? args[0].get_int() : 0); for (size_t i = 1; i < args.size(); ++i) { v = std::min(v, args[i].get_int()); } res.set_int(v); }); cs.new_command("max", "i1V", [](auto &, auto args, auto &res) { - cs_int v = (!args.empty() ? args[0].get_int() : 0); + integer_type v = (!args.empty() ? args[0].get_int() : 0); for (size_t i = 1; i < args.size(); ++i) { v = std::max(v, args[i].get_int()); } res.set_int(v); }); cs.new_command("minf", "f1V", [](auto &, auto args, auto &res) { - cs_float v = (!args.empty() ? args[0].get_float() : 0); + float_type v = (!args.empty() ? args[0].get_float() : 0); for (size_t i = 1; i < args.size(); ++i) { v = std::min(v, args[i].get_float()); } res.set_float(v); }); cs.new_command("maxf", "f1V", [](auto &, auto args, auto &res) { - cs_float v = (!args.empty() ? args[0].get_float() : 0); + float_type v = (!args.empty() ? args[0].get_float() : 0); for (size_t i = 1; i < args.size(); ++i) { v = std::max(v, args[i].get_float()); } @@ -157,8 +157,8 @@ void cs_init_lib_math(cs_state &cs) { }); cs.new_command("round", "ff", [](auto &, auto args, auto &res) { - cs_float step = args[1].get_float(); - cs_float r = args[0].get_float(); + float_type step = args[1].get_float(); + float_type r = args[0].get_float(); if (step > 0) { r += step * ((r < 0) ? -0.5 : 0.5); r -= std::fmod(r, step); @@ -169,43 +169,43 @@ void cs_init_lib_math(cs_state &cs) { }); cs.new_command("+", "i1V", [](auto &, auto args, auto &res) { - cs_mathop(args, res, 0, std::plus(), cs_math_noop()); + math_op(args, res, 0, std::plus(), math_noop()); }); cs.new_command("*", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 1, std::multiplies(), cs_math_noop() + math_op( + args, res, 1, std::multiplies(), math_noop() ); }); cs.new_command("-", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, std::minus(), std::negate() + math_op( + args, res, 0, std::minus(), std::negate() ); }); cs.new_command("^", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, std::bit_xor(), [](cs_int val) { return ~val; } + math_op( + args, res, 0, std::bit_xor(), [](integer_type val) { return ~val; } ); }); cs.new_command("~", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, std::bit_xor(), [](cs_int val) { return ~val; } + math_op( + args, res, 0, std::bit_xor(), [](integer_type val) { return ~val; } ); }); cs.new_command("&", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, std::bit_and(), cs_math_noop() + math_op( + args, res, 0, std::bit_and(), math_noop() ); }); cs.new_command("|", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, std::bit_or(), cs_math_noop() + math_op( + args, res, 0, std::bit_or(), math_noop() ); }); /* special combined cases */ cs.new_command("^~", "i1V", [](auto &, auto args, auto &res) { - cs_int val; + integer_type val; if (args.size() >= 2) { val = args[0].get_int() ^ ~args[1].get_int(); for (size_t i = 2; i < args.size(); ++i) { @@ -217,7 +217,7 @@ void cs_init_lib_math(cs_state &cs) { res.set_int(val); }); cs.new_command("&~", "i1V", [](auto &, auto args, auto &res) { - cs_int val; + integer_type val; if (args.size() >= 2) { val = args[0].get_int() & ~args[1].get_int(); for (size_t i = 2; i < args.size(); ++i) { @@ -229,7 +229,7 @@ void cs_init_lib_math(cs_state &cs) { res.set_int(val); }); cs.new_command("|~", "i1V", [](auto &, auto args, auto &res) { - cs_int val; + integer_type val; if (args.size() >= 2) { val = args[0].get_int() | ~args[1].get_int(); for (size_t i = 2; i < args.size(); ++i) { @@ -242,125 +242,125 @@ void cs_init_lib_math(cs_state &cs) { }); cs.new_command("<<", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, [](cs_int val1, cs_int val2) { - return (val2 < cs_int(sizeof(cs_int) * CHAR_BIT)) - ? (val1 << std::max(val2, cs_int(0))) + math_op( + args, res, 0, [](integer_type val1, integer_type val2) { + return (val2 < integer_type(sizeof(integer_type) * CHAR_BIT)) + ? (val1 << std::max(val2, integer_type(0))) : 0; - }, cs_math_noop() + }, math_noop() ); }); cs.new_command(">>", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, [](cs_int val1, cs_int val2) { + math_op( + args, res, 0, [](integer_type val1, integer_type val2) { return val1 >> std::clamp( - val2, cs_int(0), cs_int(sizeof(cs_int) * CHAR_BIT) + val2, integer_type(0), integer_type(sizeof(integer_type) * CHAR_BIT) ); - }, cs_math_noop() + }, math_noop() ); }); cs.new_command("+f", "f1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, std::plus(), cs_math_noop() + math_op( + args, res, 0, std::plus(), math_noop() ); }); cs.new_command("*f", "f1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 1, std::multiplies(), cs_math_noop() + math_op( + args, res, 1, std::multiplies(), math_noop() ); }); cs.new_command("-f", "f1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, std::minus(), std::negate() + math_op( + args, res, 0, std::minus(), std::negate() ); }); cs.new_command("div", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, [](cs_int val1, cs_int val2) { + math_op( + args, res, 0, [](integer_type val1, integer_type val2) { if (val2) { return val1 / val2; } - return cs_int(0); - }, cs_math_noop() + return integer_type(0); + }, math_noop() ); }); cs.new_command("mod", "i1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, [](cs_int val1, cs_int val2) { + math_op( + args, res, 0, [](integer_type val1, integer_type val2) { if (val2) { return val1 % val2; } - return cs_int(0); - }, cs_math_noop() + return integer_type(0); + }, math_noop() ); }); cs.new_command("divf", "f1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, [](cs_float val1, cs_float val2) { + math_op( + args, res, 0, [](float_type val1, float_type val2) { if (val2) { return val1 / val2; } - return cs_float(0); - }, cs_math_noop() + return float_type(0); + }, math_noop() ); }); cs.new_command("modf", "f1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, [](cs_float val1, cs_float val2) { + math_op( + args, res, 0, [](float_type val1, float_type val2) { if (val2) { - return cs_float(fmod(val1, val2)); + return float_type(fmod(val1, val2)); } - return cs_float(0); - }, cs_math_noop() + return float_type(0); + }, math_noop() ); }); cs.new_command("pow", "f1V", [](auto &, auto args, auto &res) { - cs_mathop( - args, res, 0, [](cs_float val1, cs_float val2) { - return cs_float(pow(val1, val2)); - }, cs_math_noop() + math_op( + args, res, 0, [](float_type val1, float_type val2) { + return float_type(pow(val1, val2)); + }, math_noop() ); }); cs.new_command("=", "i1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::equal_to()); + cmp_op(args, res, std::equal_to()); }); cs.new_command("!=", "i1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::not_equal_to()); + cmp_op(args, res, std::not_equal_to()); }); cs.new_command("<", "i1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::less()); + cmp_op(args, res, std::less()); }); cs.new_command(">", "i1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::greater()); + cmp_op(args, res, std::greater()); }); cs.new_command("<=", "i1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::less_equal()); + cmp_op(args, res, std::less_equal()); }); cs.new_command(">=", "i1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::greater_equal()); + cmp_op(args, res, std::greater_equal()); }); cs.new_command("=f", "f1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::equal_to()); + cmp_op(args, res, std::equal_to()); }); cs.new_command("!=f", "f1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::not_equal_to()); + cmp_op(args, res, std::not_equal_to()); }); cs.new_command("(args, res, std::less()); + cmp_op(args, res, std::less()); }); cs.new_command(">f", "f1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::greater()); + cmp_op(args, res, std::greater()); }); cs.new_command("<=f", "f1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::less_equal()); + cmp_op(args, res, std::less_equal()); }); cs.new_command(">=f", "f1V", [](auto &, auto args, auto &res) { - cs_cmpop(args, res, std::greater_equal()); + cmp_op(args, res, std::greater_equal()); }); } diff --git a/src/lib_str.cc b/src/lib_str.cc index d8b8da4..1252415 100644 --- a/src/lib_str.cc +++ b/src/lib_str.cc @@ -9,8 +9,8 @@ namespace cscript { template -static inline void cs_strgcmp( - std::span args, cs_value &res, F cfunc +static inline void str_cmp_by( + std::span args, any_value &res, F cfunc ) { bool val; if (args.size() >= 2) { @@ -24,28 +24,28 @@ static inline void cs_strgcmp( std::string_view() ); } - res.set_int(cs_int(val)); + res.set_int(integer_type(val)); }; -void cs_init_lib_string(cs_state &cs) { +void init_lib_string(state &cs) { cs.new_command("strstr", "ss", [](auto &, auto args, auto &res) { std::string_view a = args[0].get_str(), b = args[1].get_str(); auto pos = a.find(b); if (pos == a.npos) { res.set_int(-1); } else { - res.set_int(cs_int(pos)); + res.set_int(integer_type(pos)); } }); cs.new_command("strlen", "s", [](auto &, auto args, auto &res) { - res.set_int(cs_int(args[0].get_str().size())); + res.set_int(integer_type(args[0].get_str().size())); }); cs.new_command("strcode", "si", [](auto &, auto args, auto &res) { std::string_view str = args[0].get_str(); - cs_int i = args[1].get_int(); - if (i >= cs_int(str.size())) { + integer_type i = args[1].get_int(); + if (i >= integer_type(str.size())) { res.set_int(0); } else { res.set_int(static_cast(str[i])); @@ -59,7 +59,7 @@ void cs_init_lib_string(cs_state &cs) { cs.new_command("strlower", "s", [](auto &ccs, auto args, auto &res) { auto inps = std::string_view{args[0].get_str()}; - auto *ics = cs_get_sstate(ccs); + auto *ics = state_get_internal(ccs); auto *buf = ics->strman->alloc_buf(inps.size()); for (std::size_t i = 0; i < inps.size(); ++i) { buf[i] = tolower(inps[i]); @@ -69,7 +69,7 @@ void cs_init_lib_string(cs_state &cs) { cs.new_command("strupper", "s", [](auto &ccs, auto args, auto &res) { auto inps = std::string_view{args[0].get_str()}; - auto *ics = cs_get_sstate(ccs); + auto *ics = state_get_internal(ccs); auto *buf = ics->strman->alloc_buf(inps.size()); for (std::size_t i = 0; i < inps.size(); ++i) { buf[i] = toupper(inps[i]); @@ -78,31 +78,31 @@ void cs_init_lib_string(cs_state &cs) { }); cs.new_command("escape", "s", [](auto &ccs, auto args, auto &res) { - cs_charbuf s{ccs}; - cs_escape_string(std::back_inserter(s), args[0].get_str()); + charbuf s{ccs}; + escape_string(std::back_inserter(s), args[0].get_str()); res.set_str(s.str()); }); cs.new_command("unescape", "s", [](auto &ccs, auto args, auto &res) { - cs_charbuf s{ccs}; - cs_unescape_string(std::back_inserter(s), args[0].get_str()); + charbuf s{ccs}; + unescape_string(std::back_inserter(s), args[0].get_str()); res.set_str(s.str()); }); cs.new_command("concat", "V", [](auto &ccs, auto args, auto &res) { - res.set_str(cs_concat_values(ccs, args, " ")); + res.set_str(concat_values(ccs, args, " ")); }); cs.new_command("concatword", "V", [](auto &ccs, auto args, auto &res) { - res.set_str(cs_concat_values(ccs, args)); + res.set_str(concat_values(ccs, args)); }); cs.new_command("format", "V", [](auto &ccs, auto args, auto &res) { if (args.empty()) { return; } - cs_charbuf s{ccs}; - cs_strref fs = args[0].get_str(); + charbuf s{ccs}; + string_ref fs = args[0].get_str(); std::string_view f{fs}; for (auto it = f.begin(); it != f.end(); ++it) { char c = *it; @@ -132,7 +132,7 @@ void cs_init_lib_string(cs_state &cs) { int prec = std::max(int(args[1].get_int()), 1); int n = snprintf(buf, sizeof(buf), "0x%.*llX", prec, val); if (n >= int(sizeof(buf))) { - cs_charbuf s{ccs}; + charbuf s{ccs}; s.reserve(n + 1); s.data()[0] = '\0'; int nn = snprintf(s.data(), n + 1, "0x%.*llX", prec, val); @@ -145,42 +145,42 @@ void cs_init_lib_string(cs_state &cs) { return; } /* should pretty much be unreachable */ - throw cs_internal_error{"format error"}; + throw internal_error{"format error"}; }); cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) { std::string_view s = args[0].get_str(); - cs_int start = args[1].get_int(), count = args[2].get_int(); - cs_int numargs = args[3].get_int(); - cs_int len = cs_int(s.size()), offset = std::clamp(start, cs_int(0), len); + integer_type start = args[1].get_int(), count = args[2].get_int(); + integer_type numargs = args[3].get_int(); + integer_type len = integer_type(s.size()), offset = std::clamp(start, integer_type(0), len); res.set_str(std::string_view{ &s[offset], ((numargs >= 3) - ? size_t(std::clamp(count, cs_int(0), len - offset)) + ? size_t(std::clamp(count, integer_type(0), len - offset)) : size_t(len - offset)) }); }); cs.new_command("strcmp", "s1V", [](auto &, auto args, auto &res) { - cs_strgcmp(args, res, std::equal_to()); + str_cmp_by(args, res, std::equal_to()); }); cs.new_command("=s", "s1V", [](auto &, auto args, auto &res) { - cs_strgcmp(args, res, std::equal_to()); + str_cmp_by(args, res, std::equal_to()); }); cs.new_command("!=s", "s1V", [](auto &, auto args, auto &res) { - cs_strgcmp(args, res, std::not_equal_to()); + str_cmp_by(args, res, std::not_equal_to()); }); cs.new_command("()); + str_cmp_by(args, res, std::less()); }); cs.new_command(">s", "s1V", [](auto &, auto args, auto &res) { - cs_strgcmp(args, res, std::greater()); + str_cmp_by(args, res, std::greater()); }); cs.new_command("<=s", "s1V", [](auto &, auto args, auto &res) { - cs_strgcmp(args, res, std::less_equal()); + str_cmp_by(args, res, std::less_equal()); }); cs.new_command(">=s", "s1V", [](auto &, auto args, auto &res) { - cs_strgcmp(args, res, std::greater_equal()); + str_cmp_by(args, res, std::greater_equal()); }); cs.new_command("strreplace", "ssss", [](auto &ccs, auto args, auto &res) { @@ -195,7 +195,7 @@ void cs_init_lib_string(cs_state &cs) { res.set_str(s); return; } - cs_charbuf buf{ccs}; + charbuf buf{ccs}; for (size_t i = 0;; ++i) { std::string_view found; auto p = s.find(oldval); @@ -216,17 +216,17 @@ void cs_init_lib_string(cs_state &cs) { cs.new_command("strsplice", "ssii", [](auto &ccs, auto args, auto &res) { std::string_view s = args[0].get_str(); std::string_view vals = args[1].get_str(); - cs_int skip = args[2].get_int(), + integer_type skip = args[2].get_int(), count = args[3].get_int(); - cs_int offset = std::clamp(skip, cs_int(0), cs_int(s.size())), - len = std::clamp(count, cs_int(0), cs_int(s.size()) - offset); - cs_charbuf p{ccs}; + integer_type offset = std::clamp(skip, integer_type(0), integer_type(s.size())), + len = std::clamp(count, integer_type(0), integer_type(s.size()) - offset); + charbuf p{ccs}; p.reserve(s.size() - len + vals.size()); if (offset) { p.append(s.substr(0, offset)); } p.append(vals); - if ((offset + len) < cs_int(s.size())) { + if ((offset + len) < integer_type(s.size())) { p.append(s.substr(offset + len, s.size() - offset - len)); } res.set_str(p.str()); diff --git a/tools/edit_fallback.hh b/tools/edit_fallback.hh index 6852c67..0dd4df8 100644 --- a/tools/edit_fallback.hh +++ b/tools/edit_fallback.hh @@ -4,10 +4,10 @@ #include #include -inline void init_lineedit(cs_state &, std::string_view) { +inline void init_lineedit(cs::state &, std::string_view) { } -inline std::optional read_line(cs_state &, cs_svar *pr) { +inline std::optional read_line(cs::state &, cs::string_var *pr) { std::string lbuf; char buf[512]; printf("%s", pr->get_value().data()); @@ -21,7 +21,7 @@ inline std::optional read_line(cs_state &, cs_svar *pr) { return std::move(lbuf); } -inline void add_history(cs_state &, std::string_view) { +inline void add_history(cs::state &, std::string_view) { } #endif diff --git a/tools/edit_linenoise.hh b/tools/edit_linenoise.hh index 33acf71..cf76f71 100644 --- a/tools/edit_linenoise.hh +++ b/tools/edit_linenoise.hh @@ -12,7 +12,7 @@ #include "linenoise.hh" -static cs_state *ln_cs = nullptr; +static cs::state *ln_cs = nullptr; inline void ln_complete(char const *buf, linenoiseCompletions *lc) { std::string_view cmd = get_complete_cmd(buf); @@ -31,7 +31,7 @@ inline void ln_complete(char const *buf, linenoiseCompletions *lc) { } inline char *ln_hint(char const *buf, int *color, int *bold) { - cs_command *cmd = get_hint_cmd(*ln_cs, buf); + cs::command *cmd = get_hint_cmd(*ln_cs, buf); if (!cmd) { return nullptr; } @@ -49,7 +49,7 @@ inline void ln_hint_free(void *hint) { delete[] static_cast(hint); } -inline void init_lineedit(cs_state &cs, std::string_view) { +inline void init_lineedit(cs::state &cs, std::string_view) { /* sensible default history size */ linenoiseHistorySetMaxLen(1000); ln_cs = &cs; @@ -58,7 +58,7 @@ inline void init_lineedit(cs_state &cs, std::string_view) { linenoiseSetFreeHintsCallback(ln_hint_free); } -inline std::optional read_line(cs_state &, cs_svar *pr) { +inline std::optional read_line(cs::state &, cs::string_var *pr) { auto line = linenoise(pr->get_value().data()); if (!line) { /* linenoise traps ctrl-c, detect it and let the user exit */ @@ -73,7 +73,7 @@ inline std::optional read_line(cs_state &, cs_svar *pr) { return ret; } -inline void add_history(cs_state &, std::string_view line) { +inline void add_history(cs::state &, std::string_view line) { /* backed by std::string so it's terminated */ linenoiseHistoryAdd(line.data()); } diff --git a/tools/edit_readline.hh b/tools/edit_readline.hh index a0a8bc1..b7c4a02 100644 --- a/tools/edit_readline.hh +++ b/tools/edit_readline.hh @@ -9,18 +9,18 @@ #include #include -static cs_state *rd_cs = nullptr; +static cs::state *rd_cs = nullptr; inline char *ln_complete_list(char const *buf, int state) { static std::string_view cmd; - static std::span itr; + static std::span itr; if (!state) { cmd = get_complete_cmd(buf); itr = rd_cs->get_idents(); } - for (cs_ident *id: itr) { + for (cs::ident *id: itr) { if (!id->is_command()) { continue; } @@ -43,7 +43,7 @@ inline char **ln_complete(char const *buf, int, int) { } inline void ln_hint() { - cs_command *cmd = get_hint_cmd(*rd_cs, rl_line_buffer); + cs::command *cmd = get_hint_cmd(*rd_cs, rl_line_buffer); if (!cmd) { rl_redisplay(); return; @@ -59,13 +59,13 @@ inline void ln_hint() { rl_replace_line(old.data(), 0); } -inline void init_lineedit(cs_state &cs, std::string_view) { +inline void init_lineedit(cs::state &cs, std::string_view) { rd_cs = &cs; rl_attempted_completion_function = ln_complete; rl_redisplay_function = ln_hint; } -inline std::optional read_line(cs_state &, cs_svar *pr) { +inline std::optional read_line(cs::state &, cs::string_var *pr) { auto line = readline(pr->get_value().data()); if (!line) { return std::string(); @@ -75,7 +75,7 @@ inline std::optional read_line(cs_state &, cs_svar *pr) { return ret; } -inline void add_history(cs_state &, std::string_view line) { +inline void add_history(cs::state &, std::string_view line) { /* backed by std::string so it's terminated */ add_history(line.data()); } diff --git a/tools/repl.cc b/tools/repl.cc index 5876807..ef13527 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -9,7 +9,7 @@ #include -using namespace cscript; +namespace cs = cscript; std::string_view version = "CubeScript 0.0.1"; @@ -124,11 +124,11 @@ inline void fill_cmd_args(std::string &writer, std::string_view args) { } } -inline cs_command *get_hint_cmd(cs_state &cs, std::string_view buf) { +inline cs::command *get_hint_cmd(cs::state &cs, std::string_view buf) { std::string_view nextchars = "([;"; auto lp = buf.find_first_of(nextchars); if (lp != buf.npos) { - cs_command *cmd = get_hint_cmd(cs, buf.substr(1, buf.size() - 1)); + cs::command *cmd = get_hint_cmd(cs, buf.substr(1, buf.size() - 1)); if (cmd) { return cmd; } @@ -179,23 +179,23 @@ void print_version() { printf("%s\n", version.data()); } -static cs_state *scs = nullptr; +static cs::state *scs = nullptr; static void do_sigint(int n) { /* in case another SIGINT happens, terminate normally */ signal(n, SIG_DFL); - scs->set_call_hook([](cs_state &cs) { + scs->set_call_hook([](cs::state &cs) { cs.set_call_hook(nullptr); - throw cscript::cs_error(cs, ""); + throw cs::error{cs, ""}; }); } /* an example of what var printer would look like in real usage */ -static void repl_print_var(cs_state const &cs, cs_var const &var) { +static void repl_print_var(cs::state const &cs, cs::global_var const &var) { switch (var.get_type()) { - case cs_ident_type::IVAR: { - auto &iv = static_cast(var); + case cs::ident_type::IVAR: { + auto &iv = static_cast(var); auto val = iv.get_value(); - if (!(iv.get_flags() & CS_IDF_HEX) || (val < 0)) { + if (!(iv.get_flags() & cs::IDENT_FLAG_HEX) || (val < 0)) { std::printf("%s = %d\n", iv.get_name().data(), val); } else if (iv.get_val_max() == 0xFFFFFF) { std::printf( @@ -208,8 +208,8 @@ static void repl_print_var(cs_state const &cs, cs_var const &var) { } break; } - case cs_ident_type::FVAR: { - auto &fv = static_cast(var); + case cs::ident_type::FVAR: { + auto &fv = static_cast(var); auto val = fv.get_value(); if (std::floor(val) == val) { std::printf("%s = %.1f", fv.get_name().data(), val); @@ -218,8 +218,8 @@ static void repl_print_var(cs_state const &cs, cs_var const &var) { } break; } - case cs_ident_type::SVAR: { - auto &sv = static_cast(var); + case cs::ident_type::SVAR: { + auto &sv = static_cast(var); auto val = std::string_view{sv.get_value()}; if (val.find('"') == val.npos) { std::printf("%s = \"%s\"", sv.get_name().data(), val.data()); @@ -233,7 +233,9 @@ static void repl_print_var(cs_state const &cs, cs_var const &var) { } } -static bool do_run_file(cs_state &cs, std::string_view fname, cs_value &ret) { +static bool do_run_file( + cs::state &cs, std::string_view fname, cs::any_value &ret +) { FILE *f = std::fopen(fname.data(), "rb"); if (!f) { return false; @@ -260,8 +262,8 @@ static bool do_run_file(cs_state &cs, std::string_view fname, cs_value &ret) { return true; } -static bool do_call(cs_state &cs, std::string_view line, bool file = false) { - cs_value ret{cs}; +static bool do_call(cs::state &cs, std::string_view line, bool file = false) { + cs::any_value ret{cs}; scs = &cs; signal(SIGINT, do_sigint); try { @@ -272,7 +274,7 @@ static bool do_call(cs_state &cs, std::string_view line, bool file = false) { } else { cs.run(line, ret); } - } catch (cscript::cs_error const &e) { + } catch (cs::error const &e) { signal(SIGINT, SIG_DFL); scs = nullptr; std::string_view terr = e.what(); @@ -295,20 +297,20 @@ static bool do_call(cs_state &cs, std::string_view line, bool file = false) { ); if (e.get_stack().get()) { std::string str; - cscript::cs_print_stack(std::back_inserter(str), e.get_stack()); + cs::print_stack(std::back_inserter(str), e.get_stack()); std::printf("%s\n", str.data()); } return false; } signal(SIGINT, SIG_DFL); scs = nullptr; - if (ret.get_type() != cs_value_type::NONE) { + if (ret.get_type() != cs::value_type::NONE) { std::printf("%s\n", std::string_view{ret.get_str()}.data()); } return false; } -static void do_tty(cs_state &cs) { +static void do_tty(cs::state &cs) { auto prompt = cs.new_svar("PROMPT", "> "); auto prompt2 = cs.new_svar("PROMPT2", ">> "); @@ -349,16 +351,16 @@ static void do_tty(cs_state &cs) { } int main(int argc, char **argv) { - cs_state gcs; + cs::state gcs; gcs.set_var_printer(repl_print_var); gcs.init_libs(); gcs.new_command("exec", "s", [](auto &cs, auto args, auto &) { auto file = args[0].get_str(); - cs_value val{cs}; + cs::any_value val{cs}; bool ret = do_run_file(cs, file, val); if (!ret) { - throw cscript::cs_error( + throw cs::error( cs, "could not run file \"%s\"", file ); }