Skip to content

Commit

Permalink
Parser now takes expression for header declaration in column
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsVind committed May 3, 2024
1 parent 2d22303 commit 09eb114
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 162 deletions.
7 changes: 0 additions & 7 deletions docs/examples/test.dpl

This file was deleted.

26 changes: 14 additions & 12 deletions grammar/DplParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,38 @@ stm: ifstm
| assignstm
| flowstm
| returnstm
| juncexpr;
| orexpr;

stms: (stm Newline? | Newline)+;

block: Indent Newline? stms Dedent Newline?;


// If
ifstm: If juncexpr Colon block elsestm?;
ifstm: If orexpr Colon block elsestm?;

elsestm: Else Colon block;


// While
whilestm: While juncexpr Colon block;
whilestm: While orexpr Colon block;


// Assign
assignstm: subscript Assign juncexpr;
assignstm: subscript Assign orexpr;

// Flow
flowstm: Break
| Continue;

// Return
returnstm: Return juncexpr
returnstm: Return orexpr
| Return;

// Expressions
juncexpr: notexpr ((And | Or) notexpr)*;
orexpr: andexpr (Or andexpr)*;

andexpr: notexpr (And notexpr)*;

notexpr: Not notexpr
| equlexpr;
Expand All @@ -68,7 +70,7 @@ polaexpr: (Plus | Minus) polaexpr

expoexpr: <assoc = right> term (Exponent term)*;

term: OpenPar juncexpr ClosePar
term: OpenPar orexpr ClosePar
| list
| table
| number
Expand All @@ -88,20 +90,20 @@ list: OpenSquare args CloseSquare
table: OpenCurly column (Comma column)* CloseCurly
| OpenCurly CloseCurly;

column: String Colon list;
column: orexpr Colon list;

// Trailers
index: (OpenSquare juncexpr CloseSquare);
index: (OpenSquare orexpr CloseSquare);

headerindex: (OpenSquare Dollar juncexpr CloseSquare);
headerindex: (OpenSquare Dollar orexpr CloseSquare);

filtering: (OpenSquare unaryexpr CloseSquare);


unaryexpr: (Equal | NotEqual | Greater | GreaterEqual | Less | Less) juncexpr;
unaryexpr: (Equal | NotEqual | Greater | GreaterEqual | Less | Less) orexpr;


proccall: OpenPar ClosePar
| OpenPar args ClosePar;

args: juncexpr (Comma juncexpr)*;
args: orexpr (Comma orexpr)*;
3 changes: 2 additions & 1 deletion include/src/ast/AstBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 Down
6 changes: 3 additions & 3 deletions include/src/ast/nodes/ColumnNode.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef COLUMNNODE_HPP
#define COLUMNNODE_HPP

#include <UnaryNode.hpp>
#include <BinaryNode.hpp>

class ColumnNode : public UnaryNode {
class ColumnNode : public BinaryNode {
public:
ColumnNode(std::shared_ptr<AstNode> parent) : UnaryNode(std::move(parent)){};
ColumnNode(std::shared_ptr<AstNode> parent) : BinaryNode(std::move(parent)){};

void accept(std::shared_ptr<AstVisitor> visitor) override {
visitor->visit(std::static_pointer_cast<ColumnNode>(shared_from_this()));
Expand Down
26 changes: 12 additions & 14 deletions src/ast/AstBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,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 Down Expand Up @@ -313,9 +313,7 @@ antlrcpp::Any AstBuilder::visitTable(DplParser::TableContext* parseNode) {
}

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 Down
2 changes: 1 addition & 1 deletion src/ast/AstVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void AstVisitor::visit(const std::shared_ptr<AssignNode> &node) {
}

void AstVisitor::visit(const std::shared_ptr<ColumnNode> &node) {
visit(std::static_pointer_cast<UnaryNode>(node));
visit(std::static_pointer_cast<BinaryNode>(node));
}

void AstVisitor::visit(const std::shared_ptr<DivExprNode> &node) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ast/QuicksortParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AST_TEST("quicksort.dpl") {
DplLexer::Identifier,
DplLexer::Identifier,
DplParser::RuleIfstm,
DplParser::RuleJuncexpr,
DplParser::RuleAndexpr,
DplParser::RuleCompexpr,
DplLexer::Identifier,
DplLexer::Integer,
Expand Down
3 changes: 3 additions & 0 deletions tests/ast/ReplaceWithParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ AST_TEST("replace_with.dpl") {
DplLexer::Identifier,
DplParser::RuleTable,
DplParser::RuleColumn,
DplLexer::String,
DplParser::RuleList,
DplLexer::Integer,
DplLexer::Integer,
DplLexer::Integer,
DplParser::RuleColumn,
DplLexer::String,
DplParser::RuleList,
DplLexer::Integer,
DplLexer::Integer,
DplLexer::Integer,
DplParser::RuleColumn,
DplLexer::String,
DplParser::RuleList,
DplLexer::None,
DplLexer::Integer,
Expand Down
18 changes: 12 additions & 6 deletions tests/parser/BreakParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ PARSER_TEST("break.dpl") {
DplParser::RuleStm,
DplParser::RuleAssignstm,
DplParser::RuleSubscript,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -20,7 +21,8 @@ PARSER_TEST("break.dpl") {
DplParser::RuleNumber,
DplParser::RuleStm,
DplParser::RuleWhilestm,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -35,7 +37,8 @@ PARSER_TEST("break.dpl") {
DplParser::RuleStm,
DplParser::RuleAssignstm,
DplParser::RuleSubscript,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -54,7 +57,8 @@ PARSER_TEST("break.dpl") {
DplParser::RuleNumber,
DplParser::RuleStm,
DplParser::RuleIfstm,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -78,7 +82,8 @@ PARSER_TEST("break.dpl") {
DplParser::RuleStm,
DplParser::RuleFlowstm,
DplParser::RuleStm,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -91,7 +96,8 @@ PARSER_TEST("break.dpl") {
DplParser::RuleSubscript,
DplParser::RuleProccall,
DplParser::RuleArgs,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand Down
18 changes: 12 additions & 6 deletions tests/parser/ContinueParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ PARSER_TEST("continue.dpl") {
DplParser::RuleStm,
DplParser::RuleAssignstm,
DplParser::RuleSubscript,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -20,7 +21,8 @@ PARSER_TEST("continue.dpl") {
DplParser::RuleNumber,
DplParser::RuleStm,
DplParser::RuleWhilestm,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -43,7 +45,8 @@ PARSER_TEST("continue.dpl") {
DplParser::RuleStm,
DplParser::RuleAssignstm,
DplParser::RuleSubscript,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -62,7 +65,8 @@ PARSER_TEST("continue.dpl") {
DplParser::RuleNumber,
DplParser::RuleStm,
DplParser::RuleIfstm,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand Down Expand Up @@ -90,7 +94,8 @@ PARSER_TEST("continue.dpl") {
DplParser::RuleStm,
DplParser::RuleFlowstm,
DplParser::RuleStm,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand All @@ -103,7 +108,8 @@ PARSER_TEST("continue.dpl") {
DplParser::RuleSubscript,
DplParser::RuleProccall,
DplParser::RuleArgs,
DplParser::RuleJuncexpr,
DplParser::RuleOrexpr,
DplParser::RuleAndexpr,
DplParser::RuleNotexpr,
DplParser::RuleEqulexpr,
DplParser::RuleCompexpr,
Expand Down
Loading

0 comments on commit 09eb114

Please sign in to comment.