mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-26 16:58:45 +00:00
print & build visitor fixed and tested
This commit is contained in:
parent
c34523bd4f
commit
66a5dcfb4a
14 changed files with 91 additions and 62 deletions
|
|
@ -85,14 +85,8 @@ void BuildVisitor::Visit(Partition* node) {
|
|||
node->name = Partition::Test;
|
||||
} else if (name == "INTERFACE") {
|
||||
node->name = Partition::Interface;
|
||||
} else if (name == "CORE") {
|
||||
node->name = Partition::Core;
|
||||
} else if (name == "LIB") {
|
||||
node->name = Partition::Lib;
|
||||
} else if (name == "MODULE") {
|
||||
node->name = Partition::Module;
|
||||
} else if (name == "EXE") {
|
||||
node->name = Partition::Exe;
|
||||
} else if (name == "CODE") {
|
||||
node->name = Partition::Code;
|
||||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("scope");
|
||||
|
|
@ -198,10 +192,13 @@ void BuildVisitor::Visit(FunctionDeclaration* node) {
|
|||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
|
||||
for (size_t i = 0; i + 2 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
node->parameters.push_back(std::make_unique<AnnotatedAbstractType>());
|
||||
Visit(node->parameters.back().get());
|
||||
if (child_count > 2) {
|
||||
node->parameters.resize(child_count - 2);
|
||||
for (size_t i = 0; i + 2 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
node->parameters[i] = std::make_unique<AnnotatedAbstractType>();
|
||||
Visit(node->parameters.back().get());
|
||||
}
|
||||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("type");
|
||||
|
|
@ -395,6 +392,7 @@ void BuildVisitor::Visit(AnyAnnotatedType* node) {
|
|||
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i + 1);
|
||||
node->typeclasses[i] = std::make_unique<TypeclassExpression>();
|
||||
Visit(node->typeclasses[i].get());
|
||||
}
|
||||
}
|
||||
|
|
@ -871,6 +869,7 @@ void BuildVisitor::Visit(AccessExpression* node) {
|
|||
auto parse_node = current_node_;
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("name");
|
||||
node->name = std::make_unique<NameExpression>();
|
||||
Visit(node->name.get());
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("id");
|
||||
|
|
@ -1300,7 +1299,7 @@ void BuildVisitor::Visit(AnyType& node) {
|
|||
|
||||
std::string current_node_type = current_node_.GetType();
|
||||
|
||||
if (current_node_type == parser::tokens::ParametrizedType) {
|
||||
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::TupleType) {
|
||||
|
|
@ -1346,21 +1345,21 @@ void BuildVisitor::Visit(ExtendedScopedAnyType* node) {
|
|||
|
||||
void BuildVisitor::Visit(TypeclassExpression* node) {
|
||||
auto parse_node = current_node_;
|
||||
//
|
||||
|
||||
size_t child_count = parse_node.NamedChildCount();
|
||||
//
|
||||
// if (child_count > 1) {
|
||||
// node->path.resize(child_count - 1);
|
||||
//
|
||||
// for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
// current_node_ = parse_node.NthNamedChild(i);
|
||||
// Visit(node->path[i]);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
|
||||
if (child_count > 1) {
|
||||
node->path.resize(child_count - 1);
|
||||
|
||||
for (size_t i = 0; i + 1 < child_count; ++i) {
|
||||
current_node_ = parse_node.NthNamedChild(i);
|
||||
Visit(node->path[i]);
|
||||
}
|
||||
}
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("typeclass");
|
||||
Visit(node->typeclass);
|
||||
//
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
|
|
@ -1482,6 +1481,8 @@ void BuildVisitor::Visit(CharLiteral* node) {
|
|||
node->value = literal.substr(1, literal.size() - 2).back(); // TODO: special symbols, etc.
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(UnitLiteral* node) {}
|
||||
|
||||
void BuildVisitor::Visit(Literal& node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
|
|
@ -1501,6 +1502,9 @@ void BuildVisitor::Visit(Literal& node) {
|
|||
} else if (current_node_type == parser::tokens::CharLiteral) {
|
||||
node = std::make_unique<CharLiteral>();
|
||||
Visit(std::get<std::unique_ptr<CharLiteral>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::UnitLiteral) {
|
||||
node = std::make_unique<UnitLiteral>();
|
||||
Visit(std::get<std::unique_ptr<UnitLiteral>>(node).get());
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,8 +33,8 @@ int main(int argc, char** argv) { // TODO, only test version
|
|||
std::make_unique<interpreter::tokens::SourceFile>();
|
||||
|
||||
interpreter::BuildVisitor build_visitor(parse_tree);
|
||||
// interpreter::PrintVisitor print_visitor(std::cout);
|
||||
interpreter::PrintVisitor print_visitor(std::cout);
|
||||
|
||||
build_visitor.VisitSourceFile(source_file.get());
|
||||
// print_visitor.VisitSourceFile(source_file.get());
|
||||
print_visitor.VisitSourceFile(source_file.get());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,17 +38,8 @@ void PrintVisitor::Visit(Partition* node) {
|
|||
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";
|
||||
case Partition::Code:
|
||||
out_ << "CODE";
|
||||
break;
|
||||
}
|
||||
out_ << " {\n";
|
||||
|
|
@ -281,8 +272,8 @@ void PrintVisitor::Visit(TypeConstructorPattern* node) {
|
|||
for (auto& parameter : node->parameters) {
|
||||
if (!is_first) {
|
||||
out_ << ")\n";
|
||||
is_first = false;
|
||||
}
|
||||
is_first = false;
|
||||
out_ << '(';
|
||||
Visit(¶meter);
|
||||
}
|
||||
|
|
@ -514,8 +505,8 @@ void PrintVisitor::Visit(TypeConstructor* node) {
|
|||
for (auto& parameter : node->parameters) {
|
||||
if (!is_first) {
|
||||
out_ << ")\n";
|
||||
is_first = false;
|
||||
}
|
||||
is_first = false;
|
||||
out_ << '(';
|
||||
Visit(¶meter);
|
||||
}
|
||||
|
|
@ -735,4 +726,8 @@ void PrintVisitor::Visit(CharLiteral* node) {
|
|||
out_ << "[Char " << node->value << "] ";
|
||||
}
|
||||
|
||||
void PrintVisitor::Visit(UnitLiteral* node) {
|
||||
out_ << "[Unit ()] ";
|
||||
}
|
||||
|
||||
} // namespace interpreter
|
||||
|
|
|
|||
|
|
@ -330,6 +330,9 @@ void Visitor::Visit(Literal& node) {
|
|||
case 3:
|
||||
Visit(std::get<std::unique_ptr<CharLiteral>>(node).get());
|
||||
break;
|
||||
case 4:
|
||||
Visit(std::get<std::unique_ptr<UnitLiteral>>(node).get());
|
||||
break;
|
||||
default:
|
||||
// error
|
||||
break;
|
||||
|
|
@ -723,5 +726,7 @@ void Visitor::Visit(StringLiteral* node) {}
|
|||
|
||||
void Visitor::Visit(CharLiteral* node) {}
|
||||
|
||||
void Visitor::Visit(UnitLiteral* node) {}
|
||||
|
||||
} // namespace interpreter
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue