diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-06 21:05:27 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-06 21:05:27 +0000 |
commit | b5e0b062d81492b1b6b57d79a7f647b04ea992d1 (patch) | |
tree | ded96a4f3d2b934b1b769c4e2bc21b0638519346 | |
parent | 7eef2a40d5816c91acb56a8c83d32e9c70717812 (diff) | |
parent | 30f7753bb64ad95750dbd6bce9b7ab3c077b92aa (diff) | |
download | Compiler-b5e0b062d81492b1b6b57d79a7f647b04ea992d1.tar.gz Compiler-b5e0b062d81492b1b6b57d79a7f647b04ea992d1.zip |
Merge branch 'compiler'
129 files changed, 1543 insertions, 1079 deletions
@@ -1,6 +1,7 @@ bin/ c_lexer/build/ c_parser/build/ +c_compiler/build/ *.yy.cpp *.tab.cpp *.tab.hpp diff --git a/.vagrant/machines/default/virtualbox/action_provision b/.vagrant/machines/default/virtualbox/action_provision new file mode 100644 index 0000000..56a084d --- /dev/null +++ b/.vagrant/machines/default/virtualbox/action_provision @@ -0,0 +1 @@ +1.5:9a969542-a2da-4437-bafe-ff8e0a4f9172
\ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/action_set_name b/.vagrant/machines/default/virtualbox/action_set_name new file mode 100644 index 0000000..f7613f8 --- /dev/null +++ b/.vagrant/machines/default/virtualbox/action_set_name @@ -0,0 +1 @@ +1488834030
\ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/creator_uid b/.vagrant/machines/default/virtualbox/creator_uid new file mode 100644 index 0000000..e37d32a --- /dev/null +++ b/.vagrant/machines/default/virtualbox/creator_uid @@ -0,0 +1 @@ +1000
\ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/id b/.vagrant/machines/default/virtualbox/id new file mode 100644 index 0000000..eb57d45 --- /dev/null +++ b/.vagrant/machines/default/virtualbox/id @@ -0,0 +1 @@ +9a969542-a2da-4437-bafe-ff8e0a4f9172
\ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/index_uuid b/.vagrant/machines/default/virtualbox/index_uuid new file mode 100644 index 0000000..b7ff654 --- /dev/null +++ b/.vagrant/machines/default/virtualbox/index_uuid @@ -0,0 +1 @@ +dd05ec035bdc4e82b8f1d14ffc72abec
\ No newline at end of file diff --git a/.vagrant/machines/default/virtualbox/private_key b/.vagrant/machines/default/virtualbox/private_key new file mode 100644 index 0000000..98761cf --- /dev/null +++ b/.vagrant/machines/default/virtualbox/private_key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAoo+hwvBwBwGKwMyyGUOuStvHA8tSMFpOLYVB16kTjiYxRHaX +3EBOpcED9wM8KUdjqk0RcMhLyshPOnjxla1bE4Djf/bABfY4duC3PSdUC+8BXraJ +DWi0ZIEMbvBbt8RLowC0qjmpGZm4lyc+T6uRUQm3igXgDSWypSwtZ39O5IKYc5Vg +BiG+8maSSuuQTaafLeQb4A7k6odvYwVIffHeX37/HKFJ2cAPm3bF4I+BhCQ3y0B6 +XpOTtA0/qsQV0IRhbfJyxtxx5e8rcI+iunxNmfFX+kOfrPuKRKNavYNCLA6NzpbN +ABA4ioI639+vuNBITuAeR5ZPOwzSEd950RhSiwIDAQABAoIBAFHgiDg4hCRzOWDM +ePPSrAnWUpHtqZLjGTdCRByMc2YIhI4YMXrlBPfllMqoXXfpFiDjumIU8F/NrmIk +IoIiE5pqM6vzK1qVXnE50/FASsvL8OO0iqkvUJTCjvGdrISNoQ8TvutyKWLUky3G +vVLBdmrbuw8Q0H2uvCEHdEMJ00GyW6RWGbGEj9OfomWxDkIIE3y/WqIDzsSyJbi5 +doD4Fmt5FdTfaRId6Xqmv0bVEXYERgwM7CrLukm8qTyASe42gCrDk5HZPoS3SbOH +co57iNlQCn+EvMl5RjTD8LhcOh+PFEx9d1PLxBKoNe2/ckOuskWkH4/xYkkV8bbJ +bD5kpHECgYEA1+mHC6S4vNHuLmzFKgj+9hpvYVUqny8knvxTz2Uk+QNUuigizStt +tpKiv9IhPmYDpyfRlLqBManO35eia4InxG5Jttqngd3Zxm2Pi0KFXlpK/vT4FOOv ++wCLX+v0jPBhdfpoxMyDh2m9pTangFeuSwmTSKA0YDyB6gv2+XVSheMCgYEAwL5I +gsbRkJPkJu47Ny0lQZCzPBg7Rls0E5B+ZsxjlN6XsFRpaHtMcvLo1wHbP1UVnx2H +E63z/RleAJSa10qouPCFvyFRT9+1zCskN0g0rFfs3KJfUu4KQN2gnJhcgMrXIvQY +aUZBSV1KESrDhv/nIi0jiQGQP1ZVJ6CwzSuE4TkCgYBINKvaA/tYAijSU8PYG+wQ +J6UJ+u5nD+ACX0Yz/JJRyIBkRRnmXViyjvVoiwBdGsar9xAK2hhsoGozskU6COv9 +sBhs01DjiGrVBjzUCKoLdMzR2ODCM6bdnT//EyzUmWsuhlDVR8PQ3Fb00oNP04LR +S2F7D0CU0odr2xZ8NHUlmwKBgAFXUZnXWE3xOX0r2y2fGstB/n1EjeUHbW1VbAu7 +EFZQ1i1b6P/ZG8cQ+Z3FXYkrRmM9CVuRskjojEbkwc8Nre7bfZK+4Jn1rIYnJrFb +x0ILupIubwtcrKWW5S8xwDMFNkn/+MuCuaGRDJrh+UpPLYSHW9/wKtjF+QG1lIVY +8CvBAoGAATJ0xWgIszXBnC5fioLKqV9lUupk3t+aSDPFKBg5qnwlimTUKqKWzsD6 +r3rHBVFIA0agYIEr88Kt4tcXQ8dcUmTMhrIISHidAs21qzK1DBNhWeJ6AhggxLiH +kU1+pOu12B5nUpR5KBfwTyO3MaQuEB/tefu9XALDtdQbhmPKiJY= +-----END RSA PRIVATE KEY----- diff --git a/.vagrant/machines/default/virtualbox/synced_folders b/.vagrant/machines/default/virtualbox/synced_folders new file mode 100644 index 0000000..7c78dac --- /dev/null +++ b/.vagrant/machines/default/virtualbox/synced_folders @@ -0,0 +1 @@ +{"virtualbox":{"/vagrant":{"guestpath":"/vagrant","hostpath":"/home/yannherklotz/Github/langproc-2016-cw-ymh15","disabled":false,"__vagrantfile":true}}}
\ No newline at end of file @@ -74,3 +74,26 @@ ** TODO Coursework (c compiler) DEADLINE: <2017-03-28 Tue> +*** Task + +*** Mips Assembly + + - %hi(id) :: loads the upper address of the global variable id + - %lo(id) :: loads the lower address of the global variable id + - addiu :: Add Immediate Unsigned + - jr :: Jump Register + - li :: Pseudo instruction interpreted as ( lui $rd, LabelAddr[31:16] + ori $rd,$rd, LabelAddr[15:0] ) + - lui :: loads uppper immediate. + - lw :: Load Word + - move :: Pseudo instruction interpreted as (add $rt, $rs, $zero) + - nop :: No Operation + - sw :: Store Word + + +*** TODOs + + - 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 + implemented by function. diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp index 9d51816..cea7e82 100644 --- a/c_compiler/include/ast.hpp +++ b/c_compiler/include/ast.hpp @@ -1,14 +1,25 @@ #ifndef AST_HPP #define AST_HPP -#include "base.hpp" +#include <vector> +#include <string> +#include <iostream> +#include <map> +#include <cstdint> + +struct VarLocation; + +typedef std::map<std::string, VarLocation> VariableStack; + +#include "node.hpp" +#include "expression.hpp" +#include "type.hpp" +#include "initializer.hpp" +#include "declaration.hpp" #include "statement.hpp" #include "function.hpp" -#include "declaration.hpp" -#include "expression.hpp" -#include "primitives.hpp" -#include "ast_top.hpp" +#include "translation_unit.hpp" -ast_Top *parseAST(); +TranslationUnit* parseAST(); #endif diff --git a/c_compiler/include/ast_top.hpp b/c_compiler/include/ast_top.hpp deleted file mode 100644 index 737ff58..0000000 --- a/c_compiler/include/ast_top.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef TOP_AST_HPP -#define TOP_AST_HPP - -#include "ast.hpp" - -#include <vector> - -class ast_Top { -public: - void print() { - for(size_t i = 0; i < vec.size(); ++i) { - vec[i]->print(); - } - } - - void push(const Base *stmnt) { - vec.push_back(stmnt); - } - -private: - std::vector<const Base *> vec; -}; - -#endif diff --git a/c_compiler/include/base.hpp b/c_compiler/include/base.hpp deleted file mode 100644 index 9451609..0000000 --- a/c_compiler/include/base.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef AST_BASE_HPP -#define AST_BASE_HPP - -#include <string> -#include <iostream> -#include <vector> - -class Base { -public: - virtual ~Base() {} - - virtual void print() const = 0; - virtual void push(const Base* _var) const = 0; -}; - -#endif diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp index bfd3070..c46aca2 100644 --- a/c_compiler/include/declaration.hpp +++ b/c_compiler/include/declaration.hpp @@ -3,48 +3,32 @@ #include "ast.hpp" -#include <vector> - // Declaration that holds a list of declarations -class DeclarationList : public Base { -private: - mutable std::vector<const Base*> dec_list; - +class Declaration : public Node { +protected: + Type* type; + std::string id; + Initializer* init; + Declaration* next_decl; + Declaration* list_next_decl; + 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 std::string& _id = ""); -class VariableDeclaration : public Base { -private: - mutable std::vector<const Base*> var_list; + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; -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(); - } - } - - virtual void push(const Base* _var) const { - var_list.push_back(_var); - } + void addDeclaration(Declaration* _next_decl); + void addList(Declaration* _next_decl); + + void setType(Type* _type); + + Declaration* getNext() const; + Declaration* getNextListItem() const; + std::string getId() const; + std::string getType() const; }; #endif diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index f67d2d7..9e57fd4 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -3,22 +3,13 @@ #include "ast.hpp" -#include <string> -#include <iostream> - -class Expression : public Base { -private: +class Expression : public Node { public: - Expression() {} - - virtual void print() const { - - } + Expression(const Node* expr = nullptr); - 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; + virtual void printxml() const; + virtual void printasm() const; }; #endif diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp index 6fbcdee..d608531 100644 --- a/c_compiler/include/function.hpp +++ b/c_compiler/include/function.hpp @@ -3,51 +3,27 @@ #include "ast.hpp" -#include <string> -#include <iostream> -class Function : public Base { -private: - 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) {} - - virtual void print() const { - std::cout << "<Function id=\"" << id << "\">" << std::endl; - param->print(); - comp_statement->print(); - std::cout << "</Function>" << std::endl; - } - - virtual void push(const Base* var) const { - std::cerr << "Error: Can't call this function on this class" << std::endl; - (void)var; - } +struct VarLocation { + Type* type; + int32_t stack_position; }; -class ParamList : public Base { -private: - mutable std::vector<const Base*> param_list; -public: - ParamList() {} +class Function : public Node { +protected: + Type* type; + std::string id; + Declaration* parameter_list; + Statement* statement; - 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); - } +public: + Function(const std::string& _id, Declaration* _parameter_list, Statement* _statement); + + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; }; + #endif diff --git a/c_compiler/include/initializer.hpp b/c_compiler/include/initializer.hpp new file mode 100644 index 0000000..f7c0dab --- /dev/null +++ b/c_compiler/include/initializer.hpp @@ -0,0 +1,29 @@ +#ifndef INITIALIZER_HPP +#define INITIALIZER_HPP + +#include "ast.hpp" + + +class Initializer : public Node { +public: + Initializer(); + + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; +}; + + +class Integer : public Initializer { +public: + Integer(); +}; + + +class StringLiteral : public Initializer { +public: + StringLiteral(); +}; + + +#endif diff --git a/c_compiler/include/node.hpp b/c_compiler/include/node.hpp new file mode 100644 index 0000000..940a948 --- /dev/null +++ b/c_compiler/include/node.hpp @@ -0,0 +1,17 @@ +#ifndef AST_BASE_HPP +#define AST_BASE_HPP + +#include "ast.hpp" + + +class Node { +public: + virtual ~Node() {} + + virtual void print() const = 0; + virtual void printxml() const = 0; + virtual void printasm() const = 0; +}; + + +#endif diff --git a/c_compiler/include/primitives.hpp b/c_compiler/include/primitives.hpp deleted file mode 100644 index 2eeaa19..0000000 --- a/c_compiler/include/primitives.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef AST_PRIMITIVES_HPP -#define AST_PRIMITIVES_HPP - -#include "ast.hpp" - -#include <string> - -class Variable : public Base { -private: - std::string id; -public: - Variable(const std::string& _id) : id(_id) {} - - virtual void print() const { - std::cout << "<Variable id=\"" << id << "\" />" << 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 Parameter : public Base { -private: - std::string id; -public: - Parameter(const std::string& _id) : id(_id) {} - - virtual void print() const { - std::cout << "<Parameter id=\"" << id << "\" />" << 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..17a5153 100644 --- a/c_compiler/include/statement.hpp +++ b/c_compiler/include/statement.hpp @@ -3,79 +3,96 @@ #include "ast.hpp" -class Statement : public Base { + +class Statement : public Node { protected: - mutable std::vector<const Base*> list; + Statement* next_statement; public: - Statement() {} - - 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(Statement* statement = nullptr); -class StatementList : public Statement { -public: - StatementList(const Base* _statement) : Statement(_statement) {} + virtual void print() const = 0; + virtual void printxml() const = 0; + virtual void printasm() const = 0; + + virtual void count_variables(int32_t& var_count) const = 0; + + void addStatement(Statement* _next); }; + class CompoundStatement : public Statement { +protected: + Declaration* m_decl; + Statement* m_statement; + public: - CompoundStatement() : Statement() {} - CompoundStatement(const Base* _el) : Statement(_el) {} - CompoundStatement(const Base* _dec, const Base* _statement) : - Statement(_dec, _statement) {} - - virtual void print() const override { - std::cout << "<Scope>" << std::endl; - for(size_t i = 0; i < list.size(); ++i) { - list[i]->print(); - } - std::cout << "</Scope>" << std::endl; - } + CompoundStatement(Declaration* decl = nullptr, Statement* statement = nullptr); + CompoundStatement(Statement* statement); + + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; + class SelectionStatement : public Statement { +protected: + Statement* m_if; + Statement* m_else; public: - SelectionStatement() : Statement() {} - SelectionStatement(const Base* _el) : Statement(_el) {} - SelectionStatement(const Base* _if, const Base* _else) : - Statement(_if, _else) {} + SelectionStatement(Statement* _if = nullptr, Statement* _else = nullptr); + + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; + class ExpressionStatement : public Statement { +protected: + Expression* m_expr; public: - ExpressionStatement() : Statement() {} - ExpressionStatement(const Base* _el) : Statement(_el) {} + ExpressionStatement(Expression* expr = nullptr); + + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; + class JumpStatement : public Statement { +protected: + Expression* m_expr; public: - JumpStatement() : Statement() {} - JumpStatement(const Base* _el) : Statement(_el) {} + JumpStatement(Expression* expr = nullptr); + + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; + class IterationStatement : public Statement { +protected: + Statement* m_statement; public: - IterationStatement() : Statement() {} - IterationStatement(const Base* _el) : Statement(_el) {} - IterationStatement(const Base* _if, const Base* _else) : - Statement(_if, _else) {} + IterationStatement(Statement* statement); + + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; + + virtual void count_variables(int32_t& var_count) const; }; + #endif diff --git a/c_compiler/include/translation_unit.hpp b/c_compiler/include/translation_unit.hpp new file mode 100644 index 0000000..42822f9 --- /dev/null +++ b/c_compiler/include/translation_unit.hpp @@ -0,0 +1,22 @@ +#ifndef EXTERNAL_HPP +#define EXTERNAL_HPP + +#include "ast.hpp" + + +class TranslationUnit : public Node { +protected: + std::vector<Node* > translation_unit; + +public: + TranslationUnit(Node* decl); + + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; + + void push(Node* decl); +}; + + +#endif diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp new file mode 100644 index 0000000..8727ee0 --- /dev/null +++ b/c_compiler/include/type.hpp @@ -0,0 +1,70 @@ +#ifndef TYPE_HPP +#define TYPE_HPP + +#include "ast.hpp" + + +class Type : public Node { +public: + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; + + virtual std::string getType() const = 0; +}; + + +class Specifier : public Type { +public: + virtual std::string getType() const = 0; +}; + + +class Pointer : public Type { +protected: + Type* pointer_type; + +public: + Pointer(Type* _pointer_type); + + virtual std::string getType() const; +}; + + +class Array : public Type { +protected: + int32_t size; + Type* array_type; + +public: + Array(Type* _array_type, int32_t _size = 0); + + virtual std::string getType() const; +}; + + +class Void : public Specifier { +public: + Void(); + + virtual std::string getType() const; +}; + + +class Int : public Specifier { +public: + Int(); + + virtual std::string getType() const; +}; + + +class Char : public Specifier { +public: + Char(); + + virtual std::string getType() const; +}; + + +#endif diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex index c8ca90a..7690d71 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]*) @@ -38,9 +30,17 @@ ALL . %% -typedef|extern|static|auto|register { return T_STRG_SPEC; } -void|char|short|int|long|float|double|signed|unsigned { return T_TYPE_SPEC; } -const|volatile { return T_TYPE_QUAL; } +(void) { return T_VOID; } +(char) { return T_CHAR; } +(signed[ ]char) { return T_SCHAR; } +(unsigned[ ]char) { return T_UCHAR; } +((short[ ]int)|(signed[ ]short[ ]int)|short|(signed[ ]short)) { return T_SSINT; } +((unsigned[ ]short[ ]int)|(unsigned[ ]short)) { return T_USINT; } +((signed[ ]long[ ]int)|(signed[ ]long)|(long[ ]int)|long) { return T_LINT; } +((unsigned[ ]long[ ]int)|(unsigned[ ]long)) { return T_ULINT; } +((unsigned[ ]int)|unsigned) { return T_UINT; } +((signed[ ]int)|int|signed) { return T_SINT; } + [;] { return T_SC; } [,] { return T_CMA; } diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 1fc4d4a..c643f10 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 TranslationUnit* 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,116 +15,161 @@ void yyerror(const char *); // Represents the value associated with any kind of // AST node. %union{ - const Base *stmnt; + Node* node; + TranslationUnit* trans_unit; + Function* function; + Statement* statement; + Declaration* declaration; + Expression* expression; + Type* type; + Initializer* initializer; double number; - std::string *string; + 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 - - -%type <stmnt> ExtDef ExtDeclaration + +%token 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 + + T_VOID T_CHAR T_SCHAR T_UCHAR T_SSINT T_USINT T_LINT T_ULINT T_UINT T_SINT -%type <stmnt> FuncDef ParameterList Parameter ParamDeclarator +%nonassoc T_RRB +%nonassoc T_ELSE + -%type <stmnt> DeclarationList Declaration DeclarationSpec DeclarationSpec_T InitDeclarator InitDeclaratorList Declarator +%type <node> ExternalDeclaration -%type <stmnt> StatementList Statement CompoundStatement CompoundStatement_2 SelectionStatement ExpressionStatement JumpStatement IterationStatement +%type <trans_unit> TranslationUnit -%type <stmnt> Expression AssignmentExpression ConditionalExpression LogicalOrExpression LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression AndExpression EqualityExpression RelationalExpression ShiftExpression AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression +%type <function> FunctionDefinition - -%type <number> Constant T_INT_CONST +%type <statement> StatementList Statement CompoundStatement CompoundStatement_2 + SelectionStatement + ExpressionStatement JumpStatement IterationStatement + +%type <declaration> ParameterList Parameter DeclarationList Declaration InitDeclaratorList + InitDeclarator + IdentifierList + +%type <expression> Expression AssignmentExpression ConditionalExpression LogicalOrExpression + LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression + AndExpression EqualityExpression RelationalExpression ShiftExpression + AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression + PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression + +%type <type> DeclarationSpec + +%type <string> Declarator DirectDeclarator +%type <initializer> Constant -%type <string> 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 <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 MultDivRemOP UnaryOperator %start ROOT %% ROOT: - ExtDef { ; } + TranslationUnit { g_root = $1; } ; // EXTERNAL DEFINITION -ExtDef: - ExtDeclaration { g_root->push($1); } - | ExtDef ExtDeclaration { g_root->push($2); } +TranslationUnit: + ExternalDeclaration { $$ = new TranslationUnit($1); } + | TranslationUnit ExternalDeclaration { $$->push($2); } ; -ExtDeclaration: +ExternalDeclaration: Declaration { $$ = $1; } - | FuncDef { $$ = $1; } + | FunctionDefinition { $$ = $1; } ; // FUNCTION DEFINITION -FuncDef: +FunctionDefinition: DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); } ; ParameterList: - %empty { $$ = new ParamList(); } - | Parameter { $$ = new ParamList($1); } - | ParameterList T_CMA Parameter { $$->push($3); } + %empty { $$ = new Declaration(); } + | Parameter { $$ = $1; } + | ParameterList T_CMA Parameter { $3->addDeclaration($$); $$ = $3;} ; Parameter: - DeclarationSpec ParamDeclarator { $$ = $2; } - ; - -ParamDeclarator: - T_IDENTIFIER { $$ = new Parameter(*$1);} + DeclarationSpec T_IDENTIFIER { $$ = new Declaration(*$2); } ; // Declaration DeclarationList: - Declaration { $$ = new DeclarationList($1); } - | DeclarationList Declaration { $$->push($2); } + Declaration { $$ = $1; } + | DeclarationList Declaration { $2->addDeclaration($$); $$ = $2; } ; Declaration: - DeclarationSpec InitDeclaratorList T_SC { $$ = $2; } + DeclarationSpec InitDeclaratorList T_SC { + $$ = $2; + Declaration* tmp_decl = $2; + + while(tmp_decl != nullptr) { + tmp_decl->setType($1); + tmp_decl = tmp_decl->getNextListItem(); + } + } ; DeclarationSpec: - DeclarationSpec_T { ; } - | DeclarationSpec_T DeclarationSpec { ; } - ; - -DeclarationSpec_T: - T_TYPE_SPEC { ; } - | T_TYPE_QUAL { ; } - | T_STRG_SPEC { ; } + T_VOID { $$ = new Void; } + | T_CHAR { $$ = new Char; } + | T_SCHAR { $$ = new Char; } + | T_UCHAR { $$ = new Char; } + | T_SSINT { $$ = new Int; } + | T_USINT { $$ = new Int; } + | T_LINT { $$ = new Int; } + | T_ULINT { $$ = new Int; } + | T_UINT { $$ = new Int; } + | T_SINT { $$ = new Int; } ; InitDeclaratorList: - InitDeclarator { $$ = new VariableDeclaration($1); } - | InitDeclaratorList T_CMA InitDeclarator { $$->push($3); } + InitDeclarator { $$ = new Declaration(*$1); } + | InitDeclaratorList T_CMA InitDeclarator { $3->addList($$); $$ = $3; } ; InitDeclarator: - Declarator { ; } - | Declarator T_EQ AssignmentExpression { ; } + Declarator { $$ = new Declaration(*$1); } + | Declarator T_EQ AssignmentExpression { $$ = new Declaration(*$1); } ; Declarator: - T_IDENTIFIER {$$ = new Variable(*$1); } + DirectDeclarator { $$ = $1; } + | T_MULT DirectDeclarator { $$ = $2; } + ; + +DirectDeclarator: + T_IDENTIFIER { $$ = $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 Declaration(); } + | IdentifierList T_CMA T_IDENTIFIER { $$ = new Declaration(); } + // Statement StatementList: - Statement { $$ = new StatementList($1); } - | StatementList Statement { $$->push($2); } + Statement { $$ = $1; } + | StatementList Statement { $2->addStatement($$); $$ = $2; } ; Statement: @@ -147,16 +193,16 @@ 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: T_SC { $$ = new ExpressionStatement(); } - | Expression T_SC { $$ = $1; } +| Expression T_SC { $$ = new ExpressionStatement(); } ; JumpStatement: - T_RETURN ExpressionStatement { $$ = $2; } + T_RETURN ExpressionStatement { $$ = new JumpStatement(); } ; IterationStatement: @@ -244,7 +290,7 @@ MultDivRemOP: CastExpression: UnaryExpression { $$ = $1; } - | T_LRB T_TYPE_SPEC T_RRB CastExpression { $$ = $4; } + | T_LRB DeclarationSpec T_RRB CastExpression { $$ = $4; } ; UnaryExpression: @@ -252,7 +298,7 @@ UnaryExpression: | T_INCDEC UnaryExpression { $$ = $2; } | UnaryOperator CastExpression { $$ = $2; } | T_SIZEOF UnaryExpression { $$ = $2; } - | T_SIZEOF T_LRB T_TYPE_SPEC T_RRB { $$ = new Expression(); } + | T_SIZEOF T_LRB DeclarationSpec T_RRB { $$ = new Expression(); } ; UnaryOperator: @@ -274,30 +320,30 @@ PostfixExpression: PostfixExpression2: T_RRB { $$ = new Expression(); } - | ArgumentExpressionList T_RRB { $$ = $1; } + | ArgumentExpressionList T_RRB { $$ = new Expression; } ; ArgumentExpressionList: - AssignmentExpression { $$ = $1; } - | ArgumentExpressionList T_CMA AssignmentExpression { $$ = $3; } + AssignmentExpression { $$ = new Expression; } + | ArgumentExpressionList T_CMA AssignmentExpression { $$ = new Expression; } ; PrimaryExpression: T_IDENTIFIER { $$ = new Expression(); } - | Constant { $$ = new Expression(); } + | Constant { $$ = new Expression($1); } | T_LRB Expression T_RRB { $$ = $2; } ; Constant: - T_INT_CONST { $$ = $1; } + T_INT_CONST { $$ = new Initializer(); } ; %% -ast_Top *g_root; // Definition of variable (to match declaration earlier) +TranslationUnit* g_root; // Definition of variable (to match declaration earlier) -ast_Top *parseAST() { - g_root = new ast_Top; +TranslationUnit* parseAST() { + g_root = 0; yyparse(); return g_root; } diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp new file mode 100644 index 0000000..b550eba --- /dev/null +++ b/c_compiler/src/compiler_main.cpp @@ -0,0 +1,14 @@ +#include "ast.hpp" + +#include <iostream> + +int main(int argc, char *argv[]) +{ + Node* ast = parseAST(); + + ast->printxml(); + + ast->printasm(); + + return 0; +} diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp new file mode 100644 index 0000000..e2f27cc --- /dev/null +++ b/c_compiler/src/declaration.cpp @@ -0,0 +1,68 @@ +#include "ast.hpp" + + +// Declaration definition + +Declaration::Declaration(const std::string& _id) + : id(_id) +{} + +void Declaration::print() const +{ + if(next_decl != nullptr) + next_decl->print(); + + if(id != "") + std::cout << id << std::endl; +} + +void Declaration::printxml() const +{ + if(next_decl != nullptr) + next_decl->printxml(); + + if(list_next_decl != nullptr) { + list_next_decl->printxml(); + } + + if(id != "") + std::cout << "<Variable id=\""<< id << "\" />" << std::endl; +} + +void Declaration::printasm() const +{} + +void Declaration::addDeclaration(Declaration* _next_decl) +{ + next_decl = _next_decl; +} + +void Declaration::addList(Declaration* _next_decl) +{ + list_next_decl = _next_decl; +} + +void Declaration::setType(Type* _type) +{ + type = _type; +} + +Declaration* Declaration::getNext() const +{ + return next_decl; +} + +Declaration* Declaration::getNextListItem() const +{ + return list_next_decl; +} + +std::string Declaration::getId() const +{ + return id; +} + +std::string Declaration::getType() const +{ + return type->getType(); +} diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp new file mode 100644 index 0000000..4b09581 --- /dev/null +++ b/c_compiler/src/expression.cpp @@ -0,0 +1,16 @@ +#include "ast.hpp" + + +// Expression definition + +Expression::Expression(const Node* expr) +{} + +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 new file mode 100644 index 0000000..acd5547 --- /dev/null +++ b/c_compiler/src/function.cpp @@ -0,0 +1,53 @@ +#include "ast.hpp" + + +// Function definition + +Function::Function(const std::string& _id, Declaration* _parameter_list, Statement* _statement) + : id(_id), parameter_list(_parameter_list), statement(_statement) +{} + +void Function::print() const +{ + std::cout << id << std::endl; + + if(parameter_list != nullptr) + parameter_list->print(); + + if(statement != nullptr) + statement->print(); +} + +void Function::printxml() const +{ + std::cout << "<Function id=\"" << id << "\">" << std::endl; + + Declaration* parameter = parameter_list; + std::vector<std::string> parameter_vec; + + while(parameter != nullptr) { + if(parameter->getId() != "") + parameter_vec.push_back(parameter->getId()); + parameter = parameter->getNext(); + } + + for(std::vector<std::string>::reverse_iterator itr = parameter_vec.rbegin(); + itr != parameter_vec.rend(); ++itr) { + + std::cout << "<Parameter id=\"" << *itr << "\" />" << std::endl; + } + + if(statement != nullptr) + statement->printxml(); + + std::cout << "</Function>" << std::endl; +} + +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 new file mode 100644 index 0000000..580d81f --- /dev/null +++ b/c_compiler/src/initializer.cpp @@ -0,0 +1,28 @@ +#include "ast.hpp" + + +// Initializer definition + +Initializer::Initializer() +{} + +void Initializer::print() const +{} + +void Initializer::printxml() const +{} + +void Initializer::printasm() const +{} + + +// Integer definition + +Integer::Integer() : Initializer() +{} + + +// String Literal definition + +StringLiteral::StringLiteral() : Initializer() +{} diff --git a/c_compiler/src/parser_main.cpp b/c_compiler/src/parser_main.cpp deleted file mode 100644 index 9626334..0000000 --- a/c_compiler/src/parser_main.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "ast.hpp" - -#include <iostream> - -int main(int argc, char *argv[]) { - ast_Top *ast = parseAST(); - - std::cout << "<?xml version=\"1.0\"?>" << std::endl << "<Program>" << std::endl; - - ast->print(); - - std::cout << "</Program>" << std::endl; - - return 0; -} diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp new file mode 100644 index 0000000..dcd2271 --- /dev/null +++ b/c_compiler/src/statement.cpp @@ -0,0 +1,198 @@ +#include "ast.hpp" + + +// General base Statement definition + +Statement::Statement(Statement* statement) + : next_statement(statement) +{} + +void Statement::addStatement(Statement* _next) +{ + next_statement = _next; +} + + +// Compound Statement definition + +CompoundStatement::CompoundStatement(Declaration* decl, Statement* statement) + : Statement(), m_decl(decl), m_statement(statement) +{} + +CompoundStatement::CompoundStatement(Statement* statement) + : m_statement(statement) +{} + +void CompoundStatement::print() const +{ + if(m_decl != nullptr) + m_decl->print(); + + if(m_statement != nullptr) + m_statement->print(); +} + +void CompoundStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + + std::cout << "<Scope>" << std::endl; + + if(m_decl != nullptr) + m_decl->printxml(); + + if(m_statement != nullptr) + m_statement->printxml(); + + std::cout << "</Scope>" << std::endl; +} + +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++; + + std::cout << declaration_list->getType() << std::endl; + + declaration_list = declaration_list->getNextListItem(); + } + + std::cout << declaration->getType() << std::endl; + + var_count++; + + declaration = declaration->getNext(); + } +} + + +// Selection Statement definition + +SelectionStatement::SelectionStatement(Statement* _if, Statement* _else) + : Statement(), m_if(_if), m_else(_else) {} + +void SelectionStatement::print() const +{ + m_if->print(); + m_else->print(); +} + +void SelectionStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + if(m_if != nullptr) + m_if->printxml(); + if(m_else != nullptr) + m_else->printxml(); +} + +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) +{} + +void ExpressionStatement::print() const +{} + +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) +{} + +void JumpStatement::print() const +{} + +void JumpStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); +} + +void JumpStatement::printasm() const +{ + m_expr->printasm(); + 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) +{} + +void IterationStatement::print() const +{} + +void IterationStatement::printxml() const +{ + if(next_statement != nullptr) + next_statement->printxml(); + if(m_statement != nullptr) + m_statement->printxml(); +} + +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 new file mode 100644 index 0000000..a3566d7 --- /dev/null +++ b/c_compiler/src/translation_unit.cpp @@ -0,0 +1,37 @@ +#include "ast.hpp" + + +// Translation Unit definition + +TranslationUnit::TranslationUnit(Node* decl) +{ + push(decl); +} + +void TranslationUnit::print() const +{ + for(auto& node : translation_unit) { + node->print(); + } +} + +void TranslationUnit::printxml() const +{ + std::cout << "<?xml version=\"1.0\"?>\n<Program>" << std::endl; + for(auto& node : translation_unit) { + node->printxml(); + } + std::cout << "</Program>" << std::endl; +} + +void TranslationUnit::printasm() const +{ + for(auto& node : translation_unit) { + node->printasm(); + } +} + +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_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 @@ -<?xml version="1.0"?> -<Program> - <Variable id="a" /> - <Variable id="b" /> - <Variable id="c" /> - <Variable id="d" /> - <Variable id="e" /> - <Variable id="f" /> -</Program> + 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/02.diff.txt b/c_compiler/test/out/01.stdout.s index e69de29..e69de29 100644 --- a/c_compiler/test/out/02.diff.txt +++ b/c_compiler/test/out/01.stdout.s 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 @@ -<?xml version="1.0"?> -<Program> -<Variable id="a" /> -<Variable id="b" /> -<Variable id="c" /> -<Variable id="d" /> -<Variable id="e" /> -<Variable id="f" /> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> -<Variable id="a" /> -<Variable id="b" /> -<Variable id="c" /> -<Variable id="d" /> -<Variable id="e" /> -<Variable id="f" /> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Scope></Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="f"> -<Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/03.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="foo"> - <Parameter id="bar" /> - <Scope></Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/03.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="foo"> -<Parameter id="bar" /> -<Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/04.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="foo"> - <Parameter id="bar1" /> - <Parameter id="bar2" /> - <Scope> - <Variable id="x" /> - <Variable id="y" /> - <Variable id="z" /> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/04.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="foo"> -<Parameter id="bar1" /> -<Parameter id="bar2" /> -<Scope> -<Variable id="x" /> -<Variable id="y" /> -<Variable id="z" /> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/05.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="foo"> - <Parameter id="x" /> - <Parameter id="y" /> - <Scope> - <Scope></Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/05.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="foo"> -<Parameter id="x" /> -<Parameter id="y" /> -<Scope> -<Scope> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/06.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Parameter id="a" /> - <Parameter id="b" /> - <Parameter id="c" /> - <Scope> - <Variable id="d" /> - <Scope> - <Variable id="e" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/06.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="f"> -<Parameter id="a" /> -<Parameter id="b" /> -<Parameter id="c" /> -<Scope> -<Variable id="d" /> -<Scope> -<Variable id="e" /> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/07.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="foo"> - <Parameter id="a" /> - <Scope> - <Scope> - <Variable id="z" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/07.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="foo"> -<Parameter id="a" /> -<Scope> -<Scope> -<Variable id="z" /> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/08.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="func"> - <Parameter id="a" /> - <Parameter id="b" /> - <Scope> - <Variable id="c" /> - <Scope> - <Variable id="d" /> - </Scope> - <Scope> - <Variable id="e" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/08.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="func"> -<Parameter id="a" /> -<Parameter id="b" /> -<Scope> -<Variable id="c" /> -<Scope> -<Variable id="d" /> -</Scope> -<Scope> -<Variable id="e" /> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/09.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="x"> - <Scope></Scope> - </Function> - <Variable id="g" /> - <Function id="zz"> - <Parameter id="a" /> - <Parameter id="b" /> - <Parameter id="c" /> - <Scope> - <Scope> - <Variable id="a" /> - </Scope> - <Scope> - <Variable id="fsdfsdfs" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/09.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="x"> -<Scope> -</Scope> -</Function> -<Variable id="g" /> -<Function id="zz"> -<Parameter id="a" /> -<Parameter id="b" /> -<Parameter id="c" /> -<Scope> -<Scope> -<Variable id="a" /> -</Scope> -<Scope> -<Variable id="fsdfsdfs" /> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/10.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Scope></Scope> - </Function> - <Variable id="g" /> - <Function id="x"> - <Parameter id="y" /> - <Scope> - <Variable id="z" /> - <Scope> - <Variable id="r" /> - </Scope> - <Scope> - <Variable id="f" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/10.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="f"> -<Scope> -</Scope> -</Function> -<Variable id="g" /> -<Function id="x"> -<Parameter id="y" /> -<Scope> -<Variable id="z" /> -<Scope> -<Variable id="r" /> -</Scope> -<Scope> -<Variable id="f" /> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/11.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Scope> - <Variable id="x" /> - <Scope> - <Variable id="y" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/11.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="f"> -<Scope> -<Variable id="x" /> -<Scope> -<Variable id="y" /> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/12.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Parameter id="b" /> - <Parameter id="c" /> - <Scope> - <Variable id="a" /> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/12.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="f"> -<Parameter id="b" /> -<Parameter id="c" /> -<Scope> -<Variable id="a" /> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/13.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="func"> - <Parameter id="x" /> - <Parameter id="y" /> - <Scope> - <Variable id="i" /> - <Scope> - <Variable id="z" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/13.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="func"> -<Parameter id="x" /> -<Parameter id="y" /> -<Scope> -<Variable id="i" /> -<Scope> -<Variable id="z" /> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/14.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="function_1"> - <Parameter id="a" /> - <Parameter id="b" /> - <Scope> - <Variable id="x" /> - <Scope> - <Variable id="c" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/14.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="function_1"> -<Parameter id="a" /> -<Parameter id="b" /> -<Scope> -<Variable id="x" /> -<Scope> -<Variable id="c" /> -</Scope> -</Scope> -</Function> -</Program> 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 --- a/c_compiler/test/out/15.diff.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="add"> - <Parameter id="a" /> - <Parameter id="b" /> - <Scope> - <Variable id="c" /> - </Scope> - </Function> - <Function id="sub"> - <Parameter id="a" /> - <Parameter id="b" /> - <Scope> - <Variable id="c" /> - </Scope> - </Function> - <Function id="mult_by_5"> - <Parameter id="a" /> - <Scope> - <Variable id="b" /> - </Scope> - </Function> - <Function id="add_5"> - <Parameter id="a" /> - <Scope> - <Variable id="i" /> - <Scope> - <Variable id="b" /> - </Scope> - </Scope> - </Function> -</Program> 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 --- a/c_compiler/test/out/15.stderr.txt +++ /dev/null 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 @@ -<?xml version="1.0"?> -<Program> -<Function id="add"> -<Parameter id="a" /> -<Parameter id="b" /> -<Scope> -<Variable id="c" /> -</Scope> -</Function> -<Function id="sub"> -<Parameter id="a" /> -<Parameter id="b" /> -<Scope> -<Variable id="c" /> -</Scope> -</Function> -<Function id="mult_by_5"> -<Parameter id="a" /> -<Scope> -<Variable id="b" /> -</Scope> -</Function> -<Function id="add_5"> -<Parameter id="a" /> -<Scope> -<Variable id="i" /> -<Scope> -<Variable id="b" /> -</Scope> -</Scope> -</Function> -</Program> diff --git a/c_compiler/test/ref/01 b/c_compiler/test/ref/01 Binary files differnew file mode 100755 index 0000000..9655601 --- /dev/null +++ b/c_compiler/test/ref/01 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 @@ -<?xml version="1.0"?> -<Program> - <Variable id="a" /> - <Variable id="b" /> - <Variable id="c" /> - <Variable id="d" /> - <Variable id="e" /> - <Variable id="f" /> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="foo"> - <Parameter id="bar" /> - <Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="foo"> - <Parameter id="bar1" /> - <Parameter id="bar2" /> - <Scope> - <Variable id="x" /> - <Variable id="y" /> - <Variable id="z" /> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="foo"> - <Parameter id="x" /> - <Parameter id="y" /> - <Scope> - <Scope> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Parameter id="a" /> - <Parameter id="b" /> - <Parameter id="c" /> - <Scope> - <Variable id="d" /> - <Scope> - <Variable id="e" /> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="foo"> - <Parameter id="a" /> - <Scope> - <Scope> - <Variable id="z" /> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="func"> - <Parameter id="a" /> - <Parameter id="b" /> - <Scope> - <Variable id="c" /> - <Scope> - <Variable id="d" /> - </Scope> - <Scope> - <Variable id="e" /> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="x"> - <Scope> - </Scope> - </Function> - <Variable id="g" /> - <Function id="zz"> - <Parameter id="a" /> - <Parameter id="b" /> - <Parameter id="c" /> - <Scope> - <Scope> - <Variable id="a" /> - </Scope> - <Scope> - <Variable id="fsdfsdfs" /> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Scope> - </Scope> - </Function> - <Variable id="g" /> - <Function id="x"> - <Parameter id="y" /> - <Scope> - <Variable id="z" /> - <Scope> - <Variable id="r" /> - </Scope> - <Scope> - <Variable id="f" /> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Scope> - <Variable id="x" /> - <Scope> - <Variable id="y" /> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="f"> - <Parameter id="b" /> - <Parameter id="c" /> - <Scope> - <Variable id="a" /> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="func"> - <Parameter id="x" /> - <Parameter id="y" /> - <Scope> - <Variable id="i" /> - <Scope> - <Variable id="z" /> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="function_1"> - <Parameter id="a" /> - <Parameter id="b" /> - <Scope> - <Variable id="x" /> - <Scope> - <Variable id="c" /> - </Scope> - </Scope> - </Function> -</Program> 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 @@ -<?xml version="1.0"?> -<Program> - <Function id="add"> - <Parameter id="a" /> - <Parameter id="b" /> - <Scope> - <Variable id="c" /> - </Scope> - </Function> - <Function id="sub"> - <Parameter id="a" /> - <Parameter id="b" /> - <Scope> - <Variable id="c" /> - </Scope> - </Function> - <Function id="mult_by_5"> - <Parameter id="a" /> - <Scope> - <Variable id="b" /> - </Scope> - </Function> - <Function id="add_5"> - <Parameter id="a" /> - <Scope> - <Variable id="i" /> - <Scope> - <Variable id="b" /> - </Scope> - </Scope> - </Function> -</Program> 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 +< +--- +> <?xml version="1.0"?> +> <Program> +> <Variable id="a" /> +> <Variable id="b" /> +> <Variable id="c" /> +> <Variable id="d" /> +> <Variable id="e" /> +> <Variable id="f" /> +> <Function id="f"> +> <Scope></Scope> +> </Function> +> <Function id="foo"> +> <Parameter id="bar" /> +> <Scope></Scope> +> </Function> +> <Function id="foo"> +> <Parameter id="bar1" /> +> <Parameter id="bar2" /> +> <Scope> +> <Variable id="x" /> +> <Variable id="y" /> +> <Variable id="z" /> +> </Scope> +> </Function> +> <Function id="foo"> +> <Parameter id="x" /> +> <Parameter id="y" /> +> <Scope> +> <Scope></Scope> +> </Scope> +> </Function> +> <Function id="f"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Parameter id="c" /> +> <Scope> +> <Variable id="d" /> +> <Scope> +> <Variable id="e" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="foo"> +> <Parameter id="a" /> +> <Scope> +> <Scope> +> <Variable id="z" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="func"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Scope> +> <Variable id="c" /> +> <Scope> +> <Variable id="d" /> +> </Scope> +> <Scope> +> <Variable id="e" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="x"> +> <Scope></Scope> +> </Function> +> <Variable id="g" /> +> <Function id="zz"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Parameter id="c" /> +> <Scope> +> <Scope> +> <Variable id="a" /> +> </Scope> +> <Scope> +> <Variable id="fsdfsdfs" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="f"> +> <Scope></Scope> +> </Function> +> <Variable id="g" /> +> <Function id="x"> +> <Parameter id="y" /> +> <Scope> +> <Variable id="z" /> +> <Scope> +> <Variable id="r" /> +> </Scope> +> <Scope> +> <Variable id="f" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="f"> +> <Scope> +> <Variable id="x" /> +> <Scope> +> <Variable id="y" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="f"> +> <Parameter id="b" /> +> <Parameter id="c" /> +> <Scope> +> <Variable id="a" /> +> </Scope> +> </Function> +> <Function id="func"> +> <Parameter id="x" /> +> <Parameter id="y" /> +> <Scope> +> <Variable id="i" /> +> <Scope> +> <Variable id="z" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="function_1"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Scope> +> <Variable id="x" /> +> <Scope> +> <Variable id="c" /> +> </Scope> +> </Scope> +> </Function> +> <Function id="add"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Scope> +> <Variable id="c" /> +> </Scope> +> </Function> +> <Function id="sub"> +> <Parameter id="a" /> +> <Parameter id="b" /> +> <Scope> +> <Variable id="c" /> +> </Scope> +> </Function> +> <Function id="mult_by_5"> +> <Parameter id="a" /> +> <Scope> +> <Variable id="b" /> +> </Scope> +> </Function> +> <Function id="add_5"> +> <Parameter id="a" /> +> <Scope> +> <Variable id="i" /> +> <Scope> +> <Variable id="b" /> +> </Scope> +> </Scope> +> </Function> +> </Program> 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 @@ +<?xml version="1.0"?> +<Program> + <Variable id="a" /> + <Variable id="b" /> + <Variable id="c" /> + <Variable id="d" /> + <Variable id="e" /> + <Variable id="f" /> + <Function id="f"> + <Scope></Scope> + </Function> + <Function id="foo"> + <Parameter id="bar" /> + <Scope></Scope> + </Function> + <Function id="foo"> + <Parameter id="bar1" /> + <Parameter id="bar2" /> + <Scope> + <Variable id="x" /> + <Variable id="y" /> + <Variable id="z" /> + </Scope> + </Function> + <Function id="foo"> + <Parameter id="x" /> + <Parameter id="y" /> + <Scope> + <Scope></Scope> + </Scope> + </Function> + <Function id="f"> + <Parameter id="a" /> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Variable id="d" /> + <Scope> + <Variable id="e" /> + </Scope> + </Scope> + </Function> + <Function id="foo"> + <Parameter id="a" /> + <Scope> + <Scope> + <Variable id="z" /> + </Scope> + </Scope> + </Function> + <Function id="func"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + <Scope> + <Variable id="d" /> + </Scope> + <Scope> + <Variable id="e" /> + </Scope> + </Scope> + </Function> + <Function id="x"> + <Scope></Scope> + </Function> + <Variable id="g" /> + <Function id="zz"> + <Parameter id="a" /> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Scope> + <Variable id="a" /> + </Scope> + <Scope> + <Variable id="fsdfsdfs" /> + </Scope> + </Scope> + </Function> + <Function id="f"> + <Scope></Scope> + </Function> + <Variable id="g" /> + <Function id="x"> + <Parameter id="y" /> + <Scope> + <Variable id="z" /> + <Scope> + <Variable id="r" /> + </Scope> + <Scope> + <Variable id="f" /> + </Scope> + </Scope> + </Function> + <Function id="f"> + <Scope> + <Variable id="x" /> + <Scope> + <Variable id="y" /> + </Scope> + </Scope> + </Function> + <Function id="f"> + <Parameter id="b" /> + <Parameter id="c" /> + <Scope> + <Variable id="a" /> + </Scope> + </Function> + <Function id="func"> + <Parameter id="x" /> + <Parameter id="y" /> + <Scope> + <Variable id="i" /> + <Scope> + <Variable id="z" /> + </Scope> + </Scope> + </Function> + <Function id="function_1"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="x" /> + <Scope> + <Variable id="c" /> + </Scope> + </Scope> + </Function> + <Function id="add"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + </Scope> + </Function> + <Function id="sub"> + <Parameter id="a" /> + <Parameter id="b" /> + <Scope> + <Variable id="c" /> + </Scope> + </Function> + <Function id="mult_by_5"> + <Parameter id="a" /> + <Scope> + <Variable id="b" /> + </Scope> + </Function> + <Function id="add_5"> + <Parameter id="a" /> + <Scope> + <Variable id="i" /> + <Scope> + <Variable id="b" /> + </Scope> + </Scope> + </Function> +</Program> diff --git a/c_compiler/test/out/02.stderr.txt b/c_parser/test/out/.stderr.txt index e69de29..e69de29 100644 --- a/c_compiler/test/out/02.stderr.txt +++ b/c_parser/test/out/.stderr.txt 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 @@ +<?xml version="1.0"?> +<Program> +<Variable id="a" /> +<Variable id="b" /> +<Variable id="c" /> +<Variable id="d" /> +<Variable id="e" /> +<Variable id="f" /> +<Function id="f"> +<Scope> +</Scope> +</Function> +<Function id="foo"> +<Parameter id="bar" /> +<Scope> +</Scope> +</Function> +<Function id="foo"> +<Parameter id="bar1" /> +<Parameter id="bar2" /> +<Scope> +<Variable id="x" /> +<Variable id="y" /> +<Variable id="z" /> +</Scope> +</Function> +<Function id="foo"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Scope> +</Scope> +</Scope> +</Function> +<Function id="f"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="d" /> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +<Function id="foo"> +<Parameter id="a" /> +<Scope> +<Scope> +<Variable id="z" /> +</Scope> +</Scope> +</Function> +<Function id="func"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +<Scope> +<Variable id="d" /> +</Scope> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +<Function id="x"> +<Scope> +</Scope> +</Function> +<Variable id="g" /> +<Function id="zz"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Scope> +<Variable id="a" /> +</Scope> +<Scope> +<Variable id="fsdfsdfs" /> +</Scope> +</Scope> +</Function> +<Function id="f"> +<Scope> +</Scope> +</Function> +<Variable id="g" /> +<Function id="x"> +<Parameter id="y" /> +<Scope> +<Variable id="z" /> +<Scope> +<Variable id="r" /> +</Scope> +<Scope> +<Variable id="f" /> +</Scope> +</Scope> +</Function> +<Function id="f"> +<Scope> +<Variable id="x" /> +<Scope> +<Variable id="y" /> +</Scope> +</Scope> +</Function> +<Function id="f"> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="a" /> +</Scope> +</Function> +<Function id="func"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Variable id="i" /> +<Scope> +<Variable id="z" /> +</Scope> +</Scope> +</Function> +<Function id="function_1"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="x" /> +<Scope> +<Variable id="c" /> +</Scope> +</Scope> +</Function> +<Function id="add"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +</Scope> +</Function> +<Function id="sub"> +<Parameter id="a" /> +<Parameter id="b" /> +<Scope> +<Variable id="c" /> +</Scope> +</Function> +<Function id="mult_by_5"> +<Parameter id="a" /> +<Scope> +<Variable id="b" /> +</Scope> +</Function> +<Function id="add_5"> +<Parameter id="a" /> +<Scope> +<Variable id="i" /> +<Scope> +<Variable id="b" /> +</Scope> +</Scope> +</Function> +</Program> @@ -11,13 +11,17 @@ PARINC := -Ic_parser/include PARBUILDDIR := c_parser/build PARSRCDIR := c_parser/src +COMPINC := -Ic_compiler/include +COMPBUILDDIR := c_compiler/build +COMPSRCDIR := c_compiler/src + all : bin/c_lexer bin/c_parser # Make the c_lexer 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..." @@ -37,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..." @@ -62,6 +66,39 @@ $(PARSRCDIR)/c_parser.tab.cpp $(PARSRCDIR)/c_parser.tab.hpp : $(PARSRCDIR)/c_par @echo "Creating yacc..." @echo " bison -v -d $< -o $(PARSRCDIR)/c_parser.tab.cpp"; bison -v -d $< -o $(PARSRCDIR)/c_parser.tab.cpp +# Make the c_parser +bin/c_compiler: $(COMPBUILDDIR)/compiler_main.o $(COMPBUILDDIR)/statement.o \ + $(COMPBUILDDIR)/function.o $(COMPBUILDDIR)/expression.o \ + $(COMPBUILDDIR)/initializer.o $(COMPBUILDDIR)/translation_unit.o \ + $(COMPBUILDDIR)/declaration.o $(COMPBUILDDIR)/type.o \ + $(COMPBUILDDIR)/c_parser.tab.o $(COMPBUILDDIR)/c_lexer.yy.o + @echo "Linking..." + @echo " mkdir -p bin"; mkdir -p bin + @echo " $(CC) $^ -o $@"; $(CC) $^ -o $@ + +$(COMPBUILDDIR)/%.o : $(COMPSRCDIR)/%.cpp + @echo "Building sources..." + @echo " mkdir -p $(COMPBUILDDIR)"; mkdir -p $(COMPBUILDDIR) + @echo " $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $< + +$(COMPBUILDDIR)/%.yy.o : $(COMPSRCDIR)/%.yy.cpp + @echo "Building lex..." + @echo " mkdir -p $(COMPBUILDDIR)"; mkdir -p $(COMPBUILDDIR) + @echo " $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $< + +$(COMPBUILDDIR)/c_parser.tab.o : $(COMPSRCDIR)/c_parser.tab.cpp + @echo "Building yacc..." + @echo " mkdir -p $(COMPBUILDDIR)"; mkdir -p $(COMPBUILDDIR) + @echo " $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $<"; $(CC) $(CFLAGS) $(COMPINC) -c -o $@ $< + +$(COMPSRCDIR)/c_lexer.yy.cpp : $(COMPSRCDIR)/c_lexer.flex + @echo "Creating lex..." + @echo " flex -o $@ $<"; flex -o $@ $< + +$(COMPSRCDIR)/c_parser.tab.cpp $(COMPSRCDIR)/c_parser.tab.hpp : $(COMPSRCDIR)/c_parser.y + @echo "Creating yacc..." + @echo " bison -v -d $< -o $(COMPSRCDIR)/c_parser.tab.cpp"; bison -v -d $< -o $(COMPSRCDIR)/c_parser.tab.cpp + clean : @echo "Cleaning..." @echo " rm -r $(LEXBUILDDIR) bin $(LEXSRCDIR)/c_lexer.yy.cpp"; rm -r bin $(LEXBUILDDIR) $(LEXSRCDIR)/c_lexer.yy.cpp $(PARBUILDDIR) $(PARSRCDIR)/c_lexer.yy.cpp $(PARSRCDIR)/c_parser.tab.cpp $(PARSRCDIR)/c_parser.tab.hpp $(PARSRCDIR)/c_parser.output 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 "" |