Skip to content

Commit

Permalink
NodePtr *all* the things
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-staal committed Mar 7, 2024
1 parent 6f51c7e commit 44cea4b
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
#include "ast_constant.hpp"
#include "ast_context.hpp"

extern ast::Node* ParseAST(std::string file_name);
ast::NodePtr ParseAST(std::string file_name);
2 changes: 1 addition & 1 deletion include/ast_direct_declarator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DirectDeclarator : public Node
NodePtr identifier_;

public:
DirectDeclarator(Node* identifier) : identifier_(identifier){};
DirectDeclarator(NodePtr identifier) : identifier_(std::move(identifier)){};

void EmitRISC(std::ostream& stream, Context& context) const override;
void Print(std::ostream& stream) const override;
Expand Down
2 changes: 1 addition & 1 deletion include/ast_function_definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FunctionDefinition : public Node
NodePtr compound_statement_;

public:
FunctionDefinition(TypeSpecifier declaration_specifiers, Node* declarator, Node* compound_statement) : declaration_specifiers_(declaration_specifiers), declarator_(declarator), compound_statement_(compound_statement){};
FunctionDefinition(TypeSpecifier declaration_specifiers, NodePtr declarator, NodePtr compound_statement) : declaration_specifiers_(declaration_specifiers), declarator_(std::move(declarator)), compound_statement_(std::move(compound_statement)){};

void EmitRISC(std::ostream& stream, Context& context) const override;
void Print(std::ostream& stream) const override;
Expand Down
2 changes: 1 addition & 1 deletion include/ast_identifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Identifier : public Node
std::string identifier_;

public:
Identifier(std::string identifier) : identifier_(identifier){};
Identifier(std::string identifier) : identifier_(std::move(identifier)){};

void EmitRISC(std::ostream& stream, Context& context) const override;
void Print(std::ostream& stream) const override;
Expand Down
2 changes: 1 addition & 1 deletion include/ast_jump_statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ReturnStatement : public Node
NodePtr expression_;

public:
ReturnStatement(Node* expression) : expression_(expression) {}
ReturnStatement(NodePtr expression) : expression_(std::move(expression)) {}

void EmitRISC(std::ostream& stream, Context& context) const override;
void Print(std::ostream& stream) const override;
Expand Down
4 changes: 2 additions & 2 deletions include/ast_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class NodeList : public Node
std::vector<NodePtr> nodes_;

public:
NodeList(Node* first_node) { nodes_.emplace_back(first_node); }
NodeList(NodePtr first_node) { nodes_.push_back(std::move(first_node)); }

void PushBack(Node* item);
void PushBack(NodePtr item);
virtual void EmitRISC(std::ostream& stream, Context& context) const override;
virtual void Print(std::ostream& stream) const override;
};
Expand Down
4 changes: 2 additions & 2 deletions src/ast_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace ast {

void NodeList::PushBack(Node* item)
void NodeList::PushBack(NodePtr item)
{
nodes_.emplace_back(item);
nodes_.push_back(std::move(item));
}

void NodeList::EmitRISC(std::ostream& stream, Context& context) const
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main(int argc, char **argv)
NodePtr Parse(const CommandLineArguments& args)
{
std::cout << "Parsing: " << args.compile_source_path << std::endl;
NodePtr root{ ParseAST(args.compile_source_path) };
NodePtr root = ParseAST(args.compile_source_path);
std::cout << "AST parsing complete" << std::endl;
return root;
}
Expand Down
14 changes: 7 additions & 7 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ external_declaration

function_definition
: declaration_specifiers declarator compound_statement {
$$ = new FunctionDefinition($1, $2, $3);
$$ = new FunctionDefinition($1, NodePtr($2), NodePtr($3));
}
;

Expand All @@ -87,7 +87,7 @@ direct_declarator
delete $1;
}
| direct_declarator '(' ')' {
$$ = new DirectDeclarator($1);
$$ = new DirectDeclarator(NodePtr($1));
}
;

Expand All @@ -100,16 +100,16 @@ compound_statement
;

statement_list
: statement { $$ = new NodeList($1); }
| statement_list statement { $1->PushBack($2); $$=$1; }
: statement { $$ = new NodeList(NodePtr($1)); }
| statement_list statement { $1->PushBack(NodePtr($2)); $$=$1; }
;

jump_statement
: RETURN ';' {
$$ = new ReturnStatement(nullptr);
}
| RETURN expression ';' {
$$ = new ReturnStatement($2);
$$ = new ReturnStatement(NodePtr($2));
}
;

Expand Down Expand Up @@ -187,7 +187,7 @@ expression

Node* g_root;

Node* ParseAST(std::string file_name)
NodePtr ParseAST(std::string file_name)
{
yyin = fopen(file_name.c_str(), "r");
if(yyin == NULL){
Expand All @@ -198,5 +198,5 @@ Node* ParseAST(std::string file_name)
yyparse();
fclose(yyin);
yylex_destroy();
return g_root;
return NodePtr(g_root);
}

0 comments on commit 44cea4b

Please sign in to comment.