From e61c68c2774c408d9bf0ced2746474c3cefc1bdc Mon Sep 17 00:00:00 2001 From: hanez Date: Tue, 16 Sep 2025 05:39:43 +0200 Subject: [PATCH] Some housekeeping and refactoring. --- src/array_utils.c | 1 - src/builtins_io.c | 41 ---------------- src/builtins_math.c | 75 ----------------------------- src/{builtins_iter.c => iter.c} | 0 src/parser_utils.c | 2 - src/{builtins_string.c => string.c} | 0 src/vm.c | 11 ++--- 7 files changed, 4 insertions(+), 126 deletions(-) delete mode 100644 src/builtins_io.c delete mode 100644 src/builtins_math.c rename src/{builtins_iter.c => iter.c} (100%) rename src/{builtins_string.c => string.c} (100%) diff --git a/src/array_utils.c b/src/array_utils.c index 2db97db..71c3cac 100644 --- a/src/array_utils.c +++ b/src/array_utils.c @@ -8,7 +8,6 @@ */ #include "value.h" -#include /* array utilities */ diff --git a/src/builtins_io.c b/src/builtins_io.c deleted file mode 100644 index b76ef88..0000000 --- a/src/builtins_io.c +++ /dev/null @@ -1,41 +0,0 @@ -/** - * This file is part of the Fun programming language. - * https://hanez.org/project/fun/ - * - * Copyright 2025 Johannes Findeisen - * Licensed under the terms of the ISC license. - * https://opensource.org/license/isc-license-txt - */ - - -#include -#include -#include - -/* File I/O helpers: return malloc'd buffers or status ints */ - -char *bio_read_file_strdup(const char *path) { - if (!path) return strdup(""); - FILE *f = fopen(path, "rb"); - if (!f) return strdup(""); - if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return strdup(""); } - long sz = ftell(f); - if (sz < 0) { fclose(f); return strdup(""); } - rewind(f); - char *buf = (char*)malloc((size_t)sz + 1); - if (!buf) { fclose(f); return strdup(""); } - size_t n = fread(buf, 1, (size_t)sz, f); - fclose(f); - buf[n] = '\0'; - return buf; -} - -int bio_write_file(const char *path, const char *data, size_t len) { - if (!path) return 0; - FILE *f = fopen(path, "wb"); - if (!f) return 0; - if (!data) data = ""; - int ok = (fwrite(data, 1, len, f) == len); - fclose(f); - return ok; -} diff --git a/src/builtins_math.c b/src/builtins_math.c deleted file mode 100644 index c2b4c86..0000000 --- a/src/builtins_math.c +++ /dev/null @@ -1,75 +0,0 @@ -/** - * This file is part of the Fun programming language. - * https://hanez.org/project/fun/ - * - * Copyright 2025 Johannes Findeisen - * Licensed under the terms of the ISC license. - * https://opensource.org/license/isc-license-txt - */ - -/** -* @file builtins_math.c - * @brief Implements built-in math functions for the Fun VM. - * - * This file provides helper functions for mathematical operations, including: - * - Minimum, maximum, and clamping. - * - Absolute value and exponentiation. - * - Random number generation. - * - * Functions: - * - `bm_min`: Returns the smaller of two integers. - * - `bm_max`: Returns the larger of two integers. - * - `bm_clamp`: Clamps a value between a lower and upper bound. - * - `bm_abs`: Returns the absolute value of an integer. - * - `bm_pow`: Computes the power of an integer. - * - `bm_random_seed`: Seeds the random number generator. - * - `bm_random_int`: Generates a random integer within a range. - * - * Example: - * ```c - * int64_t min = bm_min(10, 20); // 10 - * int64_t rand = bm_random_int(1, 100); // Random number between 1 and 99 - * ``` - * - * @author Johannes Findeisen - * @date 2025-10-16 - */ - - -#include -#include - -/* Math built-ins (pure helpers) */ - -int64_t bm_min(int64_t a, int64_t b) { return a < b ? a : b; } -int64_t bm_max(int64_t a, int64_t b) { return a > b ? a : b; } - -int64_t bm_clamp(int64_t x, int64_t lo, int64_t hi) { - if (x < lo) return lo; - if (x > hi) return hi; - return x; -} - -int64_t bm_abs(int64_t x) { return x < 0 ? -x : x; } - -int64_t bm_pow(int64_t base, int64_t exp) { - if (exp < 0) return 0; /* integer pow: negative exponent -> 0 */ - int64_t res = 1; - int64_t b = base; - int64_t e = exp; - while (e > 0) { - if (e & 1) res *= b; - b *= b; - e >>= 1; - } - return res; -} - -/* RNG: thin wrappers over libc's srand/rand for now */ -void bm_random_seed(unsigned int seed) { srand(seed); } - -int64_t bm_random_int(int64_t lo, int64_t hi) { - if (hi <= lo) return lo; - int64_t span = hi - lo; - return lo + (rand() % span); -} diff --git a/src/builtins_iter.c b/src/iter.c similarity index 100% rename from src/builtins_iter.c rename to src/iter.c diff --git a/src/parser_utils.c b/src/parser_utils.c index f261a38..3fd4cbf 100644 --- a/src/parser_utils.c +++ b/src/parser_utils.c @@ -12,8 +12,6 @@ #include #include #include "parser.h" -#include "value.h" -#include "vm.h" static char *read_file_all(const char *path, size_t *out_len) { FILE *f = fopen(path, "rb"); diff --git a/src/builtins_string.c b/src/string.c similarity index 100% rename from src/builtins_string.c rename to src/string.c diff --git a/src/vm.c b/src/vm.c index 1028e8d..d323fe3 100644 --- a/src/vm.c +++ b/src/vm.c @@ -7,19 +7,16 @@ * https://opensource.org/license/isc-license-txt */ +/* Bring in split-out built-ins without changing the build system yet */ +#include "iter.c" +#include "map.c" +#include "string.c" #include "vm.h" #include "value.h" #include #include #include -/* Bring in split-out built-ins without changing the build system yet */ -#include "builtins_math.c" -#include "builtins_io.c" -#include "map.c" -#include "builtins_string.c" -#include "builtins_iter.c" - /* Opcode case include index (vm_case_*.inc): - Core/stack/frame: