![]() |
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. | |
| static int | ensure_array_capacity (Array *a, int newCount) |
| Ensure the internal items buffer can hold at least newCount items. | |
| 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 |
Insert a new element at a specific position in an array.
Index is clamped into [0, count]. Takes ownership of newElem.
| 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.
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. |
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). |
|
static |
Ensure the internal items buffer can hold at least newCount items.
May grow the allocation exponentially; initializes new slots to nil.
| void free_value | ( | Value | v | ) |
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). |
| Value make_bool | ( | int | v | ) |
| Value make_float | ( | double | v | ) |
| Value make_int | ( | int64_t | v | ) |
| Value make_nil | ( | void | ) |
| Value make_string | ( | const char * | s | ) |
| 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.
| int value_is_truthy | ( | const Value * | v | ) |