diff --git a/include/cubescript/cubescript/value.hh b/include/cubescript/cubescript/value.hh index 17e8587..32973a2 100644 --- a/include/cubescript/cubescript/value.hh +++ b/include/cubescript/cubescript/value.hh @@ -303,6 +303,24 @@ struct LIBCUBESCRIPT_EXPORT any_value { /** @brief Construct a value_type::NONE value. */ any_value(); + /** @brief Construct a value_type::INTEGER value. */ + any_value(integer_type val); + + /** @brief Construct a value_type::FLOAT value. */ + any_value(float_type val); + + /** @brief Construct a value_type::STRING value. */ + any_value(std::string_view val, state &cs); + + /** @brief Construct a value_type::STRING value. */ + any_value(string_ref const &val); + + /** @brief Construct a value_type::CODE value. */ + any_value(bcode_ref const &val); + + /** @brief Construct a value_type::IDENT value. */ + any_value(ident &val); + /** @brief Destroy the value. * * If holding a reference counted value, the refcount will be decreased @@ -328,6 +346,21 @@ struct LIBCUBESCRIPT_EXPORT any_value { */ any_value &operator=(any_value &&); + /** @brief Assign an integer to the value. */ + any_value &operator=(integer_type val); + + /** @brief Assign a float to the value. */ + any_value &operator=(float_type val); + + /** @brief Assign a string reference to the value. */ + any_value &operator=(string_ref const &val); + + /** @brief Assign a bytecode reference to the value. */ + any_value &operator=(bcode_ref const &val); + + /** @brief Assign an ident to the value. */ + any_value &operator=(ident &val); + /** @brief Get the type of the value. */ value_type type() const; diff --git a/src/cs_val.cc b/src/cs_val.cc index 2845f49..93953b3 100644 --- a/src/cs_val.cc +++ b/src/cs_val.cc @@ -83,6 +83,44 @@ any_value::any_value(): p_stor{}, p_type{value_type::NONE} {} +any_value::any_value(integer_type val): + p_stor{}, p_type{value_type::INTEGER} +{ + csv_get(&p_stor) = val; +} + +any_value::any_value(float_type val): + p_stor{}, p_type{value_type::FLOAT} +{ + csv_get(&p_stor) = val; +} + +any_value::any_value(std::string_view val, state &cs): + p_stor{}, p_type{value_type::STRING} +{ + csv_get(&p_stor) = state_p{cs}.ts().istate->strman->add(val); +} + +any_value::any_value(string_ref const &val): + p_stor{}, p_type{value_type::STRING} +{ + csv_get(&p_stor) = str_managed_ref(val.p_str); +} + +any_value::any_value(bcode_ref const &val): + p_stor{}, p_type{value_type::CODE} +{ + bcode *p = bcode_p{val}.get(); + bcode_addref(p->raw()); + csv_get(&p_stor) = p; +} + +any_value::any_value(ident &val): + p_stor{}, p_type{value_type::IDENT} +{ + csv_get(&p_stor) = &val; +} + any_value::~any_value() { csv_cleanup(p_type, &p_stor); } @@ -125,6 +163,31 @@ any_value &any_value::operator=(any_value &&v) { return *this; } +any_value &any_value::operator=(integer_type val) { + set_integer(val); + return *this; +} + +any_value &any_value::operator=(float_type val) { + set_float(val); + return *this; +} + +any_value &any_value::operator=(string_ref const &val) { + set_string(val); + return *this; +} + +any_value &any_value::operator=(bcode_ref const &val) { + set_code(val); + return *this; +} + +any_value &any_value::operator=(ident &val) { + set_ident(val); + return *this; +} + value_type any_value::type() const { return p_type; } diff --git a/tools/repl.cc b/tools/repl.cc index 5fd4886..5d0ad10 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -336,11 +336,9 @@ int main(int argc, char **argv) { if (nargs == 2) { nv = args[1]; } else if (nargs == 3) { - nv.set_integer( - (args[1].get_integer() << 8) | (args[2].get_integer() << 16) - ); + nv = (args[1].get_integer() << 8) | (args[2].get_integer() << 16); } else { - nv.set_integer( + nv = ( args[1].get_integer() | (args[2].get_integer() << 8) | (args[3].get_integer() << 16) );