From fdd6ff07cee824078c5315bf07926ee15bbdde85 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 6 Mar 2017 17:37:51 +0000 Subject: making changes to type in lexer now --- Notes.org | 1 - c_compiler/include/ast.hpp | 6 ++ c_compiler/include/declaration.hpp | 3 +- c_compiler/include/function.hpp | 6 ++ c_compiler/include/statement.hpp | 18 +++- c_compiler/include/type.hpp | 61 ++++++++++++- c_compiler/src/c_lexer.flex | 8 -- c_compiler/src/c_parser.y | 4 +- c_compiler/src/compiler_main.cpp | 4 +- c_compiler/src/declaration.cpp | 17 ++-- c_compiler/src/expression.cpp | 14 ++- c_compiler/src/function.cpp | 8 +- c_compiler/src/initializer.cpp | 18 ++-- c_compiler/src/statement.cpp | 95 ++++++++++++++++----- c_compiler/src/translation_unit.cpp | 15 ++-- c_compiler/src/type.cpp | 70 +++++++++++++++ c_parser/test/out/.diff.txt | 164 +++++++++++++++++++++++++++++++++++ c_parser/test/out/.pretty.xml | 161 ++++++++++++++++++++++++++++++++++ c_parser/test/out/.stderr.txt | 0 c_parser/test/out/.stdout.xml | 166 ++++++++++++++++++++++++++++++++++++ test_parser.sh | 4 +- 21 files changed, 778 insertions(+), 65 deletions(-) create mode 100644 c_compiler/src/type.cpp create mode 100644 c_parser/test/out/.diff.txt create mode 100644 c_parser/test/out/.pretty.xml create mode 100644 c_parser/test/out/.stderr.txt create mode 100644 c_parser/test/out/.stdout.xml diff --git a/Notes.org b/Notes.org index a52de5b..6766d99 100644 --- a/Notes.org +++ b/Notes.org @@ -93,7 +93,6 @@ *** TODOs - - Change parser to have two vectors with global variables and functions. - Analyse tree with map including "name", and struct with "type" and "stack position", and a counter passed by reference. - This function will be called by printasm() in the base class and will only be diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp index 86c9934..cea7e82 100644 --- a/c_compiler/include/ast.hpp +++ b/c_compiler/include/ast.hpp @@ -4,6 +4,12 @@ #include #include #include +#include +#include + +struct VarLocation; + +typedef std::map VariableStack; #include "node.hpp" #include "expression.hpp" diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp index 141d4d0..9287827 100644 --- a/c_compiler/include/declaration.hpp +++ b/c_compiler/include/declaration.hpp @@ -11,7 +11,7 @@ protected: std::string id; Initializer* init; Declaration* next_decl; - Declaration* decl_list; + Declaration* list_next_decl; public: Declaration(const std::string& _id = ""); @@ -24,6 +24,7 @@ public: void addList(Declaration* _next_decl); Declaration* getNext() const; + Declaration* getNextListItem() const; std::string getId() const; }; diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp index edfb958..967c9d8 100644 --- a/c_compiler/include/function.hpp +++ b/c_compiler/include/function.hpp @@ -4,6 +4,12 @@ #include "ast.hpp" +struct VarLocation { + Type* type; + int32_t stack_position; +}; + + class Function : public Node { protected: Type* type; diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp index dff9902..17a5153 100644 --- a/c_compiler/include/statement.hpp +++ b/c_compiler/include/statement.hpp @@ -7,6 +7,7 @@ class Statement : public Node { protected: Statement* next_statement; + public: Statement(Statement* statement = nullptr); @@ -14,9 +15,9 @@ public: virtual void printxml() const = 0; virtual void printasm() const = 0; - void addStatement(Statement* _next) { - next_statement = _next; - } + virtual void count_variables(int32_t& var_count) const = 0; + + void addStatement(Statement* _next); }; @@ -24,6 +25,7 @@ class CompoundStatement : public Statement { protected: Declaration* m_decl; Statement* m_statement; + public: CompoundStatement(Declaration* decl = nullptr, Statement* statement = nullptr); CompoundStatement(Statement* statement); @@ -31,6 +33,8 @@ public: virtual void print() const; virtual void printxml() const; virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; @@ -44,6 +48,8 @@ public: virtual void print() const; virtual void printxml() const; virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; @@ -56,6 +62,8 @@ public: virtual void print() const; virtual void printxml() const; virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; @@ -68,6 +76,8 @@ public: virtual void print() const; virtual void printxml() const; virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; @@ -80,6 +90,8 @@ public: virtual void print() const; virtual void printxml() const; virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp index 6e8eefa..415f00f 100644 --- a/c_compiler/include/type.hpp +++ b/c_compiler/include/type.hpp @@ -6,11 +6,64 @@ class Type : public Node { public: - Type(); + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; - virtual void print() const {} - virtual void printxml() const {} - virtual void printasm() const {} + virtual std::string getType() const = 0; +}; + + +class Specifier : Type { +public: + virtual std::string getType() const = 0; +}; + + +class Pointer : Type { +protected: + Type* pointer_type; + +public: + Pointer(Type* _pointer_type); + + virtual std::string getType() const; +}; + + +class Array : Type { +protected: + int32_t size; + Type* array_type; + +public: + Array(Type* _array_type, int32_t _size = 0); + + virtual std::string getType() const; +}; + + +class Void : Specifier { +public: + Void(); + + virtual std::string getType() const; +}; + + +class Int : Specifier { +public: + Int(); + + virtual std::string getType() const; +}; + + +class Char : Specifier { +public: + Char(); + + virtual std::string getType() const; }; diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex index c8ca90a..38ee946 100644 --- a/c_compiler/src/c_lexer.flex +++ b/c_compiler/src/c_lexer.flex @@ -8,18 +8,10 @@ extern "C" int fileno(FILE *stream); %} -KEYWORD auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while - IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* -OPERATOR [.][.][.]|[<>][<>][=]|[-][-]|[+][+]|[|][|]|[#][#]|[&][&]|[+\-*\/<>=!%^|&][=]|[<][<]|[->][>]|[<>&=+\/\-*(){}\[\]\.,%~!?:|^;] - ASSIGNMENT_OPERATOR (([<>][<>]|[*\/%+\-&^|])[=]|[=]) -FRACTIONALCONSTANT (([0-9]*\.[0-9]+)|([0-9]+\.)) -EXPONENTPART ([eE][+-]?[0-9]+) - -FLOATINGSUFFI X ([flFL]) INTEGERSUFFIX ([uU][lL]|[lL][uU]|[uUlL]) DECIMALCONSTANT ([1-9][0-9]*) diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 2864907..4a78ec5 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -73,14 +73,14 @@ void yyerror(const char *); %% ROOT: - TranslationUnit { g_root = $1; } + TranslationUnit { g_root = $1; } ; // EXTERNAL DEFINITION TranslationUnit: ExternalDeclaration { $$ = new TranslationUnit($1); } -| TranslationUnit ExternalDeclaration { $$->push($2); } + | TranslationUnit ExternalDeclaration { $$->push($2); } ; ExternalDeclaration: diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp index a4df8a7..b550eba 100644 --- a/c_compiler/src/compiler_main.cpp +++ b/c_compiler/src/compiler_main.cpp @@ -4,9 +4,11 @@ int main(int argc, char *argv[]) { - TranslationUnit* ast = parseAST(); + Node* ast = parseAST(); ast->printxml(); + + ast->printasm(); return 0; } diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp index 5341b5b..2fba9f9 100644 --- a/c_compiler/src/declaration.cpp +++ b/c_compiler/src/declaration.cpp @@ -4,7 +4,8 @@ // Declaration definition Declaration::Declaration(const std::string& _id) - : id(_id) {} + : id(_id) +{} void Declaration::print() const { @@ -20,15 +21,16 @@ void Declaration::printxml() const if(next_decl != nullptr) next_decl->printxml(); - if(decl_list != nullptr) { - decl_list->printxml(); + if(list_next_decl != nullptr) { + list_next_decl->printxml(); } if(id != "") std::cout << "" << std::endl; } -void Declaration::printasm() const {} +void Declaration::printasm() const +{} void Declaration::addDeclaration(Declaration* _next_decl) { @@ -37,7 +39,7 @@ void Declaration::addDeclaration(Declaration* _next_decl) void Declaration::addList(Declaration* _next_decl) { - decl_list = _next_decl; + list_next_decl = _next_decl; } Declaration* Declaration::getNext() const @@ -45,6 +47,11 @@ Declaration* Declaration::getNext() const return next_decl; } +Declaration* Declaration::getNextListItem() const +{ + return list_next_decl; +} + std::string Declaration::getId() const { return id; diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp index 5eab857..4b09581 100644 --- a/c_compiler/src/expression.cpp +++ b/c_compiler/src/expression.cpp @@ -3,8 +3,14 @@ // Expression definition -Expression::Expression(const Node* expr) {} +Expression::Expression(const Node* expr) +{} -void Expression::print() const {} -void Expression::printxml() const {} -void Expression::printasm() const {} +void Expression::print() const +{} + +void Expression::printxml() const +{} + +void Expression::printasm() const +{} diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp index a132280..acd5547 100644 --- a/c_compiler/src/function.cpp +++ b/c_compiler/src/function.cpp @@ -44,4 +44,10 @@ void Function::printxml() const } void Function::printasm() const -{} +{ + int32_t count = 0; + if(statement != nullptr) + statement->count_variables(count); + + std::cout << id << ": " << count << " variables defined" << std::endl; +} diff --git a/c_compiler/src/initializer.cpp b/c_compiler/src/initializer.cpp index 61a0413..580d81f 100644 --- a/c_compiler/src/initializer.cpp +++ b/c_compiler/src/initializer.cpp @@ -3,20 +3,26 @@ // Initializer definition -Initializer::Initializer() {} +Initializer::Initializer() +{} -void Initializer::print() const {} +void Initializer::print() const +{} -void Initializer::printxml() const {} +void Initializer::printxml() const +{} -void Initializer::printasm() const {} +void Initializer::printasm() const +{} // Integer definition -Integer::Integer() : Initializer() {} +Integer::Integer() : Initializer() +{} // String Literal definition -StringLiteral::StringLiteral() : Initializer() {} +StringLiteral::StringLiteral() : Initializer() +{} diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp index 28bd981..14d5808 100644 --- a/c_compiler/src/statement.cpp +++ b/c_compiler/src/statement.cpp @@ -4,34 +4,24 @@ // General base Statement definition Statement::Statement(Statement* statement) - : next_statement(statement) {} - -void Statement::print() const -{ - if(next_statement != nullptr) - next_statement->print(); -} - -void Statement::printxml() const -{ - if(next_statement != nullptr) - next_statement->printxml(); -} + : next_statement(statement) +{} -void Statement::printasm() const +void Statement::addStatement(Statement* _next) { - if(next_statement != nullptr) - next_statement->printasm(); + next_statement = _next; } // Compound Statement definition CompoundStatement::CompoundStatement(Declaration* decl, Statement* statement) - : Statement(), m_decl(decl), m_statement(statement) {} + : Statement(), m_decl(decl), m_statement(statement) +{} CompoundStatement::CompoundStatement(Statement* statement) - : m_statement(statement) {} + : m_statement(statement) +{} void CompoundStatement::print() const { @@ -61,6 +51,31 @@ void CompoundStatement::printxml() const void CompoundStatement::printasm() const {} +void CompoundStatement::count_variables(int32_t& var_count) const +{ + Declaration* declaration = m_decl; + + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_statement != nullptr) + m_statement->count_variables(var_count); + + while(declaration != nullptr) { + Declaration* declaration_list = declaration->getNextListItem(); + + while(declaration_list != nullptr) { + var_count++; + + declaration_list = declaration_list->getNextListItem(); + } + + var_count++; + + declaration = declaration->getNext(); + } +} + // Selection Statement definition @@ -86,11 +101,24 @@ void SelectionStatement::printxml() const void SelectionStatement::printasm() const {} +void SelectionStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_if != nullptr) + m_if->count_variables(var_count); + + if(m_else != nullptr) + m_else->count_variables(var_count); +} + // Expression Statement definition ExpressionStatement::ExpressionStatement(Expression* expr) - : Statement(), m_expr(expr) {} + : Statement(), m_expr(expr) +{} void ExpressionStatement::print() const {} @@ -99,13 +127,20 @@ void ExpressionStatement::printxml() const {} void ExpressionStatement::printasm() const -{} +{} + +void ExpressionStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); +} // Jump Statement definition JumpStatement::JumpStatement(Expression* expr) - : m_expr(expr) {} + : m_expr(expr) +{} void JumpStatement::print() const {} @@ -122,11 +157,18 @@ void JumpStatement::printasm() const std::cout << "\tlw\t$2,8($fp)" << std::endl; } +void JumpStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); +} + // Iteration Statement definition IterationStatement::IterationStatement(Statement* statement) - : m_statement(statement) {} + : m_statement(statement) +{} void IterationStatement::print() const {} @@ -141,3 +183,12 @@ void IterationStatement::printxml() const void IterationStatement::printasm() const {} + +void IterationStatement::count_variables(int32_t& var_count) const +{ + if(next_statement != nullptr) + next_statement->count_variables(var_count); + + if(m_statement != nullptr) + m_statement->count_variables(var_count); +} diff --git a/c_compiler/src/translation_unit.cpp b/c_compiler/src/translation_unit.cpp index c1e4c13..a3566d7 100644 --- a/c_compiler/src/translation_unit.cpp +++ b/c_compiler/src/translation_unit.cpp @@ -3,17 +3,20 @@ // Translation Unit definition -TranslationUnit::TranslationUnit(Node* decl) { +TranslationUnit::TranslationUnit(Node* decl) +{ push(decl); } -void TranslationUnit::print() const { +void TranslationUnit::print() const +{ for(auto& node : translation_unit) { node->print(); } } -void TranslationUnit::printxml() const { +void TranslationUnit::printxml() const +{ std::cout << "\n" << std::endl; for(auto& node : translation_unit) { node->printxml(); @@ -21,12 +24,14 @@ void TranslationUnit::printxml() const { std::cout << "" << std::endl; } -void TranslationUnit::printasm() const { +void TranslationUnit::printasm() const +{ for(auto& node : translation_unit) { node->printasm(); } } -void TranslationUnit::push(Node* decl) { +void TranslationUnit::push(Node* decl) +{ translation_unit.push_back(decl); } diff --git a/c_compiler/src/type.cpp b/c_compiler/src/type.cpp new file mode 100644 index 0000000..b7c3fec --- /dev/null +++ b/c_compiler/src/type.cpp @@ -0,0 +1,70 @@ +#include "ast.hpp" + + +// Type definition + +void Type::print() const +{ + std::cout << getType() << " " << std::endl; +} + +void Type::printxml() const +{} + +void Type::printasm() const +{} + + +// Pointer definition + +Pointer::Pointer(Type* _pointer_type) : pointer_type(_pointer_type) +{} + +std::string Pointer::getType() const +{ + return "pointer " + pointer_type->getType(); +} + + +// Array definition + +Array::Array(Type* _array_type, int32_t _size) : size(_size), array_type(_array_type) +{} + +std::string Array::getType() const +{ + return "array " + array_type->getType(); +} + + +// Void definition + +Void::Void() +{} + +std::string Void::getType() const +{ + return "void"; +} + + +// Int defintion + +Int::Int() +{} + +std::string Int::getType() const +{ + return "int"; +} + + +// Char definition + +Char::Char() +{} + +std::string Char::getType() const +{ + return "char"; +} diff --git a/c_parser/test/out/.diff.txt b/c_parser/test/out/.diff.txt new file mode 100644 index 0000000..332a940 --- /dev/null +++ b/c_parser/test/out/.diff.txt @@ -0,0 +1,164 @@ +1c1,161 +< +--- +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> diff --git a/c_parser/test/out/.pretty.xml b/c_parser/test/out/.pretty.xml new file mode 100644 index 0000000..81983a7 --- /dev/null +++ b/c_parser/test/out/.pretty.xml @@ -0,0 +1,161 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/c_parser/test/out/.stderr.txt b/c_parser/test/out/.stderr.txt new file mode 100644 index 0000000..e69de29 diff --git a/c_parser/test/out/.stdout.xml b/c_parser/test/out/.stdout.xml new file mode 100644 index 0000000..efabeac --- /dev/null +++ b/c_parser/test/out/.stdout.xml @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test_parser.sh b/test_parser.sh index fd6d4f2..cddc491 100755 --- a/test_parser.sh +++ b/test_parser.sh @@ -6,7 +6,7 @@ echo "========================================" echo " Cleaning the temporaries and outputs" make clean echo " Force building lexer" -make -B bin/c_compiler +make -B bin/c_parser if [[ "$?" -ne 0 ]]; then echo "Build failed."; @@ -25,7 +25,7 @@ for i in c_parser/test/in/*.c; do echo "" echo "Input file : ${i}" BASENAME=$(basename $i .c); - cat $i | ./bin/c_compiler > c_parser/test/out/$BASENAME.stdout.xml 2> c_parser/test/out/$BASENAME.stderr.txt + cat $i | ./bin/c_parser > c_parser/test/out/$BASENAME.stdout.xml 2> c_parser/test/out/$BASENAME.stderr.txt tidy -xml -i -q -o c_parser/test/out/$BASENAME.pretty.xml c_parser/test/out/$BASENAME.stdout.xml diff <(cat c_parser/test/ref/$BASENAME.stdout.xml | tidy -xml -i -q) <(cat c_parser/test/out/$BASENAME.stdout.xml | tidy -xml -i -q) > c_parser/test/out/$BASENAME.diff.txt -- cgit