Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typename table #245

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions src/parser.y
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#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 <stdio.h>
#include "sexpr.h"
#define YYSTYPE SexprRef
}
Expand Down Expand Up @@ -282,7 +280,33 @@ function-definition
;

%%
#include <assert.h>
#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;
}
2 changes: 2 additions & 0 deletions src/use_vector.c
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#include "use_vector.h"
#include "utility.h"

DEFINE_VECTOR(StringRef)
3 changes: 3 additions & 0 deletions src/use_vector.h
Original file line number Diff line number Diff line change
@@ -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 */
2 changes: 1 addition & 1 deletion tests/lexer_test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down