mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
build_visitor first iteration
This commit is contained in:
parent
622b86f6c6
commit
582ad5668e
5 changed files with 338 additions and 116 deletions
|
|
@ -131,7 +131,7 @@ private:
|
||||||
|
|
||||||
// Identifiers, constants, etc. -----------------
|
// Identifiers, constants, etc. -----------------
|
||||||
|
|
||||||
// // void Visit(AnyIdentifier* node) override; // std::string
|
void Visit(AnyIdentifier* node) override; // std::string
|
||||||
|
|
||||||
void Visit(FloatNumberLiteral* node) override;
|
void Visit(FloatNumberLiteral* node) override;
|
||||||
void Visit(NumberLiteral* node) override;
|
void Visit(NumberLiteral* node) override;
|
||||||
|
|
|
||||||
|
|
@ -480,7 +480,7 @@ struct LambdaFunction : public Node {
|
||||||
|
|
||||||
struct NameSuperExpression : public Node {
|
struct NameSuperExpression : public Node {
|
||||||
std::vector<TypeSubExpression> namespaces;
|
std::vector<TypeSubExpression> namespaces;
|
||||||
std::vector<NameSubSuperExpression> expressions; // last is not SuperExpression
|
std::vector<NameSubSuperExpression> expressions;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct NameExpression : public Node {
|
struct NameExpression : public Node {
|
||||||
|
|
@ -511,13 +511,13 @@ struct TypeConstructor : public Node {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TupleType : public Node {
|
struct TupleType : public Node {
|
||||||
TypeIdentifierDefinition type; // optional
|
std::optional<TypeIdentifierDefinition> type;
|
||||||
std::vector<std::pair<NameIdentifier, AnyType>> entities; // NameIdentifier is optional
|
std::vector<std::pair<std::optional<NameIdentifier>, AnyType>> entities;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct VariantType : public Node {
|
struct VariantType : public Node {
|
||||||
TypeIdentifierDefinition type; // optional
|
std::optional<TypeIdentifierDefinition> type;
|
||||||
std::vector<std::pair<TypeIdentifierDefinition, std::unique_ptr<TupleType>>> constructors;
|
std::vector<std::variant<TypeIdentifierDefinition, std::unique_ptr<TupleType>>> constructors;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AnnotatedType : public Node {
|
struct AnnotatedType : public Node {
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,8 @@ const std::string TypeclassExpression = "typeclass_expression";
|
||||||
|
|
||||||
// Identifiers, constants, etc. -----------------
|
// Identifiers, constants, etc. -----------------
|
||||||
|
|
||||||
|
const std::string TypeIdentifierDefinition = "type_identifier_definition";
|
||||||
|
|
||||||
const std::string TypeclassIdentifier = "typeclass_identifer";
|
const std::string TypeclassIdentifier = "typeclass_identifer";
|
||||||
const std::string NameIdentifier = "name_identifier";
|
const std::string NameIdentifier = "name_identifier";
|
||||||
const std::string TypeIdentifier = "type_identifer";
|
const std::string TypeIdentifier = "type_identifer";
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ void BuildVisitor::Visit(SourceFile* node) {
|
||||||
Visit(&std::get<Partition>(node->statements[i]));
|
Visit(&std::get<Partition>(node->statements[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
current_node_ = parse_node;
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -38,6 +39,7 @@ void BuildVisitor::Visit(Sources* node) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i);
|
||||||
Visit(node->statements[i]);
|
Visit(node->statements[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
current_node_ = parse_node;
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,8 +48,7 @@ void BuildVisitor::Visit(Sources* node) {
|
||||||
void BuildVisitor::Visit(Partition* node) {
|
void BuildVisitor::Visit(Partition* node) {
|
||||||
auto parse_node = current_node_;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
auto name_node = parse_node.ChildByFieldName("name");
|
std::string name = parse_node.ChildByFieldName("name").GetValue();
|
||||||
std::string name = name_node.GetValue();
|
|
||||||
|
|
||||||
if (name == "TEST") {
|
if (name == "TEST") {
|
||||||
node->type = Partition::Test;
|
node->type = Partition::Test;
|
||||||
|
|
@ -113,8 +114,8 @@ void BuildVisitor::Visit(ImportStatement* node) {
|
||||||
if (child_count > 1) {
|
if (child_count > 1) {
|
||||||
node->symbols.resize(child_count - 1);
|
node->symbols.resize(child_count - 1);
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
Visit(node->symbols[i]);
|
Visit(node->symbols[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -169,16 +170,15 @@ void BuildVisitor::Visit(VariableDefinition* node) {
|
||||||
void BuildVisitor::Visit(FunctionDeclaration* node) {
|
void BuildVisitor::Visit(FunctionDeclaration* node) {
|
||||||
auto parse_node = current_node_;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
auto name_node = parse_node.ChildByFieldName("name");
|
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||||
std::string name = name_node.GetValue();
|
|
||||||
|
|
||||||
size_t child_count = parse_node.NamedChildCount();
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
|
|
||||||
if (child_count > 1) {
|
if (child_count > 1) {
|
||||||
bool parameters_ended = false;
|
bool parameters_ended = false;
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
|
||||||
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
||||||
parameters_ended = true;
|
parameters_ended = true;
|
||||||
|
|
@ -256,8 +256,8 @@ void BuildVisitor::Visit(TypeclassDefinition* node) {
|
||||||
if (child_count > 1) {
|
if (child_count > 1) {
|
||||||
node->requirements.resize(child_count - 1, std::make_unique<FunctionDeclaration>());
|
node->requirements.resize(child_count - 1, std::make_unique<FunctionDeclaration>());
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
Visit(node->requirements[i].get());
|
Visit(node->requirements[i].get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -339,8 +339,7 @@ void BuildVisitor::Visit(ImportSymbol& node) {
|
||||||
void BuildVisitor::Visit(DefinedName* node) {
|
void BuildVisitor::Visit(DefinedName* node) {
|
||||||
auto parse_node = current_node_;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
auto name_node = parse_node.ChildByFieldName("name"); // Operator or NameIdentifier
|
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||||
std::string name = name_node.GetValue();
|
|
||||||
|
|
||||||
node->is_operator = (parse_node.NthChild(0).GetValue() == "("); // TODO
|
node->is_operator = (parse_node.NthChild(0).GetValue() == "("); // TODO
|
||||||
|
|
||||||
|
|
@ -349,8 +348,8 @@ void BuildVisitor::Visit(DefinedName* node) {
|
||||||
if (child_count > 1) {
|
if (child_count > 1) {
|
||||||
bool parameters_ended = false;
|
bool parameters_ended = false;
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
|
||||||
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
||||||
parameters_ended = true;
|
parameters_ended = true;
|
||||||
|
|
@ -372,8 +371,7 @@ void BuildVisitor::Visit(DefinedName* node) {
|
||||||
void BuildVisitor::Visit(DefinedAnnotatedName* node) {
|
void BuildVisitor::Visit(DefinedAnnotatedName* node) {
|
||||||
auto parse_node = current_node_;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
auto name_node = parse_node.ChildByFieldName("name");
|
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||||
std::string name = name_node.GetValue();
|
|
||||||
|
|
||||||
size_t child_count = parse_node.NamedChildCount();
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
|
|
||||||
|
|
@ -405,8 +403,8 @@ void BuildVisitor::Visit(DefinedType* node) {
|
||||||
if (child_count > 1) {
|
if (child_count > 1) {
|
||||||
bool parameters_ended = false;
|
bool parameters_ended = false;
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
|
||||||
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
||||||
parameters_ended = true;
|
parameters_ended = true;
|
||||||
|
|
@ -437,8 +435,8 @@ void BuildVisitor::Visit(DefinedTypeclass* node) {
|
||||||
if (child_count > 1) {
|
if (child_count > 1) {
|
||||||
bool parameters_ended = false;
|
bool parameters_ended = false;
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
|
||||||
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
||||||
parameters_ended = true;
|
parameters_ended = true;
|
||||||
|
|
@ -460,16 +458,15 @@ void BuildVisitor::Visit(DefinedTypeclass* node) {
|
||||||
void BuildVisitor::Visit(DefinitionParameter* node) {
|
void BuildVisitor::Visit(DefinitionParameter* node) {
|
||||||
auto parse_node = current_node_;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
auto name_node = parse_node.ChildByFieldName("type");
|
node->type = parse_node.ChildByFieldName("type").GetValue();
|
||||||
std::string name = name_node.GetValue();
|
|
||||||
|
|
||||||
size_t child_count = parse_node.NamedChildCount();
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
|
|
||||||
if (child_count > 1) {
|
if (child_count > 1) {
|
||||||
node->typeclasses.resize(child_count - 1, std::make_unique<ParametrizedTypeclass>());
|
node->typeclasses.resize(child_count - 1, std::make_unique<ParametrizedTypeclass>());
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
Visit(node->typeclasses[i].get());
|
Visit(node->typeclasses[i].get());
|
||||||
// choose between typeclass_expression and parametrized_typeclass
|
// choose between typeclass_expression and parametrized_typeclass
|
||||||
}
|
}
|
||||||
|
|
@ -481,16 +478,15 @@ void BuildVisitor::Visit(DefinitionParameter* node) {
|
||||||
void BuildVisitor::Visit(DefinitionArgument* node) {
|
void BuildVisitor::Visit(DefinitionArgument* node) {
|
||||||
auto parse_node = current_node_;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
auto name_node = parse_node.ChildByFieldName("name");
|
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||||
std::string name = name_node.GetValue();
|
|
||||||
|
|
||||||
size_t child_count = parse_node.NamedChildCount();
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
|
|
||||||
if (child_count > 1) {
|
if (child_count > 1) {
|
||||||
node->types.resize(child_count - 1, std::make_unique<ParametrizedType>());
|
node->types.resize(child_count - 1, std::make_unique<ParametrizedType>());
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
Visit(node->types[i].get());
|
Visit(node->types[i].get());
|
||||||
// choose between type_expression and parametrized_type
|
// choose between type_expression and parametrized_type
|
||||||
}
|
}
|
||||||
|
|
@ -565,8 +561,8 @@ void BuildVisitor::Visit(Match* node) {
|
||||||
// if (child_count > 1) { // always true (repeat1)
|
// if (child_count > 1) { // always true (repeat1)
|
||||||
node->matches.resize(child_count - 1);
|
node->matches.resize(child_count - 1);
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
Visit(&node->matches[i]);
|
Visit(&node->matches[i]);
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
|
|
@ -905,8 +901,8 @@ void BuildVisitor::Visit(FunctionCallExpression* node) {
|
||||||
// if (child_count > 1) { // always true (repeat1)
|
// if (child_count > 1) { // always true (repeat1)
|
||||||
node->arguments.resize(child_count - 1);
|
node->arguments.resize(child_count - 1);
|
||||||
|
|
||||||
for (size_t i = 1; i < child_count; ++i) {
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
current_node_ = parse_node.NthNamedChild(i);
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
Visit(node->arguments[i]);
|
Visit(node->arguments[i]);
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
|
|
@ -1010,8 +1006,45 @@ void BuildVisitor::Visit(LambdaFunction* node) {
|
||||||
|
|
||||||
// Name
|
// Name
|
||||||
|
|
||||||
void BuildVisitor::Visit(NameSuperExpression* node) { // TODO
|
void BuildVisitor::Visit(NameSuperExpression* node) {
|
||||||
TODO;
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
|
|
||||||
|
bool namespaces_ended = false;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < child_count; ++i) {
|
||||||
|
current_node_ = parse_node.NthNamedChild(i);
|
||||||
|
std::string current_node_type = current_node_.GetType();
|
||||||
|
|
||||||
|
if (current_node_type != parser::tokens::TypeSubExpression) {
|
||||||
|
namespaces_ended = true;
|
||||||
|
|
||||||
|
if (i + 1 == child_count) {
|
||||||
|
node->expressions.emplace_back();
|
||||||
|
|
||||||
|
if (current_node_type == parser::tokens::NameIdentifier) { // optimize ??
|
||||||
|
node->expressions.back() = std::make_unique<NameIdentifier>(current_node_.GetValue());
|
||||||
|
} else if (current_node_type == parser::tokens::Literal) {
|
||||||
|
node->expressions.back() = std::make_unique<Literal>();
|
||||||
|
Visit(*std::get<std::unique_ptr<Literal>>(node->expressions.back()).get());
|
||||||
|
} else {
|
||||||
|
// error
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!namespaces_ended) {
|
||||||
|
node->namespaces.emplace_back();
|
||||||
|
Visit(node->namespaces.back());
|
||||||
|
} else {
|
||||||
|
node->expressions.emplace_back();
|
||||||
|
Visit(node->expressions.back());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(NameExpression* node) {
|
void BuildVisitor::Visit(NameExpression* node) {
|
||||||
|
|
@ -1111,121 +1144,299 @@ void BuildVisitor::Visit(AnyName& node) {
|
||||||
// Type
|
// Type
|
||||||
|
|
||||||
void BuildVisitor::Visit(TypeConstructor* node) {
|
void BuildVisitor::Visit(TypeConstructor* node) {
|
||||||
out_ << "<TypeConstructor> ";
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.ChildByFieldName("type");
|
||||||
|
node->type = std::make_unique<ParametrizedType>();
|
||||||
Visit(node->type.get());
|
Visit(node->type.get());
|
||||||
out_ << '\n';
|
|
||||||
for (auto& parameter : node->parameters) {
|
size_t parameter_count = (parse_node.NamedChildCount() - 1) / 2;
|
||||||
Visit(¶meter.first);
|
|
||||||
out_ << " = ";
|
node->parameters.resize(parameter_count);
|
||||||
Visitor::Visit(parameter.second);
|
|
||||||
out_ << '\n';
|
for (size_t i = 0; i < parameter_count * 2; ++i) {
|
||||||
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
|
||||||
|
if (i % 2 == 0) {
|
||||||
|
node->parameters[i / 2].first = current_node_.GetValue();
|
||||||
|
} else {
|
||||||
|
Visit(node->parameters[i / 2].second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out_ << "</TypeConstructor>\n";
|
|
||||||
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(TupleType* node) {
|
void BuildVisitor::Visit(TupleType* node) {
|
||||||
out_ << "<TupleType> ";
|
auto parse_node = current_node_;
|
||||||
Visit(&node->type); // optional
|
|
||||||
out_ << ' ';
|
size_t current_node_n = 0;
|
||||||
for (auto& entity : node->entities) {
|
|
||||||
out_ << "& ";
|
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||||
if (entity.first != "") {
|
|
||||||
Visit(&entity.first);
|
if (current_node_.GetType() == parser::tokens::TypeIdentifierDefinition) {
|
||||||
out_ << " : ";
|
node->type = current_node_.GetValue(); // TODO check
|
||||||
}
|
|
||||||
Visitor::Visit(entity.second);
|
++current_node_n;
|
||||||
|
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||||
}
|
}
|
||||||
out_ << "</TupleType>";
|
|
||||||
|
while (current_node_n < parse_node.NamedChildCount()) {
|
||||||
|
node->entities.emplace_back();
|
||||||
|
|
||||||
|
if (current_node_.GetType() == parser::tokens::NameIdentifier) {
|
||||||
|
node->entities.back().first = current_node_.GetValue();
|
||||||
|
|
||||||
|
++current_node_n;
|
||||||
|
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||||
|
}
|
||||||
|
|
||||||
|
Visit(node->entities.back().second);
|
||||||
|
|
||||||
|
++current_node_n;
|
||||||
|
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(VariantType* node) {
|
void BuildVisitor::Visit(VariantType* node) {
|
||||||
out_ << "<VariantType> ";
|
auto parse_node = current_node_;
|
||||||
Visit(&node->type); // optional
|
|
||||||
out_ << ' ';
|
size_t current_node_n = 0;
|
||||||
for (auto& constructor : node->constructors) {
|
|
||||||
out_ << "| ";
|
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||||
Visit(&constructor.first);
|
|
||||||
Visit(constructor.second.get());
|
if (current_node_.GetType() == parser::tokens::TypeIdentifierDefinition) {
|
||||||
|
node->type = current_node_.GetValue(); // TODO check
|
||||||
|
|
||||||
|
++current_node_n;
|
||||||
|
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||||
}
|
}
|
||||||
out_ << "</VariantType>";
|
|
||||||
|
while (current_node_n < parse_node.NamedChildCount()) {
|
||||||
|
std::string current_node_type = current_node_.GetType();
|
||||||
|
|
||||||
|
node->constructors.emplace_back();
|
||||||
|
|
||||||
|
if (current_node_type == parser::tokens::TypeIdentifierDefinition) { // optimize ??
|
||||||
|
node->constructors.back() = current_node_.GetValue(); // TODO check
|
||||||
|
} else if (current_node_type == parser::tokens::TupleType) {
|
||||||
|
node->constructors.back() = std::make_unique<TupleType>();
|
||||||
|
Visit(std::get<std::unique_ptr<TupleType>>(node->constructors.back()).get());
|
||||||
|
} else {
|
||||||
|
// error
|
||||||
|
}
|
||||||
|
|
||||||
|
++current_node_n;
|
||||||
|
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(AnnotatedType* node) {
|
void BuildVisitor::Visit(AnnotatedType* node) {
|
||||||
out_ << "<AnnotatedType> ";
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.ChildByFieldName("type_expression");
|
||||||
|
node->type_expression = std::make_unique<TypeExpression>();
|
||||||
Visit(node->type_expression.get());
|
Visit(node->type_expression.get());
|
||||||
if (node->annotations.size() > 0) {
|
|
||||||
out_ << " :";
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
|
|
||||||
|
if (child_count > 1) {
|
||||||
|
node->annotations.resize(child_count - 1, std::make_unique<ParametrizedTypeclass>());
|
||||||
|
|
||||||
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
Visit(node->annotations[i].get());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (auto& annotation : node->annotations) {
|
|
||||||
out_ << " ";
|
current_node_ = parse_node;
|
||||||
Visit(annotation.get());
|
|
||||||
}
|
|
||||||
out_ << "</AnnotatedType>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(ParametrizedType* node) {
|
void BuildVisitor::Visit(ParametrizedType* node) {
|
||||||
out_ << "<ParametrizedType> ";
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.ChildByFieldName("type_expression");
|
||||||
|
node->type_expression = std::make_unique<TypeExpression>();
|
||||||
Visit(node->type_expression.get());
|
Visit(node->type_expression.get());
|
||||||
for (auto& paramater : node->parameters) {
|
|
||||||
out_ << ' ';
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
Visitor::Visit(paramater);
|
|
||||||
|
if (child_count > 1) {
|
||||||
|
node->parameters.resize(child_count - 1);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
Visit(node->parameters[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out_ << " </ParamtrizedType>";
|
|
||||||
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(TypeExpression* node) {
|
void BuildVisitor::Visit(TypeExpression* node) {
|
||||||
out_ << "<TypeExpression> ";
|
auto parse_node = current_node_;
|
||||||
for (auto& type_namespace : node->namespaces) {
|
|
||||||
Visitor::Visit(type_namespace);
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
out_ << '.';
|
|
||||||
|
if (child_count > 1) {
|
||||||
|
node->namespaces.resize(child_count - 1);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
|
current_node_ = parse_node.NthNamedChild(i);
|
||||||
|
Visit(node->namespaces[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Visit(&node->type);
|
|
||||||
out_ << " </TypeExpression>";
|
node->type = parse_node.ChildByFieldName("type").GetValue();
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BuildVisitor::Visit(AnyType& node) {
|
||||||
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.NthNamedChild(0);
|
||||||
|
|
||||||
|
std::string current_node_type = current_node_.GetType();
|
||||||
|
|
||||||
|
if (current_node_type == parser::tokens::ParametrizedType) { // optimize ??
|
||||||
|
node = std::make_unique<ParametrizedType>();
|
||||||
|
Visit(std::get<std::unique_ptr<ParametrizedType>>(node).get());
|
||||||
|
} else if (current_node_type == parser::tokens::TupleType) {
|
||||||
|
node = std::make_unique<TupleType>();
|
||||||
|
Visit(std::get<std::unique_ptr<TupleType>>(node).get());
|
||||||
|
} else if (current_node_type == parser::tokens::VariantType) {
|
||||||
|
node = std::make_unique<VariantType>();
|
||||||
|
Visit(std::get<std::unique_ptr<VariantType>>(node).get());
|
||||||
|
} else {
|
||||||
|
// error
|
||||||
|
}
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BuildVisitor::Visit(TypeSubExpression& node) {
|
||||||
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.NthNamedChild(0);
|
||||||
|
|
||||||
|
std::string current_node_type = current_node_.GetType();
|
||||||
|
|
||||||
|
if (current_node_type == parser::tokens::TypeIdentifier) { // optimize ??
|
||||||
|
node = std::make_unique<TypeIdentifier>();
|
||||||
|
Visit(std::get<std::unique_ptr<TypeIdentifier>>(node).get());
|
||||||
|
} else if (current_node_type == parser::tokens::AbstractTypeIdentifier) {
|
||||||
|
node = std::make_unique<AbstractTypeIdentifier>();
|
||||||
|
Visit(std::get<std::unique_ptr<AbstractTypeIdentifier>>(node).get());
|
||||||
|
} else if (current_node_type == parser::tokens::ParametrizedType) {
|
||||||
|
node = std::make_unique<ParametrizedType>();
|
||||||
|
Visit(std::get<std::unique_ptr<ParametrizedType>>(node).get());
|
||||||
|
} else {
|
||||||
|
// error
|
||||||
|
}
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BuildVisitor::Visit(TypeParameter& node) {
|
||||||
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.NthNamedChild(0);
|
||||||
|
|
||||||
|
std::string current_node_type = current_node_.GetType();
|
||||||
|
|
||||||
|
if (current_node_type == parser::tokens::ParametrizedType) { // optimize ??
|
||||||
|
node = std::make_unique<ParametrizedType>();
|
||||||
|
Visit(std::get<std::unique_ptr<ParametrizedType>>(node).get());
|
||||||
|
} else if (current_node_type == parser::tokens::Expression) {
|
||||||
|
node = std::make_unique<Expression>();
|
||||||
|
Visit(*std::get<std::unique_ptr<Expression>>(node).get());
|
||||||
|
} else {
|
||||||
|
// error
|
||||||
|
}
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Typeclass
|
// Typeclass
|
||||||
|
|
||||||
void BuildVisitor::Visit(AnnotatedTypeclass* node) {
|
void BuildVisitor::Visit(AnnotatedTypeclass* node) {
|
||||||
out_ << "<AnnotatedTypeclass> ";
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.ChildByFieldName("typeclass_expression");
|
||||||
|
node->typeclass_expression = std::make_unique<TypeclassExpression>();
|
||||||
Visit(node->typeclass_expression.get());
|
Visit(node->typeclass_expression.get());
|
||||||
if (node->annotations.size() > 0) {
|
|
||||||
out_ << " :";
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
|
|
||||||
|
if (child_count > 1) {
|
||||||
|
node->annotations.resize(child_count - 1, std::make_unique<ParametrizedTypeclass>());
|
||||||
|
|
||||||
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
Visit(node->annotations[i].get());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for (auto& annotation : node->annotations) {
|
|
||||||
out_ << " ";
|
current_node_ = parse_node;
|
||||||
Visit(annotation.get());
|
|
||||||
}
|
|
||||||
out_ << "</AnnotatedTypeclass>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(ParametrizedTypeclass* node) {
|
void BuildVisitor::Visit(ParametrizedTypeclass* node) {
|
||||||
out_ << "<ParametrizedTypeclass> ";
|
auto parse_node = current_node_;
|
||||||
|
|
||||||
|
current_node_ = parse_node.ChildByFieldName("typeclass_expression");
|
||||||
|
node->typeclass_expression = std::make_unique<TypeclassExpression>();
|
||||||
Visit(node->typeclass_expression.get());
|
Visit(node->typeclass_expression.get());
|
||||||
for (auto& paramater : node->parameters) {
|
|
||||||
out_ << ' ';
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
Visitor::Visit(paramater);
|
|
||||||
|
if (child_count > 1) {
|
||||||
|
node->parameters.resize(child_count - 1);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
|
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||||
|
Visit(node->parameters[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out_ << " </ParamtrizedTypeclass>";
|
|
||||||
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(TypeclassExpression* node) {
|
void BuildVisitor::Visit(TypeclassExpression* node) {
|
||||||
out_ << "<TypeclassExpression> ";
|
auto parse_node = current_node_;
|
||||||
for (auto& typeclass_namespace : node->namespaces) {
|
|
||||||
Visitor::Visit(typeclass_namespace);
|
size_t child_count = parse_node.NamedChildCount();
|
||||||
out_ << '.';
|
|
||||||
|
if (child_count > 1) {
|
||||||
|
node->namespaces.resize(child_count - 1);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||||
|
current_node_ = parse_node.NthNamedChild(i);
|
||||||
|
Visit(node->namespaces[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Visit(&node->typeclass);
|
|
||||||
out_ << " </TypeclassExpression>";
|
node->typeclass = parse_node.ChildByFieldName("typeclass").GetValue();
|
||||||
|
|
||||||
|
current_node_ = parse_node;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identifiers, constants, etc. -----------------
|
// Identifiers, constants, etc. -----------------
|
||||||
|
|
||||||
|
void BuildVisitor::Visit(AnyIdentifier* node) { // std::string
|
||||||
|
*node = current_node_.GetValue();
|
||||||
|
} // TODO use in other places ??
|
||||||
|
|
||||||
void BuildVisitor::Visit(FloatNumberLiteral* node) {
|
void BuildVisitor::Visit(FloatNumberLiteral* node) {
|
||||||
node->value = current_node_.NthChild(0).GetValue();
|
node->value = std::stod(current_node_.NthChild(0).GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(NumberLiteral* node) {
|
void BuildVisitor::Visit(NumberLiteral* node) {
|
||||||
node->value = current_node_.NthChild(0).GetValue();
|
node->value = std::stoll(current_node_.NthChild(0).GetValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(StringLiteral* node) {
|
void BuildVisitor::Visit(StringLiteral* node) {
|
||||||
|
|
@ -1233,7 +1444,7 @@ void BuildVisitor::Visit(StringLiteral* node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(CharLiteral* node) {
|
void BuildVisitor::Visit(CharLiteral* node) {
|
||||||
node->value = current_node_.NthChild(1).GetValue();
|
node->value = current_node_.NthChild(1).GetValue()[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildVisitor::Visit(Literal& node) {
|
void BuildVisitor::Visit(Literal& node) {
|
||||||
|
|
@ -1274,10 +1485,10 @@ void BuildVisitor::Visit(NameSubSuperExpression& node) {
|
||||||
Visit(std::get<std::unique_ptr<NameIdentifier>>(node).get());
|
Visit(std::get<std::unique_ptr<NameIdentifier>>(node).get());
|
||||||
} else if (current_node_type == parser::tokens::Literal) {
|
} else if (current_node_type == parser::tokens::Literal) {
|
||||||
node = std::make_unique<Literal>();
|
node = std::make_unique<Literal>();
|
||||||
Visit(std::get<std::unique_ptr<Literal>>(node).get());
|
Visit(*std::get<std::unique_ptr<Literal>>(node).get());
|
||||||
} else if (current_node_type == parser::tokens::SuperExpression) {
|
} else if (current_node_type == parser::tokens::SuperExpression) {
|
||||||
node = std::make_unique<SuperExpression>();
|
node = std::make_unique<SuperExpression>();
|
||||||
Visit(std::get<std::unique_ptr<SuperExpression>>(node).get());
|
Visit(*std::get<std::unique_ptr<SuperExpression>>(node).get());
|
||||||
} else {
|
} else {
|
||||||
// error
|
// error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -516,12 +516,14 @@ void PrintVisitor::Visit(TypeConstructor* node) {
|
||||||
|
|
||||||
void PrintVisitor::Visit(TupleType* node) {
|
void PrintVisitor::Visit(TupleType* node) {
|
||||||
out_ << "<TupleType> ";
|
out_ << "<TupleType> ";
|
||||||
Visit(&node->type); // optional
|
if (node->type.has_value()) {
|
||||||
|
Visit(&node->type.value()); // optional
|
||||||
|
}
|
||||||
out_ << ' ';
|
out_ << ' ';
|
||||||
for (auto& entity : node->entities) {
|
for (auto& entity : node->entities) {
|
||||||
out_ << "& ";
|
out_ << "& ";
|
||||||
if (entity.first != "") {
|
if (entity.first.has_value()) {
|
||||||
Visit(&entity.first);
|
Visit(&entity.first.value());
|
||||||
out_ << " : ";
|
out_ << " : ";
|
||||||
}
|
}
|
||||||
Visitor::Visit(entity.second);
|
Visitor::Visit(entity.second);
|
||||||
|
|
@ -531,12 +533,19 @@ void PrintVisitor::Visit(TupleType* node) {
|
||||||
|
|
||||||
void PrintVisitor::Visit(VariantType* node) {
|
void PrintVisitor::Visit(VariantType* node) {
|
||||||
out_ << "<VariantType> ";
|
out_ << "<VariantType> ";
|
||||||
Visit(&node->type); // optional
|
if (node->type.has_value()) {
|
||||||
|
Visit(&node->type.value());
|
||||||
|
}
|
||||||
out_ << ' ';
|
out_ << ' ';
|
||||||
for (auto& constructor : node->constructors) {
|
for (auto& constructor : node->constructors) {
|
||||||
out_ << "| ";
|
out_ << "| ";
|
||||||
Visit(&constructor.first);
|
if (std::holds_alternative<TypeIdentifierDefinition>(constructor)) {
|
||||||
Visit(constructor.second.get());
|
Visit(&std::get<TypeIdentifierDefinition>(constructor));
|
||||||
|
} else if (std::holds_alternative<std::unique_ptr<TupleType>>(constructor)) {
|
||||||
|
Visit(std::get<std::unique_ptr<TupleType>>(constructor).get());
|
||||||
|
} else {
|
||||||
|
// error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
out_ << "</VariantType>";
|
out_ << "</VariantType>";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue