Move standard initialization code into tomo_init() function

This commit is contained in:
Bruce Hill 2024-05-25 13:56:34 -04:00
parent 9d744b3e6e
commit 128b1dd239
3 changed files with 12 additions and 6 deletions

View File

@ -8,6 +8,7 @@
#include <stdlib.h>
#include <sys/param.h>
#include <uninorm.h>
#include <unistd.h>
#include "array.h"
#include "bool.h"
@ -23,6 +24,14 @@
public const char *TOMO_HASH_VECTOR = "tomo hash vector ---------------------------------------------";
public void tomo_init(void)
{
GC_INIT();
USE_COLOR = getenv("COLOR") ? strcmp(getenv("COLOR"), "1") == 0 : isatty(STDOUT_FILENO);
srand(arc4random_uniform(UINT32_MAX));
srand48(arc4random_uniform(UINT32_MAX));
}
public void fail(CORD fmt, ...)
{
if (USE_COLOR) fputs("\x1b[31;7m FAIL: \x1b[m ", stderr);

View File

@ -10,6 +10,8 @@
extern const char *TOMO_HASH_VECTOR;
void tomo_init(void);
void fail(CORD fmt, ...);
void fail_source(const char *filename, int64_t start, int64_t end, CORD fmt, ...);
CORD builtin_last_err();

7
tomo.c
View File

@ -321,16 +321,11 @@ int compile_executable(const char *filename, const char *object_files, module_co
binding_t *main_binding = get_binding(module_code->env, "main");
CORD program = CORD_all(
"#include <stdlib.h>\n"
"#include <tomo/tomo.h>\n"
"#include <unistd.h>\n"
"#include \"", filename, ".h\"\n"
"\n"
"int main(int argc, char *argv[]) {\n"
"GC_INIT();\n"
"USE_COLOR = getenv(\"COLOR\") ? strcmp(getenv(\"COLOR\"), \"1\") == 0 : isatty(STDOUT_FILENO);\n"
"srand(arc4random_uniform(UINT32_MAX));\n"
"srand48(arc4random_uniform(UINT32_MAX));\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")