158 for (
int i = 0; i <
count; ++i) {
195 if (index < 0 || index >= a->
count)
return 0;
213 if (index < 0 || index >= a->
count)
return 0;
215 a->
items[index] = newElem;
229 if (newCount <= a->
count)
return 1;
233 if (cap < 4) cap = 4;
234 while (cap < newCount)
237 if (!newItems)
return 0;
239 if (cap > a->
count) {
240 for (
int i = a->
count; i < cap; ++i) {
285 if (a->
count <= 0)
return 0;
286 int idx = a->
count - 1;
288 *out = a->
items[idx];
308 if (index < 0) index = 0;
317 for (
int i = a->
count; i > index; --i) {
320 a->
items[index] = newElem;
339 if (index < 0 || index >= a->
count)
return 0;
341 *out = a->
items[index];
345 for (
int i = index; i < a->
count - 1; ++i) {
366 if (start < 0) start = 0;
367 if (end < 0 || end > n) end = n;
368 if (start > end) start = end;
390 int na = a ? a->
count : 0;
391 int nb = b ? b->
count : 0;
396 for (
int i = 0; i < na; ++i)
397 tmp[i] = a->
items[i];
398 for (
int j = 0; j < nb; ++j)
399 tmp[na + j] = b->
items[j];
426 out.
i = v->
i ? 1 : 0;
429 out.
s = v->
s ? strdup(v->
s) : strdup(
"");
442 out.
map = (
struct Map *)m;
477 if (!a || a->
count <= 0) {
483 for (
int i = 0; i < a->
count; ++i) {
487 for (
int i = 0; i < a->
count; ++i) {
497 for (
int i = 0; i < m->
count; ++i) {
523 for (
int i = 0; i < a->
count; ++i) {
532 for (
int i = 0; i < m->
count; ++i) {
555 printf(
"%" PRId64, v->
i);
558 printf(
"%.17g", v->
d);
561 printf(
"%s", v->
s ? v->
s :
"");
564 printf(
"%s", v->
i ?
"true" :
"false");
567 printf(
"<function@%p>", (
void *)v->
fn);
573 for (
int i = 0; i < a->
count; ++i) {
574 if (i > 0) printf(
", ");
585 for (
int i = 0; i < m->
count; ++i) {
586 if (i > 0) printf(
", ");
587 printf(
"\"%s\": ", m->
keys[i] ? m->
keys[i] :
"");
619 return v->
s && v->
s[0] !=
'\0';
624 return a && a->
count > 0;
642 if (!v)
return strdup(
"nil");
647 snprintf(tmp,
sizeof(tmp),
"%" PRId64, v->
i);
652 snprintf(tmp,
sizeof(tmp),
"%.17g", v->
d);
656 return strdup(v->
s ? v->
s :
"");
658 return strdup(v->
i ?
"true" :
"false");
660 snprintf(buf,
sizeof(buf),
"<function@%p>", (
void *)v->
fn);
666 snprintf(buf,
sizeof(buf),
"[array n=%d]", n);
673 n = m ? m->
count : 0;
675 snprintf(buf,
sizeof(buf),
"{map n=%d}", n);
680 return strdup(
"nil");
707 return (a->
i != 0) == (b->
i != 0);
709 const char *sa = a->
s ? a->
s :
"";
710 const char *sb = b->
s ? b->
s :
"";
711 return strcmp(sa, sb) == 0;
Utility functions for operating on Value arrays.
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Value make_map_empty(void)
Construct a new empty map Value.
Helpers for manipulating C strings and bridging with Value arrays.
Tagged union representing a Fun value.
int value_is_truthy(const Value *v)
Evaluate a Value's truthiness according to Fun language rules.
Value make_bool(int v)
Construct a boolean Value.
void print_value(const Value *v)
Print a human-readable representation of a Value to stdout.
Value array_concat(const Value *av, const Value *bv)
Concatenate two array Values.
Value make_nil(void)
Construct a nil Value.
int array_insert(Value *v, int index, Value newElem)
Insert a new element at a specific position in an array.
int array_pop(Value *v, Value *out)
Remove the last element from an array.
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
int array_length(const Value *v)
Get the element count of an array Value.
Value make_function(struct Bytecode *fn)
Construct a function Value referencing bytecode.
Value deep_copy_value(const Value *v)
Deep copy a Value, recursively copying arrays and maps.
void free_value(Value v)
Free dynamic storage owned by a Value.
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
Value make_float(double v)
Construct a Value representing a double-precision float.
int array_push(Value *v, Value newElem)
Append a Value to an array.
int value_equals(const Value *a, const Value *b)
Compare two Values for equality.
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
static int ensure_array_capacity(Array *a, int newCount)
Ensure the internal items buffer can hold at least newCount items.
Value array_slice(const Value *v, int start, int end)
Create a shallow-copied slice of an array Value.
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
int array_set(Value *v, int index, Value newElem)
Replace an element of an array with a new Value.
Value copy_value(const Value *v)
Shallow copy a Value.
int array_remove(Value *v, int index, Value *out)
Remove an element at index from an array.
Defines the Value type and associated functions for the Fun VM.