mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-09 08:28:43 +00:00
likn_symbols _visitor done, but it is not tested yet
This commit is contained in:
parent
3c643d2759
commit
3d74b1383e
10 changed files with 236 additions and 427 deletions
|
|
@ -1,12 +1,12 @@
|
|||
// for clangd
|
||||
#include "../include/link_symbols_visitor.hpp"
|
||||
#include "../include/error_handling.hpp"
|
||||
|
||||
namespace interpreter {
|
||||
|
||||
// Sources -----------------
|
||||
|
||||
void LinkSymbolsVisitor::Visit(SourceFile* node) {
|
||||
out_ << "[SourceFile] (\n\n";
|
||||
for (auto& statement : node->statements) {
|
||||
if (std::holds_alternative<Partition>(statement)) {
|
||||
Visit(&std::get<Partition>(statement));
|
||||
|
|
@ -16,523 +16,267 @@ void LinkSymbolsVisitor::Visit(SourceFile* node) {
|
|||
// error
|
||||
}
|
||||
}
|
||||
out_ << "\n)\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(Sources* node) {
|
||||
out_ << "[Sources](\n";
|
||||
abstract_types_.EnterContext();
|
||||
for (auto& statement : node->statements) {
|
||||
Visitor::Visit(statement);
|
||||
}
|
||||
out_ << ")\n";
|
||||
abstract_types_.ExitContext();
|
||||
}
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void LinkSymbolsVisitor::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";
|
||||
// TODO
|
||||
Visit(node->scope.get());
|
||||
out_ << "}\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(Namespace* node) {
|
||||
out_ << "[Namespace] ";
|
||||
if (node->name.has_value()) {
|
||||
if (node->modifier.has_value()) {
|
||||
switch (node->modifier.value()) {
|
||||
case Namespace::Const:
|
||||
out_ << "const ";
|
||||
break;
|
||||
case Namespace::Var:
|
||||
out_ << "var ";
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
Visit(&node->name.value());
|
||||
}
|
||||
Visit(&node->type);
|
||||
out_ << "{\n";
|
||||
namespace_visitor_.EnterNamespace(node->type);
|
||||
Visit(node->scope.get());
|
||||
out_ << "}\n";
|
||||
namespace_visitor_.ExitNamespace();
|
||||
}
|
||||
|
||||
// Definitions -----------------
|
||||
|
||||
void LinkSymbolsVisitor::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) {
|
||||
Visit(&symbol);
|
||||
out_ << '\n';
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
out_ << '\n';
|
||||
}
|
||||
void LinkSymbolsVisitor::Visit(ImportStatement* node) {}
|
||||
|
||||
void LinkSymbolsVisitor::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;
|
||||
abstract_types_.EnterContext();
|
||||
for (size_t i = 0; i <node->parameters.size(); ++i) {
|
||||
abstract_types_.DefineType(node->parameters[i], node->parameter_graph_ids_[i]);
|
||||
}
|
||||
out_ << ' ';
|
||||
Visit(&node->type);
|
||||
out_ << "] = (";
|
||||
Visit(node->value.get());
|
||||
out_ << ")\n";
|
||||
abstract_types_.ExitContext();
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(VariableDefinitionStatement* node) {
|
||||
out_ << "[Variable ";
|
||||
switch (node->modifier) {
|
||||
case VariableDefinitionStatement::Const:
|
||||
out_ << "const";
|
||||
break;
|
||||
case VariableDefinitionStatement::Var:
|
||||
out_ << "var";
|
||||
break;
|
||||
}
|
||||
out_ << ' ';
|
||||
abstract_types_.EnterContext();
|
||||
Visitor::Visit(node->name);
|
||||
out_ << "] = (";
|
||||
Visitor::Visit(node->value);
|
||||
out_ << ")\n";
|
||||
abstract_types_.ExitContext();
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(FunctionDeclaration* node) {
|
||||
out_ << "[FunctionDeclaration ";
|
||||
Visit(&node->name);
|
||||
out_ << "] (";
|
||||
abstract_types_.EnterContext();
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
abstract_types_.DefineType(parameter->type, parameter->type_graph_id_);
|
||||
}
|
||||
out_ << ") : (";
|
||||
Visit(node->type.get());
|
||||
out_ << ")\n";
|
||||
abstract_types_.ExitContext();
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||
out_ << "[Function] (";
|
||||
abstract_types_.EnterContext();
|
||||
Visit(node->definition.get());
|
||||
out_ << ") = (";
|
||||
Visitor::Visit(node->value);
|
||||
out_ << ")\n";
|
||||
abstract_types_.ExitContext();
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TypeDefinitionStatement* node) {
|
||||
out_ << "[Type ";
|
||||
switch (node->modifier) {
|
||||
case TypeDefinitionStatement::Struct:
|
||||
out_ << "struct";
|
||||
break;
|
||||
case TypeDefinitionStatement::Class:
|
||||
out_ << "class";
|
||||
break;
|
||||
}
|
||||
out_ << "] (";
|
||||
abstract_types_.EnterContext();
|
||||
Visit(node->definition.get());
|
||||
out_ << ") = (";
|
||||
Visitor::Visit(node->value);
|
||||
out_ << ")\n";
|
||||
abstract_types_.ExitContext();
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
||||
out_ << "[AbstractType ";
|
||||
switch (node->modifier) {
|
||||
case AbstractTypeDefinitionStatement::Basic:
|
||||
out_ << "basic";
|
||||
break;
|
||||
case AbstractTypeDefinitionStatement::Abstract:
|
||||
out_ << "abstract";
|
||||
break;
|
||||
}
|
||||
out_ << "] (";
|
||||
Visit(node->type.get());
|
||||
out_ << ")\n";
|
||||
// TODO: can't be used before definition
|
||||
abstract_types_.DefineType(node->type->type, node->type->type_graph_id_);
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TypeclassDefinitionStatement* node) {
|
||||
out_ << "[Typeclass] (";
|
||||
abstract_types_.EnterContext();
|
||||
Visit(node->definition.get());
|
||||
if (!node->requirements.empty()) {
|
||||
out_ << ") : (\n";
|
||||
}
|
||||
for (auto& requirement : node->requirements) {
|
||||
out_ << "& ";
|
||||
Visit(requirement.get());
|
||||
out_ << "\n";
|
||||
}
|
||||
out_ << ")\n";
|
||||
abstract_types_.ExitContext();
|
||||
}
|
||||
|
||||
// Definition parts
|
||||
|
||||
void LinkSymbolsVisitor::Visit(FunctionDefinition* node) {
|
||||
out_ << "[FunctionDefinition ";
|
||||
switch (node->modifier) {
|
||||
case FunctionDefinition::Operator:
|
||||
out_ << "operator";
|
||||
break;
|
||||
case FunctionDefinition::Function:
|
||||
out_ << "function";
|
||||
break;
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
abstract_types_.DefineType(parameter->type, parameter->type_graph_id_);
|
||||
}
|
||||
out_ << ' ';
|
||||
Visit(&node->name);
|
||||
out_ << "]";
|
||||
if (!node->parameters.empty()) {
|
||||
out_ << " (";
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
if (!node->arguments.empty()) {
|
||||
out_ << " : (";
|
||||
for (auto& argument : node->arguments) {
|
||||
Visit(&argument);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
out_ << ' ';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TypeDefinition* node) {
|
||||
out_ << "[TypeDefinition] (";
|
||||
Visit(node->type.get());
|
||||
out_ << ')';
|
||||
if (!node->parameters.empty()) {
|
||||
out_ << '(';
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
}
|
||||
out_ << ')';
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
abstract_types_.DefineType(parameter->type, parameter->type_graph_id_);
|
||||
}
|
||||
out_ << ' ';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(AnyAnnotatedType* node) {
|
||||
out_ << "[Annotated (Abstract) Type ";
|
||||
Visit(&node->type);
|
||||
out_ << ']';
|
||||
// TODO check ??
|
||||
if (!node->typeclasses.empty() > 0) {
|
||||
out_ << " (";
|
||||
for (auto& typeclass : node->typeclasses) {
|
||||
Visitor::Visit(typeclass);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
out_ << ' ';
|
||||
}
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
void LinkSymbolsVisitor::Visit(MatchCase* node) {
|
||||
out_ << "[MatchCase | ";
|
||||
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";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(Match* node) {
|
||||
out_ << "[Match] (";
|
||||
Visitor::Visit(node->value);
|
||||
out_ << ") [with] (\n";
|
||||
for (auto& match_case : node->matches) {
|
||||
Visit(&match_case);
|
||||
}
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::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 LinkSymbolsVisitor::Visit(DoWhileLoop* node) {
|
||||
out_ << "[Do] (\n";
|
||||
Visitor::Visit(node->statement);
|
||||
out_ << ") [while] (";
|
||||
Visitor::Visit(node->condition);
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(WhileLoop* node) {
|
||||
out_ << "[While] (";
|
||||
Visitor::Visit(node->statement);
|
||||
out_ << ") [do] (\n";
|
||||
Visitor::Visit(node->condition);
|
||||
out_ << ")\n";
|
||||
Visitor::Visit(node->statement);
|
||||
}
|
||||
void LinkSymbolsVisitor::Visit(ForLoop* node) {
|
||||
out_ << "[For] (";
|
||||
Visitor::Visit(node->variable);
|
||||
out_ << ") [in] (";
|
||||
Visitor::Visit(node->interval);
|
||||
out_ << ") [do] (\n";
|
||||
Visitor::Visit(node->statement);
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(LoopLoop* node) {
|
||||
out_ << "[Loop] (\n";
|
||||
Visitor::Visit(node->statement);
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
// Statements, expressions, blocks, etc. -----------------
|
||||
|
||||
void LinkSymbolsVisitor::Visit(Block* node) {
|
||||
out_ << "[Block] {\n";
|
||||
for (auto& statement : node->statements) {
|
||||
Visitor::Visit(statement);
|
||||
}
|
||||
out_ << "}\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(ScopedStatement* node) {
|
||||
out_ << "[Scoped] ( ";
|
||||
Visitor::Visit(node->statement);
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(LoopControlExpression& node) { // enum
|
||||
switch (node) {
|
||||
case LoopControlExpression::Break:
|
||||
out_ << "[Break]\n";
|
||||
break;
|
||||
case LoopControlExpression::Continue:
|
||||
out_ << "[Continue]\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
void LinkSymbolsVisitor::Visit(LoopControlExpression& node) {}
|
||||
|
||||
// Operators
|
||||
|
||||
void LinkSymbolsVisitor::Visit(BinaryOperatorExpression* node) {
|
||||
out_ << "[BinaryOperator] (";
|
||||
Visitor::Visit(node->left_expression);
|
||||
out_ << ") [";
|
||||
Visit(&node->operator_name);
|
||||
out_ << "] (";
|
||||
Visitor::Visit(node->right_expression);
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(UnaryOperatorExpression* node) {
|
||||
out_ << "[UnaryOperator ";
|
||||
Visit(&node->operator_name);
|
||||
out_ << "] (";
|
||||
Visitor::Visit(node->expression);
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::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
|
||||
|
||||
void LinkSymbolsVisitor::Visit(FunctionCallExpression* node) {
|
||||
out_ << "[FunctionCall ";
|
||||
Visit(node->name.get());
|
||||
out_ << "] (";
|
||||
for (auto& argument : node->arguments) {
|
||||
Visitor::Visit(argument);
|
||||
out_ << ", ";
|
||||
}
|
||||
out_ << ")";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TupleExpression* node) {
|
||||
out_ << "[TupleExpression] (";
|
||||
for (auto& expression : node->expressions) {
|
||||
out_ << "&";
|
||||
Visitor::Visit(expression);
|
||||
}
|
||||
out_ << ")";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(VariantExpression* node) {
|
||||
out_ << "[VariantExpression] (";
|
||||
for (auto& expression : node->expressions) {
|
||||
out_ << "|";
|
||||
Visitor::Visit(expression);
|
||||
}
|
||||
out_ << ")";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(ReturnExpression* node) {
|
||||
out_ << "[Return] (";
|
||||
Visitor::Visit(node->expression);
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TypeConstructor* node) {
|
||||
out_ << "[TypeConstructor ";
|
||||
Visit(node->type.get());
|
||||
out_ << "]\n(";
|
||||
|
||||
bool is_first = true;
|
||||
for (auto& parameter : node->parameters) {
|
||||
if (!is_first) {
|
||||
out_ << ")\n";
|
||||
is_first = false;
|
||||
}
|
||||
out_ << '(';
|
||||
Visit(&std::get<0>(parameter));
|
||||
switch (std::get<1>(parameter)) {
|
||||
case TypeConstructor::Assign:
|
||||
out_ << " = ";
|
||||
break;
|
||||
case TypeConstructor::Move:
|
||||
out_ << " <- ";
|
||||
break;
|
||||
}
|
||||
Visitor::Visit(std::get<2>(parameter));
|
||||
}
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(LambdaFunction* node) {
|
||||
out_ << "[LambdaFunction] (";
|
||||
abstract_types_.EnterContext();
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(parameter.get());
|
||||
abstract_types_.DefineType(parameter->type, parameter->type_graph_id_);
|
||||
}
|
||||
if (!node->parameters.empty()) {
|
||||
out_ << ") : (";
|
||||
}
|
||||
for (auto& argument : node->arguments) {
|
||||
Visit(&argument);
|
||||
}
|
||||
out_ << ") -> (\n";
|
||||
Visitor::Visit(node->expression);
|
||||
out_ << ")\n";
|
||||
abstract_types_.ExitContext();
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(ArrayExpression* node) {
|
||||
out_ << "[ArrayExpression] ([";
|
||||
for (auto& element : node->elements) {
|
||||
Visitor::Visit(element);
|
||||
out_ << ';';
|
||||
}
|
||||
out_ << "])";
|
||||
}
|
||||
|
||||
// Name
|
||||
|
||||
void LinkSymbolsVisitor::Visit(NameExpression* node) {
|
||||
out_ << "[NameExpression] (";
|
||||
for (auto& variable_namespace : node->namespaces) {
|
||||
Visitor::Visit(variable_namespace);
|
||||
out_ << '.';
|
||||
}
|
||||
for (size_t i = 0; i < node->expressions.size(); ++i) {
|
||||
Visitor::Visit(node->expressions[i]);
|
||||
if (i + 1 < node->expressions.size()) {
|
||||
out_ << '.';
|
||||
}
|
||||
for (auto& expression : node->expressions) {
|
||||
Visitor::Visit(expression);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TupleName* node) {
|
||||
out_ << "[TupleName] (";
|
||||
for (auto& name : node->names) {
|
||||
out_ << "& ";
|
||||
Visitor::Visit(name);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(VariantName* node) {
|
||||
out_ << "[VariantName] (";
|
||||
for (auto& name : node->names) {
|
||||
out_ << "| ";
|
||||
Visitor::Visit(name);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(AnnotatedName* node) {
|
||||
out_ << "[AnnotatedName ";
|
||||
Visit(&node->name);
|
||||
out_ << ']';
|
||||
if (node->type.has_value()) {
|
||||
out_ << " : (";
|
||||
Visitor::Visit(node->type.value());
|
||||
out_ << ')';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -541,143 +285,117 @@ void LinkSymbolsVisitor::Visit(AnnotatedName* node) {
|
|||
// Type
|
||||
|
||||
void LinkSymbolsVisitor::Visit(FunctionType* node) {
|
||||
out_ << "[FunctionType] (";
|
||||
bool is_first = true;
|
||||
for (auto& type : node->types) {
|
||||
if (!is_first) {
|
||||
out_ << " -> ";
|
||||
}
|
||||
is_first = false;
|
||||
Visitor::Visit(type);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TupleType* node) {
|
||||
out_ << "[TupleType ";
|
||||
if (node->type.has_value()) {
|
||||
Visit(&node->type.value());
|
||||
}
|
||||
out_ << "] (";
|
||||
for (auto& entity : node->entities) {
|
||||
out_ << "& ";
|
||||
if (entity.first.has_value()) {
|
||||
Visit(&entity.first.value());
|
||||
out_ << " : ";
|
||||
}
|
||||
Visit(entity.second.get());
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(VariantType* node) {
|
||||
out_ << "[VariantType ";
|
||||
if (node->type.has_value()) {
|
||||
Visit(&node->type.value());
|
||||
}
|
||||
out_ << "] (";
|
||||
for (auto& constructor : node->constructors) {
|
||||
out_ << "| ";
|
||||
if (std::holds_alternative<Constructor>(constructor)) {
|
||||
Visit(&std::get<Constructor>(constructor));
|
||||
// do nothing
|
||||
} else if (std::holds_alternative<std::unique_ptr<TupleType>>(constructor)) {
|
||||
Visit(std::get<std::unique_ptr<TupleType>>(constructor).get());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(ParametrizedType* node) {
|
||||
out_ << "[ParametrizedType] (";
|
||||
Visit(node->type_expression.get());
|
||||
for (auto& parameter : node->parameters) {
|
||||
out_ << ' ';
|
||||
Visitor::Visit(parameter);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TypeExpression* node) {
|
||||
out_ << "[TypeExpression ";
|
||||
std::vector<std::string> path;
|
||||
path.reserve(node->namespaces.size());
|
||||
|
||||
if (node->array_size.has_value()) {
|
||||
out_ << "[array size: " << node->array_size.value() << ']';
|
||||
}
|
||||
|
||||
out_ << "] (";
|
||||
for (auto& type_namespace : node->namespaces) {
|
||||
Visitor::Visit(type_namespace);
|
||||
out_ << '.';
|
||||
if (std::holds_alternative<std::unique_ptr<std::string>>(type_namespace)) {
|
||||
path.push_back(*std::get<std::unique_ptr<std::string>>(type_namespace));
|
||||
} else if (std::holds_alternative<std::unique_ptr<ParametrizedType>>(type_namespace)) {
|
||||
path.push_back(std::get<std::unique_ptr<ParametrizedType>>(type_namespace)->type_expression->type);
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> maybe_type = namespace_visitor_.FindType(path, node->type);
|
||||
std::optional<utils::IdType> maybe_abstract_type = std::nullopt;
|
||||
|
||||
if (path.size() == 0) {
|
||||
maybe_abstract_type = abstract_types_.GetTypeId(node->type);
|
||||
}
|
||||
|
||||
if (maybe_abstract_type.has_value()) {
|
||||
if (maybe_type.has_value()) {
|
||||
error_handling::HandleTypecheckError("Ambigious type");
|
||||
} else {
|
||||
node->type_id_ = maybe_abstract_type.value();
|
||||
}
|
||||
} else if (maybe_type.has_value()) {
|
||||
node->type_id_ = maybe_type.value();
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("Type not found");
|
||||
}
|
||||
Visit(&node->type);
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(ExtendedScopedAnyType* node) {
|
||||
out_ << "[ExtendedScopedAnyType ";
|
||||
for (auto& reference : node->references) {
|
||||
switch (reference) {
|
||||
case ReferenceType::Reference:
|
||||
out_ << '~';
|
||||
break;
|
||||
case ReferenceType::UniqueReference:
|
||||
out_ << '@';
|
||||
break;
|
||||
}
|
||||
}
|
||||
out_ << "] (";
|
||||
Visitor::Visit(node->type);
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
// Typeclass
|
||||
|
||||
void LinkSymbolsVisitor::Visit(ParametrizedTypeclass* node) {
|
||||
out_ << "[ParametrizedTypeclass] (";
|
||||
Visit(node->typeclass_expression.get());
|
||||
for (auto& paramater : node->parameters) {
|
||||
out_ << ' ';
|
||||
Visitor::Visit(paramater);
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visitor::Visit(parameter);
|
||||
}
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(TypeclassExpression* node) {
|
||||
out_ << "[TypeclassExpression] (";
|
||||
std::vector<std::string> path;
|
||||
path.reserve(node->namespaces.size());
|
||||
|
||||
for (auto& typeclass_namespace : node->namespaces) {
|
||||
Visitor::Visit(typeclass_namespace);
|
||||
out_ << '.';
|
||||
if (std::holds_alternative<std::unique_ptr<std::string>>(typeclass_namespace)) {
|
||||
path.push_back(*std::get<std::unique_ptr<std::string>>(typeclass_namespace));
|
||||
} else if (std::holds_alternative<std::unique_ptr<ParametrizedType>>(typeclass_namespace)) {
|
||||
path.push_back(std::get<std::unique_ptr<ParametrizedType>>(typeclass_namespace)->type_expression->type);
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
}
|
||||
Visit(&node->typeclass);
|
||||
out_ << ')';
|
||||
}
|
||||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
std::optional<utils::IdType> maybe_typeclass = namespace_visitor_.FindType(path, node->typeclass);
|
||||
std::optional<utils::IdType> maybe_abstract_typeclass = std::nullopt;
|
||||
|
||||
void LinkSymbolsVisitor::Visit(ExtendedName* node) {
|
||||
out_ << "[ExtendedName " << node->name << "] ";
|
||||
}
|
||||
if (path.size() == 0) {
|
||||
maybe_abstract_typeclass = abstract_types_.GetTypeId(node->typeclass);
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(std::string* node) { // std::string
|
||||
out_ << "[Identifier " << *node << "] ";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(FloatNumberLiteral* node) {
|
||||
out_ << "[FloatNumber " << node->value << "] ";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(NumberLiteral* node) {
|
||||
out_ << "[Number " << node->value << "] ";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(StringLiteral* node) {
|
||||
out_ << "[String " << node->value << "] ";
|
||||
}
|
||||
|
||||
void LinkSymbolsVisitor::Visit(CharLiteral* node) {
|
||||
out_ << "[Char " << node->value << "] ";
|
||||
if (maybe_abstract_typeclass.has_value()) {
|
||||
if (maybe_typeclass.has_value()) {
|
||||
error_handling::HandleTypecheckError("Ambigious type");
|
||||
} else {
|
||||
node->type_id_ = maybe_abstract_typeclass.value();
|
||||
}
|
||||
} else if (maybe_typeclass.has_value()) {
|
||||
node->type_id_ = maybe_typeclass.value();
|
||||
} else {
|
||||
error_handling::HandleTypecheckError("Type not found");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace interpreter
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue