Browse Source

remove cs_ namespace in all API

master
Daniel Kolesa 4 months ago
parent
commit
b9b344cba6
  1. 602
      include/cubescript/cubescript.hh
  2. 10
      include/cubescript/cubescript_conf.hh
  3. 70
      src/cs_bcode.cc
  4. 100
      src/cs_bcode.hh
  5. 28
      src/cs_error.cc
  6. 640
      src/cs_gen.cc
  7. 262
      src/cs_ident.cc
  8. 110
      src/cs_ident.hh
  9. 68
      src/cs_parser.cc
  10. 4
      src/cs_parser.hh
  11. 443
      src/cs_state.cc
  12. 56
      src/cs_state.hh
  13. 24
      src/cs_std.hh
  14. 60
      src/cs_strman.cc
  15. 28
      src/cs_strman.hh
  16. 344
      src/cs_val.cc
  17. 892
      src/cs_vm.cc
  18. 89
      src/cs_vm.hh
  19. 100
      src/lib_base.cc
  20. 216
      src/lib_list.cc
  21. 184
      src/lib_math.cc
  22. 74
      src/lib_str.cc
  23. 6
      tools/edit_fallback.hh
  24. 10
      tools/edit_linenoise.hh
  25. 14
      tools/edit_readline.hh
  26. 50
      tools/repl.cc

602
include/cubescript/cubescript.hh
File diff suppressed because it is too large
View File

10
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 */

70
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<std::uint32_t>{cs};
std::uint32_t *bcode_alloc(state &cs, std::size_t sz) {
auto a = std_allocator<std::uint32_t>{cs};
std::size_t hdrs = sizeof(bcode_hdr) / sizeof(std::uint32_t);
auto p = a.allocate(sz + hdrs - 1);
bcode_hdr *hdr = reinterpret_cast<bcode_hdr *>(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<bcode_hdr *>(rp);
cs_allocator<std::uint32_t>{hdr->cs}.deallocate(rp, hdr->asize);
std_allocator<std::uint32_t>{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<empty_block>{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<empty_block>{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<empty_block>{cs}.deallocate(empty, CS_VAL_ANY);
void bcode_free_empty(internal_state *cs, empty_block *empty) {
std_allocator<empty_block>{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;
}

100
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,
CS_CODE_OP_MASK = 0x3F,
CS_CODE_RET = 6,
CS_CODE_RET_MASK = 0xC0,
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,
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,
/* 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_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,
/* 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 */

28
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_charbuf *>(cs.p_errbuf);
cs_gen_state *gs = cs.p_pstate;
charbuf &cb = *static_cast<charbuf *>(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_ivar *>(cs.p_state->identmap[DbgaliasIdx]);
LIBCUBESCRIPT_EXPORT stack_state error::save_stack(state &cs) {
integer_var *dalias = static_cast<integer_var *>(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<cs_stack_state_node>(
stack_state_node *st = cs.p_state->create_array<stack_state_node>(
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 */

640
src/cs_gen.cc
File diff suppressed because it is too large
View File

262
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<cs_ivar_impl *>(this));
cb_var(cs, *static_cast<ivar_impl *>(this));
break;
case ID_FVAR:
cb_var(cs, *static_cast<cs_fvar_impl *>(this));
cb_var(cs, *static_cast<fvar_impl *>(this));
break;
case ID_SVAR:
cb_var(cs, *static_cast<cs_svar_impl *>(this));
cb_var(cs, *static_cast<svar_impl *>(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<cs_bcode *>(code);
p_acode = reinterpret_cast<bcode *>(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<cs_alias *>(this);
return static_cast<alias *>(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<cs_alias const *>(this);
return static_cast<alias const *>(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<cs_command_impl *>(this);
return static_cast<command_impl *>(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<cs_command_impl const *>(this);
return static_cast<command_impl const *>(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<cs_var *>(this);
return static_cast<global_var *>(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<cs_var const *>(this);
return static_cast<global_var const *>(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<cs_ivar *>(this);
return static_cast<integer_var *>(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<cs_ivar const *>(this);
return static_cast<integer_var const *>(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<cs_fvar *>(this);
return static_cast<float_var *>(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<cs_fvar const *>(this);
return static_cast<float_var const *>(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<cs_svar *>(this);
return static_cast<string_var *>(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<cs_svar const *>(this);
return static_cast<string_var const *>(this);
}
LIBCUBESCRIPT_EXPORT cs_int cs_ivar::get_val_min() const {
return static_cast<cs_ivar_impl const *>(this)->p_minval;
LIBCUBESCRIPT_EXPORT integer_type integer_var::get_val_min() const {
return static_cast<ivar_impl const *>(this)->p_minval;
}
LIBCUBESCRIPT_EXPORT cs_int cs_ivar::get_val_max() const {
return static_cast<cs_ivar_impl const *>(this)->p_maxval;
LIBCUBESCRIPT_EXPORT integer_type integer_var::get_val_max() const {
return static_cast<ivar_impl const *>(this)->p_maxval;
}
LIBCUBESCRIPT_EXPORT cs_int cs_ivar::get_value() const {
return static_cast<cs_ivar_impl const *>(this)->p_storage;
LIBCUBESCRIPT_EXPORT integer_type integer_var::get_value() const {
return static_cast<ivar_impl const *>(this)->p_storage;
}
LIBCUBESCRIPT_EXPORT void cs_ivar::set_value(cs_int val) {
static_cast<cs_ivar_impl *>(this)->p_storage = val;
LIBCUBESCRIPT_EXPORT void integer_var::set_value(integer_type val) {
static_cast<ivar_impl *>(this)->p_storage = val;
}
LIBCUBESCRIPT_EXPORT cs_float cs_fvar::get_val_min() const {
return static_cast<cs_fvar_impl const *>(this)->p_minval;
LIBCUBESCRIPT_EXPORT float_type float_var::get_val_min() const {
return static_cast<fvar_impl const *>(this)->p_minval;
}
LIBCUBESCRIPT_EXPORT cs_float cs_fvar::get_val_max() const {
return static_cast<cs_fvar_impl const *>(this)->p_maxval;
LIBCUBESCRIPT_EXPORT float_type float_var::get_val_max() const {
return static_cast<fvar_impl const *>(this)->p_maxval;
}
LIBCUBESCRIPT_EXPORT cs_float cs_fvar::get_value() const {
return static_cast<cs_fvar_impl const *>(this)->p_storage;
LIBCUBESCRIPT_EXPORT float_type float_var::get_value() const {
return static_cast<fvar_impl const *>(this)->p_storage;
}
LIBCUBESCRIPT_EXPORT void cs_fvar::set_value(cs_float val) {
static_cast<cs_fvar_impl *>(this)->p_storage = val;
LIBCUBESCRIPT_EXPORT void float_var::set_value(float_type val) {
static_cast<fvar_impl *>(this)->p_storage = val;
}
LIBCUBESCRIPT_EXPORT cs_strref cs_svar::get_value() const {
return static_cast<cs_svar_impl const *>(this)->p_storage;
LIBCUBESCRIPT_EXPORT string_ref string_var::get_value() const {
return static_cast<svar_impl const *>(this)->p_storage;
}
LIBCUBESCRIPT_EXPORT void cs_svar::set_value(cs_strref val) {
static_cast<cs_svar_impl *>(this)->p_storage = val;
LIBCUBESCRIPT_EXPORT void string_var::set_value(string_ref val) {
static_cast<svar_impl *>(this)->p_storage = val;
}
LIBCUBESCRIPT_EXPORT cs_value cs_alias::get_value() const {
return static_cast<cs_alias_impl const *>(this)->p_val;
LIBCUBESCRIPT_EXPORT any_value alias::get_value() const {
return static_cast<alias_impl const *>(this)->p_val;
}
void cs_alias::get_cval(cs_value &v) const {
auto *imp = static_cast<cs_alias_impl const *>(this);
void alias::get_cval(any_value &v) const {
auto *imp = static_cast<alias_impl const *>(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<cs_command_impl const *>(this)->p_cargs;
LIBCUBESCRIPT_EXPORT std::string_view command::get_args() const {
return static_cast<command_impl const *>(this)->p_cargs;
}
LIBCUBESCRIPT_EXPORT int cs_command::get_num_args() const {
return static_cast<cs_command_impl const *>(this)->p_numargs;
LIBCUBESCRIPT_EXPORT int command::get_num_args() const {
return static_cast<command_impl const *>(this)->p_numargs;
}
} /* namespace cscript */

110
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<cs_value> args, cs_value &ret) {
void call(state &cs, std::span<any_value> 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 */

68
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<bool Hex, char e1 = Hex ? 'p' : 'e', char e2 = Hex ? 'P' : 'E'>
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<bool Hex>
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;

4
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);

443
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<cs_strman>(this)},
strman{create<string_pool>(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{};