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
make_array.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
31
32
case
OP_MAKE_ARRAY
: {
33
int
n
= inst.operand;
34
if
(
n < 0 || vm->
sp + 1 <
n
) {
35
fprintf
(stderr,
"Runtime error: invalid element count for MAKE_ARRAY\n"
);
36
exit
(1);
37
}
38
/* pop n values into temp array preserving original order */
39
Value
*
vals
= (
Value
*)malloc(
sizeof
(
Value
) *
n
);
40
if
(!
vals
) {
41
fprintf
(stderr,
"Runtime error: OOM in MAKE_ARRAY\n"
);
42
exit
(1);
43
}
44
for
(
int
i =
n
- 1; i >= 0; --i) {
45
vals
[i] = pop_value(vm);
/* take ownership */
46
}
47
/* build array by copying values, then free originals */
48
Value
arr
=
make_array_from_values
(
vals
,
n
);
49
for
(
int
i = 0; i <
n
; ++i) {
50
free_value
(
vals
[i]);
51
}
52
free
(
vals
);
53
push_value
(vm,
arr
);
54
break
;
55
}
OP_MAKE_ARRAY
@ OP_MAKE_ARRAY
Definition
bytecode.h:79
arr
array_clear & arr
Definition
clear.c:38
n
int n
Definition
insert.c:41
push_value
push_value(vm, arr)
vals
Value * vals
Definition
make_array.c:39
free
free(vals)
Value
Tagged union representing a Fun value.
Definition
value.h:68
free_value
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition
value.c:517
make_array_from_values
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition
value.c:142
fprintf
#define fprintf
Definition
vm.c:200
exit
#define exit(code)
Definition
vm.c:230
Generated on
for Fun by
1.16.1