diff --git a/src/parser.y b/src/parser.y index 52aae5b1..a7b57114 100644 --- a/src/parser.y +++ b/src/parser.y @@ -1,23 +1,21 @@ %code { -#include "utility.h" - -#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); +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); +bool type_table_exists(StringRef name); } %code requires { -#include #include "sexpr.h" #define YYSTYPE SexprRef } @@ -282,7 +280,33 @@ function-definition ; %% +#include +#include "use_vector.h" +typedef VECTORREF(StringRef) TypeTableRef; +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); +} +void type_table_add(StringRef name) { + VECTORFUNC(StringRef, push_back)(g_type_table, name); +} +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(name, *it)) { + return true; + } + } + return false; +} 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 */ 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