clean up any_value APIs for consistency

master
Daniel Kolesa 2021-04-06 00:54:46 +02:00
parent 0b4f7573d2
commit e531ab3434
10 changed files with 353 additions and 320 deletions

View File

@ -74,7 +74,7 @@ private:
}; };
enum class value_type { enum class value_type {
NONE = 0, INT, FLOAT, STRING, CODE, IDENT NONE = 0, INTEGER, FLOAT, STRING, CODE, IDENT
}; };
struct LIBCUBESCRIPT_EXPORT any_value { struct LIBCUBESCRIPT_EXPORT any_value {
@ -92,27 +92,28 @@ struct LIBCUBESCRIPT_EXPORT any_value {
value_type get_type() const; value_type get_type() const;
void set_int(integer_type val); void set_integer(integer_type val);
void set_float(float_type val); void set_float(float_type val);
void set_str(std::string_view val); void set_string(std::string_view val);
void set_str(string_ref const &val); void set_string(string_ref const &val);
void set_none(); void set_none();
void set_code(bcode_ref const &val); void set_code(bcode_ref const &val);
void set_ident(ident *val); void set_ident(ident *val);
string_ref get_str() const; string_ref get_string() const;
integer_type get_int() const; integer_type get_integer() const;
float_type get_float() const; float_type get_float() const;
bcode_ref get_code() const; bcode_ref get_code() const;
ident *get_ident() const; ident *get_ident() const;
void get_val(any_value &r) const; any_value get_plain() const;
bool get_bool() const; bool get_bool() const;
void force_none(); void force_none();
void force_plain();
float_type force_float(); float_type force_float();
integer_type force_int(); integer_type force_integer();
std::string_view force_str(); std::string_view force_string();
bcode_ref force_code(state &cs); bcode_ref force_code(state &cs);
ident &force_ident(state &cs); ident &force_ident(state &cs);

View File

@ -40,7 +40,7 @@ alias_impl::alias_impl(
): ):
ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs} ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
{ {
p_initial.val_s.set_str(a); p_initial.val_s.set_string(a);
} }
alias_impl::alias_impl( alias_impl::alias_impl(
@ -48,13 +48,13 @@ alias_impl::alias_impl(
): ):
ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs} ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
{ {
p_initial.val_s.set_str(a); p_initial.val_s.set_string(a);
} }
alias_impl::alias_impl(state &cs, string_ref name, integer_type a, int fl): alias_impl::alias_impl(state &cs, string_ref name, integer_type a, int fl):
ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs} ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
{ {
p_initial.val_s.set_int(a); p_initial.val_s.set_integer(a);
} }
alias_impl::alias_impl(state &cs, string_ref name, float_type a, int fl): alias_impl::alias_impl(state &cs, string_ref name, float_type a, int fl):
@ -72,7 +72,7 @@ alias_impl::alias_impl(state &cs, string_ref name, int fl):
alias_impl::alias_impl(state &cs, string_ref name, any_value v, int fl): alias_impl::alias_impl(state &cs, string_ref name, any_value v, int fl):
ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs} ident_impl{ident_type::ALIAS, name, fl}, p_initial{cs}
{ {
p_initial.val_s = v; p_initial.val_s = v.get_plain();
} }
command_impl::command_impl( command_impl::command_impl(

View File

@ -131,10 +131,10 @@ state::state(alloc_func func, void *data) {
auto &cs, auto args, auto & auto &cs, auto args, auto &
) { ) {
auto *iv = args[0].get_ident()->get_ivar(); auto *iv = args[0].get_ident()->get_ivar();
if (args[2].get_int() <= 1) { if (args[2].get_integer() <= 1) {
std::printf("%s = %d\n", iv->get_name().data(), iv->get_value()); std::printf("%s = %d\n", iv->get_name().data(), iv->get_value());
} else { } else {
iv->set_value(cs, args[1].get_int()); iv->set_value(cs, args[1].get_integer());
} }
}); });
@ -142,7 +142,7 @@ state::state(alloc_func func, void *data) {
auto &cs, auto args, auto & auto &cs, auto args, auto &
) { ) {
auto *fv = args[0].get_ident()->get_fvar(); auto *fv = args[0].get_ident()->get_fvar();
if (args[2].get_int() <= 1) { if (args[2].get_integer() <= 1) {
auto val = fv->get_value(); auto val = fv->get_value();
if (std::floor(val) == val) { if (std::floor(val) == val) {
std::printf("%s = %.1f\n", fv->get_name().data(), val); std::printf("%s = %.1f\n", fv->get_name().data(), val);
@ -158,7 +158,7 @@ state::state(alloc_func func, void *data) {
auto &cs, auto args, auto & auto &cs, auto args, auto &
) { ) {
auto *sv = args[0].get_ident()->get_svar(); auto *sv = args[0].get_ident()->get_svar();
if (args[2].get_int() <= 1) { if (args[2].get_integer() <= 1) {
auto val = std::string_view{sv->get_value()}; auto val = std::string_view{sv->get_value()};
if (val.find('"') == val.npos) { if (val.find('"') == val.npos) {
std::printf("%s = \"%s\"\n", sv->get_name().data(), val.data()); std::printf("%s = \"%s\"\n", sv->get_name().data(), val.data());
@ -166,7 +166,7 @@ state::state(alloc_func func, void *data) {
std::printf("%s = [%s]\n", sv->get_name().data(), val.data()); std::printf("%s = [%s]\n", sv->get_name().data(), val.data());
} }
} else { } else {
sv->set_value(cs, args[1].get_str()); sv->set_value(cs, args[1].get_string());
} }
}); });
@ -197,13 +197,13 @@ state::state(alloc_func func, void *data) {
static_cast<command_impl *>(p)->p_type = ID_RESULT; static_cast<command_impl *>(p)->p_type = ID_RESULT;
p = &new_command("!", "t", [](auto &, auto args, auto &res) { p = &new_command("!", "t", [](auto &, auto args, auto &res) {
res.set_int(!args[0].get_bool()); res.set_integer(!args[0].get_bool());
}); });
static_cast<command_impl *>(p)->p_type = ID_NOT; static_cast<command_impl *>(p)->p_type = ID_NOT;
p = &new_command("&&", "E1V", [](auto &cs, auto args, auto &res) { p = &new_command("&&", "E1V", [](auto &cs, auto args, auto &res) {
if (args.empty()) { if (args.empty()) {
res.set_int(1); res.set_integer(1);
} else { } else {
for (size_t i = 0; i < args.size(); ++i) { for (size_t i = 0; i < args.size(); ++i) {
auto code = args[i].get_code(); auto code = args[i].get_code();
@ -222,7 +222,7 @@ state::state(alloc_func func, void *data) {
p = &new_command("||", "E1V", [](auto &cs, auto args, auto &res) { p = &new_command("||", "E1V", [](auto &cs, auto args, auto &res) {
if (args.empty()) { if (args.empty()) {
res.set_int(0); res.set_integer(0);
} else { } else {
for (size_t i = 0; i < args.size(); ++i) { for (size_t i = 0; i < args.size(); ++i) {
auto code = args[i].get_code(); auto code = args[i].get_code();
@ -339,7 +339,7 @@ LIBCUBESCRIPT_EXPORT void state::clear_override(ident &id) {
switch (id.get_type()) { switch (id.get_type()) {
case ident_type::ALIAS: { case ident_type::ALIAS: {
auto &ast = p_tstate->get_astack(static_cast<alias *>(&id)); auto &ast = p_tstate->get_astack(static_cast<alias *>(&id));
ast.node->val_s.set_str(""); ast.node->val_s.set_string("");
ast.node->code = bcode_ref{}; ast.node->code = bcode_ref{};
ast.flags &= ~IDENT_FLAG_OVERRIDDEN; ast.flags &= ~IDENT_FLAG_OVERRIDDEN;
return; return;

View File

@ -108,7 +108,7 @@ any_value &any_value::operator=(any_value const &v) {
csv_cleanup(p_type, &p_stor); csv_cleanup(p_type, &p_stor);
p_type = value_type::NONE; p_type = value_type::NONE;
switch (v.get_type()) { switch (v.get_type()) {
case value_type::INT: case value_type::INTEGER:
case value_type::FLOAT: case value_type::FLOAT:
case value_type::IDENT: case value_type::IDENT:
p_type = v.p_type; p_type = v.p_type;
@ -139,9 +139,9 @@ value_type any_value::get_type() const {
return p_type; return p_type;
} }
void any_value::set_int(integer_type val) { void any_value::set_integer(integer_type val) {
csv_cleanup(p_type, &p_stor); csv_cleanup(p_type, &p_stor);
p_type = value_type::INT; p_type = value_type::INTEGER;
csv_get<integer_type>(&p_stor) = val; csv_get<integer_type>(&p_stor) = val;
} }
@ -151,13 +151,13 @@ void any_value::set_float(float_type val) {
csv_get<float_type>(&p_stor) = val; csv_get<float_type>(&p_stor) = val;
} }
void any_value::set_str(std::string_view val) { void any_value::set_string(std::string_view val) {
csv_cleanup(p_type, &p_stor); csv_cleanup(p_type, &p_stor);
new (&p_stor) string_ref{get_state(), val}; new (&p_stor) string_ref{get_state(), val};
p_type = value_type::STRING; p_type = value_type::STRING;
} }
void any_value::set_str(string_ref const &val) { void any_value::set_string(string_ref const &val) {
csv_cleanup(p_type, &p_stor); csv_cleanup(p_type, &p_stor);
new (&p_stor) string_ref{val}; new (&p_stor) string_ref{val};
p_type = value_type::STRING; p_type = value_type::STRING;
@ -189,10 +189,22 @@ void any_value::force_none() {
set_none(); set_none();
} }
void any_value::force_plain() {
switch (get_type()) {
case value_type::FLOAT:
case value_type::INTEGER:
case value_type::STRING:
return;
default:
break;
}
force_none();
}
float_type any_value::force_float() { float_type any_value::force_float() {
float_type rf = 0.0f; float_type rf = 0.0f;
switch (get_type()) { switch (get_type()) {
case value_type::INT: case value_type::INTEGER:
rf = float_type(csv_get<integer_type>(&p_stor)); rf = float_type(csv_get<integer_type>(&p_stor));
break; break;
case value_type::STRING: case value_type::STRING:
@ -209,7 +221,7 @@ float_type any_value::force_float() {
return rf; return rf;
} }
integer_type any_value::force_int() { integer_type any_value::force_integer() {
integer_type ri = 0; integer_type ri = 0;
switch (get_type()) { switch (get_type()) {
case value_type::FLOAT: case value_type::FLOAT:
@ -220,23 +232,23 @@ integer_type any_value::force_int() {
*std::launder(reinterpret_cast<string_ref const *>(&p_stor)) *std::launder(reinterpret_cast<string_ref const *>(&p_stor))
); );
break; break;
case value_type::INT: case value_type::INTEGER:
return csv_get<integer_type>(&p_stor); return csv_get<integer_type>(&p_stor);
default: default:
break; break;
} }
set_int(ri); set_integer(ri);
return ri; return ri;
} }
std::string_view any_value::force_str() { std::string_view any_value::force_string() {
charbuf rs{get_state()}; charbuf rs{get_state()};
std::string_view str; std::string_view str;
switch (get_type()) { switch (get_type()) {
case value_type::FLOAT: case value_type::FLOAT:
str = floatstr(csv_get<float_type>(&p_stor), rs); str = floatstr(csv_get<float_type>(&p_stor), rs);
break; break;
case value_type::INT: case value_type::INTEGER:
str = intstr(csv_get<integer_type>(&p_stor), rs); str = intstr(csv_get<integer_type>(&p_stor), rs);
break; break;
case value_type::STRING: case value_type::STRING:
@ -245,7 +257,7 @@ std::string_view any_value::force_str() {
str = rs.str(); str = rs.str();
break; break;
} }
set_str(str); set_string(str);
return std::string_view(*std::launder( return std::string_view(*std::launder(
reinterpret_cast<string_ref const *>(&p_stor) reinterpret_cast<string_ref const *>(&p_stor)
)); ));
@ -260,7 +272,7 @@ bcode_ref any_value::force_code(state &cs) {
} }
codegen_state gs{state_p{cs}.ts()}; codegen_state gs{state_p{cs}.ts()};
gs.code.reserve(64); gs.code.reserve(64);
gs.gen_main(get_str()); gs.gen_main(get_string());
gs.done(); gs.done();
uint32_t *cbuf = bcode_alloc(state_p{cs}.ts().istate, gs.code.size()); uint32_t *cbuf = bcode_alloc(state_p{cs}.ts().istate, gs.code.size());
std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t)); std::memcpy(cbuf, gs.code.data(), gs.code.size() * sizeof(std::uint32_t));
@ -277,17 +289,17 @@ ident &any_value::force_ident(state &cs) {
break; break;
} }
auto &id = state_p{cs}.ts().istate->new_ident( auto &id = state_p{cs}.ts().istate->new_ident(
cs, get_str(), IDENT_FLAG_UNKNOWN cs, get_string(), IDENT_FLAG_UNKNOWN
); );
set_ident(&id); set_ident(&id);
return id; return id;
} }
integer_type any_value::get_int() const { integer_type any_value::get_integer() const {
switch (get_type()) { switch (get_type()) {
case value_type::FLOAT: case value_type::FLOAT:
return integer_type(csv_get<float_type>(&p_stor)); return integer_type(csv_get<float_type>(&p_stor));
case value_type::INT: case value_type::INTEGER:
return csv_get<integer_type>(&p_stor); return csv_get<integer_type>(&p_stor);
case value_type::STRING: case value_type::STRING:
return parse_int( return parse_int(
@ -303,7 +315,7 @@ float_type any_value::get_float() const {
switch (get_type()) { switch (get_type()) {
case value_type::FLOAT: case value_type::FLOAT:
return csv_get<float_type>(&p_stor); return csv_get<float_type>(&p_stor);
case value_type::INT: case value_type::INTEGER:
return float_type(csv_get<integer_type>(&p_stor)); return float_type(csv_get<integer_type>(&p_stor));
case value_type::STRING: case value_type::STRING:
return parse_float( return parse_float(
@ -329,11 +341,11 @@ ident *any_value::get_ident() const {
return csv_get<ident *>(&p_stor); return csv_get<ident *>(&p_stor);
} }
string_ref any_value::get_str() const { string_ref any_value::get_string() const {
switch (get_type()) { switch (get_type()) {
case value_type::STRING: case value_type::STRING:
return *std::launder(reinterpret_cast<string_ref const *>(&p_stor)); return *std::launder(reinterpret_cast<string_ref const *>(&p_stor));
case value_type::INT: { case value_type::INTEGER: {
charbuf rs{get_state()}; charbuf rs{get_state()};
return string_ref{ return string_ref{
get_state(), intstr(csv_get<integer_type>(&p_stor), rs) get_state(), intstr(csv_get<integer_type>(&p_stor), rs)
@ -351,28 +363,23 @@ string_ref any_value::get_str() const {
return string_ref{get_state(), ""}; return string_ref{get_state(), ""};
} }
void any_value::get_val(any_value &r) const { any_value any_value::get_plain() const {
switch (get_type()) { switch (get_type()) {
case value_type::STRING: case value_type::STRING:
r = *this; case value_type::INTEGER:
break;
case value_type::INT:
r.set_int(csv_get<integer_type>(&p_stor));
break;
case value_type::FLOAT: case value_type::FLOAT:
r.set_float(csv_get<float_type>(&p_stor)); return *this;
break;
default: default:
r.set_none();
break; break;
} }
return any_value{*get_state()};
} }
bool any_value::get_bool() const { bool any_value::get_bool() const {
switch (get_type()) { switch (get_type()) {
case value_type::FLOAT: case value_type::FLOAT:
return csv_get<float_type>(&p_stor) != 0; return csv_get<float_type>(&p_stor) != 0;
case value_type::INT: case value_type::INTEGER:
return csv_get<integer_type>(&p_stor) != 0; return csv_get<integer_type>(&p_stor) != 0;
case value_type::STRING: { case value_type::STRING: {
std::string_view s = *std::launder( std::string_view s = *std::launder(
@ -406,11 +413,11 @@ LIBCUBESCRIPT_EXPORT string_ref concat_values(
charbuf buf{cs}; charbuf buf{cs};
for (std::size_t i = 0; i < vals.size(); ++i) { for (std::size_t i = 0; i < vals.size(); ++i) {
switch (vals[i].get_type()) { switch (vals[i].get_type()) {
case value_type::INT: case value_type::INTEGER:
case value_type::FLOAT: case value_type::FLOAT:
case value_type::STRING: case value_type::STRING:
std::ranges::copy( std::ranges::copy(
any_value{vals[i]}.force_str(), std::back_inserter(buf) any_value{vals[i]}.force_string(), std::back_inserter(buf)
); );
break; break;
default: default:

View File

@ -28,12 +28,12 @@ static inline void force_arg(any_value &v, int type) {
switch (type) { switch (type) {
case BC_RET_STRING: case BC_RET_STRING:
if (v.get_type() != value_type::STRING) { if (v.get_type() != value_type::STRING) {
v.force_str(); v.force_string();
} }
break; break;
case BC_RET_INT: case BC_RET_INT:
if (v.get_type() != value_type::INT) { if (v.get_type() != value_type::INTEGER) {
v.force_int(); v.force_integer();
} }
break; break;
case BC_RET_FLOAT: case BC_RET_FLOAT:
@ -58,10 +58,10 @@ void exec_command(
if (rep) { if (rep) {
break; break;
} }
args[i].set_int(0); args[i].set_integer(0);
fakeargs++; fakeargs++;
} else { } else {
args[i].force_int(); args[i].force_integer();
} }
break; break;
case 'b': case 'b':
@ -69,10 +69,12 @@ void exec_command(
if (rep) { if (rep) {
break; break;
} }
args[i].set_int(std::numeric_limits<integer_type>::min()); args[i].set_integer(
std::numeric_limits<integer_type>::min()
);
fakeargs++; fakeargs++;
} else { } else {
args[i].force_int(); args[i].force_integer();
} }
break; break;
case 'f': case 'f':
@ -102,10 +104,10 @@ void exec_command(
if (rep) { if (rep) {
break; break;
} }
args[i].set_str(""); args[i].set_string("");
fakeargs++; fakeargs++;
} else { } else {
args[i].force_str(); args[i].force_string();
} }
break; break;
case 'T': case 'T':
@ -126,9 +128,9 @@ void exec_command(
args[i].set_none(); args[i].set_none();
fakeargs++; fakeargs++;
} else if (args[i].get_type() == value_type::STRING) { } else if (args[i].get_type() == value_type::STRING) {
auto str = std::string_view{args[i].get_str()}; auto str = std::string_view{args[i].get_string()};
if (str.empty()) { if (str.empty()) {
args[i].set_int(0); args[i].set_integer(0);
} else { } else {
args[i].force_code(*ts.pstate); args[i].force_code(*ts.pstate);
} }
@ -164,12 +166,12 @@ void exec_command(
break; break;
case 'N': case 'N':
i += 1; i += 1;
args[i].set_int(integer_type(lookup ? -1 : i - fakeargs)); args[i].set_integer(integer_type(lookup ? -1 : i - fakeargs));
break; break;
case 'C': { case 'C': {
i = std::max(i + 1, numargs); i = std::max(i + 1, numargs);
any_value tv{*ts.pstate}; any_value tv{*ts.pstate};
tv.set_str(concat_values( tv.set_string(concat_values(
*ts.pstate, std::span{args, std::size_t(i)}, " " *ts.pstate, std::span{args, std::size_t(i)}, " "
)); ));
static_cast<command_impl *>(id)->call( static_cast<command_impl *>(id)->call(
@ -198,6 +200,7 @@ void exec_command(
static_cast<command_impl *>(id)->call( static_cast<command_impl *>(id)->call(
ts, std::span<any_value>{args, std::size_t(i)}, res ts, std::span<any_value>{args, std::size_t(i)}, res
); );
res.force_plain();
} }
bool exec_alias( bool exec_alias(
@ -238,7 +241,7 @@ bool exec_alias(
if (!aast.node->code) { if (!aast.node->code) {
codegen_state gs{ts}; codegen_state gs{ts};
gs.code.reserve(64); gs.code.reserve(64);
gs.gen_main(aast.node->val_s.get_str()); gs.gen_main(aast.node->val_s.get_string());
/* i wish i could steal the memory somehow */ /* i wish i could steal the memory somehow */
uint32_t *code = bcode_alloc(ts.istate, gs.code.size()); uint32_t *code = bcode_alloc(ts.istate, gs.code.size());
memcpy(code, gs.code.data(), gs.code.size() * sizeof(uint32_t)); memcpy(code, gs.code.data(), gs.code.size() * sizeof(uint32_t));
@ -317,7 +320,7 @@ static inline int get_lookupu_type(
if (arg.get_type() != value_type::STRING) { if (arg.get_type() != value_type::STRING) {
return -2; /* default case */ return -2; /* default case */
} }
id = ts.pstate->get_ident(arg.get_str()); id = ts.pstate->get_ident(arg.get_string());
if (id) { if (id) {
switch(id->get_type()) { switch(id->get_type()) {
case ident_type::ALIAS: { case ident_type::ALIAS: {
@ -354,7 +357,7 @@ static inline int get_lookupu_type(
return ID_UNKNOWN; return ID_UNKNOWN;
} }
} }
throw error{*ts.pstate, "unknown alias lookup: %s", arg.get_str().data()}; throw error{*ts.pstate, "unknown alias lookup: %s", arg.get_string().data()};
} }
std::uint32_t *vm_exec( std::uint32_t *vm_exec(
@ -380,44 +383,44 @@ std::uint32_t *vm_exec(
result.set_none(); result.set_none();
continue; continue;
case BC_INST_NULL | BC_RET_STRING: case BC_INST_NULL | BC_RET_STRING:
result.set_str(""); result.set_string("");
continue; continue;
case BC_INST_NULL | BC_RET_INT: case BC_INST_NULL | BC_RET_INT:
result.set_int(0); result.set_integer(0);
continue; continue;
case BC_INST_NULL | BC_RET_FLOAT: case BC_INST_NULL | BC_RET_FLOAT:
result.set_float(0.0f); result.set_float(0.0f);
continue; continue;
case BC_INST_FALSE | BC_RET_STRING: case BC_INST_FALSE | BC_RET_STRING:
result.set_str("0"); result.set_string("0");
continue; continue;
case BC_INST_FALSE | BC_RET_NULL: case BC_INST_FALSE | BC_RET_NULL:
case BC_INST_FALSE | BC_RET_INT: case BC_INST_FALSE | BC_RET_INT:
result.set_int(0); result.set_integer(0);
continue; continue;
case BC_INST_FALSE | BC_RET_FLOAT: case BC_INST_FALSE | BC_RET_FLOAT:
result.set_float(0.0f); result.set_float(0.0f);
continue; continue;
case BC_INST_TRUE | BC_RET_STRING: case BC_INST_TRUE | BC_RET_STRING:
result.set_str("1"); result.set_string("1");
continue; continue;
case BC_INST_TRUE | BC_RET_NULL: case BC_INST_TRUE | BC_RET_NULL:
case BC_INST_TRUE | BC_RET_INT: case BC_INST_TRUE | BC_RET_INT:
result.set_int(1); result.set_integer(1);
continue; continue;
case BC_INST_TRUE | BC_RET_FLOAT: case BC_INST_TRUE | BC_RET_FLOAT:
result.set_float(1.0f); result.set_float(1.0f);
continue; continue;
case BC_INST_NOT | BC_RET_STRING: case BC_INST_NOT | BC_RET_STRING:
result.set_str(args.back().get_bool() ? "0" : "1"); result.set_string(args.back().get_bool() ? "0" : "1");
args.pop_back(); args.pop_back();
continue; continue;
case BC_INST_NOT | BC_RET_NULL: case BC_INST_NOT | BC_RET_NULL:
case BC_INST_NOT | BC_RET_INT: case BC_INST_NOT | BC_RET_INT:
result.set_int(!args.back().get_bool()); result.set_integer(!args.back().get_bool());
args.pop_back(); args.pop_back();
continue; continue;
case BC_INST_NOT | BC_RET_FLOAT: case BC_INST_NOT | BC_RET_FLOAT:
@ -464,10 +467,10 @@ std::uint32_t *vm_exec(
continue; continue;
case BC_INST_FORCE | BC_RET_STRING: case BC_INST_FORCE | BC_RET_STRING:
args.back().force_str(); args.back().force_string();
continue; continue;
case BC_INST_FORCE | BC_RET_INT: case BC_INST_FORCE | BC_RET_INT:
args.back().force_int(); args.back().force_integer();
continue; continue;
case BC_INST_FORCE | BC_RET_FLOAT: case BC_INST_FORCE | BC_RET_FLOAT:
args.back().force_float(); args.back().force_float();
@ -475,12 +478,12 @@ std::uint32_t *vm_exec(
case BC_INST_DUP | BC_RET_NULL: { case BC_INST_DUP | BC_RET_NULL: {
auto &v = args.back(); auto &v = args.back();
v.get_val(args.emplace_back(cs)); args.emplace_back(cs) = v.get_plain();
continue; continue;
} }
case BC_INST_DUP | BC_RET_INT: { case BC_INST_DUP | BC_RET_INT: {
auto &v = args.back(); auto &v = args.back();
args.emplace_back(cs).set_int(v.get_int()); args.emplace_back(cs).set_integer(v.get_integer());
continue; continue;
} }
case BC_INST_DUP | BC_RET_FLOAT: { case BC_INST_DUP | BC_RET_FLOAT: {
@ -492,13 +495,13 @@ std::uint32_t *vm_exec(
auto &v = args.back(); auto &v = args.back();
auto &nv = args.emplace_back(cs); auto &nv = args.emplace_back(cs);
nv = v; nv = v;
nv.force_str(); nv.force_string();
continue; continue;
} }
case BC_INST_VAL | BC_RET_STRING: { case BC_INST_VAL | BC_RET_STRING: {
std::uint32_t len = op >> 8; std::uint32_t len = op >> 8;
args.emplace_back(cs).set_str(std::string_view{ args.emplace_back(cs).set_string(std::string_view{
reinterpret_cast<char const *>(code), len reinterpret_cast<char const *>(code), len
}); });
code += len / sizeof(std::uint32_t) + 1; code += len / sizeof(std::uint32_t) + 1;
@ -511,7 +514,7 @@ std::uint32_t *vm_exec(
char((op >> 24) & 0xFF), '\0' char((op >> 24) & 0xFF), '\0'
}; };
/* gotta cast or r.size() == potentially 3 */ /* gotta cast or r.size() == potentially 3 */
args.emplace_back(cs).set_str(static_cast<char const *>(s)); args.emplace_back(cs).set_string(static_cast<char const *>(s));
continue; continue;
} }
case BC_INST_VAL | BC_RET_NULL: case BC_INST_VAL | BC_RET_NULL:
@ -519,13 +522,13 @@ std::uint32_t *vm_exec(
args.emplace_back(cs).set_none(); args.emplace_back(cs).set_none();
continue; continue;
case BC_INST_VAL | BC_RET_INT: case BC_INST_VAL | BC_RET_INT:
args.emplace_back(cs).set_int( args.emplace_back(cs).set_integer(
*reinterpret_cast<integer_type const *>(code) *reinterpret_cast<integer_type const *>(code)
); );
code += bc_store_size<integer_type>; code += bc_store_size<integer_type>;
continue; continue;
case BC_INST_VAL_INT | BC_RET_INT: case BC_INST_VAL_INT | BC_RET_INT:
args.emplace_back(cs).set_int(integer_type(op) >> 8); args.emplace_back(cs).set_integer(integer_type(op) >> 8);
continue; continue;
case BC_INST_VAL | BC_RET_FLOAT: case BC_INST_VAL | BC_RET_FLOAT:
args.emplace_back(cs).set_float( args.emplace_back(cs).set_float(
@ -686,10 +689,10 @@ std::uint32_t *vm_exec(
any_value &arg = args.back(); any_value &arg = args.back();
codegen_state gs{ts}; codegen_state gs{ts};
switch (arg.get_type()) { switch (arg.get_type()) {
case value_type::INT: case value_type::INTEGER:
gs.code.reserve(8); gs.code.reserve(8);
gs.code.push_back(BC_INST_START); gs.code.push_back(BC_INST_START);
gs.gen_int(arg.get_int()); gs.gen_int(arg.get_integer());
gs.code.push_back(BC_INST_RESULT); gs.code.push_back(BC_INST_RESULT);
gs.code.push_back(BC_INST_EXIT); gs.code.push_back(BC_INST_EXIT);
break; break;
@ -702,7 +705,7 @@ std::uint32_t *vm_exec(
break; break;
case value_type::STRING: case value_type::STRING:
gs.code.reserve(64); gs.code.reserve(64);
gs.gen_main(arg.get_str()); gs.gen_main(arg.get_string());
break; break;
default: default:
gs.code.reserve(8); gs.code.reserve(8);
@ -728,7 +731,7 @@ std::uint32_t *vm_exec(
any_value &arg = args.back(); any_value &arg = args.back();
switch (arg.get_type()) { switch (arg.get_type()) {
case value_type::STRING: { case value_type::STRING: {
std::string_view s = arg.get_str(); std::string_view s = arg.get_string();
if (!s.empty()) { if (!s.empty()) {
codegen_state gs{ts}; codegen_state gs{ts};
gs.code.reserve(64); gs.code.reserve(64);
@ -769,7 +772,7 @@ std::uint32_t *vm_exec(
ident *id = ts.istate->id_dummy; ident *id = ts.istate->id_dummy;
if (arg.get_type() == value_type::STRING) { if (arg.get_type() == value_type::STRING) {
id = &ts.istate->new_ident( id = &ts.istate->new_ident(
cs, arg.get_str(), IDENT_FLAG_UNKNOWN cs, arg.get_string(), IDENT_FLAG_UNKNOWN
); );
} }
alias *a = static_cast<alias *>(id); alias *a = static_cast<alias *>(id);
@ -788,21 +791,27 @@ std::uint32_t *vm_exec(
switch (get_lookupu_type(ts, arg, id, op, ast)) { switch (get_lookupu_type(ts, arg, id, op, ast)) {
case ID_ALIAS: case ID_ALIAS:
arg = ast->node->val_s; arg = ast->node->val_s;
arg.force_str(); arg.force_string();
continue; continue;
case ID_SVAR: case ID_SVAR:
arg.set_str(static_cast<string_var *>(id)->get_value()); arg.set_string(
static_cast<string_var *>(id)->get_value()
);
continue; continue;
case ID_IVAR: case ID_IVAR:
arg.set_int(static_cast<integer_var *>(id)->get_value()); arg.set_integer(
arg.force_str(); static_cast<integer_var *>(id)->get_value()
);
arg.force_string();
continue; continue;
case ID_FVAR: case ID_FVAR:
arg.set_float(static_cast<float_var *>(id)->get_value()); arg.set_float(
arg.force_str(); static_cast<float_var *>(id)->get_value()
);
arg.force_string();
continue; continue;
case ID_UNKNOWN: case ID_UNKNOWN:
arg.set_str(""); arg.set_string("");
continue; continue;
default: default:
continue; continue;
@ -813,11 +822,11 @@ std::uint32_t *vm_exec(
alias_stack *ast; alias_stack *ast;
alias *a = get_lookup_id(ts, op, ast); alias *a = get_lookup_id(ts, op, ast);
if (!a) { if (!a) {
args.emplace_back(cs).set_str(""); args.emplace_back(cs).set_string("");
} else { } else {
auto &v = args.emplace_back(cs); auto &v = args.emplace_back(cs);
v = ast->node->val_s; v = ast->node->val_s;
v.force_str(); v.force_string();
} }
continue; continue;
} }
@ -828,23 +837,25 @@ std::uint32_t *vm_exec(
any_value &arg = args.back(); any_value &arg = args.back();
switch (get_lookupu_type(ts, arg, id, op, ast)) { switch (get_lookupu_type(ts, arg, id, op, ast)) {
case ID_ALIAS: case ID_ALIAS:
arg.set_int(ast->node->val_s.get_int()); arg.set_integer(ast->node->val_s.get_integer());
continue; continue;
case ID_SVAR: case ID_SVAR:
arg.set_int(parse_int( arg.set_integer(parse_int(
static_cast<string_var *>(id)->get_value() static_cast<string_var *>(id)->get_value()
)); ));
continue; continue;
case ID_IVAR: case ID_IVAR:
arg.set_int(static_cast<integer_var *>(id)->get_value()); arg.set_integer(
continue; static_cast<integer_var *>(id)->get_value()
case ID_FVAR:
arg.set_int(
integer_type(static_cast<float_var *>(id)->get_value())
); );
continue; continue;
case ID_FVAR:
arg.set_integer(integer_type(
static_cast<float_var *>(id)->get_value()
));
continue;
case ID_UNKNOWN: case ID_UNKNOWN:
arg.set_int(0); arg.set_integer(0);
continue; continue;
default: default:
continue; continue;
@ -854,9 +865,11 @@ std::uint32_t *vm_exec(
alias_stack *ast; alias_stack *ast;
alias *a = get_lookup_id(ts, op, ast); alias *a = get_lookup_id(ts, op, ast);
if (!a) { if (!a) {
args.emplace_back(cs).set_int(0); args.emplace_back(cs).set_integer(0);
} else { } else {
args.emplace_back(cs).set_int(ast->node->val_s.get_int()); args.emplace_back(cs).set_integer(
ast->node->val_s.get_integer()
);
} }
continue; continue;
} }
@ -908,13 +921,17 @@ std::uint32_t *vm_exec(
any_value &arg = args.back(); any_value &arg = args.back();
switch (get_lookupu_type(ts, arg, id, op, ast)) { switch (get_lookupu_type(ts, arg, id, op, ast)) {
case ID_ALIAS: case ID_ALIAS:
ast->node->val_s.get_val(arg); arg = ast->node->val_s.get_plain();
continue; continue;
case ID_SVAR: case ID_SVAR:
arg.set_str(static_cast<string_var *>(id)->get_value()); arg.set_string(
static_cast<string_var *>(id)->get_value()
);
continue; continue;
case ID_IVAR: case ID_IVAR:
arg.set_int(static_cast<integer_var *>(id)->get_value()); arg.set_integer(
static_cast<integer_var *>(id)->get_value()
);
continue; continue;
case ID_FVAR: case ID_FVAR:
arg.set_float( arg.set_float(
@ -934,7 +951,7 @@ std::uint32_t *vm_exec(
if (!a) { if (!a) {
args.emplace_back(cs).set_none(); args.emplace_back(cs).set_none();
} else { } else {
ast->node->val_s.get_val(args.emplace_back(cs)); args.emplace_back(cs) = ast->node->val_s.get_plain();
} }
continue; continue;
} }
@ -953,19 +970,19 @@ std::uint32_t *vm_exec(
((op & BC_INST_OP_MASK) == BC_INST_CONC) ? " " : "" ((op & BC_INST_OP_MASK) == BC_INST_CONC) ? " " : ""
); );
args.resize(args.size() - numconc, any_value{cs}); args.resize(args.size() - numconc, any_value{cs});
args.emplace_back(cs).set_str(buf); args.emplace_back(cs).set_string(buf);
force_arg(args.back(), op & BC_INST_RET_MASK); force_arg(args.back(), op & BC_INST_RET_MASK);
continue; continue;
} }
case BC_INST_SVAR | BC_RET_STRING: case BC_INST_SVAR | BC_RET_STRING:
case BC_INST_SVAR | BC_RET_NULL: case BC_INST_SVAR | BC_RET_NULL:
args.emplace_back(cs).set_str(static_cast<string_var *>( args.emplace_back(cs).set_string(static_cast<string_var *>(
ts.istate->identmap[op >> 8] ts.istate->identmap[op >> 8]
)->get_value()); )->get_value());
continue; continue;
case BC_INST_SVAR | BC_RET_INT: case BC_INST_SVAR | BC_RET_INT:
args.emplace_back(cs).set_int(parse_int( args.emplace_back(cs).set_integer(parse_int(
static_cast<string_var *>( static_cast<string_var *>(
ts.istate->identmap[op >> 8] ts.istate->identmap[op >> 8]
)->get_value() )->get_value()
@ -981,16 +998,16 @@ std::uint32_t *vm_exec(
case BC_INST_IVAR | BC_RET_INT: case BC_INST_IVAR | BC_RET_INT:
case BC_INST_IVAR | BC_RET_NULL: case BC_INST_IVAR | BC_RET_NULL:
args.emplace_back(cs).set_int(static_cast<integer_var *>( args.emplace_back(cs).set_integer(static_cast<integer_var *>(
ts.istate->identmap[op >> 8] ts.istate->identmap[op >> 8]
)->get_value()); )->get_value());
continue; continue;
case BC_INST_IVAR | BC_RET_STRING: { case BC_INST_IVAR | BC_RET_STRING: {
auto &v = args.emplace_back(cs); auto &v = args.emplace_back(cs);
v.set_int(static_cast<integer_var *>( v.set_integer(static_cast<integer_var *>(
ts.istate->identmap[op >> 8] ts.istate->identmap[op >> 8]
)->get_value()); )->get_value());
v.force_str(); v.force_string();
continue; continue;
} }
case BC_INST_IVAR | BC_RET_FLOAT: case BC_INST_IVAR | BC_RET_FLOAT:
@ -1009,16 +1026,18 @@ std::uint32_t *vm_exec(
continue; continue;
case BC_INST_FVAR | BC_RET_STRING: { case BC_INST_FVAR | BC_RET_STRING: {
auto &v = args.emplace_back(cs); auto &v = args.emplace_back(cs);
v.set_int(integer_type(std::floor(static_cast<float_var *>( v.set_float(static_cast<float_var *>(
ts.istate->identmap[op >> 8] ts.istate->identmap[op >> 8]
)->get_value()))); )->get_value());
v.force_str(); v.force_string();
continue; continue;
} }
case BC_INST_FVAR | BC_RET_INT: case BC_INST_FVAR | BC_RET_INT:
args.emplace_back(cs).set_int(int(static_cast<float_var *>( args.emplace_back(cs).set_integer(
ts.istate->identmap[op >> 8] integer_type(std::floor(static_cast<float_var *>(
)->get_value())); ts.istate->identmap[op >> 8]
)->get_value()))
);
continue; continue;
case BC_INST_ALIAS: { case BC_INST_ALIAS: {
@ -1037,7 +1056,7 @@ std::uint32_t *vm_exec(
case BC_INST_ALIAS_U: { case BC_INST_ALIAS_U: {
auto v = std::move(args.back()); auto v = std::move(args.back());
args.pop_back(); args.pop_back();
cs.set_alias(args.back().get_str(), std::move(v)); cs.set_alias(args.back().get_string(), std::move(v));
args.pop_back(); args.pop_back();
continue; continue;
} }
@ -1082,7 +1101,7 @@ litval:
args.resize(offset - 1, any_value{cs}); args.resize(offset - 1, any_value{cs});
continue; continue;
} }
auto idn = idarg.get_str(); auto idn = idarg.get_string();
ident *id = cs.get_ident(idn); ident *id = cs.get_ident(idn);
if (!id) { if (!id) {
noid: noid:
@ -1248,7 +1267,7 @@ noid:
result.force_none(); result.force_none();
{ {
any_value tv{cs}; any_value tv{cs};
tv.set_str(concat_values(cs, std::span{ tv.set_string(concat_values(cs, std::span{
&args[offset], callargs &args[offset], callargs
}, " ")); }, " "));
id->call(ts, std::span<any_value>{&tv, 1}, result); id->call(ts, std::span<any_value>{&tv, 1}, result);

View File

@ -18,7 +18,7 @@ static inline void do_loop(
if (alias_local st{cs, &id}; st) { if (alias_local st{cs, &id}; st) {
any_value idv{cs}; any_value idv{cs};
for (integer_type i = 0; i < n; ++i) { for (integer_type i = 0; i < n; ++i) {
idv.set_int(offset + i * step); idv.set_integer(offset + i * step);
st.set(idv); st.set(idv);
if (cond && !cs.run(cond).get_bool()) { if (cond && !cs.run(cond).get_bool()) {
break; break;
@ -44,7 +44,7 @@ static inline void do_loop_conc(
charbuf s{cs}; charbuf s{cs};
any_value idv{cs}; any_value idv{cs};
for (integer_type i = 0; i < n; ++i) { for (integer_type i = 0; i < n; ++i) {
idv.set_int(offset + i * step); idv.set_integer(offset + i * step);
st.set(idv); st.set(idv);
any_value v{cs}; any_value v{cs};
switch (cs.run_loop(body, v)) { switch (cs.run_loop(body, v)) {
@ -58,23 +58,23 @@ static inline void do_loop_conc(
if (space && i) { if (space && i) {
s.push_back(' '); s.push_back(' ');
} }
s.append(v.get_str()); s.append(v.get_string());
} }
end: end:
res.set_str(s.str()); res.set_string(s.str());
} }
} }
void init_lib_base(state &gcs) { void init_lib_base(state &gcs) {
gcs.new_command("error", "s", [](auto &cs, auto args, auto &) { gcs.new_command("error", "s", [](auto &cs, auto args, auto &) {
throw error{cs, args[0].get_str()}; throw error{cs, args[0].get_string()};
}); });
gcs.new_command("pcall", "err", [](auto &cs, auto args, auto &ret) { gcs.new_command("pcall", "err", [](auto &cs, auto args, auto &ret) {
alias *cret = args[1].get_ident()->get_alias(); alias *cret = args[1].get_ident()->get_alias();
alias *css = args[2].get_ident()->get_alias(); alias *css = args[2].get_ident()->get_alias();
if (!cret || !css) { if (!cret || !css) {
ret.set_int(0); ret.set_integer(0);
return; return;
} }
any_value result{cs}, tback{cs}; any_value result{cs}, tback{cs};
@ -82,15 +82,15 @@ void init_lib_base(state &gcs) {
try { try {
result = cs.run(args[0].get_code()); result = cs.run(args[0].get_code());
} catch (error const &e) { } catch (error const &e) {
result.set_str(e.what()); result.set_string(e.what());
if (e.get_stack().get()) { if (e.get_stack().get()) {
charbuf buf{cs}; charbuf buf{cs};
print_stack(std::back_inserter(buf), e.get_stack()); print_stack(std::back_inserter(buf), e.get_stack());
tback.set_str(buf.str()); tback.set_string(buf.str());
} }
rc = false; rc = false;
} }
ret.set_int(rc); ret.set_integer(rc);
auto &ts = state_p{cs}.ts(); auto &ts = state_p{cs}.ts();
ts.get_astack(cret).set_alias(cret, ts, result); ts.get_astack(cret).set_alias(cret, ts, result);
ts.get_astack(css).set_alias(css, ts, tback); ts.get_astack(css).set_alias(css, ts, tback);
@ -119,11 +119,11 @@ void init_lib_base(state &gcs) {
}); });
gcs.new_command("case", "ite2V", [](auto &cs, auto args, auto &res) { gcs.new_command("case", "ite2V", [](auto &cs, auto args, auto &res) {
integer_type val = args[0].get_int(); integer_type val = args[0].get_integer();
for (size_t i = 1; (i + 1) < args.size(); i += 2) { for (size_t i = 1; (i + 1) < args.size(); i += 2) {
if ( if (
(args[i].get_type() == value_type::NONE) || (args[i].get_type() == value_type::NONE) ||
(args[i].get_int() == val) (args[i].get_integer() == val)
) { ) {
res = cs.run(args[i + 1].get_code()); res = cs.run(args[i + 1].get_code());
return; return;
@ -145,11 +145,11 @@ void init_lib_base(state &gcs) {
}); });
gcs.new_command("cases", "ste2V", [](auto &cs, auto args, auto &res) { gcs.new_command("cases", "ste2V", [](auto &cs, auto args, auto &res) {
string_ref val = args[0].get_str(); string_ref val = args[0].get_string();
for (size_t i = 1; (i + 1) < args.size(); i += 2) { for (size_t i = 1; (i + 1) < args.size(); i += 2) {
if ( if (
(args[i].get_type() == value_type::NONE) || (args[i].get_type() == value_type::NONE) ||
(args[i].get_str() == val) (args[i].get_string() == val)
) { ) {
res = cs.run(args[i + 1].get_code()); res = cs.run(args[i + 1].get_code());
return; return;
@ -171,57 +171,59 @@ void init_lib_base(state &gcs) {
gcs.new_command("loop", "rie", [](auto &cs, auto args, auto &) { gcs.new_command("loop", "rie", [](auto &cs, auto args, auto &) {
do_loop( do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), 1, nullptr, cs, *args[0].get_ident(), 0, args[1].get_integer(), 1, nullptr,
args[2].get_code() args[2].get_code()
); );
}); });
gcs.new_command("loop+", "riie", [](auto &cs, auto args, auto &) { gcs.new_command("loop+", "riie", [](auto &cs, auto args, auto &) {
do_loop( do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, cs, *args[0].get_ident(), args[1].get_integer(),
nullptr, args[3].get_code() args[2].get_integer(), 1, nullptr, args[3].get_code()
); );
}); });
gcs.new_command("loop*", "riie", [](auto &cs, auto args, auto &) { gcs.new_command("loop*", "riie", [](auto &cs, auto args, auto &) {
do_loop( do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), args[2].get_int(), cs, *args[0].get_ident(), 0, args[1].get_integer(),
nullptr, args[3].get_code() args[2].get_integer(), nullptr, args[3].get_code()
); );
}); });
gcs.new_command("loop+*", "riiie", [](auto &cs, auto args, auto &) { gcs.new_command("loop+*", "riiie", [](auto &cs, auto args, auto &) {
do_loop( do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), cs, *args[0].get_ident(), args[1].get_integer(),
args[2].get_int(), nullptr, args[4].get_code() args[3].get_integer(), args[2].get_integer(),
nullptr, args[4].get_code()
); );
}); });
gcs.new_command("loopwhile", "riee", [](auto &cs, auto args, auto &) { gcs.new_command("loopwhile", "riee", [](auto &cs, auto args, auto &) {
do_loop( do_loop(
cs, *args[0].get_ident(), 0, args[1].get_int(), 1, cs, *args[0].get_ident(), 0, args[1].get_integer(), 1,
args[2].get_code(), args[3].get_code() args[2].get_code(), args[3].get_code()
); );
}); });
gcs.new_command("loopwhile+", "riiee", [](auto &cs, auto args, auto &) { gcs.new_command("loopwhile+", "riiee", [](auto &cs, auto args, auto &) {
do_loop( do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[2].get_int(), 1, cs, *args[0].get_ident(), args[1].get_integer(),
args[3].get_code(), args[4].get_code() args[2].get_integer(), 1, args[3].get_code(), args[4].get_code()
); );
}); });
gcs.new_command("loopwhile*", "riiee", [](auto &cs, auto args, auto &) { gcs.new_command("loopwhile*", "riiee", [](auto &cs, auto args, auto &) {
do_loop( do_loop(
cs, *args[0].get_ident(), 0, args[2].get_int(), args[1].get_int(), cs, *args[0].get_ident(), 0, args[2].get_integer(),
args[3].get_code(), args[4].get_code() args[1].get_integer(), args[3].get_code(), args[4].get_code()
); );
}); });
gcs.new_command("loopwhile+*", "riiiee", [](auto &cs, auto args, auto &) { gcs.new_command("loopwhile+*", "riiiee", [](auto &cs, auto args, auto &) {
do_loop( do_loop(
cs, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), cs, *args[0].get_ident(), args[1].get_integer(),
args[2].get_int(), args[4].get_code(), args[5].get_code() args[3].get_integer(), args[2].get_integer(), args[4].get_code(),
args[5].get_code()
); );
}); });
@ -242,35 +244,36 @@ end:
gcs.new_command("loopconcat", "rie", [](auto &cs, auto args, auto &res) { gcs.new_command("loopconcat", "rie", [](auto &cs, auto args, auto &res) {
do_loop_conc( do_loop_conc(
cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1, cs, res, *args[0].get_ident(), 0, args[1].get_integer(), 1,
args[2].get_code(), true args[2].get_code(), true
); );
}); });
gcs.new_command("loopconcat+", "riie", [](auto &cs, auto args, auto &res) { gcs.new_command("loopconcat+", "riie", [](auto &cs, auto args, auto &res) {
do_loop_conc( do_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), cs, res, *args[0].get_ident(), args[1].get_integer(),
args[2].get_int(), 1, args[3].get_code(), true args[2].get_integer(), 1, args[3].get_code(), true
); );
}); });
gcs.new_command("loopconcat*", "riie", [](auto &cs, auto args, auto &res) { gcs.new_command("loopconcat*", "riie", [](auto &cs, auto args, auto &res) {
do_loop_conc( do_loop_conc(
cs, res, *args[0].get_ident(), 0, args[2].get_int(), cs, res, *args[0].get_ident(), 0, args[2].get_integer(),
args[1].get_int(), args[3].get_code(), true args[1].get_integer(), args[3].get_code(), true
); );
}); });
gcs.new_command("loopconcat+*", "riiie", [](auto &cs, auto args, auto &res) { gcs.new_command("loopconcat+*", "riiie", [](auto &cs, auto args, auto &res) {
do_loop_conc( do_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), cs, res, *args[0].get_ident(), args[1].get_integer(),
args[3].get_int(), args[2].get_int(), args[4].get_code(), true args[3].get_integer(), args[2].get_integer(),
args[4].get_code(), true
); );
}); });
gcs.new_command("loopconcatword", "rie", [](auto &cs, auto args, auto &res) { gcs.new_command("loopconcatword", "rie", [](auto &cs, auto args, auto &res) {
do_loop_conc( do_loop_conc(
cs, res, *args[0].get_ident(), 0, args[1].get_int(), 1, cs, res, *args[0].get_ident(), 0, args[1].get_integer(), 1,
args[2].get_code(), false args[2].get_code(), false
); );
}); });
@ -279,8 +282,8 @@ end:
auto &cs, auto args, auto &res auto &cs, auto args, auto &res
) { ) {
do_loop_conc( do_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), cs, res, *args[0].get_ident(), args[1].get_integer(),
args[2].get_int(), 1, args[3].get_code(), false args[2].get_integer(), 1, args[3].get_code(), false
); );
}); });
@ -288,8 +291,8 @@ end:
auto &cs, auto args, auto &res auto &cs, auto args, auto &res
) { ) {
do_loop_conc( do_loop_conc(
cs, res, *args[0].get_ident(), 0, args[2].get_int(), cs, res, *args[0].get_ident(), 0, args[2].get_integer(),
args[1].get_int(), args[3].get_code(), false args[1].get_integer(), args[3].get_code(), false
); );
}); });
@ -297,8 +300,9 @@ end:
auto &cs, auto args, auto &res auto &cs, auto args, auto &res
) { ) {
do_loop_conc( do_loop_conc(
cs, res, *args[0].get_ident(), args[1].get_int(), args[3].get_int(), cs, res, *args[0].get_ident(), args[1].get_integer(),
args[2].get_int(), args[4].get_code(), false args[3].get_integer(), args[2].get_integer(),
args[4].get_code(), false
); );
}); });
@ -313,21 +317,21 @@ end:
}); });
gcs.new_command("resetvar", "s", [](auto &cs, auto args, auto &) { gcs.new_command("resetvar", "s", [](auto &cs, auto args, auto &) {
cs.reset_var(args[0].get_str()); cs.reset_var(args[0].get_string());
}); });
gcs.new_command("alias", "st", [](auto &cs, auto args, auto &) { gcs.new_command("alias", "st", [](auto &cs, auto args, auto &) {
cs.set_alias(args[0].get_str(), args[1]); cs.set_alias(args[0].get_string(), args[1]);
}); });
gcs.new_command("identexists", "s", [](auto &cs, auto args, auto &res) { gcs.new_command("identexists", "s", [](auto &cs, auto args, auto &res) {
res.set_int(cs.have_ident(args[0].get_str())); res.set_integer(cs.have_ident(args[0].get_string()));
}); });
gcs.new_command("getalias", "s", [](auto &cs, auto args, auto &res) { gcs.new_command("getalias", "s", [](auto &cs, auto args, auto &res) {
auto *id = cs.get_alias(args[0].get_str()); auto *id = cs.get_alias(args[0].get_string());
if (id) { if (id) {
id->get_value(cs).get_val(res); res = id->get_value(cs);
} }
}); });
} }

View File

@ -14,7 +14,7 @@ struct arg_val;
template<> template<>
struct arg_val<integer_type> { struct arg_val<integer_type> {
static integer_type get(any_value &tv) { static integer_type get(any_value &tv) {
return tv.get_int(); return tv.get_integer();
} }
}; };
@ -28,7 +28,7 @@ struct arg_val<float_type> {
template<> template<>
struct arg_val<std::string_view> { struct arg_val<std::string_view> {
static std::string_view get(any_value &tv) { static std::string_view get(any_value &tv) {
return tv.get_str(); return tv.get_string();
} }
}; };
@ -36,11 +36,11 @@ template<typename T, typename F>
static inline void list_find( static inline void list_find(
state &cs, std::span<any_value> args, any_value &res, F cmp state &cs, std::span<any_value> args, any_value &res, F cmp
) { ) {
integer_type n = 0, skip = args[2].get_int(); integer_type n = 0, skip = args[2].get_integer();
T val = arg_val<T>::get(args[1]); T val = arg_val<T>::get(args[1]);
for (list_parser p{cs, args[0].get_str()}; p.parse(); ++n) { for (list_parser p{cs, args[0].get_string()}; p.parse(); ++n) {
if (cmp(p, val)) { if (cmp(p, val)) {
res.set_int(n); res.set_integer(n);
return; return;
} }
for (int i = 0; i < skip; ++i) { for (int i = 0; i < skip; ++i) {
@ -51,7 +51,7 @@ static inline void list_find(
} }
} }
notfound: notfound:
res.set_int(-1); res.set_integer(-1);
} }
template<typename T, typename F> template<typename T, typename F>
@ -59,10 +59,10 @@ static inline void list_assoc(
state &cs, std::span<any_value> args, any_value &res, F cmp state &cs, std::span<any_value> args, any_value &res, F cmp
) { ) {
T val = arg_val<T>::get(args[1]); T val = arg_val<T>::get(args[1]);
for (list_parser p{cs, args[0].get_str()}; p.parse();) { for (list_parser p{cs, args[0].get_string()}; p.parse();) {
if (cmp(p, val)) { if (cmp(p, val)) {
if (p.parse()) { if (p.parse()) {
res.set_str(p.get_item()); res.set_string(p.get_item());
} }
return; return;
} }
@ -81,7 +81,7 @@ static void loop_list_conc(
charbuf r{cs}; charbuf r{cs};
int n = 0; int n = 0;
for (list_parser p{cs, list}; p.parse(); ++n) { for (list_parser p{cs, list}; p.parse(); ++n) {
idv.set_str(p.get_item()); idv.set_string(p.get_item());
st.set(std::move(idv)); st.set(std::move(idv));
if (n && space) { if (n && space) {
r.push_back(' '); r.push_back(' ');
@ -95,10 +95,10 @@ static void loop_list_conc(
default: default:
break; break;
} }
r.append(v.get_str()); r.append(v.get_string());
} }
end: end:
res.set_str(r.str()); res.set_string(r.str());
} }
} }
@ -119,8 +119,8 @@ template<bool PushList, bool Swap, typename F>
static inline void list_merge( static inline void list_merge(
state &cs, std::span<any_value> args, any_value &res, F cmp state &cs, std::span<any_value> args, any_value &res, F cmp
) { ) {
std::string_view list = args[0].get_str(); std::string_view list = args[0].get_string();
std::string_view elems = args[1].get_str(); std::string_view elems = args[1].get_string();
charbuf buf{cs}; charbuf buf{cs};
if (PushList) { if (PushList) {
buf.append(list); buf.append(list);
@ -136,14 +136,14 @@ static inline void list_merge(
buf.append(p.get_quoted_item()); buf.append(p.get_quoted_item());
} }
} }
res.set_str(buf.str()); res.set_string(buf.str());
} }
static void init_lib_list_sort(state &cs); static void init_lib_list_sort(state &cs);
void init_lib_list(state &gcs) { void init_lib_list(state &gcs) {
gcs.new_command("listlen", "s", [](auto &cs, auto args, auto &res) { gcs.new_command("listlen", "s", [](auto &cs, auto args, auto &res) {
res.set_int(integer_type(list_parser{cs, args[0].get_str()}.count())); res.set_integer(integer_type(list_parser{cs, args[0].get_string()}.count()));
}); });
gcs.new_command("at", "si1V", [](auto &cs, auto args, auto &res) { gcs.new_command("at", "si1V", [](auto &cs, auto args, auto &res) {
@ -154,11 +154,11 @@ void init_lib_list(state &gcs) {
res = args[0]; res = args[0];
return; return;
} }
auto str = args[0].get_str(); auto str = args[0].get_string();
list_parser p{cs, str}; list_parser p{cs, str};
for (size_t i = 1; i < args.size(); ++i) { for (size_t i = 1; i < args.size(); ++i) {
p.set_input(str); p.set_input(str);
integer_type pos = args[i].get_int(); integer_type pos = args[i].get_integer();
for (; pos > 0; --pos) { for (; pos > 0; --pos) {
if (!p.parse()) { if (!p.parse()) {
break; break;
@ -168,18 +168,18 @@ void init_lib_list(state &gcs) {
p.set_input(""); p.set_input("");
} }
} }
res.set_str(p.get_item()); res.set_string(p.get_item());
}); });
gcs.new_command("sublist", "siiN", [](auto &cs, auto args, auto &res) { gcs.new_command("sublist", "siiN", [](auto &cs, auto args, auto &res) {
integer_type skip = args[1].get_int(), integer_type skip = args[1].get_integer(),
count = args[2].get_int(), count = args[2].get_integer(),
numargs = args[3].get_int(); numargs = args[3].get_integer();
integer_type offset = std::max(skip, integer_type(0)), integer_type offset = std::max(skip, integer_type(0)),
len = (numargs >= 3) ? std::max(count, integer_type(0)) : -1; len = (numargs >= 3) ? std::max(count, integer_type(0)) : -1;
list_parser p{cs, args[0].get_str()}; list_parser p{cs, args[0].get_string()};
for (integer_type i = 0; i < offset; ++i) { for (integer_type i = 0; i < offset; ++i) {
if (!p.parse()) break; if (!p.parse()) break;
} }
@ -187,7 +187,7 @@ void init_lib_list(state &gcs) {
if (offset > 0) { if (offset > 0) {
p.skip_until_item(); p.skip_until_item();
} }
res.set_str(p.get_input()); res.set_string(p.get_input());
return; return;
} }
@ -195,12 +195,12 @@ void init_lib_list(state &gcs) {
if (len > 0 && p.parse()) { if (len > 0 && p.parse()) {
while (--len > 0 && p.parse()); while (--len > 0 && p.parse());
} else { } else {
res.set_str(""); res.set_string("");
return; return;
} }
auto quote = p.get_quoted_item(); auto quote = p.get_quoted_item();
auto *qend = &quote[quote.size()]; auto *qend = &quote[quote.size()];
res.set_str(std::string_view{list, qend}); res.set_string(std::string_view{list, qend});
}); });
gcs.new_command("listfind", "rse", [](auto &cs, auto args, auto &res) { gcs.new_command("listfind", "rse", [](auto &cs, auto args, auto &res) {
@ -208,17 +208,17 @@ void init_lib_list(state &gcs) {
any_value idv{cs}; any_value idv{cs};
auto body = args[2].get_code(); auto body = args[2].get_code();
int n = -1; int n = -1;
for (list_parser p{cs, args[1].get_str()}; p.parse();) { for (list_parser p{cs, args[1].get_string()}; p.parse();) {
++n; ++n;
idv.set_str(p.get_raw_item()); idv.set_string(p.get_raw_item());
st.set(std::move(idv)); st.set(std::move(idv));
if (cs.run(body).get_bool()) { if (cs.run(body).get_bool()) {
res.set_int(integer_type(n)); res.set_integer(integer_type(n));
return; return;
} }
} }
} }
res.set_int(-1); res.set_integer(-1);
}); });
gcs.new_command("listassoc", "rse", [](auto &cs, auto args, auto &res) { gcs.new_command("listassoc", "rse", [](auto &cs, auto args, auto &res) {
@ -226,13 +226,13 @@ void init_lib_list(state &gcs) {
any_value idv{cs}; any_value idv{cs};
auto body = args[2].get_code(); auto body = args[2].get_code();
int n = -1; int n = -1;
for (list_parser p{cs, args[1].get_str()}; p.parse();) { for (list_parser p{cs, args[1].get_string()}; p.parse();) {
++n; ++n;
idv.set_str(p.get_raw_item()); idv.set_string(p.get_raw_item());
st.set(std::move(idv)); st.set(std::move(idv));
if (cs.run(body).get_bool()) { if (cs.run(body).get_bool()) {
if (p.parse()) { if (p.parse()) {
res.set_str(p.get_item()); res.set_string(p.get_item());
} }
break; break;
} }
@ -292,8 +292,8 @@ void init_lib_list(state &gcs) {
any_value idv{cs}; any_value idv{cs};
auto body = args[2].get_code(); auto body = args[2].get_code();
int n = 0; int n = 0;
for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { for (list_parser p{cs, args[1].get_string()}; p.parse(); ++n) {
idv.set_str(p.get_item()); idv.set_string(p.get_item());
st.set(std::move(idv)); st.set(std::move(idv));
switch (cs.run_loop(body)) { switch (cs.run_loop(body)) {
case loop_state::BREAK: case loop_state::BREAK:
@ -314,13 +314,13 @@ void init_lib_list(state &gcs) {
any_value idv{cs}; any_value idv{cs};
auto body = args[3].get_code(); auto body = args[3].get_code();
int n = 0; int n = 0;
for (list_parser p{cs, args[2].get_str()}; p.parse(); n += 2) { for (list_parser p{cs, args[2].get_string()}; p.parse(); n += 2) {
idv.set_str(p.get_item()); idv.set_string(p.get_item());
st1.set(std::move(idv)); st1.set(std::move(idv));
if (p.parse()) { if (p.parse()) {
idv.set_str(p.get_item()); idv.set_string(p.get_item());
} else { } else {
idv.set_str(""); idv.set_string("");
} }
st2.set(std::move(idv)); st2.set(std::move(idv));
switch (cs.run_loop(body)) { switch (cs.run_loop(body)) {
@ -342,19 +342,19 @@ void init_lib_list(state &gcs) {
any_value idv{cs}; any_value idv{cs};
auto body = args[4].get_code(); auto body = args[4].get_code();
int n = 0; int n = 0;
for (list_parser p{cs, args[3].get_str()}; p.parse(); n += 3) { for (list_parser p{cs, args[3].get_string()}; p.parse(); n += 3) {
idv.set_str(p.get_item()); idv.set_string(p.get_item());
st1.set(std::move(idv)); st1.set(std::move(idv));
if (p.parse()) { if (p.parse()) {
idv.set_str(p.get_item()); idv.set_string(p.get_item());
} else { } else {
idv.set_str(""); idv.set_string("");
} }
st2.set(std::move(idv)); st2.set(std::move(idv));
if (p.parse()) { if (p.parse()) {
idv.set_str(p.get_item()); idv.set_string(p.get_item());
} else { } else {
idv.set_str(""); idv.set_string("");
} }
st3.set(std::move(idv)); st3.set(std::move(idv));
switch (cs.run_loop(body)) { switch (cs.run_loop(body)) {
@ -368,7 +368,7 @@ void init_lib_list(state &gcs) {
gcs.new_command("looplistconcat", "rse", [](auto &cs, auto args, auto &res) { gcs.new_command("looplistconcat", "rse", [](auto &cs, auto args, auto &res) {
loop_list_conc( loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_str(), cs, res, args[0].get_ident(), args[1].get_string(),
args[2].get_code(), true args[2].get_code(), true
); );
}); });
@ -377,7 +377,7 @@ void init_lib_list(state &gcs) {
auto &cs, auto args, auto &res auto &cs, auto args, auto &res
) { ) {
loop_list_conc( loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_str(), cs, res, args[0].get_ident(), args[1].get_string(),
args[2].get_code(), false args[2].get_code(), false
); );
}); });
@ -388,8 +388,8 @@ void init_lib_list(state &gcs) {
auto body = args[2].get_code(); auto body = args[2].get_code();
charbuf r{cs}; charbuf r{cs};
int n = 0; int n = 0;
for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { for (list_parser p{cs, args[1].get_string()}; p.parse(); ++n) {
idv.set_str(p.get_raw_item()); idv.set_string(p.get_raw_item());
st.set(std::move(idv)); st.set(std::move(idv));
if (cs.run(body).get_bool()) { if (cs.run(body).get_bool()) {
if (r.size()) { if (r.size()) {
@ -398,7 +398,7 @@ void init_lib_list(state &gcs) {
r.append(p.get_quoted_item()); r.append(p.get_quoted_item());
} }
} }
res.set_str(r.str()); res.set_string(r.str());
} }
}); });
@ -407,21 +407,21 @@ void init_lib_list(state &gcs) {
any_value idv{cs}; any_value idv{cs};
auto body = args[2].get_code(); auto body = args[2].get_code();
int n = 0, r = 0; int n = 0, r = 0;
for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) { for (list_parser p{cs, args[1].get_string()}; p.parse(); ++n) {
idv.set_str(p.get_raw_item()); idv.set_string(p.get_raw_item());
st.set(std::move(idv)); st.set(std::move(idv));
if (cs.run(body).get_bool()) { if (cs.run(body).get_bool()) {
r++; r++;
} }
} }
res.set_int(r); res.set_integer(r);
} }
}); });
gcs.new_command("prettylist", "ss", [](auto &cs, auto args, auto &res) { gcs.new_command("prettylist", "ss", [](auto &cs, auto args, auto &res) {
charbuf buf{cs}; charbuf buf{cs};
std::string_view s = args[0].get_str(); std::string_view s = args[0].get_string();
std::string_view conj = args[1].get_str(); std::string_view conj = args[1].get_string();
list_parser p{cs, s}; list_parser p{cs, s};
size_t len = p.count(); size_t len = p.count();
size_t n = 0; size_t n = 0;
@ -443,12 +443,12 @@ void init_lib_list(state &gcs) {
buf.push_back(' '); buf.push_back(' ');
} }
} }
res.set_str(buf.str()); res.set_string(buf.str());
}); });
gcs.new_command("indexof", "ss", [](auto &cs, auto args, auto &res) { gcs.new_command("indexof", "ss", [](auto &cs, auto args, auto &res) {
res.set_int( res.set_integer(
list_includes(cs, args[0].get_str(), args[1].get_str()) list_includes(cs, args[0].get_string(), args[1].get_string())
); );
}); });
@ -463,10 +463,10 @@ void init_lib_list(state &gcs) {
}); });
gcs.new_command("listsplice", "ssii", [](auto &cs, auto args, auto &res) { gcs.new_command("listsplice", "ssii", [](auto &cs, auto args, auto &res) {
integer_type offset = std::max(args[2].get_int(), integer_type(0)); integer_type offset = std::max(args[2].get_integer(), integer_type(0));
integer_type len = std::max(args[3].get_int(), integer_type(0)); integer_type len = std::max(args[3].get_integer(), integer_type(0));
std::string_view s = args[0].get_str(); std::string_view s = args[0].get_string();
std::string_view vals = args[1].get_str(); std::string_view vals = args[1].get_string();
char const *list = s.data(); char const *list = s.data();
list_parser p{cs, s}; list_parser p{cs, s};
for (integer_type i = 0; i < offset; ++i) { for (integer_type i = 0; i < offset; ++i) {
@ -505,7 +505,7 @@ void init_lib_list(state &gcs) {
break; break;
} }
} }
res.set_str(buf.str()); res.set_string(buf.str());
}); });
init_lib_list_sort(gcs); init_lib_list_sort(gcs);
@ -523,9 +523,9 @@ struct ListSortFun {
bool operator()(ListSortItem const &xval, ListSortItem const &yval) { bool operator()(ListSortItem const &xval, ListSortItem const &yval) {
any_value v{cs}; any_value v{cs};
v.set_str(xval.str); v.set_string(xval.str);
xst.set(std::move(v)); xst.set(std::move(v));
v.set_str(yval.str); v.set_string(yval.str);
yst.set(std::move(v)); yst.set(std::move(v));
return cs.run(*body).get_bool(); return cs.run(*body).get_bool();
} }
@ -554,7 +554,7 @@ static void list_sort(
} }
if (items.empty()) { if (items.empty()) {
res.set_str(list); res.set_string(list);
return; return;
} }
@ -609,19 +609,19 @@ static void list_sort(
} }
sorted.append(item.quote); sorted.append(item.quote);
} }
res.set_str(sorted.str()); res.set_string(sorted.str());
} }
static void init_lib_list_sort(state &gcs) { static void init_lib_list_sort(state &gcs) {
gcs.new_command("sortlist", "srree", [](auto &cs, auto args, auto &res) { gcs.new_command("sortlist", "srree", [](auto &cs, auto args, auto &res) {
list_sort( list_sort(
cs, res, args[0].get_str(), args[1].get_ident(), cs, res, args[0].get_string(), args[1].get_ident(),
args[2].get_ident(), args[3].get_code(), args[4].get_code() args[2].get_ident(), args[3].get_code(), args[4].get_code()
); );
}); });
gcs.new_command("uniquelist", "srre", [](auto &cs, auto args, auto &res) { gcs.new_command("uniquelist", "srre", [](auto &cs, auto args, auto &res) {
list_sort( list_sort(
cs, res, args[0].get_str(), args[1].get_ident(), cs, res, args[0].get_string(), args[1].get_ident(),
args[2].get_ident(), nullptr, args[3].get_code() args[2].get_ident(), nullptr, args[3].get_code()
); );
}); });

View File

@ -18,10 +18,10 @@ struct math_val;
template<> template<>
struct math_val<integer_type> { struct math_val<integer_type> {
static integer_type get(any_value &tv) { static integer_type get(any_value &tv) {
return tv.get_int(); return tv.get_integer();
} }
static void set(any_value &res, integer_type val) { static void set(any_value &res, integer_type val) {
res.set_int(val); res.set_integer(val);
} }
}; };
@ -70,7 +70,7 @@ static inline void cmp_op(std::span<any_value> args, any_value &res, F cmp) {
} else { } else {
val = cmp(!args.empty() ? math_val<T>::get(args[0]) : T(0), T(0)); val = cmp(!args.empty() ? math_val<T>::get(args[0]) : T(0), T(0));
} }
res.set_int(integer_type(val)); res.set_integer(integer_type(val));
} }
void init_lib_math(state &cs) { void init_lib_math(state &cs) {
@ -115,18 +115,18 @@ void init_lib_math(state &cs) {
}); });
cs.new_command("min", "i1V", [](auto &, auto args, auto &res) { cs.new_command("min", "i1V", [](auto &, auto args, auto &res) {
integer_type v = (!args.empty() ? args[0].get_int() : 0); integer_type v = (!args.empty() ? args[0].get_integer() : 0);
for (size_t i = 1; i < args.size(); ++i) { for (size_t i = 1; i < args.size(); ++i) {
v = std::min(v, args[i].get_int()); v = std::min(v, args[i].get_integer());
} }
res.set_int(v); res.set_integer(v);
}); });
cs.new_command("max", "i1V", [](auto &, auto args, auto &res) { cs.new_command("max", "i1V", [](auto &, auto args, auto &res) {
integer_type v = (!args.empty() ? args[0].get_int() : 0); integer_type v = (!args.empty() ? args[0].get_integer() : 0);
for (size_t i = 1; i < args.size(); ++i) { for (size_t i = 1; i < args.size(); ++i) {
v = std::max(v, args[i].get_int()); v = std::max(v, args[i].get_integer());
} }
res.set_int(v); res.set_integer(v);
}); });
cs.new_command("minf", "f1V", [](auto &, auto args, auto &res) { cs.new_command("minf", "f1V", [](auto &, auto args, auto &res) {
float_type v = (!args.empty() ? args[0].get_float() : 0); float_type v = (!args.empty() ? args[0].get_float() : 0);
@ -144,7 +144,7 @@ void init_lib_math(state &cs) {
}); });
cs.new_command("abs", "i", [](auto &, auto args, auto &res) { cs.new_command("abs", "i", [](auto &, auto args, auto &res) {
res.set_int(std::abs(args[0].get_int())); res.set_integer(std::abs(args[0].get_integer()));
}); });
cs.new_command("absf", "f", [](auto &, auto args, auto &res) { cs.new_command("absf", "f", [](auto &, auto args, auto &res) {
res.set_float(std::abs(args[0].get_float())); res.set_float(std::abs(args[0].get_float()));
@ -208,38 +208,38 @@ void init_lib_math(state &cs) {
cs.new_command("^~", "i1V", [](auto &, auto args, auto &res) { cs.new_command("^~", "i1V", [](auto &, auto args, auto &res) {
integer_type val; integer_type val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = args[0].get_int() ^ ~args[1].get_int(); val = args[0].get_integer() ^ ~args[1].get_integer();
for (size_t i = 2; i < args.size(); ++i) { for (size_t i = 2; i < args.size(); ++i) {
val ^= ~args[i].get_int(); val ^= ~args[i].get_integer();
} }
} else { } else {
val = !args.empty() ? args[0].get_int() : 0; val = !args.empty() ? args[0].get_integer() : 0;
} }
res.set_int(val); res.set_integer(val);
}); });
cs.new_command("&~", "i1V", [](auto &, auto args, auto &res) { cs.new_command("&~", "i1V", [](auto &, auto args, auto &res) {
integer_type val; integer_type val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = args[0].get_int() & ~args[1].get_int(); val = args[0].get_integer() & ~args[1].get_integer();
for (size_t i = 2; i < args.size(); ++i) { for (size_t i = 2; i < args.size(); ++i) {
val &= ~args[i].get_int(); val &= ~args[i].get_integer();
} }
} else { } else {
val = !args.empty() ? args[0].get_int() : 0; val = !args.empty() ? args[0].get_integer() : 0;
} }
res.set_int(val); res.set_integer(val);
}); });
cs.new_command("|~", "i1V", [](auto &, auto args, auto &res) { cs.new_command("|~", "i1V", [](auto &, auto args, auto &res) {
integer_type val; integer_type val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = args[0].get_int() | ~args[1].get_int(); val = args[0].get_integer() | ~args[1].get_integer();
for (size_t i = 2; i < args.size(); ++i) { for (size_t i = 2; i < args.size(); ++i) {
val |= ~args[i].get_int(); val |= ~args[i].get_integer();
} }
} else { } else {
val = !args.empty() ? args[0].get_int() : 0; val = !args.empty() ? args[0].get_integer() : 0;
} }
res.set_int(val); res.set_integer(val);
}); });
cs.new_command("<<", "i1V", [](auto &, auto args, auto &res) { cs.new_command("<<", "i1V", [](auto &, auto args, auto &res) {

View File

@ -15,87 +15,87 @@ static inline void str_cmp_by(
) { ) {
bool val; bool val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = cfunc(args[0].get_str(), args[1].get_str()); val = cfunc(args[0].get_string(), args[1].get_string());
for (size_t i = 2; (i < args.size()) && val; ++i) { for (size_t i = 2; (i < args.size()) && val; ++i) {
val = cfunc(args[i - 1].get_str(), args[i].get_str()); val = cfunc(args[i - 1].get_string(), args[i].get_string());
} }
} else { } else {
val = cfunc( val = cfunc(
!args.empty() ? args[0].get_str() : std::string_view(), !args.empty() ? args[0].get_string() : std::string_view(),
std::string_view() std::string_view()
); );
} }
res.set_int(integer_type(val)); res.set_integer(integer_type(val));
} }
void init_lib_string(state &cs) { void init_lib_string(state &cs) {
cs.new_command("strstr", "ss", [](auto &, auto args, auto &res) { cs.new_command("strstr", "ss", [](auto &, auto args, auto &res) {
std::string_view a = args[0].get_str(), b = args[1].get_str(); std::string_view a = args[0].get_string(), b = args[1].get_string();
auto pos = a.find(b); auto pos = a.find(b);
if (pos == a.npos) { if (pos == a.npos) {
res.set_int(-1); res.set_integer(-1);
} else { } else {
res.set_int(integer_type(pos)); res.set_integer(integer_type(pos));
} }
}); });
cs.new_command("strlen", "s", [](auto &, auto args, auto &res) { cs.new_command("strlen", "s", [](auto &, auto args, auto &res) {
res.set_int(integer_type(args[0].get_str().size())); res.set_integer(integer_type(args[0].get_string().size()));
}); });
cs.new_command("strcode", "si", [](auto &, auto args, auto &res) { cs.new_command("strcode", "si", [](auto &, auto args, auto &res) {
std::string_view str = args[0].get_str(); std::string_view str = args[0].get_string();
integer_type i = args[1].get_int(); integer_type i = args[1].get_integer();
if (i >= integer_type(str.size())) { if (i >= integer_type(str.size())) {
res.set_int(0); res.set_integer(0);
} else { } else {
res.set_int(static_cast<unsigned char>(str[i])); res.set_integer(static_cast<unsigned char>(str[i]));
} }
}); });
cs.new_command("codestr", "i", [](auto &, auto args, auto &res) { cs.new_command("codestr", "i", [](auto &, auto args, auto &res) {
char const p[2] = { char(args[0].get_int()), '\0' }; char const p[2] = { char(args[0].get_integer()), '\0' };
res.set_str(std::string_view{static_cast<char const *>(p)}); res.set_string(std::string_view{static_cast<char const *>(p)});
}); });
cs.new_command("strlower", "s", [](auto &ccs, auto args, auto &res) { cs.new_command("strlower", "s", [](auto &ccs, auto args, auto &res) {
auto inps = std::string_view{args[0].get_str()}; auto inps = std::string_view{args[0].get_string()};
auto *ics = state_p{ccs}.ts().istate; auto *ics = state_p{ccs}.ts().istate;
auto *buf = ics->strman->alloc_buf(inps.size()); auto *buf = ics->strman->alloc_buf(inps.size());
for (std::size_t i = 0; i < inps.size(); ++i) { for (std::size_t i = 0; i < inps.size(); ++i) {
buf[i] = char(tolower(inps[i])); buf[i] = char(tolower(inps[i]));
} }
res.set_str(ics->strman->steal(buf)); res.set_string(ics->strman->steal(buf));
}); });
cs.new_command("strupper", "s", [](auto &ccs, auto args, auto &res) { cs.new_command("strupper", "s", [](auto &ccs, auto args, auto &res) {
auto inps = std::string_view{args[0].get_str()}; auto inps = std::string_view{args[0].get_string()};
auto *ics = state_p{ccs}.ts().istate; auto *ics = state_p{ccs}.ts().istate;
auto *buf = ics->strman->alloc_buf(inps.size()); auto *buf = ics->strman->alloc_buf(inps.size());
for (std::size_t i = 0; i < inps.size(); ++i) { for (std::size_t i = 0; i < inps.size(); ++i) {
buf[i] = char(toupper(inps[i])); buf[i] = char(toupper(inps[i]));
} }
res.set_str(ics->strman->steal(buf)); res.set_string(ics->strman->steal(buf));
}); });
cs.new_command("escape", "s", [](auto &ccs, auto args, auto &res) { cs.new_command("escape", "s", [](auto &ccs, auto args, auto &res) {
charbuf s{ccs}; charbuf s{ccs};
escape_string(std::back_inserter(s), args[0].get_str()); escape_string(std::back_inserter(s), args[0].get_string());
res.set_str(s.str()); res.set_string(s.str());
}); });
cs.new_command("unescape", "s", [](auto &ccs, auto args, auto &res) { cs.new_command("unescape", "s", [](auto &ccs, auto args, auto &res) {
charbuf s{ccs}; charbuf s{ccs};
unescape_string(std::back_inserter(s), args[0].get_str()); unescape_string(std::back_inserter(s), args[0].get_string());
res.set_str(s.str()); res.set_string(s.str());
}); });
cs.new_command("concat", "V", [](auto &ccs, auto args, auto &res) { cs.new_command("concat", "V", [](auto &ccs, auto args, auto &res) {
res.set_str(concat_values(ccs, args, " ")); res.set_string(concat_values(ccs, args, " "));
}); });
cs.new_command("concatword", "V", [](auto &ccs, auto args, auto &res) { cs.new_command("concatword", "V", [](auto &ccs, auto args, auto &res) {
res.set_str(concat_values(ccs, args)); res.set_string(concat_values(ccs, args));
}); });
cs.new_command("format", "V", [](auto &ccs, auto args, auto &res) { cs.new_command("format", "V", [](auto &ccs, auto args, auto &res) {
@ -103,7 +103,7 @@ void init_lib_string(state &cs) {
return; return;
} }
charbuf s{ccs}; charbuf s{ccs};
string_ref fs = args[0].get_str(); string_ref fs = args[0].get_string();
std::string_view f{fs}; std::string_view f{fs};
for (auto it = f.begin(); it != f.end(); ++it) { for (auto it = f.begin(); it != f.end(); ++it) {
char c = *it; char c = *it;
@ -114,7 +114,7 @@ void init_lib_string(state &cs) {
if ((ic >= '1') && (ic <= '9')) { if ((ic >= '1') && (ic <= '9')) {
int i = ic - '0'; int i = ic - '0';
if (std::size_t(i) < args.size()) { if (std::size_t(i) < args.size()) {
s.append(args[i].get_str()); s.append(args[i].get_string());
} }
} else { } else {
s.push_back(ic); s.push_back(ic);
@ -123,14 +123,14 @@ void init_lib_string(state &cs) {
s.push_back(c); s.push_back(c);
} }
} }
res.set_str(s.str()); res.set_string(s.str());
}); });
cs.new_command("tohex", "ii", [](auto &ccs, auto args, auto &res) { cs.new_command("tohex", "ii", [](auto &ccs, auto args, auto &res) {
char buf[32]; char buf[32];
/* use long long as the largest signed integer type */ /* use long long as the largest signed integer type */
auto val = static_cast<long long>(args[0].get_int()); auto val = static_cast<long long>(args[0].get_integer());
int prec = std::max(int(args[1].get_int()), 1); int prec = std::max(int(args[1].get_integer()), 1);
int n = snprintf(buf, sizeof(buf), "0x%.*llX", prec, val); int n = snprintf(buf, sizeof(buf), "0x%.*llX", prec, val);
if (n >= int(sizeof(buf))) { if (n >= int(sizeof(buf))) {
charbuf s{ccs}; charbuf s{ccs};
@ -138,11 +138,11 @@ void init_lib_string(state &cs) {
s.data()[0] = '\0'; s.data()[0] = '\0';
int nn = snprintf(s.data(), n + 1, "0x%.*llX", prec, val); int nn = snprintf(s.data(), n + 1, "0x%.*llX", prec, val);
if ((nn > 0) && (nn <= n)) { if ((nn > 0) && (nn <= n)) {
res.set_str(std::string_view{s.data(), std::size_t(nn)}); res.set_string(std::string_view{s.data(), std::size_t(nn)});
return; return;
} }
} else if (n > 0) { } else if (n > 0) {
res.set_str(static_cast<char const *>(buf)); res.set_string(static_cast<char const *>(buf));
return; return;
} }
/* should pretty much be unreachable */ /* should pretty much be unreachable */
@ -150,11 +150,12 @@ void init_lib_string(state &cs) {
}); });
cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) { cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) {
std::string_view s = args[0].get_str(); std::string_view s = args[0].get_string();
integer_type start = args[1].get_int(), count = args[2].get_int(); auto start = args[1].get_integer(), count = args[2].get_integer();
integer_type numargs = args[3].get_int(); auto numargs = args[3].get_integer();
integer_type len = integer_type(s.size()), offset = std::clamp(start, integer_type(0), len); auto len = integer_type(s.size());
res.set_str(std::string_view{ auto offset = std::clamp(start, integer_type(0), len);
res.set_string(std::string_view{
&s[offset], &s[offset],
((numargs >= 3) ((numargs >= 3)
? size_t(std::clamp(count, integer_type(0), len - offset)) ? size_t(std::clamp(count, integer_type(0), len - offset))
@ -185,15 +186,15 @@ void init_lib_string(state &cs) {
}); });
cs.new_command("strreplace", "ssss", [](auto &ccs, auto args, auto &res) { cs.new_command("strreplace", "ssss", [](auto &ccs, auto args, auto &res) {
std::string_view s = args[0].get_str(); std::string_view s = args[0].get_string();
std::string_view oldval = args[1].get_str(), std::string_view oldval = args[1].get_string(),
newval = args[2].get_str(), newval = args[2].get_string(),
newval2 = args[3].get_str(); newval2 = args[3].get_string();
if (newval2.empty()) { if (newval2.empty()) {
newval2 = newval; newval2 = newval;
} }
if (oldval.empty()) { if (oldval.empty()) {
res.set_str(s); res.set_string(s);
return; return;
} }
charbuf buf{ccs}; charbuf buf{ccs};
@ -201,7 +202,7 @@ void init_lib_string(state &cs) {
auto p = s.find(oldval); auto p = s.find(oldval);
if (p == s.npos) { if (p == s.npos) {
buf.append(s); buf.append(s);
res.set_str(s); res.set_string(s);
return; return;
} }
buf.append(s.substr(0, p)); buf.append(s.substr(0, p));
@ -214,10 +215,10 @@ void init_lib_string(state &cs) {
}); });
cs.new_command("strsplice", "ssii", [](auto &ccs, auto args, auto &res) { cs.new_command("strsplice", "ssii", [](auto &ccs, auto args, auto &res) {
std::string_view s = args[0].get_str(); std::string_view s = args[0].get_string();
std::string_view vals = args[1].get_str(); std::string_view vals = args[1].get_string();
integer_type skip = args[2].get_int(), integer_type skip = args[2].get_integer(),
count = args[3].get_int(); count = args[3].get_integer();
integer_type offset = std::clamp(skip, integer_type(0), integer_type(s.size())), 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); len = std::clamp(count, integer_type(0), integer_type(s.size()) - offset);
charbuf p{ccs}; charbuf p{ccs};
@ -229,7 +230,7 @@ void init_lib_string(state &cs) {
if ((offset + len) < integer_type(s.size())) { if ((offset + len) < integer_type(s.size())) {
p.append(s.substr(offset + len, s.size() - offset - len)); p.append(s.substr(offset + len, s.size() - offset - len));
} }
res.set_str(p.str()); res.set_string(p.str());
}); });
} }

View File

@ -271,7 +271,7 @@ static bool do_call(cs::state &cs, std::string_view line, bool file = false) {
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
scs = nullptr; 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()); std::printf("%s\n", std::string_view{ret.get_string()}.data());
} }
return false; return false;
} }
@ -326,7 +326,7 @@ int main(int argc, char **argv) {
*/ */
gcs.new_command("//ivar", "$iiiN", [](auto &css, auto args, auto &) { gcs.new_command("//ivar", "$iiiN", [](auto &css, auto args, auto &) {
auto *iv = args[0].get_ident()->get_ivar(); auto *iv = args[0].get_ident()->get_ivar();
auto nargs = args[4].get_int(); auto nargs = args[4].get_integer();
if (nargs <= 1) { if (nargs <= 1) {
auto val = iv->get_value(); auto val = iv->get_value();
if ((val >= 0) && (val < 0xFFFFFF)) { if ((val >= 0) && (val < 0xFFFFFF)) {
@ -341,15 +341,16 @@ int main(int argc, char **argv) {
return; return;
} }
if (nargs == 2) { if (nargs == 2) {
iv->set_value(css, args[1].get_int()); iv->set_value(css, args[1].get_integer());
} else if (nargs == 3) { } else if (nargs == 3) {
iv->set_value( iv->set_value(
css, (args[1].get_int() << 8) | (args[2].get_int() << 16) css, (args[1].get_integer() << 8) |
(args[2].get_integer() << 16)
); );
} else { } else {
iv->set_value( iv->set_value(
css, args[1].get_int() | (args[2].get_int() << 8) | css, args[1].get_integer() | (args[2].get_integer() << 8) |
(args[3].get_int() << 16) (args[3].get_integer() << 16)
); );
} }
}); });
@ -361,7 +362,7 @@ int main(int argc, char **argv) {
}); });
gcs.new_command("exec", "s", [](auto &css, auto args, auto &) { gcs.new_command("exec", "s", [](auto &css, auto args, auto &) {
auto file = args[0].get_str(); auto file = args[0].get_string();
cs::any_value val{css}; cs::any_value val{css};
bool ret = do_run_file(css, file, val); bool ret = do_run_file(css, file, val);
if (!ret) { if (!ret) {
@ -372,7 +373,7 @@ int main(int argc, char **argv) {
}); });
gcs.new_command("echo", "C", [](auto &, auto args, auto &) { gcs.new_command("echo", "C", [](auto &, auto args, auto &) {
std::printf("%s\n", std::string_view{args[0].get_str()}.data()); std::printf("%s\n", std::string_view{args[0].get_string()}.data());
}); });
int firstarg = 0; int firstarg = 0;