-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cfbad2
commit 71a5970
Showing
9 changed files
with
353 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.