aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-05-31 20:34:29 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-05-31 20:34:29 -0400
commit11efaa6f8f45aa4557a3a80dfc32f885ac455908 (patch)
tree885013dbbe9548d36a0286b42914f85185bc84d9
parentfdaadee13352323d0b68d1aaae0c811f83edf07e (diff)
Change how behavior is handled for running files with no main() func
-rw-r--r--tomo.c10
1 files 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 <tomo/tomo.h>\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"
);