From aeb81c1a8959905878c242e752b596325325db8f Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Thu, 23 Mar 2017 12:47:29 +0000 Subject: Better types --- c_compiler/include/declaration.hpp | 2 +- c_compiler/include/type.hpp | 82 +++++++++++++----- c_compiler/src/c_parser.y | 33 ++++++-- c_compiler/src/declaration.cpp | 5 +- c_compiler/src/expression.cpp | 6 +- c_compiler/src/type.cpp | 168 ++++++++++++++++++++++++++++--------- 6 files changed, 218 insertions(+), 78 deletions(-) diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp index f99c4a3..bcd12a7 100644 --- a/c_compiler/include/declaration.hpp +++ b/c_compiler/include/declaration.hpp @@ -33,7 +33,7 @@ public: void linkDeclaration(Declaration* next_declaration); void linkListDeclaration(Declaration* next_list_declaration); - void setType(Type* type); + void setType(TypePtr type); void setInitializer(Expression* initializer); void setExternDeclaration(bool is_extern); diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp index bd9fd4b..8c881c1 100644 --- a/c_compiler/include/type.hpp +++ b/c_compiler/include/type.hpp @@ -13,15 +13,14 @@ typedef std::shared_ptr TypePtr; class Type : public Node { public: - virtual void print() const; - virtual void printXml() const; - virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned& label_count) const; - - virtual std::string getType() const = 0; - - virtual TypePtr type(); - virtual TypePtr type(Type* type_ptr); - virtual TypePtr type(TypePtr type_ptr); + virtual void print() const = 0; + virtual void printXml() const = 0; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const = 0; + + virtual TypePtr type() = 0; + virtual TypePtr type(Type *type_ptr) = 0; + virtual TypePtr type(TypePtr type_ptr) = 0; + virtual void setSigned(bool _signed); virtual void setExtern(bool _extern); virtual void setStatic(bool _static); @@ -31,16 +30,35 @@ public: class Array : public Type { +private: + int size_; + TypePtr type_; public: - Array(); - virtual std::string getType() const; + Array(const int &size, TypePtr type_); + + virtual void print() const; + virtual void printXml() const; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; + + virtual TypePtr type(); + virtual TypePtr type(Type *type_ptr); + virtual TypePtr type(TypePtr type_ptr); }; class Pointer : public Type { +private: + TypePtr type_; public: Pointer(); - virtual std::string getType() const; + + virtual void print() const; + virtual void printXml() const; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; + + virtual TypePtr type(); + virtual TypePtr type(Type *type_ptr); + virtual TypePtr type(TypePtr type_ptr); }; class TypeContainer : public Type @@ -56,10 +74,14 @@ protected: public: TypeContainer(); - virtual std::string getType() const; + virtual void print() const; + virtual void printXml() const; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; + virtual TypePtr type(); - virtual TypePtr type(Type* type_ptr); + virtual TypePtr type(Type *type_ptr); virtual TypePtr type(TypePtr type_ptr); + virtual void setSigned(bool _signed); virtual void setExtern(bool _extern); virtual void setStatic(bool _static); @@ -70,39 +92,53 @@ public: class Specifier : public Type { public: - virtual std::string getType() const = 0; + virtual void print() const = 0; + virtual void printXml() const = 0; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const = 0; + + virtual TypePtr type(); + virtual TypePtr type(Type *type_ptr); + virtual TypePtr type(TypePtr type_ptr); }; class Int : public Specifier { public: Int(); - - virtual std::string getType() const; + + virtual void print() const; + virtual void printXml() const; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; }; class Void : public Specifier { public: Void(); - - virtual std::string getType() const; + + virtual void print() const; + virtual void printXml() const; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; }; class Char : public Specifier { public: Char(); - - virtual std::string getType() const; + + virtual void print() const; + virtual void printXml() const; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; }; class Float : public Specifier { public: Float(); - - virtual std::string getType() const; + + virtual void print() const; + virtual void printXml() const; + virtual VariableStackBindings printAsm(VariableStackBindings bindings, unsigned &label_count) const; }; #endif 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 DeclarationSpecifierList +%type DeclarationSpecifierList Pointer %type T_INT_CONST %type 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 tmp_type; + if(tmp_decl->getType() == nullptr) + tmp_type = std::make_shared(); + 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 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; } -- cgit