aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-03-06 21:04:01 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-03-06 21:04:01 +0000
commit30f7753bb64ad95750dbd6bce9b7ab3c077b92aa (patch)
treeded96a4f3d2b934b1b769c4e2bc21b0638519346
parentfdd6ff07cee824078c5315bf07926ee15bbdde85 (diff)
downloadCompiler-30f7753bb64ad95750dbd6bce9b7ab3c077b92aa.tar.gz
Compiler-30f7753bb64ad95750dbd6bce9b7ab3c077b92aa.zip
Made good progress
-rw-r--r--.gitignore1
-rw-r--r--.vagrant/machines/default/virtualbox/action_provision1
-rw-r--r--.vagrant/machines/default/virtualbox/action_set_name1
-rw-r--r--.vagrant/machines/default/virtualbox/creator_uid1
-rw-r--r--.vagrant/machines/default/virtualbox/id1
-rw-r--r--.vagrant/machines/default/virtualbox/index_uuid1
-rw-r--r--.vagrant/machines/default/virtualbox/private_key27
-rw-r--r--.vagrant/machines/default/virtualbox/synced_folders1
-rw-r--r--c_compiler/include/declaration.hpp3
-rw-r--r--c_compiler/include/function.hpp2
-rw-r--r--c_compiler/include/type.hpp12
-rw-r--r--c_compiler/src/c_lexer.flex14
-rw-r--r--c_compiler/src/c_parser.y45
-rw-r--r--c_compiler/src/declaration.cpp10
-rw-r--r--c_compiler/src/statement.cpp4
-rw-r--r--makefile2
16 files changed, 98 insertions, 28 deletions
diff --git a/.gitignore b/.gitignore
index 615b21b..4555810 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,3 +9,4 @@ c_compiler/build/
*.output
#*
.#*
+*.log
diff --git a/.vagrant/machines/default/virtualbox/action_provision b/.vagrant/machines/default/virtualbox/action_provision
new file mode 100644
index 0000000..56a084d
--- /dev/null
+++ b/.vagrant/machines/default/virtualbox/action_provision
@@ -0,0 +1 @@
+1.5:9a969542-a2da-4437-bafe-ff8e0a4f9172 \ No newline at end of file
diff --git a/.vagrant/machines/default/virtualbox/action_set_name b/.vagrant/machines/default/virtualbox/action_set_name
new file mode 100644
index 0000000..f7613f8
--- /dev/null
+++ b/.vagrant/machines/default/virtualbox/action_set_name
@@ -0,0 +1 @@
+1488834030 \ No newline at end of file
diff --git a/.vagrant/machines/default/virtualbox/creator_uid b/.vagrant/machines/default/virtualbox/creator_uid
new file mode 100644
index 0000000..e37d32a
--- /dev/null
+++ b/.vagrant/machines/default/virtualbox/creator_uid
@@ -0,0 +1 @@
+1000 \ No newline at end of file
diff --git a/.vagrant/machines/default/virtualbox/id b/.vagrant/machines/default/virtualbox/id
new file mode 100644
index 0000000..eb57d45
--- /dev/null
+++ b/.vagrant/machines/default/virtualbox/id
@@ -0,0 +1 @@
+9a969542-a2da-4437-bafe-ff8e0a4f9172 \ No newline at end of file
diff --git a/.vagrant/machines/default/virtualbox/index_uuid b/.vagrant/machines/default/virtualbox/index_uuid
new file mode 100644
index 0000000..b7ff654
--- /dev/null
+++ b/.vagrant/machines/default/virtualbox/index_uuid
@@ -0,0 +1 @@
+dd05ec035bdc4e82b8f1d14ffc72abec \ No newline at end of file
diff --git a/.vagrant/machines/default/virtualbox/private_key b/.vagrant/machines/default/virtualbox/private_key
new file mode 100644
index 0000000..98761cf
--- /dev/null
+++ b/.vagrant/machines/default/virtualbox/private_key
@@ -0,0 +1,27 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIEogIBAAKCAQEAoo+hwvBwBwGKwMyyGUOuStvHA8tSMFpOLYVB16kTjiYxRHaX
+3EBOpcED9wM8KUdjqk0RcMhLyshPOnjxla1bE4Djf/bABfY4duC3PSdUC+8BXraJ
+DWi0ZIEMbvBbt8RLowC0qjmpGZm4lyc+T6uRUQm3igXgDSWypSwtZ39O5IKYc5Vg
+BiG+8maSSuuQTaafLeQb4A7k6odvYwVIffHeX37/HKFJ2cAPm3bF4I+BhCQ3y0B6
+XpOTtA0/qsQV0IRhbfJyxtxx5e8rcI+iunxNmfFX+kOfrPuKRKNavYNCLA6NzpbN
+ABA4ioI639+vuNB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DMFNkn/+MuCuaGRDJrh+UpPLYSHW9/wKtjF+QG1lIVY
+8CvBAoGAATJ0xWgIszXBnC5fioLKqV9lUupk3t+aSDPFKBg5qnwlimTUKqKWzsD6
+r3rHBVFIA0agYIEr88Kt4tcXQ8dcUmTMhrIISHidAs21qzK1DBNhWeJ6AhggxLiH
+kU1+pOu12B5nUpR5KBfwTyO3MaQuEB/tefu9XALDtdQbhmPKiJY=
+-----END RSA PRIVATE KEY-----
diff --git a/.vagrant/machines/default/virtualbox/synced_folders b/.vagrant/machines/default/virtualbox/synced_folders
new file mode 100644
index 0000000..7c78dac
--- /dev/null
+++ b/.vagrant/machines/default/virtualbox/synced_folders
@@ -0,0 +1 @@
+{"virtualbox":{"/vagrant":{"guestpath":"/vagrant","hostpath":"/home/yannherklotz/Github/langproc-2016-cw-ymh15","disabled":false,"__vagrantfile":true}}} \ No newline at end of file
diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp
index 9287827..c46aca2 100644
--- a/c_compiler/include/declaration.hpp
+++ b/c_compiler/include/declaration.hpp
@@ -23,9 +23,12 @@ public:
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/function.hpp b/c_compiler/include/function.hpp
index 967c9d8..d608531 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"
diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp
index 415f00f..8727ee0 100644
--- a/c_compiler/include/type.hpp
+++ b/c_compiler/include/type.hpp
@@ -14,13 +14,13 @@ public:
};
-class Specifier : Type {
+class Specifier : public Type {
public:
virtual std::string getType() const = 0;
};
-class Pointer : Type {
+class Pointer : public Type {
protected:
Type* pointer_type;
@@ -31,7 +31,7 @@ public:
};
-class Array : Type {
+class Array : public Type {
protected:
int32_t size;
Type* array_type;
@@ -43,7 +43,7 @@ public:
};
-class Void : Specifier {
+class Void : public Specifier {
public:
Void();
@@ -51,7 +51,7 @@ public:
};
-class Int : Specifier {
+class Int : public Specifier {
public:
Int();
@@ -59,7 +59,7 @@ public:
};
-class Char : Specifier {
+class Char : public Specifier {
public:
Char();
diff --git a/c_compiler/src/c_lexer.flex b/c_compiler/src/c_lexer.flex
index 38ee946..7690d71 100644
--- a/c_compiler/src/c_lexer.flex
+++ b/c_compiler/src/c_lexer.flex
@@ -30,9 +30,17 @@ ALL .
%%
-typedef|extern|static|auto|register { return T_STRG_SPEC; }
-void|char|short|int|long|float|double|signed|unsigned { return T_TYPE_SPEC; }
-const|volatile { return T_TYPE_QUAL; }
+(void) { return T_VOID; }
+(char) { return T_CHAR; }
+(signed[ ]char) { return T_SCHAR; }
+(unsigned[ ]char) { return T_UCHAR; }
+((short[ ]int)|(signed[ ]short[ ]int)|short|(signed[ ]short)) { return T_SSINT; }
+((unsigned[ ]short[ ]int)|(unsigned[ ]short)) { return T_USINT; }
+((signed[ ]long[ ]int)|(signed[ ]long)|(long[ ]int)|long) { return T_LINT; }
+((unsigned[ ]long[ ]int)|(unsigned[ ]long)) { return T_ULINT; }
+((unsigned[ ]int)|unsigned) { return T_UINT; }
+((signed[ ]int)|int|signed) { return T_SINT; }
+
[;] { return T_SC; }
[,] { return T_CMA; }
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y
index 4a78ec5..c643f10 100644
--- a/c_compiler/src/c_parser.y
+++ b/c_compiler/src/c_parser.y
@@ -27,11 +27,12 @@ void yyerror(const char *);
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
- T_ADDSUB_OP T_ASSIGN_OPER T_EQ T_SIZEOF T_INT_CONST T_IF T_WHILE T_DO T_FOR
- T_RETURN
+%token 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 T_ADDSUB_OP T_ASSIGN_OPER T_EQ
+ T_SIZEOF T_INT_CONST T_IF T_WHILE T_DO T_FOR T_RETURN
+
+ T_VOID T_CHAR T_SCHAR T_UCHAR T_SSINT T_USINT T_LINT T_ULINT T_UINT T_SINT
%nonassoc T_RRB
%nonassoc T_ELSE
@@ -57,7 +58,7 @@ void yyerror(const char *);
AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression
PostfixExpression PostfixExpression2 ArgumentExpressionList PrimaryExpression
-%type <type> DeclarationSpec DeclarationSpec_T
+%type <type> DeclarationSpec
%type <string> Declarator DirectDeclarator
@@ -112,18 +113,28 @@ DeclarationList:
;
Declaration:
- DeclarationSpec InitDeclaratorList T_SC { $$ = $2; }
+ DeclarationSpec InitDeclaratorList T_SC {
+ $$ = $2;
+ Declaration* tmp_decl = $2;
+
+ while(tmp_decl != nullptr) {
+ tmp_decl->setType($1);
+ tmp_decl = tmp_decl->getNextListItem();
+ }
+ }
;
DeclarationSpec:
- DeclarationSpec_T { ; }
- | DeclarationSpec_T DeclarationSpec { ; }
- ;
-
-DeclarationSpec_T:
- T_TYPE_SPEC { ; }
- | T_TYPE_QUAL { ; }
- | T_STRG_SPEC { ; }
+ T_VOID { $$ = new Void; }
+ | T_CHAR { $$ = new Char; }
+ | T_SCHAR { $$ = new Char; }
+ | T_UCHAR { $$ = new Char; }
+ | T_SSINT { $$ = new Int; }
+ | T_USINT { $$ = new Int; }
+ | T_LINT { $$ = new Int; }
+ | T_ULINT { $$ = new Int; }
+ | T_UINT { $$ = new Int; }
+ | T_SINT { $$ = new Int; }
;
InitDeclaratorList:
@@ -279,7 +290,7 @@ MultDivRemOP:
CastExpression:
UnaryExpression { $$ = $1; }
- | T_LRB T_TYPE_SPEC T_RRB CastExpression { $$ = $4; }
+ | T_LRB DeclarationSpec T_RRB CastExpression { $$ = $4; }
;
UnaryExpression:
@@ -287,7 +298,7 @@ UnaryExpression:
| T_INCDEC UnaryExpression { $$ = $2; }
| UnaryOperator CastExpression { $$ = $2; }
| T_SIZEOF UnaryExpression { $$ = $2; }
- | T_SIZEOF T_LRB T_TYPE_SPEC T_RRB { $$ = new Expression(); }
+ | T_SIZEOF T_LRB DeclarationSpec T_RRB { $$ = new Expression(); }
;
UnaryOperator:
diff --git a/c_compiler/src/declaration.cpp b/c_compiler/src/declaration.cpp
index 2fba9f9..e2f27cc 100644
--- a/c_compiler/src/declaration.cpp
+++ b/c_compiler/src/declaration.cpp
@@ -42,6 +42,11 @@ void Declaration::addList(Declaration* _next_decl)
list_next_decl = _next_decl;
}
+void Declaration::setType(Type* _type)
+{
+ type = _type;
+}
+
Declaration* Declaration::getNext() const
{
return next_decl;
@@ -56,3 +61,8 @@ std::string Declaration::getId() const
{
return id;
}
+
+std::string Declaration::getType() const
+{
+ return type->getType();
+}
diff --git a/c_compiler/src/statement.cpp b/c_compiler/src/statement.cpp
index 14d5808..dcd2271 100644
--- a/c_compiler/src/statement.cpp
+++ b/c_compiler/src/statement.cpp
@@ -67,9 +67,13 @@ void CompoundStatement::count_variables(int32_t& var_count) const
while(declaration_list != nullptr) {
var_count++;
+ std::cout << declaration_list->getType() << std::endl;
+
declaration_list = declaration_list->getNextListItem();
}
+ std::cout << declaration->getType() << std::endl;
+
var_count++;
declaration = declaration->getNext();
diff --git a/makefile b/makefile
index 8bc9679..0702fb5 100644
--- a/makefile
+++ b/makefile
@@ -70,7 +70,7 @@ $(PARSRCDIR)/c_parser.tab.cpp $(PARSRCDIR)/c_parser.tab.hpp : $(PARSRCDIR)/c_par
bin/c_compiler: $(COMPBUILDDIR)/compiler_main.o $(COMPBUILDDIR)/statement.o \
$(COMPBUILDDIR)/function.o $(COMPBUILDDIR)/expression.o \
$(COMPBUILDDIR)/initializer.o $(COMPBUILDDIR)/translation_unit.o \
- $(COMPBUILDDIR)/declaration.o \
+ $(COMPBUILDDIR)/declaration.o $(COMPBUILDDIR)/type.o \
$(COMPBUILDDIR)/c_parser.tab.o $(COMPBUILDDIR)/c_lexer.yy.o
@echo "Linking..."
@echo " mkdir -p bin"; mkdir -p bin