Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
value.h File Reference

Defines the Value type and associated functions for the Fun VM. More...

#include <inttypes.h>
Include dependency graph for value.h:
This graph shows which files directly or indirectly include this file:

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.

Detailed Description

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:

Value num = make_int(42);
Value str = make_string("Hello, Fun!");
Value str
Definition regex_match.c:41
Tagged union representing a Fun value.
Definition value.h:68
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Author
Johannes Findeisen
Date
2025-10-16

Definition in file value.h.

Enumeration Type Documentation

◆ ValueType

Enumeration of all runtime value types supported by Fun.

Enumerator
VAL_INT 
VAL_BOOL 
VAL_STRING 
VAL_FUNCTION 
VAL_ARRAY 
VAL_MAP 
VAL_NIL 
VAL_FLOAT 

Definition at line 50 of file value.h.

Function Documentation

◆ array_clear()

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.

Parameters
vArray Value to clear (in place). No-op if not an array.

Definition at line 68 of file array_utils.c.

Here is the call graph for this function:

◆ array_concat()

Value array_concat(const Value *av,
const Value *bv )

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.

Parameters
avFirst array.
bvSecond array.
Returns
A new concatenated array Value or VAL_NIL on type/alloc error.

Definition at line 386 of file value.c.

Here is the call graph for this function:

◆ array_contains()

int array_contains(const Value *v,
const Value *needle )

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.

Parameters
vArray Value to inspect (may be NULL).
needleValue to search for.
Returns
1 if found, 0 otherwise or if v is not an array.

Definition at line 26 of file array_utils.c.

Here is the call graph for this function:

◆ array_get_copy()

int array_get_copy(const Value *v,
intindex,
Value *out )

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.

Parameters
vArray Value.
indexZero-based index.
outDestination pointer to receive the copied Value (may be NULL to only validate index).
Returns
1 on success, 0 on bounds/type error.

Definition at line 192 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ array_index_of()

int array_index_of(const Value *v,
const Value *needle )

Find the index of the first occurrence of an element in an array.

Return index of needle in arr or -1.

Parameters
vArray Value to search (may be NULL).
needleValue to look for.
Returns
Zero-based index when found; -1 if not found or if v is not an array.

Definition at line 47 of file array_utils.c.

Here is the call graph for this function:

◆ array_insert()

int array_insert(Value *v,
intindex,
ValuenewElem )

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.

Parameters
vArray Value to modify.
indexInsertion index.
newElemElement to insert.
Returns
New array length on success (>=0), or -1 on allocation/type error.

Definition at line 305 of file value.c.

Here is the call graph for this function:

◆ array_join_with_sep()

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.

Parameters
vPointer to Value (expected VAL_ARRAY).
sepSeparator C string inserted between items (may be NULL).
Returns
Newly allocated joined string; never NULL.

Definition at line 141 of file str_utils.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ array_length()

int array_length(const Value *v)

Get the element count of an array Value.

Get array length or -1 if v is not an array.

Parameters
vArray Value.
Returns
Number of elements, or -1 if v is not a valid array.

Definition at line 176 of file value.c.

Here is the caller graph for this function:

◆ array_pop()

int array_pop(Value *v,
Value *out )

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.

Parameters
vArray Value to pop from.
outOptional destination for removed element.
Returns
1 on success, 0 if array empty or invalid.

Definition at line 282 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ array_push()

int array_push(Value *v,
ValuenewElem )

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.

Parameters
vArray Value to append to.
newElemElement to append.
Returns
New array length on success (>=0), or -1 on failure/type error.

Definition at line 257 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ array_remove()

int array_remove(Value *v,
intindex,
Value *out )

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.

Parameters
vArray Value to modify.
indexZero-based index to remove.
outOptional destination for removed element.
Returns
1 on success, 0 on bounds/type error.

Definition at line 336 of file value.c.

Here is the call graph for this function:

◆ array_set()

int array_set(Value *v,
intindex,
ValuenewElem )

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.

Parameters
vArray Value to mutate.
indexZero-based index to replace.
newElemNew element (ownership transferred to array).
Returns
1 on success, 0 on bounds/type error.

Definition at line 210 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ array_slice()

Value array_slice(const Value *v,
intstart,
intend )

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.

Parameters
vSource array Value.
startInclusive zero-based start index (clamped to >= 0).
endExclusive end index (clamped to <= length; -1 means length).
Returns
A new array Value (possibly empty) or VAL_NIL if v is not an array.

Definition at line 362 of file value.c.

Here is the call graph for this function:

◆ copy_value()

Value copy_value(const Value *v)

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.

Parameters
vSource Value.
Returns
A new Value with appropriate copy semantics.

Definition at line 415 of file value.c.

Here is the caller graph for this function:

◆ deep_copy_value()

Value deep_copy_value(const Value *v)

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.

Parameters
vSource Value.
Returns
A deep-copied Value.

Definition at line 463 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_value()

void free_value(Valuev)

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.

Parameters
vValue whose owned resources should be released.

Definition at line 517 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_array_from_values()

Value make_array_from_values(const Value *vals,
intcount )

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.

Parameters
valsPointer to input items; may be NULL when count == 0.
countNumber of items to copy (negative treated as 0).
Returns
A Value with type VAL_ARRAY or VAL_NIL on failure.

Definition at line 142 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_bool()

Value make_bool(intv)

Construct a boolean Value.

Create a boolean Value (0=false, non-zero=true).

Any non-zero input is treated as true, zero as false.

Parameters
vInteger truthy/falsey indicator.
Returns
A Value with type VAL_BOOL and normalized 0/1 payload.

Definition at line 79 of file value.c.

Here is the caller graph for this function:

◆ make_float()

Value make_float(doublev)

Construct a Value representing a double-precision float.

Create a floating-point Value.

Parameters
vThe floating-point payload.
Returns
A Value with type VAL_FLOAT holding v.

Definition at line 64 of file value.c.

Here is the caller graph for this function:

◆ make_function()

Value make_function(struct Bytecode *fn)

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.

Parameters
fnPointer to function bytecode (may be NULL to represent an invalid function).
Returns
A Value with type VAL_FUNCTION.

Definition at line 114 of file value.c.

Here is the caller graph for this function:

◆ make_int()

Value make_int(int64_tv)

Construct a Value representing a 64-bit integer.

Create an integer Value.

Parameters
vThe integer payload.
Returns
A Value with type VAL_INT holding v.

Definition at line 51 of file value.c.

Here is the caller graph for this function:

◆ make_map_empty()

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.

Returns
A Value of type VAL_MAP on success, or VAL_NIL on allocation failure.

Definition at line 35 of file map.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_nil()

Value make_nil(void)

Construct a nil Value.

Create a nil Value.

Returns
A Value with type VAL_NIL.

Definition at line 126 of file value.c.

Here is the caller graph for this function:

◆ make_string()

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.

Parameters
sNUL-terminated C string (may be NULL).
Returns
A Value with type VAL_STRING.

Definition at line 95 of file value.c.

Here is the caller graph for this function:

◆ map_get_copy()

int map_get_copy(const Value *vm,
const char *key,
Value *out )

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.

Parameters
vmSource map Value (VAL_MAP).
keyKey to search for.
outOutput pointer to receive a copy; may be NULL to only test presence.
Returns
1 if found (and out filled if non-NULL), 0 otherwise.

Definition at line 112 of file map.c.

Here is the call graph for this function:

◆ map_has()

int map_has(const Value *vm,
const char *key )

Check whether the map contains the specified key.

Test if key exists; returns 1/0.

Parameters
vmMap Value (VAL_MAP).
keyKey to search for.
Returns
1 if present, 0 if absent or on invalid input.

Definition at line 130 of file map.c.

◆ map_keys_array()

Value map_keys_array(const Value *vm)

Return all map keys as an array of strings.

Return array of string keys.

Ownership: Caller must free the returned Value with free_value().

Parameters
vmMap Value (VAL_MAP).
Returns
Array Value of keys; empty array if vm is not a map or is empty.

Definition at line 147 of file map.c.

Here is the call graph for this function:

◆ map_set()

int map_set(Value *vm,
const char *key,
Valuev )

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.

Parameters
vmTarget Value of type VAL_MAP.
keyNUL-terminated key string (copied into the map).
vValue to store; consumed on success.
Returns
1 on success, 0 on error (type mismatch, OOM, or NULL params).

Definition at line 79 of file map.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ map_values_array()

Value map_values_array(const Value *vm)

Return all map values as an array (deep-copied).

Return array of values (copies).

Ownership: Caller must free the returned Value with free_value().

Parameters
vmMap Value (VAL_MAP).
Returns
Array Value of values; empty array if vm is not a map or is empty.

Definition at line 171 of file map.c.

Here is the call graph for this function:

◆ print_value()

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.

Parameters
vValue to print.

Definition at line 552 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_find()

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.

Parameters
hayHaystack C string (may be NULL).
needleNeedle C string (may be NULL).
Returns
Zero-based index or -1 if not found/invalid input.

Definition at line 56 of file str_utils.c.

Here is the caller graph for this function:

◆ string_split_to_array()

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.

Parameters
sSource C string (may be NULL).
sepSeparator C string (may be NULL). Empty means split into chars.
Returns
Value of type VAL_ARRAY with string elements.

Definition at line 74 of file str_utils.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ string_substr()

char * string_substr(const char *s,
intstart,
intlen )

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.

Parameters
sSource C string (may be NULL).
startZero-based start index; clamped to [0, strlen(s)].
lenMaximum number of characters to copy; negative treated as 0.
Returns
Newly allocated NUL-terminated substring; never NULL.

Definition at line 35 of file str_utils.c.

Here is the caller graph for this function:

◆ value_equals()

int value_equals(const Value *a,
const Value *b )

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.

Parameters
aFirst Value.
bSecond Value.
Returns
1 if equal, 0 otherwise.

Definition at line 695 of file value.c.

Here is the caller graph for this function:

◆ value_is_truthy()

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.

Parameters
vValue to evaluate.
Returns
1 if truthy, 0 otherwise.

Definition at line 610 of file value.c.

◆ value_to_string_alloc()

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().

Parameters
vValue to convert.
Returns
Newly allocated NUL-terminated string describing v.

Definition at line 641 of file value.c.

Here is the call graph for this function:
Here is the caller graph for this function: