Skip to content

Commit

Permalink
#85 add option to dump ast as a Ravi code
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Nov 12, 2022
1 parent 596364a commit 48e2880
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
5 changes: 4 additions & 1 deletion tests/tcommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/tcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);
Expand Down
21 changes: 12 additions & 9 deletions tests/trun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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);
Expand Down

0 comments on commit 48e2880

Please sign in to comment.