Skip to content

Commit

Permalink
fix(parser): seg fault on accessing VarNode value
Browse files Browse the repository at this point in the history
  • Loading branch information
nullptr0x committed Jan 26, 2024
1 parent 36d4715 commit 2ca41a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Swirl/include/parser/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ struct Var: Node {

struct Function: Node {
std::string ident;
std::string ret_type;
std::string ret_type = "none";
std::vector<Param> params{};

NodeType getType() const override {
Expand All @@ -199,6 +199,8 @@ struct Function: Node {
std::vector<Param> getParams() const override {
return params;
}

llvm::Value* codegen() override;
};

struct FuncCall: Node {
Expand Down
25 changes: 11 additions & 14 deletions Swirl/src/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


std::stack<Node*> ScopeTrack{};
std::vector<std::unique_ptr<Node>> Module{};
std::vector<std::unique_ptr<Node>> ParsedModule{};

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

Expand All @@ -34,10 +34,9 @@ std::unordered_map<std::string, int> precedence_table = {

void pushToModule(std::unique_ptr<Node> node, bool isNotParent = true) {
if (!ScopeTrack.empty()) node->setParent(ScopeTrack.top());
Module.emplace_back(std::move(node));
ParsedModule.emplace_back(std::move(node));
if (!isNotParent) {
std::cout << "stack push: " << Module.back()->getType() << std::endl;
ScopeTrack.push(Module.back().get());
ScopeTrack.push(ParsedModule.back().get());
}
}

Expand Down Expand Up @@ -89,8 +88,8 @@ void Parser::dispatch() {
m_Stream.next();
}

for (const auto& nd : Module) {
std::cout << nd->getType() << " -> " << nd->getParent() << std::endl;
for (const auto& nd : ParsedModule) {
nd->codegen();
}
}

Expand Down Expand Up @@ -148,7 +147,6 @@ void Parser::parseVar() {
parseExpr(&var_node.value);
}


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

Expand Down Expand Up @@ -250,17 +248,16 @@ void Parser::parseExpr(std::variant<std::vector<Expression>*, Expression*> ptr,
ops.pop();
}


Expression expr{};
expr.expr.reserve(output.size());
std::move(
std::make_move_iterator(output.begin()),
std::make_move_iterator(output.end()),
std::back_inserter(expr.expr)
);
for (auto& nd : output) {
auto n = std::make_unique<Node>();
n = std::move(nd);
expr.expr.push_back(std::move(n));
}

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);
std::get<Expression*>(ptr)->expr = std::move(expr.expr);
}

0 comments on commit 2ca41a7

Please sign in to comment.