pslang/libs/interpreter/include/pslang/interpreter/value.hpp

106 lines
2.1 KiB
C++

#pragma once
#include <pslang/types/type.hpp>
#include <pslang/ast/function.hpp>
#include <pslang/interpreter/value_fwd.hpp>
#include <cstdint>
#include <variant>
#include <iostream>
#include <vector>
#include <unordered_map>
namespace pslang::interpreter
{
struct unit_value
{};
template <typename T>
struct primitive_value_base
{
using native_type = T;
T value;
};
using bool_value = primitive_value_base<bool>;
using i8_value = primitive_value_base<std::int8_t>;
using u8_value = primitive_value_base<std::uint8_t>;
using i16_value = primitive_value_base<std::int16_t>;
using u16_value = primitive_value_base<std::uint16_t>;
using i32_value = primitive_value_base<std::int32_t>;
using u32_value = primitive_value_base<std::uint32_t>;
using i64_value = primitive_value_base<std::int64_t>;
using u64_value = primitive_value_base<std::uint64_t>;
using f16_value = primitive_value_base<types::half_float>;
using f32_value = primitive_value_base<float>;
using f64_value = primitive_value_base<double>;
using primitive_value_impl = std::variant<
bool_value,
i8_value,
u8_value,
i16_value,
u16_value,
i32_value,
u32_value,
i64_value,
u64_value,
f16_value,
f32_value,
f64_value
>;
struct primitive_value
: primitive_value_impl
{
using primitive_value_impl::primitive_value_impl;
};
struct array_value
{
// Can't infer type from elements in case of zero-sized array
types::type_ptr element_type;
std::vector<value_ptr> elements;
};
struct struct_value
{
types::type_ptr struct_type;
std::unordered_map<std::string, value_ptr> fields;
};
struct function_value
{
struct argument
{
std::string name;
types::type_ptr type;
};
std::vector<argument> arguments;
types::type_ptr return_type;
ast::statement_list_ptr statements;
};
using value_impl = std::variant<
unit_value,
primitive_value,
array_value,
struct_value,
function_value
>;
struct value
: value_impl
{
using value_impl::value_impl;
};
types::type type_of(value const & value);
void print(std::ostream & out, value const & value);
}