Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
serial_send.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_send.c
12 * @brief Implements OP_SERIAL_SEND to write bytes to a serial port.
13 *
14 * Behavior:
15 * - Pops data (string) and fd (int); writes data to the serial port; pushes number of bytes written (>=0) or -1 on error.
16 * - Only supported on UNIX-like systems; other platforms push -1.
17 *
18 * Errors:
19 * - On wrong types, prints an error and pushes -1.
20 */
21
22#ifdef __unix__
23#include <unistd.h>
24#endif
25
27 /* Pops data (string), fd (int); returns bytes sent (int) */
28 Value datav = pop_value(vm);
30 int sent = -1;
31#ifdef __unix__
32 if (fdv.type != VAL_INT || datav.type != VAL_STRING) {
33 fprintf(stderr, "Runtime type error: serial_send expects (int fd, string data)\n");
34 } else {
35 int fd = (int)fdv.i;
36 const char *buf = datav.s ? datav.s : "";
37 size_t len = strlen(buf);
38 ssize_t n = write(fd, buf, len);
39 if (n >= 0) sent = (int)n;
40 }
41#endif
45 break;
46}
@ OP_SERIAL_SEND
Definition bytecode.h:243
Value fdv
FunCurlBuf buf
Definition get.c:48
size_t len
Definition input_line.c:102
int n
Definition insert.c:41
Value datav
int fd
Definition serial_open.c:92
free_value(datav)
int sent
Definition serial_send.c:30
push_value(vm, make_int(sent))
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_STRING
Definition value.h:53
@ 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