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
index_set.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_INDEX_SET
: {
33
Value
v
= pop_value(vm);
34
Value
idx
= pop_value(vm);
35
Value
container
= pop_value(vm);
36
#ifdef FUN_DEBUG
37
fprintf
(stderr,
"DEBUG INDEX_SET: container.type=%d idx.type=%d value.type=%d\n"
,
38
container
.type,
idx
.type,
v
.type);
39
#endif
40
if
(
container
.type ==
VAL_ARRAY
) {
41
if
(
idx
.type !=
VAL_INT
) {
42
fprintf
(stderr,
"INDEX_SET index must be int for array\n"
);
43
exit
(1);
44
}
45
if
(!
array_set
(&
container
, (
int
)
idx
.i,
v
)) {
46
fprintf
(stderr,
"Runtime error: index out of range\n"
);
47
exit
(1);
48
}
49
free_value
(
container
);
50
free_value
(
idx
);
51
}
else
if
(
container
.type ==
VAL_MAP
) {
52
if
(
idx
.type !=
VAL_STRING
) {
53
fprintf
(stderr,
"INDEX_SET key must be string for map\n"
);
54
exit
(1);
55
}
56
if
(!
map_set
(&
container
,
idx
.s ?
idx
.s :
""
,
v
)) {
57
fprintf
(stderr,
"Runtime error: map set failed\n"
);
58
exit
(1);
59
}
60
free_value
(
container
);
61
free_value
(
idx
);
62
}
else
{
63
fprintf
(stderr,
"Runtime type error: INDEX_SET expects array or map\n"
);
64
exit
(1);
65
}
66
break
;
67
}
OP_INDEX_SET
@ OP_INDEX_SET
Definition
bytecode.h:81
v
Value v
Definition
cast.c:22
container
Value container
Definition
index_get.c:35
idx
int idx
Definition
index_of.c:38
map_set
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Definition
map.c:79
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
array_set
int array_set(Value *v, int index, Value newElem)
Replace an element of an array with a new Value.
Definition
value.c:210
VAL_ARRAY
@ VAL_ARRAY
Definition
value.h:55
VAL_MAP
@ VAL_MAP
Definition
value.h:56
VAL_STRING
@ VAL_STRING
Definition
value.h:53
VAL_INT
@ VAL_INT
Definition
value.h:51
fprintf
#define fprintf
Definition
vm.c:200
exit
#define exit(code)
Definition
vm.c:230
Generated on
for Fun by
1.16.1