![]() | Fun 0.41.5 The programming language that makes You have fun |
Implementation of the runtime Value type, including constructors, dynamic array/map utilities, copying, comparison, printing, and string conversion helpers. More...
#include "value.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include "array_utils.c"#include "str_utils.c"
Go to the source code of this file.
Data Structures | |
| struct | Array |
| struct | Map |
Typedefs | |
| typedef struct Array | Array |
| typedef struct Map | Map |
Functions | |
| Value | make_int (int64_t v) |
| Construct a Value representing a 64-bit integer. | |
| Value | make_float (double v) |
| Construct a Value representing a double-precision float. | |
| 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_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 *av, const Value *bv) |
| Concatenate two array Values. | |
| 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. | |
| char * | value_to_string_alloc (const Value *v) |
| Allocate a printable C string for a Value. | |
| int | value_equals (const Value *a, const Value *b) |
| Compare two Values for equality. | |
Implementation of the runtime Value type, including constructors, dynamic array/map utilities, copying, comparison, printing, and string conversion helpers.
This translation unit provides the concrete operations for the Fun programming language's Value structure (ints, floats, bools, strings, arrays, maps, functions and nil). It is used by the VM and standard library to construct and manipulate runtime values.
Definition in file value.c.
| typedef struct Array Array |
| typedef struct Map Map |
Concatenate two array Values.
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.

Copy an array element into out.
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.


| int array_length | ( | const Value * | v | ) |
Remove the last element from an array.
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.


Remove an element at index from an array.
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.
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.
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.

Deep copy a Value, recursively copying arrays and 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.
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.
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 | ) |
| Value make_float | ( | double | v | ) |
Construct a function Value referencing bytecode.
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_nil | ( | void | ) |
| Value make_string | ( | const char * | s | ) |
Construct a string Value by duplicating the given C string.
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.

| void print_value | ( | const Value * | v | ) |
Compare two Values for equality.
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 | ) |
| char * value_to_string_alloc | ( | const Value * | v | ) |