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
arrays
join.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 join.c
12
* @brief Implements the OP_JOIN opcode for joining array elements into a string in the VM.
13
*
14
* This file handles the OP_JOIN instruction, which joins the elements of an array into a string
15
* using a separator. The separator and array are popped from the stack, and the resulting string
16
* is pushed back.
17
*
18
* Behavior:
19
* - Pops the separator and array from the stack.
20
* - Joins the array elements into a string using the separator.
21
* - Pushes the resulting string onto the stack.
22
*
23
* Error Handling:
24
* - Exits with an error if the array or separator is of the wrong type.
25
*
26
* Example:
27
* - Bytecode: OP_JOIN
28
* - Stack before: [", ", ["a", "b", "c"]]
29
* - Stack after: ["a, b, c"]
30
*/
31
32
case
OP_JOIN
: {
33
Value
sep =
pop_value
(vm);
34
Value
arr
=
pop_value
(vm);
35
if
(
arr
.type !=
VAL_ARRAY
|| sep.
type
!=
VAL_STRING
) {
36
fprintf
(stderr,
"Runtime type error: JOIN expects (array, string)\n"
);
37
exit
(1);
38
}
39
Value
out
=
bi_join
(&
arr
, &sep);
40
free_value
(
arr
);
41
free_value
(sep);
42
push_value
(vm,
out
);
43
break
;
44
}
out
Value out
Definition
apop.c:38
OP_JOIN
@ OP_JOIN
Definition
bytecode.h:102
arr
array_clear & arr
Definition
clear.c:38
free_value
free_value(arr)
push_value
push_value(vm, out)
bi_join
Value bi_join(const Value *arr, const Value *sep)
Join an array of strings with a separator into a single string Value.
Definition
string.c:56
Value
Tagged union representing a Fun value.
Definition
value.h:68
Value::type
ValueType type
Definition
value.h:69
VAL_ARRAY
@ VAL_ARRAY
Definition
value.h:55
VAL_STRING
@ VAL_STRING
Definition
value.h:53
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
Generated on
for Fun by
1.16.1