diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-16 21:24:02 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-16 21:24:02 +0000 |
commit | 4858aa179ddc6044c55c8ae8708a76af3e287b05 (patch) | |
tree | f9bcc54ed13720c8a74a5dd726ecfdea083a7e28 /c_compiler/src | |
parent | f7b531ebaaa784a0dcebb877ec7b831b324f3510 (diff) | |
download | Compiler-4858aa179ddc6044c55c8ae8708a76af3e287b05.tar.gz Compiler-4858aa179ddc6044c55c8ae8708a76af3e287b05.zip |
function calls now work completely
Diffstat (limited to 'c_compiler/src')
-rw-r--r-- | c_compiler/src/c_parser.y | 25 | ||||
-rw-r--r-- | c_compiler/src/declaration.cpp | 6 | ||||
-rw-r--r-- | c_compiler/src/function.cpp | 4 |
3 files changed, 18 insertions, 17 deletions
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 4a9f929..1ee7966 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -57,6 +57,7 @@ void yyerror(const char *); %type <declaration> ParameterList Parameter DeclarationList Declaration InitDeclaratorList InitDeclarator IdentifierList + Declarator DirectDeclarator %type <expression> Expression AssignmentExpression ConditionalExpression LogicalOrExpression LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression @@ -67,8 +68,6 @@ void yyerror(const char *); %type <type> DeclarationSpec -%type <string> Declarator DirectDeclarator - %type <number> T_INT_CONST %type <string> T_IDENTIFIER ASSIGN_OPER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT @@ -97,19 +96,14 @@ ExternalDeclaration: // FUNCTION DEFINITION FunctionDefinition: - DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { - if($4->getId() == "") - $$ = new Function(*$2, $6); - else - $$ = new Function(*$2, $6, $4); - delete $2; - } + DeclarationSpec Declarator CompoundStatement { + $$ = new Function($2->getId(), $3, $2->getNext()); } ; ParameterList: %empty { $$ = new Declaration(); } | Parameter { $$ = $1; } - | ParameterList T_CMA Parameter { $3->linkDeclaration($$); $$ = $3;} + | ParameterList T_CMA Parameter { $3->linkDeclaration($$); $$ = $3; } ; Parameter: DeclarationSpec T_IDENTIFIER { $$ = new Declaration(*$2); delete $2; } @@ -146,12 +140,12 @@ DeclarationSpec: ; InitDeclaratorList: - InitDeclarator { $$ = new Declaration(*$1); delete $1;} + InitDeclarator { $$ = $1; } | InitDeclaratorList T_CMA InitDeclarator { $3->linkListDeclaration($$); $$ = $3; } ; -InitDeclarator: Declarator { $$ = new Declaration(*$1); delete $1; } - | Declarator T_EQ AssignmentExpression { $$ = new Declaration(*$1, $3); delete $1; } +InitDeclarator: Declarator { $$ = $1; } + | Declarator T_EQ AssignmentExpression { $$->setInitializer($3); } ; Declarator: DirectDeclarator { $$ = $1; } @@ -159,11 +153,12 @@ Declarator: DirectDeclarator { $$ = $1; } ; DirectDeclarator: - T_IDENTIFIER { $$ = $1; } + T_IDENTIFIER { $$ = new Declaration(*$1); delete $1; } | T_LRB Declarator T_RRB { $$ = $2; } | DirectDeclarator T_LSB ConditionalExpression T_RSB { $$ = $1; } | DirectDeclarator T_LSB T_RSB { $$ = $1; } - | DirectDeclarator T_LRB ParameterList T_RRB { $$ = $1; } + | DirectDeclarator T_LRB T_RRB { $$ = $1; } + | DirectDeclarator T_LRB ParameterList T_RRB { $1->linkDeclaration($3); $$ = $1; } | DirectDeclarator T_LRB IdentifierList T_RRB { $$ = $1; } ; diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index d1a0f69..353b470 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -87,6 +87,12 @@ void Declaration::setType(Type* type) type_ = type_ptr; } +void Declaration::setInitializer(Expression* initializer) +{ + ExpressionPtr expression_ptr(initializer); + initializer_ = expression_ptr; +} + DeclarationPtr Declaration::getNext() const { return next_declaration_; diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index 179079e..4fb9d95 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -6,7 +6,7 @@ // Function definition -Function::Function(const std::string& id, Statement* statement, Declaration* parameter_list) +Function::Function(const std::string& id, Statement* statement, DeclarationPtr parameter_list) : id_(id), statement_(statement), parameter_list_(parameter_list) {} @@ -102,7 +102,7 @@ void Function::printParameterAsm(VariableStackBindings& bindings, unsigned& stac } for(auto itr = parameter_vector.rbegin(); itr != parameter_vector.rend(); ++itr) { - unsigned i = parameter_vector.rbegin() - itr; + unsigned i = itr-parameter_vector.rbegin(); bindings.insertBinding((*itr)->getId(), (*itr)->getType(), (i+stack_offset)*4); if(i < 4) std::cout << "\tsw\t$" << 4+i << "," << (i+stack_offset)*4 << "($fp)\n"; |