diff options
author | ymherklotz <ymherklotz@gmail.com> | 2017-03-10 17:38:14 +0000 |
---|---|---|
committer | ymherklotz <ymherklotz@gmail.com> | 2017-03-10 17:38:14 +0000 |
commit | 7a61349a09b4db96794a2a90d2d488f020885442 (patch) | |
tree | 22194f4eeb49b3111531dc352df14264c3cfb973 | |
parent | b9ab15bb2188543b8883ee8cf86540ed2684ed9d (diff) | |
download | Compiler-7a61349a09b4db96794a2a90d2d488f020885442.tar.gz Compiler-7a61349a09b4db96794a2a90d2d488f020885442.zip |
Finished assignment expression
-rw-r--r-- | Notes.org | 4 | ||||
-rw-r--r-- | c_compiler/GRTAGS | bin | 98304 -> 98304 bytes | |||
-rw-r--r-- | c_compiler/GTAGS | bin | 57344 -> 57344 bytes | |||
-rw-r--r-- | c_compiler/include/expression.hpp | 36 | ||||
-rw-r--r-- | c_compiler/src/c_parser.y | 4 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 47 | ||||
-rw-r--r-- | c_compiler/src/statement.cpp | 6 | ||||
-rw-r--r-- | c_compiler/test/in/04.c | 9 | ||||
-rw-r--r-- | c_compiler/test/in/05.c | 6 |
9 files changed, 88 insertions, 24 deletions
@@ -117,6 +117,10 @@ Have to be careful because of the way the expressions are parsed. Constant expressions will just output li $2, 6 for example +***** Assignment + + I'm assuming that the right hand side of an assigment is always a variable. + **** TODO Function class **** TODO Statement class diff --git a/c_compiler/GRTAGS b/c_compiler/GRTAGS Binary files differindex 13d7d20..4a1db03 100644 --- a/c_compiler/GRTAGS +++ b/c_compiler/GRTAGS diff --git a/c_compiler/GTAGS b/c_compiler/GTAGS Binary files differindex 5418332..b730179 100644 --- a/c_compiler/GTAGS +++ b/c_compiler/GTAGS diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index d446972..d77d9d5 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -4,25 +4,55 @@ #include "node.hpp" -class Expression : public Node { +class Expression : public Node +{ public: + virtual ~Expression(); + + virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0; + virtual void print() const; virtual void printxml() const; + virtual int32_t getPostfixStackPosition(VariableStackBindings bindings) const; +}; + + +class OperationExpression : public Expression +{ +protected: + Expression* lhs; + Expression* rhs; +public: + OperationExpression(Expression* _lhs, Expression* _rhs); + virtual ~OperationExpression(); + virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0; }; -class Identifier : public Expression { +class AssignmentExpression : public OperationExpression +{ +public: + AssignmentExpression(Expression* _lhs, Expression* _rhs); + + virtual VariableStackBindings printasm(VariableStackBindings bindings) const; +}; + + +class Identifier : public Expression +{ private: std::string m_id; public: Identifier(const std::string& id); virtual VariableStackBindings printasm(VariableStackBindings bindings) const; + virtual int32_t getPostfixStackPosition(VariableStackBindings bindings) const; }; -class Constant : public Expression { +class Constant : public Expression +{ private: int32_t m_constant; public: diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index b3430ee..c762151 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -202,7 +202,7 @@ SelectionStatement: ExpressionStatement: T_SC { $$ = new ExpressionStatement(); } -| Expression T_SC { $$ = new ExpressionStatement(); } +| Expression T_SC { $$ = new ExpressionStatement($1); } ; JumpStatement: @@ -223,7 +223,7 @@ Expression: AssignmentExpression: ConditionalExpression { $$ = $1; } - | UnaryExpression ASSIGN_OPER AssignmentExpression { $$ = $1; } +| UnaryExpression ASSIGN_OPER AssignmentExpression { $$ = new AssignmentExpression($1, $3); } ; ASSIGN_OPER: diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index d9a366b..29bfe0d 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -5,12 +5,50 @@ // Expression definition +Expression::~Expression() +{} + void Expression::print() const {} void Expression::printxml() const {} +int32_t Expression::getPostfixStackPosition(VariableStackBindings bindings) const +{ + return -1; +} + + +// OperationExpression definition + +OperationExpression::OperationExpression(Expression* _lhs, Expression* _rhs) + : lhs(_lhs), rhs(_rhs) +{} + +OperationExpression::~OperationExpression() +{ + delete lhs; + delete rhs; +} + + +// Assignment Expression definition + +AssignmentExpression::AssignmentExpression(Expression* _lhs, Expression* _rhs) + : OperationExpression(_lhs, _rhs) +{} + +VariableStackBindings AssignmentExpression::printasm(VariableStackBindings bindings) const +{ + int32_t store_stack_position = lhs->getPostfixStackPosition(bindings); + rhs->printasm(bindings); + + std::cout << "\tsw\t$2," << store_stack_position << "($fp)" << std::endl; + + return bindings; +} + // Identifier definition @@ -30,6 +68,15 @@ VariableStackBindings Identifier::printasm(VariableStackBindings bindings) const return bindings; } +int32_t Identifier::getPostfixStackPosition(VariableStackBindings bindings) const +{ + if(bindings.bindingExists(m_id)) { + return bindings.getStackPosition(m_id); + } + + return -1; +} + // Constant definition diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index af382dc..50217fb 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -150,11 +150,9 @@ VariableStackBindings ExpressionStatement::printasm(VariableStackBindings bindin { if(next_statement != nullptr) next_statement->printasm(bindings); - - if(m_expr != nullptr) { - std::cout << "SHould print" << std::endl; + + if(m_expr != nullptr) m_expr->printasm(bindings); - } return bindings; } diff --git a/c_compiler/test/in/04.c b/c_compiler/test/in/04.c deleted file mode 100644 index 57cf697..0000000 --- a/c_compiler/test/in/04.c +++ /dev/null @@ -1,9 +0,0 @@ -int main() { - int a = 3; - int b = 5; - return a + b; -} - -int f(int a, int v) { - return a + v; -} diff --git a/c_compiler/test/in/05.c b/c_compiler/test/in/05.c deleted file mode 100644 index 546012c..0000000 --- a/c_compiler/test/in/05.c +++ /dev/null @@ -1,6 +0,0 @@ -char *glob; - -int main() { - glob = "Yann"; - return 0; -} |