From 556f603487a3a0a2b2c8084ae9debb8b7ad1a29d Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Tue, 23 Jul 2024 21:29:38 +0200 Subject: [PATCH] Restructure test system --- bootstrap/main.c | 125 +++++++++++------- debug.sh | 12 +- run_tests.sh | 19 ++- .../main.nat => break_continue.nat} | 0 tests/{comparison/main.nat => comparison.nat} | 0 .../main.nat => constant_prop.nat} | 0 tests/{first/main.nat => first.nat} | 0 .../main.nat => function_call_args.nat} | 0 tests/{if/main.nat => if.nat} | 0 ...in.nat => simple_variable_declaration.nat} | 0 tests/{while/main.nat => while.nat} | 0 11 files changed, 105 insertions(+), 51 deletions(-) rename tests/{break_continue/main.nat => break_continue.nat} (100%) rename tests/{comparison/main.nat => comparison.nat} (100%) rename tests/{constant_prop/main.nat => constant_prop.nat} (100%) rename tests/{first/main.nat => first.nat} (100%) rename tests/{function_call_args/main.nat => function_call_args.nat} (100%) rename tests/{if/main.nat => if.nat} (100%) rename tests/{simple_variable_declaration/main.nat => simple_variable_declaration.nat} (100%) rename tests/{while/main.nat => while.nat} (100%) diff --git a/bootstrap/main.c b/bootstrap/main.c index 24f52d2..4763af7 100644 --- a/bootstrap/main.c +++ b/bootstrap/main.c @@ -229,6 +229,18 @@ fn String string_base(String string) return result; } +fn String string_no_extension(String string) +{ + String result = {}; + auto index = string_last_ch(string, '.'); + if (index != -1) + { + result = s_get_slice(u8, string, 0, index); + } + + return result; +} + fn u64 parse_decimal(String string) { u64 value = 0; @@ -4618,65 +4630,86 @@ fn void unit_tests() } #endif +Slice(String) arguments; + #if LINK_LIBC -int main() +int main(int argc, const char* argv[]) #else extern "C" void entry_point() #endif { #if DO_UNIT_TESTS - unit_tests(); + unit_tests(); #endif + + if (argc < 2) + { + fail(); + } + Arena* global_arena = arena_init_default(KB(64)); + { + arguments.pointer = arena_allocate(global_arena, String, argc); + arguments.length = argc; + + for (u32 i = 0; i < argc; i += 1) + { + u64 len = strlen(argv[i]); + arguments.pointer[i] = (String) { + .pointer = (u8*)argv[i], + .length = len, + }; + } + } + + String source_file_path = arguments.pointer[1]; + Thread* thread = arena_allocate(global_arena, Thread, 1); thread_init(thread); mkdir("nest", 0755); - for (u32 i = 0; i < array_length(test_files); i += 1) + File file = { + .path = source_file_path, + .source = file_read(thread->arena, source_file_path), + }; + analyze_file(thread, &file); + auto test_dir = string_no_extension(file.path); + auto test_name = string_base(test_dir); + + for (u32 function_i = 0; function_i < thread->buffer.functions.length; function_i += 1) { - File file = { - .path = test_files[i], - .source = file_read(thread->arena, test_files[i]), - }; - analyze_file(thread, &file); - auto test_dir = string_dir(file.path); - auto test_name = string_base(test_dir); - - for (u32 function_i = 0; function_i < thread->buffer.functions.length; function_i += 1) - { - Function* function = &thread->buffer.functions.pointer[function_i]; - NodeIndex start_node_index = function->start; - NodeIndex stop_node_index = function->stop; - iterate_peepholes(thread, stop_node_index); - print_string(strlit("Before optimizations\n")); - print_function(thread, function); - gcm_build_cfg(thread, start_node_index, stop_node_index); - print_string(strlit("After optimizations\n")); - print_function(thread, function); - } - - auto lowered_source = c_lower(thread); - print("Transpiled to C:\n```\n{s}\n```\n", lowered_source); - - auto c_source_path = arena_join_string(thread->arena, (Slice(String)) array_to_slice(((String[]) { - strlit("nest/"), - test_name, - strlit(".c"), - }))); - - file_write(c_source_path, lowered_source); - - auto exe_path = s_get_slice(u8, c_source_path, 0, c_source_path.length - 2); - - auto command = arena_join_string(thread->arena, (Slice(String)) array_to_slice(((String[]) { - strlit("clang -g -o "), - exe_path, - strlit(" "), - c_source_path, - }))); - system((char*)command.pointer); - - thread_clear(thread); + Function* function = &thread->buffer.functions.pointer[function_i]; + NodeIndex start_node_index = function->start; + NodeIndex stop_node_index = function->stop; + iterate_peepholes(thread, stop_node_index); + print_string(strlit("Before optimizations\n")); + print_function(thread, function); + gcm_build_cfg(thread, start_node_index, stop_node_index); + print_string(strlit("After optimizations\n")); + print_function(thread, function); } + + auto lowered_source = c_lower(thread); + print("Transpiled to C:\n```\n{s}\n```\n", lowered_source); + + auto c_source_path = arena_join_string(thread->arena, (Slice(String)) array_to_slice(((String[]) { + strlit("nest/"), + test_name, + strlit(".c"), + }))); + + file_write(c_source_path, lowered_source); + + auto exe_path = s_get_slice(u8, c_source_path, 0, c_source_path.length - 2); + + auto command = arena_join_string(thread->arena, (Slice(String)) array_to_slice(((String[]) { + strlit("clang -g -o "), + exe_path, + strlit(" "), + c_source_path, + }))); + system((char*)command.pointer); + + thread_clear(thread); } diff --git a/debug.sh b/debug.sh index 49db25f..3ab7a81 100755 --- a/debug.sh +++ b/debug.sh @@ -2,15 +2,21 @@ set -e +path=$1 +echo $path + source ./compile.sh build_dir="build" exe_name="nest" exe_path=$build_dir/$exe_name +debug_flags="-g" +optimization_flags="" +bootstrap_args="$path" -compile $build_dir $exe_name "-g" ""; +compile $build_dir $exe_name $debug_flags $optimization_flags case "$OSTYPE" in - darwin*) lldb $exe_path ;; - linux*) gf2 -ex r $exe_path ;; + darwin*) lldb -- $exe_path $bootstrap_args;; + linux*) gf2 -ex r --args $exe_path $bootstrap_args;; *) echo "unknown: $OSTYPE" ;; esac diff --git a/run_tests.sh b/run_tests.sh index 1907b2d..2346231 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -5,6 +5,21 @@ source ./compile.sh build_dir="build" exe_name="nest" exe_path=$build_dir/$exe_name +debug_flags="-g" +optimization_flags="" +test_names="first" + +compile $build_dir $exe_name $debug_flags $optimization_flags; + +printf "\n======================\n" +printf "TESTS" +printf "\n======================\n\n" +for test_name in "${test_names[@]}" +do + echo "$test_name..." + build/nest "tests/$test_name.nat" + echo "$test_name [COMPILATION] [OK]" + nest/$test_name + echo "$test_name [RUN] [OK]" +done -compile $build_dir $exe_name "-g" ""; -build/nest diff --git a/tests/break_continue/main.nat b/tests/break_continue.nat similarity index 100% rename from tests/break_continue/main.nat rename to tests/break_continue.nat diff --git a/tests/comparison/main.nat b/tests/comparison.nat similarity index 100% rename from tests/comparison/main.nat rename to tests/comparison.nat diff --git a/tests/constant_prop/main.nat b/tests/constant_prop.nat similarity index 100% rename from tests/constant_prop/main.nat rename to tests/constant_prop.nat diff --git a/tests/first/main.nat b/tests/first.nat similarity index 100% rename from tests/first/main.nat rename to tests/first.nat diff --git a/tests/function_call_args/main.nat b/tests/function_call_args.nat similarity index 100% rename from tests/function_call_args/main.nat rename to tests/function_call_args.nat diff --git a/tests/if/main.nat b/tests/if.nat similarity index 100% rename from tests/if/main.nat rename to tests/if.nat diff --git a/tests/simple_variable_declaration/main.nat b/tests/simple_variable_declaration.nat similarity index 100% rename from tests/simple_variable_declaration/main.nat rename to tests/simple_variable_declaration.nat diff --git a/tests/while/main.nat b/tests/while.nat similarity index 100% rename from tests/while/main.nat rename to tests/while.nat