mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
grammar refactoring: part of build_visitor fixed
This commit is contained in:
parent
64653e6a6a
commit
3c2d496a85
3 changed files with 75 additions and 49 deletions
|
|
@ -391,8 +391,6 @@ void BuildVisitor::Visit(DefinitionParameter* node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// TODO <-- fixes ended there
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
void BuildVisitor::Visit(MatchCase* node) {
|
||||
|
|
@ -544,6 +542,32 @@ void BuildVisitor::Visit(FlowControl& node) {
|
|||
|
||||
// Statements, expressions, blocks, etc. -----------------
|
||||
|
||||
void BuildVisitor::Visit(BlockStatement& 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::Expression) { // optimize ??
|
||||
node = std::make_unique<Expression>();
|
||||
Visit(*std::get<std::unique_ptr<Expression>>(node));
|
||||
} else if (current_node_type == parser::tokens::VariableDefinitionStatement) {
|
||||
node = std::make_unique<VariableDefinitionStatement>();
|
||||
Visit(std::get<std::unique_ptr<VariableDefinitionStatement>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::FlowControl) {
|
||||
node = std::make_unique<FlowControl>();
|
||||
Visit(*std::get<std::unique_ptr<FlowControl>>(node));
|
||||
} else if (current_node_type == parser::tokens::PrefixedExpression) {
|
||||
node = std::make_unique<PrefixedExpression>();
|
||||
Visit(*std::get<std::unique_ptr<PrefixedExpression>>(node));
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(Block* node) {
|
||||
auto parse_node = current_node_;
|
||||
size_t statement_count = parse_node.NamedChildCount();
|
||||
|
|
@ -558,29 +582,6 @@ void BuildVisitor::Visit(Block* node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(ScopedStatement* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
current_node_ = parse_node.ChildByFieldName("statement");
|
||||
Visit(node->statement);
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(LoopControlExpression& node) {
|
||||
std::string value = current_node_.NthChild(0).GetValue();
|
||||
|
||||
if (value == "break") {
|
||||
node = LoopControlExpression::Break;
|
||||
} else if (value == "continue") {
|
||||
node = LoopControlExpression::Continue;
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
void BuildVisitor::Visit(SubExpressionToken& node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
|
|
@ -617,6 +618,12 @@ void BuildVisitor::Visit(SubExpression& node) {
|
|||
} else if (current_node_type == parser::tokens::SubExpressionToken) {
|
||||
node = std::make_unique<SubExpressionToken>();
|
||||
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));
|
||||
} else if (current_node_type == parser::tokens::ReferenceExpression) {
|
||||
node = std::make_unique<ReferenceExpression>();
|
||||
Visit(std::get<std::unique_ptr<ReferenceExpression>>(node));
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
|
|
@ -702,29 +709,11 @@ void BuildVisitor::Visit(SuperExpression& node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
|
||||
void BuildVisitor::Visit(BlockStatement& node) {
|
||||
void BuildVisitor::Visit(ScopedStatement* 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::Expression) { // optimize ??
|
||||
node = std::make_unique<Expression>();
|
||||
Visit(*std::get<std::unique_ptr<Expression>>(node));
|
||||
} else if (current_node_type == parser::tokens::VariableDefinitionStatement) {
|
||||
node = std::make_unique<VariableDefinitionStatement>();
|
||||
Visit(std::get<std::unique_ptr<VariableDefinitionStatement>>(node).get());
|
||||
} else if (current_node_type == parser::tokens::FlowControl) {
|
||||
node = std::make_unique<FlowControl>();
|
||||
Visit(*std::get<std::unique_ptr<FlowControl>>(node));
|
||||
} else if (current_node_type == parser::tokens::PrefixedExpression) {
|
||||
node = std::make_unique<PrefixedExpression>();
|
||||
Visit(*std::get<std::unique_ptr<PrefixedExpression>>(node));
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
current_node_ = parse_node.ChildByFieldName("statement");
|
||||
Visit(node->statement);
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
|
@ -756,7 +745,32 @@ void BuildVisitor::Visit(UnaryOperatorExpression* node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// Simple Expressions
|
||||
void BuildVisitor::Visit(ReferenceExpression* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
||||
size_t child_count = parse_node.ChildCount();
|
||||
|
||||
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.ChildByFieldName("expression");
|
||||
Visit(node->expression.get());
|
||||
|
||||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
// TODO <-- current place
|
||||
|
||||
// Other expressions
|
||||
|
||||
void BuildVisitor::Visit(FunctionCallExpression* node) {
|
||||
auto parse_node = current_node_;
|
||||
|
|
@ -818,6 +832,18 @@ void BuildVisitor::Visit(ReturnExpression* node) {
|
|||
current_node_ = parse_node;
|
||||
}
|
||||
|
||||
void BuildVisitor::Visit(LoopControlExpression& node) {
|
||||
std::string value = current_node_.NthChild(0).GetValue();
|
||||
|
||||
if (value == "break") {
|
||||
node = LoopControlExpression::Break;
|
||||
} else if (value == "continue") {
|
||||
node = LoopControlExpression::Continue;
|
||||
} else {
|
||||
// error
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
void BuildVisitor::Visit(FunctionArgument& node) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue