This repository has been archived on 2023-09-10. You can view files and clone it, but cannot push or open issues/pull-requests.
joko/lib/Value.hpp

49 lines
1.2 KiB
C++
Raw Permalink Normal View History

2023-09-09 13:09:43 +00:00
#ifndef jk_VALUE_HPP
#define jk_VALUE_HPP
#include "commons.hpp"
namespace jk
{
class Type;
2023-09-09 22:03:28 +00:00
class Function;
class Code;
2023-09-09 13:09:43 +00:00
class Value
{
public:
static std::shared_ptr<Value> make_nil();
static std::shared_ptr<Value> make_int(int val);
2023-09-10 12:39:23 +00:00
static std::shared_ptr<Value> make_bool(bool val);
2023-09-09 22:03:28 +00:00
static std::shared_ptr<Value> make_function(std::shared_ptr<Function>
val);
static std::shared_ptr<Value> make_code(std::shared_ptr<Code> val);
static std::shared_ptr<Value> make_ref(size_t val);
2023-09-09 13:09:43 +00:00
explicit Value() = default;
virtual ~Value() = default;
int as_int() const { return *m_int_val; }
2023-09-10 12:39:23 +00:00
int as_bool() const { return *m_bool_val; }
2023-09-09 22:03:28 +00:00
size_t as_ref() const { return *m_ref_val; }
std::shared_ptr<Function> as_function() const;
std::shared_ptr<Code> as_code() const;
2023-09-09 13:09:43 +00:00
std::weak_ptr<Type> type() const;
2023-09-09 22:03:28 +00:00
std::string string() const;
2023-09-09 13:09:43 +00:00
private:
std::shared_ptr<Type> m_type;
std::optional<int> m_int_val;
2023-09-10 12:39:23 +00:00
std::optional<bool> m_bool_val;
2023-09-09 22:03:28 +00:00
std::shared_ptr<Function> m_function_val;
std::shared_ptr<Code> m_code_val;
std::optional<size_t> m_ref_val;
2023-09-09 13:09:43 +00:00
};
}
#endif