1
0
Fork 0
forked from fun/fun

Refactored all OP_ functions to vm/CATEGORY/ and renamed the files.

This commit is contained in:
Johannes Findeisen 2025-09-15 23:26:42 +02:00
commit 50a91d0b6f
67 changed files with 91 additions and 92 deletions

9
src/vm/math/abs.c Normal file
View file

@ -0,0 +1,9 @@
case OP_ABS: {
Value x = pop_value(vm);
if (x.type != VAL_INT) { fprintf(stderr, "ABS expects int\n"); exit(1); }
int64_t v = x.i;
if (v < 0) v = -v;
push_value(vm, make_int(v));
free_value(x);
break;
}

17
src/vm/math/clamp.c Normal file
View file

@ -0,0 +1,17 @@
case OP_CLAMP: {
Value hi = pop_value(vm);
Value lo = pop_value(vm);
Value x = pop_value(vm);
if (x.type != VAL_INT || lo.type != VAL_INT || hi.type != VAL_INT) {
fprintf(stderr, "CLAMP expects ints\n");
exit(1);
}
int64_t v = x.i;
if (v < lo.i) v = lo.i;
if (v > hi.i) v = hi.i;
push_value(vm, make_int(v));
free_value(x);
free_value(lo);
free_value(hi);
break;
}

8
src/vm/math/max.c Normal file
View file

@ -0,0 +1,8 @@
case OP_MAX: {
Value b = pop_value(vm);
Value a = pop_value(vm);
if (a.type != VAL_INT || b.type != VAL_INT) { fprintf(stderr, "MAX expects ints\n"); exit(1); }
push_value(vm, make_int(a.i > b.i ? a.i : b.i));
free_value(a); free_value(b);
break;
}

8
src/vm/math/min.c Normal file
View file

@ -0,0 +1,8 @@
case OP_MIN: {
Value b = pop_value(vm);
Value a = pop_value(vm);
if (a.type != VAL_INT || b.type != VAL_INT) { fprintf(stderr, "MIN expects ints\n"); exit(1); }
push_value(vm, make_int(a.i < b.i ? a.i : b.i));
free_value(a); free_value(b);
break;
}

18
src/vm/math/mod.c Normal file
View file

@ -0,0 +1,18 @@
case OP_MOD: {
Value b = pop_value(vm);
Value a = pop_value(vm);
if (a.type != VAL_INT || b.type != VAL_INT) {
fprintf(stderr, "Runtime type error: MOD expects ints, got %s and %s\n",
value_type_name(a.type), value_type_name(b.type));
exit(1);
}
if (b.i == 0) {
fprintf(stderr, "Runtime error: modulo by zero\n");
exit(1);
}
Value res = make_int(a.i % b.i);
free_value(a);
free_value(b);
push_value(vm, res);
break;
}

18
src/vm/math/pow.c Normal file
View file

@ -0,0 +1,18 @@
case OP_POW: {
Value b = pop_value(vm);
Value a = pop_value(vm);
if (a.type != VAL_INT || b.type != VAL_INT) { fprintf(stderr, "POW expects ints\n"); exit(1); }
int64_t base = a.i;
int64_t exp = b.i;
int64_t res = 1;
if (exp < 0) { res = 0; } else {
while (exp > 0) {
if (exp & 1) res *= base;
base *= base;
exp >>= 1;
}
}
push_value(vm, make_int(res));
free_value(a); free_value(b);
break;
}

12
src/vm/math/random_int.c Normal file
View file

@ -0,0 +1,12 @@
case OP_RANDOM_INT: {
Value hi = pop_value(vm);
Value lo = pop_value(vm);
if (lo.type != VAL_INT || hi.type != VAL_INT) { fprintf(stderr, "RANDOM_INT expects (int, int)\n"); exit(1); }
int64_t a = lo.i, b = hi.i;
if (b <= a) { push_value(vm, make_int((int64_t)a)); free_value(lo); free_value(hi); break; }
int64_t span = b - a;
int64_t r = (int64_t)(rand() % (span));
push_value(vm, make_int(a + r));
free_value(lo); free_value(hi);
break;
}

View file

@ -0,0 +1,8 @@
case OP_RANDOM_SEED: {
Value seed = pop_value(vm);
if (seed.type != VAL_INT) { fprintf(stderr, "RANDOM_SEED expects int\n"); exit(1); }
srand((unsigned int)seed.i);
free_value(seed);
push_value(vm, make_int(0));
break;
}