pslang/libs/ast/source/print.cpp
2025-12-20 13:20:44 +03:00

377 lines
8.2 KiB
C++

#include <pslang/ast/print.hpp>
#include <pslang/ast/statement.hpp>
#include <pslang/ast/type_visitor.hpp>
#include <pslang/ast/expression_visitor.hpp>
#include <pslang/ast/statement_visitor.hpp>
#include <pslang/types/print.hpp>
#include <pslang/types/type.hpp>
#include <iomanip>
namespace pslang::ast
{
namespace
{
print_options as_child(print_options options)
{
options.indent_level += 1;
return options;
}
void put_indent(std::ostream & out, print_options const & options)
{
for (std::size_t i = 0; i < options.indent_level; ++i)
out << options.indent_string;
}
struct type_print_visitor
{
std::ostream & out;
void operator()(types::unit_type const &)
{
out << "unit";
}
template <typename T>
void operator()(types::primitive_type_base<T> const & type)
{
types::print(out, types::primitive_type{type});
}
void operator()(array_type const & type)
{
apply(*this, *type.element_type);
out << "[" << type.size << "]";
}
void operator()(function_type const & type)
{
if (type.arguments.size() == 1)
{
apply(*this, *type.arguments.front());
out << " -> ";
apply(*this, *type.result);
return;
}
out << '(';
bool first = true;
for (auto const & argument : type.arguments)
{
if (!first) out << ", ";
first = false;
apply(*this, *argument);
}
out << ") -> ";
apply(*this, *type.result);
}
void operator()(type_identifier const & type)
{
out << type.name;
}
};
struct expression_print_visitor
{
std::ostream & out;
print_options options;
template <typename Node>
void child(Node const & node)
{
++options.indent_level;
apply(*this, node);
--options.indent_level;
}
void operator()(bool_literal const & node)
{
put_indent(out, options);
out << "bool literal { value = " << (node.value ? "true" : "false") << " }\n";
}
void operator()(i8_literal const & node)
{
put_indent(out, options);
out << "i8 literal { value = " << (std::int32_t)node.value << " }\n";
}
void operator()(u8_literal const & node)
{
put_indent(out, options);
out << "u8 literal { value = " << (std::uint32_t)node.value << " }\n";
}
void operator()(i16_literal const & node)
{
put_indent(out, options);
out << "i16 literal { value = " << node.value << " }\n";
}
void operator()(u16_literal const & node)
{
put_indent(out, options);
out << "u16 literal { value = " << node.value << " }\n";
}
void operator()(i32_literal const & node)
{
put_indent(out, options);
out << "i32 literal { value = " << node.value << " }\n";
}
void operator()(u32_literal const & node)
{
put_indent(out, options);
out << "u32 literal { value = " << node.value << " }\n";
}
void operator()(i64_literal const & node)
{
put_indent(out, options);
out << "i64 literal { value = " << node.value << " }\n";
}
void operator()(u64_literal const & node)
{
put_indent(out, options);
out << "u64 literal { value = " << node.value << " }\n";
}
void operator()(f32_literal const & node)
{
put_indent(out, options);
out << "f32 literal { value = " << std::setprecision(7) << node.value << " }\n";
}
void operator()(f64_literal const & node)
{
put_indent(out, options);
out << "f64 literal { value = " << std::setprecision(15) << node.value << " }\n";
}
void operator()(identifier const & node)
{
put_indent(out, options);
out << "identifier { name = \"" << node.name << "\" }\n";
}
void operator()(unary_operation const & node)
{
put_indent(out, options);
out << node.type << '\n';
child(*node.arg1);
}
void operator()(binary_operation const & node)
{
put_indent(out, options);
out << node.type << '\n';
child(*node.arg1);
child(*node.arg2);
}
void operator()(cast_operation const & node)
{
put_indent(out, options);
out << "cast as ";
print(out, *node.type);
out << '\n';
child(*node.expression);
}
void operator()(function_call const & node)
{
put_indent(out, options);
out << "call\n";
child(*node.function);
for (auto const & argument : node.arguments)
child(*argument);
}
void operator()(array const & node)
{
put_indent(out, options);
out << "array\n";
for (auto const & element : node.elements)
child(*element);
}
void operator()(array_access const & node)
{
put_indent(out, options);
out << "array access\n";
child(*node.array);
child(*node.index);
}
void operator()(field_access const & node)
{
put_indent(out, options);
out << "field access { name = \"" << node.field_name << "\" }\n";
child(*node.object);
}
};
struct statement_print_visitor
{
std::ostream & out;
print_options options;
template <typename Node>
void child(Node const & node)
{
++options.indent_level;
ast::apply(*this, node);
--options.indent_level;
}
void operator()(expression_ptr const & node)
{
print(out, *node, options);
}
void operator()(assignment const & node)
{
put_indent(out, options);
out << "assignment\n";
child(node.lhs);
child(node.rhs);
}
void operator()(variable_declaration const & node)
{
put_indent(out, options);
out << "variable declaration { category = " << node.category << ", name = \"" << node.name << "\"";
if (node.type)
{
out << ", type = ";
print(out, *node.type);
}
out << " }\n";
child(node.initializer);
}
void operator()(if_block const & node)
{
put_indent(out, options);
out << "if\n";
child(node.condition);
}
void operator()(else_block const & node)
{
put_indent(out, options);
out << "else\n";
}
void operator()(else_if_block const & node)
{
put_indent(out, options);
out << "else if\n";
child(node.condition);
}
void operator()(if_chain const & node)
{
put_indent(out, options);
out << "if chain\n";
++options.indent_level;
for (auto const & block : node.blocks)
{
put_indent(out, options);
out << "condition\n";
if (block.condition)
child(block.condition);
else
{
put_indent(out, as_child(options));
out << "(none)\n";
}
put_indent(out, options);
out << "body\n";
child(*block.statements);
}
--options.indent_level;
}
void operator()(while_block const & node)
{
put_indent(out, options);
out << "while\n";
child(node.condition);
child(*node.statements);
}
void operator()(function_definition const & node)
{
put_indent(out, options);
out << "function { name = \"" << node.name << "\", return type = ";
print(out, *node.return_type);
out << " }\n";
++options.indent_level;
for (auto const & arg : node.arguments)
{
put_indent(out, options);
out << "argument { name = \"" << arg.name << "\", type = ";
print(out, *arg.type);
out << " }\n";
}
put_indent(out, options);
out << "body\n";
child(*node.statements);
}
void operator()(return_statement const & node)
{
put_indent(out, options);
out << "return\n";
if (node.value)
child(node.value);
}
void operator()(field_definition const & node)
{
put_indent(out, options);
out << "field { name = \"" << node.name << "\", type = ";
print(out, *node.type);
out << " }\n";
}
void operator()(struct_definition const & node)
{
put_indent(out, options);
out << "struct { name = \"" << node.name << "\" }\n";
for (auto const & field : node.fields)
child(field);
}
};
}
void print(std::ostream & out, type const & node)
{
apply(type_print_visitor{out}, node);
}
void print(std::ostream & out, expression const & node, print_options const & options)
{
apply(expression_print_visitor{out, options}, node);
}
void print(std::ostream & out, statement const & node, print_options const & options)
{
apply(statement_print_visitor{out, options}, node);
}
void print(std::ostream & out, statement_list const & node, print_options const & options)
{
apply(statement_print_visitor{out, options}, node);
}
}