Skip to content

Commit

Permalink
Added for, while and switch again
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewGodsmark committed Mar 21, 2024
1 parent 3cfbad2 commit 71a5970
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 12 deletions.
3 changes: 3 additions & 0 deletions include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "ast_variable_ref.hpp"

#include "ast_if_statement.hpp"
#include "ast_switch_statement.hpp"
#include "ast_while_statement.hpp"
#include "ast_for_statement.hpp"
#include "ast_binary_op.hpp"
#include "ast_parameter_decl.hpp"

Expand Down
53 changes: 53 additions & 0 deletions include/ast_for_statement.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef AST_FOR_STATEMENT_HPP
#define AST_FOR_STATEMENT_HPP

#include "ast_node.hpp"

class ForStatement : public Node
{
private:
Node *expression_statement_initialize_;
Node *expression_statement_loop_;
Node *statement_;

public:
ForStatement(Node *expression_statement_initialize, Node *expression_statement_loop, Node *statement) : expression_statement_initialize_(expression_statement_initialize),
expression_statement_loop_(expression_statement_loop),
statement_(statement){};
~ForStatement()
{
delete expression_statement_initialize_;
delete expression_statement_loop_;
delete statement_;
};

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

class ExprForStatement : public Node
{
private:
Node *expression_statement_initialize_;
Node *expression_statement_loop_;
Node *expression_;
Node *statement_;

public:
ExprForStatement(Node *expression_statement_initialize, Node *expression_statement_loop, Node *expression, Node *statement) : expression_statement_initialize_(expression_statement_initialize),
expression_statement_loop_(expression_statement_loop),
expression_(expression),
statement_(statement){};
~ExprForStatement()
{
delete expression_statement_initialize_;
delete expression_statement_loop_;
delete expression_;
delete statement_;
};

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

#endif
59 changes: 59 additions & 0 deletions include/ast_switch_statement.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef AST_SWITCH_STATEMENT_HPP
#define AST_SWITCH_STATEMENT_HPP

#include "ast_node.hpp"

class SwitchStatement : public Node
{
private:
Node *expression_;
Node *statement_;

public:
SwitchStatement(Node *expression, Node *statement) : expression_(expression), statement_(statement){};
~SwitchStatement()
{
delete expression_;
delete statement_;
};

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

class CaseStatement : public Node
{
private:
Node *constant_expression_;
Node *statement_;

public:
CaseStatement(Node *constant_expression, Node *statement) : constant_expression_(constant_expression), statement_(statement){};
~CaseStatement()
{
delete constant_expression_;
delete statement_;
};

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

class DefaultStatement : public Node
{
private:
Node *statement_;

public:
DefaultStatement(Node *statement) : statement_(statement){};
~DefaultStatement()
{
delete statement_;
};

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

#endif

42 changes: 42 additions & 0 deletions include/ast_while_statement.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef AST_WHILE_STATEMENT_HPP
#define AST_WHILE_STATEMENT_HPP

#include "ast_node.hpp"

class WhileStatement : public Node
{
private:
Node *expression_;
Node *statement_;

public:
WhileStatement(Node *expression, Node *statement) : expression_(expression), statement_(statement){};
~WhileStatement()
{
delete expression_;
delete statement_;
};

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

class DoWhileStatement : public Node
{
private:
Node *statement_;
Node *expression_;

public:
DoWhileStatement(Node *statement, Node *expression) : statement_(statement), expression_(expression){};
~DoWhileStatement()
{
delete statement_;
delete expression_;
};

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

#endif
69 changes: 69 additions & 0 deletions src/ast_for_statement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "ast_for_statement.hpp"

void ForStatement::EmitRISC(std::ostream &stream, Context &context) const
{

expression_statement_initialize_->EmitRISC(stream, context);

std::string startlabel = context.NewLabel();
std::string endlabel = context.NewLabel();

stream << startlabel << ":" << std::endl;

expression_statement_loop_->EmitRISC(stream, context);

stream << "beqz a0, " << endlabel << std::endl;

statement_->EmitRISC(stream, context);

stream << "beqz zero, " << startlabel << std::endl;

stream << endlabel << ":" << std::endl;
}

void ForStatement::Print(std::ostream &stream) const
{
stream << "for(";
expression_statement_initialize_->Print(stream);
stream << "; ";
expression_statement_loop_->Print(stream);
stream << "){" << std::endl;
statement_->Print(stream);
stream << "}" << std::endl;
}

void ExprForStatement::EmitRISC(std::ostream &stream, Context &context) const
{

expression_statement_initialize_->EmitRISC(stream, context);

std::string startname = context.NewLabel();
std::string endname = context.NewLabel();

stream << startname << ":" << std::endl;

expression_statement_loop_->EmitRISC(stream, context);

stream << "beqz a0, " << endname << std::endl;

statement_->EmitRISC(stream, context);

expression_->EmitRISC(stream, context);

stream << "beqz zero, " << startname << std::endl;

stream << endname << ":" << std::endl;
}

void ExprForStatement::Print(std::ostream &stream) const
{
stream << "for(";
expression_statement_initialize_->Print(stream);
stream << "; ";
expression_statement_loop_->Print(stream);
stream << "; ";
expression_->Print(stream);
stream << "){" << std::endl;
statement_->Print(stream);
stream << "}" << std::endl;
}
10 changes: 5 additions & 5 deletions src/ast_if_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ void IfStatement::EmitRISC(std::ostream &stream, Context &context) const

void IfStatement::Print(std::ostream &stream) const
{
stream << "if ( ";
stream << "if(";
expression_->Print(stream);
stream << " ) { " << std::endl;
stream << "){" << std::endl;
statement_->Print(stream);
stream << "}" << std::endl;
}
Expand Down Expand Up @@ -47,12 +47,12 @@ void IfElseStatement::EmitRISC(std::ostream &stream, Context &context) const

void IfElseStatement::Print(std::ostream &stream) const
{
stream << "if ( ";
stream << "if(";
expression_->Print(stream);
stream << " ) { " << std::endl;
stream << "){" << std::endl;
statement_->Print(stream);
stream << "}" << std::endl;
stream << "else {";
stream << "else{";
elsestatement_->Print(stream);
stream << "}" << std::endl;

Expand Down
57 changes: 57 additions & 0 deletions src/ast_switch_statement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "ast_switch_statement.hpp"

void SwitchStatement::EmitRISC(std::ostream &stream, Context &context) const
{

expression_->EmitRISC(stream, context);

stream << "mv t0, a0" << std::endl;

statement_->EmitRISC(stream, context);

}

void SwitchStatement::Print(std::ostream &stream) const
{
stream << "switch(";
expression_->Print(stream);
stream << "){" << std::endl;
statement_->Print(stream);
stream << "}" << std::endl;
}

void CaseStatement::EmitRISC(std::ostream &stream, Context &context) const
{

constant_expression_->EmitRISC(stream, context);

std::string NotEqual = context.NewLabel();

stream << "BNE t0, a0, " << NotEqual << std::endl;

statement_->EmitRISC(stream, context);

stream << NotEqual << ":" << std::endl;

}

void CaseStatement::Print(std::ostream &stream) const
{
stream << "case ";
constant_expression_->Print(stream);
stream << ":" << std::endl;
statement_->Print(stream);
}

void DefaultStatement::EmitRISC(std::ostream &stream, Context &context) const
{

statement_->EmitRISC(stream, context);

}

void DefaultStatement::Print(std::ostream &stream) const
{
stream << "default: ";
statement_->Print(stream);
}
58 changes: 58 additions & 0 deletions src/ast_while_statement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "ast_while_statement.hpp"

void WhileStatement::EmitRISC(std::ostream &stream, Context &context) const
{

std::string StartLabel = context.NewLabel();
std::string EndLabel = context.NewLabel();


stream << StartLabel << ":" << std::endl;

expression_->EmitRISC(stream, context);

stream << "beqz a0, " << EndLabel << std::endl;

statement_->EmitRISC(stream, context);

stream << "beqz zero, " << StartLabel << std::endl;

stream << EndLabel << ":" << std::endl;
}

void WhileStatement::Print(std::ostream &stream) const
{
stream << "while(";
expression_->Print(stream);
stream << "){" << std::endl;
statement_->Print(stream);
stream << "}" << std::endl;
}

void DoWhileStatement::EmitRISC(std::ostream &stream, Context &context) const
{

std::string StartLabel = context.NewLabel();
std::string EndLabel = context.NewLabel();

stream << StartLabel << ":" << std::endl;

statement_->EmitRISC(stream, context);

expression_->EmitRISC(stream, context);

stream << "beqz a0, " << EndLabel << std::endl;

stream << "beqz zero, " << StartLabel << std::endl;

stream << EndLabel << ":" << std::endl;
}

void DoWhileStatement::Print(std::ostream &stream) const
{
stream << "do{";
statement_->Print(stream);
stream << "}while(";
expression_->Print(stream);
stream << ");" << std::endl;
}
Loading

0 comments on commit 71a5970

Please sign in to comment.