aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-05 20:55:12 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-03-05 20:55:12 +0000
commiteccb6c5360f213675a513e875bc424b8bdee16e7 (patch)
tree88b2826a2898223bcd5f02d5e5fd1f2c1c9b7e2a /c_compiler
parent6b7f5137edb18372bc3e3076dab1cb45c8e4c2ec (diff)
downloadCompiler-eccb6c5360f213675a513e875bc424b8bdee16e7.tar.gz
Compiler-eccb6c5360f213675a513e875bc424b8bdee16e7.zip
Finished ast change and it works
Diffstat (limited to 'c_compiler')
-rw-r--r--c_compiler/include/ast.hpp4
-rw-r--r--c_compiler/include/ast_top.hpp24
-rw-r--r--c_compiler/include/declaration.hpp23
-rw-r--r--c_compiler/include/expression.hpp7
-rw-r--r--c_compiler/include/function.hpp15
-rw-r--r--c_compiler/include/initializer.hpp12
-rw-r--r--c_compiler/include/node.hpp117
-rw-r--r--c_compiler/include/primitives.hpp41
-rw-r--r--c_compiler/include/statement.hpp66
-rw-r--r--c_compiler/include/translation_unit.hpp21
-rw-r--r--c_compiler/src/base.cpp7
-rw-r--r--c_compiler/src/c_parser.y236
-rw-r--r--c_compiler/src/compiler_main.cpp5
-rw-r--r--c_compiler/src/declaration.cpp51
-rw-r--r--c_compiler/src/expression.cpp9
-rw-r--r--c_compiler/src/function.cpp51
-rw-r--r--c_compiler/src/initializer.cpp22
-rw-r--r--c_compiler/src/primitives.cpp35
-rw-r--r--c_compiler/src/statement.cpp126
-rw-r--r--c_compiler/src/translation_unit.cpp32
20 files changed, 562 insertions, 342 deletions
diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp
index 5a31fca..86c9934 100644
--- a/c_compiler/include/ast.hpp
+++ b/c_compiler/include/ast.hpp
@@ -6,15 +6,13 @@
#include <iostream>
#include "node.hpp"
-//#include "expression.hpp"
-//#include "primitives.hpp"
+#include "expression.hpp"
#include "type.hpp"
#include "initializer.hpp"
#include "declaration.hpp"
#include "statement.hpp"
#include "function.hpp"
#include "translation_unit.hpp"
-//#include "ast_top.hpp"
TranslationUnit* parseAST();
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/declaration.hpp b/c_compiler/include/declaration.hpp
index 8eeb3c1..141d4d0 100644
--- a/c_compiler/include/declaration.hpp
+++ b/c_compiler/include/declaration.hpp
@@ -10,22 +10,21 @@ protected:
Type* type;
std::string id;
Initializer* init;
- Declaration* decl;
+ Declaration* next_decl;
+ Declaration* decl_list;
public:
- Declaration(const std::string& _id = "") : id(_id) {}
+ Declaration(const std::string& _id = "");
- virtual void print() const {
- if(decl != nullptr)
- decl->print();
- std::cout << id << std::endl;
- }
- virtual void printxml() const {}
- virtual void printasm() const {}
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
- void addDeclaration(Declaration* _decl) {
- decl = _decl;
- }
+ void addDeclaration(Declaration* _next_decl);
+ void addList(Declaration* _next_decl);
+
+ Declaration* getNext() const;
+ std::string getId() const;
};
#endif
diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp
index b9d2339..9e57fd4 100644
--- a/c_compiler/include/expression.hpp
+++ b/c_compiler/include/expression.hpp
@@ -4,11 +4,12 @@
#include "ast.hpp"
class Expression : public Node {
-private:
public:
- Expression(const Node* expr = new EmptyNode);
+ Expression(const Node* expr = nullptr);
- virtual void printasm() const override;
+ 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 cb68b2f..edfb958 100644
--- a/c_compiler/include/function.hpp
+++ b/c_compiler/include/function.hpp
@@ -1,4 +1,4 @@
-#ifndef AST_FUNCTION_HPP
+ #ifndef AST_FUNCTION_HPP
#define AST_FUNCTION_HPP
#include "ast.hpp"
@@ -12,16 +12,11 @@ protected:
Statement* statement;
public:
- Function(const std::string& _id, Declaration* _parameter_list) : id(_id), parameter_list(_parameter_list) {}
+ Function(const std::string& _id, Declaration* _parameter_list, Statement* _statement);
- virtual void print() const {
- std::cout << id << std::endl;
- if(parameter_list != nullptr)
- parameter_list->print();
- }
-
- virtual void printxml() const {}
- virtual void printasm() const {}
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
};
diff --git a/c_compiler/include/initializer.hpp b/c_compiler/include/initializer.hpp
index f28fbcc..f7c0dab 100644
--- a/c_compiler/include/initializer.hpp
+++ b/c_compiler/include/initializer.hpp
@@ -6,23 +6,23 @@
class Initializer : public Node {
public:
- Initializer() {}
+ Initializer();
- virtual void print() const {}
- virtual void printxml() const {}
- virtual void printasm() const {}
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
};
class Integer : public Initializer {
public:
- Integer() : Initializer() {}
+ Integer();
};
class StringLiteral : public Initializer {
public:
- StringLiteral() : Initializer() {}
+ StringLiteral();
};
diff --git a/c_compiler/include/node.hpp b/c_compiler/include/node.hpp
index 3390d5b..940a948 100644
--- a/c_compiler/include/node.hpp
+++ b/c_compiler/include/node.hpp
@@ -13,122 +13,5 @@ public:
virtual void printasm() const = 0;
};
-
-class EmptyNode : public Node {
-public:
- EmptyNode() {}
-
- virtual void print() const {}
- virtual void printxml() const {}
- virtual void printasm() const {}
-};
-
-
-class NodeList : public Node {
-protected:
- mutable std::vector<const Node*> list;
-
-public:
- NodeList(const Node* _var) {
- push(_var);
- }
-
- virtual ~NodeList() {
- for(auto& var : list) {
- delete var;
- }
- }
-
- virtual void print() const {
- for(auto&& declaration : list) {
- declaration->print();
- }
- }
-
- virtual void printxml() const {
- for(auto&& declaration : list) {
- declaration->printxml();
- }
- }
-
- virtual void printasm() const {
- for(auto&& declaration : list) {
- declaration->printasm();
- }
- }
-
- virtual void push(const Node* _var) const {
- list.push_back(_var);
- }
-};
-
-
-class NodeNode : public Node {
-protected:
- const Node* leftNode;
- const Node* rightNode;
-
-public:
- NodeNode(const Node* _left = new EmptyNode, const Node* _right = new EmptyNode)
- : leftNode(_left), rightNode(_right) {}
-
- virtual ~NodeNode() {
- delete leftNode;
- delete rightNode;
- }
-
- virtual void print() const {
- leftNode->print();
- rightNode->print();
- }
-
- virtual void printxml() const {
- leftNode->printxml();
- rightNode->printxml();
- }
-
- virtual void printasm() const {
- leftNode->printasm();
- rightNode->printasm();
- }
-
- virtual const Node* getLeft() const {
- return leftNode;
- }
-
- virtual const Node* getRight() const {
- return rightNode;
- }
-};
-
-
-class NodePrimitive : public Node {
-protected:
- std::string id;
- const Node* type;
-
-public:
- NodePrimitive(const std::string& _id = "", const Node* _type = new EmptyNode)
- : id(_id), type(_type) {}
-
- virtual ~NodePrimitive() {
- delete type;
- }
-
- virtual void print() const {}
- virtual void printxml() const {}
- virtual void printasm() const {}
-};
-
-
-class NodeType : public Node {
-public:
- NodeType() {}
-
- virtual void print() const {}
- virtual void printxml() const {}
- virtual void printasm() const {}
-};
-
#endif
diff --git a/c_compiler/include/primitives.hpp b/c_compiler/include/primitives.hpp
deleted file mode 100644
index d433072..0000000
--- a/c_compiler/include/primitives.hpp
+++ /dev/null
@@ -1,41 +0,0 @@
-#ifndef PRIMITIVES_HPP
-#define PRIMITIVES_HPP
-
-#include "ast.hpp"
-
-#include <cstdint>
-
-
-class ParamList : public BaseList {
-public:
- ParamList();
- ParamList(const Base* _param);
-};
-
-
-class Declarator : public BasePrimitive {
-public:
- Declarator(const std::string& _id);
-
- virtual void printxml() const;
-};
-
-
-class Parameter : public BasePrimitive {
-public:
- Parameter(const std::string& _id);
-
- virtual void printxml() const;
-};
-
-class Immediate : public BasePrimitive {
-protected:
- int32_t imm;
-public:
- Immediate(const int32_t& _imm);
-
- virtual void printasm() const override;
-};
-
-
-#endif
diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp
index ee4887a..dff9902 100644
--- a/c_compiler/include/statement.hpp
+++ b/c_compiler/include/statement.hpp
@@ -5,46 +5,82 @@
class Statement : public Node {
+protected:
+ Statement* next_statement;
public:
- //Statement(const Node* _left = new EmptyNode, const Node* _right = new EmptyNode);
- Statement() {}
+ Statement(Statement* statement = nullptr);
- virtual void print() const {}
- virtual void printxml() const {}
- virtual void printasm() const {}
+ virtual void print() const = 0;
+ virtual void printxml() const = 0;
+ virtual void printasm() const = 0;
+
+ void addStatement(Statement* _next) {
+ next_statement = _next;
+ }
};
-/*
class CompoundStatement : public Statement {
+protected:
+ Declaration* m_decl;
+ Statement* m_statement;
public:
- CompoundStatement(const Node* _dec = new EmptyNode, const Node* _statement = new EmptyNode);
+ CompoundStatement(Declaration* decl = nullptr, Statement* statement = nullptr);
+ CompoundStatement(Statement* statement);
- virtual void printxml() const override;
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
};
+
class SelectionStatement : public Statement {
+protected:
+ Statement* m_if;
+ Statement* m_else;
public:
- SelectionStatement(const Node* _if, const Node* _else = new EmptyNode);
+ SelectionStatement(Statement* _if = nullptr, Statement* _else = nullptr);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
};
+
class ExpressionStatement : public Statement {
+protected:
+ Expression* m_expr;
public:
- ExpressionStatement(const Node* expr = new EmptyNode);
+ ExpressionStatement(Expression* expr = nullptr);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
};
+
class JumpStatement : public Statement {
+protected:
+ Expression* m_expr;
public:
- JumpStatement(const Node* _el);
-
- virtual void printasm() const override;
+ JumpStatement(Expression* expr = nullptr);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
};
+
class IterationStatement : public Statement {
+protected:
+ Statement* m_statement;
public:
- IterationStatement(const Node* _el);
+ IterationStatement(Statement* statement);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
};
-*/
#endif
diff --git a/c_compiler/include/translation_unit.hpp b/c_compiler/include/translation_unit.hpp
index dd8ff03..42822f9 100644
--- a/c_compiler/include/translation_unit.hpp
+++ b/c_compiler/include/translation_unit.hpp
@@ -6,23 +6,16 @@
class TranslationUnit : public Node {
protected:
- std::vector<Node* > m_transUnit;
+ std::vector<Node* > translation_unit;
+
public:
- TranslationUnit(Node* decl) {
- m_transUnit.push_back(decl);
- }
+ TranslationUnit(Node* decl);
- virtual void print() const {
- for(auto& i : m_transUnit) {
- i->print();
- }
- }
- virtual void printxml() const {}
- virtual void printasm() const {}
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual void printasm() const;
- void push(Node* decl) {
- m_transUnit.push_back(decl);
- }
+ void push(Node* decl);
};
diff --git a/c_compiler/src/base.cpp b/c_compiler/src/base.cpp
deleted file mode 100644
index f2d9029..0000000
--- a/c_compiler/src/base.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-#include "ast.hpp"
-
-
-// Base List definition
-
-BaseList::BaseList()
-
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y
index 7a3899b..2864907 100644
--- a/c_compiler/src/c_parser.y
+++ b/c_compiler/src/c_parser.y
@@ -18,13 +18,15 @@ void yyerror(const char *);
Node* node;
TranslationUnit* trans_unit;
Function* function;
+ Statement* statement;
+ Declaration* declaration;
+ Expression* expression;
Type* type;
Initializer* initializer;
- Declaration* declaration;
double number;
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
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
@@ -33,6 +35,7 @@ void yyerror(const char *);
%nonassoc T_RRB
%nonassoc T_ELSE
+
%type <node> ExternalDeclaration
@@ -40,18 +43,30 @@ void yyerror(const char *);
%type <function> FunctionDefinition
-%type <declaration> Parameter Declaration InitDeclaratorList InitDeclarator ParameterList
+%type <statement> StatementList Statement CompoundStatement CompoundStatement_2
+ SelectionStatement
+ ExpressionStatement JumpStatement IterationStatement
+
+%type <declaration> ParameterList Parameter DeclarationList Declaration InitDeclaratorList
+ InitDeclarator
+ IdentifierList
-%type <type> DeclarationSpec
+%type <expression> Expression AssignmentExpression ConditionalExpression LogicalOrExpression
+ LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression
+ AndExpression EqualityExpression RelationalExpression ShiftExpression
+ AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression
+ PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression
+
+%type <type> DeclarationSpec DeclarationSpec_T
%type <string> Declarator DirectDeclarator
+
+%type <initializer> Constant
%type <number> T_INT_CONST
-
-%type <initializer> Initializer
-%type <string> T_IDENTIFIER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT
- T_MULT T_DIV T_REM
+%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
@@ -61,11 +76,11 @@ ROOT:
TranslationUnit { g_root = $1; }
;
-// TRANSLATION UNIT
+// EXTERNAL DEFINITION
TranslationUnit:
ExternalDeclaration { $$ = new TranslationUnit($1); }
- | TranslationUnit ExternalDeclaration { $$->push($2); }
+| TranslationUnit ExternalDeclaration { $$->push($2); }
;
ExternalDeclaration:
@@ -76,13 +91,13 @@ ExternalDeclaration:
// FUNCTION DEFINITION
FunctionDefinition:
- DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB T_SC { $$ = new Function(*$2, $4); }
+ DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function(*$2, $4, $6); }
;
ParameterList:
%empty { $$ = new Declaration(); }
| Parameter { $$ = $1; }
- | ParameterList T_CMA Parameter { $3->addDeclaration($$); $$ = $3; }
+ | ParameterList T_CMA Parameter { $3->addDeclaration($$); $$ = $3;}
;
Parameter:
@@ -91,6 +106,11 @@ Parameter:
// Declaration
+DeclarationList:
+ Declaration { $$ = $1; }
+ | DeclarationList Declaration { $2->addDeclaration($$); $$ = $2; }
+ ;
+
Declaration:
DeclarationSpec InitDeclaratorList T_SC { $$ = $2; }
;
@@ -107,19 +127,15 @@ DeclarationSpec_T:
;
InitDeclaratorList:
- InitDeclarator { $$ = $1; }
- | InitDeclaratorList T_CMA InitDeclarator { $3->addDeclaration($$); $$ = $3; }
+ InitDeclarator { $$ = new Declaration(*$1); }
+ | InitDeclaratorList T_CMA InitDeclarator { $3->addList($$); $$ = $3; }
;
InitDeclarator:
Declarator { $$ = new Declaration(*$1); }
- | Declarator T_EQ Initializer { $$ = new Declaration(*$1); }
+ | Declarator T_EQ AssignmentExpression { $$ = new Declaration(*$1); }
;
-Initializer:
- T_INT_CONST { $$ = new Initializer(); }
- ;
-
Declarator:
DirectDeclarator { $$ = $1; }
| T_MULT DirectDeclarator { $$ = $2; }
@@ -127,6 +143,188 @@ Declarator:
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 { $$ = $1; }
+ | StatementList Statement { $2->addStatement($$); $$ = $2; }
+ ;
+
+Statement:
+ CompoundStatement { $$ = $1; }
+ | SelectionStatement { $$ = $1; }
+ | ExpressionStatement { $$ = $1; }
+ | JumpStatement { $$ = $1; }
+ | IterationStatement { $$ = $1; }
+ ;
+
+CompoundStatement:
+ T_LCB CompoundStatement_2 { $$ = $2; }
+ ;
+
+CompoundStatement_2:
+ T_RCB { $$ = new CompoundStatement; }
+ | DeclarationList T_RCB { $$ = new CompoundStatement($1); }
+ | DeclarationList StatementList T_RCB { $$ = new CompoundStatement($1, $2); }
+ | StatementList T_RCB { $$ = new CompoundStatement($1); }
+ ;
+
+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); }
+ ;
+
+ExpressionStatement:
+ T_SC { $$ = new ExpressionStatement(); }
+| Expression T_SC { $$ = new ExpressionStatement(); }
+ ;
+
+JumpStatement:
+ T_RETURN ExpressionStatement { $$ = new JumpStatement(); }
+ ;
+
+IterationStatement:
+ T_WHILE T_LRB Expression T_RRB Statement { $$ = $5; }
+ | T_DO Statement T_WHILE T_LRB Expression T_RRB T_SC { $$ = $2; }
+ | T_FOR T_LRB Expression T_SC Expression T_SC Expression T_RRB Statement { $$ = $9; }
+ ;
+
+// Expressions
+
+Expression:
+ AssignmentExpression { $$ = $1; }
+ ;
+
+AssignmentExpression:
+ ConditionalExpression { $$ = $1; }
+ | UnaryExpression ASSIGN_OPER AssignmentExpression { $$ = $1; }
+ ;
+
+ASSIGN_OPER:
+ T_ASSIGN_OPER { ; }
+ | T_EQ { ; }
+ ;
+
+ConditionalExpression:
+ LogicalOrExpression { $$ = $1; }
+ | LogicalOrExpression T_QU Expression T_COL ConditionalExpression { $$ = $1; }
+ ;
+
+LogicalOrExpression:
+ LogicalAndExpression { $$ = $1; }
+ | LogicalOrExpression T_LOG_OR LogicalAndExpression { $$ = $3; }
+ ;
+
+LogicalAndExpression:
+ InclusiveOrExpression { $$ = $1; }
+ | LogicalAndExpression T_LOG_AND InclusiveOrExpression { $$ = $3; }
+ ;
+
+InclusiveOrExpression:
+ ExclusiveOrExpression { $$ = $1; }
+ | InclusiveOrExpression T_OR ExclusiveOrExpression { $$ = $3; }
+ ;
+
+ExclusiveOrExpression:
+ AndExpression { $$ = $1; }
+ | ExclusiveOrExpression T_XOR AndExpression { $$ = $3; }
+ ;
+
+AndExpression:
+ EqualityExpression { $$ = $1; }
+ | AndExpression T_AND EqualityExpression { $$ = $3; }
+ ;
+
+EqualityExpression:
+ RelationalExpression { $$ = $1; }
+ | EqualityExpression T_EQUALITY_OP RelationalExpression { $$ = $3; }
+ ;
+
+RelationalExpression:
+ ShiftExpression { $$ = $1; }
+ | RelationalExpression T_REL_OP ShiftExpression { $$ = $3; }
+ ;
+
+ShiftExpression:
+ AdditiveExpression { $$ = $1; }
+ | ShiftExpression T_SHIFT_OP AdditiveExpression { $$ = $3; }
+ ;
+
+AdditiveExpression:
+ MultiplicativeExpression { $$ = $1; }
+ | AdditiveExpression T_ADDSUB_OP MultiplicativeExpression { $$ = $3; }
+ ;
+
+MultiplicativeExpression:
+ CastExpression { $$ = $1; }
+ | MultiplicativeExpression MultDivRemOP CastExpression { $$ = $3; }
+ ;
+
+MultDivRemOP:
+ T_MULT { $$ = $1; }
+ | T_DIV { $$ = $1; }
+ | T_REM { $$ = $1; }
+ ;
+
+CastExpression:
+ UnaryExpression { $$ = $1; }
+ | T_LRB T_TYPE_SPEC T_RRB CastExpression { $$ = $4; }
+ ;
+
+UnaryExpression:
+ PostfixExpression { $$ = $1; }
+ | T_INCDEC UnaryExpression { $$ = $2; }
+ | UnaryOperator CastExpression { $$ = $2; }
+ | T_SIZEOF UnaryExpression { $$ = $2; }
+ | T_SIZEOF T_LRB T_TYPE_SPEC T_RRB { $$ = new Expression(); }
+ ;
+
+UnaryOperator:
+ T_AND { $$ = $1; }
+ | T_ADDSUB_OP { $$ = $1; }
+ | T_MULT { $$ = $1; }
+ | T_TILDE { $$ = $1; }
+ | T_NOT { $$ = $1; }
+ ;
+
+PostfixExpression:
+ PrimaryExpression { $$ = $1; }
+ | PostfixExpression T_LSB Expression T_RSB { $$ = $3; }
+ | PostfixExpression T_LRB PostfixExpression2 { $$ = $3; }
+ | PostfixExpression T_DOT T_IDENTIFIER { $$ = new Expression(); }
+ | PostfixExpression T_ARROW T_IDENTIFIER { $$ = new Expression(); }
+ | PostfixExpression T_INCDEC { $$ = new Expression(); }
+ ;
+
+PostfixExpression2:
+ T_RRB { $$ = new Expression(); }
+ | ArgumentExpressionList T_RRB { $$ = new Expression; }
+ ;
+
+ArgumentExpressionList:
+ AssignmentExpression { $$ = new Expression; }
+ | ArgumentExpressionList T_CMA AssignmentExpression { $$ = new Expression; }
+ ;
+
+PrimaryExpression:
+ T_IDENTIFIER { $$ = new Expression(); }
+ | Constant { $$ = new Expression($1); }
+ | T_LRB Expression T_RRB { $$ = $2; }
+ ;
+
+Constant:
+ T_INT_CONST { $$ = new Initializer(); }
;
%%
diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp
index 8dce76f..a4df8a7 100644
--- a/c_compiler/src/compiler_main.cpp
+++ b/c_compiler/src/compiler_main.cpp
@@ -2,10 +2,11 @@
#include <iostream>
-int main(int argc, char *argv[]) {
+int main(int argc, char *argv[])
+{
TranslationUnit* ast = parseAST();
- ast->print();
+ ast->printxml();
return 0;
}
diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp
new file mode 100644
index 0000000..5341b5b
--- /dev/null
+++ b/c_compiler/src/declaration.cpp
@@ -0,0 +1,51 @@
+#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(decl_list != nullptr) {
+ decl_list->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)
+{
+ decl_list = _next_decl;
+}
+
+Declaration* Declaration::getNext() const
+{
+ return next_decl;
+}
+
+std::string Declaration::getId() const
+{
+ return id;
+}
diff --git a/c_compiler/src/expression.cpp b/c_compiler/src/expression.cpp
index cf2b58f..5eab857 100644
--- a/c_compiler/src/expression.cpp
+++ b/c_compiler/src/expression.cpp
@@ -3,9 +3,8 @@
// Expression definition
-Expression::Expression(const Base* _expr)
- : BaseNode(_expr) {}
+Expression::Expression(const Node* expr) {}
-void Expression::printasm() const {
- leftNode->printasm();
-}
+void Expression::print() const {}
+void Expression::printxml() const {}
+void Expression::printasm() const {}
diff --git a/c_compiler/src/function.cpp b/c_compiler/src/function.cpp
index 420c782..a132280 100644
--- a/c_compiler/src/function.cpp
+++ b/c_compiler/src/function.cpp
@@ -1,18 +1,47 @@
#include "ast.hpp"
-Function::Function(const std::string& _id, const BaseList* _param_list,
- const BaseNode* _comp_statement)
- : BaseNode(_param_list, _comp_statement), id(_id) {}
-void Function::printxml() const {
+// 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;
- leftNode->printxml();
- rightNode->printxml();
+
+ 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 {
- std::cout << id << ":\n\taddiu\t$sp,$sp,-24\n\tsw\t$fp,20($sp)\n\tmove\t$fp,$sp" << std::endl;
- rightNode->printasm();
- std::cout << "\tmove\t$sp,$fp\n\tlw\t$fp,20($sp)\n\taddiu\t$sp,$sp,24\n\tjr\t$31\n\tnop" << std::endl;
-}
+void Function::printasm() const
+{}
diff --git a/c_compiler/src/initializer.cpp b/c_compiler/src/initializer.cpp
new file mode 100644
index 0000000..61a0413
--- /dev/null
+++ b/c_compiler/src/initializer.cpp
@@ -0,0 +1,22 @@
+#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/primitives.cpp b/c_compiler/src/primitives.cpp
deleted file mode 100644
index e3ec0eb..0000000
--- a/c_compiler/src/primitives.cpp
+++ /dev/null
@@ -1,35 +0,0 @@
-#include "ast.hpp"
-
-
-// Parameter list definition (used in functions)
-
-ParamList::ParamList() : BaseList() {}
-
-ParamList::ParamList(const Base* _param) : BaseList(_param) {}
-
-
-// Declarator defintion, used when declaring a variable
-
-Declarator::Declarator(const std::string& _id) : BasePrimitive(_id) {}
-
-void Declarator::printxml() const {
- std::cout << "<Variable id=\"" << id << "\" />" << std::endl;
-}
-
-
-// Parameter class defition
-
-Parameter::Parameter(const std::string& _id) : BasePrimitive(_id) {}
-
-void Parameter::printxml() const {
- std::cout << "<Parameter id=\"" << id << "\" />" << std::endl;
-}
-
-
-// Immediate class definition, used when loading an immediate value
-
-Immediate::Immediate(const int32_t& _imm) : BasePrimitive(), imm(_imm) {}
-
-void Immediate::printasm() const {
- std::cout << "\tli\t$2," << imm << "\n\tsw\t$2,8($fp)" << std::endl;
-}
diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp
index e828eaa..28bd981 100644
--- a/c_compiler/src/statement.cpp
+++ b/c_compiler/src/statement.cpp
@@ -3,51 +3,141 @@
// General base Statement definition
-Statement::Statement(const Base* _left, const Base* _right)
- : BaseNode(_left, _right) {}
+Statement::Statement(Statement* statement)
+ : next_statement(statement) {}
+void Statement::print() const
+{
+ if(next_statement != nullptr)
+ next_statement->print();
+}
-// Statement list definition
+void Statement::printxml() const
+{
+ if(next_statement != nullptr)
+ next_statement->printxml();
+}
-StatementList::StatementList(const Base* _statement)
- : BaseList(_statement) {}
+void Statement::printasm() const
+{
+ if(next_statement != nullptr)
+ next_statement->printasm();
+}
// Compound Statement definition
-CompoundStatement::CompoundStatement(const Base* _dec, const Base* _statement)
- : Statement(_dec, _statement) {}
+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 {
+void CompoundStatement::printxml() const
+{
+ if(next_statement != nullptr)
+ next_statement->printxml();
+
std::cout << "<Scope>" << std::endl;
- leftNode->printxml();
- rightNode->printxml();
+
+ if(m_decl != nullptr)
+ m_decl->printxml();
+
+ if(m_statement != nullptr)
+ m_statement->printxml();
+
std::cout << "</Scope>" << std::endl;
}
+void CompoundStatement::printasm() const
+{}
+
// Selection Statement definition
-SelectionStatement::SelectionStatement(const Base* _if, const Base* _else)
- : Statement(_if, _else) {}
+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
+{}
// Expression Statement definition
-ExpressionStatement::ExpressionStatement(const Base* _expr)
- : Statement(_expr) {}
+ExpressionStatement::ExpressionStatement(Expression* expr)
+ : Statement(), m_expr(expr) {}
+
+void ExpressionStatement::print() const
+{}
+
+void ExpressionStatement::printxml() const
+{}
+
+void ExpressionStatement::printasm() const
+{}
// Jump Statement definition
-JumpStatement::JumpStatement(const Base* _el) : Statement(_el) {}
+JumpStatement::JumpStatement(Expression* expr)
+ : m_expr(expr) {}
+
+void JumpStatement::print() const
+{}
-void JumpStatement::printasm() const {
- leftNode->printasm();
+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;
}
// Iteration Statement definition
-IterationStatement::IterationStatement(const Base* _el) : Statement(_el) {}
+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
+{}
diff --git a/c_compiler/src/translation_unit.cpp b/c_compiler/src/translation_unit.cpp
new file mode 100644
index 0000000..c1e4c13
--- /dev/null
+++ b/c_compiler/src/translation_unit.cpp
@@ -0,0 +1,32 @@
+#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);
+}