lang_2023/src/print_visitor.cpp

671 lines
15 KiB
C++
Raw Normal View History

// for clangd
#include "../include/print_visitor.hpp"
#include <ctime>
namespace interpreter {
// Sources -----------------
void PrintVisitor::Visit(SourceFile* node) {
out_ << "[SourceFile] (\n\n";
for (auto& statement : node->statements) {
if (std::holds_alternative<Partition>(statement)) {
Visit(&std::get<Partition>(statement));
} else if (std::holds_alternative<SourceStatement>(statement)) {
Visitor::Visit(std::get<SourceStatement>(statement));
} else {
// error
}
}
out_ << "\n)\n";
}
void PrintVisitor::Visit(Sources* node) {
out_ << "[Sources](\n";
for (auto& statement : node->statements) {
Visitor::Visit(statement);
}
out_ << ")\n";
}
2023-04-02 15:10:32 +03:00
// Namespaces, partitions -----------------
void PrintVisitor::Visit(Partition* node) {
out_ << "[Partition] ";
switch (node->name) {
case Partition::Test:
out_ << "TEST";
break;
case Partition::Interface:
out_ << "INTERFACE";
break;
case Partition::Core:
out_ << "CORE";
break;
case Partition::Lib:
out_ << "LIB";
break;
case Partition::Module:
out_ << "MODULE";
break;
case Partition::Exe:
out_ << "EXE";
break;
}
out_ << " {\n";
Visit(node->scope.get());
out_ << "}\n";
}
void PrintVisitor::Visit(Namespace* node) {
out_ << "[Namespace] ";
if (node->name.has_value()) {
switch (node->modifier) {
case Namespace::Const:
out_ << "const ";
break;
case Namespace::Var:
out_ << "var ";
break;
2023-03-28 12:05:20 +03:00
}
Visit(&node->name.value());
}
Visit(&node->type);
out_ << "{\n";
Visit(node->scope.get());
out_ << "}\n";
}
// Definitions -----------------
void PrintVisitor::Visit(ImportStatement* node) {
if (node->name.has_value()) {
out_ << "[Use " << node->name.value() << "] = ";
}
out_ << "[Import " << node->module_name << "]";
if (!node->symbols.empty()) {
out_ << " (\n";
for (auto& symbol : node->symbols) {
Visitor::Visit(&symbol);
out_ << '\n';
}
out_ << ')';
}
out_ << '\n';
}
void PrintVisitor::Visit(AliasDefinitionStatement* node) {
out_ << "[Alias ";
switch (node->modifier) {
case AliasDefinitionStatement::Alias:
out_ << "alias";
break;
case AliasDefinitionStatement::Type:
out_ << "type";
break;
case AliasDefinitionStatement::Let:
out_ << "let";
break;
}
out_ << ' ';
Visit(&node->type);
out_ << "] = (";
Visit(node->value.get());
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
void PrintVisitor::Visit(VariableDefinitionStatement* node) {
out_ << "[Variable ";
switch (node->modifier) {
case VariableDefinitionStatement::Const:
out_ << "const";
break;
case VariableDefinitionStatement::Var:
out_ << "var";
break;
}
out_ << ' ';
Visitor::Visit(node->name);
out_ << "] = (";
Visitor::Visit(node->value);
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
void PrintVisitor::Visit(FunctionDeclaration* node) {
out_ << "[FunctionDeclaration ";
Visit(&node->name);
out_ << "] (";
for (auto& parameter : node->parameters) {
Visit(parameter.get());
}
out_ << ") : (";
Visit(node->type.get());
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
void PrintVisitor::Visit(FunctionDefinitionStatement* node) {
out_ << "[Function] (";
Visit(node->definition.get());
out_ << ") = (";
Visitor::Visit(node->value);
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
void PrintVisitor::Visit(TypeDefinitionStatement* node) {
out_ << "[Type ";
switch (node->modifier) {
case TypeDefinitionStatement::Struct:
out_ << "struct";
break;
case TypeDefinitionStatement::Class:
out_ << "class";
break;
}
out_ << "] (";
Visit(node->definition.get());
out_ << ") = (";
Visitor::Visit(node->value);
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
void PrintVisitor::Visit(AbstractTypeDefinitionStatement* node) {
out_ << "[AbstractType ";
switch (node->modifier) {
case AbstractTypeDefinitionStatement::Basic:
out_ << "basic";
break;
case AbstractTypeDefinitionStatement::Abstract:
out_ << "abstract";
break;
}
out_ << "] (";
Visit(node->definition.get());
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
void PrintVisitor::Visit(TypeclassDefinitionStatement* node) {
out_ << "[Typeclass] (";
Visit(node->definition.get());
2023-04-02 15:10:32 +03:00
if (!node->requirements.empty()) {
out_ << ") : (\n";
}
for (auto& requirement : node->requirements) {
out_ << "& ";
Visit(requirement.get());
out_ << "\n";
}
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
// Definition parts
void PrintVisitor::Visit(FunctionDefinition* node) {
out_ << "[FunctionDefinition ";
switch (node->modifier) {
case FunctionDefinition::Operator:
out_ << "operator";
break;
case FunctionDefinition::Function:
out_ << "function";
break;
}
out_ << ' ';
Visit(&node->name);
out_ << "]";
2023-04-02 15:10:32 +03:00
if (!node->parameters.empty()) {
out_ << " (";
for (auto& parameter : node->parameters) {
Visit(parameter.get());
}
out_ << ')';
}
2023-04-02 15:10:32 +03:00
if (!node->arguments.empty()) {
out_ << " : (";
for (auto& argument : node->arguments) {
Visit(&argument);
}
out_ << ')';
}
out_ << ' ';
}
void PrintVisitor::Visit(TypeDefinition* node) {
out_ << "[TypeDefinition] (";
Visit(node->type.get());
out_ << ')';
2023-04-02 15:10:32 +03:00
if (!node->parameters.empty()) {
out_ << '(';
for (auto& parameter : node->parameters) {
Visit(parameter.get());
}
out_ << ')';
}
out_ << ' ';
}
void PrintVisitor::Visit(DefinitionParameter* node) {
out_ << "[DefinitionParameter ";
Visit(&node->type);
out_ << ']';
if (!node->typeclasses.empty() > 0) {
out_ << " (";
for (auto& typeclass : node->typeclasses) {
Visitor::Visit(typeclass);
}
out_ << ')';
}
out_ << ' ';
}
// Flow control -----------------
2023-03-29 11:42:00 +03:00
void PrintVisitor::Visit(MatchCase* node) {
out_ << "[MatchCase | ";
2023-03-29 11:42:00 +03:00
Visitor::Visit(node->value);
if (node->condition.has_value()) {
out_ << " ? ";
Visitor::Visit(node->condition.value());
}
if (node->statement.has_value()) {
out_ << " -> ";
Visitor::Visit(node->statement.value());
}
out_ << "]\n";
2023-03-29 11:42:00 +03:00
}
void PrintVisitor::Visit(Match* node) {
out_ << "[Match] (";
Visitor::Visit(node->value);
out_ << ") [with] (\n";
for (auto& match_case : node->matches) {
2023-03-29 11:42:00 +03:00
Visit(&match_case);
}
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
void PrintVisitor::Visit(Condition* node) {
out_ << "[If] (";
Visitor::Visit(node->conditions[0]);
out_ << ") [then] (\n";
Visitor::Visit(node->statements[0]);
out_ << ')';
for (size_t i = 1; i < node->conditions.size(); ++i) {
out_ << " [elif] (";
Visitor::Visit(node->conditions[i]);
out_ << ") [then] (\n";
Visitor::Visit(node->statements[i]);
out_ << ')';
}
if (node->statements.size() > node->conditions.size()) {
out_ << " [else] (\n";
Visitor::Visit(node->statements[node->conditions.size()]);
out_ << ')';
}
out_ << '\n';
}
void PrintVisitor::Visit(DoWhileLoop* node) {
out_ << "[Do] (\n";
Visitor::Visit(node->statement);
out_ << ") [while] (";
Visitor::Visit(node->condition);
out_ << ")\n";
}
void PrintVisitor::Visit(WhileLoop* node) {
out_ << "[While] (";
Visitor::Visit(node->statement);
out_ << ") [do] (\n";
Visitor::Visit(node->condition);
out_ << ")\n";
}
void PrintVisitor::Visit(ForLoop* node) {
out_ << "[For] (";
Visitor::Visit(node->variable);
out_ << ") [in] (";
Visitor::Visit(node->interval);
out_ << ") [do] (\n";
Visitor::Visit(node->statement);
2023-03-31 12:10:12 +03:00
out_ << ")\n";
}
void PrintVisitor::Visit(LoopLoop* node) {
out_ << "[Loop] (\n";
Visitor::Visit(node->statement);
out_ << ")\n";
}
// Statements, expressions, blocks, etc. -----------------
void PrintVisitor::Visit(Block* node) {
out_ << "[Block] {\n";
for (auto& statement : node->statements) {
Visitor::Visit(statement);
}
out_ << "}\n";
}
void PrintVisitor::Visit(ScopedStatement* node) {
out_ << "[Scoped] ( ";
Visitor::Visit(node->statement);
out_ << ")\n";
}
2023-03-28 12:05:20 +03:00
void PrintVisitor::Visit(LoopControlExpression& node) { // enum
switch (node) {
case LoopControlExpression::Break:
out_ << "[Break]\n";
2023-03-28 12:05:20 +03:00
break;
case LoopControlExpression::Continue:
out_ << "[Continue]\n";
2023-03-28 12:05:20 +03:00
break;
}
}
// Operators
void PrintVisitor::Visit(BinaryOperatorExpression* node) {
out_ << "[BinaryOperator] (";
Visitor::Visit(node->left_expression);
out_ << ") [";
Visit(&node->operator_name);
out_ << "] (";
Visitor::Visit(node->right_expression);
out_ << ')';
}
void PrintVisitor::Visit(UnaryOperatorExpression* node) {
out_ << "[UnaryOperator ";
Visit(&node->operator_name);
out_ << "] (";
Visitor::Visit(node->expression);
out_ << ')';
}
void PrintVisitor::Visit(ReferenceExpression* node) {
out_ << "[ReferenceExpression ";
for (auto& reference : node->references) {
switch (reference) {
case ReferenceType::Reference:
out_ << '~';
break;
case ReferenceType::UniqueReference:
out_ << '@';
break;
}
}
out_ << "] (";
Visit(node->expression.get());
out_ << ')';
}
// Other Expressions
2023-03-27 03:10:04 +03:00
void PrintVisitor::Visit(FunctionCallExpression* node) {
out_ << "[FunctionCall ";
2023-03-27 03:10:04 +03:00
Visit(node->name.get());
out_ << "] (";
2023-03-27 03:10:04 +03:00
for (auto& argument : node->arguments) {
Visitor::Visit(argument);
out_ << ", ";
2023-03-27 03:10:04 +03:00
}
2023-03-31 12:10:12 +03:00
out_ << ")\n";
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(TupleExpression* node) {
out_ << "[TupleExpression] (";
2023-03-27 03:10:04 +03:00
for (auto& expression : node->expressions) {
out_ << "&";
2023-03-27 03:10:04 +03:00
Visitor::Visit(expression);
}
out_ << ")";
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(VariantExpression* node) {
out_ << "[VariantExpression] (";
2023-03-27 03:10:04 +03:00
for (auto& expression : node->expressions) {
out_ << "|";
2023-03-27 03:10:04 +03:00
Visitor::Visit(expression);
}
out_ << ")";
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(ReturnExpression* node) {
out_ << "[Return] (";
2023-03-27 03:10:04 +03:00
Visitor::Visit(node->expression);
out_ << ")\n";
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(TypeConstructor* node) {
out_ << "[TypeConstructor ";
Visit(node->type.get());
out_ << "]\n(";
for (auto& parameter : node->parameters) {
Visit(&parameter.first);
out_ << " = ";
Visitor::Visit(parameter.second);
out_ << ")\n(";
}
out_ << ")\n";
}
2023-03-27 03:10:04 +03:00
void PrintVisitor::Visit(LambdaFunction* node) {
out_ << "[LambdaFunction] (";
2023-03-27 03:10:04 +03:00
for (auto& parameter : node->parameters) {
Visit(parameter.get());
}
2023-04-02 15:10:32 +03:00
if (!node->parameters.empty()) {
out_ << ") : (";
2023-03-27 03:10:04 +03:00
}
for (auto& argument : node->arguments) {
Visit(&argument);
2023-03-27 03:10:04 +03:00
}
out_ << ") -> (\n";
2023-03-27 03:10:04 +03:00
Visitor::Visit(node->expression);
out_ << ")\n";
}
void PrintVisitor::Visit(ArrayExpression* node) {
out_ << "[ArrayExpression] ([";
for (auto& element : node->elements) {
Visitor::Visit(element);
out_ << ';';
}
out_ << "])";
2023-03-27 03:10:04 +03:00
}
// Name
void PrintVisitor::Visit(NameExpression* node) {
out_ << "[NameExpression] (";
2023-03-27 03:10:04 +03:00
for (auto& variable_namespace : node->namespaces) {
Visitor::Visit(variable_namespace);
out_ << '.';
}
2023-03-31 12:10:12 +03:00
for (size_t i = 0; i < node->expressions.size(); ++i) {
2023-03-27 03:10:04 +03:00
Visitor::Visit(node->expressions[i]);
if (i + 1 < node->expressions.size()) {
out_ << '.';
}
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(TupleName* node) {
out_ << "[TupleName] (";
2023-03-27 03:10:04 +03:00
for (auto& name : node->names) {
out_ << "& ";
Visitor::Visit(name);
2023-03-27 03:10:04 +03:00
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(VariantName* node) {
out_ << "[VariantName] (";
2023-03-27 03:10:04 +03:00
for (auto& name : node->names) {
out_ << "| ";
Visitor::Visit(name);
2023-03-27 03:10:04 +03:00
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(AnnotatedName* node) {
out_ << "[AnnotatedName ";
2023-03-27 03:10:04 +03:00
Visit(&node->name);
out_ << ']';
2023-03-29 11:42:00 +03:00
if (node->type.has_value()) {
out_ << " : (";
Visitor::Visit(node->type.value());
out_ << ')';
2023-03-29 11:42:00 +03:00
}
2023-03-27 03:10:04 +03:00
}
// Type, typeclass, etc. -----------------
// Type
void PrintVisitor::Visit(FunctionType* node) {
out_ << "[FunctionType] (";
bool first = true;
for (auto& type : node->types) {
if (!first) {
out_ << " -> ";
}
first = false;
Visitor::Visit(type);
2023-03-27 03:10:04 +03:00
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(TupleType* node) {
out_ << "[TupleType ";
2023-03-29 23:19:54 +03:00
if (node->type.has_value()) {
2023-03-31 12:10:12 +03:00
Visit(&node->type.value());
2023-03-29 23:19:54 +03:00
}
out_ << "] (";
2023-03-27 03:10:04 +03:00
for (auto& entity : node->entities) {
out_ << "& ";
2023-03-29 23:19:54 +03:00
if (entity.first.has_value()) {
Visit(&entity.first.value());
2023-03-27 03:10:04 +03:00
out_ << " : ";
}
Visit(entity.second.get());
2023-03-27 03:10:04 +03:00
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(VariantType* node) {
out_ << "[VariantType ";
2023-03-29 23:19:54 +03:00
if (node->type.has_value()) {
Visit(&node->type.value());
}
out_ << "] (";
2023-03-27 03:10:04 +03:00
for (auto& constructor : node->constructors) {
out_ << "| ";
if (std::holds_alternative<Constructor>(constructor)) {
Visit(&std::get<Constructor>(constructor));
2023-03-29 23:19:54 +03:00
} else if (std::holds_alternative<std::unique_ptr<TupleType>>(constructor)) {
Visit(std::get<std::unique_ptr<TupleType>>(constructor).get());
} else {
// error
}
2023-03-27 03:10:04 +03:00
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(AnnotatedType* node) {
out_ << "[AnnotatedType ";
2023-03-27 03:10:04 +03:00
Visit(node->type_expression.get());
out_ << ']';
2023-04-02 15:10:32 +03:00
if (!node->annotations.empty()) {
out_ << " : (";
for (auto& annotation : node->annotations) {
Visitor::Visit(annotation);
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
}
void PrintVisitor::Visit(ParametrizedType* node) {
out_ << "[ParametrizedType] (";
2023-03-27 03:10:04 +03:00
Visit(node->type_expression.get());
2023-04-02 15:10:32 +03:00
for (auto& parameter : node->parameters) {
2023-03-27 03:10:04 +03:00
out_ << ' ';
2023-04-02 15:10:32 +03:00
Visitor::Visit(parameter);
2023-03-27 03:10:04 +03:00
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(TypeExpression* node) {
out_ << "[TypeExpression] (";
2023-03-27 03:10:04 +03:00
for (auto& type_namespace : node->namespaces) {
Visitor::Visit(type_namespace);
out_ << '.';
}
Visit(&node->type);
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(ExtendedScopedAnyType* node) {
out_ << "[ExtendedScopedAnyType ";
for (auto& reference : node->references) {
switch (reference) {
case ReferenceType::Reference:
out_ << '~';
break;
case ReferenceType::UniqueReference:
out_ << '@';
break;
}
2023-03-27 03:10:04 +03:00
}
out_ << "] (";
Visitor::Visit(node->type);
out_ << ')';
2023-03-27 03:10:04 +03:00
}
// Typeclass
2023-03-27 03:10:04 +03:00
void PrintVisitor::Visit(ParametrizedTypeclass* node) {
out_ << "[ParametrizedTypeclass] (";
2023-03-27 03:10:04 +03:00
Visit(node->typeclass_expression.get());
for (auto& paramater : node->parameters) {
out_ << ' ';
Visitor::Visit(paramater);
}
out_ << ')';
2023-03-27 03:10:04 +03:00
}
void PrintVisitor::Visit(TypeclassExpression* node) {
out_ << "[TypeclassExpression] (";
2023-03-27 03:10:04 +03:00
for (auto& typeclass_namespace : node->namespaces) {
Visitor::Visit(typeclass_namespace);
out_ << '.';
}
Visit(&node->typeclass);
out_ << ')';
2023-03-27 03:10:04 +03:00
}
// Identifiers, constants, etc. -----------------
void PrintVisitor::Visit(AnyIdentifier* node) { // std::string
out_ << "[Identifier " << *node << "] ";
}
void PrintVisitor::Visit(FloatNumberLiteral* node) {
out_ << "[FloatNumber " << node->value << "] ";
}
void PrintVisitor::Visit(NumberLiteral* node) {
out_ << "[Number " << node->value << "] ";
}
void PrintVisitor::Visit(StringLiteral* node) {
out_ << "[String " << node->value << "] ";
}
void PrintVisitor::Visit(CharLiteral* node) {
out_ << "[Char " << node->value << "] ";
}
} // namespace interpreter