From cbda0181b9f5480cfce2c08e17fd998babb77d4d Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 5 Aug 2025 22:51:26 +0200 Subject: Add runtime library for catskill transpiled artifacts, wrapping main function with correct types Signed-off-by: Mel --- boot/runtime/runtime.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 boot/runtime/runtime.c (limited to 'boot/runtime/runtime.c') diff --git a/boot/runtime/runtime.c b/boot/runtime/runtime.c new file mode 100644 index 0000000..47958a6 --- /dev/null +++ b/boot/runtime/runtime.c @@ -0,0 +1,48 @@ +/* + * the catskill transpiled-source runtime. + * wraps the catskill source entry point in a normal c main function, + * and provides some basic runtime startup and exit functionality. + * + * Copyright (c) 2025, Mel G. + * + * SPDX-License-Identifier: MPL-2.0 + */ + +// catskill programs that use the runtime are expected to +// provide a `main` which can have the following signatures: +// * main = fun () {} +// * main = fun (args [string]) {} +// the transpiler will define the macro `CATSKILL_MAIN_TAKES_ARGS` +// to indicate that the main function takes arguments. +// exit codes are returned with the use of the `exit` function. +#ifdef CATSKILL_MAIN_TAKES_ARGS +void catskill_main(Array(struct String) args); +#else +void catskill_main(void); +#endif + +// the main function that wraps the catskill main function +// and provides the runtime startup and exit functionality. +int +main(int argc, char* argv[]) +{ +#ifdef CATSKILL_MAIN_TAKES_ARGS + Array(struct String) args = array_new(struct String); + for (int i = 0; i < argc; i++) { + struct String arg = string_from(argv[i]); + array_push(&args, &arg); + } + + catskill_main(args); + + ARRAY_FREE_ELEMENTS(struct String, args, string_free); + array_free(&args); +#else + catskill_main(); + + (void)argc; + (void)argv; +#endif + // on error catskill_main will call `exit` with a non-zero code. + return 0; +} -- cgit 1.4.1