1
0
Fork 0
forked from fun/fun

Added some math opcodes. (0.37.42)

This commit is contained in:
Johannes Findeisen 2026-01-03 03:14:25 +01:00
commit 194c06e4aa
14 changed files with 413 additions and 2 deletions

43
src/vm/math/ceil.c Normal file
View file

@ -0,0 +1,43 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file ceil.c
* @brief Implements the OP_CEIL opcode using C99 math.h ceil().
*/
#include <math.h>
case OP_CEIL: {
Value v = pop_value(vm);
if (v.type == VAL_INT) {
/* ceil(n) == n for integers */
push_value(vm, make_int(v.i));
free_value(v);
} else if (v.type == VAL_FLOAT) {
double r = ceil(v.d);
if (r >= (double)INT64_MIN && r <= (double)INT64_MAX) {
int64_t ii = (int64_t)r;
if ((double)ii == r) {
push_value(vm, make_int(ii));
} else {
push_value(vm, make_float(r));
}
} else {
push_value(vm, make_float(r));
}
free_value(v);
} else {
fprintf(stderr, "Runtime type error: CEIL expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

43
src/vm/math/floor.c Normal file
View file

@ -0,0 +1,43 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file floor.c
* @brief Implements the OP_FLOOR opcode using C99 math.h floor().
*/
#include <math.h>
case OP_FLOOR: {
Value v = pop_value(vm);
if (v.type == VAL_INT) {
/* floor(n) == n for integers */
push_value(vm, make_int(v.i));
free_value(v);
} else if (v.type == VAL_FLOAT) {
double r = floor(v.d);
if (r >= (double)INT64_MIN && r <= (double)INT64_MAX) {
int64_t ii = (int64_t)r;
if ((double)ii == r) {
push_value(vm, make_int(ii));
} else {
push_value(vm, make_float(r));
}
} else {
push_value(vm, make_float(r));
}
free_value(v);
} else {
fprintf(stderr, "Runtime type error: FLOOR expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

43
src/vm/math/round.c Normal file
View file

@ -0,0 +1,43 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file round.c
* @brief Implements the OP_ROUND opcode using C99 math.h round().
* C99 round() rounds half away from zero.
*/
#include <math.h>
case OP_ROUND: {
Value v = pop_value(vm);
if (v.type == VAL_INT) {
push_value(vm, make_int(v.i));
free_value(v);
} else if (v.type == VAL_FLOAT) {
double r = round(v.d);
if (r >= (double)INT64_MIN && r <= (double)INT64_MAX) {
int64_t ii = (int64_t)r;
if ((double)ii == r) {
push_value(vm, make_int(ii));
} else {
push_value(vm, make_float(r));
}
} else {
push_value(vm, make_float(r));
}
free_value(v);
} else {
fprintf(stderr, "Runtime type error: ROUND expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

42
src/vm/math/trunc.c Normal file
View file

@ -0,0 +1,42 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file trunc.c
* @brief Implements the OP_TRUNC opcode using C99 math.h trunc().
*/
#include <math.h>
case OP_TRUNC: {
Value v = pop_value(vm);
if (v.type == VAL_INT) {
push_value(vm, make_int(v.i));
free_value(v);
} else if (v.type == VAL_FLOAT) {
double r = trunc(v.d);
if (r >= (double)INT64_MIN && r <= (double)INT64_MAX) {
int64_t ii = (int64_t)r;
if ((double)ii == r) {
push_value(vm, make_int(ii));
} else {
push_value(vm, make_float(r));
}
} else {
push_value(vm, make_float(r));
}
free_value(v);
} else {
fprintf(stderr, "Runtime type error: TRUNC expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}