From 11efaa6f8f45aa4557a3a80dfc32f885ac455908 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 31 May 2024 20:34:29 -0400 Subject: Change how behavior is handled for running files with no main() func --- tomo.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tomo.c b/tomo.c index a9800518..c81f50b1 100644 --- a/tomo.c +++ b/tomo.c @@ -376,6 +376,11 @@ int compile_object_file(const char *filename, bool force_recompile) int compile_executable(const char *filename, array_t object_files, module_code_t *module_code) { + binding_t *main_binding = get_binding(module_code->env, "main"); + if (!main_binding || main_binding->type->tag != FunctionType) { + errx(1, "No main() function has been defined for %s, so it can't be run!", filename); + } + const char *bin_name = heap_strn(filename, strlen(filename) - strlen(".tm")); const char *run = heap_strf("%s | %s %s %s %s %s %s -x c - -o %s", autofmt, cc, cflags, ldflags, ldlibs, objfiles, CORD_to_const_char_star(Text$join(" ", object_files)), bin_name); @@ -383,7 +388,6 @@ int compile_executable(const char *filename, array_t object_files, module_code_t printf("%s\n", run); FILE *runner = popen(run, "w"); - binding_t *main_binding = get_binding(module_code->env, "main"); CORD program = CORD_all( "#include \n" "#include \"", filename, ".h\"\n" @@ -391,9 +395,7 @@ int compile_executable(const char *filename, array_t object_files, module_code_t "int main(int argc, char *argv[]) {\n" "tomo_init();\n" "\n", - main_binding && main_binding->type->tag == FunctionType ? - CORD_all(compile_cli_arg_call(module_code->env, main_binding->code, main_binding->type), "return 0;\n") - : "errx(1, \"No main function is defined!\");\n", + CORD_all(compile_cli_arg_call(module_code->env, main_binding->code, main_binding->type), "return 0;\n"), "}\n" ); -- cgit v1.2.3