mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2026-01-25 13:07:13 +00:00
145 lines
4.7 KiB
C++
145 lines
4.7 KiB
C++
#include "basic_printers.hpp"
|
|
|
|
#include <string>
|
|
|
|
#include "basic_nodes.hpp"
|
|
#include "utils.hpp"
|
|
|
|
namespace printers {
|
|
|
|
void print_modifier(const nodes::Modifier &modifier, Printer &printer,
|
|
bool const_is_none) {
|
|
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;
|
|
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 "
|
|
: "<-|-- ");
|
|
break;
|
|
case nodes::Modifier::REF_OR_OUT:
|
|
printer.print(printer.print_words_instead_of_symbols() ? "ref|out "
|
|
: "<>|-> ");
|
|
break;
|
|
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;
|
|
//
|
|
case nodes::Modifier::OPTIONAL:
|
|
printer.print("?");
|
|
break;
|
|
case nodes::Modifier::RESULT:
|
|
printer.print("!");
|
|
break;
|
|
case nodes::Modifier::NONE:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void print_literal(const nodes::Literal &literal, Printer &printer) {
|
|
switch (literal.get_any()->index()) {
|
|
case 0: // double
|
|
// print in parseable form ??
|
|
printer.print(std::to_string(*literal.get<double>().value()));
|
|
return;
|
|
case 1: // long long
|
|
printer.print(std::to_string(*literal.get<long long>().value()));
|
|
return;
|
|
case 2: // std::string
|
|
printer.print("\"");
|
|
printer.print_converted(
|
|
*literal.get<std::string>()
|
|
.value()); // special symbols are converted inside
|
|
printer.print("\"");
|
|
return;
|
|
case 3: // char
|
|
printer.print("\'\'");
|
|
printer.print_converted(std::string(1, *literal.get<char>().value()));
|
|
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) {
|
|
printer.print(*identifier.get());
|
|
}
|
|
|
|
void print_annotation(const std::string &annotation, Printer &printer) {
|
|
printer.print("@");
|
|
printer.print(annotation);
|
|
}
|
|
|
|
void print_extra(const nodes::Extra &extra, Printer &printer) {
|
|
printer.print(*extra.content());
|
|
}
|
|
|
|
void print_empty_lines(const nodes::EmptyLines &empty_lines, Printer &printer) {
|
|
for (size_t i = 0; i < empty_lines.line_count(); ++i) {
|
|
printer.new_indent_line();
|
|
}
|
|
}
|
|
|
|
} // namespace printers
|