Skip to content

Commit

Permalink
chores(parser): tidied up
Browse files Browse the repository at this point in the history
  • Loading branch information
nullptr0x committed Dec 13, 2023
1 parent daf2e93 commit 21ac190
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
8 changes: 5 additions & 3 deletions Swirl/include/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <utility>
#include <stack>
#include <variant>

#include <tokenizer/Tokenizer.h>
#include <llvm/IR/Value.h>
Expand Down Expand Up @@ -172,10 +173,12 @@ struct Var: Node {
return var_ident;
}


NodeType getType() const override {
return ND_VAR;
}

std::vector<std::unique_ptr<Node>>& getExprValue() override { return value.expr; }
llvm::Value* codegen() override;
};

struct Function: Node {
Expand Down Expand Up @@ -223,8 +226,7 @@ class Parser {
std::unique_ptr<Node> parseCall();
void dispatch();
void parseVar();
void parseExpr(std::vector<Expression>*, bool isCall = false);
void parseExpr(Expression&, bool isCall = false);
void parseExpr(std::variant<std::vector<Expression>*, Expression*>, bool isCall = false);
void parseLoop(TokenType);
// void appendAST(Node&);
inline void next(bool swsFlg = false, bool snsFlg = false);
Expand Down
62 changes: 29 additions & 33 deletions Swirl/src/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,21 @@
#include <string>
#include <set>
#include <stack>
#include <variant>
#include <unordered_map>
#include <unordered_set>

#include <parser/parser.h>
#include <llvm/IR/Type.h>

#define n_ptr std::make_unique<Node>
#define IS_INSTANCE_OF(cls) std::enable_if_t<std::is_base_of<cls, T>::value>

using namespace std::string_literals;

// Flags
short ang_ind = 0;
uint8_t is_first_t = 1;
uint8_t rd_param = 0;
uint8_t rd_func = 0;
uint8_t rd_param_cnt = 0;

std::vector<std::unique_ptr<Node>> Module{};
std::stack<std::vector<std::unique_ptr<Node>>*> LatestScopePtr{};
std::stack<std::vector<std::unique_ptr<Node>>*> InsertPoint{};

extern std::unordered_map<std::string, const char*> type_registry;
extern std::unordered_map<std::string, uint8_t> valid_expr_bin_ops;

extern std::unordered_map<std::string, uint8_t> valid_expr_bin_ops;

std::unordered_map<std::string, int> precedence_table = {
{"-", 1},
Expand All @@ -43,19 +36,14 @@ std::unordered_map<std::string, int> precedence_table = {
{"~", 8}
};


template <typename T, typename = std::enable_if_t<std::is_base_of<Node, T>::value>>
template <typename T, typename = IS_INSTANCE_OF(Node)>
void pushToModule(std::unique_ptr<Node> node) {
if (LatestScopePtr.empty())
if (InsertPoint.empty())
Module.emplace_back(std::move(node));
else
LatestScopePtr.top()->emplace_back(std::move(node));
InsertPoint.top()->emplace_back(std::move(node));
}

// this function is meant to be used for debugging purpose
void handleNodes(NodeType type, std::unique_ptr<Node>& nd) {

}

Parser::Parser(TokenStream &tks) : m_Stream(tks) {}
Parser::~Parser() = default;
Expand Down Expand Up @@ -88,11 +76,18 @@ void Parser::dispatch() {
}
}

if (t_type == PUNC) {
if (t_val == "}") {
InsertPoint.pop();
}
}

if (t_type == IDENT) {
if (m_Stream.peek().type == PUNC && m_Stream.peek().value == "(") {
pushToModule<FuncCall>(parseCall());
}
}

m_Stream.next();
}
}
Expand Down Expand Up @@ -134,8 +129,10 @@ void Parser::parseVar() {
var_node.initialized = true;
next();
next();
// parseExpr();
parseExpr(&var_node.value);
}

pushToModule<Var>(std::make_unique<Var>(std::move(var_node)));
}

std::unique_ptr<Node> Parser::parseCall() {
Expand All @@ -157,16 +154,17 @@ std::unique_ptr<Node> Parser::parseCall() {
}
std::cout << std::endl;
}

std::cout << "----------" << std::endl;
return call_node;
}



/* This method converts the expression into a postfix form, each expression object it creates
* consists of two vectors, one for the expr, the other for the operators sorted
* in the order of their precedence by this algorithm. Inspired from the Shunting-Yard-Algorithm.
* The method assumes that the current token(m_Stream.p_CurTk) is the start of the expression.*/
void Parser::parseExpr(std::vector<Expression>* ptr, bool isCall) {
/* This method is an implementation of the `Shunting Yard Algorithm`.
* */
void Parser::parseExpr(std::variant<std::vector<Expression>*, Expression*> ptr, bool isCall) {

bool kill_yourself = false;
std::stack<Op> ops{}; // our operator stack
std::vector<std::unique_ptr<Node>> output{}; // the final postfix(RPN) form
Expand Down Expand Up @@ -243,12 +241,6 @@ void Parser::parseExpr(std::vector<Expression>* ptr, bool isCall) {
ops.pop();
}

// uncomment for debugging purpose
for (auto& elem : output) {
handleNodes(elem->getType(), elem);
}

// std::cout << " <--- in postfix" << "\n";

Expression expr{};
expr.expr.reserve(output.size());
Expand All @@ -257,5 +249,9 @@ void Parser::parseExpr(std::vector<Expression>* ptr, bool isCall) {
std::make_move_iterator(output.end()),
std::back_inserter(expr.expr)
);
ptr->push_back(std::move(expr));

if (std::holds_alternative<std::vector<Expression>*>(ptr))
std::get<std::vector<Expression>*>(ptr)->push_back(std::move(expr));
else
std::get<Expression*>(ptr)->expr = std::move(output);
}

0 comments on commit 21ac190

Please sign in to comment.