Fun
0.41.5
The programming language that makes you have fun!
Main Page
Data Structures
Files
File List
Globals
Loading...
Searching...
No Matches
src
vm
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
27
case
OP_PRINT
: {
28
Value
v
=
pop_value
(vm);
29
Value
snap
=
deep_copy_value
(&
v
);
30
free_value
(
v
);
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
{
37
free_value
(
snap
);
38
fprintf
(stderr,
"Runtime error: output buffer overflow\n"
);
39
exit
(1);
40
}
41
break
;
42
}
OP_PRINT
@ OP_PRINT
Definition
bytecode.h:63
v
Value v
Definition
cast.c:22
snap
Value snap
Definition
echo.c:26
idx
int idx
Definition
index_of.c:38
free_value
free_value(v)
Value
Tagged union representing a Fun value.
Definition
value.h:68
deep_copy_value
Value deep_copy_value(const Value *v)
Deep copy a Value, recursively copying arrays and maps.
Definition
value.c:463
pop_value
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition
vm.c:580
fprintf
#define fprintf
Definition
vm.c:200
exit
#define exit(code)
Definition
vm.c:230
OUTPUT_SIZE
#define OUTPUT_SIZE
Definition
vm.h:38
Generated on
for Fun by
1.16.1