diff --git a/lib/compiler.c b/lib/compiler.c index 42ee972..bbaf08e 100644 --- a/lib/compiler.c +++ b/lib/compiler.c @@ -62,7 +62,6 @@ void compiler_run(compiler_t* compiler, node_t* node) assert(node->children.size == 1); compiler_run(compiler, node_child(node, 0)); mod_push_instr(compiler->mod, OP_ASSERT, RZ_NO_PARAM); - } break; default: { diff --git a/lib/loader.c b/lib/loader.c index 068e228..dd9268c 100644 --- a/lib/loader.c +++ b/lib/loader.c @@ -16,7 +16,7 @@ void loader_free(loader_t* loader) assert(loader); } -void loader_ldfile(loader_t* loader, char const* path, int debug) +int loader_ldfile(loader_t* loader, char const* path, int debug) { assert(loader); str_t source; @@ -46,7 +46,7 @@ void loader_ldfile(loader_t* loader, char const* path, int debug) if (str_empty(&source)) { str_free(&source); - return; + return -1; } tysy_t tysy; @@ -104,7 +104,20 @@ void loader_ldfile(loader_t* loader, char const* path, int debug) vm_t vm; vm_init(&vm, &err); - vm_exec_mod(&vm, &mod); + int status = vm_exec_mod(&vm, &mod); + + if (status != 0) + { + vm_free(&vm); + mod_free(&mod); + parser_free(&parser); + lexer_free(&lex); + str_free(&source); + tysy_free(&tysy); + err_free(&err); + + return status; + } if (debug) { @@ -142,4 +155,5 @@ void loader_ldfile(loader_t* loader, char const* path, int debug) } err_free(&err); + return 0; } diff --git a/lib/loader.h b/lib/loader.h index 91f39f7..7bd4950 100644 --- a/lib/loader.h +++ b/lib/loader.h @@ -10,6 +10,6 @@ typedef struct { void loader_init(loader_t* loader); void loader_free(loader_t* loader); -void loader_ldfile(loader_t* loader, char const* path, int debug); +int loader_ldfile(loader_t* loader, char const* path, int debug); #endif diff --git a/lib/vm.c b/lib/vm.c index 6c495cb..15b4875 100644 --- a/lib/vm.c +++ b/lib/vm.c @@ -43,7 +43,7 @@ size_t vm_stack_str(vm_t* vm, char* buffer, size_t size) return sz; } -void vm_exec_mod(vm_t* vm, mod_t* mod) +int vm_exec_mod(vm_t* vm, mod_t* mod) { assert(vm); vm->pc = 0; @@ -55,11 +55,13 @@ void vm_exec_mod(vm_t* vm, mod_t* mod) mod->program.ops[vm->pc], mod->program.params[vm->pc]); - if (!status) + if (status != 0) { - return; + return status; } } + + return 0; } int vm_exec_instr(vm_t* vm, mod_t* mod, Opcode op, param_t param) @@ -102,7 +104,7 @@ int vm_exec_instr(vm_t* vm, mod_t* mod, Opcode op, param_t param) { fprintf(stderr, "\33[31mASSERT\33[0m[:%d] assertion failed\n", value->line); - return 0; + return -1; } vm->pc++; @@ -116,5 +118,5 @@ int vm_exec_instr(vm_t* vm, mod_t* mod, Opcode op, param_t param) }; } - return 1; + return 0; } diff --git a/lib/vm.h b/lib/vm.h index b93080f..9660207 100644 --- a/lib/vm.h +++ b/lib/vm.h @@ -22,7 +22,7 @@ param_t vm_pop(vm_t* vm); size_t vm_stack_str(vm_t* vm, char* buffer, size_t size); -void vm_exec_mod(vm_t* vm, mod_t* mod); +int vm_exec_mod(vm_t* vm, mod_t* mod); int vm_exec_instr(vm_t* vm, mod_t* mod, Opcode op, param_t param); #endif diff --git a/src/main.c b/src/main.c index b2e9863..bf2f116 100644 --- a/src/main.c +++ b/src/main.c @@ -54,7 +54,13 @@ int main(int argc, char** argv) while (optind < argc) { - loader_ldfile(&loader, argv[optind++], debug); + int status = loader_ldfile(&loader, argv[optind++], debug); + + if (status != 0) + { + loader_free(&loader); + return status; + } } loader_free(&loader); diff --git a/tests/acceptances/assert.roza b/tests/acceptances/assert.roza new file mode 100644 index 0000000..dfeb590 --- /dev/null +++ b/tests/acceptances/assert.roza @@ -0,0 +1 @@ +assert true \ No newline at end of file diff --git a/tests/acceptances/run.sh b/tests/acceptances/run.sh new file mode 100755 index 0000000..077c1f8 --- /dev/null +++ b/tests/acceptances/run.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +H="========" +FILES="$(find . -name '*.roza' | sort)" +TOTAL=0 +PASSED=0 + +FAILED_LST="" +PASSED_LST="" +TRACE_LST="" + +for file in $FILES +do + TRACE="$(roza $file 2>&1)" + RES=$? + + # echo -en "\e[33m$file\e[0m ... " + + if [ $RES -ne 0 ] + then + echo + echo -e "$file \e[31mfailed\e[0m" + echo "$TRACE" + echo + FAILED_LST="$FAILED_LST $file " + TRACE_LST="$TRACE_LST $TRACE " + else + PASSED=$(($PASSED + 1)) + PASSED_LST="$PASSED_LST $file" + fi + + TOTAL=$(($TOTAL + 1)) +done + +echo + +if [ $PASSED -eq $TOTAL ] +then + echo -e "\e[32m$H All tests passed $H\e[0m" +else + echo -e "\e[31m$H Some tests failed $H\e[0m" +fi + +echo "PASSED: $PASSED" +for ok in $PASSED_LST +do + echo -e "\t$ok \e[32mpassed\e[0m" +done + +echo + +echo "FAILED: $(($TOTAL - $PASSED))" +for ko in $FAILED_LST +do + echo -e "\t$ko \e[31mfailed\e[0m" +done + +echo +echo "TOTAL: $TOTAL"