Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
print.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 print.c
12 * @brief Implements the OP_PRINT opcode for printing values in the VM.
13 *
14 * This file handles the OP_PRINT instruction, which prints the top value on the stack
15 * to the output buffer. The value is popped from the stack.
16 *
17 * Behavior:
18 * - Pops the value from the stack.
19 * - Prints the value to the output buffer.
20 *
21 * Example:
22 * - Bytecode: OP_PRINT
23 * - Stack before: [42]
24 * - Stack after: []
25 */
26
27case OP_PRINT: {
28 Value v = pop_value(vm);
31 if (vm->output_count < OUTPUT_SIZE) {
32 int idx = vm->output_count;
33 vm->output[idx] = snap;
34 vm->output_is_partial[idx] = 0; // PRINT terminates the line
35 vm->output_count++;
36 } else {
38 fprintf(stderr, "Runtime error: output buffer overflow\n");
39 exit(1);
40 }
41 break;
42}
@ OP_PRINT
Definition bytecode.h:63
Value v
Definition cast.c:22
Value snap
Definition echo.c:26
int idx
Definition index_of.c:38
free_value(v)
Tagged union representing a Fun value.
Definition value.h:68
Value deep_copy_value(const Value *v)
Deep copy a Value, recursively copying arrays and maps.
Definition value.c:463
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
#define fprintf
Definition vm.c:200
#define exit(code)
Definition vm.c:230
#define OUTPUT_SIZE
Definition vm.h:38