mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2026-01-25 13:07:13 +00:00
fixes, expression printers
This commit is contained in:
parent
3669084f55
commit
bf49f4030c
9 changed files with 349 additions and 101 deletions
|
|
@ -2,56 +2,29 @@
|
|||
|
||||
#include "error_handling.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
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());
|
||||
printer.print(std::to_string(*literal.get<double>().value()));
|
||||
return;
|
||||
case 1: // long long
|
||||
printer.print(literal.get<long long>().value());
|
||||
printer.print(std::to_string(*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("\"\"");
|
||||
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(utils::to_printable_symbol(*literal.get<char>().value()));
|
||||
printer.print_converted(std::string(1, *literal.get<char>().value()));
|
||||
printer.print("\'\'");
|
||||
return;
|
||||
case 4: // bool
|
||||
|
|
@ -72,11 +45,11 @@ void print_literal(const nodes::Literal &literal, Printer &printer) {
|
|||
}
|
||||
|
||||
void print_identifier(const nodes::Identifier &identifier, Printer &printer) {
|
||||
printer.print(identifier.get());
|
||||
printer.print(*identifier.get());
|
||||
}
|
||||
|
||||
void print_annotation(const std::string &annotation, Printer &printer) {
|
||||
printer.print('@');
|
||||
printer.print("@");
|
||||
printer.print(annotation);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue