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
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
60
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 */
82Value make_int(int64_t v);
84Value make_bool(int v);
86Value make_string(const char *s);
90Value make_nil(void);
92Value make_float(double v);
93
94/* arrays */
98int array_length(const Value *v);
100int array_get_copy(const Value *v, int index, Value *out);
102int array_set(Value *v, int index, Value newElem);
104int array_push(Value *v, Value newElem);
106int array_pop(Value *v, Value *out);
108int array_insert(Value *v, int index, Value newElem);
110int array_remove(Value *v, int index, Value *out);
112Value array_slice(const Value *v, int start, int end);
114Value array_concat(const Value *a, const Value *b);
115
116/* maps (string keys) */
120int map_set(Value *m, const char *key, Value v);
122int map_get_copy(const Value *m, const char *key, Value *out);
124int map_has(const Value *m, const char *key);
129
130/* copy/free */
132Value copy_value(const Value *v);
136void free_value(Value v);
137
138/* utilities */
140void print_value(const Value *v);
142int value_is_truthy(const Value *v);
144int value_equals(const Value *a, const Value *b);
145
146/* stringify into a newly-allocated C string; caller must free */
148char *value_to_string_alloc(const Value *v);
149
150/* array utils */
152int array_contains(const Value *arr, const Value *needle);
154int array_index_of(const Value *arr, const Value *needle);
156void array_clear(Value *arr);
157
158/* string helpers returning newly allocated C strings or arrays */
160char *string_substr(const char *s, int start, int len);
162int string_find(const char *hay, const char *needle);
164Value string_split_to_array(const char *s, const char *sep);
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:37
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:266
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