Skip to content

Commit

Permalink
#198 sync upstream and add api to generate ast dump in the new code f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
dibyendumajumdar committed Nov 12, 2022
1 parent e31da6c commit a874596
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
7 changes: 6 additions & 1 deletion ravicomp/src/ast_printer_n.c
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,12 @@ void raviX_dump_ast(CompilerState *compiler_state, FILE *fp)
{
TextBuffer mbuf;
raviX_buffer_init(&mbuf, 1024);
raviX_dump_ast_node(&mbuf, compiler_state->main_function, 0);
raviX_dump_ast_to_buffer(compiler_state, &mbuf);
fputs(mbuf.buf, fp);
raviX_buffer_free(&mbuf);
}

void raviX_dump_ast_to_buffer(CompilerState *compiler_state, TextBuffer *mbuf)
{
raviX_dump_ast_node(mbuf, compiler_state->main_function, 0);
}
1 change: 1 addition & 0 deletions ravicomp/src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ typedef struct ParserState {

void raviX_print_ast_node(TextBuffer *buf, AstNode *node, int level); /* output the AST structure recursively */
void raviX_dump_ast_node(TextBuffer *buf, AstNode *node, int level);
void raviX_dump_ast_to_buffer(CompilerState *compiler_state, TextBuffer *mbuf);
const char *raviX_get_type_name(ravitype_t tt);
AstNode *raviX_cast_to_integer(CompilerState *compiler_state, AstNode *subexpr);

Expand Down
9 changes: 9 additions & 0 deletions ravicomp/src/ravi_binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ int raviX_compile(struct Ravi_CompilerInterface *compiler_interface)
{
int rc = 0;
int dump_ir = 0;
int dump_ast = 0;
if (compiler_interface->compiler_options != NULL) {
dump_ir = strstr(compiler_interface->compiler_options, "--dump-ir") != NULL;
dump_ast = strstr(compiler_interface->compiler_options, "--dump-ast") != NULL;
}
compiler_interface->generated_code = NULL;
CompilerState *compiler_state = raviX_init_compiler(compiler_interface->memory_allocator);
Expand All @@ -46,6 +48,13 @@ int raviX_compile(struct Ravi_CompilerInterface *compiler_interface)
compiler_interface->error_message(compiler_interface->context, raviX_get_last_error(compiler_state));
goto L_exit;
}
if (dump_ast) {
TextBuffer mbuf;
raviX_buffer_init(&mbuf, 1024);
raviX_dump_ast_to_buffer(compiler_state, &mbuf);
compiler_interface->generated_code = mbuf.buf;
goto L_exit;
}
rc = raviX_ast_lower(compiler_state);
if (rc != 0) {
compiler_interface->error_message(compiler_interface->context, raviX_get_last_error(compiler_state));
Expand Down
58 changes: 55 additions & 3 deletions src/ravi_complib.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static int load_and_compile_file(lua_State* L) {
}
// FIXME we should use pcall here
int rc = load_and_compile_internal(L, s, options);
free((void *)s);
free((void*)s);
if (rc == 0) {
luaL_error(L, "Failed to compile");
}
Expand Down Expand Up @@ -254,8 +254,60 @@ static int generate(lua_State* L) {
}
}

static const luaL_Reg ravilib[] = {
{"load", load_and_compile}, {"loadfile", load_and_compile_file}, {"compile", generate}, {NULL, NULL}};
/* Compile given chunk of code and generate C code, optional arg specifies name of main function */
static int parse(lua_State* L) {
const char* s = luaL_checkstring(L, 1);
const char* mainfunc = NULL;
if (lua_isstring(L, 2)) {
mainfunc = luaL_checkstring(L, 2);
}
struct CompilerContext ccontext = {.L = L, .jit = G(L)->ravi_state};
C_MemoryAllocator allocator = {.arena = create_mspace(0, 0),
.create_arena = create_mspace,
.destroy_arena = destroy_mspace,
.calloc = mspace_calloc,
.realloc = mspace_realloc,
.free = mspace_free};
struct Ravi_CompilerInterface ravicomp_interface = {.source = s,
.source_len = strlen(s),
.source_name = "input",
.generated_code = NULL,
.context = &ccontext,
.compiler_options = "--dump-ast",
.memory_allocator = &allocator,
.debug_message = debug_message,
.error_message = error_message};
if (mainfunc) {
snprintf(ravicomp_interface.main_func_name, sizeof ravicomp_interface.main_func_name, "%s", mainfunc);
}
else {
#ifdef USE_MIRJIT
snprintf(ravicomp_interface.main_func_name, sizeof ravicomp_interface.main_func_name, "__luachunk_%lld",
ccontext.jit->id++);
#else
snprintf(ravicomp_interface.main_func_name, sizeof ravicomp_interface.main_func_name, "mymain");
#endif
}
int rc = raviX_compile(&ravicomp_interface);
if (rc == 0) {
lua_pushstring(L, ravicomp_interface.generated_code);
raviX_release(&ravicomp_interface);
destroy_mspace(allocator.arena);
return 1;
}
else {
raviX_release(&ravicomp_interface);
destroy_mspace(allocator.arena);
lua_error(L);
return 0;
}
}

static const luaL_Reg ravilib[] = {{"load", load_and_compile},
{"loadfile", load_and_compile_file},
{"compile", generate},
{"parse", parse},
{NULL, NULL}};

int(raviopen_compiler)(lua_State* L) {
luaL_newlib(L, ravilib);
Expand Down

0 comments on commit a874596

Please sign in to comment.