diff --git a/src/Makefile b/src/Makefile index 512ae58c..e13e0aa4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -24,7 +24,7 @@ YACC_TARGET := parser.tab.o LEX := flex LEX_OPTION := YACC := bison -YACC_OPTION := -d +YACC_OPTION := --debug -d TARGET := kmc90 OBJS += $(AST_OBJS) OBJS += $(CODEGEN_OBJS) @@ -45,3 +45,5 @@ $(LEX_INTERMEDIATE): $(LEX_SRC) $(YACC_INTERMEDIATE) $(LEX_TARGET): CFLAGS += -Wno-unused-function .PHONY: build + +main.c: $(YACC_INTERMEDIATE) diff --git a/src/lexer.l b/src/lexer.l index 5813707d..dbd8a5e1 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -156,5 +156,5 @@ void set_yyin_string(const char *code) { } void set_yyin_file(FILE* fp) { - yyin = fp; + yyset_in(fp); } diff --git a/src/main.c b/src/main.c index d5862c48..f94434fa 100644 --- a/src/main.c +++ b/src/main.c @@ -1,30 +1,20 @@ #include -#include -#include -#include +#include "parser.tab.h" +#include "sexpr_pool.h" #include "utility.h" +extern int yydebug; + int main(int argc, char *argv[]) { - LLVMModuleRef module = LLVMModuleCreateWithName("kmc90_module"); - /* int main() { return 0; } */ - LLVMTypeRef main_type = LLVMFunctionType(LLVMInt32Type(), NULL, 0, false); - LLVMValueRef main_func = LLVMAddFunction(module, "main", main_type); - LLVMBasicBlockRef main_entry = LLVMAppendBasicBlock(main_func, "main_entry"); - LLVMValueRef return_value = LLVMConstInt(LLVMInt32Type(), 0, false); - LLVMBuilderRef builder = LLVMCreateBuilder(); - LLVMPositionBuilderAtEnd(builder, main_entry); - LLVMBuildRet(builder, return_value); - { - char *error = NULL; - LLVMVerifyModule(module, LLVMAbortProcessAction, &error); - LLVMDisposeMessage(error); - } - if (LLVMWriteBitcodeToFile(module, "main.bc") != 0) { - fputs("failed to write bitcode to file\n", stderr); + yydebug = 1; + if (1 < argc) { + FILE* fp = fopen(argv[1], "r"); + if (fp) { + sexpr_initialize_pool(); + parse(fp); + sexpr_finalize_pool(); + fclose(fp); + } } - LLVMDisposeBuilder(builder); - LLVMDisposeModule(module); return 0; - UNUSED(argc); - UNUSED(argv); } diff --git a/src/parser.y b/src/parser.y index 52aae5b1..7ffcaf0e 100644 --- a/src/parser.y +++ b/src/parser.y @@ -7,6 +7,7 @@ YYERROR; \ } while (false) +static YYSTYPE g_ast_root; void yyerror(const char *); } @@ -14,6 +15,7 @@ void yyerror(const char *); int yylex(void); void set_yyin_file(FILE* fp); void set_yyin_string(const char *code); +YYSTYPE parse(FILE* fp); } %code requires { @@ -83,7 +85,7 @@ void set_yyin_string(const char *code); %token VOLATILE "volatile" %token WHILE "while" -%start translation-unit +%start entry-point %% @@ -281,8 +283,31 @@ function-definition : declaration-specifiers function-definition-declarator compound-statement ; +entry-point +: translation-unit { + g_ast_root = $[translation-unit]; +} + %% void yyerror(const char* s) { fprintf(stderr, "%s\n", s); } + +YYSTYPE parse(FILE* fp) { + set_yyin_file(fp); + switch (yyparse()) { + case 0: + return g_ast_root; + case 1: + yyerror("invalid input in yyparse"); + break; + case 2: + yyerror("memory exhaustion in yyparse"); + break; + default: + yyerror("unknown error in yyparse"); + break; + } + return NULL; +}