Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1#ifndef REDUCT_CORE_H
2#define REDUCT_CORE_H 1
3
4#include <reduct/arena.h>
5#include <reduct/atom.h>
6#include <reduct/defs.h>
7#include <reduct/error.h>
8#include <reduct/eval.h>
9#include <reduct/gc.h>
10#include <reduct/item.h>
11#include <reduct/list.h>
12#include <reduct/native.h>
13#include <reduct/schema.h>
14#include <reduct/scratch.h>
15#include <reduct/task.h>
16
17struct reduct_item;
18struct reduct_eval_frame;
19
20#include <assert.h>
21#include <stdatomic.h>
22#include <stdlib.h>
23
24/**
25 * @file core.h
26 * @brief Core definitions and structures.
27 * @defgroup core
28 *
29 * @{
30 */
31
32#define REDUCT_IMPORT_PATHS_INITIAL 4 ///< Initial size of the import path array.
33#define REDUCT_IMPORT_PATHS_GROWTH 2 ///< Growth factor of the import path array.
34
35#define REDUCT_SCHEMA_INITIAL 4 ///< Initial size of the schema array.
36#define REDUCT_SCHEMA_GROWTH 2 ///< Growth factor of the schema array.
37
38#define REDUCT_CONSTANTS_MAX 8 ///< Maximum amount of predefined constants.
39
40/**
41 * @brief Input flags.
42 */
43typedef enum
44{
46 REDUCT_INPUT_FLAG_OWNED = 1 ///< The input buffer is owned by the input structure and should be freed.
48
49/**
50 * @brief Input structure.
51 * @struct reduct_input_t
52 */
53typedef struct reduct_input
54{
55 struct reduct_input* prev;
57 const char* buffer;
58 const char* end;
61 char path[REDUCT_PATH_MAX];
63
64/**
65 * @brief Global input-related state structure.
66 * @struct reduct_input_global_t
67 */
74
75/**
76 * @brief Global import-related state structure.
77 * @struct reduct_import_global_t
78 */
79typedef struct
80{
82 char** paths;
83 size_t count;
84 size_t capacity;
86
87/**
88 * @brief Global library-related state structure.
89 * @struct reduct_lib_global_t
90 */
98
99#define REDUCT_SCRATCH_INITIAL 128 ///< Initial scratch buffer size.
100#define REDUCT_SCRATCH_MAX 16 ///< The maximum number of scratch buffers.
101
102#define REDUCT_LIBS_INITIAL 4 ///< Initial size of the library array.
103#define REDUCT_LIBS_GROWTH 2 ///< Growth factor of the library array.
104
105/**
106 * @brief Global state structure.
107 * @struct reduct_global_t
108 */
127
128/**
129 * @brief Per-thread state structure.
130 * @struct reduct_t
131 */
143
144/**
145 * @brief Create a new Reduct environment.
146 *
147 * @return A pointer to the first per thread state structure of the new environment or `NULL`.
148 */
150
151/**
152 * @brief Free the Reduct structure.
153 *
154 * @param reduct Pointer to the Reduct structure to free.
155 */
157
158/**
159 * @brief Add a loaded library handle to the global state.
160 *
161 * @param reduct Pointer to the Reduct structure.
162 * @param lib The library handle.
163 */
165
166/**
167 * @brief Set the user data pointer for the Reduct structure.
168 *
169 * @param reduct Pointer to the Reduct structure.
170 * @param userdata The user data pointer.
171 */
172REDUCT_API void reduct_userdata_set(reduct_t* reduct, void* userdata);
173
174/**
175 * @brief Get the user data pointer from the Reduct structure.
176 *
177 * @param reduct Pointer to the Reduct structure.
178 * @return The user data pointer.
179 */
181
182/**
183 * @brief Set the command line arguments for the Reduct structure.
184 *
185 * Will be utilized by the `(args!)` native.
186 *
187 * @param reduct Pointer to the Reduct structure.
188 * @param argc The number of arguments.
189 * @param argv The argument strings.
190 */
191REDUCT_API void reduct_args_set(reduct_t* reduct, int argc, char** argv);
192
193/**
194 * @brief Add a path to search when importing modules.
195 *
196 * @param reduct Pointer to the Reduct structure.
197 * @param path The directory path, will be copied.
198 */
199REDUCT_API void reduct_add_import_path(reduct_t* reduct, const char* path);
200
201/**
202 * @brief Create a new input structure and push it onto the input stack.
203 *
204 * @param reduct Pointer to the Reduct structure.
205 * @param buffer The input buffer.
206 * @param length The length of the input buffer.
207 * @param path The path to the input file.
208 * @param flags Input flags.
209 * @return A pointer to the newly created input structure.
210 */
211REDUCT_API reduct_input_t* reduct_input_new(reduct_t* reduct, const char* buffer, size_t length, const char* path,
213
214/**
215 * @brief Lookup an input structure by its ID.
216 *
217 * @param reduct Pointer to the Reduct structure.
218 * @param id The ID of the input structure.
219 * @return A pointer to the input structure, or `NULL` if not found.
220 */
222
223/**
224 * @brief Resolve a path relative to the current execution frame or import paths.
225 *
226 * @param reduct Pointer to the Reduct structure.
227 * @param path The path string to resolve.
228 * @param pathLen The length of the path string.
229 * @param outPath Pointer to the buffer where the resolved path will be stored.
230 * @param maxLen The maximum length of the output buffer.
231 * @param checkExistence Whether to check if the file exists when resolving relative paths.
232 */
233REDUCT_API void reduct_resolve_path(reduct_t* reduct, const char* path, size_t pathLen, char* outPath, size_t maxLen,
234 bool checkExistence);
235
236/**
237 * @brief Hash a string.
238 *
239 * @param str The string to hash.
240 * @param len The length of the string.
241 * @return The hash of the string.
242 */
243static inline REDUCT_ALWAYS_INLINE uint32_t reduct_hash(const char* str, size_t len)
244{
245 uint32_t hash = REDUCT_FNV_OFFSET;
246 for (size_t i = 0; i < len; i++)
247 {
248 hash ^= (unsigned char)str[i];
249 hash *= REDUCT_FNV_PRIME;
250 }
251 return hash;
252}
253
254/** @} */
255
256#endif
Arena allocation.
Atom representation and operations.
uint16_t reduct_input_id_t
Identifies a reduct_input_t within a Reduct structure.
Definition defs.h:147
#define REDUCT_ALWAYS_INLINE
Definition defs.h:61
#define REDUCT_PATH_MAX
Maximum path length for Reduct.
Definition defs.h:102
void * reduct_lib_t
Definition defs.h:38
#define REDUCT_API
Definition defs.h:24
Error handling and reporting.
Virtual machine evaluation.
Garbage collection.
#define REDUCT_FNV_PRIME
FNV-1a 32-bit prime.
Definition atom.h:121
#define REDUCT_FNV_OFFSET
FNV-1a 32-bit offset basis.
Definition atom.h:122
REDUCT_API reduct_input_t * reduct_input_new(reduct_t *reduct, const char *buffer, size_t length, const char *path, reduct_input_flags_t flags)
Create a new input structure and push it onto the input stack.
REDUCT_API void * reduct_userdata_get(reduct_t *reduct)
Get the user data pointer from the Reduct structure.
REDUCT_API void reduct_add_import_path(reduct_t *reduct, const char *path)
Add a path to search when importing modules.
REDUCT_API void reduct_userdata_set(reduct_t *reduct, void *userdata)
Set the user data pointer for the Reduct structure.
REDUCT_API void reduct_resolve_path(reduct_t *reduct, const char *path, size_t pathLen, char *outPath, size_t maxLen, bool checkExistence)
Resolve a path relative to the current execution frame or import paths.
static REDUCT_ALWAYS_INLINE uint32_t reduct_hash(const char *str, size_t len)
Hash a string.
Definition core.h:243
reduct_input_flags_t
Input flags.
Definition core.h:44
REDUCT_API reduct_input_t * reduct_input_lookup(reduct_t *reduct, reduct_input_id_t id)
Lookup an input structure by its ID.
REDUCT_API reduct_t * reduct_new(void)
Create a new Reduct environment.
REDUCT_API void reduct_free(reduct_t *reduct)
Free the Reduct structure.
REDUCT_API void reduct_args_set(reduct_t *reduct, int argc, char **argv)
Set the command line arguments for the Reduct structure.
REDUCT_API void reduct_global_lib_add(reduct_t *reduct, reduct_lib_t lib)
Add a loaded library handle to the global state.
@ REDUCT_INPUT_FLAG_NONE
Definition core.h:45
@ REDUCT_INPUT_FLAG_OWNED
The input buffer is owned by the input structure and should be freed.
Definition core.h:46
Item management.
List management.
Native function and intrinsic registration.
Schema transformation.
Scratch buffer allocation.
Per-thread arena-related state structure.
Definition arena.h:60
Global atom-related environment structure.
Definition atom.h:129
Error structure.
Definition error.h:55
Per-thread eval-related state structure.
Definition eval.h:42
Global garbage collection-related state structure.
Definition gc.h:25
Global state structure.
Definition core.h:110
reduct_task_global_t task
Definition core.h:125
reduct_handle_t nil
Definition core.h:113
struct reduct * threads
Definition core.h:117
reduct_optimize_global_t optimize
Definition core.h:124
uint64_t threadCount
Definition core.h:118
reduct_input_global_t input
Definition core.h:114
reduct_atom_global_t atom
Definition core.h:119
reduct_gc_global_t gc
Definition core.h:122
reduct_item_global_t item
Definition core.h:121
reduct_lib_global_t lib
Definition core.h:116
char ** argv
Definition core.h:112
reduct_native_global_t native
Definition core.h:120
reduct_schema_global_t schema
Definition core.h:123
Handle type.
Definition defs.h:119
Global import-related state structure.
Definition core.h:80
reduct_rwmutex_t mutex
Definition core.h:81
Global input-related state structure.
Definition core.h:69
reduct_input_id_t nextId
Definition core.h:72
reduct_rwmutex_t mutex
Definition core.h:70
reduct_input_t * head
Definition core.h:71
Input structure.
Definition core.h:54
reduct_input_id_t id
Definition core.h:59
reduct_handle_t ast
Definition core.h:56
const char * end
Definition core.h:58
struct reduct_input * prev
Definition core.h:55
const char * buffer
Definition core.h:57
reduct_input_flags_t flags
Definition core.h:60
Global item-related state structure.
Definition item.h:112
Per-thread item-related state structure.
Definition item.h:126
Global library-related state structure.
Definition core.h:92
reduct_lib_t * array
Definition core.h:94
reduct_rwmutex_t mutex
Definition core.h:93
size_t capacity
Definition core.h:96
Global native-related state structure.
Definition native.h:46
Global optimization-related state structure.
Definition optimize.h:44
Read-Write Mutex structure.
Definition sync.h:23
Global schema-related state structure.
Definition schema.h:72
Per-thread scratch-related state structure.
Definition scratch.h:38
Per-thread state structure.
Definition core.h:133
reduct_global_t * global
Definition core.h:135
reduct_error_t * error
Definition core.h:136
reduct_eval_local_t eval
Definition core.h:140
reduct_arena_local_t arena
Definition core.h:137
reduct_item_local_t item
Definition core.h:138
thrd_t thrd
Definition core.h:134
reduct_scratch_local_t scratch
Definition core.h:139
void * userdata
Definition core.h:141
Global thread-related state structure.
Definition task.h:56
Task primitives.