Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
trunc.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2026 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
14
15#include <math.h>
16
17case OP_TRUNC: {
18 Value v = pop_value(vm);
19 if (v.type == VAL_INT) {
20 push_value(vm, make_int(v.i));
22 } else if (v.type == VAL_FLOAT) {
23 double r = trunc(v.d);
24 if (r >= (double)INT64_MIN && r <= (double)INT64_MAX) {
25 int64_t ii = (int64_t)r;
26 if ((double)ii == r) {
27 push_value(vm, make_int(ii));
28 } else {
29 push_value(vm, make_float(r));
30 }
31 } else {
32 push_value(vm, make_float(r));
33 }
35 } else {
36 fprintf(stderr, "Runtime type error: TRUNC expects number, got %s\n", value_type_name(v.type));
37 exit(1);
38 }
39 break;
40}
uint32_t r
Definition band.c:33
@ OP_TRUNC
Definition bytecode.h:255
Value v
Definition cast.c:22
Tagged union representing a Fun value.
Definition value.h:68
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition value.c:517
Value make_float(double v)
Construct a Value representing a double-precision float.
Definition value.c:64
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_INT
Definition value.h:51
@ VAL_FLOAT
Definition value.h:58
#define fprintf
Definition vm.c:200
#define exit(code)
Definition vm.c:230