Add interpreter::value_visitor
This commit is contained in:
parent
8c07e1950b
commit
83b4d23ab2
2 changed files with 165 additions and 105 deletions
|
|
@ -0,0 +1,71 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <pslang/interpreter/value.hpp>
|
||||||
|
|
||||||
|
namespace pslang::interpreter
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename Visitor>
|
||||||
|
struct const_value_visitor_helper
|
||||||
|
{
|
||||||
|
Visitor & visitor;
|
||||||
|
|
||||||
|
template <typename Value>
|
||||||
|
auto operator()(Value const & value)
|
||||||
|
{
|
||||||
|
return visitor(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator()(value const & value)
|
||||||
|
{
|
||||||
|
return std::visit(*this, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator()(primitive_value const & value)
|
||||||
|
{
|
||||||
|
return std::visit(*this, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Visitor>
|
||||||
|
struct value_visitor_helper
|
||||||
|
{
|
||||||
|
Visitor & visitor;
|
||||||
|
|
||||||
|
template <typename Value>
|
||||||
|
auto operator()(Value & value)
|
||||||
|
{
|
||||||
|
return visitor(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator()(value & value)
|
||||||
|
{
|
||||||
|
return std::visit(*this, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator()(primitive_value & value)
|
||||||
|
{
|
||||||
|
return std::visit(*this, value);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Visitor>
|
||||||
|
auto apply(Visitor && visitor, value const & value)
|
||||||
|
{
|
||||||
|
detail::const_value_visitor_helper<Visitor> helper{visitor};
|
||||||
|
return helper(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Visitor>
|
||||||
|
auto apply(Visitor && visitor, value & value)
|
||||||
|
{
|
||||||
|
detail::value_visitor_helper<Visitor> helper{visitor};
|
||||||
|
return helper(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include <pslang/interpreter/value.hpp>
|
#include <pslang/interpreter/value.hpp>
|
||||||
|
#include <pslang/interpreter/value_visitor.hpp>
|
||||||
#include <pslang/types/print.hpp>
|
#include <pslang/types/print.hpp>
|
||||||
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
@ -9,132 +10,120 @@ namespace pslang::interpreter
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
types::type type_of_impl(unit_value const &)
|
struct type_of_visitor
|
||||||
{
|
{
|
||||||
return types::unit_type{};
|
types::type operator()(unit_value const &)
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
types::type type_of_impl(primitive_value_base<T> const &)
|
|
||||||
{
|
|
||||||
return types::primitive_type(types::primitive_type_base<T>{});
|
|
||||||
}
|
|
||||||
|
|
||||||
types::type type_of_impl(primitive_value const & value)
|
|
||||||
{
|
|
||||||
return std::visit([](auto const & value){ return type_of_impl(value); }, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
types::type type_of_impl(array_value const & value)
|
|
||||||
{
|
|
||||||
return types::array_type{.element_type = value.element_type, .size = value.elements.size()};
|
|
||||||
}
|
|
||||||
|
|
||||||
types::type type_of_impl(struct_value const & value)
|
|
||||||
{
|
|
||||||
return *value.struct_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
types::type type_of_impl(function_value const & value)
|
|
||||||
{
|
|
||||||
types::function_type result;
|
|
||||||
for (auto const & argument : value.arguments)
|
|
||||||
result.arguments.push_back(argument.type);
|
|
||||||
result.result = value.return_type;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
types::type type_of_impl(value const & value)
|
|
||||||
{
|
|
||||||
return std::visit([](auto const & value){ return type_of_impl(value); }, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_impl(std::ostream & out, unit_value const &)
|
|
||||||
{
|
|
||||||
out << "unit";
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void print_impl(std::ostream & out, primitive_value_base<T> const & value)
|
|
||||||
{
|
|
||||||
if constexpr (std::is_same_v<T, bool>)
|
|
||||||
{
|
{
|
||||||
out << (value.value ? "true" : "false");
|
return types::unit_type{};
|
||||||
}
|
}
|
||||||
else if constexpr (std::is_integral_v<T> && std::is_signed_v<T>)
|
|
||||||
{
|
|
||||||
out << (std::int64_t)value.value;
|
|
||||||
}
|
|
||||||
else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>)
|
|
||||||
{
|
|
||||||
out << (std::uint64_t)value.value;
|
|
||||||
}
|
|
||||||
else if constexpr (std::is_same_v<T, float>)
|
|
||||||
{
|
|
||||||
out << std::setprecision(7) << value.value;
|
|
||||||
}
|
|
||||||
else if constexpr (std::is_same_v<T, double>)
|
|
||||||
{
|
|
||||||
out << std::setprecision(15) << value.value;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
out << "(unknown)";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_impl(std::ostream & out, primitive_value const & value)
|
template <typename T>
|
||||||
{
|
types::type operator()(primitive_value_base<T> const &)
|
||||||
std::visit([&](auto const & value){ return print_impl(out, value); }, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_impl(std::ostream & out, array_value const & value)
|
|
||||||
{
|
|
||||||
out << "[";
|
|
||||||
for (std::size_t i = 0; i < value.elements.size(); ++i)
|
|
||||||
{
|
{
|
||||||
if (i > 0)
|
return types::primitive_type(types::primitive_type_base<T>{});
|
||||||
out << ", ";
|
|
||||||
print(out, *value.elements[i]);
|
|
||||||
}
|
}
|
||||||
out << "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_impl(std::ostream & out, struct_value const & value)
|
types::type operator()(array_value const & value)
|
||||||
{
|
|
||||||
out << "{";
|
|
||||||
bool first = true;
|
|
||||||
for (auto const & field : value.fields)
|
|
||||||
{
|
{
|
||||||
if (!first)
|
return types::array_type{.element_type = value.element_type, .size = value.elements.size()};
|
||||||
out << ", ";
|
|
||||||
first = false;
|
|
||||||
out << field.first << " = ";
|
|
||||||
print(out, *field.second);
|
|
||||||
}
|
}
|
||||||
out << "}";
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_impl(std::ostream & out, function_value const & value)
|
types::type operator()(struct_value const & value)
|
||||||
{
|
{
|
||||||
out << "func";
|
return *value.struct_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_impl(std::ostream & out, value const & value)
|
types::type operator()(function_value const & value)
|
||||||
|
{
|
||||||
|
types::function_type result;
|
||||||
|
for (auto const & argument : value.arguments)
|
||||||
|
result.arguments.push_back(argument.type);
|
||||||
|
result.result = value.return_type;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct print_visitor
|
||||||
{
|
{
|
||||||
std::visit([&](auto const & value){ return print_impl(out, value); }, value);
|
std::ostream & out;
|
||||||
}
|
|
||||||
|
void operator()(unit_value const &)
|
||||||
|
{
|
||||||
|
out << "unit";
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void operator()(primitive_value_base<T> const & value)
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<T, bool>)
|
||||||
|
{
|
||||||
|
out << (value.value ? "true" : "false");
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_integral_v<T> && std::is_signed_v<T>)
|
||||||
|
{
|
||||||
|
out << (std::int64_t)value.value;
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>)
|
||||||
|
{
|
||||||
|
out << (std::uint64_t)value.value;
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, float>)
|
||||||
|
{
|
||||||
|
out << std::setprecision(7) << value.value;
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, double>)
|
||||||
|
{
|
||||||
|
out << std::setprecision(15) << value.value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
out << "(unknown)";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(array_value const & value)
|
||||||
|
{
|
||||||
|
out << "[";
|
||||||
|
for (std::size_t i = 0; i < value.elements.size(); ++i)
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
out << ", ";
|
||||||
|
apply(*this, *value.elements[i]);
|
||||||
|
}
|
||||||
|
out << "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(struct_value const & value)
|
||||||
|
{
|
||||||
|
out << "{";
|
||||||
|
bool first = true;
|
||||||
|
for (auto const & field : value.fields)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
out << ", ";
|
||||||
|
first = false;
|
||||||
|
out << field.first << " = ";
|
||||||
|
apply(*this, *field.second);
|
||||||
|
}
|
||||||
|
out << "}";
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(function_value const & value)
|
||||||
|
{
|
||||||
|
out << "func";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
types::type type_of(value const & value)
|
types::type type_of(value const & value)
|
||||||
{
|
{
|
||||||
return type_of_impl(value);
|
return apply(type_of_visitor{}, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(std::ostream & out, value const & value)
|
void print(std::ostream & out, value const & value)
|
||||||
{
|
{
|
||||||
print_impl(out, value);
|
apply(print_visitor{out}, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue