aboutsummaryrefslogtreecommitdiffstats
path: root/c_compiler/include
diff options
context:
space:
mode:
Diffstat (limited to 'c_compiler/include')
-rw-r--r--c_compiler/include/ast.hpp24
-rw-r--r--c_compiler/include/declaration.hpp35
-rw-r--r--c_compiler/include/expression.hpp35
-rw-r--r--c_compiler/include/function.hpp26
-rw-r--r--c_compiler/include/node.hpp30
-rw-r--r--c_compiler/include/statement.hpp101
-rw-r--r--c_compiler/include/translation_unit.hpp24
-rw-r--r--c_compiler/include/type.hpp70
8 files changed, 345 insertions, 0 deletions
diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp
new file mode 100644
index 0000000..88ef36b
--- /dev/null
+++ b/c_compiler/include/ast.hpp
@@ -0,0 +1,24 @@
+#ifndef AST_HPP
+#define AST_HPP
+
+#include <vector>
+#include <string>
+#include <iostream>
+#include <map>
+#include <cstdint>
+
+struct VarLocation;
+
+typedef std::map<std::string, VarLocation> VariableStackBindings;
+
+#include "node.hpp"
+#include "type.hpp"
+#include "expression.hpp"
+#include "declaration.hpp"
+#include "statement.hpp"
+#include "function.hpp"
+#include "translation_unit.hpp"
+
+Node* parseAST();
+
+#endif
diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp
new file mode 100644
index 0000000..8226090
--- /dev/null
+++ b/c_compiler/include/declaration.hpp
@@ -0,0 +1,35 @@
+#ifndef AST_DECLARATION_HPP
+#define AST_DECLARATION_HPP
+
+#include "node.hpp"
+
+class Expression;
+
+
+class Declaration : public Node {
+private:
+ Type* type;
+ std::string id;
+ Expression* init;
+ Declaration* next_decl;
+ Declaration* list_next_decl;
+
+public:
+ Declaration(const std::string& _id = "");
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+
+ void addDeclaration(Declaration* _next_decl);
+ void addList(Declaration* _next_decl);
+
+ void setType(Type* _type);
+
+ Declaration* getNext() const;
+ Declaration* getNextListItem() const;
+ std::string getId() const;
+ std::string getType() const;
+};
+
+#endif
diff --git a/c_compiler/include/expression.hpp b/c_compiler/include/expression.hpp
new file mode 100644
index 0000000..d446972
--- /dev/null
+++ b/c_compiler/include/expression.hpp
@@ -0,0 +1,35 @@
+#ifndef AST_EXPRESSION_HPP
+#define AST_EXPRESSION_HPP
+
+#include "node.hpp"
+
+
+class Expression : public Node {
+public:
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0;
+};
+
+
+class Identifier : public Expression {
+private:
+ std::string m_id;
+public:
+ Identifier(const std::string& id);
+
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+};
+
+
+class Constant : public Expression {
+private:
+ int32_t m_constant;
+public:
+ Constant(const int32_t& constant);
+
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+};
+
+
+#endif
diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp
new file mode 100644
index 0000000..77d1372
--- /dev/null
+++ b/c_compiler/include/function.hpp
@@ -0,0 +1,26 @@
+#ifndef AST_FUNCTION_HPP
+#define AST_FUNCTION_HPP
+
+#include "node.hpp"
+
+class Declaration;
+class Statement;
+
+
+class Function : public Node {
+protected:
+ Type* type;
+ std::string id;
+ Declaration* parameter_list;
+ Statement* statement;
+
+public:
+ Function(const std::string& _id, Declaration* _parameter_list, Statement* _statement);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+};
+
+
+#endif
diff --git a/c_compiler/include/node.hpp b/c_compiler/include/node.hpp
new file mode 100644
index 0000000..584ed32
--- /dev/null
+++ b/c_compiler/include/node.hpp
@@ -0,0 +1,30 @@
+#ifndef NODE_HPP
+#define NODE_HPP
+
+#include <cstdint>
+#include <map>
+#include <string>
+
+struct VarLocation;
+class Type;
+
+typedef std::map<std::string, VarLocation> VariableStackBindings;
+
+
+struct VarLocation {
+ Type* type;
+ int32_t stack_position;
+};
+
+
+class Node {
+public:
+ virtual ~Node() {}
+
+ virtual void print() const = 0;
+ virtual void printxml() const = 0;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0;
+};
+
+
+#endif
diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp
new file mode 100644
index 0000000..b5f7e9c
--- /dev/null
+++ b/c_compiler/include/statement.hpp
@@ -0,0 +1,101 @@
+#ifndef AST_STATEMENT_HPP
+#define AST_STATEMENT_HPP
+
+#include "node.hpp"
+
+class Declaration;
+class Expression;
+
+
+class Statement : public Node {
+protected:
+ Statement* next_statement;
+
+public:
+ Statement(Statement* statement = nullptr);
+
+ virtual void print() const = 0;
+ virtual void printxml() const = 0;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const = 0;
+
+ virtual void count_variables(int32_t& var_count) const = 0;
+
+ void addStatement(Statement* _next);
+};
+
+
+class CompoundStatement : public Statement {
+protected:
+ Declaration* m_decl;
+ Statement* m_statement;
+
+public:
+ CompoundStatement(Declaration* decl = nullptr, Statement* statement = nullptr);
+ CompoundStatement(Statement* statement);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+
+ virtual void count_variables(int32_t& var_count) const;
+};
+
+
+class SelectionStatement : public Statement {
+protected:
+ Statement* m_if;
+ Statement* m_else;
+public:
+ SelectionStatement(Statement* _if = nullptr, Statement* _else = nullptr);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+
+ virtual void count_variables(int32_t& var_count) const;
+};
+
+
+class ExpressionStatement : public Statement {
+protected:
+ Expression* m_expr;
+public:
+ ExpressionStatement(Expression* expr = nullptr);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+
+ virtual void count_variables(int32_t& var_count) const;
+};
+
+
+class JumpStatement : public Statement {
+protected:
+ Expression* m_expr;
+public:
+ JumpStatement(Expression* expr = nullptr);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+
+ virtual void count_variables(int32_t& var_count) const;
+};
+
+
+class IterationStatement : public Statement {
+protected:
+ Statement* m_statement;
+public:
+ IterationStatement(Statement* statement);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+
+ virtual void count_variables(int32_t& var_count) const;
+};
+
+
+#endif
diff --git a/c_compiler/include/translation_unit.hpp b/c_compiler/include/translation_unit.hpp
new file mode 100644
index 0000000..c290163
--- /dev/null
+++ b/c_compiler/include/translation_unit.hpp
@@ -0,0 +1,24 @@
+#ifndef TRANSLATION_UNIT_HPP
+#define TRANSLATION_UNIT_HPP
+
+#include "node.hpp"
+
+#include <vector>
+
+
+class TranslationUnit : public Node {
+protected:
+ std::vector<Node* > translation_unit;
+
+public:
+ TranslationUnit(Node* decl);
+
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+
+ void push(Node* decl);
+};
+
+
+#endif
diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp
new file mode 100644
index 0000000..3deca58
--- /dev/null
+++ b/c_compiler/include/type.hpp
@@ -0,0 +1,70 @@
+#ifndef TYPE_HPP
+#define TYPE_HPP
+
+#include "node.hpp"
+
+
+class Type : public Node {
+public:
+ virtual void print() const;
+ virtual void printxml() const;
+ virtual VariableStackBindings printasm(VariableStackBindings bindings) const;
+
+ virtual std::string getType() const = 0;
+};
+
+
+class Specifier : public Type {
+public:
+ virtual std::string getType() const = 0;
+};
+
+
+class Pointer : public Type {
+protected:
+ Type* pointer_type;
+
+public:
+ Pointer(Type* _pointer_type);
+
+ virtual std::string getType() const;
+};
+
+
+class Array : public Type {
+protected:
+ int32_t size;
+ Type* array_type;
+
+public:
+ Array(Type* _array_type, int32_t _size = 0);
+
+ virtual std::string getType() const;
+};
+
+
+class Void : public Specifier {
+public:
+ Void();
+
+ virtual std::string getType() const;
+};
+
+
+class Int : public Specifier {
+public:
+ Int();
+
+ virtual std::string getType() const;
+};
+
+
+class Char : public Specifier {
+public:
+ Char();
+
+ virtual std::string getType() const;
+};
+
+
+#endif