From 4bd736666c49f386b160ad67e5888ec3548c4fe3 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 13:31:33 +0900 Subject: [PATCH 1/9] Use vector of string --- src/use_vector.c | 2 ++ src/use_vector.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/use_vector.c b/src/use_vector.c index c2d09e49..4f9c296c 100644 --- a/src/use_vector.c +++ b/src/use_vector.c @@ -1,2 +1,4 @@ #include "use_vector.h" #include "utility.h" + +DEFINE_VECTOR(StringRef) diff --git a/src/use_vector.h b/src/use_vector.h index 94b687d2..409aaada 100644 --- a/src/use_vector.h +++ b/src/use_vector.h @@ -1,6 +1,9 @@ #ifndef KMC_C90_COMPILER_USE_VECTOR_H #define KMC_C90_COMPILER_USE_VECTOR_H +#include "stdstring.h" #include "vector.h" +DECLARE_VECTOR(StringRef) + #endif /* KMC_C90_COMPILER_USE_VECTOR_H */ From 4cad9920cc73a475ddda2315d10293e2e7a8067d Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 13:32:45 +0900 Subject: [PATCH 2/9] Add g_type_table --- src/parser.y | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/parser.y b/src/parser.y index 52aae5b1..96fe6de2 100644 --- a/src/parser.y +++ b/src/parser.y @@ -282,6 +282,9 @@ function-definition ; %% +#include "use_vector.h" +typedef VECTORREF(StringRef) TypeTableRef; +static TypeTableRef g_type_table; void yyerror(const char* s) { fprintf(stderr, "%s\n", s); From 8f9432c270b9ade6d213a6152e87948a240feb8a Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 15:18:22 +0900 Subject: [PATCH 3/9] Add type_table_(initialize|finalize) --- src/parser.y | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/parser.y b/src/parser.y index 96fe6de2..b2c79e1f 100644 --- a/src/parser.y +++ b/src/parser.y @@ -8,6 +8,8 @@ } while (false) void yyerror(const char *); +void type_table_initialize(void); +void type_table_finalize(void); } %code provides { @@ -282,6 +284,7 @@ function-definition ; %% +#include #include "use_vector.h" typedef VECTORREF(StringRef) TypeTableRef; static TypeTableRef g_type_table; @@ -289,3 +292,12 @@ static TypeTableRef g_type_table; void yyerror(const char* s) { fprintf(stderr, "%s\n", s); } + +void type_table_initialize(void) { + assert(!g_type_table); + g_type_table = VECTORFUNC(StringRef, ctor)(NULL); +} +void type_table_finalize(void) { + assert(g_type_table); + VECTORFUNC(StringRef, dtor)(&g_type_table); +} From 81b86df1f867dea65350b9d22a74ef5c133a7fe5 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 15:12:43 +0900 Subject: [PATCH 4/9] Add type_table_add --- src/parser.y | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/parser.y b/src/parser.y index b2c79e1f..abf81a6c 100644 --- a/src/parser.y +++ b/src/parser.y @@ -1,4 +1,5 @@ %code { +#include "stdstring.h" #include "utility.h" #define AST_ERROR(lhs, rhs) \ @@ -10,6 +11,7 @@ void yyerror(const char *); void type_table_initialize(void); void type_table_finalize(void); +void type_table_add(StringRef typename); } %code provides { @@ -301,3 +303,6 @@ void type_table_finalize(void) { assert(g_type_table); VECTORFUNC(StringRef, dtor)(&g_type_table); } +void type_table_add(StringRef typename) { + VECTORFUNC(StringRef, push_back)(g_type_table, typename); +} From 834e48c728e43e3612acd7b12e647c6ea788aeca Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 15:15:28 +0900 Subject: [PATCH 5/9] Add type_table_exists --- src/parser.y | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/parser.y b/src/parser.y index abf81a6c..d35ff757 100644 --- a/src/parser.y +++ b/src/parser.y @@ -1,6 +1,4 @@ %code { -#include "stdstring.h" -#include "utility.h" #define AST_ERROR(lhs, rhs) \ do { \ @@ -18,11 +16,14 @@ void type_table_add(StringRef typename); int yylex(void); void set_yyin_file(FILE* fp); void set_yyin_string(const char *code); +bool type_table_exists(StringRef typename); } %code requires { #include #include "sexpr.h" +#include "stdstring.h" +#include "utility.h" #define YYSTYPE SexprRef } @@ -306,3 +307,13 @@ void type_table_finalize(void) { void type_table_add(StringRef typename) { VECTORFUNC(StringRef, push_back)(g_type_table, typename); } +bool type_table_exists(StringRef typename) { + const StringRef* it = VECTORFUNC(StringRef, begin)(g_type_table); + const StringRef* const end = VECTORFUNC(StringRef, end)(g_type_table); + for (; it != end; ++it) { + if (string_compare(typename, *it)) { + return true; + } + } + return false; +} From 62d1dabac311c97085b96ae636f7a08d01e69d54 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 13:39:14 +0900 Subject: [PATCH 6/9] Add dependency to use_vector.c --- tests/lexer_test/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lexer_test/Makefile b/tests/lexer_test/Makefile index f570ae61..22a7107b 100644 --- a/tests/lexer_test/Makefile +++ b/tests/lexer_test/Makefile @@ -9,7 +9,7 @@ include $(TOP_DIR)/Makefile.common include $(GTEST_DIR)/Makefile.common include $(TESTS_DIR)/Makefile.common -LEX_SRCS := lex.yy.c utility.c parser.tab.c allocator.c memory_pool.c stdstring.c sexpr.c sexpr_pool.c +LEX_SRCS := lex.yy.c utility.c parser.tab.c allocator.c memory_pool.c stdstring.c sexpr.c sexpr_pool.c use_vector.c LEX_OBJS := $(LEX_SRCS:%.c=$(SRC_DIR)/%.o) TARGET := lexer_test.out From c7ec2b6ce4bd1f83b765d62c2982e84bface8e20 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 16:17:52 +0900 Subject: [PATCH 7/9] Rearrange headers --- src/parser.y | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/parser.y b/src/parser.y index d35ff757..8a11736b 100644 --- a/src/parser.y +++ b/src/parser.y @@ -13,6 +13,9 @@ void type_table_add(StringRef typename); } %code provides { +#include +#include "stdstring.h" +#include "utility.h" int yylex(void); void set_yyin_file(FILE* fp); void set_yyin_string(const char *code); @@ -20,10 +23,7 @@ bool type_table_exists(StringRef typename); } %code requires { -#include #include "sexpr.h" -#include "stdstring.h" -#include "utility.h" #define YYSTYPE SexprRef } From 9c43aec01600920c52376d3f6093e3700af1d6d0 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 16:21:58 +0900 Subject: [PATCH 8/9] Remove unused macro --- src/parser.y | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/parser.y b/src/parser.y index 8a11736b..2e4a44b7 100644 --- a/src/parser.y +++ b/src/parser.y @@ -1,11 +1,4 @@ %code { - -#define AST_ERROR(lhs, rhs) \ - do { \ - yyerror("cannot parse `" lhs "` as `" rhs "`"); \ - YYERROR; \ - } while (false) - void yyerror(const char *); void type_table_initialize(void); void type_table_finalize(void); From 39431c6749a77a5948cc909f71ca5836d0851192 Mon Sep 17 00:00:00 2001 From: hatsusato Date: Fri, 23 Sep 2016 16:30:41 +0900 Subject: [PATCH 9/9] Avoid C++ keyword --- src/parser.y | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/parser.y b/src/parser.y index 2e4a44b7..a7b57114 100644 --- a/src/parser.y +++ b/src/parser.y @@ -12,7 +12,7 @@ void type_table_add(StringRef typename); int yylex(void); void set_yyin_file(FILE* fp); void set_yyin_string(const char *code); -bool type_table_exists(StringRef typename); +bool type_table_exists(StringRef name); } %code requires { @@ -297,14 +297,14 @@ void type_table_finalize(void) { assert(g_type_table); VECTORFUNC(StringRef, dtor)(&g_type_table); } -void type_table_add(StringRef typename) { - VECTORFUNC(StringRef, push_back)(g_type_table, typename); +void type_table_add(StringRef name) { + VECTORFUNC(StringRef, push_back)(g_type_table, name); } -bool type_table_exists(StringRef typename) { +bool type_table_exists(StringRef name) { const StringRef* it = VECTORFUNC(StringRef, begin)(g_type_table); const StringRef* const end = VECTORFUNC(StringRef, end)(g_type_table); for (; it != end; ++it) { - if (string_compare(typename, *it)) { + if (string_compare(name, *it)) { return true; } }