diff --git a/CMakeLists.txt b/CMakeLists.txt index 6dd9dea..4ef0597 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,4 +79,11 @@ add_executable(tastwalk tests/tastwalk.c tests/tcommon.c tests/tcommon.h) target_link_libraries(tastwalk ravicomp) target_include_directories(tastwalk PUBLIC "${CMAKE_CURRENT_BINARY_DIR}" + PUBLIC "${RaviCompiler_SOURCE_DIR}/include") + +add_executable(trun tests/trun.c tests/tcommon.c tests/tcommon.h) +target_link_libraries(trun ravicomp) +target_include_directories(trun + PUBLIC "${CMAKE_CURRENT_BINARY_DIR}" + PRIVATE "${RaviCompiler_SOURCE_DIR}/src" PUBLIC "${RaviCompiler_SOURCE_DIR}/include") \ No newline at end of file diff --git a/include/ravi_compiler.h b/include/ravi_compiler.h index ee19443..6effbb6 100644 --- a/include/ravi_compiler.h +++ b/include/ravi_compiler.h @@ -544,6 +544,7 @@ static inline void raviX_buffer_reset(membuff_t *mb) { mb->pos = 0; } /* following convert input to string before adding */ RAVICOMP_EXPORT void raviX_buffer_add_string(membuff_t *mb, const char *str); +RAVICOMP_EXPORT void raviX_buffer_add_bytes(membuff_t *mb, const char *str, size_t len); RAVICOMP_EXPORT void raviX_buffer_add_fstring(membuff_t *mb, const char *str, ...) FORMAT_ATTR(2); /* strncpy() replacement with guaranteed 0 termination */ diff --git a/src/implementation.h b/src/implementation.h index 50f865b..f9eac9c 100644 --- a/src/implementation.h +++ b/src/implementation.h @@ -243,6 +243,7 @@ struct function_expression { struct var_type type; unsigned int is_vararg : 1; unsigned int is_method : 1; + uint32_t proc_id; /* Backend allocated id */ struct ast_node *parent_function; /* parent function or NULL if main chunk */ struct block_scope *main_block; /* the function's main block */ struct ast_node_list *function_statement_list; /* statements in this block */ diff --git a/src/linearizer.c b/src/linearizer.c index 86a8c12..bead8f7 100644 --- a/src/linearizer.c +++ b/src/linearizer.c @@ -395,7 +395,8 @@ static struct proc *allocate_proc(struct linearizer_state *linearizer, struct as assert(function_expr->type == EXPR_FUNCTION); struct proc *proc = raviX_allocator_allocate(&linearizer->proc_allocator, 0); proc->function_expr = function_expr; - proc->id = ptrlist_size((struct ptr_list *)linearizer->all_procs); + proc->id = ptrlist_size((struct ptr_list *)linearizer->all_procs)+1; // so that 0 is not assigned + function_expr->function_expr.proc_id = proc->id; ptrlist_add((struct ptr_list **)&linearizer->all_procs, proc, &linearizer->ptrlist_allocator); if (linearizer->current_proc) { proc->parent = linearizer->current_proc; diff --git a/src/membuf.c b/src/membuf.c index 4282923..0d80559 100644 --- a/src/membuf.c +++ b/src/membuf.c @@ -54,15 +54,19 @@ void raviX_buffer_reserve(membuff_t *mb, size_t n) } } void raviX_buffer_free(membuff_t *mb) { free(mb->buf); } -void raviX_buffer_add_string(membuff_t *mb, const char *str) +void raviX_buffer_add_bytes(membuff_t *mb, const char *str, size_t len) { - size_t len = strlen(str); size_t required_size = mb->pos + len + 1; /* extra byte for NULL terminator */ raviX_buffer_resize(mb, required_size); assert(mb->allocated_size - mb->pos > len); raviX_string_copy(&mb->buf[mb->pos], str, mb->allocated_size - mb->pos); mb->pos += len; } +void raviX_buffer_add_string(membuff_t *mb, const char *str) +{ + size_t len = strlen(str); + raviX_buffer_add_bytes(mb, str, len); +} void raviX_buffer_add_fstring(membuff_t *mb, const char *fmt, ...) { diff --git a/src/parser.c b/src/parser.c index 4b4db58..3c4b113 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1470,6 +1470,7 @@ static struct ast_node *new_function(struct parser_state *parser) set_type(&node->function_expr.type, RAVI_TFUNCTION); node->function_expr.is_method = false; node->function_expr.is_vararg = false; + node->function_expr.proc_id = 0; node->function_expr.args = NULL; node->function_expr.child_functions = NULL; node->function_expr.upvalues = NULL; diff --git a/tests/expected/results.expected b/tests/expected/results.expected index 765a0b0..f50de46 100644 --- a/tests/expected/results.expected +++ b/tests/expected/results.expected @@ -5,7 +5,7 @@ end function() return end -define Proc%0 +define Proc%1 L0 (entry) RET {L1} L1 (exit) @@ -18,7 +18,7 @@ function() return 1 end -define Proc%0 +define Proc%1 L0 (entry) RET {1 Kint(0)} {L1} L1 (exit) @@ -43,7 +43,7 @@ function() , 'hello' end -define Proc%0 +define Proc%1 L0 (entry) RET {42 Kint(0), 4.200000000000 Kflt(1), true, 'hello' Ks(2)} {L1} L1 (exit) @@ -64,7 +64,7 @@ function() --[primary end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {a} {T(0)} RET {T(0)} {L1} @@ -86,7 +86,7 @@ function() 2 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) ADDii {1 Kint(0), 2 Kint(1)} {Tint(0)} RET {Tint(0)} {L1} @@ -124,7 +124,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) POW {2 Kint(0), 3 Kint(1)} {Tflt(0)} MULii {5 Kint(2), 4 Kint(3)} {Tint(0)} @@ -148,7 +148,7 @@ function() 1 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) ADDii {1 Kint(0), 1 Kint(0)} {Tint(0)} RET {Tint(0)} {L1} @@ -178,7 +178,7 @@ function() 1 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) ADDii {1 Kint(0), 1 Kint(0)} {Tint(0)} ADDii {Tint(0), 1 Kint(0)} {Tint(1)} @@ -217,7 +217,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) DIVii {3 Kint(1), 5 Kint(2)} {Tflt(0)} MULfi {Tflt(0), 4 Kint(3)} {Tflt(1)} @@ -241,7 +241,7 @@ function() 5 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) IDIV {4.200000000000 Kflt(0), 5 Kint(1)} {T(0)} RET {T(0)} {L1} @@ -255,7 +255,7 @@ function() return 0.0000000000000000 end -define Proc%0 +define Proc%1 L0 (entry) RET {0.000000000000 Kflt(0)} {L1} L1 (exit) @@ -268,7 +268,7 @@ function() return 0 end -define Proc%0 +define Proc%1 L0 (entry) RET {0 Kint(0)} {L1} L1 (exit) @@ -295,7 +295,7 @@ function() 1 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) UNMi {0 Kint(0)} {Tint(0)} IDIV {Tint(0), 1 Kint(1)} {Tint(1)} @@ -324,7 +324,7 @@ function() --[unary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) UNMi {1 Kint(1)} {Tint(0)} POW {3 Kint(0), Tint(0)} {Tflt(0)} @@ -379,7 +379,7 @@ function() --[suffixed expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) ADDii {1 Kint(0), 1 Kint(0)} {Tint(0)} ADDii {50 Kint(1), 50 Kint(1)} {Tint(1)} @@ -433,7 +433,7 @@ function() --[suffixed expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) UNMi {2 Kint(0)} {Tint(0)} SUBii {31 Kint(1), 2 Kint(0)} {Tint(1)} @@ -487,7 +487,7 @@ function() 3.0000000000000000 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) POW {3 Kint(0), 0 Kint(1)} {Tflt(0)} UNMf {Tflt(0)} {Tflt(1)} @@ -528,7 +528,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) BANDii {170 Kint(2), 253 Kint(3)} {Tint(0)} BXOR {204.000000000000 Kflt(1), Tint(0)} {Tint(1)} @@ -572,7 +572,7 @@ function() --[suffixed expr end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) BNOT {4080 Kint(0)} {T(0)} BOR {T(0), 4080 Kint(0)} {T(1)} @@ -606,7 +606,7 @@ function() --[unary expr end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) UNMf {100024.000000000000 Kflt(0)} {Tflt(0)} BNOT {Tflt(0)} {T(0)} @@ -668,7 +668,7 @@ function() 2 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) SHLii {100 Kint(0), 6 Kint(1)} {Tint(0)} UNMi {4 Kint(2)} {Tint(1)} @@ -733,7 +733,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) POW {3 Kint(1), 2 Kint(0)} {Tflt(0)} POW {2 Kint(0), Tflt(0)} {Tflt(1)} @@ -799,7 +799,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) POW {2 Kint(0), 3 Kint(1)} {Tflt(0)} MULfi {Tflt(0), 4 Kint(2)} {Tflt(1)} @@ -907,7 +907,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) UNMi {2 Kint(1)} {Tint(0)} POW {2.000000000000 Kflt(0), Tint(0)} {Tflt(0)} @@ -998,7 +998,7 @@ function() --[unary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) NOT {nil} {T(2)} MOV {T(2)} {T(1)} @@ -1079,7 +1079,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) UNMi {3 Kint(0)} {Tint(0)} SUBii {Tint(0), 1 Kint(1)} {Tint(1)} @@ -1204,7 +1204,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) POW {2 Kint(0), 2 Kint(0)} {Tflt(0)} UNMf {Tflt(0)} {Tflt(1)} @@ -1236,7 +1236,7 @@ function() end function() end -define Proc%0 +define Proc%1 L0 (entry) BR {L1} L1 (exit) @@ -1532,7 +1532,7 @@ function() 244 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) BANDii {170 Kint(2), 253 Kint(3)} {Tint(0)} BXORii {204 Kint(1), Tint(0)} {Tint(1)} @@ -1581,7 +1581,7 @@ function() 244 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) BANDii {253 Kint(0), 170 Kint(1)} {Tint(0)} BXORii {Tint(0), 204 Kint(2)} {Tint(1)} @@ -1622,7 +1622,7 @@ function() 16 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) ADDii {15 Kint(1), 1 Kint(2)} {Tint(0)} BANDii {240 Kint(0), Tint(0)} {Tint(1)} @@ -1678,7 +1678,7 @@ function() 2 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) POW {3 Kint(0), 4 Kint(1)} {Tflt(0)} POW {2 Kint(2), 3 Kint(0)} {Tflt(1)} @@ -1734,7 +1734,7 @@ function() --[suffixed expr end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {true} {T(1)} CBR {T(1)} {L5, L4} @@ -1776,7 +1776,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {true} {T(0)} CBR {T(0)} {L3, L2} @@ -1857,7 +1857,7 @@ function() true --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {1 Kint(0)} {T(2)} CBR {T(2)} {L7, L6} @@ -1945,7 +1945,7 @@ function() false --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {nil} {T(2)} CBR {T(2)} {L6, L7} @@ -2081,7 +2081,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {1 Kint(0)} {T(1)} CBR {T(1)} {L5, L4} @@ -2221,7 +2221,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {b} {T(2)} MOV {T(2)} {T(1)} @@ -2339,7 +2339,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) LIii {2 Kint(0), 3 Kint(1)} {T(2)} MOV {T(2)} {T(1)} @@ -2446,7 +2446,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(2)} LOADGLOBAL {y} {T(3)} @@ -2539,13 +2539,13 @@ function() end end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(1)} + CLOSURE {Proc%2} {T(1)} STOREGLOBAL {x, T(1)} BR {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) MOV {1 Kint(0)} {Tint(0)} MOV {10 Kint(1)} {Tint(1)} @@ -2625,25 +2625,25 @@ function() end end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(1)} + CLOSURE {Proc%2} {T(1)} STOREGLOBAL {x, T(1)} BR {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) MOV {1 Kint(0)} {local(a, 0)} - CLOSURE {Proc%2} {T(1)} + CLOSURE {Proc%3} {T(1)} STOREGLOBAL {y, T(1)} BR {L1} L1 (exit) -define Proc%2 +define Proc%3 L0 (entry) - CLOSURE {Proc%3} {T(0)} + CLOSURE {Proc%4} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%3 +define Proc%4 L0 (entry) RET {Upval(0)} {L1} L1 (exit) @@ -2662,7 +2662,7 @@ function() 1 --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) RET {1 Kint(0)} {L1} L1 (exit) @@ -2681,7 +2681,7 @@ function() 'hello' --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) RET {'hello' Ks(0)} {L1} L1 (exit) @@ -2702,7 +2702,7 @@ function() } --[table constructor end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) NEWTABLE {T(0)} RET {T(0)} {L1} @@ -2724,7 +2724,7 @@ function() } --[table constructor end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) NEWIARRAY {T(0)} RET {T(0)} {L1} @@ -2746,7 +2746,7 @@ function() } --[table constructor end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) NEWFARRAY {T(0)} RET {T(0)} {L1} @@ -2768,12 +2768,12 @@ function() end --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(0)} + CLOSURE {Proc%2} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) BR {L1} L1 (exit) @@ -2792,7 +2792,7 @@ function() 54.3999999999999986 --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) RET {54.400000000000 Kflt(0)} {L1} L1 (exit) @@ -2819,7 +2819,7 @@ function() --[suffixed expr end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {a} {T(0)} TOTYPE {'User.Type' Ks(0)} {T(0)} @@ -2870,7 +2870,7 @@ function() --[indexed assign end] } --[table constructor end] end -define Proc%0 +define Proc%1 L0 (entry) NEWTABLE {T(0)} TPUTik {T(0), 1 Kint(0), 1 Kint(0)} @@ -2921,7 +2921,7 @@ function() --[indexed assign end] } --[table constructor end] end -define Proc%0 +define Proc%1 L0 (entry) NEWTABLE {T(0)} LOADGLOBAL {a} {T(1)} @@ -2969,7 +2969,7 @@ function() --[indexed assign end] } --[table constructor end] end -define Proc%0 +define Proc%1 L0 (entry) NEWTABLE {T(0)} LOADGLOBAL {b} {T(1)} @@ -3043,7 +3043,7 @@ function() } --[table constructor end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) NEWIARRAY {T(0)} IAPUT {T(0), 1 Kint(0), 5.500000000000 Kflt(1)} @@ -3117,7 +3117,7 @@ function() } --[table constructor end] --[unary expr end] end -define Proc%0 +define Proc%1 L0 (entry) NEWFARRAY {T(0)} FAPUT {T(0), 1 Kint(0), 4 Kint(1)} @@ -3155,7 +3155,7 @@ function() false end end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -3235,7 +3235,7 @@ function() 5 end end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -3283,7 +3283,7 @@ function() 'hi' end end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -3349,7 +3349,7 @@ function() end end end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -3421,7 +3421,7 @@ function() 4 end end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -3455,7 +3455,7 @@ function() 2 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {1 Kint(0)} {T(0)} CBR {T(0)} {L2, L3} @@ -3490,7 +3490,7 @@ function() 5 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {3 Kint(0)} {T(1)} CBR {T(1)} {L4, L5} @@ -3523,7 +3523,7 @@ function() 2 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {1 Kint(0)} {T(0)} CBR {T(0)} {L3, L2} @@ -3558,7 +3558,7 @@ function() 5 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {3 Kint(0)} {T(1)} CBR {T(1)} {L5, L4} @@ -3605,7 +3605,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} GETik {T(0), 1 Kint(0)} {T(1)} @@ -3640,7 +3640,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} CALL {T(0)} {T(0..)} @@ -3685,7 +3685,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} GETik {T(0), 1 Kint(0)} {T(1)} @@ -3731,7 +3731,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} GETik {T(0), 1 Kint(0)} {T(1)} @@ -3784,7 +3784,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} GETik {T(0), 1 Kint(0)} {T(1)} @@ -3845,7 +3845,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} CALL {T(0)} {T(0)} @@ -3904,7 +3904,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {y} {T(0)} LOADGLOBAL {x} {T(1)} @@ -3941,7 +3941,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) STOREGLOBAL {x, 1 Kint(0)} BR {L1} @@ -3979,7 +3979,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) STOREGLOBAL {x, 2 Kint(1)} BR {L1} @@ -4027,7 +4027,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} PUTik {T(0), 1 Kint(0), 1 Kint(0)} @@ -4084,7 +4084,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} LOADGLOBAL {b} {T(2)} @@ -4152,7 +4152,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} GETik {T(0), 1 Kint(0)} {T(1)} @@ -4195,7 +4195,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} CALL {T(0)} {T(0..)} @@ -4246,7 +4246,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} CALL {T(0)} {T(0)} @@ -4328,7 +4328,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} CALL {T(0)} {T(0)} @@ -4383,7 +4383,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) STOREGLOBAL {y, 2 Kint(1)} STOREGLOBAL {x, 1 Kint(0)} @@ -4450,7 +4450,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {f} {T(2)} CALL {T(2)} {T(2..)} @@ -4575,7 +4575,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} LOADGLOBAL {y} {T(2)} @@ -4714,7 +4714,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} GETik {T(0), 1 Kint(0)} {T(1)} @@ -4788,7 +4788,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {y} {T(2)} LOADGLOBAL {x} {T(3)} @@ -4881,7 +4881,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {z} {T(3)} LOADGLOBAL {y} {T(4)} @@ -4998,7 +4998,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) STOREGLOBAL {i, 3 Kint(0)} LOADGLOBAL {a} {T(1)} @@ -5134,7 +5134,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} LOADGLOBAL {y} {T(1)} @@ -5164,7 +5164,7 @@ function() --[expressions] 0 end -define Proc%0 +define Proc%1 L0 (entry) MOV {0 Kint(0)} {local(i, 0)} BR {L1} @@ -5194,7 +5194,7 @@ function() , 2 end -define Proc%0 +define Proc%1 L0 (entry) MOV {2 Kint(1)} {local(j, 1)} MOV {1 Kint(0)} {local(i, 0)} @@ -5225,7 +5225,7 @@ function() --[primary end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {a} {T(0)} MOV {T(0)} {local(a, 0)} @@ -5256,7 +5256,7 @@ function() --[primary end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {b} {T(0)} MOV {T(0)} {local(a, 0)} @@ -5303,7 +5303,7 @@ function() --[primary end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} LOADGLOBAL {y} {T(1)} @@ -5344,7 +5344,7 @@ function() 3 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) ADDii {local(a, 0), 3 Kint(0)} {Tint(0)} RET {Tint(0)} {L1} @@ -5404,7 +5404,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {t} {T(0)} DIVii {local(i, 0), 5 Kint(0)} {Tflt(0)} @@ -5450,7 +5450,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) IAGETik {local(t, 0), 0 Kint(0)} {Tint(0)} RET {Tint(0)} {L1} @@ -5494,7 +5494,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {f} {T(0)} CALL {T(0)} {T(0)} @@ -5540,7 +5540,7 @@ function() --[suffix list end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {x} {T(0)} GETsk {T(0), 'y' Ks(0)} {T(1)} @@ -5614,7 +5614,7 @@ function() return false end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -5675,7 +5675,7 @@ function() --[primary end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) LEN {local(t, 0)} {T(0)} TOINT {T(0)} @@ -5741,12 +5741,12 @@ function() --[expression statement end] end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(0)} + CLOSURE {Proc%2} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) TOTABLE {local(t, 0)} TOINT {local(i, 1)} @@ -5790,7 +5790,7 @@ function() goto L1 return end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -5888,7 +5888,7 @@ function() --[expression statement end] goto l1 end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -6059,36 +6059,36 @@ function() --[suffixed expr end] end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(0)} + CLOSURE {Proc%2} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) MOV {1 Kint(0)} {local(a, 0)} - CLOSURE {Proc%2} {T(0)} + CLOSURE {Proc%3} {T(0)} MOV {T(0)} {local(y, 1)} MOV {5 Kint(1)} {local(a, 2)} - CLOSURE {Proc%4} {T(0)} + CLOSURE {Proc%5} {T(0)} MOV {T(0)} {local(z, 3)} RET {local(y, 1), local(z, 3)} {L1} L1 (exit) -define Proc%2 +define Proc%3 L0 (entry) - CLOSURE {Proc%3} {T(0)} + CLOSURE {Proc%4} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%3 +define Proc%4 L0 (entry) RET {Upval(0)} {L1} L1 (exit) -define Proc%4 +define Proc%5 L0 (entry) - CLOSURE {Proc%5} {T(0)} + CLOSURE {Proc%6} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%5 +define Proc%6 L0 (entry) RET {Upval(0)} {L1} L1 (exit) @@ -6280,12 +6280,12 @@ function() ::l4:: end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(0)} + CLOSURE {Proc%2} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) BR {L2} L1 (exit) @@ -6416,12 +6416,12 @@ function() end end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(0)} + CLOSURE {Proc%2} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) BR {L2} L1 (exit) @@ -6538,12 +6538,12 @@ function() ::L1:: end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(0)} + CLOSURE {Proc%2} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) BR {L2} L1 (exit) @@ -6644,14 +6644,14 @@ function() --[expression statement end] end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(0)} + CLOSURE {Proc%2} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) - CLOSURE {Proc%2} {T(0)} + CLOSURE {Proc%3} {T(0)} NEWTABLE {T(1)} LOADGLOBAL {f} {T(2)} CALL {T(2)} {T(2..)} @@ -6659,7 +6659,7 @@ L0 (entry) CALL {T(0), T(1)} {T(0..)} BR {L1} L1 (exit) -define Proc%2 +define Proc%3 L0 (entry) BR {L1} L1 (exit) @@ -6839,12 +6839,12 @@ function() --[suffixed expr end] end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(0)} + CLOSURE {Proc%2} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) MOV {1 Kint(0)} {Tint(0)} MOV {500 Kint(1)} {Tint(1)} @@ -7658,14 +7658,14 @@ function() --[primary end] --[suffixed expr end] end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(1)} + CLOSURE {Proc%2} {T(1)} STOREGLOBAL {matmul, T(1)} LOADGLOBAL {matmul} {T(0)} RET {T(0)} {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) TOTABLE {local(a, 0)} TOTABLE {local(b, 1)} @@ -8009,7 +8009,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) UNMi {3 Kint(0)} {Tint(0)} MULii {4 Kint(1), 5 Kint(2)} {Tint(1)} @@ -8096,7 +8096,7 @@ function() --[binary expr end] --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) UNMi {3 Kint(0)} {Tint(0)} MOD {Tint(0), 5 Kint(1)} {Tint(1)} @@ -8212,7 +8212,7 @@ function() 3 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) POW {2.000000000000 Kflt(0), 8 Kint(1)} {Tflt(0)} UNMi {1 Kint(2)} {Tint(0)} @@ -8325,7 +8325,7 @@ function() 3 --[binary expr end] end -define Proc%0 +define Proc%1 L0 (entry) POW {2 Kint(0), 8 Kint(1)} {Tflt(0)} UNMi {1 Kint(2)} {Tint(0)} @@ -8506,7 +8506,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) MOV {12 Kint(0)} {local(y, 1)} BR {L5} @@ -8594,7 +8594,7 @@ function() --[expression statement end] end end -define Proc%0 +define Proc%1 L0 (entry) MOV {1 Kint(0)} {Tint(0)} MOV {10 Kint(1)} {Tint(1)} @@ -8692,7 +8692,7 @@ function() --[expression statement end] end end -define Proc%0 +define Proc%1 L0 (entry) MOV {10 Kint(0)} {Tint(0)} MOV {1 Kint(1)} {Tint(1)} @@ -8763,7 +8763,7 @@ function() --[expression statement end] end end -define Proc%0 +define Proc%1 L0 (entry) BR {L2} L1 (exit) @@ -8838,7 +8838,7 @@ function() true --[repeat end] end -define Proc%0 +define Proc%1 L0 (entry) BR {L3} L1 (exit) @@ -8900,7 +8900,7 @@ function() --[repeat end] return end -define Proc%0 +define Proc%1 L0 (entry) BR {L3} L1 (exit) @@ -8965,7 +8965,7 @@ function() end return end -define Proc%0 +define Proc%1 L0 (entry) MOV {1 Kint(0)} {Tint(0)} MOV {10 Kint(1)} {Tint(1)} @@ -9213,15 +9213,15 @@ function() end end end -define Proc%0 +define Proc%1 L0 (entry) NEWTABLE {T(0)} MOV {T(0)} {local(msgs, 0)} - CLOSURE {Proc%1} {T(1)} + CLOSURE {Proc%2} {T(1)} STOREGLOBAL {Message, T(1)} BR {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) BR {L2} L1 (exit) @@ -9466,7 +9466,7 @@ function() --[expression list end] --[expression statement end] end -define Proc%0 +define Proc%1 L0 (entry) LOADGLOBAL {rawget} {T(2)} LOADGLOBAL {_G} {T(3)} @@ -9529,13 +9529,13 @@ function() --locals bar, zee end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(1)} + CLOSURE {Proc%2} {T(1)} STOREGLOBAL {foo, T(1)} BR {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) TOIARRAY {local(bar, 0)} TOFARRAY {local(zee, 1)} @@ -9564,13 +9564,13 @@ function() --locals bar, zee end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(1)} + CLOSURE {Proc%2} {T(1)} STOREGLOBAL {foo, T(1)} BR {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) TOFLT {local(bar, 0)} TOSTRING {local(zee, 1)} @@ -9599,13 +9599,13 @@ function() --locals bar, zee end end -define Proc%0 +define Proc%1 L0 (entry) - CLOSURE {Proc%1} {T(1)} + CLOSURE {Proc%2} {T(1)} STOREGLOBAL {foo, T(1)} BR {L1} L1 (exit) -define Proc%1 +define Proc%2 L0 (entry) TOCLOSURE {local(bar, 0)} TOTYPE {'My.User.Type' Ks(0)} {local(zee, 1)} diff --git a/tests/input/t00_exprs.in b/tests/input/t00_exprs.in new file mode 100644 index 0000000..8c390d8 --- /dev/null +++ b/tests/input/t00_exprs.in @@ -0,0 +1,106 @@ +return +# +return 1 +# +return 42, 4.2, true, 'hello' +# +return 1+2 +# +return 2^3-5*4 +# +return 1+1 +# +return 1+1+1 +# +return 2-3/5*4 +# +return 4.2//5 +# +return 0.0 +# +return 0 +# +return -0//1 +# +return 3^-1 +# +return (1 + 1)^(50 + 50) +# +return (-2)^(31 - 2) +# +return (-3^0 + 5) // 3.0 +# +return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD +# +return ~(~0xFF0 | 0xFF0) +# +return ~~-100024.0 +# +return ((100 << 6) << -4) >> 2 +# +return 2^3^2 == 2^(3^2) +# +return 2^3*4 == (2^3)*4 +# +return 2.0^-2 == 1/4 and -2^- -2 == - - -4 +# +return not nil and 2 and not(2>3 or 3<2) +# +return -3-1-5 == 0+0-9 +# +return -2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0 +# +return 2*1+3/3 == 3 and 1+2 .. 3*1 == '33' +# +return not(2+1 > 3*1) and 'a'..'b' > 'a' +# +return '7' .. 3 << 1 == 146 +# +return 10 >> 1 .. '9' == 0 +# +return 10 | 1 .. '9' == 27 +# +return 0xF0 | 0xCC ~ 0xAA & 0xFD == 0xF4 +# +return 0xFD & 0xAA ~ 0xCC | 0xF0 == 0xF4 +# +return 0xF0 & 0x0F + 1 == 0x10 +# +return 3^4//2^3//5 == 2 +# +return not ((true or false) and nil) +# +return true or false and nil +# +return (((1 or false) and true) or false) == true +# +return (((nil and true) or false) and true) == false +# +return -(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75 +# +return ((2<3) or 1) == true and (2<3 and 4) == 4 +# +return (x>y) and x or y == 2 +# +return {1,2,3} +# +return 1 and 2 +# +return 3 and 4 and 5 +# +return 1 or 2 +# +return 3 or 4 or 5 +# +return -3+4*5//2^3^2//9+4%10/3 == (-3)+(((4*5)//(2^(3^2)))//9)+((4%10)/3) +# +return -3%5 == 2 and -3+5 == 2 +# +return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 +# +return -((2^8 + -(-1)) % 8)//2 * 4 - 3 +# +return function () + (function () end){f()} + end +# diff --git a/tests/input/t01_globals.in b/tests/input/t01_globals.in new file mode 100644 index 0000000..fb44384 --- /dev/null +++ b/tests/input/t01_globals.in @@ -0,0 +1,54 @@ +print 'hello' +# +x = 1 +# +x = 1, 2 +# +x[1] = 1 +# +x[1] = b +# +x[1][1] = b +# +x() +# +return x[1] +# +return x() +# +return x[1]() +# +return x[1]:name() +# +return x[1]:name(1,2) +# +return x(), y() +# +return y(x()) +# +x()[1] +# +x()[1](a,b) +# +x,y = 1,2 +# +x,y = f() +# +x[1],y[1],c,d = 1,z() +# +x[1][2],y[1],c,d = 1,z() +# +x,y = y,x +# +x,y,z = z,y,x +# +i = 3; i, a[i] = i+1, 20 +# +x(y(a[10],5,z()))[1] = 9 +# +return f()[1] +# +return x.y[1] +# +return (b or a)+1 == 2 and (10 or a)+1 == 11 +# diff --git a/tests/input/t02_locals.in b/tests/input/t02_locals.in new file mode 100644 index 0000000..cc852f1 --- /dev/null +++ b/tests/input/t02_locals.in @@ -0,0 +1,24 @@ +local i = 0 +# +local i, j = 1, 2 +# +local a = a +# +local a = b +# +local a, b = x, y +# +local a: integer return a+3 +# +local i: integer; return t[i/5] +# +local t: integer[]; return t[0] +# +local t: table local len: integer = #t return len +# +_soft = rawget(_G, "_soft") or false +Z[1][2] = false or true +local _g +r()[a].name = _g and 'Dibyendu' or 'majumdar' +# + diff --git a/tests/input/t03_types.in b/tests/input/t03_types.in new file mode 100644 index 0000000..75346b1 --- /dev/null +++ b/tests/input/t03_types.in @@ -0,0 +1,56 @@ +function foo(bar: integer[], zee: number[], ...) end +# +function foo(bar: number, zee: string) end +# +function foo(bar: closure, zee: My.User.Type) end +# +return @integer 1 +# +return @string 'hello' +# +return @table {} +# +return @integer[] {} +# +return @number[] {} +# +return @closure function() end +# +return @number 54.4 +# +return @User.Type a +# +return @integer[]{[1] = 5.5, [2] = 4} +# +return @number[] {[1] = 4, [2] = 5.4} +# +local a: integer return a+3 +# +local i: integer; return t[i/5] +# +local t: integer[]; return t[0] +# +local t: integer[] if (t[1] == 5) then return true end return false +# +local t: table local len: integer = #t return len +# +return function(t: table, i: integer) i = #t end +# +function matmul(a: table, b: table) + assert(@integer(#a[1]) == #b); + local m: integer, n: integer, p: integer, x: table = #a, #a[1], #b[1], {}; + local c: table = matrix.T(b); -- transpose for efficiency + for i = 1, m do + local xi: number[] = table.numarray(p, 0.0) + x[i] = xi + for j = 1, p do + local sum: number, ai: number[], cj: number[] = 0.0, @number[](a[i]), @number[](c[j]); + -- for luajit, caching c[j] or not makes no difference; lua is not so clever + for k = 1, n do sum = sum + ai[k] * cj[k] end + xi[j] = sum; + end + end + return x + end +return matmul +# \ No newline at end of file diff --git a/tests/input/t04_stmts_1.in b/tests/input/t04_stmts_1.in new file mode 100644 index 0000000..bfd4c46 --- /dev/null +++ b/tests/input/t04_stmts_1.in @@ -0,0 +1,55 @@ +::L1:: a = 1; goto L1; return +# +::l1:: do goto l1; x = 1; ::l1:: z = 2 end y = 1; goto l1 +# +goto l1; do ::l1:: end +# +return function (a) ::L2:: if not(a < 10) then goto L1 end; a = a + 1; + goto L2; ::L1:: end +# +for i=1,10 do print(i) end +# +for i=10,1,-1 do print(i) end +# +while true do print('forever') end +# +repeat print('forever') brek until true +# +repeat print('forever') break until true return +# +for i =1,10 do if i == 2 then break end end return +# +return function (a) while a < 10 do a = a + 1 end end +# +return function (a, b, c, d, e) + if a == b then goto l1 + elseif a == c then goto l2 + elseif a == d then goto l2 + else if a == e then goto l3 + else goto l3 + end + end + ::l1:: ::l2:: ::l3:: ::l4:: +end +# +return function () + local sum + for j = 1,500 do + sum = 0.0 + for k = 1,10000 do + sum = sum + 1.0/(k*k) + end + end + return sum +end +# +local x +do + local y = 12 + goto l1 + ::l2:: x = x + 1; goto l3 + ::l1:: x = y; goto l2 +end +::l3:: ::l3_1:: assert(x == 13) +# + diff --git a/tests/input/t05_upvals.in b/tests/input/t05_upvals.in new file mode 100644 index 0000000..5163760 --- /dev/null +++ b/tests/input/t05_upvals.in @@ -0,0 +1,39 @@ +local a = 1 +function f() + return function() + return function() + a = a + 1 + return a + end + end +end +# +return function() + local a = 1 + local function y() + return function() return a end + end + local a = 5 + local function z() + return function() return a end + end + return y, z +end +# +local x +do + local y = 12 + goto l1 + ::l2:: x = x + 1; goto l3 + ::l1:: x = y; goto l2 +end +::l3:: ::l3_1:: assert(x == 13) +# +local msgs = {} +function Message (m) + if not _nomsg then + print(m) + msgs[#msgs+1] = string.sub(m, 3, -3) + end +end +# \ No newline at end of file diff --git a/tests/trun.c b/tests/trun.c new file mode 100644 index 0000000..f55c5f9 --- /dev/null +++ b/tests/trun.c @@ -0,0 +1,150 @@ +/* simple smoke test for parser */ + +#include "ravi_compiler.h" +#include "tcommon.h" +#include "ptrlist.h" +#include "allocate.h" +#include "membuf.h" + +#include +#include +#include + +DECLARE_PTR_LIST(string_list, char); + +struct chunk_data { + struct allocator string_allocator; + struct allocator ptrlist_allocator; + struct string_list *list; +}; + +/* return next line - i.e. first char following newline */ +static const char* scan_next(const char *cp, const char *endp) { + if (cp >= endp) + return NULL; + const char *nextp = strchr(cp, '\n'); + if (!nextp) + // In case we dont have line ending in new line + return endp; + nextp++; // skip past newline + if (nextp > endp) + return NULL; + return nextp; +} + +static void add_chunk(struct chunk_data *chunks, membuff_t* buf) +{ + size_t len = raviX_buffer_len(buf); + char *s = raviX_allocator_allocate(&chunks->string_allocator, len+1); + raviX_string_copy(s, raviX_buffer_data(buf), len+1); + ptrlist_add((struct ptr_list **) &chunks->list, s, &chunks->ptrlist_allocator); +} + +/* Input text is supposed to contain multiple chunks + * separated by delimiter line. + * Each chunk will be added as an item in the list + */ +static uint32_t read_chunks(const char *input, struct chunk_data *chunks, const char *delim) { + membuff_t buf; + raviX_buffer_init(&buf, 1024); + const char *cp = input; + const char *endp = input + strlen(input); + const char *nextp = cp; + uint32_t count = 0; + while (nextp) { + cp = nextp; + nextp = scan_next(cp, endp); + if (*cp == 0) + continue; + if (*cp == *delim) { + add_chunk(chunks, &buf); + count++; + raviX_buffer_reset(&buf); + } + else if (nextp) { + raviX_buffer_add_bytes(&buf, cp, nextp-cp); + } + } + if (raviX_buffer_len(&buf) > 0) { + add_chunk(chunks, &buf); + count++; + } + raviX_buffer_free(&buf); + return count; +} + +static void init_chunks(struct chunk_data *chunks) +{ + raviX_allocator_init(&chunks->ptrlist_allocator, "ptrlists", sizeof(struct ptr_list), sizeof(double), + sizeof(struct ptr_list) * 32); + raviX_allocator_init(&chunks->string_allocator, "strings", 0, sizeof(double), 1024); + chunks->list = NULL; +} + +static int do_code(const char *code) +{ + printf("%s\n", code); + int rc = 0; + struct compiler_state *container = raviX_init_compiler(); + rc = raviX_parse(container, code, strlen(code), "input"); + if (rc != 0) { + fprintf(stderr, "%s\n", raviX_get_last_error(container)); + goto L_exit; + } + raviX_output_ast(container, stdout); + rc = raviX_ast_typecheck(container); + if (rc != 0) { + fprintf(stderr, "%s\n", raviX_get_last_error(container)); + goto L_exit; + } + raviX_output_ast(container, stdout); + + struct linearizer_state *linearizer = raviX_init_linearizer(container); + + rc = raviX_ast_linearize(linearizer); + if (rc != 0) { + fprintf(stderr, "%s\n", raviX_get_last_error(container)); + goto L_linend; + } + raviX_output_linearizer(linearizer, stdout); + + L_linend: + raviX_destroy_linearizer(linearizer); + + L_exit: + raviX_destroy_compiler(container); + + return rc; +} + +int main(int argc, const char *argv[]) +{ + struct arguments args; + parse_arguments(&args, argc, argv); + + const char* code = NULL; + if (args.code) { + code = args.code; + } else if (args.filename) { + code = read_file(args.filename); + } + if (!code) { + fprintf(stderr, "No code to process\n"); + exit(1); + } + + struct chunk_data chunks; + init_chunks(&chunks); + uint32_t count = read_chunks(code, &chunks, "#"); + if (count == 0) { + fprintf(stderr, "No code to process\n"); + exit(1); + } + + const char *chunk = NULL; + FOR_EACH_PTR(chunks.list, chunk) { + do_code(chunk); + } END_FOR_EACH_PTR(chunk) + + return 0; +}