mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-26 16:58:45 +00:00
grammar refactoring: build_visitor fixed & visitors tested
This commit is contained in:
parent
3c2d496a85
commit
e4802896bd
35 changed files with 118128 additions and 91770 deletions
|
|
@ -1,7 +1,6 @@
|
|||
#include <iostream>
|
||||
|
||||
#include <memory>
|
||||
#include <sched.h>
|
||||
|
||||
// for clangd
|
||||
#include "../include/build_visitor.hpp"
|
||||
|
|
@ -115,8 +114,10 @@ void BuildVisitor::Visit(ImportStatement* node) {
|
|||
++excluded_child_count;
|
||||
}
|
||||
|
||||
auto module_name_node = parse_node.ChildByFieldName("module_name");
|
||||
node->module_name = module_name_node.NthChild(1).GetValue();
|
||||
StringLiteral module_name;
|
||||
current_node_ = parse_node.ChildByFieldName("module_name");
|
||||
Visit(&module_name);
|
||||
node->module_name = module_name.value;
|
||||
++excluded_child_count;
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
|
@ -180,6 +181,13 @@ void BuildVisitor::Visit(VariableDefinitionStatement* node) {
|
|||
current_node_ = parse_node.ChildByFieldName("value");
|
||||
Visit(node->value);
|
||||
|
||||
std::string assignment_modifier = parse_node.NthChild(2).GetValue();
|
||||
if (assignment_modifier == "=") {
|
||||
node->assignment_modifier = VariableDefinitionStatement::Assign;
|
||||
} else if (assignment_modifier == "<-") {
|
||||
node->assignment_modifier = VariableDefinitionStatement::Move;
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
|
|
@ -197,6 +205,7 @@ void BuildVisitor::Visit(FunctionDeclaration* node) {
|
|||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("type");
|
||||
node->type = std::make_unique<FunctionType>();
|
||||
Visit(node->type.get());
|
||||
|
||||
current_node_ = parse_node;
|
||||
|
|
@ -205,6 +214,8 @@ void BuildVisitor::Visit(FunctionDeclaration* node) {
|
|||
void BuildVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
node->is_inline = (parse_node.NthChild(1).GetValue() == "inline");
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("definition");
|
||||
node->definition = std::make_unique<FunctionDefinition>();
|
||||
Visit(node->definition.get());
|
||||
|
|
@ -319,7 +330,7 @@ void BuildVisitor::Visit(SourceStatement& node) {
|
|||
void BuildVisitor::Visit(FunctionDefinition* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
node->name = parse_node.ChildByFieldName("name").GetValue(); // TODO: check on operator
|
||||
node->name = parse_node.ChildByFieldName("name").GetValue();
|
||||
|
||||
if (parse_node.NthChild(0).GetValue() == "(") {
|
||||
node->modifier = FunctionDefinition::Operator;
|
||||
|
|
@ -431,7 +442,7 @@ void BuildVisitor::Visit(Match* node) {
|
|||
// if (child_count > 1) { // always true (repeat1)
|
||||
node->matches.resize(child_count - 1);
|
||||
|
||||
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
Visit(&node->matches[i]);
|
||||
}
|
||||
|
|
@ -589,7 +600,7 @@ void BuildVisitor::Visit(SubExpressionToken& node) {
|
|||
|
||||
std::string current_node_type = current_node_.GetType();
|
||||
|
||||
if (current_node_type == parser::tokens::NameExpression) { // optimize ??
|
||||
if (current_node_type == parser::tokens::NameExpression) {
|
||||
node = std::make_unique<NameExpression>();
|
||||
Visit(std::get<std::unique_ptr<NameExpression>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::ScopedStatement) {
|
||||
|
|
@ -620,10 +631,10 @@ void BuildVisitor::Visit(SubExpression& node) {
|
|||
Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node));
|
||||
} else if (current_node_type == parser::tokens::ArrayExpression) {
|
||||
node = std::make_unique<ArrayExpression>();
|
||||
Visit(std::get<std::unique_ptr<ArrayExpression>>(node));
|
||||
Visit(std::get<std::unique_ptr<ArrayExpression>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::ReferenceExpression) {
|
||||
node = std::make_unique<ReferenceExpression>();
|
||||
Visit(std::get<std::unique_ptr<ReferenceExpression>>(node));
|
||||
Visit(std::get<std::unique_ptr<ReferenceExpression>>(node).get());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
|
@ -763,15 +774,34 @@ void BuildVisitor::Visit(ReferenceExpression* node) {
|
|||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("expression");
|
||||
node->expression = std::make_unique<ScopedStatement>();
|
||||
Visit(node->expression.get());
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// TODO <-- current place
|
||||
|
||||
// Other expressions
|
||||
|
||||
void BuildVisitor::Visit(FunctionArgument& 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::SubExpressionToken) {
|
||||
node = SubExpressionToken();
|
||||
Visit(std::get<SubExpressionToken>(node));
|
||||
} else if (current_node_type == parser::tokens::TypeSubExpression) {
|
||||
node = TypeSubExpression();
|
||||
Visit(std::get<TypeSubExpression>(node));
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(FunctionCallExpression* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
|
|
@ -784,9 +814,9 @@ void BuildVisitor::Visit(FunctionCallExpression* node) {
|
|||
// if (child_count > 1) { // always true (repeat1)
|
||||
node->arguments.resize(child_count - 1);
|
||||
|
||||
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
Visit(node->arguments[i]);
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
Visit(node->arguments[i]);
|
||||
}
|
||||
// }
|
||||
|
||||
|
|
@ -801,8 +831,8 @@ void BuildVisitor::Visit(TupleExpression* node) {
|
|||
node->expressions.resize(expressions_count);
|
||||
|
||||
for (size_t i = 0; i < expressions_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->expressions[i]);
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->expressions[i]);
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
|
|
@ -816,8 +846,8 @@ void BuildVisitor::Visit(VariantExpression* node) {
|
|||
node->expressions.resize(expressions_count);
|
||||
|
||||
for (size_t i = 0; i < expressions_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->expressions[i]);
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->expressions[i]);
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
|
|
@ -832,6 +862,83 @@ void BuildVisitor::Visit(ReturnExpression* node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(TypeConstructor* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("type");
|
||||
node->type = std::make_unique<ParametrizedType>();
|
||||
Visit(node->type.get());
|
||||
|
||||
size_t parameter_count = (parse_node.NamedChildCount() - 1) / 2;
|
||||
|
||||
node->parameters.resize(parameter_count);
|
||||
|
||||
for (size_t i = 0; i < parameter_count * 2; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
|
||||
if (i % 2 == 0) {
|
||||
Visit(&std::get<0>(node->parameters[i / 2]));
|
||||
|
||||
std::string assignment_modifier = current_node_.NextSibling().GetValue();
|
||||
if (assignment_modifier == "=") {
|
||||
std::get<1>(node->parameters[i / 2]) = TypeConstructor::Assign;
|
||||
} else if (assignment_modifier == "<-") {
|
||||
std::get<1>(node->parameters[i / 2]) = TypeConstructor::Move;
|
||||
}
|
||||
} else {
|
||||
Visit(std::get<2>(node->parameters[i / 2]));
|
||||
}
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(LambdaFunction* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
if (child_count > 1) {
|
||||
bool parameters_ended = false;
|
||||
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
|
||||
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
||||
parameters_ended = true;
|
||||
}
|
||||
|
||||
if (!parameters_ended) {
|
||||
node->parameters.push_back(std::make_unique<DefinitionParameter>());
|
||||
Visit(node->parameters.back().get());
|
||||
} else {
|
||||
node->arguments.emplace_back();
|
||||
Visit(&node->arguments.back());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("expression");
|
||||
Visit(node->expression);
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(ArrayExpression* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
size_t elements_count = parse_node.NamedChildCount();
|
||||
|
||||
node->elements.resize(elements_count);
|
||||
|
||||
for (size_t i = 0; i < elements_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->elements[i]);
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(LoopControlExpression& node) {
|
||||
std::string value = current_node_.NthChild(0).GetValue();
|
||||
|
||||
|
|
@ -844,61 +951,6 @@ void BuildVisitor::Visit(LoopControlExpression& node) {
|
|||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
void BuildVisitor::Visit(FunctionArgument& 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::SubExpressionToken) { // optimize ??
|
||||
node = std::make_unique<SubExpressionToken>();
|
||||
Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node));
|
||||
} else if (current_node_type == parser::tokens::TypeSubExpression) {
|
||||
node = std::make_unique<TypeSubExpression>();
|
||||
Visit(*std::get<std::unique_ptr<TypeSubExpression>>(node));
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// Lambda
|
||||
|
||||
void BuildVisitor::Visit(LambdaFunction* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
if (child_count > 1) {
|
||||
bool parameters_ended = false;
|
||||
|
||||
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
|
||||
if (current_node_.GetType() != parser::tokens::DefinitionParameter) {
|
||||
parameters_ended = true;
|
||||
}
|
||||
|
||||
if (!parameters_ended) {
|
||||
node->parameters.push_back(std::make_unique<DefinitionParameter>());
|
||||
Visit(node->parameters.back().get());
|
||||
} else {
|
||||
node->arguments.push_back(std::make_unique<DefinitionArgument>());
|
||||
Visit(node->arguments.back().get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("expression");
|
||||
Visit(node->expression);
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// Name
|
||||
|
||||
void BuildVisitor::Visit(NameExpression* node) {
|
||||
|
|
@ -918,8 +970,9 @@ void BuildVisitor::Visit(NameExpression* node) {
|
|||
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());
|
||||
if (current_node_type == parser::tokens::ExtendedName) {
|
||||
node->expressions.back() = std::make_unique<ExtendedName>();
|
||||
Visit(node->expressions.back());
|
||||
} 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()));
|
||||
|
|
@ -954,7 +1007,7 @@ void BuildVisitor::Visit(TupleName* node) {
|
|||
for (size_t i = 0; i < names_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
node->names[i] = std::make_unique<AnnotatedName>();
|
||||
Visit(node->names[i].get());
|
||||
Visit(node->names[i]);
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
|
|
@ -970,7 +1023,7 @@ void BuildVisitor::Visit(VariantName* node) {
|
|||
for (size_t i = 0; i < names_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
node->names[i] =std::make_unique<AnnotatedName>();
|
||||
Visit(node->names[i].get());
|
||||
Visit(node->names[i]);
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
|
|
@ -984,7 +1037,7 @@ void BuildVisitor::Visit(AnnotatedName* node) {
|
|||
if (parse_node.NamedChildCount() > 1) {
|
||||
current_node_ = parse_node.ChildByFieldName("type");
|
||||
node->type = std::make_unique<ParametrizedType>();
|
||||
Visit(node->type.value().get());
|
||||
Visit(node->type.value());
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
|
@ -997,7 +1050,7 @@ void BuildVisitor::Visit(AnyName& node) {
|
|||
|
||||
std::string current_node_type = current_node_.GetType();
|
||||
|
||||
if (current_node_type == parser::tokens::AnnotatedName) { // optimize ??
|
||||
if (current_node_type == parser::tokens::AnnotatedName) {
|
||||
node = std::make_unique<AnnotatedName>();
|
||||
Visit(std::get<std::unique_ptr<AnnotatedName>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::TupleName) {
|
||||
|
|
@ -1013,27 +1066,20 @@ void BuildVisitor::Visit(AnyName& node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// Type, typeclass, etc. -----------------
|
||||
|
||||
// Type
|
||||
|
||||
void BuildVisitor::Visit(TypeConstructor* node) {
|
||||
void BuildVisitor::Visit(FunctionType* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("type");
|
||||
node->type = std::make_unique<ParametrizedType>();
|
||||
Visit(node->type.get());
|
||||
size_t types_count = parse_node.NamedChildCount();
|
||||
|
||||
size_t parameter_count = (parse_node.NamedChildCount() - 1) / 2;
|
||||
node->types.resize(types_count);
|
||||
|
||||
node->parameters.resize(parameter_count);
|
||||
|
||||
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);
|
||||
}
|
||||
for (size_t i = 0; i < types_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->types[i]);
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
|
|
@ -1047,7 +1093,7 @@ void BuildVisitor::Visit(TupleType* node) {
|
|||
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||
|
||||
if (current_node_.GetType() == parser::tokens::Constructor) {
|
||||
node->type = current_node_.GetValue(); // TODO check
|
||||
node->type = current_node_.GetValue();
|
||||
|
||||
++current_node_n;
|
||||
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||
|
|
@ -1056,14 +1102,16 @@ void BuildVisitor::Visit(TupleType* node) {
|
|||
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();
|
||||
if (current_node_.GetType() == parser::tokens::ExtendedName) {
|
||||
node->entities.back().first.emplace();
|
||||
Visit(&node->entities.back().first.value());
|
||||
|
||||
++current_node_n;
|
||||
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||
}
|
||||
|
||||
Visit(node->entities.back().second);
|
||||
node->entities.back().second = std::make_unique<ExtendedScopedAnyType>();
|
||||
Visit(node->entities.back().second.get());
|
||||
|
||||
++current_node_n;
|
||||
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||
|
|
@ -1080,7 +1128,7 @@ void BuildVisitor::Visit(VariantType* node) {
|
|||
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||
|
||||
if (current_node_.GetType() == parser::tokens::Constructor) {
|
||||
node->type = current_node_.GetValue(); // TODO check
|
||||
node->type = current_node_.GetValue();
|
||||
|
||||
++current_node_n;
|
||||
current_node_ = parse_node.NthNamedChild(current_node_n);
|
||||
|
|
@ -1091,8 +1139,8 @@ void BuildVisitor::Visit(VariantType* node) {
|
|||
|
||||
node->constructors.emplace_back();
|
||||
|
||||
if (current_node_type == parser::tokens::TypeIdentifierDefinition) { // optimize ??
|
||||
node->constructors.back() = current_node_.GetValue(); // TODO check
|
||||
if (current_node_type == parser::tokens::Constructor) {
|
||||
node->constructors.back() = current_node_.GetValue();
|
||||
} 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());
|
||||
|
|
@ -1119,21 +1167,9 @@ void BuildVisitor::Visit(AnnotatedType* node) {
|
|||
if (child_count > 1) {
|
||||
node->annotations.resize(child_count - 1);
|
||||
|
||||
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
|
||||
std::string current_node_type = current_node_.GetType();
|
||||
|
||||
node->annotations[i] = std::make_unique<ParametrizedTypeclass>();
|
||||
|
||||
if (current_node_type == parser::tokens::TypeclassExpression) { // optimize ??
|
||||
node->annotations[i]->typeclass_expression = std::make_unique<TypeclassExpression>();
|
||||
Visit(node->annotations[i]->typeclass_expression.get());
|
||||
} else if (current_node_type == parser::tokens::ParametrizedTypeclass) {
|
||||
Visit(node->annotations[i].get());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
Visit(node->annotations[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1152,9 +1188,9 @@ void BuildVisitor::Visit(ParametrizedType* node) {
|
|||
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]);
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
Visit(node->parameters[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1164,30 +1200,48 @@ void BuildVisitor::Visit(ParametrizedType* node) {
|
|||
void BuildVisitor::Visit(TypeExpression* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
size_t excluded_child_count = 0;
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
if (child_count > 1) {
|
||||
node->namespaces.resize(child_count - 1);
|
||||
current_node_ = parse_node.ChildByFieldName("type");
|
||||
node->type = current_node_.GetValue();
|
||||
++excluded_child_count;
|
||||
|
||||
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||
current_node_ = current_node_.NextSibling();
|
||||
|
||||
if (!current_node_.IsNull()) {
|
||||
current_node_ = current_node_.NextSibling();
|
||||
if (!current_node_.IsNull()) {
|
||||
NumberLiteral literal;
|
||||
Visit(&literal);
|
||||
node->array_size = literal.value;
|
||||
++excluded_child_count;
|
||||
} else {
|
||||
node->array_size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (child_count > excluded_child_count) {
|
||||
node->namespaces.resize(child_count - excluded_child_count);
|
||||
|
||||
for (size_t i = 0; i + excluded_child_count < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->namespaces[i]);
|
||||
}
|
||||
}
|
||||
|
||||
node->type = parse_node.ChildByFieldName("type").GetValue();
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(AnyType& node) { // Or ScopedAnyType
|
||||
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 ??
|
||||
if (current_node_type == parser::tokens::ParametrizedType) {
|
||||
node = std::make_unique<ParametrizedType>();
|
||||
Visit(std::get<std::unique_ptr<ParametrizedType>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::TupleType) {
|
||||
|
|
@ -1196,6 +1250,9 @@ void BuildVisitor::Visit(AnyType& node) { // Or ScopedAnyType
|
|||
} else if (current_node_type == parser::tokens::VariantType) {
|
||||
node = std::make_unique<VariantType>();
|
||||
Visit(std::get<std::unique_ptr<VariantType>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::FunctionType) {
|
||||
node = std::make_unique<FunctionType>();
|
||||
Visit(std::get<std::unique_ptr<FunctionType>>(node).get());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
|
@ -1203,82 +1260,46 @@ void BuildVisitor::Visit(AnyType& node) { // Or ScopedAnyType
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(TypeSubExpression& node) {
|
||||
void BuildVisitor::Visit(ExtendedScopedAnyType* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
current_node_ = parse_node.NthNamedChild(0);
|
||||
size_t child_count = parse_node.ChildCount();
|
||||
|
||||
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
|
||||
if (child_count > 1) {
|
||||
node->references.resize(child_count - 1);
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
std::string reference = parse_node.NthChild(i).GetValue();
|
||||
if (reference == "~") {
|
||||
node->references[i] = ReferenceType::Reference;
|
||||
} else if (reference == "@") {
|
||||
node->references[i] = ReferenceType::UniqueReference;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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::TypeExpression) { // optimize ??
|
||||
node = std::make_unique<TypeExpression>();
|
||||
Visit(std::get<std::unique_ptr<TypeExpression>>(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 if (current_node_type == parser::tokens::Expression) {
|
||||
node = std::make_unique<Expression>();
|
||||
Visit(*std::get<std::unique_ptr<Expression>>(node));
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
current_node_ = parse_node.ChildByFieldName("type");
|
||||
Visit(node->type);
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// Typeclass
|
||||
|
||||
void BuildVisitor::Visit(AnnotatedTypeclass* node) {
|
||||
void BuildVisitor::Visit(TypeclassUsage& node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("typeclass_expression");
|
||||
node->typeclass_expression = std::make_unique<TypeclassExpression>();
|
||||
Visit(node->typeclass_expression.get());
|
||||
current_node_ = parse_node.NthNamedChild(0);
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
std::string current_node_type = current_node_.GetType();
|
||||
|
||||
if (child_count > 1) {
|
||||
node->annotations.resize(child_count - 1);
|
||||
|
||||
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
|
||||
std::string current_node_type = current_node_.GetType();
|
||||
|
||||
node->annotations[i] = std::make_unique<ParametrizedTypeclass>();
|
||||
|
||||
if (current_node_type == parser::tokens::TypeclassExpression) { // optimize ??
|
||||
node->annotations[i]->typeclass_expression = std::make_unique<TypeclassExpression>();
|
||||
Visit(node->annotations[i]->typeclass_expression.get());
|
||||
} else if (current_node_type == parser::tokens::ParametrizedTypeclass) {
|
||||
Visit(node->annotations[i].get());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
}
|
||||
if (current_node_type == parser::tokens::TypeclassExpression) {
|
||||
node = std::make_unique<TypeclassExpression>();
|
||||
Visit(std::get<std::unique_ptr<TypeclassExpression>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::ParametrizedTypeclass) {
|
||||
node = std::make_unique<ParametrizedTypeclass>();
|
||||
Visit(std::get<std::unique_ptr<ParametrizedTypeclass>>(node).get());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
|
|
@ -1296,7 +1317,7 @@ void BuildVisitor::Visit(ParametrizedTypeclass* node) {
|
|||
if (child_count > 1) {
|
||||
node->parameters.resize(child_count - 1);
|
||||
|
||||
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
Visit(node->parameters[i]);
|
||||
}
|
||||
|
|
@ -1313,7 +1334,7 @@ void BuildVisitor::Visit(TypeclassExpression* node) {
|
|||
if (child_count > 1) {
|
||||
node->namespaces.resize(child_count - 1);
|
||||
|
||||
for (size_t i = 0; i < child_count - 1; ++i) {
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->namespaces[i]);
|
||||
}
|
||||
|
|
@ -1324,9 +1345,54 @@ void BuildVisitor::Visit(TypeclassExpression* node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// Typeclass & Type -----------------
|
||||
|
||||
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::TypeExpression) {
|
||||
node = std::make_unique<TypeExpression>();
|
||||
Visit(std::get<std::unique_ptr<TypeExpression>>(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(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) {
|
||||
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;
|
||||
}
|
||||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
void BuildVisitor::Visit(ExtendedName* node) { // TODO: check
|
||||
void BuildVisitor::Visit(ExtendedName* node) {
|
||||
size_t child_count = current_node_.NamedChildCount();
|
||||
if (child_count > 1) {
|
||||
node->name = current_node_.GetValue();
|
||||
|
|
@ -1348,11 +1414,13 @@ void BuildVisitor::Visit(NumberLiteral* node) {
|
|||
}
|
||||
|
||||
void BuildVisitor::Visit(StringLiteral* node) {
|
||||
node->value = current_node_.NthChild(1).GetValue();
|
||||
std::string literal = current_node_.GetValue();
|
||||
node->value = literal.substr(1, literal.size() - 2);
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(CharLiteral* node) {
|
||||
node->value = current_node_.NthChild(1).GetValue()[0];
|
||||
std::string literal = current_node_.GetValue();
|
||||
node->value = literal.substr(1, literal.size() - 2).back(); // TODO: special symbols, etc.
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(Literal& node) {
|
||||
|
|
@ -1362,7 +1430,7 @@ void BuildVisitor::Visit(Literal& node) {
|
|||
|
||||
std::string current_node_type = current_node_.GetType();
|
||||
|
||||
if (current_node_type == parser::tokens::FloatNumberLiteral) { // optimize ??
|
||||
if (current_node_type == parser::tokens::FloatNumberLiteral) {
|
||||
node = std::make_unique<FloatNumberLiteral>();
|
||||
Visit(std::get<std::unique_ptr<FloatNumberLiteral>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::NumberLiteral) {
|
||||
|
|
@ -1381,16 +1449,16 @@ void BuildVisitor::Visit(Literal& node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(NameSubSuperExpression& node) {
|
||||
void BuildVisitor::Visit(NameSubExpression& 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::NameIdentifier) { // optimize ??
|
||||
node = std::make_unique<NameIdentifier>(current_node_.GetValue());
|
||||
//Visit(std::get<std::unique_ptr<NameIdentifier>>(node).get());
|
||||
if (current_node_type == parser::tokens::ExtendedName) {
|
||||
node = std::make_unique<ExtendedName>();
|
||||
Visit(std::get<std::unique_ptr<ExtendedName>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::Literal) {
|
||||
node = std::make_unique<Literal>();
|
||||
Visit(*std::get<std::unique_ptr<Literal>>(node));
|
||||
|
|
|
|||
206449
src/parser.c
206449
src/parser.c
File diff suppressed because it is too large
Load diff
|
|
@ -90,7 +90,7 @@ void PrintVisitor::Visit(ImportStatement* node) {
|
|||
if (!node->symbols.empty()) {
|
||||
out_ << " (\n";
|
||||
for (auto& symbol : node->symbols) {
|
||||
Visitor::Visit(&symbol);
|
||||
Visit(&symbol);
|
||||
out_ << '\n';
|
||||
}
|
||||
out_ << ')';
|
||||
|
|
@ -183,7 +183,7 @@ void PrintVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
|||
break;
|
||||
}
|
||||
out_ << "] (";
|
||||
Visit(node->definition.get());
|
||||
Visit(node->type.get());
|
||||
out_ << ")\n";
|
||||
}
|
||||
|
||||
|
|
@ -413,7 +413,7 @@ void PrintVisitor::Visit(FunctionCallExpression* node) {
|
|||
Visitor::Visit(argument);
|
||||
out_ << ", ";
|
||||
}
|
||||
out_ << ")\n";
|
||||
out_ << ")";
|
||||
}
|
||||
|
||||
void PrintVisitor::Visit(TupleExpression* node) {
|
||||
|
|
@ -444,11 +444,24 @@ void PrintVisitor::Visit(TypeConstructor* node) {
|
|||
out_ << "[TypeConstructor ";
|
||||
Visit(node->type.get());
|
||||
out_ << "]\n(";
|
||||
|
||||
bool is_first = true;
|
||||
for (auto& parameter : node->parameters) {
|
||||
Visit(¶meter.first);
|
||||
out_ << " = ";
|
||||
Visitor::Visit(parameter.second);
|
||||
out_ << ")\n(";
|
||||
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";
|
||||
}
|
||||
|
|
@ -530,12 +543,12 @@ void PrintVisitor::Visit(AnnotatedName* node) {
|
|||
|
||||
void PrintVisitor::Visit(FunctionType* node) {
|
||||
out_ << "[FunctionType] (";
|
||||
bool first = true;
|
||||
bool is_first = true;
|
||||
for (auto& type : node->types) {
|
||||
if (!first) {
|
||||
if (!is_first) {
|
||||
out_ << " -> ";
|
||||
}
|
||||
first = false;
|
||||
is_first = false;
|
||||
Visitor::Visit(type);
|
||||
}
|
||||
out_ << ')';
|
||||
|
|
@ -601,7 +614,13 @@ void PrintVisitor::Visit(ParametrizedType* node) {
|
|||
}
|
||||
|
||||
void PrintVisitor::Visit(TypeExpression* node) {
|
||||
out_ << "[TypeExpression] (";
|
||||
out_ << "[TypeExpression ";
|
||||
|
||||
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_ << '.';
|
||||
|
|
@ -655,7 +674,7 @@ void PrintVisitor::Visit(ExtendedName* node) {
|
|||
out_ << "[ExtendedName " << node->name << "] ";
|
||||
}
|
||||
|
||||
void PrintVisitor::Visit(AnyIdentifier* node) { // std::string
|
||||
void PrintVisitor::Visit(std::string* node) { // std::string
|
||||
out_ << "[Identifier " << *node << "] ";
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -191,10 +191,10 @@ void Visitor::Visit(SuperExpression& node) {
|
|||
void Visitor::Visit(FunctionArgument& node) {
|
||||
switch (node.index()) {
|
||||
case 0:
|
||||
Visit(*std::get<std::unique_ptr<SubExpressionToken>>(node));
|
||||
Visit(std::get<SubExpressionToken>(node));
|
||||
break;
|
||||
case 1:
|
||||
Visit(*std::get<std::unique_ptr<TypeSubExpression>>(node));
|
||||
Visit(std::get<TypeSubExpression>(node));
|
||||
break;
|
||||
default:
|
||||
// error
|
||||
|
|
@ -225,7 +225,7 @@ void Visitor::Visit(AnyName& node) {
|
|||
|
||||
// Type
|
||||
|
||||
void Visitor::Visit(AnyType& node) { // Or ScopedAnyType
|
||||
void Visitor::Visit(AnyType& node) {
|
||||
switch (node.index()) {
|
||||
case 0:
|
||||
Visit(std::get<std::unique_ptr<ParametrizedType>>(node).get());
|
||||
|
|
@ -318,7 +318,7 @@ void Visitor::Visit(Literal& node) {
|
|||
void Visitor::Visit(NameSubExpression& node) {
|
||||
switch (node.index()) {
|
||||
case 0:
|
||||
Visit(std::get<std::unique_ptr<NameIdentifier>>(node).get());
|
||||
Visit(std::get<std::unique_ptr<ExtendedName>>(node).get());
|
||||
break;
|
||||
case 1:
|
||||
Visit(*std::get<std::unique_ptr<Literal>>(node));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue