Skip to content

Commit

Permalink
Comply with Google C++ Style Guide naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jpnock committed Jan 31, 2024
1 parent 7cc832c commit fe35d61
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 138 deletions.
3 changes: 2 additions & 1 deletion include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#include "ast_node.hpp"
#include "ast_type_specifier.hpp"
#include "ast_constant.hpp"
#include "ast_context.hpp"

extern Node *parseAST(std::string file_name);
extern Node *ParseAST(std::string file_name);

#endif
12 changes: 6 additions & 6 deletions include/ast_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
class IntConstant : public Node
{
private:
int value;
int value_;

public:
IntConstant(int _value) : value(_value) {}
IntConstant(int value) : value_(value) {}

void emitRISC(std::ostream &stream, Context &ctx) const override
void EmitRISC(std::ostream &stream, Context &context) const override
{
stream << "li a0, " << value << std::endl;
stream << "li a0, " << value_ << std::endl;
}

void print(std::ostream &stream) const override
void Print(std::ostream &stream) const override
{
stream << value;
stream << value_;
}
};

Expand Down
10 changes: 5 additions & 5 deletions include/ast_direct_declarator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
class DirectDeclarator : public Node
{
private:
Node *identifier;
Node *identifier_;

public:
DirectDeclarator(Node *_identifier) : identifier(_identifier){};
DirectDeclarator(Node *identifier) : identifier_(identifier){};
~DirectDeclarator()
{
delete identifier;
delete identifier_;
};
void emitRISC(std::ostream &stream, Context &context) const;
void print(std::ostream &stream) const override;
void EmitRISC(std::ostream &stream, Context &context) const override;
void Print(std::ostream &stream) const override;
};

#endif
18 changes: 9 additions & 9 deletions include/ast_function_definition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
class FunctionDefinition : public Node
{
private:
Node *declarationSpecifiers;
Node *declarator;
Node *compoundStatement;
Node *declaration_specifiers_;
Node *declarator_;
Node *compound_statement_;

public:
FunctionDefinition(Node *_declarationSpecifiers, Node *_declarator, Node *_compoundStatement) : declarationSpecifiers(_declarationSpecifiers), declarator(_declarator), compoundStatement(_compoundStatement){};
FunctionDefinition(Node *declaration_specifiers, Node *declarator, Node *compound_statement) : declaration_specifiers_(declaration_specifiers), declarator_(declarator), compound_statement_(compound_statement){};
~FunctionDefinition()
{
delete declarationSpecifiers;
delete declarator;
delete compoundStatement;
delete declaration_specifiers_;
delete declarator_;
delete compound_statement_;
};
void emitRISC(std::ostream &stream, Context &context) const;
void print(std::ostream &stream) const override;
void EmitRISC(std::ostream &stream, Context &context) const override;
void Print(std::ostream &stream) const override;
};

#endif
11 changes: 4 additions & 7 deletions include/ast_identifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
class Identifier : public Node
{
private:
std::string identifier;
std::string identifier_;

public:
Identifier(std::string _identifier) : identifier(_identifier){};
Identifier(std::string identifier) : identifier_(identifier){};
~Identifier(){};
void emitRISC(std::ostream &stream, Context &context) const;
void print(std::ostream &stream) const override
{
stream << identifier;
};
void EmitRISC(std::ostream &stream, Context &context) const override;
void Print(std::ostream &stream) const override;
};

#endif
43 changes: 9 additions & 34 deletions include/ast_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,35 @@
class Node
{
protected:
std::vector<Node *> branches;
std::vector<Node *> branches_;

public:
Node(){};
virtual ~Node();
virtual void emitRISC(std::ostream &stream, Context &context) const = 0;
virtual void print(std::ostream &stream) const = 0;
virtual void EmitRISC(std::ostream &stream, Context &context) const = 0;
virtual void Print(std::ostream &stream) const = 0;
};

// Represents a list of nodes.
class NodeList : public Node
{
private:
std::vector<Node *> nodes;
std::vector<Node *> nodes_;

public:
NodeList(Node *firstNode) : nodes({firstNode}) {}
NodeList(Node *first_node) : nodes_({first_node}) {}

~NodeList()
{
for (auto node : nodes)
for (auto node : nodes_)
{
delete node;
}
}

inline void push_back(Node *item)
{
nodes.push_back(item);
}

virtual void emitRISC(std::ostream &stream, Context &context) const override
{
for (auto node : nodes)
{
if (node == nullptr)
{
continue;
}
node->emitRISC(stream, context);
}
}

virtual void print(std::ostream &stream) const override
{
for (auto node : nodes)
{
if (node == nullptr)
{
continue;
}
node->print(stream);
}
}
void PushBack(Node *item);
virtual void EmitRISC(std::ostream &stream, Context &context) const override;
virtual void Print(std::ostream &stream) const override;
};

#endif
14 changes: 9 additions & 5 deletions include/ast_statements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
class ReturnStatement : public Node
{
private:
Node *expression;
Node *expression_;

public:
ReturnStatement(Node *_expression) { expression = _expression; };
~ReturnStatement() { delete expression; };
void emitRISC(std::ostream &stream, Context &context) const override;
void print(std::ostream &stream) const override;
ReturnStatement(Node *expression) : expression_(expression) {}
~ReturnStatement()
{
delete expression_;
};

void EmitRISC(std::ostream &stream, Context &context) const override;
void Print(std::ostream &stream) const override;
};

#endif
14 changes: 8 additions & 6 deletions include/ast_type_specifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

#include "ast_node.hpp"

class TypeSpecifier : public Node {
class TypeSpecifier : public Node
{
private:
std::string type;
std::string type_;

public:
TypeSpecifier(std::string _type) : type(_type) {};
~TypeSpecifier() {};
void emitRISC(std::ostream &stream, Context &context) const;
void print(std::ostream &stream) const override;
TypeSpecifier(std::string type) : type_(type){};
~TypeSpecifier(){};
void EmitRISC(std::ostream &stream, Context &context) const override;
void Print(std::ostream &stream) const override;
};

#endif
6 changes: 3 additions & 3 deletions include/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

struct CommandLineArguments
{
std::string compileSourcePath;
std::string compileOutputPath;
std::string compile_source_path;
std::string compile_output_path;
};

CommandLineArguments parseCommandLineArgs(int argc, char **argv);
CommandLineArguments ParseCommandLineArgs(int argc, char **argv);

#endif
8 changes: 4 additions & 4 deletions src/ast_direct_declarator.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "ast_direct_declarator.hpp"

void DirectDeclarator::emitRISC(std::ostream &stream, Context &context) const
void DirectDeclarator::EmitRISC(std::ostream &stream, Context &context) const
{
identifier->emitRISC(stream, context);
identifier_->EmitRISC(stream, context);
stream << ":" << std::endl;
}

void DirectDeclarator::print(std::ostream &stream) const
void DirectDeclarator::Print(std::ostream &stream) const
{
identifier->print(stream);
identifier_->Print(stream);
}
22 changes: 11 additions & 11 deletions src/ast_function_definition.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
#include "ast_function_definition.hpp"

void FunctionDefinition::emitRISC(std::ostream &stream, Context &context) const
void FunctionDefinition::EmitRISC(std::ostream &stream, Context &context) const
{
// TODO: this is not complete.
std::cerr << "FunctionDefinition: emitRISC is not fully implemented." << std::endl;
std::cerr << "FunctionDefinition: EmitRISC is not fully implemented." << std::endl;

// Emit assembler directives.
// TODO: these are just examples ones, make sure you understand
// the concept of directives and correct them.
stream << ".text" << std::endl;
stream << ".globl f" << std::endl;

this->declarator->emitRISC(stream, context);
declarator_->EmitRISC(stream, context);

if (this->compoundStatement != nullptr)
if (compound_statement_ != nullptr)
{
this->compoundStatement->emitRISC(stream, context);
compound_statement_->EmitRISC(stream, context);
}
}

void FunctionDefinition::print(std::ostream &stream) const
void FunctionDefinition::Print(std::ostream &stream) const
{
// TODO: this is not complete.
std::cerr << "FunctionDefinition: print is not fully implemented." << std::endl;
std::cerr << "FunctionDefinition: Print is not fully implemented." << std::endl;

this->declarationSpecifiers->print(stream);
declaration_specifiers_->Print(stream);
stream << " ";

this->declarator->print(stream);
declarator_->Print(stream);
stream << "() {" << std::endl;

if (this->compoundStatement != nullptr)
if (compound_statement_ != nullptr)
{
this->compoundStatement->print(stream);
compound_statement_->Print(stream);
}
stream << "}" << std::endl;
}
9 changes: 7 additions & 2 deletions src/ast_identifier.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include "ast_identifier.hpp"

void Identifier::emitRISC(std::ostream &stream, Context &context) const
void Identifier::EmitRISC(std::ostream &stream, Context &context) const
{
stream << identifier;
stream << identifier_;
}

void Identifier::Print(std::ostream &stream) const
{
stream << identifier_;
};
39 changes: 35 additions & 4 deletions src/ast_node.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
#include "ast_node.hpp"

Node::~Node() {
for (unsigned i = 0; i < branches.size(); i++){
delete branches[i];
}
Node::~Node()
{
for (auto branch : branches_)
{
delete branch;
}
}

void NodeList::PushBack(Node *item)
{
nodes_.push_back(item);
}

void NodeList::EmitRISC(std::ostream &stream, Context &context) const
{
for (auto node : nodes_)
{
if (node == nullptr)
{
continue;
}
node->EmitRISC(stream, context);
}
}

void NodeList::Print(std::ostream &stream) const
{
for (auto node : nodes_)
{
if (node == nullptr)
{
continue;
}
node->Print(stream);
}
}
14 changes: 7 additions & 7 deletions src/ast_statements.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include "ast_statements.hpp"

void ReturnStatement::emitRISC(std::ostream &stream, Context &context) const
void ReturnStatement::EmitRISC(std::ostream &stream, Context &context) const
{
// TODO: this implementation is incomplete
std::cerr << "ReturnStatement: emitRISC is not fully implemented." << std::endl;
if (this->expression != nullptr)
std::cerr << "ReturnStatement: EmitRISC is not fully implemented." << std::endl;
if (expression_ != nullptr)
{
expression->emitRISC(stream, context);
expression_->EmitRISC(stream, context);
}
stream << "ret" << std::endl;
}

void ReturnStatement::print(std::ostream &stream) const
void ReturnStatement::Print(std::ostream &stream) const
{
stream << "return";
if (this->expression != nullptr)
if (expression_ != nullptr)
{
stream << " ";
expression->print(stream);
expression_->Print(stream);
}
stream << ";" << std::endl;
}
Loading

0 comments on commit fe35d61

Please sign in to comment.