aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser/include
diff options
context:
space:
mode:
Diffstat (limited to 'c_parser/include')
-rw-r--r--c_parser/include/ast.hpp14
-rw-r--r--c_parser/include/ast_base.hpp16
-rw-r--r--c_parser/include/ast_declaration.hpp50
-rw-r--r--c_parser/include/ast_expression.hpp24
-rw-r--r--c_parser/include/ast_function.hpp53
-rw-r--r--c_parser/include/ast_primitives.hpp40
-rw-r--r--c_parser/include/ast_statement.hpp81
-rw-r--r--c_parser/include/ast_top.hpp24
8 files changed, 302 insertions, 0 deletions
diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp
new file mode 100644
index 0000000..b73506f
--- /dev/null
+++ b/c_parser/include/ast.hpp
@@ -0,0 +1,14 @@
+#ifndef AST_HPP
+#define AST_HPP
+
+#include "ast_base.hpp"
+#include "ast_statement.hpp"
+#include "ast_function.hpp"
+#include "ast_declaration.hpp"
+#include "ast_expression.hpp"
+#include "ast_primitives.hpp"
+#include "ast_top.hpp"
+
+ast_Top *parseAST();
+
+#endif
diff --git a/c_parser/include/ast_base.hpp b/c_parser/include/ast_base.hpp
new file mode 100644
index 0000000..9451609
--- /dev/null
+++ b/c_parser/include/ast_base.hpp
@@ -0,0 +1,16 @@
+#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_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp
new file mode 100644
index 0000000..bfd3070
--- /dev/null
+++ b/c_parser/include/ast_declaration.hpp
@@ -0,0 +1,50 @@
+#ifndef AST_DECLARATION_HPP
+#define AST_DECLARATION_HPP
+
+#include "ast.hpp"
+
+#include <vector>
+
+// Declaration that holds a list of declarations
+
+class DeclarationList : public Base {
+private:
+ mutable std::vector<const Base*> dec_list;
+
+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);
+ }
+};
+
+class VariableDeclaration : public Base {
+private:
+ mutable std::vector<const Base*> var_list;
+
+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);
+ }
+};
+
+#endif
diff --git a/c_parser/include/ast_expression.hpp b/c_parser/include/ast_expression.hpp
new file mode 100644
index 0000000..f67d2d7
--- /dev/null
+++ b/c_parser/include/ast_expression.hpp
@@ -0,0 +1,24 @@
+#ifndef AST_EXPRESSION_HPP
+#define AST_EXPRESSION_HPP
+
+#include "ast.hpp"
+
+#include <string>
+#include <iostream>
+
+class Expression : public Base {
+private:
+public:
+ Expression() {}
+
+ virtual void print() const {
+
+ }
+
+ virtual void push(const Base* _base) const {
+ std::cerr << "Can't call this function for this type" << std::endl;
+ (void)_base;
+ }
+};
+
+#endif
diff --git a/c_parser/include/ast_function.hpp b/c_parser/include/ast_function.hpp
new file mode 100644
index 0000000..6fbcdee
--- /dev/null
+++ b/c_parser/include/ast_function.hpp
@@ -0,0 +1,53 @@
+#ifndef AST_FUNCTION_HPP
+#define AST_FUNCTION_HPP
+
+#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;
+ }
+};
+
+class ParamList : public Base {
+private:
+ mutable std::vector<const Base*> param_list;
+
+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);
+ }
+};
+
+#endif
diff --git a/c_parser/include/ast_primitives.hpp b/c_parser/include/ast_primitives.hpp
new file mode 100644
index 0000000..2eeaa19
--- /dev/null
+++ b/c_parser/include/ast_primitives.hpp
@@ -0,0 +1,40 @@
+#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_parser/include/ast_statement.hpp b/c_parser/include/ast_statement.hpp
new file mode 100644
index 0000000..4761efb
--- /dev/null
+++ b/c_parser/include/ast_statement.hpp
@@ -0,0 +1,81 @@
+#ifndef AST_STATEMENT_HPP
+#define AST_STATEMENT_HPP
+
+#include "ast.hpp"
+
+class Statement : public Base {
+protected:
+ mutable std::vector<const Base*> list;
+
+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);
+ }
+};
+
+class StatementList : public Statement {
+public:
+ StatementList(const Base* _statement) : Statement(_statement) {}
+};
+
+class CompoundStatement : public 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;
+ }
+};
+
+class SelectionStatement : public Statement {
+public:
+ SelectionStatement() : Statement() {}
+ SelectionStatement(const Base* _el) : Statement(_el) {}
+ SelectionStatement(const Base* _if, const Base* _else) :
+ Statement(_if, _else) {}
+};
+
+class ExpressionStatement : public Statement {
+public:
+ ExpressionStatement() : Statement() {}
+ ExpressionStatement(const Base* _el) : Statement(_el) {}
+};
+
+class JumpStatement : public Statement {
+public:
+ JumpStatement() : Statement() {}
+ JumpStatement(const Base* _el) : Statement(_el) {}
+};
+
+class IterationStatement : public Statement {
+public:
+ IterationStatement() : Statement() {}
+ IterationStatement(const Base* _el) : Statement(_el) {}
+ IterationStatement(const Base* _if, const Base* _else) :
+ Statement(_if, _else) {}
+};
+
+#endif
diff --git a/c_parser/include/ast_top.hpp b/c_parser/include/ast_top.hpp
new file mode 100644
index 0000000..737ff58
--- /dev/null
+++ b/c_parser/include/ast_top.hpp
@@ -0,0 +1,24 @@
+#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