aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/include
diff options
context:
space:
mode:
Diffstat (limited to 'c_compiler/include')
-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
10 files changed, 85 insertions, 245 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);
};