Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
exit.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 exit.c
12 * @brief Implements the OP_EXIT opcode to terminate the script with an exit code.
13 *
14 * Behavior:
15 * - Pops a value from the stack (if available) and converts it to an integer exit code.
16 * - Sets vm->exit_code.
17 * - Stops the VM execution immediately (returns from vm_run).
18 */
19
20case OP_EXIT: {
21 int code = 0;
22 if (vm->sp >= 0) {
23 Value v = pop_value(vm);
24 if (v.type == VAL_INT) {
25 code = (int)v.i;
26 } else if (v.type == VAL_STRING) {
27 /* best-effort parse number from string */
28 code = (int)strtoll(v.s, NULL, 10);
29 } else if (v.type == VAL_NIL) {
30 code = 0;
31 } else {
32 /* unsupported type for exit; default to 0 */
33 code = 0;
34 }
36 }
37 vm->exit_code = code;
38 return;
39}
@ OP_EXIT
Definition bytecode.h:235
Value v
Definition cast.c:22
int code
Definition proc_system.c:31
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
@ VAL_STRING
Definition value.h:53
@ VAL_NIL
Definition value.h:57
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580