diff --git a/src/Value.cpp b/src/Value.cpp index 709f133..e7b51dc 100644 --- a/src/Value.cpp +++ b/src/Value.cpp @@ -130,6 +130,16 @@ namespace grino return value; } + /*static*/ + std::shared_ptr Value::make_user_data(Loc const& loc, + void* val) + { + auto value = std::make_shared(loc); + value->m_type = TYPE_PROGRAM; + value->m_user_data_val = val; + return value; + } + std::shared_ptr Value::as_program() const { return m_program_val; @@ -152,6 +162,7 @@ namespace grino case TYPE_FUNCTION: return ""; case TYPE_REF: return "&" + std::to_string(*m_ref_val); case TYPE_PROGRAM: return ""; + case TYPE_USER_DATA: return ""; case TYPE_MODULE: return "name() + ">"; case TYPE_ARRAY: { std::stringstream ss; @@ -188,6 +199,7 @@ namespace grino case TYPE_REF: return *m_ref_val == *other.m_ref_val; case TYPE_PROGRAM: return false; case TYPE_MODULE: return false; + case TYPE_USER_DATA: return m_user_data_val == other.m_user_data_val; default: std::cerr << "cannot compare equality with value " diff --git a/src/Value.hpp b/src/Value.hpp index 3472f7b..4c84828 100644 --- a/src/Value.hpp +++ b/src/Value.hpp @@ -44,6 +44,9 @@ namespace grino static std::shared_ptr make_module(Loc const& loc, std::shared_ptr val); + static std::shared_ptr make_user_data(Loc const& loc, + void* val); + explicit Value(Loc const& loc); virtual ~Value() = default; @@ -59,6 +62,7 @@ namespace grino val_array_t const& as_array() const { return *m_array_val; } std::string const& as_string() const { return *m_string_val; } std::shared_ptr as_module() const; + void* as_user_data() const { return m_user_data_val; } std::string string() const; bool equals(Value const& other) const; @@ -75,6 +79,7 @@ namespace grino std::optional m_array_val; std::optional m_string_val; std::shared_ptr m_module_val; + void* m_user_data_val = nullptr; }; } diff --git a/src/types.hpp b/src/types.hpp index 121d407..1113f0e 100644 --- a/src/types.hpp +++ b/src/types.hpp @@ -13,7 +13,8 @@ G(TYPE_FLOAT), \ G(TYPE_ARRAY), \ G(TYPE_STRING), \ - G(TYPE_MODULE) + G(TYPE_MODULE), \ + G(TYPE_USER_DATA) namespace grino