From: Michael D. Lowis Date: Mon, 29 Jun 2015 01:15:13 +0000 (-0400) Subject: Reworked parser and tree printing X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=24efc60c979bbc86ca2a4a1f46f59602180cdb02;p=proto%2Fgir.git Reworked parser and tree printing --- diff --git a/build.rb b/build.rb index 95f8328..813b3b9 100755 --- a/build.rb +++ b/build.rb @@ -6,18 +6,16 @@ require './modules/build-system/setup' #------------------------------------------------------------------------------ # Define the default compiler environment main_env = BuildEnv.new do |env| - env["CFLAGS"] += ['-O3', '-Wall', '-Wextra', '-Werror', '--std=c99', '--pedantic'] - env["CPPPATH"] += Dir['modules/libcds/source/**/'] + - Dir['modules/libc/source/**/'] + env["CFLAGS"] += ['-O3', '-Wall', '-Wextra', '--std=c99', '--pedantic'] + env["CPPPATH"] += Dir['modules/libc/source/**/'] end #------------------------------------------------------------------------------ # Release Build Targets #------------------------------------------------------------------------------ # Build third party libraries -main_env.Library('build/lib/libcds.a', FileList['modules/libcds/source/**/*.c']) main_env.Library('build/lib/libc.a', FileList['modules/libc/source/**/*.c']) -runtime_libs = ['build/lib/libcds.a', 'build/lib/libc.a'] +runtime_libs = ['build/lib/libc.a'] # Build the parser main_env.Program('parser', FileList['source/*.c'] + runtime_libs) diff --git a/source/parser.c b/source/parser.c index 85f16f1..68bc24d 100644 --- a/source/parser.c +++ b/source/parser.c @@ -42,9 +42,10 @@ *****************************************************************************/ // Types typedef struct AST { - int type; slist_node_t link; slist_t children; + char* text; + int type; } AST; typedef struct strbuf_t { @@ -107,6 +108,7 @@ static void ast_print(AST* tree, int depth); // String Buffer static void strbuf_init(strbuf_t* buf); static void strbuf_putc(strbuf_t* buf, int ch); +static char* strbuf_string(strbuf_t* buf); /* *****************************************************************************/ @@ -163,7 +165,6 @@ static AST* unary_send(void) AST* msg = ast_new(UNARY_MSG); ast_add_child(msg, expr); ast_add_child(msg, ast_tok(IDENTIFIER)); - printf("%d\n", (int)slist_size(&msg->children)); expr = msg; } return expr; @@ -290,9 +291,13 @@ static bool optional(int expected) *****************************************************************************/ static int token(void) { + // Re-init the token + strbuf_init(&Token); + // Skip any whitespace. whitespace(); + // Get the next real token if (EOF == current()) { return EOF; } else if ('$' == current()) { @@ -461,15 +466,18 @@ static void fetch(void) *****************************************************************************/ static AST* ast_new(int type) { - AST* tree = (AST*)calloc(1,sizeof(AST)); + AST* tree = (AST*)malloc(sizeof(AST)); + memset(tree, 0, sizeof(AST)); tree->type = type; return tree; } static AST* ast_tok(int type) { + AST* ast = ast_new(type); expect(type); - return ast_new(type); + ast->text = strbuf_string(&Token); + return ast; } static void ast_add_child(AST* parent, AST* child) @@ -481,7 +489,7 @@ static void ast_print(AST* tree, int depth) { int indent = depth * 2; printf("%*s(", indent, ""); - printf("%d", tree->type); + printf("%d %s", tree->type, tree->text ? tree->text : ""); if (slist_size(&tree->children) == 0) { printf(")\n"); } else { @@ -521,10 +529,10 @@ static void strbuf_putc(strbuf_t* buf, int ch) // strbuf_putc(buf, *str++); //} -//static char* strbuf_string(strbuf_t* buf) -//{ -// char* str = buf->string; -// strbuf_init(buf); -// return str; -//} +static char* strbuf_string(strbuf_t* buf) +{ + char* str = buf->string; + strbuf_init(buf); + return str; +} diff --git a/source/world.c b/source/world.c index 3f6c6f7..dab195c 100644 --- a/source/world.c +++ b/source/world.c @@ -4,6 +4,7 @@ */ //#include "world.h" +#if 0 #include "set.h" #include "map.h" @@ -55,5 +56,9 @@ void world_init(void) HashSet = object_clone(Object); HashMap = object_clone(Object); } +#endif +void world_init(void) +{ +}