Skip to content

Commit

Permalink
Resolving merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
IO32NQ committed May 7, 2024
1 parent 1cd04f5 commit 1a7144a
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 159 deletions.
15 changes: 12 additions & 3 deletions include/src/ast/AstBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <functional>
#include <memory>

#include "AstException.hpp"

using namespace antlr4;
using namespace dplgrammar;

Expand Down Expand Up @@ -41,7 +43,8 @@ class AstBuilder : public DplParserBaseVisitor {
antlrcpp::Any visitWhilestm(DplParser::WhilestmContext *parseNode) override;

// Expressions The expressions isn't correctly sequenced
antlrcpp::Any visitJuncexpr(DplParser::JuncexprContext *parseNode) override;
antlrcpp::Any visitOrexpr(DplParser::OrexprContext *parseNode) override;
antlrcpp::Any visitAndexpr(DplParser::AndexprContext *parseNode) override;
antlrcpp::Any visitNotexpr(DplParser::NotexprContext *parseNode) override;
antlrcpp::Any visitEqulexpr(DplParser::EqulexprContext *parseNode) override;
antlrcpp::Any visitCompexpr(DplParser::CompexprContext *parseNode) override;
Expand All @@ -53,6 +56,7 @@ class AstBuilder : public DplParserBaseVisitor {

// Terms
antlrcpp::Any visitTerminal(tree::TerminalNode *node) override;
antlrcpp::Any visitNumber(DplParser::NumberContext *parseNode) override;
antlrcpp::Any visitList(DplParser::ListContext *parseNode) override;
antlrcpp::Any visitTable(DplParser::TableContext *parseNode) override;
antlrcpp::Any visitColumn(DplParser::ColumnContext *parseNode) override;
Expand All @@ -73,13 +77,18 @@ class AstBuilder : public DplParserBaseVisitor {
void initNewNode(antlr4::ParserRuleContext *parseNode, const std::shared_ptr<AstNode> &newNode,
const std::string &text = "");

antlrcpp::Any indexNode(const std::function<std::shared_ptr<AstNode>()>& createNode,
antlr4::ParserRuleContext* parseNode, size_t childIndex,
const std::string& text = "");

antlrcpp::Any unaryNode(const std::function<std::shared_ptr<AstNode>()> &createNode,
antlr4::ParserRuleContext *parseNode, size_t childIndex,
const std::string &text = "", bool restoreOldCurrent = true);
const std::string &text = "", bool hasChild = true);

antlrcpp::Any unaryNodeList(const std::function<std::shared_ptr<AstNode>()> &createNode,
antlr4::ParserRuleContext *parseNode, size_t startIndex,
size_t interval, const std::string &text = "");
size_t interval, const std::string &text = "",
bool hasChild = true);

antlrcpp::Any binaryNode(const std::function<std::shared_ptr<AstNode>()> &createNode,
antlr4::ParserRuleContext *parseNode, size_t leftIndex,
Expand Down
138 changes: 88 additions & 50 deletions src/ast/AstBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <AstBuilder.hpp>

using namespace dplsrc;

antlrcpp::Any AstBuilder::visitProg(DplParser::ProgContext* parseNode) {
std::shared_ptr<AstNode> newNode = std::make_shared<ProgNode>();
newNode->setRule(parseNode->getRuleIndex());
Expand Down Expand Up @@ -62,10 +64,11 @@ antlrcpp::Any AstBuilder::visitProccall(DplParser::ProccallContext* parseNode) {
std::shared_ptr<AstNode> newNode = std::static_pointer_cast<AstNode>(procCallNewNode);
initNewNode(parseNode, newNode, "() proccall");

if (parseNode->children.size() != 2) { // not empty params
if (parseNode->children.size() > 2) { // not empty params
parseNode->children[1]->accept(this);
}
procCallNewNode->stopVisitingParams();
currentNode = newNode;

return nullptr;
}
Expand Down Expand Up @@ -121,16 +124,20 @@ antlrcpp::Any AstBuilder::visitTerminal(tree::TerminalNode* node) {
if (!isIdentifier) {
switch (node->getSymbol()->getType()) {
case DplLexer::Integer:
newNode->setType(dplsrc::Type::INT);
newNode->setVal(0);
break;
case DplLexer::Float:
newNode->setType(dplsrc::Type::FLOAT);
newNode->setVal(0.0);
break;
case DplLexer::String:
newNode->setType(dplsrc::Type::STR);
newNode->setVal("");
break;
case DplLexer::Bool:
newNode->setType(dplsrc::Type::BOOL);
newNode->setVal(false);
break;
default:
newNode->setVal(nullptr);
break;
}
}

Expand Down Expand Up @@ -161,18 +168,18 @@ antlrcpp::Any AstBuilder::visitWhilestm(DplParser::WhilestmContext* parseNode) {
3, true);
}

antlrcpp::Any AstBuilder::visitJuncexpr(DplParser::JuncexprContext* parseNode) {
antlrcpp::Any AstBuilder::visitOrexpr(DplParser::OrexprContext* parseNode) {
return binaryExpr(
[this](size_t operatorType) -> std::shared_ptr<AstNode> {
switch (operatorType) {
case DplLexer::And:
return std::make_shared<AndExprNode>(currentNode);
case DplLexer::Or:
return std::make_shared<OrExprNode>(currentNode);
default:
throw std::runtime_error("Junc expr was not valid operator");
}
return nullptr;
[this]([[maybe_unused]] size_t unused) -> std::shared_ptr<AstNode> {
return std::make_shared<OrExprNode>(currentNode);
},
parseNode);
}

antlrcpp::Any AstBuilder::visitAndexpr(DplParser::AndexprContext* parseNode) {
return binaryExpr(
[this]([[maybe_unused]] size_t unused) -> std::shared_ptr<AstNode> {
return std::make_shared<AndExprNode>(currentNode);
},
parseNode);
}
Expand All @@ -194,7 +201,7 @@ antlrcpp::Any AstBuilder::visitEqulexpr(DplParser::EqulexprContext* parseNode) {
case DplLexer::NotEqual:
return std::make_shared<NotEqualExprNode>(currentNode);
default:
throw std::runtime_error("Junc expr was not valid operator");
throw AstException("Junc expr was not valid operator");
}
return nullptr;
},
Expand All @@ -214,7 +221,7 @@ antlrcpp::Any AstBuilder::visitCompexpr(DplParser::CompexprContext* parseNode) {
case DplLexer::LessEqual:
return std::make_shared<LessEqualExprNode>(currentNode);
default:
throw std::runtime_error("Compexpr was not valid operator");
throw AstException("Compexpr was not valid operator");
}
return nullptr;
},
Expand All @@ -230,7 +237,7 @@ antlrcpp::Any AstBuilder::visitPlusexpr(DplParser::PlusexprContext* parseNode) {
case DplLexer::Minus:
return std::make_shared<MinusExprNode>(currentNode);
default:
throw std::runtime_error("Plusexpr was not valid operator");
throw AstException("Plusexpr was not valid operator");
}
return nullptr;
},
Expand All @@ -246,7 +253,7 @@ antlrcpp::Any AstBuilder::visitTablexpr(DplParser::TablexprContext* parseNode) {
case DplLexer::Intersection:
return std::make_shared<IntersectionExprNode>(currentNode);
default:
throw std::runtime_error("Tablexpr was not valid operator");
throw AstException("Tablexpr was not valid operator");
}
return nullptr;
},
Expand All @@ -264,7 +271,7 @@ antlrcpp::Any AstBuilder::visitMultexpr(DplParser::MultexprContext* parseNode) {
case DplLexer::Mod:
return std::make_shared<ModExprNode>(currentNode);
default:
throw std::runtime_error("Multexpr was not valid operator");
throw AstException("Multexpr was not valid operator");
}
return nullptr;
},
Expand All @@ -280,7 +287,7 @@ antlrcpp::Any AstBuilder::visitPolaexpr(DplParser::PolaexprContext* parseNode) {
case DplLexer::Minus:
return std::make_shared<MinusNode>(currentNode);
default:
throw std::runtime_error("Polaexpr was not valid operator");
throw AstException("Polaexpr was not valid operator");
}
return nullptr;
},
Expand All @@ -296,21 +303,21 @@ antlrcpp::Any AstBuilder::visitExpoexpr(DplParser::ExpoexprContext* parseNode) {
}

antlrcpp::Any AstBuilder::visitList(DplParser::ListContext* parseNode) {
size_t index =
(parseNode->children.size() == 3) ? 1 : 9; // dirty way to not visit children if empty list
return unaryNode([this]() { return std::make_shared<ListNode>(currentNode); }, parseNode, index,
"[] List");
bool hasChild =
parseNode->children.size() > 2; // dirty way to not visit children if empty list
return unaryNode([this]() { return std::make_shared<ListNode>(currentNode); }, parseNode, 1,
"[] List", hasChild);
}

antlrcpp::Any AstBuilder::visitTable(DplParser::TableContext* parseNode) {
bool hasChild = parseNode->children.size() > 2;
return unaryNodeList([this]() { return std::make_shared<TableNode>(currentNode); }, parseNode,
1, 2, "{} Table");
1, 2, "{} Table", hasChild);
}

antlrcpp::Any AstBuilder::visitColumn(DplParser::ColumnContext* parseNode) {
std::string text = parseNode->children[0]->getText() + ":";
return unaryNode([this]() { return std::make_shared<ColumnNode>(currentNode); }, parseNode, 2,
text);
return binaryNode([this]() { return std::make_shared<ColumnNode>(currentNode); }, parseNode, 0,
2, true, "Column");
}

antlrcpp::Any AstBuilder::visitTerm(DplParser::TermContext* parseNode) {
Expand All @@ -324,6 +331,22 @@ antlrcpp::Any AstBuilder::visitTerm(DplParser::TermContext* parseNode) {
parseNode, 1, "() Parenthesis");
}

antlrcpp::Any AstBuilder::visitNumber(DplParser::NumberContext* parseNode) {
return unaryExpr(
[this](size_t operatorType) -> std::shared_ptr<AstNode> {
switch (operatorType) {
case DplLexer::Plus:
return std::make_shared<PlusNode>(currentNode);
case DplLexer::Minus:
return std::make_shared<MinusNode>(currentNode);
default:
throw AstException("Numberexpr was not valid operator");
}
return nullptr;
},
parseNode);
}

antlrcpp::Any AstBuilder::visitSubscript(DplParser::SubscriptContext* parseNode) {
size_t childAmount = parseNode->children.size();
if (childAmount < 2) {
Expand All @@ -345,24 +368,25 @@ antlrcpp::Any AstBuilder::visitSubscript(DplParser::SubscriptContext* parseNode)
}

antlrcpp::Any AstBuilder::visitIndex(DplParser::IndexContext* parseNode) {
return unaryNode([this]() { return std::make_shared<IndexNode>(currentNode); }, parseNode, 1,
"[] Index", false);
return indexNode([this]() { return std::make_shared<IndexNode>(currentNode); }, parseNode, 1,
"[] Index");
}

antlrcpp::Any AstBuilder::visitHeaderindex(DplParser::HeaderindexContext* parseNode) {
return unaryNode([this]() { return std::make_shared<HeaderIndexNode>(currentNode); }, parseNode,
2, "[$] Header Indexing", false);
return indexNode([this]() { return std::make_shared<HeaderIndexNode>(currentNode); }, parseNode,
2, "[$] Header Indexing");
}

antlrcpp::Any AstBuilder::visitFiltering(DplParser::FilteringContext* parseNode) {
return unaryNode([this]() { return std::make_shared<FilterNode>(currentNode); }, parseNode, 1,
"[] Filter", false);
parseNode->children[1]->accept(this);
return nullptr;
}
// To here

antlrcpp::Any AstBuilder::visitUnaryexpr(DplParser::UnaryexprContext* parseNode) {
return unaryNode([this]() { return std::make_shared<UnaryExprNode>(currentNode); }, parseNode,
1, parseNode->children[0]->getText());
std::string text = parseNode->children[0]->getText();
return indexNode([this]() { return std::make_shared<FilterNode>(currentNode); }, parseNode, 1,
text);
}

std::shared_ptr<AstNode> AstBuilder::getRoot() { return root; }
Expand All @@ -376,33 +400,47 @@ void AstBuilder::initNewNode(antlr4::ParserRuleContext* parseNode,
currentNode = newNode;
}

antlrcpp::Any AstBuilder::unaryNode(const std::function<std::shared_ptr<AstNode>()>& createNode,
antlrcpp::Any AstBuilder::indexNode(const std::function<std::shared_ptr<AstNode>()>& createNode,
antlr4::ParserRuleContext* parseNode, size_t childIndex,
const std::string& text, bool restoreOldCurrent) {
std::shared_ptr<AstNode> oldNode = currentNode;
const std::string& text) {
std::shared_ptr<AstNode> newNode = createNode();
initNewNode(parseNode, newNode, text);

tree::ParseTree* child = parseNode->children[childIndex];
parseNode->children[childIndex]->accept(this);

if (child != nullptr) {
child->accept(this);
}
currentNode = newNode;
return nullptr;
}

if (restoreOldCurrent) {
currentNode = oldNode;
antlrcpp::Any AstBuilder::unaryNode(const std::function<std::shared_ptr<AstNode>()>& createNode,
antlr4::ParserRuleContext* parseNode, size_t childIndex,
const std::string& text, bool hasChild) {
std::shared_ptr<AstNode> oldNode = currentNode;
std::shared_ptr<AstNode> newNode = createNode();
initNewNode(parseNode, newNode, text);

if (!hasChild) {
return nullptr;
}

parseNode->children[childIndex]->accept(this);

currentNode = oldNode;

return nullptr;
}

antlrcpp::Any AstBuilder::unaryNodeList(const std::function<std::shared_ptr<AstNode>()>& createNode,
antlr4::ParserRuleContext* parseNode, size_t startIndex,
size_t interval, const std::string& text) {
size_t interval, const std::string& text, bool hasChild) {
std::shared_ptr<AstNode> oldNode = currentNode;
std::shared_ptr<AstNode> newNode = createNode();
initNewNode(parseNode, newNode, text);

if (!hasChild) {
return nullptr;
}

for (size_t i = startIndex; i < parseNode->children.size(); i = i + interval) {
parseNode->children[i]->accept(this);
}
Expand Down Expand Up @@ -490,5 +528,5 @@ antlr4::Token* AstBuilder::getChildToken(antlr4::tree::ParseTree* parseNode, siz
if (terminalNode != nullptr) {
return terminalNode->getSymbol();
}
throw std::runtime_error("Child node is not a TerminalNode");
}
throw AstException("Child node is not a TerminalNode");
}
13 changes: 13 additions & 0 deletions src/ast/nodes/IndexNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <IndexNode.hpp>

using namespace dplsrc;

void IndexNode::addChild(std::shared_ptr<AstNode> child) {
if (getRightNode() == nullptr) {
setRightNode(child);
} else if (getLeftNode() == nullptr) {
setLeftNode(child);
} else {
throw AstException("BinaryNode already has two children");
}
}
Loading

0 comments on commit 1a7144a

Please sign in to comment.