diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-22 16:53:48 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-22 16:53:48 +0000 |
commit | 561b76bdebd584d03d4e451375777651a9d74017 (patch) | |
tree | fdab95e056a212a26b65a96ae2af894e8f42b82e /c_compiler | |
parent | f12ccd62ecf08774ce599a2e15d9042500d2760a (diff) | |
download | Compiler-561b76bdebd584d03d4e451375777651a9d74017.tar.gz Compiler-561b76bdebd584d03d4e451375777651a9d74017.zip |
Have to work on case
Diffstat (limited to 'c_compiler')
-rw-r--r-- | c_compiler/include/expression.hpp | 6 | ||||
-rw-r--r-- | c_compiler/src/c_lexer.flex | 7 | ||||
-rw-r--r-- | c_compiler/src/c_parser.y | 53 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 28 |
4 files changed, 87 insertions, 7 deletions
diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index 21e5880..1377a22 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -30,6 +30,8 @@ public: virtual int postfixStackPosition(VariableStackBindings bindings) const; virtual void setPostfixExpression(Expression* postfix_expression); virtual std::string id() const; + virtual ExpressionPtr getLhs() const; + virtual ExpressionPtr getRhs() const; void linkExpression(Expression* next_expression); ExpressionPtr nextExpression() const; @@ -43,11 +45,14 @@ protected: ExpressionPtr rhs_; public: OperationExpression(Expression* lhs, Expression* rhs); + OperationExpression(ExpressionPtr lhs, Expression* rhs); virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_count) const = 0; virtual int constantFold() const; virtual void expressionDepth(unsigned& depth_count) const; + virtual ExpressionPtr getLhs() const; + virtual ExpressionPtr getRhs() const; void evaluateExpression(VariableStackBindings bindings, unsigned& label_count) const; }; @@ -248,6 +253,7 @@ class AssignmentExpression : public OperationExpression { public: AssignmentExpression(Expression* lhs, Expression* rhs); + AssignmentExpression(ExpressionPtr lhs, Expression* rhs); virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_count) const; }; diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex index 4255880..2547c5a 100644 --- a/c_compiler/src/c_lexer.flex +++ b/c_compiler/src/c_lexer.flex @@ -8,7 +8,7 @@ IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* -ASSIGNMENT_OPERATOR (([<>][<>]|[*\/%+\-&^|])[=]|[=]) +ASSIGNMENT_OPERATOR (([<>][<>]|[*\/%+\-&^|])[=]) INTEGERSUFFIX ([uU][lL]|[lL][uU]|[uUlL]) @@ -59,6 +59,8 @@ ALL . [}] { return T_RCB; } [[] { return T_LSB; } []] { return T_RSB; } +[=] { yylval.string = new std::string(yytext); return T_EQ; } +{ASSIGNMENT_OPERATOR} { yylval.string = new std::string(yytext); return T_ASSIGN_OPER; } [?] { return T_QU; } [:] { return T_COL; } [|][|] { return T_LOG_OR; } @@ -79,9 +81,6 @@ ALL . [-][>] { return T_ARROW; } [+-][+-] { yylval.string = new std::string(yytext); return T_INCDEC; } [+-] { 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; } if { return T_IF; } else { return T_ELSE; } diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index c58bc75..79d8902 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -71,7 +71,7 @@ void yyerror(const char *); AndExpression EqualityExpression RelationalExpression ShiftExpression AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression - Constant + Constant Initializer InitializerList %type <type> DeclarationSpecifierList @@ -174,7 +174,7 @@ InitDeclaratorList: ; InitDeclarator: Declarator { $$ = $1; } - | Declarator T_EQ AssignmentExpression { $$->setInitializer($3); delete $2; } + | Declarator T_EQ Initializer { $$ = $1; $$->setInitializer($3); delete $2; } ; Declarator: DirectDeclarator { $$ = $1; } @@ -194,6 +194,15 @@ DirectDeclarator: IdentifierList: T_IDENTIFIER { $$ = new Declaration(); } | IdentifierList T_CMA T_IDENTIFIER { $$ = new Declaration(); } +Initializer: AssignmentExpression { $$ = $1; } + | T_LCB InitializerList T_RCB { $$ = $2; } + | T_LCB InitializerList T_CMA T_RCB { $$ = $2; } + ; + +InitializerList: + Initializer { $$ = $1; } + | InitializerList T_CMA Initializer { $3->linkExpression($$); $$ = $3; } + // Statement StatementList: @@ -250,7 +259,45 @@ Expression: AssignmentExpression { $$ = $1; } AssignmentExpression: ConditionalExpression { $$ = $1; } - | UnaryExpression ASSIGN_OPER AssignmentExpression { $$ = new AssignmentExpression($1, $3); delete $2; } + | UnaryExpression ASSIGN_OPER AssignmentExpression + { + Expression* tmp; + if(*$2 == "=") { + $$ = new AssignmentExpression($1, $3); + } else if(*$2 == "+=") { + tmp = new AdditiveExpression($1, "+", $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else if(*$2 == "-=") { + tmp = new AdditiveExpression($1, "-", $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else if(*$2 == "*=") { + tmp = new MultiplicativeExpression($1, "*", $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else if(*$2 == "/=") { + tmp = new MultiplicativeExpression($1, "/", $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else if(*$2 == "%=") { + tmp = new MultiplicativeExpression($1, "%", $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else if(*$2 == "&=") { + tmp = new AndExpression($1, $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else if(*$2 == "^=") { + tmp = new ExclusiveOrExpression($1, $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else if(*$2 == "|=") { + tmp = new InclusiveOrExpression($1, $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else if(*$2 == "<<=") { + tmp = new ShiftExpression($1, "<<", $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } else { + tmp = new ShiftExpression($1, ">>", $3); + $$ = new AssignmentExpression(tmp->getLhs(), tmp); + } + + delete $2; + } ; ASSIGN_OPER: T_ASSIGN_OPER { ; } diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index cdf7d0a..e00c9b0 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -46,6 +46,16 @@ std::string Expression::id() const return ""; } +ExpressionPtr Expression::getLhs() const +{ + throw std::runtime_error("Error : Cannot get lhs"); +} + +ExpressionPtr Expression::getRhs() const +{ + throw std::runtime_error("Error : Cannot get rhs"); +} + void Expression::linkExpression(Expression *next_expression) { ExpressionPtr expression_ptr(next_expression); @@ -64,6 +74,10 @@ OperationExpression::OperationExpression(Expression* lhs, Expression* rhs) : lhs_(lhs), rhs_(rhs) {} +OperationExpression::OperationExpression(ExpressionPtr lhs, Expression* rhs) + : lhs_(lhs), rhs_(rhs) +{} + int OperationExpression::constantFold() const { throw std::runtime_error("Error : Cannot constant fold expression\n"); @@ -83,6 +97,16 @@ void OperationExpression::expressionDepth(unsigned& depth_count) const depth_count = rhs_depth_count; } +ExpressionPtr OperationExpression::getLhs() const +{ + return lhs_; +} + +ExpressionPtr OperationExpression::getRhs() const +{ + return rhs_; +} + void OperationExpression::evaluateExpression(VariableStackBindings bindings, unsigned& label_count) const { // I can just evaluate the lhs with the same entry stack position @@ -542,6 +566,10 @@ AssignmentExpression::AssignmentExpression(Expression* lhs, Expression* rhs) : OperationExpression(lhs, rhs) {} +AssignmentExpression::AssignmentExpression(ExpressionPtr lhs, Expression* rhs) + : OperationExpression(lhs, rhs) +{} + VariableStackBindings AssignmentExpression::printAsm(VariableStackBindings bindings, unsigned& label_count) const { // TODO add stack and store results in there, also for addition and multiplication. |