mirror of
https://codeberg.org/ProgramSnail/lang.git
synced 2025-12-06 06:58:46 +00:00
52 lines
1.2 KiB
C++
52 lines
1.2 KiB
C++
#pragma once
|
|
|
|
#include "basic_nodes.hpp"
|
|
#include <iostream>
|
|
|
|
namespace printers {
|
|
|
|
class Printer {
|
|
public:
|
|
Printer(std::ostream &output, size_t tab_width,
|
|
bool print_words_instead_of_symbols)
|
|
: output_(output), tab_width_(tab_width),
|
|
print_words_instead_of_symbols_(print_words_instead_of_symbols),
|
|
current_column_(0) {}
|
|
|
|
template <typename T> void print(const T &value) { output_ << value; }
|
|
|
|
void new_line() {
|
|
print('\n');
|
|
print_spaces(current_column_ * tab_width_);
|
|
}
|
|
|
|
void indent() { ++current_column_; }
|
|
|
|
void deindent() { --current_column_; }
|
|
|
|
void tab() { print_spaces(tab_width_); }
|
|
|
|
void space() { print_spaces(1); }
|
|
|
|
bool print_words_instead_of_symbols() {
|
|
return print_words_instead_of_symbols_;
|
|
}
|
|
|
|
private:
|
|
void print_spaces(size_t n) { print(std::string(n, ' ')); }
|
|
|
|
private:
|
|
std::ostream &output_;
|
|
size_t tab_width_ = 2;
|
|
bool print_words_instead_of_symbols_ = false;
|
|
|
|
size_t current_column_ = 0;
|
|
};
|
|
|
|
void print_literal(const nodes::Literal &literal, Printer &printer);
|
|
|
|
void print_identifier(const nodes::Identifier &identifier, Printer &printer);
|
|
|
|
void print_annotation(const std::string &annotation, Printer &printer);
|
|
|
|
} // namespace printers
|