Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
sleep_ms.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 sleep_ms.c
12 * @brief Implements OP_SLEEP_MS to suspend execution for a number of milliseconds.
13 *
14 * Behavior:
15 * - Pops an integer value ms from the stack and sleeps for that many milliseconds.
16 * - Always pushes Nil after completion to keep stack discipline for statement POPs.
17 *
18 * Errors:
19 * - If the popped value is not an integer, prints an error, frees it, and pushes Nil.
20 * - Negative durations are treated as no-op; Nil is still pushed.
21 */
22
24 Value ms = pop_value(vm);
25 if (ms.type != VAL_INT) {
26 fprintf(stderr, "Runtime type error: sleep(ms) expects Number (milliseconds)\n");
28 /* push Nil so caller-side POP is safe */
29 push_value(vm, make_nil());
30 break;
31 }
32 long t = (long)ms.i;
33 if (t > 0) fun_sleep_ms(t);
35 /* push Nil so statement POP does not underflow */
36 push_value(vm, make_nil());
37 break;
38}
@ OP_SLEEP_MS
Definition bytecode.h:153
long t
Definition sleep_ms.c:32
Tagged union representing a Fun value.
Definition value.h:68
static void fun_sleep_ms(long ms)
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition value.c:517
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
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