2023-07-24 18:47:57 +03:00
|
|
|
#include "basic_printers.hpp"
|
|
|
|
|
|
2023-07-24 21:23:18 +03:00
|
|
|
#include <string>
|
2023-07-24 18:47:57 +03:00
|
|
|
|
2023-07-31 22:07:32 +03:00
|
|
|
#include "basic_nodes.hpp"
|
|
|
|
|
#include "utils.hpp"
|
|
|
|
|
|
2023-07-24 21:23:18 +03:00
|
|
|
namespace printers {
|
2023-07-24 18:47:57 +03:00
|
|
|
|
2023-07-31 22:07:32 +03:00
|
|
|
void print_modifier(const nodes::Modifier &modifier, Printer &printer,
|
|
|
|
|
bool const_is_none) {
|
2023-07-26 13:43:14 +03:00
|
|
|
switch (modifier) {
|
|
|
|
|
case nodes::Modifier::IN:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "in " : "<- ");
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::REF:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "ref " : "<> ");
|
|
|
|
|
break;
|
2023-07-31 22:07:32 +03:00
|
|
|
case nodes::Modifier::CONST:
|
|
|
|
|
if (!const_is_none) {
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "ref " : "<> ");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::OUT:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "out " : "-> ");
|
|
|
|
|
break;
|
|
|
|
|
// ---
|
|
|
|
|
case nodes::Modifier::IN_OR_REF:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "in|ref "
|
|
|
|
|
: "<-|<> ");
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::IN_OR_CONST:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "in|const "
|
|
|
|
|
: "<-|-- ");
|
2023-07-29 12:19:37 +03:00
|
|
|
break;
|
2023-07-31 22:07:32 +03:00
|
|
|
case nodes::Modifier::REF_OR_OUT:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "ref|out "
|
|
|
|
|
: "<>|-> ");
|
2023-07-29 12:19:37 +03:00
|
|
|
break;
|
2023-07-31 22:07:32 +03:00
|
|
|
case nodes::Modifier::CONST_OR_OUT:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "const|out "
|
|
|
|
|
: "--|-> ");
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::REF_OR_CONST:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "ref|const "
|
|
|
|
|
: "<>|-- ");
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::IN_OR_OUT:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "in|out "
|
|
|
|
|
: "<-|-> ");
|
|
|
|
|
break;
|
|
|
|
|
// ---
|
|
|
|
|
case nodes::Modifier::IN_OR_REF_OR_OUT:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "in|ref|out "
|
|
|
|
|
: "<-|<>|-> ");
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::IN_OR_CONST_OR_OUT:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "in|const|out "
|
|
|
|
|
: "<-|--|-> ");
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::IN_OR_REF_OR_CONST:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "in|ref|const "
|
|
|
|
|
: "<-|<>|-- ");
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::REF_OR_CONST_OR_OUT:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "ref|const|out "
|
|
|
|
|
: "<>|--|-> ");
|
|
|
|
|
break;
|
|
|
|
|
// ---
|
|
|
|
|
case nodes::Modifier::IN_OR_REF_OR_CONST_OR_OUT:
|
|
|
|
|
printer.print(printer.print_words_instead_of_symbols() ? "in|ref|const|out "
|
|
|
|
|
: "<-|<>|--|-> ");
|
|
|
|
|
break;
|
|
|
|
|
//
|
2023-07-28 19:42:09 +03:00
|
|
|
case nodes::Modifier::OPTIONAL:
|
2023-07-26 13:43:14 +03:00
|
|
|
printer.print("?");
|
|
|
|
|
break;
|
2023-07-28 19:42:09 +03:00
|
|
|
case nodes::Modifier::RESULT:
|
2023-07-26 13:43:14 +03:00
|
|
|
printer.print("!");
|
|
|
|
|
break;
|
|
|
|
|
case nodes::Modifier::NONE:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 18:47:57 +03:00
|
|
|
void print_literal(const nodes::Literal &literal, Printer &printer) {
|
|
|
|
|
switch (literal.get_any()->index()) {
|
|
|
|
|
case 0: // double
|
|
|
|
|
// print in parseable form ??
|
2023-07-24 21:23:18 +03:00
|
|
|
printer.print(std::to_string(*literal.get<double>().value()));
|
2023-07-24 18:47:57 +03:00
|
|
|
return;
|
|
|
|
|
case 1: // long long
|
2023-07-24 21:23:18 +03:00
|
|
|
printer.print(std::to_string(*literal.get<long long>().value()));
|
2023-07-24 18:47:57 +03:00
|
|
|
return;
|
|
|
|
|
case 2: // std::string
|
2023-07-24 21:23:18 +03:00
|
|
|
printer.print("\"");
|
|
|
|
|
printer.print_converted(
|
|
|
|
|
*literal.get<std::string>()
|
|
|
|
|
.value()); // special symbols are converted inside
|
|
|
|
|
printer.print("\"");
|
2023-07-24 18:47:57 +03:00
|
|
|
return;
|
|
|
|
|
case 3: // char
|
|
|
|
|
printer.print("\'\'");
|
2023-07-24 21:23:18 +03:00
|
|
|
printer.print_converted(std::string(1, *literal.get<char>().value()));
|
2023-07-24 18:47:57 +03:00
|
|
|
printer.print("\'\'");
|
|
|
|
|
return;
|
|
|
|
|
case 4: // bool
|
|
|
|
|
printer.print(literal.get<bool>().value() ? "true" : "false");
|
|
|
|
|
return;
|
|
|
|
|
case 5: // unit
|
|
|
|
|
printer.print("()");
|
|
|
|
|
return;
|
|
|
|
|
case 6: // null
|
|
|
|
|
printer.print("null");
|
|
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error_handling::handle_general_error("Unreachable");
|
|
|
|
|
exit(1); // unreachable
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print_identifier(const nodes::Identifier &identifier, Printer &printer) {
|
2023-07-24 21:23:18 +03:00
|
|
|
printer.print(*identifier.get());
|
2023-07-24 18:47:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print_annotation(const std::string &annotation, Printer &printer) {
|
2023-07-24 21:23:18 +03:00
|
|
|
printer.print("@");
|
2023-07-24 18:47:57 +03:00
|
|
|
printer.print(annotation);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 22:07:32 +03:00
|
|
|
void print_extra(const nodes::Extra &extra, Printer &printer) {
|
|
|
|
|
printer.print(*extra.content());
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 21:33:57 +03:00
|
|
|
void print_empty_lines(const nodes::EmptyLines &empty_lines, Printer &printer) {
|
2023-07-31 22:07:32 +03:00
|
|
|
for (size_t i = 0; i < empty_lines.line_count(); ++i) {
|
2023-07-25 21:33:57 +03:00
|
|
|
printer.new_indent_line();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 18:47:57 +03:00
|
|
|
} // namespace printers
|