diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-02 23:22:51 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-03-02 23:22:51 +0000 |
commit | 446c2394ec8970198d645bbbb462c67b9e3f1b1e (patch) | |
tree | fc85d32c2b68efa80910d0a4ce4c1bed78ec4717 /c_compiler/include | |
parent | 34d69709e621b9609833a3d6bae31195b425f2f8 (diff) | |
download | Compiler-446c2394ec8970198d645bbbb462c67b9e3f1b1e.tar.gz Compiler-446c2394ec8970198d645bbbb462c67b9e3f1b1e.zip |
Changing ast structure again
Diffstat (limited to 'c_compiler/include')
-rw-r--r-- | c_compiler/include/ast.hpp | 7 | ||||
-rw-r--r-- | c_compiler/include/base.hpp | 130 | ||||
-rw-r--r-- | c_compiler/include/declaration.hpp | 43 | ||||
-rw-r--r-- | c_compiler/include/expression.hpp | 18 | ||||
-rw-r--r-- | c_compiler/include/external.hpp | 13 | ||||
-rw-r--r-- | c_compiler/include/function.hpp | 46 | ||||
-rw-r--r-- | c_compiler/include/primitives.hpp | 31 | ||||
-rw-r--r-- | c_compiler/include/statement.hpp | 55 |
8 files changed, 199 insertions, 144 deletions
diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp index 9d51816..bb3e92f 100644 --- a/c_compiler/include/ast.hpp +++ b/c_compiler/include/ast.hpp @@ -1,14 +1,19 @@ #ifndef AST_HPP #define AST_HPP +#include <vector> +#include <string> +#include <iostream> + #include "base.hpp" #include "statement.hpp" #include "function.hpp" #include "declaration.hpp" #include "expression.hpp" #include "primitives.hpp" +#include "external.hpp" #include "ast_top.hpp" -ast_Top *parseAST(); +const BaseList* parseAST(); #endif diff --git a/c_compiler/include/base.hpp b/c_compiler/include/base.hpp index 9451609..ae7cfd7 100644 --- a/c_compiler/include/base.hpp +++ b/c_compiler/include/base.hpp @@ -1,16 +1,138 @@ #ifndef AST_BASE_HPP #define AST_BASE_HPP -#include <string> -#include <iostream> -#include <vector> +#include "ast.hpp" + class Base { public: virtual ~Base() {} virtual void print() const = 0; - virtual void push(const Base* _var) const = 0; + virtual void printxml() const = 0; + virtual void printasm() const = 0; +}; + + +class EmptyNode : public Base { +public: + EmptyNode() {} + + virtual void print() const {} + virtual void printxml() const {} + virtual void printasm() const {} +}; + + +class BaseList : public Base { +protected: + mutable std::vector<const Base*> list; + +public: + BaseList() {} + BaseList(const Base* _var) { + push(_var); + } + + virtual ~BaseList() { + for(auto& var : list) { + delete var; + } + } + + virtual void print() const { + for(auto&& declaration : list) { + declaration->print(); + } + } + + virtual void printxml() const { + for(auto&& declaration : list) { + declaration->printxml(); + } + } + + virtual void printasm() const { + for(auto&& declaration : list) { + declaration->printasm(); + } + } + + virtual void push(const Base* _var) const { + list.push_back(_var); + } +}; + + +class BaseNode : public Base { +protected: + const Base* leftNode; + const Base* rightNode; + +public: + BaseNode(const Base* _left = new EmptyNode, const Base* _right = new EmptyNode) + : leftNode(_left), rightNode(_right) {} + + virtual ~BaseNode() { + delete leftNode; + delete rightNode; + } + + virtual void print() const { + leftNode->print(); + rightNode->print(); + } + + virtual void printxml() const { + leftNode->printxml(); + rightNode->printxml(); + } + + virtual void printasm() const { + leftNode->printasm(); + rightNode->printasm(); + } + + virtual const Base* getLeft() const { + return leftNode; + } + + virtual const Base* getRight() const { + return rightNode; + } +}; + + +class BasePrimitive : public Base { +protected: + std::string id; + const Base* type; + +public: + BasePrimitive(const std::string& _id) + : id(_id), type(new EmptyNode) {} + + BasePrimitive(const std::string& _id, const Base* _type) + : id(_id), type(_type) {} + + virtual ~BasePrimitive() { + delete type; + } + + virtual void print() const {} + virtual void printxml() const {} + virtual void printasm() const {} +}; + + +class BaseType : public Base { +public: + BaseType() {} + + virtual void print() const {} + virtual void printxml() const {} + virtual void printasm() const {} }; + #endif diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp index bfd3070..bf72f2d 100644 --- a/c_compiler/include/declaration.hpp +++ b/c_compiler/include/declaration.hpp @@ -3,48 +3,21 @@ #include "ast.hpp" -#include <vector> - // Declaration that holds a list of declarations -class DeclarationList : public Base { -private: - mutable std::vector<const Base*> dec_list; - +class Declaration : public BaseNode { public: - DeclarationList(const Base* _dec) { - dec_list.push_back(_dec); - } - - virtual void print() const { - for(size_t i = 0; i < dec_list.size(); ++i) { - dec_list[i]->print(); - } - } - - virtual void push(const Base* _dec) const { - dec_list.push_back(_dec); - } + Declaration(const Base* _var) : BaseNode(_var) {} }; -class VariableDeclaration : public Base { -private: - mutable std::vector<const Base*> var_list; - +class DeclarationList : public BaseList { public: - VariableDeclaration(const Base* _var) { - var_list.push_back(_var); - } - - virtual void print() const { - for(size_t i = 0; i < var_list.size(); ++i) { - var_list[i]->print(); - } - } + DeclarationList(const Base* _var) : BaseList(_var) {} +}; - virtual void push(const Base* _var) const { - var_list.push_back(_var); - } +class InitDeclaratorList : public BaseList { +public: + InitDeclaratorList(const Base* _var) : BaseList(_var) {} }; #endif diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp index f67d2d7..9eb9efd 100644 --- a/c_compiler/include/expression.hpp +++ b/c_compiler/include/expression.hpp @@ -3,22 +3,14 @@ #include "ast.hpp" -#include <string> -#include <iostream> - -class Expression : public Base { +class Expression : public BaseNode { private: public: - Expression() {} - - virtual void print() const { - - } + Expression() : BaseNode() {} - virtual void push(const Base* _base) const { - std::cerr << "Can't call this function for this type" << std::endl; - (void)_base; - } + virtual void print() const override {} + virtual void printxml() const override {} + virtual void printasm() const override {} }; #endif diff --git a/c_compiler/include/external.hpp b/c_compiler/include/external.hpp new file mode 100644 index 0000000..866e18a --- /dev/null +++ b/c_compiler/include/external.hpp @@ -0,0 +1,13 @@ +#ifndef EXTERNAL_HPP +#define EXTERNAL_HPP + +#include "ast.hpp" + + +class ExternalDefinition : public BaseList { +public: + ExternalDefinition(const Base* _var) : BaseList(_var) {} +}; + + +#endif diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp index 6fbcdee..955420d 100644 --- a/c_compiler/include/function.hpp +++ b/c_compiler/include/function.hpp @@ -3,51 +3,29 @@ #include "ast.hpp" -#include <string> -#include <iostream> -class Function : public Base { -private: +class Function : public BaseNode { +protected: std::string id; - const Base* param; - const Base* comp_statement; + public: - Function(const std::string& _id, const Base* _param, const Base* _comp_statement) : - id(_id), param(_param), comp_statement(_comp_statement) {} + Function(const std::string& _id, const BaseList* _param_list, const BaseNode* _comp_statement) + : BaseNode(_param_list, _comp_statement), id(_id) {} - virtual void print() const { + virtual void printxml() const override { std::cout << "<Function id=\"" << id << "\">" << std::endl; - param->print(); - comp_statement->print(); + leftNode->printxml(); + rightNode->printxml(); 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; - } }; -class ParamList : public Base { -private: - mutable std::vector<const Base*> param_list; +class ParamList : public BaseList { public: - ParamList() {} - - ParamList(const Base* param) { - param_list.push_back(param); - } - - virtual void print() const { - for(size_t i = 0; i < param_list.size(); ++i) { - param_list[i]->print(); - } - } - - virtual void push(const Base* _var) const { - param_list.push_back(_var); - } + ParamList() : BaseList() {} + ParamList(const Base* _param) : BaseList(_param) {} }; + #endif diff --git a/c_compiler/include/primitives.hpp b/c_compiler/include/primitives.hpp index 2eeaa19..f4c5087 100644 --- a/c_compiler/include/primitives.hpp +++ b/c_compiler/include/primitives.hpp @@ -3,38 +3,25 @@ #include "ast.hpp" -#include <string> -class Variable : public Base { -private: - std::string id; +class Declarator : public BasePrimitive { public: - Variable(const std::string& _id) : id(_id) {} + Declarator(const std::string& _id) : BasePrimitive(_id) {} - virtual void print() const { - std::cout << "<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; + virtual void printxml() const { + std::cout << "<Variable id=\"" << id << "\" />" << std::endl; } }; -class Parameter : public Base { -private: - std::string id; + +class Parameter : public BasePrimitive { public: - Parameter(const std::string& _id) : id(_id) {} + Parameter(const std::string& _id) : BasePrimitive(_id) {} - virtual void print() const { + virtual void printxml() const { std::cout << "<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..451d368 100644 --- a/c_compiler/include/statement.hpp +++ b/c_compiler/include/statement.hpp @@ -3,49 +3,33 @@ #include "ast.hpp" -class Statement : public Base { -protected: - mutable std::vector<const Base*> list; - + +class Statement : public BaseNode { public: - Statement() {} + Statement() : BaseNode() {} - Statement(const Base* _el) { - list.push_back(_el); - } - - Statement(const Base* _dec, const Base* _statement) { - list.push_back(_dec); - list.push_back(_statement); - } - virtual void print() const { - for(size_t i = 0; i < list.size(); ++i) { - list[i]->print(); - } - } - - virtual void push(const Base* _var) const { - list.push_back(_var); - } + Statement(const Base* _el) : BaseNode(_el) {} }; -class StatementList : public Statement { +class StatementList : public BaseList { public: - StatementList(const Base* _statement) : Statement(_statement) {} + StatementList(const Base* _statement) : BaseList(_statement) {} }; class CompoundStatement : public Statement { public: CompoundStatement() : Statement() {} CompoundStatement(const Base* _el) : Statement(_el) {} - CompoundStatement(const Base* _dec, const Base* _statement) : - Statement(_dec, _statement) {} + + CompoundStatement(const Base* _dec, const Base* _statement) { + leftNode = _dec; + rightNode = _statement; + } - virtual void print() const override { + virtual void printxml() const override { std::cout << "<Scope>" << std::endl; - for(size_t i = 0; i < list.size(); ++i) { - list[i]->print(); - } + leftNode->printxml(); + rightNode->printxml(); std::cout << "</Scope>" << std::endl; } }; @@ -54,14 +38,17 @@ class SelectionStatement : public Statement { public: SelectionStatement() : Statement() {} SelectionStatement(const Base* _el) : Statement(_el) {} - SelectionStatement(const Base* _if, const Base* _else) : - Statement(_if, _else) {} + + SelectionStatement(const Base* _if, const Base* _else) { + leftNode = _if; + rightNode = _else; + } }; class ExpressionStatement : public Statement { public: ExpressionStatement() : Statement() {} - ExpressionStatement(const Base* _el) : Statement(_el) {} + ExpressionStatement(const Base* expr) : Statement(expr) {} }; class JumpStatement : public Statement { @@ -74,8 +61,6 @@ class IterationStatement : public Statement { public: IterationStatement() : Statement() {} IterationStatement(const Base* _el) : Statement(_el) {} - IterationStatement(const Base* _if, const Base* _else) : - Statement(_if, _else) {} }; #endif |