diff options
Diffstat (limited to 'c_compiler')
-rw-r--r-- | c_compiler/include/ast.hpp | 6 | ||||
-rw-r--r-- | c_compiler/include/declaration.hpp | 24 | ||||
-rw-r--r-- | c_compiler/include/expression.hpp | 4 | ||||
-rw-r--r-- | c_compiler/include/external.hpp | 13 | ||||
-rw-r--r-- | c_compiler/include/function.hpp | 11 | ||||
-rw-r--r-- | c_compiler/include/initializer.hpp | 22 | ||||
-rw-r--r-- | c_compiler/include/node.hpp (renamed from c_compiler/include/base.hpp) | 42 | ||||
-rw-r--r-- | c_compiler/include/statement.hpp | 19 | ||||
-rw-r--r-- | c_compiler/include/translation_unit.hpp | 15 | ||||
-rw-r--r-- | c_compiler/include/type.hpp | 13 | ||||
-rw-r--r-- | c_compiler/src/c_parser.y | 79 |
11 files changed, 154 insertions, 94 deletions
diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp index bb3e92f..4a57282 100644 --- a/c_compiler/include/ast.hpp +++ b/c_compiler/include/ast.hpp @@ -5,13 +5,15 @@ #include <string> #include <iostream> -#include "base.hpp" +#include "node.hpp" #include "statement.hpp" #include "function.hpp" #include "declaration.hpp" #include "expression.hpp" #include "primitives.hpp" -#include "external.hpp" +#include "type.hpp" +#include "initializer.hpp" +#include "translation_unit.hpp" #include "ast_top.hpp" const BaseList* parseAST(); diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp index bf72f2d..7fdee1c 100644 --- a/c_compiler/include/declaration.hpp +++ b/c_compiler/include/declaration.hpp @@ -5,19 +5,23 @@ // Declaration that holds a list of declarations -class Declaration : public BaseNode { +class Declaration : public Node { +protected: + Type* type; + std::string id; + Initializer* init; + Declaration* decl; + public: - Declaration(const Base* _var) : BaseNode(_var) {} -}; + Declaration(const Type* _type = nullptr, + const std::string _id = "", + const Initializer* _init = nullptr); -class DeclarationList : public BaseList { -public: - DeclarationList(const Base* _var) : BaseList(_var) {} -}; + virtual void print() const; + virtual void printxml() const; + virtual void printasm() const; -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 1e2bf04..b9d2339 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -3,10 +3,10 @@ #include "ast.hpp" -class Expression : public BaseNode { +class Expression : public Node { private: public: - Expression(const Base* expr = new EmptyNode); + Expression(const Node* expr = new EmptyNode); virtual void printasm() const override; }; diff --git a/c_compiler/include/external.hpp b/c_compiler/include/external.hpp deleted file mode 100644 index 866e18a..0000000 --- a/c_compiler/include/external.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#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 b1ee88d..c58699c 100644 --- a/c_compiler/include/function.hpp +++ b/c_compiler/include/function.hpp @@ -4,15 +4,22 @@ #include "ast.hpp" -class Function : public BaseNode { +class Function : public Node { protected: + const Type* type; std::string id; + const DeclarationList* parameter_list; + const Statement* statement; public: - Function(const std::string& _id, const BaseList* _param_list, const BaseNode* _comp_statement); + Function(const std::string& _id, const Statement* _comp_statement); + + virtual ~Function(); virtual void printxml() const override; virtual void printasm() const override; + + void push_parameter(const Declaration* declaration) const; }; diff --git a/c_compiler/include/initializer.hpp b/c_compiler/include/initializer.hpp new file mode 100644 index 0000000..723f7aa --- /dev/null +++ b/c_compiler/include/initializer.hpp @@ -0,0 +1,22 @@ +#ifndef INITIALIZER_HPP +#define INITIALIZER_HPP + +#include "ast.hpp" + + +class Initializer : public Node { + +}; + + +class Integer : public Initializer { + +}; + + +class StringLiteral : public Initializer { + +}; + + +#endif diff --git a/c_compiler/include/base.hpp b/c_compiler/include/node.hpp index 2ea3e4e..3390d5b 100644 --- a/c_compiler/include/base.hpp +++ b/c_compiler/include/node.hpp @@ -4,9 +4,9 @@ #include "ast.hpp" -class Base { +class Node { public: - virtual ~Base() {} + virtual ~Node() {} virtual void print() const = 0; virtual void printxml() const = 0; @@ -14,7 +14,7 @@ public: }; -class EmptyNode : public Base { +class EmptyNode : public Node { public: EmptyNode() {} @@ -24,16 +24,16 @@ public: }; -class BaseList : public Base { +class NodeList : public Node { protected: - mutable std::vector<const Base*> list; + mutable std::vector<const Node*> list; public: - BaseList(const Base* _var) { + NodeList(const Node* _var) { push(_var); } - virtual ~BaseList() { + virtual ~NodeList() { for(auto& var : list) { delete var; } @@ -57,22 +57,22 @@ public: } } - virtual void push(const Base* _var) const { + virtual void push(const Node* _var) const { list.push_back(_var); } }; -class BaseNode : public Base { +class NodeNode : public Node { protected: - const Base* leftNode; - const Base* rightNode; + const Node* leftNode; + const Node* rightNode; public: - BaseNode(const Base* _left = new EmptyNode, const Base* _right = new EmptyNode) + NodeNode(const Node* _left = new EmptyNode, const Node* _right = new EmptyNode) : leftNode(_left), rightNode(_right) {} - virtual ~BaseNode() { + virtual ~NodeNode() { delete leftNode; delete rightNode; } @@ -92,26 +92,26 @@ public: rightNode->printasm(); } - virtual const Base* getLeft() const { + virtual const Node* getLeft() const { return leftNode; } - virtual const Base* getRight() const { + virtual const Node* getRight() const { return rightNode; } }; -class BasePrimitive : public Base { +class NodePrimitive : public Node { protected: std::string id; - const Base* type; + const Node* type; public: - BasePrimitive(const std::string& _id = "", const Base* _type = new EmptyNode) + NodePrimitive(const std::string& _id = "", const Node* _type = new EmptyNode) : id(_id), type(_type) {} - virtual ~BasePrimitive() { + virtual ~NodePrimitive() { delete type; } @@ -121,9 +121,9 @@ public: }; -class BaseType : public Base { +class NodeType : public Node { public: - BaseType() {} + NodeType() {} virtual void print() const {} virtual void printxml() const {} diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp index bf15aa5..e715937 100644 --- a/c_compiler/include/statement.hpp +++ b/c_compiler/include/statement.hpp @@ -4,43 +4,38 @@ #include "ast.hpp" -class Statement : public BaseNode { +class Statement : public Node { public: - Statement(const Base* _left = new EmptyNode, const Base* _right = new EmptyNode); -}; - -class StatementList : public BaseList { -public: - StatementList(const Base* _statement); + Statement(const Node* _left = new EmptyNode, const Node* _right = new EmptyNode); }; class CompoundStatement : public Statement { public: - CompoundStatement(const Base* _dec = new EmptyNode, const Base* _statement = new EmptyNode); + CompoundStatement(const Node* _dec = new EmptyNode, const Node* _statement = new EmptyNode); virtual void printxml() const override; }; class SelectionStatement : public Statement { public: - SelectionStatement(const Base* _if, const Base* _else = new EmptyNode); + SelectionStatement(const Node* _if, const Node* _else = new EmptyNode); }; class ExpressionStatement : public Statement { public: - ExpressionStatement(const Base* expr = new EmptyNode); + ExpressionStatement(const Node* expr = new EmptyNode); }; class JumpStatement : public Statement { public: - JumpStatement(const Base* _el); + JumpStatement(const Node* _el); virtual void printasm() const override; }; class IterationStatement : public Statement { public: - IterationStatement(const Base* _el); + IterationStatement(const Node* _el); }; #endif diff --git a/c_compiler/include/translation_unit.hpp b/c_compiler/include/translation_unit.hpp new file mode 100644 index 0000000..6601994 --- /dev/null +++ b/c_compiler/include/translation_unit.hpp @@ -0,0 +1,15 @@ +#ifndef EXTERNAL_HPP +#define EXTERNAL_HPP + +#include "ast.hpp" + + +class TranslationUnit : public Node { +protected: + // TODO includes all the variable declarations and function definitions +public: + TranslationUnit() {} +}; + + +#endif diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp new file mode 100644 index 0000000..8b24b30 --- /dev/null +++ b/c_compiler/include/type.hpp @@ -0,0 +1,13 @@ +#ifndef TYPE_HPP +#define TYPE_HPP + +#include "ast.hpp" + + +class Type : public Node { +public: + Type(); +}; + + +#endif diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y index 374ee46..39bc13c 100644 --- a/c_compiler/src/c_parser.y +++ b/c_compiler/src/c_parser.y @@ -2,7 +2,7 @@ #include "ast.hpp" -extern const BaseList* g_root; // A way of getting the AST out +extern const 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 @@ -15,12 +15,13 @@ void yyerror(const char *); // Represents the value associated with any kind of // AST node. %union{ - const BaseNode* base_node; - const BaseList* base_list; - const BasePrimitive* base_prim; - const BaseType* base_type; + Node* node; + TranslationUnit* trans_unit; + Function* function; + Type* type; + Declaration* declaration; double number; - std::string *string; + std::string* string; } %token T_TYPE_SPEC T_TYPE_QUAL T_STRG_SPEC T_IDENTIFIER T_SC T_CMA T_LRB T_LCB T_RCB @@ -33,10 +34,24 @@ void yyerror(const char *); %nonassoc T_ELSE -%type <base_list> ExtDef ParameterList DeclarationList InitDeclaratorList IdentifierList +%type <base_list> ExtDef DeclarationList InitDeclaratorList IdentifierList StatementList ArgumentExpressionList -%type <base_node> ExtDeclaration FuncDef Declaration DeclarationSpec DeclarationSpec_T +%type <node> ExternalDeclaration + +%type <trans_unit> TranslationUnit + +%type <function> FunctionDefinition + +%type <declaration> Parameter ParamDeclarator Declaration + +%type <decl_list> ParameterList + +%type <type> DeclarationSpec + +%type <string> Declarator DirectDeclarator + +%type <base_node> ExtDeclaration Declaration DeclarationSpec DeclarationSpec_T Statement CompoundStatement CompoundStatement_2 PrimaryExpression SelectionStatement ExpressionStatement JumpStatement IterationStatement Expression AssignmentExpression ConditionalExpression LogicalOrExpression @@ -45,7 +60,7 @@ void yyerror(const char *); AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression PostfixExpression PostfixExpression2 -%type <base_prim> Parameter ParamDeclarator Declarator DirectDeclarator InitDeclarator Constant +%type <base_prim> Declarator DirectDeclarator InitDeclarator Constant %type <number> T_INT_CONST @@ -58,39 +73,35 @@ void yyerror(const char *); %% ROOT: - ExtDef { g_root = $1; } + TranslationUnit { g_root = $1; } ; -// EXTERNAL DEFINITION +// TRANSLATION UNIT -ExtDef: - ExtDeclaration { $$ = new ExternalDefinition($1); } -| ExtDef ExtDeclaration { $$->push($2); } +TranslationUnit: + ExternalDeclaration { $$ = new TranslationUnit($1); } + | TranslationUnit ExternalDeclaration { $$->push($2); } ; -ExtDeclaration: +ExternalDeclaration: Declaration { $$ = $1; } - | FuncDef { $$ = $1; } + | FunctionDefinition { $$ = $1; } ; // FUNCTION DEFINITION -FuncDef: - DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); } +FunctionDefinition: + DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function($1, $2, $4, $6); } ; ParameterList: - %empty { $$ = new ParamList(); } - | Parameter { $$ = new ParamList($1); } + %empty { $$ = new DeclarationList(); } + | Parameter { $$ = new DeclarationList($1); } | ParameterList T_CMA Parameter { $$->push($3); } ; Parameter: - DeclarationSpec ParamDeclarator { $$ = $2; } - ; - -ParamDeclarator: - T_IDENTIFIER { $$ = new Parameter(*$1);} + DeclarationSpec T_IDENTIFIER { $$ = new Declaration($1, *$2); } ; // Declaration @@ -116,22 +127,26 @@ DeclarationSpec_T: ; InitDeclaratorList: - InitDeclarator { $$ = new InitDeclaratorList($1); } - | InitDeclaratorList T_CMA InitDeclarator { $$->push($3); } + InitDeclarator { $$ = $1; } + | InitDeclarator T_CMA InitDeclaratorList { $1->addDeclaration($$); $$ = $1; } ; InitDeclarator: - Declarator { $$ = $1; } - | Declarator T_EQ AssignmentExpression { $$ = $1; } + Declarator { $$ = new Declaration(*$1); } + | Declarator T_EQ Initializer { $$ = new Declaration(*$1, $3); } ; +Initializer: + T_INT_CONST { $$ = new Initializer(); } + ; + Declarator: DirectDeclarator { $$ = $1; } | T_MULT DirectDeclarator { $$ = $2; } ; DirectDeclarator: - T_IDENTIFIER { $$ = new Declarator(*$1); } + T_IDENTIFIER { $$ = $1; } | T_LRB Declarator T_RRB { $$ = $2; } | DirectDeclarator T_LSB ConditionalExpression T_RSB { $$ = $1; } | DirectDeclarator T_LSB T_RSB { $$ = $1; } @@ -319,9 +334,9 @@ Constant: %% -const BaseList* g_root; // Definition of variable (to match declaration earlier) +const TranslationUnit* g_root; // Definition of variable (to match declaration earlier) -const BaseList* parseAST() { +const TranslationUnit* parseAST() { g_root = 0; yyparse(); return g_root; |