Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
value.h
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file value.h
12 * @brief Defines the Value type and associated functions for the Fun VM.
13 *
14 * This file defines the `Value` type, which represents all possible data types
15 * in the Fun language, including integers, strings, functions, arrays, maps, and nil.
16 * It also provides utility functions for creating, copying, and freeing `Value` objects.
17 *
18 * Key Types:
19 * - `ValueType`: Enumeration of supported value types (e.g., `VAL_INT`, `VAL_STRING`).
20 * - `Value`: Union type that can hold any Fun value.
21 *
22 * Functions:
23 * - `make_int`, `make_string`, `make_function`, `make_nil`: Constructors for `Value`.
24 * - `array_length`, `array_get_copy`, `array_set`: Array manipulation functions.
25 * - `make_map_empty`, `map_set`, `map_get_copy`: Map manipulation functions.
26 * - `copy_value`, `deep_copy_value`, `free_value`: Functions for copying and freeing values.
27 *
28 * Example:
29 * ```c
30 * Value num = make_int(42);
31 * Value str = make_string("Hello, Fun!");
32 * ```
33 *
34 * @author Johannes Findeisen
35 * @date 2025-10-16
36 */
37
38#ifndef FUN_VALUE_H
39#define FUN_VALUE_H
40
41#include <inttypes.h>
42
43struct Bytecode; /* forward */
44struct Array; /* forward */
45struct Map; /* forward */
46
47/**
48 * @brief Enumeration of all runtime value types supported by Fun.
49 */
60
61/**
62 * @brief Tagged union representing a Fun value.
63 *
64 * The active field in the anonymous union is determined by @ref ValueType in
65 * the @c type tag. Ownership/RC semantics for complex members (arrays/maps)
66 * are defined by the array/map APIs.
67 */
68typedef struct {
70 union {
71 int64_t i;
72 double d;
73 char *s;
74 struct Bytecode *fn;
75 struct Array *arr;
76 struct Map *map;
77 };
78} Value;
79
80/* constructors / helpers */
81/** Create an integer Value. */
82Value make_int(int64_t v);
83/** Create a boolean Value (0=false, non-zero=true). */
84Value make_bool(int v);
85/** Create a string Value by copying @p s. */
86Value make_string(const char *s);
87/** Create a function Value from bytecode pointer (shallow). */
88Value make_function(struct Bytecode *fn);
89/** Create a nil Value. */
90Value make_nil(void);
91/** Create a floating-point Value. */
92Value make_float(double v);
93
94/* arrays */
95/** Build an array from a list of Values (deep-copies vals). */
97/** Get array length or -1 if @p v is not an array. */
98int array_length(const Value *v);
99/** Copy array item at index to out; returns 0 on error. */
100int array_get_copy(const Value *v, int index, Value *out);
101/** Set element at index; takes ownership of newElem; 0 on error. */
102int array_set(Value *v, int index, Value newElem);
103/** Push new element; returns new length or -1 on error. */
104int array_push(Value *v, Value newElem);
105/** Pop last element into out; returns 1 on success. */
106int array_pop(Value *v, Value *out);
107/** Insert at index; returns new length or -1 on error. */
108int array_insert(Value *v, int index, Value newElem);
109/** Remove at index into out; returns 1 on success. */
110int array_remove(Value *v, int index, Value *out);
111/** Return slice [start,end) (negative end means till end). */
112Value array_slice(const Value *v, int start, int end);
113/** Concatenate arrays a and b into a new array. */
114Value array_concat(const Value *a, const Value *b);
115
116/* maps (string keys) */
117/** Create a new empty string-keyed map Value. */
119/** Set key to v (takes ownership); returns 1 on success. */
120int map_set(Value *m, const char *key, Value v);
121/** Lookup key; returns 1 and copies value to out on success. */
122int map_get_copy(const Value *m, const char *key, Value *out);
123/** Test if key exists; returns 1/0. */
124int map_has(const Value *m, const char *key);
125/** Return array of string keys. */
127/** Return array of values (copies). */
129
130/* copy/free */
131/** Shallow/deep copy depending on type (deep for strings, RC for arrays/maps). */
132Value copy_value(const Value *v);
133/** Deep copy including arrays/maps. */
135/** Free owned resources of v. */
136void free_value(Value v);
137
138/* utilities */
139/** Print value in a human-readable form to stdout. */
140void print_value(const Value *v);
141/** Truthiness predicate used by the language semantics. */
142int value_is_truthy(const Value *v);
143/** Equality for ints/strings; other types may be pointer/semantic based. */
144int value_equals(const Value *a, const Value *b);
145
146/* stringify into a newly-allocated C string; caller must free */
147/** Convert Value to a newly allocated C string; caller must free. */
148char *value_to_string_alloc(const Value *v);
149
150/* array utils */
151/** Return 1 if needle equals any element in arr. */
152int array_contains(const Value *arr, const Value *needle);
153/** Return index of needle in arr or -1. */
154int array_index_of(const Value *arr, const Value *needle);
155/** Free elements and reset count to 0. */
156void array_clear(Value *arr);
157
158/* string helpers returning newly allocated C strings or arrays */
159/** Create newly allocated substring (bounds clamped). */
160char *string_substr(const char *s, int start, int len);
161/** Find first index of needle in hay or -1. */
162int string_find(const char *hay, const char *needle);
163/** Split C string by sep into Value array of strings. */
164Value string_split_to_array(const char *s, const char *sep);
165/** Join Value array items into a newly allocated C string with separator. */
166char *array_join_with_sep(const Value *arr, const char *sep);
167
168#endif
Value a
Definition add.c:37
Value out
Definition apop.c:38
uint32_t b
Definition band.c:32
Value v
Definition cast.c:22
array_clear & arr
Definition clear.c:38
Value hay
Definition find.c:34
const char * key
Definition get_bool.c:40
Value m
Definition has_key.c:27
size_t len
Definition input_line.c:102
Value * vals
Definition make_array.c:39
int count
Definition parser.c:263
uint32_t s
Definition rol.c:31
Value start
Definition slice.c:34
Definition value.c:31
Definition map.c:20
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
struct Map * map
Definition value.h:76
struct Array * arr
Definition value.h:75
double d
Definition value.h:72
ValueType type
Definition value.h:69
struct Bytecode * fn
Definition value.h:74
char * s
Definition value.h:73
int value_is_truthy(const Value *v)
Evaluate a Value's truthiness according to Fun language rules.
Definition value.c:610
Value make_bool(int v)
Construct a boolean Value.
Definition value.c:79
char * string_substr(const char *s, int start, int len)
Create a newly allocated substring of s.
Definition str_utils.c:35
void print_value(const Value *v)
Print a human-readable representation of a Value to stdout.
Definition value.c:552
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.
Definition str_utils.c:141
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
int array_insert(Value *v, int index, Value newElem)
Insert a new element at a specific position in an array.
Definition value.c:305
int array_pop(Value *v, Value *out)
Remove the last element from an array.
Definition value.c:282
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
int array_length(const Value *v)
Get the element count of an array Value.
Definition value.c:176
int map_set(Value *m, const char *key, Value v)
Insert or replace a key in the map.
Definition map.c:79
Value make_function(struct Bytecode *fn)
Construct a function Value referencing bytecode.
Definition value.c:114
Value deep_copy_value(const Value *v)
Deep copy a Value, recursively copying arrays and maps.
Definition value.c:463
int array_contains(const Value *arr, const Value *needle)
Check if an array Value contains a given element.
Definition array_utils.c:26
void free_value(Value v)
Free dynamic storage owned by a Value.
Definition value.c:517
Value array_concat(const Value *a, const Value *b)
Concatenate two array Values.
Definition value.c:386
int map_get_copy(const Value *m, const char *key, Value *out)
Look up a key and copy the stored value into out.
Definition map.c:112
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
Definition value.c:192
Value make_float(double v)
Construct a Value representing a double-precision float.
Definition value.c:64
Value make_map_empty(void)
Construct a new empty map Value.
Definition map.c:35
Value map_values_array(const Value *m)
Return all map values as an array (deep-copied).
Definition map.c:171
int map_has(const Value *m, const char *key)
Check whether the map contains the specified key.
Definition map.c:130
int array_push(Value *v, Value newElem)
Append a Value to an array.
Definition value.c:257
int value_equals(const Value *a, const Value *b)
Compare two Values for equality.
Definition value.c:695
int string_find(const char *hay, const char *needle)
Find first occurrence of needle in hay.
Definition str_utils.c:56
void array_clear(Value *arr)
Remove all elements from an array Value, freeing their contents.
Definition array_utils.c:68
Value string_split_to_array(const char *s, const char *sep)
Split a C string by separator into a Value array of strings.
Definition str_utils.c:74
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
int array_index_of(const Value *arr, const Value *needle)
Find the index of the first occurrence of an element in an array.
Definition array_utils.c:47
Value array_slice(const Value *v, int start, int end)
Create a shallow-copied slice of an array Value.
Definition value.c:362
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142
ValueType
Enumeration of all runtime value types supported by Fun.
Definition value.h:50
@ VAL_BOOL
Definition value.h:52
@ VAL_ARRAY
Definition value.h:55
@ VAL_MAP
Definition value.h:56
@ VAL_STRING
Definition value.h:53
@ VAL_FUNCTION
Definition value.h:54
@ VAL_NIL
Definition value.h:57
@ VAL_INT
Definition value.h:51
@ VAL_FLOAT
Definition value.h:58
int array_set(Value *v, int index, Value newElem)
Replace an element of an array with a new Value.
Definition value.c:210
Value map_keys_array(const Value *m)
Return all map keys as an array of strings.
Definition map.c:147
Value copy_value(const Value *v)
Shallow copy a Value.
Definition value.c:415
int array_remove(Value *v, int index, Value *out)
Remove an element at index from an array.
Definition value.c:336