diff options
author | ymherklotz <ymherklotz@gmail.com> | 2017-03-10 19:57:46 +0000 |
---|---|---|
committer | ymherklotz <ymherklotz@gmail.com> | 2017-03-10 19:57:46 +0000 |
commit | a6fa6da5de8252a205e000dd7acc2589735aa55c (patch) | |
tree | 77a791e0877fc4925f8d0dba6eb746bb32eb88e9 | |
parent | fc4486443f1af75526306377437cb82c2c9f37ac (diff) | |
download | Compiler-a6fa6da5de8252a205e000dd7acc2589735aa55c.tar.gz Compiler-a6fa6da5de8252a205e000dd7acc2589735aa55c.zip |
Can do simple add and subtract
-rw-r--r-- | Notes.org | 15 | ||||
-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 | 4 | ||||
-rw-r--r-- | c_compiler/src/c_lexer.flex | 2 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 33 |
6 files changed, 44 insertions, 10 deletions
@@ -98,10 +98,23 @@ The class will contain map of strings containing the variable name as the _key_ and the stack position as the stack position and type as the attributes, which will be combined using a struct. -***** TODO Work on statements and declaration +***** DONE Work on statements and declaration Implement the adding to the bindings part that has to be added in the statements and declarations. +***** TODO Work on expressions + +***** TODO Comment code + + comment code to know where we are at and what we still have to do. + +****** TODO Implement add and sub + still have to work on the subtraction + + work on different types to implement the correct add + +****** TODO Implement multiplication + **** TODO Expression ***** printasm(VSB binding) diff --git a/c_compiler/GRTAGS b/c_compiler/GRTAGS Binary files differindex f6e85c0..216dfbb 100644 --- a/c_compiler/GRTAGS +++ b/c_compiler/GRTAGS diff --git a/c_compiler/GTAGS b/c_compiler/GTAGS Binary files differindex e0846e7..d62a222 100644 --- a/c_compiler/GTAGS +++ b/c_compiler/GTAGS diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index 2476de1..89a996e 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -42,10 +42,10 @@ public: class AdditiveExpression : public OperationExpression { private: - std::string operand; + std::string operation; public: - AdditiveExpression(Expression* _lhs, const std::string& _operand, Expression* _rhs); + AdditiveExpression(Expression* _lhs, const std::string& _operation, Expression* _rhs); virtual VariableStackBindings printasm(VariableStackBindings bindings) const; }; diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex index b225522..f6f2dd3 100644 --- a/c_compiler/src/c_lexer.flex +++ b/c_compiler/src/c_lexer.flex @@ -67,7 +67,7 @@ ALL . [.] { return T_DOT; } [-][>] { return T_ARROW; } [+-][+-] { return T_INCDEC; } -[+-] { return T_ADDSUB_OP; } +[+-] { yylval.string = new std::string(yytext); return T_ADDSUB_OP; } [=] { yylval.string = new std::string(yytext); return T_EQ; } {ASSIGNMENT_OPERATOR} { yylval.string = new std::string(yytext); return T_ASSIGN_OPER; } diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 8935e37..fb5b62b 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -5,17 +5,25 @@ // Expression definition +// There are no values to delete so it is just empty Expression::~Expression() {} void Expression::print() const -{} +{ + std::cerr << "This expression has not been implemented yet" << std::endl; +} void Expression::printxml() const -{} +{ + // Does nothing as I do not want it to appear in the xml output +} int32_t Expression::getPostfixStackPosition(VariableStackBindings bindings) const { + std::cerr << "Error: Can't call 'getPostfixStackPosition(VariableStackBindings " << + "bindings)' on this type of expression" << std::endl; + (void)bindings; return -1; } @@ -26,6 +34,7 @@ OperationExpression::OperationExpression(Expression* _lhs, Expression* _rhs) : lhs(_lhs), rhs(_rhs) {} +// deletes the two member variables that have been initialized OperationExpression::~OperationExpression() { delete lhs; @@ -41,30 +50,41 @@ AssignmentExpression::AssignmentExpression(Expression* _lhs, Expression* _rhs) VariableStackBindings AssignmentExpression::printasm(VariableStackBindings bindings) const { + // the lhs is forced to have a stack position due to it being a function, array or other type of variable int32_t store_stack_position = lhs->getPostfixStackPosition(bindings); rhs->printasm(bindings); + // we are assigning so we don't have to evaluate the lhs as it will be overwritten anyways std::cout << "\tsw\t$2," << store_stack_position << "($fp)" << std::endl; - return bindings; } // Additive Expression definition -AdditiveExpression::AdditiveExpression(Expression* _lhs, const std::string& _operand, Expression* _rhs) - : OperationExpression(_lhs, _rhs), operand(_operand) +AdditiveExpression::AdditiveExpression(Expression* _lhs, const std::string& _operation, Expression* _rhs) + : OperationExpression(_lhs, _rhs), operation(_operation) {} VariableStackBindings AdditiveExpression::printasm(VariableStackBindings bindings) const { rhs->printasm(bindings); + // move the rhs out of the way to be able to evaluate the lhs std::cout << "\tmove\t$3,$2" << std::endl; lhs->printasm(bindings); - std::cout << "\tadd\t$2,$2,$3" << std::endl; + // then perform the right operation + + // currently using signed and sub because I only have signed numbers implemented + // must update this as I add more types + if(operation == "+") + std::cout << "\tadd\t$2,$2,$3" << std::endl; + else if(operation == "-") + std::cout << "\tsub\t$2,$2,$3" << std::endl; + else + std::cerr << "Don't recognize symbol: '" << operation << "'" << std::endl; return bindings; } @@ -106,6 +126,7 @@ Constant::Constant(const int32_t& constant) VariableStackBindings Constant::printasm(VariableStackBindings bindings) const { + // constant only has to load to $2 because the other expression will take care of the rest std::cout << "\tli\t$2," << m_constant << std::endl; return bindings; |