Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
div.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 2025 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
10/**
11 * @file div.c
12 * @brief Implements the OP_DIV opcode (numeric division) in the VM.
13 *
14 * Handles the OP_DIV instruction, dividing two numeric operands and pushing
15 * the result. If either operand is a float, division is performed in double
16 * precision and a VAL_FLOAT is produced; otherwise integer division is
17 * used and a VAL_INT is produced.
18 *
19 * Behavior:
20 * - Pops two values from the stack.
21 * - If any operand is VAL_FLOAT, computes (double)a / (double)b and pushes a VAL_FLOAT.
22 * - Else computes a.i / b.i and pushes a VAL_INT.
23 *
24 * Error Handling:
25 * - Raises a runtime error and aborts execution if operands are not numeric.
26 * - Raises a runtime error on division by zero (both integer and floating cases).
27 *
28 * Example:
29 * - Bytecode: OP_DIV
30 * - Stack before: [10, 2]
31 * - Stack after: [5]
32 * - Stack before: [5.0, 2]
33 * - Stack after: [2.5]
34 */
35
36case OP_DIV: {
37 Value b = pop_value(vm);
39 if ((a.type == VAL_INT || a.type == VAL_FLOAT) && (b.type == VAL_INT || b.type == VAL_FLOAT)) {
40 if (a.type == VAL_FLOAT || b.type == VAL_FLOAT) {
41 double da = (a.type == VAL_FLOAT) ? a.d : (double)a.i;
42 double db = (b.type == VAL_FLOAT) ? b.d : (double)b.i;
43 if (db == 0.0) {
44 vm_raise_error(vm, "division by zero");
45 break;
46 }
50 push_value(vm, res);
51 } else {
52 if (b.i == 0) {
53 vm_raise_error(vm, "division by zero");
54 break;
55 }
56 Value res = make_int(a.i / b.i);
59 push_value(vm, res);
60 }
61 } else {
62 fprintf(stderr, "Runtime type error: DIV expects numbers, got %s and %s\n",
63 value_type_name(a.type), value_type_name(b.type));
64 exit(1);
65 }
66 break;
67}
Value a
Definition add.c:37
uint32_t b
Definition band.c:32
@ OP_DIV
Definition bytecode.h:47
double da
Definition fmax.c:26
double db
Definition fmax.c:27
const char * res
Definition get_string.c:38
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
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
void vm_raise_error(VM *vm, const char *msg)
Raise a runtime error inside the VM, honoring try/catch/finally.
Definition vm.c:248
static const char * value_type_name(ValueType t)
Get a human-readable name for a ValueType.
Definition vm.c:318
static void push_value(VM *vm, Value v)
Push a Value onto the VM operand stack.
Definition vm.c:564
#define fprintf
Definition vm.c:200
#define exit(code)
Definition vm.c:230