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

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"
Include dependency graph for value.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.

Detailed Description

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 Documentation

◆ Array

typedef struct Array Array

◆ Map

typedef struct Map Map

Function Documentation

◆ array_concat()

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

Concatenate two array Values.

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

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

Copy an array element into out.

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

int array_insert(Value *v,
intindex,
ValuenewElem )

Insert a new element at a specific position in an array.

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

int array_length(const Value *v)

Get the element count of an array Value.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

Value make_nil(void)

Construct 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.

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:

◆ print_value()

void print_value(const Value *v)

Print a human-readable representation of a Value 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:

◆ value_equals()

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

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.

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.

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.

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: