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++

#ifndef jk_VALUE_HPP
#define jk_VALUE_HPP
#include "commons.hpp"
namespace jk
{
class Type;
class Function;
class Code;
class Value
{
public:
static std::shared_ptr<Value> make_nil();
static std::shared_ptr<Value> make_int(int val);
static std::shared_ptr<Value> make_bool(bool val);
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);
explicit Value() = default;
virtual ~Value() = default;
int as_int() const { return *m_int_val; }
int as_bool() const { return *m_bool_val; }
size_t as_ref() const { return *m_ref_val; }
std::shared_ptr<Function> as_function() const;
std::shared_ptr<Code> as_code() const;
std::weak_ptr<Type> type() const;
std::string string() const;
private:
std::shared_ptr<Type> m_type;
std::optional<int> m_int_val;
std::optional<bool> m_bool_val;
std::shared_ptr<Function> m_function_val;
std::shared_ptr<Code> m_code_val;
std::optional<size_t> m_ref_val;
};
}
#endif