Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
sign.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
15case OP_SIGN: {
16 Value v = pop_value(vm);
17 int out = 0;
18 if (v.type == VAL_INT) {
19 out = (v.i > 0) - (v.i < 0);
20 } else if (v.type == VAL_FLOAT) {
21 if (v.d > 0.0)
22 out = 1;
23 else if (v.d < 0.0)
24 out = -1;
25 else
26 out = 0;
27 } else {
28 fprintf(stderr, "Runtime type error: SIGN expects number, got %s\n", value_type_name(v.type));
29 exit(1);
30 }
33 break;
34}
Value out
Definition apop.c:38
@ OP_SIGN
Definition bytecode.h:271
Value v
Definition cast.c:22
push_value(vm, make_int(out))
free_value(v)
Tagged union representing a Fun value.
Definition value.h:68
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