Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
serial_close.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 serial_close.c
12 * @brief Implements OP_SERIAL_CLOSE to close an open serial port.
13 *
14 * Behavior:
15 * - Pops fd (int); attempts to close it; pushes 1 on success, 0 on failure.
16 * - Only supported on UNIX-like systems.
17 *
18 * Errors:
19 * - If argument type is wrong, prints an error and returns 0.
20 */
21
22#ifdef __unix__
23#include <unistd.h>
24#endif
25
27 /* Pops fd (int); returns 1/0 */
28 Value fdv = pop_value(vm);
29 int ok = 0;
30#ifdef __unix__
31 if (fdv.type != VAL_INT) {
32 fprintf(stderr, "Runtime type error: serial_close expects (int fd)\n");
33 } else {
34 int fd = (int)fdv.i;
35 if (close(fd) == 0) ok = 1;
36 }
37#endif
40 break;
41}
@ OP_SERIAL_CLOSE
Definition bytecode.h:245
int ok
Definition contains.c:38
Value fdv
free_value(fdv)
push_value(vm, make_int(ok))
int fd
Definition serial_open.c:92
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
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
#define fprintf
Definition vm.c:200