Some housekeeping and refactoring.
This commit is contained in:
parent
cfdeb4a22f
commit
e61c68c277
7 changed files with 4 additions and 126 deletions
|
|
@ -8,7 +8,6 @@
|
|||
*/
|
||||
|
||||
#include "value.h"
|
||||
#include <string.h>
|
||||
|
||||
/* array utilities */
|
||||
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the ISC license.
|
||||
* https://opensource.org/license/isc-license-txt
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://hanez.org/project/fun/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* 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 <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
|
@ -12,8 +12,6 @@
|
|||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#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");
|
||||
|
|
|
|||
11
src/vm.c
11
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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue