basic printers, type printers, some fixes, part of expression printers

This commit is contained in:
ProgramSnail 2023-07-24 18:47:57 +03:00
parent 3914ff7d8b
commit 3669084f55
14 changed files with 795 additions and 39 deletions

83
src/basic_printers.cpp Normal file
View file

@ -0,0 +1,83 @@
#include "basic_printers.hpp"
#include "error_handling.hpp"
namespace printers {
namespace utils {
std::string to_printable_symbol(char ch) {
switch (ch) {
case '\a':
return "\\a";
case '\b':
return "\\b";
case '\e':
return "\\e";
case '\f':
return "\\f";
case '\n':
return "\\n";
case '\r':
return "\\r";
case '\t':
return "\\t";
case '\v':
return "\\v";
// case ' ':
// return "\\s";
default:
return std::string(1, ch);
}
}
} // namespace utils
void print_literal(const nodes::Literal &literal, Printer &printer) {
switch (literal.get_any()->index()) {
case 0: // double
// print in parseable form ??
printer.print(literal.get<double>().value());
return;
case 1: // long long
printer.print(literal.get<long long>().value());
return;
case 2: // std::string
printer.print("\"\"");
// more efficient approach ??
for (auto &ch : *literal.get<std::string>().value()) {
printer.print(utils::to_printable_symbol(ch));
}
printer.print("\"\"");
return;
case 3: // char
printer.print("\'\'");
printer.print(utils::to_printable_symbol(*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);
}
} // namespace printers