use c++20 std::span
parent
4068c96ec2
commit
320fdbaefd
|
@ -457,8 +457,8 @@ struct OSTD_EXPORT cs_state {
|
|||
cs_alias *get_alias(std::string_view name);
|
||||
bool have_ident(std::string_view name);
|
||||
|
||||
cs_ident_r get_idents();
|
||||
cs_const_ident_r get_idents() const;
|
||||
std::span<cs_ident *> get_idents();
|
||||
std::span<cs_ident const *> get_idents() const;
|
||||
|
||||
void reset_var(std::string_view name);
|
||||
void touch_var(std::string_view name);
|
||||
|
@ -466,12 +466,12 @@ struct OSTD_EXPORT cs_state {
|
|||
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, cs_value_r args, cs_value &ret);
|
||||
void run(cs_ident *id, std::span<cs_value> args, cs_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, cs_value_r args);
|
||||
cs_value run(cs_ident *id, std::span<cs_value> args);
|
||||
|
||||
cs_loop_state run_loop(cs_bcode *code, cs_value &ret);
|
||||
cs_loop_state run_loop(cs_bcode *code);
|
||||
|
@ -495,7 +495,7 @@ struct OSTD_EXPORT cs_state {
|
|||
);
|
||||
|
||||
void set_var_int_checked(cs_ivar *iv, cs_int v);
|
||||
void set_var_int_checked(cs_ivar *iv, cs_value_r args);
|
||||
void set_var_int_checked(cs_ivar *iv, std::span<cs_value> args);
|
||||
void set_var_float_checked(cs_fvar *fv, cs_float v);
|
||||
void set_var_str_checked(cs_svar *fv, std::string_view v);
|
||||
|
||||
|
@ -658,7 +658,8 @@ OSTD_EXPORT cs_strref list_get_item(cs_list_parse_state &ps, cs_state &cs);
|
|||
OSTD_EXPORT void list_find_item(cs_list_parse_state &ps);
|
||||
|
||||
OSTD_EXPORT cs_strref value_list_concat(
|
||||
cs_state &cs, cs_value_r vals, std::string_view sep = std::string_view{}
|
||||
cs_state &cs, std::span<cs_value> vals,
|
||||
std::string_view sep = std::string_view{}
|
||||
);
|
||||
|
||||
namespace util {
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <limits.h>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include <ostd/range.hh>
|
||||
|
||||
/* do not modify */
|
||||
|
@ -11,10 +12,6 @@ namespace cscript {
|
|||
struct cs_ident;
|
||||
struct cs_value;
|
||||
struct cs_var;
|
||||
|
||||
using cs_value_r = ostd::iterator_range<cs_value *>;
|
||||
using cs_ident_r = ostd::iterator_range<cs_ident **>;
|
||||
using cs_const_ident_r = ostd::iterator_range<cs_ident const **>;
|
||||
}
|
||||
|
||||
/* configurable section */
|
||||
|
@ -33,7 +30,7 @@ namespace cscript {
|
|||
*/
|
||||
using cs_var_cb = std::function<void(cs_state &, cs_ident &)>;
|
||||
using cs_vprint_cb = std::function<void(cs_state const &, cs_var const &)>;
|
||||
using cs_command_cb = std::function<void(cs_state &, cs_value_r, cs_value &)>;
|
||||
using cs_command_cb = std::function<void(cs_state &, std::span<cs_value>, cs_value &)>;
|
||||
using cs_hook_cb = std::function<void(cs_state &)>;
|
||||
using cs_alloc_cb = void *(*)(void *, void *, size_t, size_t);
|
||||
|
||||
|
|
|
@ -557,7 +557,7 @@ OSTD_EXPORT void list_find_item(cs_list_parse_state &ps) {
|
|||
}
|
||||
|
||||
OSTD_EXPORT cs_strref value_list_concat(
|
||||
cs_state &cs, cs_value_r vals, std::string_view sep
|
||||
cs_state &cs, std::span<cs_value> vals, std::string_view sep
|
||||
) {
|
||||
auto app = ostd::appender<cs_charbuf>(cs);
|
||||
for (std::size_t i = 0; i < vals.size(); ++i) {
|
||||
|
|
60
src/cs_vm.cc
60
src/cs_vm.cc
|
@ -9,7 +9,7 @@ namespace cscript {
|
|||
|
||||
struct cs_cmd_internal {
|
||||
static void call(
|
||||
cs_state &cs, cs_command *c, cs_value_r args, cs_value &ret
|
||||
cs_state &cs, cs_command *c, std::span<cs_value> args, cs_value &ret
|
||||
) {
|
||||
c->p_cb_cftv(cs, args, ret);
|
||||
}
|
||||
|
@ -424,13 +424,19 @@ static inline void callcommand(
|
|||
case 'C': {
|
||||
i = std::max(i + 1, numargs);
|
||||
cs_value tv{cs};
|
||||
tv.set_str(value_list_concat(cs, ostd::iter(args, args + i), " "));
|
||||
cs_cmd_internal::call(cs, id, cs_value_r(&tv, &tv + 1), res);
|
||||
tv.set_str(value_list_concat(
|
||||
cs, std::span{args, std::size_t(i)}, " "
|
||||
));
|
||||
cs_cmd_internal::call(
|
||||
cs, id, std::span<cs_value>(&tv, &tv + 1), res
|
||||
);
|
||||
return;
|
||||
}
|
||||
case 'V':
|
||||
i = std::max(i + 1, numargs);
|
||||
cs_cmd_internal::call(cs, id, ostd::iter(args, args + i), res);
|
||||
cs_cmd_internal::call(
|
||||
cs, id, std::span{args, std::size_t(i)}, res
|
||||
);
|
||||
return;
|
||||
case '1':
|
||||
case '2':
|
||||
|
@ -444,7 +450,9 @@ static inline void callcommand(
|
|||
}
|
||||
}
|
||||
++i;
|
||||
cs_cmd_internal::call(cs, id, cs_value_r(args, args + i), res);
|
||||
cs_cmd_internal::call(
|
||||
cs, id, std::span<cs_value>{args, std::size_t(i)}, res
|
||||
);
|
||||
}
|
||||
|
||||
static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result);
|
||||
|
@ -1300,9 +1308,9 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
|
|||
);
|
||||
int offset = numargs - id->get_num_args();
|
||||
result.force_none();
|
||||
cs_cmd_internal::call(cs, id, cs_value_r(
|
||||
&args[0] + offset, &args[0] + offset + id->get_num_args()
|
||||
), result);
|
||||
cs_cmd_internal::call(cs, id, std::span<cs_value>{
|
||||
&args[0] + offset, std::size_t(id->get_num_args())
|
||||
}, result);
|
||||
force_arg(result, op & CS_CODE_RET_MASK);
|
||||
numargs = offset;
|
||||
continue;
|
||||
|
@ -1315,11 +1323,12 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
|
|||
cs_command *id = static_cast<cs_command *>(
|
||||
cs.p_state->identmap[op >> 13]
|
||||
);
|
||||
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs;
|
||||
std::size_t callargs = (op >> 8) & 0x1F,
|
||||
offset = numargs - callargs;
|
||||
result.force_none();
|
||||
cs_cmd_internal::call(cs, id, ostd::iter(
|
||||
&args[offset], &args[offset + callargs]
|
||||
), result);
|
||||
cs_cmd_internal::call(
|
||||
cs, id, std::span{&args[offset], callargs}, result
|
||||
);
|
||||
force_arg(result, op & CS_CODE_RET_MASK);
|
||||
numargs = offset;
|
||||
continue;
|
||||
|
@ -1331,14 +1340,17 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
|
|||
cs_command *id = static_cast<cs_command *>(
|
||||
cs.p_state->identmap[op >> 13]
|
||||
);
|
||||
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs;
|
||||
std::size_t callargs = (op >> 8) & 0x1F,
|
||||
offset = numargs - callargs;
|
||||
result.force_none();
|
||||
{
|
||||
cs_value tv{cs};
|
||||
tv.set_str(value_list_concat(cs, ostd::iter(
|
||||
&args[offset], &args[offset + callargs]
|
||||
), " "));
|
||||
cs_cmd_internal::call(cs, id, cs_value_r(&tv, &tv + 1), result);
|
||||
tv.set_str(value_list_concat(cs, std::span{
|
||||
&args[offset], callargs
|
||||
}, " "));
|
||||
cs_cmd_internal::call(
|
||||
cs, id, std::span<cs_value>{&tv, 1}, result
|
||||
);
|
||||
}
|
||||
force_arg(result, op & CS_CODE_RET_MASK);
|
||||
numargs = offset;
|
||||
|
@ -1353,9 +1365,9 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
|
|||
case CS_CODE_CONC_W | CS_RET_STRING:
|
||||
case CS_CODE_CONC_W | CS_RET_FLOAT:
|
||||
case CS_CODE_CONC_W | CS_RET_INT: {
|
||||
int numconc = op >> 8;
|
||||
std::size_t numconc = op >> 8;
|
||||
auto buf = value_list_concat(
|
||||
cs, ostd::iter(&args[numargs - numconc], &args[numargs]),
|
||||
cs, std::span{&args[numargs - numconc], numconc},
|
||||
((op & CS_CODE_OP_MASK) == CS_CODE_CONC) ? " " : ""
|
||||
);
|
||||
numargs = numargs - numconc;
|
||||
|
@ -1369,9 +1381,9 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
|
|||
case CS_CODE_CONC_M | CS_RET_STRING:
|
||||
case CS_CODE_CONC_M | CS_RET_FLOAT:
|
||||
case CS_CODE_CONC_M | CS_RET_INT: {
|
||||
int numconc = op >> 8;
|
||||
std::size_t numconc = op >> 8;
|
||||
auto buf = value_list_concat(
|
||||
cs, ostd::iter(&args[numargs - numconc], &args[numargs])
|
||||
cs, std::span{&args[numargs - numconc], numconc}
|
||||
);
|
||||
numargs = numargs - numconc;
|
||||
result.set_str(buf);
|
||||
|
@ -1501,7 +1513,7 @@ noid:
|
|||
} else {
|
||||
cs.set_var_int_checked(
|
||||
static_cast<cs_ivar *>(id),
|
||||
ostd::iter(&args[offset], &args[offset + callargs])
|
||||
std::span{&args[offset], std::size_t(callargs)}
|
||||
);
|
||||
}
|
||||
numargs = offset - 1;
|
||||
|
@ -1588,7 +1600,7 @@ void cs_state::run(
|
|||
cs_run(*this, source, code, ret);
|
||||
}
|
||||
|
||||
void cs_state::run(cs_ident *id, cs_value_r args, cs_value &ret) {
|
||||
void cs_state::run(cs_ident *id, std::span<cs_value> args, cs_value &ret) {
|
||||
int nargs = int(args.size());
|
||||
ret.set_none();
|
||||
RunDepthRef level{*this}; /* incr and decr on scope exit */
|
||||
|
@ -1679,7 +1691,7 @@ cs_value cs_state::run(std::string_view code, std::string_view source) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
cs_value cs_state::run(cs_ident *id, cs_value_r args) {
|
||||
cs_value cs_state::run(cs_ident *id, std::span<cs_value> args) {
|
||||
cs_value ret{*this};
|
||||
run(id, args, ret);
|
||||
return ret;
|
||||
|
|
|
@ -518,16 +518,16 @@ OSTD_EXPORT bool cs_state::have_ident(std::string_view name) {
|
|||
return p_state->idents.find(name) != p_state->idents.end();
|
||||
}
|
||||
|
||||
OSTD_EXPORT cs_ident_r cs_state::get_idents() {
|
||||
return cs_ident_r(
|
||||
OSTD_EXPORT std::span<cs_ident *> cs_state::get_idents() {
|
||||
return std::span<cs_ident *>{
|
||||
p_state->identmap.data(),
|
||||
p_state->identmap.data() + p_state->identmap.size()
|
||||
);
|
||||
p_state->identmap.size()
|
||||
};
|
||||
}
|
||||
|
||||
OSTD_EXPORT cs_const_ident_r cs_state::get_idents() const {
|
||||
OSTD_EXPORT std::span<cs_ident const *> cs_state::get_idents() const {
|
||||
auto ptr = const_cast<cs_ident const **>(p_state->identmap.data());
|
||||
return cs_const_ident_r(ptr, ptr + p_state->identmap.size());
|
||||
return std::span<cs_ident const *>{ptr, p_state->identmap.size()};
|
||||
}
|
||||
|
||||
OSTD_EXPORT cs_ivar *cs_state::new_ivar(
|
||||
|
@ -844,7 +844,9 @@ OSTD_EXPORT void cs_state::set_var_int_checked(cs_ivar *iv, cs_int v) {
|
|||
iv->changed(*this);
|
||||
}
|
||||
|
||||
OSTD_EXPORT void cs_state::set_var_int_checked(cs_ivar *iv, cs_value_r args) {
|
||||
OSTD_EXPORT void cs_state::set_var_int_checked(
|
||||
cs_ivar *iv, std::span<cs_value> args
|
||||
) {
|
||||
cs_int v = args[0].force_int();
|
||||
if ((iv->get_flags() & CS_IDF_HEX) && (args.size() > 1)) {
|
||||
v = (v << 16) | (args[1].force_int() << 8);
|
||||
|
|
|
@ -31,7 +31,7 @@ struct cs_arg_val<std::string_view> {
|
|||
|
||||
template<typename T, typename F>
|
||||
static inline void cs_list_find(
|
||||
cs_state &cs, cs_value_r args, cs_value &res, F cmp
|
||||
cs_state &cs, std::span<cs_value> args, cs_value &res, F cmp
|
||||
) {
|
||||
cs_int n = 0, skip = args[2].get_int();
|
||||
T val = cs_arg_val<T>::get(args[1]);
|
||||
|
@ -53,7 +53,7 @@ notfound:
|
|||
|
||||
template<typename T, typename F>
|
||||
static inline void cs_list_assoc(
|
||||
cs_state &cs, cs_value_r args, cs_value &res, F cmp
|
||||
cs_state &cs, std::span<cs_value> args, cs_value &res, F cmp
|
||||
) {
|
||||
T val = cs_arg_val<T>::get(args[1]);
|
||||
for (cs_list_parse_state p{args[0].get_str()}; list_parse(p, cs);) {
|
||||
|
@ -115,7 +115,7 @@ int cs_list_includes(
|
|||
|
||||
template<bool PushList, bool Swap, typename F>
|
||||
static inline void cs_list_merge(
|
||||
cs_state &cs, cs_value_r args, cs_value &res, F cmp
|
||||
cs_state &cs, std::span<cs_value> args, cs_value &res, F cmp
|
||||
) {
|
||||
std::string_view list = args[0].get_str();
|
||||
std::string_view elems = args[1].get_str();
|
||||
|
|
|
@ -43,7 +43,7 @@ struct cs_math_noop {
|
|||
|
||||
template<typename T, typename F1, typename F2>
|
||||
static inline void cs_mathop(
|
||||
cs_value_r args, cs_value &res, T initval,
|
||||
std::span<cs_value> args, cs_value &res, T initval,
|
||||
F1 binop, F2 unop
|
||||
) {
|
||||
T val;
|
||||
|
@ -59,7 +59,7 @@ static inline void cs_mathop(
|
|||
}
|
||||
|
||||
template<typename T, typename F>
|
||||
static inline void cs_cmpop(cs_value_r args, cs_value &res, F cmp) {
|
||||
static inline void cs_cmpop(std::span<cs_value> args, cs_value &res, F cmp) {
|
||||
bool val;
|
||||
if (args.size() >= 2) {
|
||||
val = cmp(cs_math_val<T>::get(args[0]), cs_math_val<T>::get(args[1]));
|
||||
|
|
|
@ -7,7 +7,9 @@
|
|||
namespace cscript {
|
||||
|
||||
template<typename F>
|
||||
static inline void cs_strgcmp(cs_value_r args, cs_value &res, F cfunc) {
|
||||
static inline void cs_strgcmp(
|
||||
std::span<cs_value> args, cs_value &res, F cfunc
|
||||
) {
|
||||
bool val;
|
||||
if (args.size() >= 2) {
|
||||
val = cfunc(args[0].get_str(), args[1].get_str());
|
||||
|
|
Loading…
Reference in New Issue