From 48e28809eae8f556b8e6742b279096927eaf17ae Mon Sep 17 00:00:00 2001 From: dibyendumajumdar Date: Sat, 12 Nov 2022 11:54:56 +0000 Subject: [PATCH] #85 add option to dump ast as a Ravi code --- tests/tcommon.c | 5 ++++- tests/tcommon.h | 2 +- tests/trun.c | 21 ++++++++++++--------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tests/tcommon.c b/tests/tcommon.c index d0f8d28..07aa6bf 100644 --- a/tests/tcommon.c +++ b/tests/tcommon.c @@ -37,6 +37,7 @@ void parse_arguments(struct arguments *args, int argc, const char *argv[]) args->irdump = 1; args->cfgdump = 1; args->codump = 1; + args->table_ast = 0; args->simplify_ast = 0; args->remove_unreachable_blocks = 0; args->gen_C = 0; @@ -63,6 +64,8 @@ void parse_arguments(struct arguments *args, int argc, const char *argv[]) args->gen_C = 1; } else if (strcmp(argv[i], "--opt-upvalues") == 0) { args->opt_upvalue = 1; + } else if (strcmp(argv[i], "--table-ast") == 0) { + args->table_ast = 1; } else if (strcmp(argv[i], "-main") == 0) { if (i < argc - 1) { i++; @@ -99,7 +102,7 @@ void parse_arguments(struct arguments *args, int argc, const char *argv[]) const char *read_file(const char *filename) { - /* We need to use binary read on windows to get file size correctly */ + /* We need to use binary read on Windows to get file size correctly */ #ifdef _WIN32 FILE *fp = fopen(filename, "rb"); #else diff --git a/tests/tcommon.h b/tests/tcommon.h index 5f582d1..e2214dc 100644 --- a/tests/tcommon.h +++ b/tests/tcommon.h @@ -30,7 +30,7 @@ struct arguments { const char *filename; const char *code; unsigned typecheck : 1, linearize : 1, astdump : 1, irdump : 1, cfgdump : 1, codump : 1, simplify_ast : 1, - remove_unreachable_blocks: 1, gen_C: 1, opt_upvalue: 1; + remove_unreachable_blocks: 1, gen_C: 1, opt_upvalue: 1, table_ast : 1; const char *mainfunc; /* name of the main function in generated code, only applies if gen_C is on */ }; extern void parse_arguments(struct arguments *args, int argc, const char *argv[]); diff --git a/tests/trun.c b/tests/trun.c index 6d60c7b..0a9e8bf 100644 --- a/tests/trun.c +++ b/tests/trun.c @@ -113,6 +113,15 @@ static void destroy_chunks(struct chunk_data *chunks) destroy_allocator(&chunks->allocator); } +static void output_ast(CompilerState *compiler_state, const struct arguments *args) { + if (args->astdump) { + if (args->table_ast) + raviX_dump_ast(compiler_state, stdout); + else + raviX_output_ast(compiler_state, stdout); + } +} + static int do_code(C_MemoryAllocator *allocator, const char *code, const struct arguments *args) { int rc = 0; @@ -130,9 +139,7 @@ static int do_code(C_MemoryAllocator *allocator, const char *code, const struct fprintf(stderr, "%s\n", raviX_get_last_error(compiler_state)); goto L_exit; } - if (args->astdump) { - raviX_output_ast(compiler_state, stdout); - } + output_ast(compiler_state, args); rc = raviX_ast_lower(compiler_state); if (rc != 0) { fprintf(stderr, "%s\n", raviX_get_last_error(compiler_state)); @@ -143,18 +150,14 @@ static int do_code(C_MemoryAllocator *allocator, const char *code, const struct fprintf(stderr, "%s\n", raviX_get_last_error(compiler_state)); goto L_exit; } - if (args->astdump) { - raviX_output_ast(compiler_state, stdout); - } + output_ast(compiler_state, args); if (args->simplify_ast) { rc = raviX_ast_simplify(compiler_state); if (rc != 0) { fprintf(stderr, "%s\n", raviX_get_last_error(compiler_state)); goto L_exit; } - if (args->astdump) { - raviX_output_ast(compiler_state, stdout); - } + output_ast(compiler_state, args); } LinearizerState *linearizer = raviX_init_linearizer(compiler_state); rc = raviX_ast_linearize(linearizer);