diff options
Diffstat (limited to 'c_compiler/src')
-rw-r--r-- | c_compiler/src/c_parser.y | 33 | ||||
-rw-r--r-- | c_compiler/src/declaration.cpp | 5 | ||||
-rw-r--r-- | c_compiler/src/expression.cpp | 6 | ||||
-rw-r--r-- | c_compiler/src/type.cpp | 168 |
4 files changed, 158 insertions, 54 deletions
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index b6e8159..cbf1e32 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -69,13 +69,13 @@ void yyerror(const char *); PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression Constant Initializer InitializerList -%type <type> DeclarationSpecifierList +%type <type> DeclarationSpecifierList Pointer %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 T_MULT T_DIV T_REM T_EQUALITY_OP T_REL_OP T_SHIFT_OP T_INCDEC MultDivRemOP - UnaryOperator DeclarationSpecifier + UnaryOperator DeclarationSpecifier TypeQualifier TypeQualifierList %start ROOT @@ -123,11 +123,16 @@ DeclarationList: Declaration: DeclarationSpecifierList InitDeclaratorList T_SC { $$ = $2; - Declaration* tmp_decl = $2; - Type* tmp_type = new TypeContainer(); - + Declaration *tmp_decl = $2; + std::shared_ptr<Type> tmp_type; + if(tmp_decl->getType() == nullptr) + tmp_type = std::make_shared<TypeContainer>(); + else + tmp_type = tmp_decl->getType(); + while(tmp_decl != nullptr) { tmp_type->type($1->type()); + tmp_decl->setType(tmp_type); tmp_decl = tmp_decl->getNextListItem().get(); } @@ -174,9 +179,25 @@ InitDeclarator: Declarator { $$ = $1; } ; Declarator: DirectDeclarator { $$ = $1; } - | T_MULT DirectDeclarator { $$ = $2; } +| Pointer DirectDeclarator { $$ = $2; std::shared_ptr<Type> tmp($1); $$->setType(tmp); } ; +Pointer: +T_MULT { $$ = new Pointer(); delete $1; } +| T_MULT Pointer { $$ = $2; delete $1; } +| T_MULT TypeQualifierList Pointer { $$ = $3; delete $1; delete $2; } +; + +TypeQualifierList: +TypeQualifier { $$ = $1; } +| TypeQualifierList TypeQualifier { $$ = $2; delete $1; } +; + +TypeQualifier: +T_CONST { $$ = new std::string("const"); } +| T_VOLATILE { $$ = new std::string("volatile"); } +; + DirectDeclarator: T_IDENTIFIER { $$ = new Declaration(*$1); delete $1; } | T_LRB Declarator T_RRB { $$ = $2; } diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index eb98480..b151077 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -85,10 +85,9 @@ void Declaration::linkListDeclaration(Declaration* next_declaration) next_list_declaration_ = decl_ptr; } -void Declaration::setType(Type* type) +void Declaration::setType(TypePtr type) { - TypePtr type_ptr(type); - type_ = type_ptr; + type_ = type; } void Declaration::setInitializer(Expression* initializer) diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index e00c9b0..9c04afe 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -23,10 +23,8 @@ void Expression::countArguments(unsigned& argument_count) const (void)argument_count; } -void Expression::expressionDepth(unsigned& depth_count) const -{ - (void)depth_count; -} +void Expression::expressionDepth(unsigned &) const +{} int Expression::postfixStackPosition(VariableStackBindings bindings) const { diff --git a/c_compiler/src/type.cpp b/c_compiler/src/type.cpp index 04bc245..29b6902 100644 --- a/c_compiler/src/type.cpp +++ b/c_compiler/src/type.cpp @@ -6,57 +6,67 @@ // Type definition -void Type::print() const +void Type::setSigned(bool) { - printf("%s\n", getType().c_str()); + throw std::runtime_error("Error : cannot set sign"); } -void Type::printXml() const -{} - -VariableStackBindings Type::printAsm(VariableStackBindings bindings, unsigned&) const +void Type::setExtern(bool) { - return bindings; + throw std::runtime_error("Error : cannot set extern"); } -TypePtr Type::type() +void Type::setStatic(bool) { - throw std::runtime_error("Error : does not have a type"); + throw std::runtime_error("Error : cannot set static"); } -TypePtr Type::type(Type*) +void Type::setConst(bool) { - throw std::runtime_error("Error : cannot assign type"); + throw std::runtime_error("Error : cannot set const"); } -TypePtr Type::type(TypePtr) +void Type::setSize(int) { - throw std::runtime_error("Error : cannot assign type"); + throw std::runtime_error("Error : cannot set size"); } -void Type::setSigned(bool) + +// Array definition + +Array::Array(const int &size, TypePtr type) + : size_(size), type_(type) +{} + +void Array::print() const { - throw std::runtime_error("Error : cannot set sign"); + printf("Array\n"); } -void Type::setExtern(bool) +void Array::printXml() const +{} + +VariableStackBindings Array::printAsm(VariableStackBindings bindings, unsigned &label_count) const { - throw std::runtime_error("Error : cannot set extern"); + return bindings; } -void Type::setStatic(bool) +TypePtr Array::type() { - throw std::runtime_error("Error : cannot set static"); + return type_; } -void Type::setConst(bool) +TypePtr Array::type(Type *type_ptr) { - throw std::runtime_error("Error : cannot set const"); + TypePtr sh_type_ptr(type_ptr); + type_ = sh_type_ptr; + return type_; } -void Type::setSize(int) +TypePtr Array::type(TypePtr type_ptr) { - throw std::runtime_error("Error : cannot set size"); + type_ = type_ptr; + return type_; } @@ -65,32 +75,58 @@ void Type::setSize(int) Pointer::Pointer() {} -std::string Pointer::getType() const +void Pointer::print() const { - return "pointer"; + printf("Pointer\n"); } +void Pointer::printXml() const +{} -// Array definition +VariableStackBindings Pointer::printAsm(VariableStackBindings bindings, unsigned &label_count) const +{ + return bindings; +} -Array::Array() -{} +TypePtr Pointer::type() +{ + return type_; +} -std::string Array::getType() const +TypePtr Pointer::type(Type *type_ptr) { - return "array"; + TypePtr sh_type_ptr(type_ptr); + type_ = sh_type_ptr; + return type_; +} + +TypePtr Pointer::type(TypePtr type_ptr) +{ + type_ = type_ptr; + return type_; } + + // TypeContainer definition TypeContainer::TypeContainer() : type_(nullptr), size_(32), extern_(false), static_(false), const_(false), signed_(true) {} -std::string TypeContainer::getType() const +void TypeContainer::print() const +{ + printf("TypeContainer : "); + type_->print(); +} + +void TypeContainer::printXml() const +{} + +VariableStackBindings TypeContainer::printAsm(VariableStackBindings bindings, unsigned &label_count) const { - return type_->getType(); + return bindings; } TypePtr TypeContainer::type() @@ -98,7 +134,7 @@ TypePtr TypeContainer::type() return type_; } -TypePtr TypeContainer::type(Type* type_ptr) +TypePtr TypeContainer::type(Type *type_ptr) { TypePtr new_type_ptr(type_ptr); type_ = new_type_ptr; @@ -139,14 +175,40 @@ void TypeContainer::setSize(int size) } +// Specifier definitions + +TypePtr Specifier::type() +{ + throw std::runtime_error("Error : Cannot get type"); +} + +TypePtr Specifier::type(Type *) +{ + throw std::runtime_error("Error : Cannot get type"); +} + +TypePtr Specifier::type(TypePtr) +{ + throw std::runtime_error("Error : Cannot get type"); +} + + // Int definition Int::Int() {} -std::string Int::getType() const +void Int::print() const +{ + printf("Int\n"); +} + +void Int::printXml() const +{} + +VariableStackBindings Int::printAsm(VariableStackBindings bindings, unsigned &label_count) const { - return "int"; + return bindings; } @@ -155,9 +217,17 @@ std::string Int::getType() const Void::Void() {} -std::string Void::getType() const +void Void::print() const +{ + printf("Void\n"); +} + +void Void::printXml() const +{} + +VariableStackBindings Void::printAsm(VariableStackBindings bindings, unsigned &label_count) const { - return "void"; + return bindings; } @@ -166,9 +236,17 @@ std::string Void::getType() const Char::Char() {} -std::string Char::getType() const +void Char::print() const { - return "char"; + printf("Char\n"); +} + +void Char::printXml() const +{} + +VariableStackBindings Char::printAsm(VariableStackBindings bindings, unsigned &label_count) const +{ + return bindings; } @@ -177,7 +255,15 @@ std::string Char::getType() const Float::Float() {} -std::string Float::getType() const +void Float::print() const { - return "float"; + printf("Float\n"); +} + +void Float::printXml() const +{} + +VariableStackBindings Float::printAsm(VariableStackBindings bindings, unsigned &label_count) const +{ + return bindings; } |