![]() | Fun 0.41.5 The programming language that makes You have fun |
Defines the Value type and associated functions for the Fun VM. More...
#include <inttypes.h>

Go to the source code of this file.
Data Structures | |
| struct | Value |
| Tagged union representing a Fun value. More... | |
Enumerations | |
| enum | ValueType { VAL_INT , VAL_BOOL , VAL_STRING , VAL_FUNCTION , VAL_ARRAY , VAL_MAP , VAL_NIL , VAL_FLOAT } |
| Enumeration of all runtime value types supported by Fun. More... | |
Functions | |
| Value | make_int (int64_t v) |
| Construct a Value representing a 64-bit integer. | |
| Value | make_bool (int v) |
| Construct a boolean Value. | |
| Value | make_string (const char *s) |
| Construct a string Value by duplicating the given C string. | |
| Value | make_function (struct Bytecode *fn) |
| Construct a function Value referencing bytecode. | |
| Value | make_nil (void) |
| Construct a nil Value. | |
| Value | make_float (double v) |
| Construct a Value representing a double-precision float. | |
| Value | make_array_from_values (const Value *vals, int count) |
| Create an array Value by copying items from an input span. | |
| int | array_length (const Value *v) |
| Get the element count of an array Value. | |
| int | array_get_copy (const Value *v, int index, Value *out) |
| Copy an array element into out. | |
| int | array_set (Value *v, int index, Value newElem) |
| Replace an element of an array with a new Value. | |
| int | array_push (Value *v, Value newElem) |
| Append a Value to an array. | |
| int | array_pop (Value *v, Value *out) |
| Remove the last element from an array. | |
| int | array_insert (Value *v, int index, Value newElem) |
| Insert a new element at a specific position in an array. | |
| int | array_remove (Value *v, int index, Value *out) |
| Remove an element at index from an array. | |
| Value | array_slice (const Value *v, int start, int end) |
| Create a shallow-copied slice of an array Value. | |
| Value | array_concat (const Value *a, const Value *b) |
| Concatenate two array Values. | |
| Value | make_map_empty (void) |
| Construct a new empty map Value. | |
| int | map_set (Value *m, const char *key, Value v) |
| Insert or replace a key in the map. | |
| int | map_get_copy (const Value *m, const char *key, Value *out) |
| Look up a key and copy the stored value into out. | |
| int | map_has (const Value *m, const char *key) |
| Check whether the map contains the specified key. | |
| Value | map_keys_array (const Value *m) |
| Return all map keys as an array of strings. | |
| Value | map_values_array (const Value *m) |
| Return all map values as an array (deep-copied). | |
| Value | copy_value (const Value *v) |
| Shallow copy a Value. | |
| 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. | |
| void | print_value (const Value *v) |
| Print a human-readable representation of a Value to stdout. | |
| int | value_is_truthy (const Value *v) |
| Evaluate a Value's truthiness according to Fun language rules. | |
| int | value_equals (const Value *a, const Value *b) |
| Compare two Values for equality. | |
| char * | value_to_string_alloc (const Value *v) |
| Allocate a printable C string for a Value. | |
| int | array_contains (const Value *arr, const Value *needle) |
| Check if an array Value contains a given element. | |
| int | array_index_of (const Value *arr, const Value *needle) |
| Find the index of the first occurrence of an element in an array. | |
| void | array_clear (Value *arr) |
| Remove all elements from an array Value, freeing their contents. | |
| char * | string_substr (const char *s, int start, int len) |
| Create a newly allocated substring of s. | |
| int | string_find (const char *hay, const char *needle) |
| Find first occurrence of needle in hay. | |
| Value | string_split_to_array (const char *s, const char *sep) |
| Split a C string by separator into a Value array of strings. | |
| char * | array_join_with_sep (const Value *arr, const char *sep) |
| Join the elements of a Value array into a single newly allocated C string. | |
Defines the Value type and associated functions for the Fun VM.
This file defines the Value type, which represents all possible data types in the Fun language, including integers, strings, functions, arrays, maps, and nil. It also provides utility functions for creating, copying, and freeing Value objects.
Key Types:
Functions:
Example:
Definition in file value.h.
| enum ValueType |
| void array_clear | ( | Value * | v | ) |
Remove all elements from an array Value, freeing their contents.
Free elements and reset count to 0.
Uses array_pop() repeatedly to clear the array.
Definition at line 68 of file array_utils.c.

Concatenate two array Values.
Concatenate arrays a and b into a new array.
Copies elements into a new array. If either input is not an array, returns VAL_NIL.
| av | First array. |
| bv | Second array. |
Definition at line 386 of file value.c.

Check if an array Value contains a given element.
Return 1 if needle equals any element in arr.
Performs linear search using value_equals() on copied elements.
Definition at line 26 of file array_utils.c.

Copy an array element into out.
Copy array item at index to out; returns 0 on error.
The element is copied with copy_value; ownership of out remains with caller.
| v | Array Value. |
| index | Zero-based index. |
| out | Destination pointer to receive the copied Value (may be NULL to only validate index). |
Definition at line 192 of file value.c.


Find the index of the first occurrence of an element in an array.
Return index of needle in arr or -1.
Definition at line 47 of file array_utils.c.

Insert a new element at a specific position in an array.
Insert at index; returns new length or -1 on error.
Index is clamped into [0, count]. Takes ownership of newElem.
Definition at line 305 of file value.c.

| char * array_join_with_sep | ( | const Value * | v, |
| const char * | sep ) |
Join the elements of a Value array into a single newly allocated C string.
Join Value array items into a newly allocated C string with separator.
Each array element is converted to a string via value_to_string_alloc. NULL/invalid inputs yield an empty string. The caller owns the returned buffer and must free() it.
| v | Pointer to Value (expected VAL_ARRAY). |
| sep | Separator C string inserted between items (may be NULL). |
Definition at line 141 of file str_utils.c.


| int array_length | ( | const Value * | v | ) |
Remove the last element from an array.
Pop last element into out; returns 1 on success.
If out is provided, ownership of the removed element is transferred to *out; otherwise the element is freed.
Definition at line 282 of file value.c.


Append a Value to an array.
Push new element; returns new length or -1 on error.
On success, ownership of newElem is transferred to the array.
Definition at line 257 of file value.c.


Remove an element at index from an array.
Remove at index into out; returns 1 on success.
If out is provided, ownership of the removed element is transferred; else it is freed. Remaining items are shifted left.
| v | Array Value to modify. |
| index | Zero-based index to remove. |
| out | Optional destination for removed element. |
Definition at line 336 of file value.c.

Replace an element of an array with a new Value.
Set element at index; takes ownership of newElem; 0 on error.
Takes ownership of newElem and frees the old element.
| v | Array Value to mutate. |
| index | Zero-based index to replace. |
| newElem | New element (ownership transferred to array). |
Definition at line 210 of file value.c.


Create a shallow-copied slice of an array Value.
Return slice [start,end) (negative end means till end).
Start and end are clamped into valid bounds; end < start yields empty array.
| v | Source array Value. |
| start | Inclusive zero-based start index (clamped to >= 0). |
| end | Exclusive end index (clamped to <= length; -1 means length). |
Definition at line 362 of file value.c.

Shallow copy a Value.
Shallow/deep copy depending on type (deep for strings, RC for arrays/maps).
Strings are duplicated, arrays/maps have their refcount incremented, and function pointers are copied as-is.
Definition at line 415 of file value.c.

Deep copy a Value, recursively copying arrays and maps.
Deep copy including arrays/maps.
Function Values are copied shallowly. On allocation failure, returns nil or an empty container as appropriate.
Definition at line 463 of file value.c.


| void free_value | ( | Value | v | ) |
Free dynamic storage owned by a Value.
Free owned resources of v.
Strings are freed, arrays/maps are reference-counted and freed recursively when their refcount drops to zero. Functions are not freed here.
Definition at line 517 of file value.c.


Create an array Value by copying items from an input span.
Build an array from a list of Values (deep-copies vals).
Performs a shallow copy for scalars and reference-counted copy for arrays/maps via copy_value. On allocation failure, returns VAL_NIL.
| vals | Pointer to input items; may be NULL when count == 0. |
| count | Number of items to copy (negative treated as 0). |
Definition at line 142 of file value.c.


| Value make_bool | ( | int | v | ) |
Construct a boolean Value.
Create a boolean Value (0=false, non-zero=true).
Any non-zero input is treated as true, zero as false.
| v | Integer truthy/falsey indicator. |
Definition at line 79 of file value.c.

| Value make_float | ( | double | v | ) |
Construct a function Value referencing bytecode.
Create a function Value from bytecode pointer (shallow).
The Bytecode pointer is stored as-is; ownership/lifetime is managed by the caller/VM and not freed by free_value.
| fn | Pointer to function bytecode (may be NULL to represent an invalid function). |
Definition at line 114 of file value.c.

| Value make_int | ( | int64_t | v | ) |
| Value make_map_empty | ( | void | ) |
Construct a new empty map Value.
Create a new empty string-keyed map Value.
Allocates an internal Map structure with refcount=1 and zero capacity.
Definition at line 35 of file map.c.


| Value make_nil | ( | void | ) |
| Value make_string | ( | const char * | s | ) |
Construct a string Value by duplicating the given C string.
Create a string Value by copying s.
If s is NULL, an empty string is used. The returned Value owns an allocated copy which must be released via free_value.
| s | NUL-terminated C string (may be NULL). |
Definition at line 95 of file value.c.

Look up a key and copy the stored value into out.
Lookup key; returns 1 and copies value to out on success.
The returned value is a deep copy; caller owns it and must free it.
| vm | Source map Value (VAL_MAP). |
| key | Key to search for. |
| out | Output pointer to receive a copy; may be NULL to only test presence. |
Definition at line 112 of file map.c.

| int map_has | ( | const Value * | vm, |
| const char * | key ) |
Return all map keys as an array of strings.
Return array of string keys.
Ownership: Caller must free the returned Value with free_value().
Definition at line 147 of file map.c.

Insert or replace a key in the map.
Set key to v (takes ownership); returns 1 on success.
On success, ownership of v transfers into the map. On failure, v is freed.
| vm | Target Value of type VAL_MAP. |
| key | NUL-terminated key string (copied into the map). |
| v | Value to store; consumed on success. |
Definition at line 79 of file map.c.


Return all map values as an array (deep-copied).
Return array of values (copies).
Ownership: Caller must free the returned Value with free_value().
Definition at line 171 of file map.c.

| void print_value | ( | const Value * | v | ) |
Print a human-readable representation of a Value to stdout.
Print value in a human-readable form to stdout.
Numbers are printed in decimal; arrays/maps are formatted compactly; strings are printed without quotes.
Definition at line 552 of file value.c.


| int string_find | ( | const char * | hay, |
| const char * | needle ) |
Find first occurrence of needle in hay.
Find first index of needle in hay or -1.
| hay | Haystack C string (may be NULL). |
| needle | Needle C string (may be NULL). |
Definition at line 56 of file str_utils.c.

| Value string_split_to_array | ( | const char * | s, |
| const char * | sep ) |
Split a C string by separator into a Value array of strings.
Split C string by sep into Value array of strings.
When sep is empty, splits into individual UTF-8 bytes (characters). Uses make_string/make_array_from_values; the returned Value owns internal memory per Value semantics. NULL inputs are treated as empty strings.
| s | Source C string (may be NULL). |
| sep | Separator C string (may be NULL). Empty means split into chars. |
Definition at line 74 of file str_utils.c.


| char * string_substr | ( | const char * | s, |
| int | start, | ||
| int | len ) |
Create a newly allocated substring of s.
Create newly allocated substring (bounds clamped).
Indices are clamped into valid range. If s is NULL, an empty string is returned. The caller owns the returned buffer and must free() it.
| s | Source C string (may be NULL). |
| start | Zero-based start index; clamped to [0, strlen(s)]. |
| len | Maximum number of characters to copy; negative treated as 0. |
Definition at line 35 of file str_utils.c.

Compare two Values for equality.
Equality for ints/strings; other types may be pointer/semantic based.
Supports numeric cross-type equality between ints and floats. Strings are compared by content. Other types default to pointer/type equality as implemented in the switch.
Definition at line 695 of file value.c.

| int value_is_truthy | ( | const Value * | v | ) |
Evaluate a Value's truthiness according to Fun language rules.
Truthiness predicate used by the language semantics.
Empty strings, zero numbers, nil and empty arrays are falsey; everything else is truthy.
| char * value_to_string_alloc | ( | const Value * | v | ) |
Allocate a printable C string for a Value.
Convert Value to a newly allocated C string; caller must free.
The returned string must be freed by the caller with free().
Definition at line 641 of file value.c.

