libcubescript/src/cs_val.cc

373 lines
9.1 KiB
C++
Raw Normal View History

2017-06-20 21:21:39 +02:00
#include <cubescript/cubescript.hh>
#include "cs_vm.hh"
#include "cs_util.hh"
namespace cscript {
template<typename T>
struct stor_priv_t {
cs_shared_state *state;
T val;
};
2016-09-06 20:06:49 +02:00
template<typename T, typename U>
static inline T &csv_get(U &stor) {
2016-09-06 20:06:49 +02:00
/* ugly, but internal and unlikely to cause bugs */
return const_cast<T &>(reinterpret_cast<stor_priv_t<T> const &>(stor).val);
2016-09-06 20:06:49 +02:00
}
template<typename T>
2017-02-13 18:10:40 +01:00
static inline void csv_cleanup(cs_value_type tv, T &stor) {
switch (tv) {
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
2021-03-17 02:47:34 +01:00
reinterpret_cast<cs_strref *>(&stor)->~cs_strref();
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::CODE: {
2017-01-25 02:10:17 +01:00
uint32_t *bcode = csv_get<uint32_t *>(stor);
2021-03-18 20:55:14 +01:00
if (bcode[-1] == CS_CODE_START) {
2016-09-08 22:04:36 +02:00
delete[] &bcode[-1];
}
break;
}
default:
break;
}
}
cs_value::cs_value(cs_state &st): cs_value(*st.p_state) {}
cs_value::cs_value(cs_shared_state &st):
2021-03-18 20:55:14 +01:00
p_stor(), p_type(cs_value_type::NONE)
{
reinterpret_cast<stor_priv_t<void *> *>(&p_stor)->state = &st;
}
2017-02-13 18:10:40 +01:00
cs_value::~cs_value() {
csv_cleanup(p_type, p_stor);
2016-09-06 22:57:10 +02:00
}
cs_value::cs_value(cs_value const &v): cs_value(*v.state()) {
*this = v;
}
2017-02-13 18:10:40 +01:00
cs_value &cs_value::operator=(cs_value const &v) {
csv_cleanup(p_type, p_stor);
2021-03-18 20:55:14 +01:00
p_type = cs_value_type::NONE;
switch (v.get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
case cs_value_type::FLOAT:
case cs_value_type::IDENT:
p_type = v.p_type;
2016-09-06 22:57:10 +02:00
p_stor = v.p_stor;
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
p_type = cs_value_type::STRING;
2021-03-17 02:47:34 +01:00
new (&p_stor) cs_strref{
*reinterpret_cast<cs_strref const *>(&v.p_stor)
};
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::CODE:
set_code(cs_copy_code(v.get_code()));
break;
default:
break;
}
return *this;
}
2017-02-13 18:10:40 +01:00
cs_value_type cs_value::get_type() const {
return p_type;
}
2017-02-13 18:10:40 +01:00
void cs_value::set_int(cs_int val) {
csv_cleanup(p_type, p_stor);
2021-03-18 20:55:14 +01:00
p_type = cs_value_type::INT;
2017-02-13 18:10:40 +01:00
csv_get<cs_int>(p_stor) = val;
}
2017-02-13 18:10:40 +01:00
void cs_value::set_float(cs_float val) {
csv_cleanup(p_type, p_stor);
2021-03-18 20:55:14 +01:00
p_type = cs_value_type::FLOAT;
2017-02-13 18:10:40 +01:00
csv_get<cs_float>(p_stor) = val;
}
void cs_value::set_str(ostd::string_range val) {
2016-09-15 23:09:52 +02:00
csv_cleanup(p_type, p_stor);
new (&p_stor) cs_strref{*state(), val};
2021-03-18 20:55:14 +01:00
p_type = cs_value_type::STRING;
}
void cs_value::set_str(cs_strref const &val) {
csv_cleanup(p_type, p_stor);
new (&p_stor) cs_strref{val};
2021-03-18 20:55:14 +01:00
p_type = cs_value_type::STRING;
}
2021-03-18 20:55:14 +01:00
void cs_value::set_none() {
csv_cleanup(p_type, p_stor);
2021-03-18 20:55:14 +01:00
p_type = cs_value_type::NONE;
}
2017-02-13 18:10:40 +01:00
void cs_value::set_code(cs_bcode *val) {
csv_cleanup(p_type, p_stor);
2021-03-18 20:55:14 +01:00
p_type = cs_value_type::CODE;
2017-02-13 18:10:40 +01:00
csv_get<cs_bcode *>(p_stor) = val;
}
2017-02-13 18:10:40 +01:00
void cs_value::set_ident(cs_ident *val) {
csv_cleanup(p_type, p_stor);
2021-03-18 20:55:14 +01:00
p_type = cs_value_type::IDENT;
2017-02-13 18:10:40 +01:00
csv_get<cs_ident *>(p_stor) = val;
}
2021-03-18 20:55:14 +01:00
void cs_value::force_none() {
if (get_type() == cs_value_type::NONE) {
return;
}
2021-03-18 20:55:14 +01:00
set_none();
}
2017-02-13 18:10:40 +01:00
cs_float cs_value::force_float() {
cs_float rf = 0.0f;
switch (get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
2017-02-13 18:10:40 +01:00
rf = csv_get<cs_int>(p_stor);
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
2017-02-16 19:07:22 +01:00
rf = cs_parse_float(ostd::string_range(
2021-03-17 21:16:25 +01:00
*reinterpret_cast<cs_strref const *>(&p_stor)
2017-02-09 21:27:57 +01:00
));
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::FLOAT:
2017-02-13 18:10:40 +01:00
return csv_get<cs_float>(p_stor);
default:
break;
}
set_float(rf);
return rf;
}
2017-02-13 18:10:40 +01:00
cs_int cs_value::force_int() {
cs_int ri = 0;
switch (get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::FLOAT:
2017-02-13 18:10:40 +01:00
ri = csv_get<cs_float>(p_stor);
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
2017-02-16 19:07:22 +01:00
ri = cs_parse_int(ostd::string_range(
2021-03-17 21:16:25 +01:00
*reinterpret_cast<cs_strref const *>(&p_stor)
2017-02-09 21:27:57 +01:00
));
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
2017-02-13 18:10:40 +01:00
return csv_get<cs_int>(p_stor);
default:
break;
}
set_int(ri);
return ri;
}
2017-02-16 19:07:22 +01:00
ostd::string_range cs_value::force_str() {
2017-02-13 18:10:40 +01:00
cs_string rs;
switch (get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::FLOAT:
2017-02-13 18:10:40 +01:00
rs = floatstr(csv_get<cs_float>(p_stor));
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
2017-02-13 18:10:40 +01:00
rs = intstr(csv_get<cs_int>(p_stor));
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
2017-02-16 19:07:22 +01:00
return ostd::string_range(
2021-03-17 21:16:25 +01:00
*reinterpret_cast<cs_strref const *>(&p_stor)
2017-02-09 21:27:57 +01:00
);
default:
break;
}
set_str(rs);
2021-03-17 21:16:25 +01:00
return ostd::string_range(*reinterpret_cast<cs_strref const *>(&p_stor));
}
2017-02-13 18:10:40 +01:00
cs_int cs_value::get_int() const {
switch (get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::FLOAT:
2017-02-13 18:10:40 +01:00
return cs_int(csv_get<cs_float>(p_stor));
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
2017-02-13 18:10:40 +01:00
return csv_get<cs_int>(p_stor);
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
2017-02-16 19:07:22 +01:00
return cs_parse_int(ostd::string_range(
2021-03-17 21:16:25 +01:00
*reinterpret_cast<cs_strref const *>(&p_stor)
2017-02-09 21:27:57 +01:00
));
default:
break;
}
return 0;
}
2017-02-13 18:10:40 +01:00
cs_float cs_value::get_float() const {
switch (get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::FLOAT:
2017-02-13 18:10:40 +01:00
return csv_get<cs_float>(p_stor);
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
2017-02-13 18:10:40 +01:00
return cs_float(csv_get<cs_int>(p_stor));
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
2017-02-16 19:07:22 +01:00
return cs_parse_float(ostd::string_range(
2021-03-17 21:16:25 +01:00
*reinterpret_cast<cs_strref const *>(&p_stor)
2017-02-09 21:27:57 +01:00
));
default:
break;
}
return 0.0f;
}
2017-02-13 18:10:40 +01:00
cs_bcode *cs_value::get_code() const {
2021-03-18 20:55:14 +01:00
if (get_type() != cs_value_type::CODE) {
return nullptr;
}
2017-02-13 18:10:40 +01:00
return csv_get<cs_bcode *>(p_stor);
}
2017-02-13 18:10:40 +01:00
cs_ident *cs_value::get_ident() const {
2021-03-18 20:55:14 +01:00
if (get_type() != cs_value_type::IDENT) {
return nullptr;
}
2017-02-13 18:10:40 +01:00
return csv_get<cs_ident *>(p_stor);
}
cs_strref cs_value::get_str() const {
switch (get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
return *reinterpret_cast<cs_strref const *>(&p_stor);
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
return cs_strref{*state(), intstr(csv_get<cs_int>(p_stor))};
2021-03-18 20:55:14 +01:00
case cs_value_type::FLOAT:
return cs_strref{*state(), floatstr(csv_get<cs_float>(p_stor))};
default:
break;
}
return cs_strref{*state(), ""};
}
2017-02-13 18:10:40 +01:00
void cs_value::get_val(cs_value &r) const {
switch (get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
2021-03-17 02:47:34 +01:00
r = *this;
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
2017-02-13 18:10:40 +01:00
r.set_int(csv_get<cs_int>(p_stor));
break;
2021-03-18 20:55:14 +01:00
case cs_value_type::FLOAT:
2017-02-13 18:10:40 +01:00
r.set_float(csv_get<cs_float>(p_stor));
break;
default:
2021-03-18 20:55:14 +01:00
r.set_none();
break;
}
}
2017-02-13 18:10:40 +01:00
OSTD_EXPORT bool cs_code_is_empty(cs_bcode *code) {
if (!code) {
return true;
}
return (
2021-03-18 20:55:14 +01:00
*reinterpret_cast<uint32_t *>(code) & CS_CODE_OP_MASK
) == CS_CODE_EXIT;
}
2017-02-13 18:10:40 +01:00
bool cs_value::code_is_empty() const {
2021-03-18 20:55:14 +01:00
if (get_type() != cs_value_type::CODE) {
return true;
}
2017-02-13 18:10:40 +01:00
return cscript::cs_code_is_empty(csv_get<cs_bcode *>(p_stor));
}
2017-02-16 19:07:22 +01:00
static inline bool cs_get_bool(ostd::string_range s) {
if (s.empty()) {
return false;
}
2017-02-16 19:07:22 +01:00
ostd::string_range end = s;
2017-02-13 18:10:40 +01:00
cs_int ival = cs_parse_int(end, &end);
if (end.empty()) {
return !!ival;
}
end = s;
2017-02-13 18:10:40 +01:00
cs_float fval = cs_parse_float(end, &end);
if (end.empty()) {
return !!fval;
}
return true;
}
2017-02-13 18:10:40 +01:00
bool cs_value::get_bool() const {
switch (get_type()) {
2021-03-18 20:55:14 +01:00
case cs_value_type::FLOAT:
2017-02-13 18:10:40 +01:00
return csv_get<cs_float>(p_stor) != 0;
2021-03-18 20:55:14 +01:00
case cs_value_type::INT:
2017-02-13 18:10:40 +01:00
return csv_get<cs_int>(p_stor) != 0;
2021-03-18 20:55:14 +01:00
case cs_value_type::STRING:
2017-02-16 19:07:22 +01:00
return cs_get_bool(ostd::string_range(
2021-03-17 21:16:25 +01:00
*reinterpret_cast<cs_strref const *>(&p_stor)
2017-02-09 21:27:57 +01:00
));
default:
return false;
}
}
/* stacked value for easy stack management */
cs_stacked_value::cs_stacked_value(cs_state &cs, cs_ident *id):
cs_value(cs), p_a(nullptr), p_stack{cs}, p_pushed(false)
{
set_alias(id);
}
2017-02-13 18:10:40 +01:00
cs_stacked_value::~cs_stacked_value() {
pop();
2017-02-13 18:10:40 +01:00
static_cast<cs_value *>(this)->~cs_value();
}
2017-02-13 18:10:40 +01:00
cs_stacked_value &cs_stacked_value::operator=(cs_value const &v) {
*static_cast<cs_value *>(this) = v;
return *this;
}
2017-02-13 18:10:40 +01:00
cs_stacked_value &cs_stacked_value::operator=(cs_value &&v) {
*static_cast<cs_value *>(this) = std::move(v);
return *this;
}
2017-02-13 18:10:40 +01:00
bool cs_stacked_value::set_alias(cs_ident *id) {
if (!id || !id->is_alias()) {
return false;
2016-09-06 20:06:49 +02:00
}
2017-02-13 18:10:40 +01:00
p_a = static_cast<cs_alias *>(id);
return true;
}
2017-02-13 18:10:40 +01:00
cs_alias *cs_stacked_value::get_alias() const {
return p_a;
}
2017-02-13 18:10:40 +01:00
bool cs_stacked_value::has_alias() const {
return p_a != nullptr;
}
2017-02-13 18:10:40 +01:00
bool cs_stacked_value::push() {
if (!p_a) {
return false;
}
2018-04-27 23:53:55 +02:00
cs_alias_internal::push_arg(p_a, *this, p_stack);
p_pushed = true;
return true;
}
2017-02-13 18:10:40 +01:00
bool cs_stacked_value::pop() {
if (!p_pushed || !p_a) {
return false;
}
2018-04-27 23:53:55 +02:00
cs_alias_internal::pop_arg(p_a);
p_pushed = false;
return true;
2016-09-06 20:06:49 +02:00
}
} /* namespace cscript */