From 446c2394ec8970198d645bbbb462c67b9e3f1b1e Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 2 Mar 2017 23:22:51 +0000 Subject: Changing ast structure again --- Notes.org | 12 ++++ c_compiler/include/ast.hpp | 7 +- c_compiler/include/base.hpp | 130 +++++++++++++++++++++++++++++++++++-- c_compiler/include/declaration.hpp | 43 +++--------- c_compiler/include/expression.hpp | 18 ++--- c_compiler/include/external.hpp | 13 ++++ c_compiler/include/function.hpp | 46 ++++--------- c_compiler/include/primitives.hpp | 31 +++------ c_compiler/include/statement.hpp | 55 ++++++---------- c_compiler/src/c_parser.y | 87 ++++++++++++++++--------- c_compiler/src/parser_main.cpp | 2 +- c_compiler/test/in/01.c | 9 +-- c_compiler/test/in/02.c | 1 - c_compiler/test/in/03.c | 1 - c_compiler/test/in/04.c | 4 -- c_compiler/test/in/05.c | 3 - c_compiler/test/in/06.c | 6 -- c_compiler/test/in/07.c | 5 -- c_compiler/test/in/08.c | 10 --- c_compiler/test/in/09.c | 15 ----- c_compiler/test/in/10.c | 18 ----- c_compiler/test/in/11.c | 10 --- c_compiler/test/in/12.c | 4 -- c_compiler/test/in/13.c | 8 --- c_compiler/test/in/14.c | 12 ---- c_compiler/test/in/15.c | 28 -------- c_compiler/test/out/01.pretty.xml | 10 +-- c_compiler/test/out/01.stderr.txt | 1 + c_compiler/test/out/01.stdout.s | 0 c_compiler/test/out/01.stdout.txt | 9 --- c_compiler/test/out/01.stdout.xml | 9 --- c_compiler/test/out/02.diff.txt | 0 c_compiler/test/out/02.pretty.xml | 6 -- c_compiler/test/out/02.stderr.txt | 0 c_compiler/test/out/02.stdout.xml | 7 -- c_compiler/test/out/03.diff.txt | 0 c_compiler/test/out/03.pretty.xml | 7 -- c_compiler/test/out/03.stderr.txt | 0 c_compiler/test/out/03.stdout.xml | 8 --- c_compiler/test/out/04.diff.txt | 0 c_compiler/test/out/04.pretty.xml | 12 ---- c_compiler/test/out/04.stderr.txt | 0 c_compiler/test/out/04.stdout.xml | 12 ---- c_compiler/test/out/05.diff.txt | 0 c_compiler/test/out/05.pretty.xml | 10 --- c_compiler/test/out/05.stderr.txt | 0 c_compiler/test/out/05.stdout.xml | 11 ---- c_compiler/test/out/06.diff.txt | 0 c_compiler/test/out/06.pretty.xml | 14 ---- c_compiler/test/out/06.stderr.txt | 0 c_compiler/test/out/06.stdout.xml | 14 ---- c_compiler/test/out/07.diff.txt | 0 c_compiler/test/out/07.pretty.xml | 11 ---- c_compiler/test/out/07.stderr.txt | 0 c_compiler/test/out/07.stdout.xml | 11 ---- c_compiler/test/out/08.diff.txt | 0 c_compiler/test/out/08.pretty.xml | 16 ----- c_compiler/test/out/08.stderr.txt | 0 c_compiler/test/out/08.stdout.xml | 16 ----- c_compiler/test/out/09.diff.txt | 0 c_compiler/test/out/09.pretty.xml | 20 ------ c_compiler/test/out/09.stderr.txt | 0 c_compiler/test/out/09.stdout.xml | 21 ------ c_compiler/test/out/10.diff.txt | 0 c_compiler/test/out/10.pretty.xml | 19 ------ c_compiler/test/out/10.stderr.txt | 0 c_compiler/test/out/10.stdout.xml | 20 ------ c_compiler/test/out/11.diff.txt | 0 c_compiler/test/out/11.pretty.xml | 11 ---- c_compiler/test/out/11.stderr.txt | 0 c_compiler/test/out/11.stdout.xml | 11 ---- c_compiler/test/out/12.diff.txt | 0 c_compiler/test/out/12.pretty.xml | 10 --- c_compiler/test/out/12.stderr.txt | 0 c_compiler/test/out/12.stdout.xml | 10 --- c_compiler/test/out/13.diff.txt | 0 c_compiler/test/out/13.pretty.xml | 13 ---- c_compiler/test/out/13.stderr.txt | 0 c_compiler/test/out/13.stdout.xml | 13 ---- c_compiler/test/out/14.diff.txt | 0 c_compiler/test/out/14.pretty.xml | 13 ---- c_compiler/test/out/14.stderr.txt | 0 c_compiler/test/out/14.stdout.xml | 13 ---- c_compiler/test/out/15.diff.txt | 0 c_compiler/test/out/15.pretty.xml | 32 --------- c_compiler/test/out/15.stderr.txt | 0 c_compiler/test/out/15.stdout.xml | 32 --------- c_compiler/test/ref/01 | Bin 0 -> 2929088 bytes c_compiler/test/ref/01.s | 38 +++++++++++ c_compiler/test/ref/01.stdout.xml | 9 --- c_compiler/test/ref/02.stdout.xml | 7 -- c_compiler/test/ref/03.stdout.xml | 8 --- c_compiler/test/ref/04.stdout.xml | 12 ---- c_compiler/test/ref/05.stdout.xml | 11 ---- c_compiler/test/ref/06.stdout.xml | 14 ---- c_compiler/test/ref/07.stdout.xml | 11 ---- c_compiler/test/ref/08.stdout.xml | 16 ----- c_compiler/test/ref/09.stdout.xml | 21 ------ c_compiler/test/ref/10.stdout.xml | 20 ------ c_compiler/test/ref/11.stdout.xml | 11 ---- c_compiler/test/ref/12.stdout.xml | 10 --- c_compiler/test/ref/13.stdout.xml | 13 ---- c_compiler/test/ref/14.stdout.xml | 13 ---- c_compiler/test/ref/15.stdout.xml | 32 --------- makefile | 6 +- test_compiler.sh | 43 ++++++++++++ 106 files changed, 357 insertions(+), 938 deletions(-) create mode 100644 c_compiler/include/external.hpp delete mode 100644 c_compiler/test/in/02.c delete mode 100644 c_compiler/test/in/03.c delete mode 100644 c_compiler/test/in/04.c delete mode 100644 c_compiler/test/in/05.c delete mode 100644 c_compiler/test/in/06.c delete mode 100644 c_compiler/test/in/07.c delete mode 100644 c_compiler/test/in/08.c delete mode 100644 c_compiler/test/in/09.c delete mode 100644 c_compiler/test/in/10.c delete mode 100644 c_compiler/test/in/11.c delete mode 100644 c_compiler/test/in/12.c delete mode 100644 c_compiler/test/in/13.c delete mode 100644 c_compiler/test/in/14.c delete mode 100644 c_compiler/test/in/15.c create mode 100644 c_compiler/test/out/01.stdout.s delete mode 100644 c_compiler/test/out/01.stdout.txt delete mode 100644 c_compiler/test/out/02.diff.txt delete mode 100644 c_compiler/test/out/02.pretty.xml delete mode 100644 c_compiler/test/out/02.stderr.txt delete mode 100644 c_compiler/test/out/02.stdout.xml delete mode 100644 c_compiler/test/out/03.diff.txt delete mode 100644 c_compiler/test/out/03.pretty.xml delete mode 100644 c_compiler/test/out/03.stderr.txt delete mode 100644 c_compiler/test/out/03.stdout.xml delete mode 100644 c_compiler/test/out/04.diff.txt delete mode 100644 c_compiler/test/out/04.pretty.xml delete mode 100644 c_compiler/test/out/04.stderr.txt delete mode 100644 c_compiler/test/out/04.stdout.xml delete mode 100644 c_compiler/test/out/05.diff.txt delete mode 100644 c_compiler/test/out/05.pretty.xml delete mode 100644 c_compiler/test/out/05.stderr.txt delete mode 100644 c_compiler/test/out/05.stdout.xml delete mode 100644 c_compiler/test/out/06.diff.txt delete mode 100644 c_compiler/test/out/06.pretty.xml delete mode 100644 c_compiler/test/out/06.stderr.txt delete mode 100644 c_compiler/test/out/06.stdout.xml delete mode 100644 c_compiler/test/out/07.diff.txt delete mode 100644 c_compiler/test/out/07.pretty.xml delete mode 100644 c_compiler/test/out/07.stderr.txt delete mode 100644 c_compiler/test/out/07.stdout.xml delete mode 100644 c_compiler/test/out/08.diff.txt delete mode 100644 c_compiler/test/out/08.pretty.xml delete mode 100644 c_compiler/test/out/08.stderr.txt delete mode 100644 c_compiler/test/out/08.stdout.xml delete mode 100644 c_compiler/test/out/09.diff.txt delete mode 100644 c_compiler/test/out/09.pretty.xml delete mode 100644 c_compiler/test/out/09.stderr.txt delete mode 100644 c_compiler/test/out/09.stdout.xml delete mode 100644 c_compiler/test/out/10.diff.txt delete mode 100644 c_compiler/test/out/10.pretty.xml delete mode 100644 c_compiler/test/out/10.stderr.txt delete mode 100644 c_compiler/test/out/10.stdout.xml delete mode 100644 c_compiler/test/out/11.diff.txt delete mode 100644 c_compiler/test/out/11.pretty.xml delete mode 100644 c_compiler/test/out/11.stderr.txt delete mode 100644 c_compiler/test/out/11.stdout.xml delete mode 100644 c_compiler/test/out/12.diff.txt delete mode 100644 c_compiler/test/out/12.pretty.xml delete mode 100644 c_compiler/test/out/12.stderr.txt delete mode 100644 c_compiler/test/out/12.stdout.xml delete mode 100644 c_compiler/test/out/13.diff.txt delete mode 100644 c_compiler/test/out/13.pretty.xml delete mode 100644 c_compiler/test/out/13.stderr.txt delete mode 100644 c_compiler/test/out/13.stdout.xml delete mode 100644 c_compiler/test/out/14.diff.txt delete mode 100644 c_compiler/test/out/14.pretty.xml delete mode 100644 c_compiler/test/out/14.stderr.txt delete mode 100644 c_compiler/test/out/14.stdout.xml delete mode 100644 c_compiler/test/out/15.diff.txt delete mode 100644 c_compiler/test/out/15.pretty.xml delete mode 100644 c_compiler/test/out/15.stderr.txt delete mode 100644 c_compiler/test/out/15.stdout.xml create mode 100755 c_compiler/test/ref/01 create mode 100644 c_compiler/test/ref/01.s delete mode 100644 c_compiler/test/ref/01.stdout.xml delete mode 100644 c_compiler/test/ref/02.stdout.xml delete mode 100644 c_compiler/test/ref/03.stdout.xml delete mode 100644 c_compiler/test/ref/04.stdout.xml delete mode 100644 c_compiler/test/ref/05.stdout.xml delete mode 100644 c_compiler/test/ref/06.stdout.xml delete mode 100644 c_compiler/test/ref/07.stdout.xml delete mode 100644 c_compiler/test/ref/08.stdout.xml delete mode 100644 c_compiler/test/ref/09.stdout.xml delete mode 100644 c_compiler/test/ref/10.stdout.xml delete mode 100644 c_compiler/test/ref/11.stdout.xml delete mode 100644 c_compiler/test/ref/12.stdout.xml delete mode 100644 c_compiler/test/ref/13.stdout.xml delete mode 100644 c_compiler/test/ref/14.stdout.xml delete mode 100644 c_compiler/test/ref/15.stdout.xml create mode 100755 test_compiler.sh diff --git a/Notes.org b/Notes.org index 58cd47b..0b37cbb 100644 --- a/Notes.org +++ b/Notes.org @@ -74,3 +74,15 @@ ** TODO Coursework (c compiler) DEADLINE: <2017-03-28 Tue> +*** Task + +*** Mips Assembly + + - addiu :: Add Immediate Unsigned + - jr :: Jump Register + - li :: Pseudo instruction interpreted as ( lui $rd, LabelAddr[31:16] + ori $rd,$rd, LabelAddr[15:0] ) + - lw :: Load Word + - move :: Pseudo instruction interpreted as (add $rt, $rs, $zero) + - nop :: No Operation + - sw :: Store Word diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp index 9d51816..bb3e92f 100644 --- a/c_compiler/include/ast.hpp +++ b/c_compiler/include/ast.hpp @@ -1,14 +1,19 @@ #ifndef AST_HPP #define AST_HPP +#include +#include +#include + #include "base.hpp" #include "statement.hpp" #include "function.hpp" #include "declaration.hpp" #include "expression.hpp" #include "primitives.hpp" +#include "external.hpp" #include "ast_top.hpp" -ast_Top *parseAST(); +const BaseList* parseAST(); #endif diff --git a/c_compiler/include/base.hpp b/c_compiler/include/base.hpp index 9451609..ae7cfd7 100644 --- a/c_compiler/include/base.hpp +++ b/c_compiler/include/base.hpp @@ -1,16 +1,138 @@ #ifndef AST_BASE_HPP #define AST_BASE_HPP -#include -#include -#include +#include "ast.hpp" + class Base { public: virtual ~Base() {} virtual void print() const = 0; - virtual void push(const Base* _var) const = 0; + virtual void printxml() const = 0; + virtual void printasm() const = 0; +}; + + +class EmptyNode : public Base { +public: + EmptyNode() {} + + virtual void print() const {} + virtual void printxml() const {} + virtual void printasm() const {} +}; + + +class BaseList : public Base { +protected: + mutable std::vector list; + +public: + BaseList() {} + BaseList(const Base* _var) { + push(_var); + } + + virtual ~BaseList() { + for(auto& var : list) { + delete var; + } + } + + virtual void print() const { + for(auto&& declaration : list) { + declaration->print(); + } + } + + virtual void printxml() const { + for(auto&& declaration : list) { + declaration->printxml(); + } + } + + virtual void printasm() const { + for(auto&& declaration : list) { + declaration->printasm(); + } + } + + virtual void push(const Base* _var) const { + list.push_back(_var); + } +}; + + +class BaseNode : public Base { +protected: + const Base* leftNode; + const Base* rightNode; + +public: + BaseNode(const Base* _left = new EmptyNode, const Base* _right = new EmptyNode) + : leftNode(_left), rightNode(_right) {} + + virtual ~BaseNode() { + delete leftNode; + delete rightNode; + } + + virtual void print() const { + leftNode->print(); + rightNode->print(); + } + + virtual void printxml() const { + leftNode->printxml(); + rightNode->printxml(); + } + + virtual void printasm() const { + leftNode->printasm(); + rightNode->printasm(); + } + + virtual const Base* getLeft() const { + return leftNode; + } + + virtual const Base* getRight() const { + return rightNode; + } +}; + + +class BasePrimitive : public Base { +protected: + std::string id; + const Base* type; + +public: + BasePrimitive(const std::string& _id) + : id(_id), type(new EmptyNode) {} + + BasePrimitive(const std::string& _id, const Base* _type) + : id(_id), type(_type) {} + + virtual ~BasePrimitive() { + delete type; + } + + virtual void print() const {} + virtual void printxml() const {} + virtual void printasm() const {} +}; + + +class BaseType : public Base { +public: + BaseType() {} + + virtual void print() const {} + virtual void printxml() const {} + virtual void printasm() const {} }; + #endif diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp index bfd3070..bf72f2d 100644 --- a/c_compiler/include/declaration.hpp +++ b/c_compiler/include/declaration.hpp @@ -3,48 +3,21 @@ #include "ast.hpp" -#include - // Declaration that holds a list of declarations -class DeclarationList : public Base { -private: - mutable std::vector dec_list; - +class Declaration : public BaseNode { public: - DeclarationList(const Base* _dec) { - dec_list.push_back(_dec); - } - - virtual void print() const { - for(size_t i = 0; i < dec_list.size(); ++i) { - dec_list[i]->print(); - } - } - - virtual void push(const Base* _dec) const { - dec_list.push_back(_dec); - } + Declaration(const Base* _var) : BaseNode(_var) {} }; -class VariableDeclaration : public Base { -private: - mutable std::vector var_list; - +class DeclarationList : public BaseList { public: - VariableDeclaration(const Base* _var) { - var_list.push_back(_var); - } - - virtual void print() const { - for(size_t i = 0; i < var_list.size(); ++i) { - var_list[i]->print(); - } - } + DeclarationList(const Base* _var) : BaseList(_var) {} +}; - virtual void push(const Base* _var) const { - var_list.push_back(_var); - } +class InitDeclaratorList : public BaseList { +public: + InitDeclaratorList(const Base* _var) : BaseList(_var) {} }; #endif diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index f67d2d7..9eb9efd 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -3,22 +3,14 @@ #include "ast.hpp" -#include -#include - -class Expression : public Base { +class Expression : public BaseNode { private: public: - Expression() {} - - virtual void print() const { - - } + Expression() : BaseNode() {} - virtual void push(const Base* _base) const { - std::cerr << "Can't call this function for this type" << std::endl; - (void)_base; - } + virtual void print() const override {} + virtual void printxml() const override {} + virtual void printasm() const override {} }; #endif diff --git a/c_compiler/include/external.hpp b/c_compiler/include/external.hpp new file mode 100644 index 0000000..866e18a --- /dev/null +++ b/c_compiler/include/external.hpp @@ -0,0 +1,13 @@ +#ifndef EXTERNAL_HPP +#define EXTERNAL_HPP + +#include "ast.hpp" + + +class ExternalDefinition : public BaseList { +public: + ExternalDefinition(const Base* _var) : BaseList(_var) {} +}; + + +#endif diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp index 6fbcdee..955420d 100644 --- a/c_compiler/include/function.hpp +++ b/c_compiler/include/function.hpp @@ -3,51 +3,29 @@ #include "ast.hpp" -#include -#include -class Function : public Base { -private: +class Function : public BaseNode { +protected: std::string id; - const Base* param; - const Base* comp_statement; + public: - Function(const std::string& _id, const Base* _param, const Base* _comp_statement) : - id(_id), param(_param), comp_statement(_comp_statement) {} + Function(const std::string& _id, const BaseList* _param_list, const BaseNode* _comp_statement) + : BaseNode(_param_list, _comp_statement), id(_id) {} - virtual void print() const { + virtual void printxml() const override { std::cout << "" << std::endl; - param->print(); - comp_statement->print(); + leftNode->printxml(); + rightNode->printxml(); std::cout << "" << std::endl; } - - virtual void push(const Base* var) const { - std::cerr << "Error: Can't call this function on this class" << std::endl; - (void)var; - } }; -class ParamList : public Base { -private: - mutable std::vector param_list; +class ParamList : public BaseList { public: - ParamList() {} - - ParamList(const Base* param) { - param_list.push_back(param); - } - - virtual void print() const { - for(size_t i = 0; i < param_list.size(); ++i) { - param_list[i]->print(); - } - } - - virtual void push(const Base* _var) const { - param_list.push_back(_var); - } + ParamList() : BaseList() {} + ParamList(const Base* _param) : BaseList(_param) {} }; + #endif diff --git a/c_compiler/include/primitives.hpp b/c_compiler/include/primitives.hpp index 2eeaa19..f4c5087 100644 --- a/c_compiler/include/primitives.hpp +++ b/c_compiler/include/primitives.hpp @@ -3,38 +3,25 @@ #include "ast.hpp" -#include -class Variable : public Base { -private: - std::string id; +class Declarator : public BasePrimitive { public: - Variable(const std::string& _id) : id(_id) {} + Declarator(const std::string& _id) : BasePrimitive(_id) {} - virtual void print() const { - std::cout << "" << std::endl; - } - - virtual void push(const Base* var) const { - std::cerr << "Error: Can't call this function on this class" << std::endl; - (void)var; + virtual void printxml() const { + std::cout << "" << std::endl; } }; -class Parameter : public Base { -private: - std::string id; + +class Parameter : public BasePrimitive { public: - Parameter(const std::string& _id) : id(_id) {} + Parameter(const std::string& _id) : BasePrimitive(_id) {} - virtual void print() const { + virtual void printxml() const { std::cout << "" << std::endl; } - - virtual void push(const Base* var) const { - std::cerr << "Error: Can't call this function on this class" << std::endl; - (void)var; - } }; + #endif diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp index 4761efb..451d368 100644 --- a/c_compiler/include/statement.hpp +++ b/c_compiler/include/statement.hpp @@ -3,49 +3,33 @@ #include "ast.hpp" -class Statement : public Base { -protected: - mutable std::vector list; - + +class Statement : public BaseNode { public: - Statement() {} + Statement() : BaseNode() {} - Statement(const Base* _el) { - list.push_back(_el); - } - - Statement(const Base* _dec, const Base* _statement) { - list.push_back(_dec); - list.push_back(_statement); - } - virtual void print() const { - for(size_t i = 0; i < list.size(); ++i) { - list[i]->print(); - } - } - - virtual void push(const Base* _var) const { - list.push_back(_var); - } + Statement(const Base* _el) : BaseNode(_el) {} }; -class StatementList : public Statement { +class StatementList : public BaseList { public: - StatementList(const Base* _statement) : Statement(_statement) {} + StatementList(const Base* _statement) : BaseList(_statement) {} }; class CompoundStatement : public Statement { public: CompoundStatement() : Statement() {} CompoundStatement(const Base* _el) : Statement(_el) {} - CompoundStatement(const Base* _dec, const Base* _statement) : - Statement(_dec, _statement) {} + + CompoundStatement(const Base* _dec, const Base* _statement) { + leftNode = _dec; + rightNode = _statement; + } - virtual void print() const override { + virtual void printxml() const override { std::cout << "" << std::endl; - for(size_t i = 0; i < list.size(); ++i) { - list[i]->print(); - } + leftNode->printxml(); + rightNode->printxml(); std::cout << "" << std::endl; } }; @@ -54,14 +38,17 @@ class SelectionStatement : public Statement { public: SelectionStatement() : Statement() {} SelectionStatement(const Base* _el) : Statement(_el) {} - SelectionStatement(const Base* _if, const Base* _else) : - Statement(_if, _else) {} + + SelectionStatement(const Base* _if, const Base* _else) { + leftNode = _if; + rightNode = _else; + } }; class ExpressionStatement : public Statement { public: ExpressionStatement() : Statement() {} - ExpressionStatement(const Base* _el) : Statement(_el) {} + ExpressionStatement(const Base* expr) : Statement(expr) {} }; class JumpStatement : public Statement { @@ -74,8 +61,6 @@ class IterationStatement : public Statement { public: IterationStatement() : Statement() {} IterationStatement(const Base* _el) : Statement(_el) {} - IterationStatement(const Base* _if, const Base* _else) : - Statement(_if, _else) {} }; #endif diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 1fc4d4a..2f9f1b8 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -1,7 +1,8 @@ %code requires{ #include "ast.hpp" -extern ast_Top *g_root; // A way of getting the AST out + +extern const BaseList* g_root; // A way of getting the AST out //! This is to fix problems when generating C++ // We are declaring the functions provided by Flex, so @@ -14,48 +15,57 @@ void yyerror(const char *); // Represents the value associated with any kind of // AST node. %union{ - const Base *stmnt; + const BaseNode* base_node; + const BaseList* base_list; + const BasePrimitive* base_prim; + const BaseType* base_type; double number; std::string *string; } -%token T_TYPE_SPEC T_TYPE_QUAL T_STRG_SPEC T_IDENTIFIER -%token T_SC T_CMA T_LRB T_LCB T_RCB T_LSB T_RSB T_QU T_COL T_LOG_OR T_LOG_AND T_OR T_XOR T_AND T_EQUALITY_OP T_REL_OP T_SHIFT_OP T_MULT T_DIV T_REM T_TILDE T_NOT T_DOT T_ARROW T_INCDEC T_ADDSUB_OP T_ASSIGN_OPER T_EQ T_SIZEOF -%token T_INT_CONST -%token T_IF T_WHILE T_DO T_FOR T_RETURN -%nonassoc T_RRB -%nonassoc T_ELSE +%token T_TYPE_SPEC T_TYPE_QUAL T_STRG_SPEC T_IDENTIFIER T_SC T_CMA T_LRB T_LCB T_RCB + T_LSB T_RSB T_QU T_COL T_LOG_OR T_LOG_AND T_OR T_XOR T_AND T_EQUALITY_OP + T_REL_OP T_SHIFT_OP T_MULT T_DIV T_REM T_TILDE T_NOT T_DOT T_ARROW T_INCDEC + T_ADDSUB_OP T_ASSIGN_OPER T_EQ T_SIZEOF T_INT_CONST T_IF T_WHILE T_DO T_FOR + T_RETURN - -%type ExtDef ExtDeclaration - -%type FuncDef ParameterList Parameter ParamDeclarator - -%type DeclarationList Declaration DeclarationSpec DeclarationSpec_T InitDeclarator InitDeclaratorList Declarator +%nonassoc T_RRB +%nonassoc T_ELSE -%type StatementList Statement CompoundStatement CompoundStatement_2 SelectionStatement ExpressionStatement JumpStatement IterationStatement + +%type ExtDef ParameterList DeclarationList InitDeclaratorList IdentifierList + StatementList ArgumentExpressionList + +%type ExtDeclaration FuncDef Declaration DeclarationSpec DeclarationSpec_T + InitDeclarator Statement CompoundStatement CompoundStatement_2 + SelectionStatement ExpressionStatement JumpStatement IterationStatement + Expression AssignmentExpression ConditionalExpression LogicalOrExpression + LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression + AndExpression EqualityExpression RelationalExpression ShiftExpression + AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression + PostfixExpression PostfixExpression2 PrimaryExpression -%type Expression AssignmentExpression ConditionalExpression LogicalOrExpression LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression AndExpression EqualityExpression RelationalExpression ShiftExpression AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression - +%type Parameter ParamDeclarator Declarator DirectDeclarator -%type Constant T_INT_CONST +%type T_INT_CONST Constant -%type T_IDENTIFIER MultDivRemOP UnaryOperator ASSIGN_OPER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT T_MULT T_DIV T_REM //T_Operator +%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 MultDivRemOP UnaryOperator %start ROOT %% ROOT: - ExtDef { ; } + ExtDef { g_root = $1; } ; // EXTERNAL DEFINITION ExtDef: - ExtDeclaration { g_root->push($1); } - | ExtDef ExtDeclaration { g_root->push($2); } + ExtDeclaration { $$ = new ExternalDefinition($1); } + | ExtDef ExtDeclaration { $$->push($2); } ; ExtDeclaration: @@ -91,7 +101,7 @@ DeclarationList: ; Declaration: - DeclarationSpec InitDeclaratorList T_SC { $$ = $2; } + DeclarationSpec InitDeclaratorList T_SC { $$ = new Declaration($2); } ; DeclarationSpec: @@ -106,17 +116,32 @@ DeclarationSpec_T: ; InitDeclaratorList: - InitDeclarator { $$ = new VariableDeclaration($1); } + InitDeclarator { $$ = new InitDeclaratorList($1); } | InitDeclaratorList T_CMA InitDeclarator { $$->push($3); } ; InitDeclarator: - Declarator { ; } - | Declarator T_EQ AssignmentExpression { ; } + Declarator { $$ = $1; } + | Declarator T_EQ AssignmentExpression { $$ = $1; } ; Declarator: - T_IDENTIFIER {$$ = new Variable(*$1); } + DirectDeclarator { $$ = $1; } + | T_MULT DirectDeclarator { $$ = $2; } + ; + +DirectDeclarator: + T_IDENTIFIER { $$ = new Declarator(*$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 IdentifierList T_RRB { $$ = $1; } + ; + +IdentifierList: + T_IDENTIFIER { $$ = new Declarator(*$1); } + | IdentifierList T_CMA T_IDENTIFIER { $$ = new Declarator(*$3); } ; // Statement @@ -147,7 +172,7 @@ CompoundStatement_2: SelectionStatement: T_IF T_LRB Expression T_RRB Statement { $$ = new SelectionStatement($5); } -| T_IF T_LRB Expression T_RRB Statement T_ELSE Statement { $$ = new SelectionStatement($5, $7); } + | T_IF T_LRB Expression T_RRB Statement T_ELSE Statement { $$ = new SelectionStatement($5, $7); } ; ExpressionStatement: @@ -294,10 +319,10 @@ Constant: %% -ast_Top *g_root; // Definition of variable (to match declaration earlier) +const BaseList* g_root; // Definition of variable (to match declaration earlier) -ast_Top *parseAST() { - g_root = new ast_Top; +const BaseList* parseAST() { + g_root = NULL; yyparse(); return g_root; } diff --git a/c_compiler/src/parser_main.cpp b/c_compiler/src/parser_main.cpp index 02dcfeb..8922417 100644 --- a/c_compiler/src/parser_main.cpp +++ b/c_compiler/src/parser_main.cpp @@ -3,7 +3,7 @@ #include int main(int argc, char *argv[]) { - ast_Top *ast = parseAST(); + const Base *ast = parseAST(); ast->print(); diff --git a/c_compiler/test/in/01.c b/c_compiler/test/in/01.c index 76425b5..c362948 100644 --- a/c_compiler/test/in/01.c +++ b/c_compiler/test/in/01.c @@ -1,6 +1,3 @@ -int a; -int b = 0; -int c, d; - -int e, - f; +int main() { + return 13; +} diff --git a/c_compiler/test/in/02.c b/c_compiler/test/in/02.c deleted file mode 100644 index 45b1467..0000000 --- a/c_compiler/test/in/02.c +++ /dev/null @@ -1 +0,0 @@ -int f() {} diff --git a/c_compiler/test/in/03.c b/c_compiler/test/in/03.c deleted file mode 100644 index e57aaa9..0000000 --- a/c_compiler/test/in/03.c +++ /dev/null @@ -1 +0,0 @@ -int foo(int bar) {} diff --git a/c_compiler/test/in/04.c b/c_compiler/test/in/04.c deleted file mode 100644 index 2e3aa5d..0000000 --- a/c_compiler/test/in/04.c +++ /dev/null @@ -1,4 +0,0 @@ -int foo(int bar1, int bar2) { - int x; - int y, z; -} diff --git a/c_compiler/test/in/05.c b/c_compiler/test/in/05.c deleted file mode 100644 index 2317163..0000000 --- a/c_compiler/test/in/05.c +++ /dev/null @@ -1,3 +0,0 @@ -int foo(int x, int y) { - {} -} diff --git a/c_compiler/test/in/06.c b/c_compiler/test/in/06.c deleted file mode 100644 index 2dcdc2b..0000000 --- a/c_compiler/test/in/06.c +++ /dev/null @@ -1,6 +0,0 @@ -int f(int a, int b, int c) { - int d; - { - int e; - } -} diff --git a/c_compiler/test/in/07.c b/c_compiler/test/in/07.c deleted file mode 100644 index 80d8051..0000000 --- a/c_compiler/test/in/07.c +++ /dev/null @@ -1,5 +0,0 @@ -int foo(int a) { - if(x == y) { - int z; - } -} diff --git a/c_compiler/test/in/08.c b/c_compiler/test/in/08.c deleted file mode 100644 index c57a6ab..0000000 --- a/c_compiler/test/in/08.c +++ /dev/null @@ -1,10 +0,0 @@ -int func(int a, int b) { - int c = 0; - if(a == b) { - int d; - } else if(a == d) { - int e; - return d; - } - return c; -} diff --git a/c_compiler/test/in/09.c b/c_compiler/test/in/09.c deleted file mode 100644 index ae86bb1..0000000 --- a/c_compiler/test/in/09.c +++ /dev/null @@ -1,15 +0,0 @@ -int x() -{} - -int g; - -int zz(int a, int b, int c) -{ - if(a==b){ - int a; - return a; - }else{ - int fsdfsdfs; - return c; - } -} diff --git a/c_compiler/test/in/10.c b/c_compiler/test/in/10.c deleted file mode 100644 index 37a6ecc..0000000 --- a/c_compiler/test/in/10.c +++ /dev/null @@ -1,18 +0,0 @@ -int f() -{} - -int g = 2; - -int x(int y) { - int z = 3; - - if(y < z || g < z) { - int r; - ++y; - } else if(y == z) { - int f; - --y; - } else return y; - - return g; -} diff --git a/c_compiler/test/in/11.c b/c_compiler/test/in/11.c deleted file mode 100644 index 2d8cac6..0000000 --- a/c_compiler/test/in/11.c +++ /dev/null @@ -1,10 +0,0 @@ -int f() { - int x = 0; - - while(x < 5) { - int y = 0; - x++; - } - - return x; -} diff --git a/c_compiler/test/in/12.c b/c_compiler/test/in/12.c deleted file mode 100644 index 37b8b7e..0000000 --- a/c_compiler/test/in/12.c +++ /dev/null @@ -1,4 +0,0 @@ -int f(int b, int c) { - int a = b+c; - return a; -} diff --git a/c_compiler/test/in/13.c b/c_compiler/test/in/13.c deleted file mode 100644 index d912d70..0000000 --- a/c_compiler/test/in/13.c +++ /dev/null @@ -1,8 +0,0 @@ -int func(int x, int y) { - int i; - for(i = 0; i < 5; ++i) { - int z = 0; - z = x + y + i; - return x + y; - } -} diff --git a/c_compiler/test/in/14.c b/c_compiler/test/in/14.c deleted file mode 100644 index 94506d7..0000000 --- a/c_compiler/test/in/14.c +++ /dev/null @@ -1,12 +0,0 @@ -int function_1(int a, int b) { - int x = 0; - - do { - int c = a + b; - x += a; - x = x * b; - x -= c; - } while(x < 500); - - return x; -} diff --git a/c_compiler/test/in/15.c b/c_compiler/test/in/15.c deleted file mode 100644 index 3189b56..0000000 --- a/c_compiler/test/in/15.c +++ /dev/null @@ -1,28 +0,0 @@ -int add(int a, int b) -{ - int c = a + b; - return c; -} - -int sub(int a, int b) -{ - int c = a - b; - return c; -} - -int mult_by_5(int a) -{ - int b = 5; - return a * b; -} - -int add_5(int a) -{ - int i; - for(i = 0; i < 5; ++i) - { - int b = ++a; - } - - return a; -} diff --git a/c_compiler/test/out/01.pretty.xml b/c_compiler/test/out/01.pretty.xml index e0545e4..8b13789 100644 --- a/c_compiler/test/out/01.pretty.xml +++ b/c_compiler/test/out/01.pretty.xml @@ -1,9 +1 @@ - - - - - - - - - + diff --git a/c_compiler/test/out/01.stderr.txt b/c_compiler/test/out/01.stderr.txt index e69de29..37006dc 100644 --- a/c_compiler/test/out/01.stderr.txt +++ b/c_compiler/test/out/01.stderr.txt @@ -0,0 +1 @@ +./test_compiler.sh: line 28: ./bin/c_compiler: No such file or directory diff --git a/c_compiler/test/out/01.stdout.s b/c_compiler/test/out/01.stdout.s new file mode 100644 index 0000000..e69de29 diff --git a/c_compiler/test/out/01.stdout.txt b/c_compiler/test/out/01.stdout.txt deleted file mode 100644 index bd7cd1f..0000000 --- a/c_compiler/test/out/01.stdout.txt +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/c_compiler/test/out/01.stdout.xml b/c_compiler/test/out/01.stdout.xml index bd7cd1f..e69de29 100644 --- a/c_compiler/test/out/01.stdout.xml +++ b/c_compiler/test/out/01.stdout.xml @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/c_compiler/test/out/02.diff.txt b/c_compiler/test/out/02.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/02.pretty.xml b/c_compiler/test/out/02.pretty.xml deleted file mode 100644 index 1f73bca..0000000 --- a/c_compiler/test/out/02.pretty.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/c_compiler/test/out/02.stderr.txt b/c_compiler/test/out/02.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/02.stdout.xml b/c_compiler/test/out/02.stdout.xml deleted file mode 100644 index bc37d7a..0000000 --- a/c_compiler/test/out/02.stdout.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/c_compiler/test/out/03.diff.txt b/c_compiler/test/out/03.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/03.pretty.xml b/c_compiler/test/out/03.pretty.xml deleted file mode 100644 index 6ab6ab3..0000000 --- a/c_compiler/test/out/03.pretty.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/c_compiler/test/out/03.stderr.txt b/c_compiler/test/out/03.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/03.stdout.xml b/c_compiler/test/out/03.stdout.xml deleted file mode 100644 index 6ca0ab2..0000000 --- a/c_compiler/test/out/03.stdout.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/c_compiler/test/out/04.diff.txt b/c_compiler/test/out/04.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/04.pretty.xml b/c_compiler/test/out/04.pretty.xml deleted file mode 100644 index caae53b..0000000 --- a/c_compiler/test/out/04.pretty.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/c_compiler/test/out/04.stderr.txt b/c_compiler/test/out/04.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/04.stdout.xml b/c_compiler/test/out/04.stdout.xml deleted file mode 100644 index 9257eac..0000000 --- a/c_compiler/test/out/04.stdout.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/c_compiler/test/out/05.diff.txt b/c_compiler/test/out/05.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/05.pretty.xml b/c_compiler/test/out/05.pretty.xml deleted file mode 100644 index 944d950..0000000 --- a/c_compiler/test/out/05.pretty.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/c_compiler/test/out/05.stderr.txt b/c_compiler/test/out/05.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/05.stdout.xml b/c_compiler/test/out/05.stdout.xml deleted file mode 100644 index 7381e51..0000000 --- a/c_compiler/test/out/05.stdout.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/c_compiler/test/out/06.diff.txt b/c_compiler/test/out/06.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/06.pretty.xml b/c_compiler/test/out/06.pretty.xml deleted file mode 100644 index 6422001..0000000 --- a/c_compiler/test/out/06.pretty.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/06.stderr.txt b/c_compiler/test/out/06.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/06.stdout.xml b/c_compiler/test/out/06.stdout.xml deleted file mode 100644 index fbcbebd..0000000 --- a/c_compiler/test/out/06.stdout.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/07.diff.txt b/c_compiler/test/out/07.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/07.pretty.xml b/c_compiler/test/out/07.pretty.xml deleted file mode 100644 index 11c3b77..0000000 --- a/c_compiler/test/out/07.pretty.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/c_compiler/test/out/07.stderr.txt b/c_compiler/test/out/07.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/07.stdout.xml b/c_compiler/test/out/07.stdout.xml deleted file mode 100644 index ff0efb2..0000000 --- a/c_compiler/test/out/07.stdout.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/c_compiler/test/out/08.diff.txt b/c_compiler/test/out/08.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/08.pretty.xml b/c_compiler/test/out/08.pretty.xml deleted file mode 100644 index 6cf0f8e..0000000 --- a/c_compiler/test/out/08.pretty.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/08.stderr.txt b/c_compiler/test/out/08.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/08.stdout.xml b/c_compiler/test/out/08.stdout.xml deleted file mode 100644 index 7929e75..0000000 --- a/c_compiler/test/out/08.stdout.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/09.diff.txt b/c_compiler/test/out/09.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/09.pretty.xml b/c_compiler/test/out/09.pretty.xml deleted file mode 100644 index 6c137df..0000000 --- a/c_compiler/test/out/09.pretty.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/09.stderr.txt b/c_compiler/test/out/09.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/09.stdout.xml b/c_compiler/test/out/09.stdout.xml deleted file mode 100644 index 7a7a770..0000000 --- a/c_compiler/test/out/09.stdout.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/10.diff.txt b/c_compiler/test/out/10.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/10.pretty.xml b/c_compiler/test/out/10.pretty.xml deleted file mode 100644 index 89a6292..0000000 --- a/c_compiler/test/out/10.pretty.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/10.stderr.txt b/c_compiler/test/out/10.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/10.stdout.xml b/c_compiler/test/out/10.stdout.xml deleted file mode 100644 index b07997e..0000000 --- a/c_compiler/test/out/10.stdout.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/11.diff.txt b/c_compiler/test/out/11.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/11.pretty.xml b/c_compiler/test/out/11.pretty.xml deleted file mode 100644 index b9779d7..0000000 --- a/c_compiler/test/out/11.pretty.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/c_compiler/test/out/11.stderr.txt b/c_compiler/test/out/11.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/11.stdout.xml b/c_compiler/test/out/11.stdout.xml deleted file mode 100644 index 0822742..0000000 --- a/c_compiler/test/out/11.stdout.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/c_compiler/test/out/12.diff.txt b/c_compiler/test/out/12.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/12.pretty.xml b/c_compiler/test/out/12.pretty.xml deleted file mode 100644 index 9f49265..0000000 --- a/c_compiler/test/out/12.pretty.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/c_compiler/test/out/12.stderr.txt b/c_compiler/test/out/12.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/12.stdout.xml b/c_compiler/test/out/12.stdout.xml deleted file mode 100644 index 471f225..0000000 --- a/c_compiler/test/out/12.stdout.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/c_compiler/test/out/13.diff.txt b/c_compiler/test/out/13.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/13.pretty.xml b/c_compiler/test/out/13.pretty.xml deleted file mode 100644 index a604ecd..0000000 --- a/c_compiler/test/out/13.pretty.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/c_compiler/test/out/13.stderr.txt b/c_compiler/test/out/13.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/13.stdout.xml b/c_compiler/test/out/13.stdout.xml deleted file mode 100644 index 511d93b..0000000 --- a/c_compiler/test/out/13.stdout.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/c_compiler/test/out/14.diff.txt b/c_compiler/test/out/14.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/14.pretty.xml b/c_compiler/test/out/14.pretty.xml deleted file mode 100644 index 866d44e..0000000 --- a/c_compiler/test/out/14.pretty.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/c_compiler/test/out/14.stderr.txt b/c_compiler/test/out/14.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/14.stdout.xml b/c_compiler/test/out/14.stdout.xml deleted file mode 100644 index 1b412d3..0000000 --- a/c_compiler/test/out/14.stdout.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/c_compiler/test/out/15.diff.txt b/c_compiler/test/out/15.diff.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/15.pretty.xml b/c_compiler/test/out/15.pretty.xml deleted file mode 100644 index b06dde0..0000000 --- a/c_compiler/test/out/15.pretty.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/out/15.stderr.txt b/c_compiler/test/out/15.stderr.txt deleted file mode 100644 index e69de29..0000000 diff --git a/c_compiler/test/out/15.stdout.xml b/c_compiler/test/out/15.stdout.xml deleted file mode 100644 index d4c29b2..0000000 --- a/c_compiler/test/out/15.stdout.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/ref/01 b/c_compiler/test/ref/01 new file mode 100755 index 0000000..9655601 Binary files /dev/null and b/c_compiler/test/ref/01 differ diff --git a/c_compiler/test/ref/01.s b/c_compiler/test/ref/01.s new file mode 100644 index 0000000..db5d0b4 --- /dev/null +++ b/c_compiler/test/ref/01.s @@ -0,0 +1,38 @@ + .file 1 "01.c" + .section .mdebug.abi32 + .previous + .nan legacy + .module fp=xx + .module nooddspreg + .abicalls + .option pic0 + .text + .align 2 + .globl main + .set nomips16 + .set nomicromips + .ent main + .type main, @function +main: + .frame $fp,24,$31 # vars= 8, regs= 1/0, args= 0, gp= 8 + .mask 0x40000000,-4 + .fmask 0x00000000,0 + .set noreorder + .set nomacro + addiu $sp,$sp,-24 + sw $fp,20($sp) + move $fp,$sp + li $2,13 # 0xd + sw $2,8($fp) + lw $2,8($fp) + move $sp,$fp + lw $fp,20($sp) + addiu $sp,$sp,24 + jr $31 + nop + + .set macro + .set reorder + .end main + .size main, .-main + .ident "GCC: (Codescape GNU Tools 2016.05-03 for MIPS MTI Linux) 4.9.2" diff --git a/c_compiler/test/ref/01.stdout.xml b/c_compiler/test/ref/01.stdout.xml deleted file mode 100644 index e0545e4..0000000 --- a/c_compiler/test/ref/01.stdout.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/c_compiler/test/ref/02.stdout.xml b/c_compiler/test/ref/02.stdout.xml deleted file mode 100644 index 6caf2fc..0000000 --- a/c_compiler/test/ref/02.stdout.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/c_compiler/test/ref/03.stdout.xml b/c_compiler/test/ref/03.stdout.xml deleted file mode 100644 index 2c29a50..0000000 --- a/c_compiler/test/ref/03.stdout.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/c_compiler/test/ref/04.stdout.xml b/c_compiler/test/ref/04.stdout.xml deleted file mode 100644 index caae53b..0000000 --- a/c_compiler/test/ref/04.stdout.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/c_compiler/test/ref/05.stdout.xml b/c_compiler/test/ref/05.stdout.xml deleted file mode 100644 index f4b306f..0000000 --- a/c_compiler/test/ref/05.stdout.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/c_compiler/test/ref/06.stdout.xml b/c_compiler/test/ref/06.stdout.xml deleted file mode 100644 index 3526139..0000000 --- a/c_compiler/test/ref/06.stdout.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - diff --git a/c_compiler/test/ref/07.stdout.xml b/c_compiler/test/ref/07.stdout.xml deleted file mode 100644 index 17c7c1b..0000000 --- a/c_compiler/test/ref/07.stdout.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/c_compiler/test/ref/08.stdout.xml b/c_compiler/test/ref/08.stdout.xml deleted file mode 100644 index 0838fe5..0000000 --- a/c_compiler/test/ref/08.stdout.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/ref/09.stdout.xml b/c_compiler/test/ref/09.stdout.xml deleted file mode 100644 index 646cd98..0000000 --- a/c_compiler/test/ref/09.stdout.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/ref/10.stdout.xml b/c_compiler/test/ref/10.stdout.xml deleted file mode 100644 index 21cadeb..0000000 --- a/c_compiler/test/ref/10.stdout.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/c_compiler/test/ref/11.stdout.xml b/c_compiler/test/ref/11.stdout.xml deleted file mode 100644 index ad96d6c..0000000 --- a/c_compiler/test/ref/11.stdout.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/c_compiler/test/ref/12.stdout.xml b/c_compiler/test/ref/12.stdout.xml deleted file mode 100644 index 9f49265..0000000 --- a/c_compiler/test/ref/12.stdout.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/c_compiler/test/ref/13.stdout.xml b/c_compiler/test/ref/13.stdout.xml deleted file mode 100644 index f0385cb..0000000 --- a/c_compiler/test/ref/13.stdout.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/c_compiler/test/ref/14.stdout.xml b/c_compiler/test/ref/14.stdout.xml deleted file mode 100644 index 63aec85..0000000 --- a/c_compiler/test/ref/14.stdout.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - diff --git a/c_compiler/test/ref/15.stdout.xml b/c_compiler/test/ref/15.stdout.xml deleted file mode 100644 index 029ccb8..0000000 --- a/c_compiler/test/ref/15.stdout.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/makefile b/makefile index 9cef91e..cf2b7e1 100644 --- a/makefile +++ b/makefile @@ -21,7 +21,7 @@ all : bin/c_lexer bin/c_parser bin/c_lexer : $(LEXBUILDDIR)/main.o $(LEXBUILDDIR)/c_lexer.o $(LEXBUILDDIR)/c_lexer.yy.o @echo "Linking..." @echo " mkdir -p bin"; mkdir -p bin - @echo " $(CC) $^ -o bin/c_lexer"; $(CC) $^ -o bin/c_lexer + @echo " $(CC) $^ -o $@"; $(CC) $^ -o $@ $(LEXBUILDDIR)/%.o : $(LEXSRCDIR)/%.cpp @echo "Building sources..." @@ -41,7 +41,7 @@ $(LEXSRCDIR)/c_lexer.yy.cpp : $(LEXSRCDIR)/c_lexer.flex bin/c_parser : $(PARBUILDDIR)/parser_main.o $(PARBUILDDIR)/c_parser.tab.o $(PARBUILDDIR)/c_lexer.yy.o @echo "Linking..." @echo " mkdir -p bin"; mkdir -p bin - @echo " $(CC) $^ -o bin/c_parser"; $(CC) $^ -o bin/c_parser + @echo " $(CC) $^ -o $@"; $(CC) $^ -o $@ $(PARBUILDDIR)/%.o : $(PARSRCDIR)/%.cpp @echo "Building sources..." @@ -70,7 +70,7 @@ $(PARSRCDIR)/c_parser.tab.cpp $(PARSRCDIR)/c_parser.tab.hpp : $(PARSRCDIR)/c_par bin/c_compiler : $(COMPBUILDDIR)/parser_main.o $(COMPBUILDDIR)/c_parser.tab.o $(COMPBUILDDIR)/c_lexer.yy.o @echo "Linking..." @echo " mkdir -p bin"; mkdir -p bin - @echo " $(CC) $^ -o bin/c_parser"; $(CC) $^ -o bin/c_parser + @echo " $(CC) $^ -o $@"; $(CC) $^ -o $@ $(COMPBUILDDIR)/%.o : $(COMPSRCDIR)/%.cpp @echo "Building sources..." diff --git a/test_compiler.sh b/test_compiler.sh new file mode 100755 index 0000000..3f64457 --- /dev/null +++ b/test_compiler.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# script to test the lexer + +echo "========================================" +echo " Cleaning the temporaries and outputs" +make clean +echo " Force building lexer" +make -B bin/c_compiler + +if [[ "$?" -ne 0 ]]; then + echo "Build failed."; +fi + +echo "" +echo "" +echo "=========================================" +echo " Testing compiler" + +PASSED=0 +CHECKED=0 + +for i in c_compiler/test/in/*.c; do + echo "===========================" + echo "" + echo "Input file : ${i}" + BASENAME=$(basename $i .c); + cat $i | ./bin/c_compiler > c_compiler/test/out/$BASENAME.stdout.s 2> c_compiler/test/out/$BASENAME.stderr.txt + + diff <(cat c_compiler/test/ref/$BASENAME.stdout.s) <(cat c_compiler/test/out/$BASENAME.stdout.s) > c_compiler/test/out/$BASENAME.diff.txt + + if [[ "$?" -ne "0" ]]; then + echo -e "\nERROR" + else + PASSED=$(( ${PASSED}+1 )); + fi + + CHECKED=$(( ${CHECKED}+1 )); +done + +echo "########################################" +echo "Passed ${PASSED} out of ${CHECKED}". +echo "" -- cgit