246 lines
6.4 KiB
C++
246 lines
6.4 KiB
C++
#include <pslang/ast/print.hpp>
|
|
#include <pslang/type/print.hpp>
|
|
|
|
#include <iomanip>
|
|
|
|
namespace pslang::ast
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
print_options 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;
|
|
}
|
|
|
|
void newline(std::ostream & out)
|
|
{
|
|
out << '\n';
|
|
}
|
|
|
|
}
|
|
|
|
void print(std::ostream & out, bool_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "bool literal { value = " << (node.value ? "true" : "false") << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, i8_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "i8 literal { value = " << (std::int32_t)node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, u8_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "u8 literal { value = " << (std::uint32_t)node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, i16_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "i16 literal { value = " << node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, u16_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "u16 literal { value = " << node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, i32_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "i32 literal { value = " << node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, u32_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "u32 literal { value = " << node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, i64_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "i64 literal { value = " << node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, u64_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "u64 literal { value = " << node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, f32_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "f32 literal { value = " << std::setprecision(7) << node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, f64_literal const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "f64 literal { value = " << std::setprecision(15) << node.value << " }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, literal const & node, print_options const & options)
|
|
{
|
|
std::visit([&](auto const & value){ print(out, value, options); }, node);
|
|
}
|
|
|
|
void print(std::ostream & out, identifier const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "identifier { name = \"" << node.name << "\" }";
|
|
newline(out);
|
|
}
|
|
|
|
void print(std::ostream & out, unary_operation const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << node.type;
|
|
newline(out);
|
|
print(out, node.arg1, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, binary_operation const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << node.type;
|
|
newline(out);
|
|
print(out, node.arg1, child(options));
|
|
print(out, node.arg2, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, cast_operation const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "cast as ";
|
|
type::print(out, *node.type);
|
|
newline(out);
|
|
print(out, node.expression, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, expression_ptr const & node, print_options const & options)
|
|
{
|
|
std::visit([&](auto const & value){ print(out, value, options); }, *node);
|
|
}
|
|
|
|
void print(std::ostream & out, assignment const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "assignment";
|
|
newline(out);
|
|
print(out, node.lhs, child(options));
|
|
print(out, node.rhs, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, variable_declaration const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "variable declaration { category = " << node.category << ", name = \"" << node.name << "\"";
|
|
if (node.type)
|
|
{
|
|
out << ", type = ";
|
|
type::print(out, *node.type);
|
|
}
|
|
out << " }";
|
|
newline(out);
|
|
print(out, node.initializer, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, if_block const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "if";
|
|
newline(out);
|
|
print(out, node.condition, child(options));
|
|
print(out, node.statements, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, else_block const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "else";
|
|
newline(out);
|
|
print(out, node.statements, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, else_if_block const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "else if";
|
|
newline(out);
|
|
print(out, node.condition, child(options));
|
|
print(out, node.statements, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, if_chain const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "if chain";
|
|
newline(out);
|
|
for (auto const & block : node.blocks)
|
|
{
|
|
put_indent(out, child(options));
|
|
out << "condition";
|
|
newline(out);
|
|
if (block.condition)
|
|
print(out, block.condition, child(child(options)));
|
|
else
|
|
{
|
|
put_indent(out, child(child(options)));
|
|
out << "(none)";
|
|
newline(out);
|
|
}
|
|
|
|
put_indent(out, child(options));
|
|
out << "body";
|
|
newline(out);
|
|
print(out, block.statements, child(child(options)));
|
|
}
|
|
}
|
|
|
|
void print(std::ostream & out, while_block const & node, print_options const & options)
|
|
{
|
|
put_indent(out, options);
|
|
out << "while";
|
|
newline(out);
|
|
print(out, node.condition, child(options));
|
|
print(out, node.statements, child(options));
|
|
}
|
|
|
|
void print(std::ostream & out, statement_ptr const & node, print_options const & options)
|
|
{
|
|
std::visit([&](auto const & value){ print(out, value, options); }, *node);
|
|
}
|
|
|
|
void print(std::ostream & out, statement_list_ptr const & node, print_options const & options)
|
|
{
|
|
for (auto const & statement : node->statements)
|
|
print(out, statement, options);
|
|
}
|
|
|
|
}
|