Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
eval.h
Go to the documentation of this file.
1#ifndef REDUCT_EVAL_H
2#define REDUCT_EVAL_H 1
3
4#include <reduct/function.h>
5#include <reduct/handle.h>
6#include <reduct/optimize.h>
7
8struct reduct_closure;
9
10/**
11 * @file eval.h
12 * @brief Virtual machine evaluation.
13 * @defgroup eval Evaluation
14 *
15 * @{
16 */
17
18#define REDUCT_EVAL_REGS_INITIAL 64 ///< The initial amount of registers.
19#define REDUCT_EVAL_REGS_GROWTH_FACTOR 2 ///< The growth factor of the registers array.
20
21#define REDUCT_EVAL_FRAMES_INITIAL 32 ///< The initial size of the frames array.
22#define REDUCT_EVAL_FRAMES_GROWTH_FACTOR 2 ///< The growth factor of the frames array.
23
24/**
25 * @brief Evaluation frame structure.
26 * @struct reduct_eval_frame_t
27 */
28typedef struct reduct_eval_frame
29{
30 struct reduct_closure* closure; ///< The closure being evaluated.
31 reduct_inst_t* ip; ///< The current instruction pointer.
32 reduct_handle_t* constants; ///< Cached pointer to closure constants.
33 uint32_t base; ///< The base register, where the functions registers start.
34 uint32_t prevRegCount; ///< The previous register count to restore upon return.
36
37/**
38 * @brief Per-thread eval-related state structure.
39 * @struct reduct_eval_local_t
40 */
41typedef struct reduct_eval_state
42{
43 struct reduct_eval_frame* frames;
44 size_t frameCount;
47 size_t regCount;
50
51/**
52 * @brief Initialize a local eval state.
53 *
54 * @param local Pointer to the local eval state to initialize.
55 */
57
58/**
59 * @brief Deinitialize a local eval state.
60 *
61 * @param local Pointer to the local eval state to deinitialize.
62 */
64
65/**
66 * @brief Evaluates a handle.
67 *
68 * If the handle is a compiled function then it will be interpreted, otherwise, it will first be compiled into a
69 * function.
70 *
71 * @param reduct The Reduct instance.
72 * @param handle The handle to evaluate.
73 * @return The result of the evaluation as a Reduct handle.
74 */
76
77/**
78 * @brief Parses, builds, optimizes, emits and evaluates a file.
79 *
80 * @param reduct The Reduct instance.
81 * @param path The path to the file.
82 * @param flags Optimization flags to control which optimizations are applied.
83 * @return The result of the evaluation.
84 */
85REDUCT_API reduct_handle_t reduct_eval_file(struct reduct* reduct, const char* path, reduct_optimize_flags_t optimize);
86
87/**
88 * @brief Parses, builds, optimizes, emits and evaluates a string.
89 *
90 * @param reduct The Reduct instance.
91 * @param str The string to evaluate.
92 * @param len The length of the string.
93 * @param flags Optimization flags to control which optimizations are applied.
94 * @return The result of the evaluation.
95 */
96REDUCT_API reduct_handle_t reduct_eval_string(struct reduct* reduct, const char* str, size_t len,
98
99/**
100 * @brief Calls a Reduct callable (closure or native) with arguments.
101 *
102 * @param reduct The Reduct instance.
103 * @param callable The callable item handle.
104 * @param argc The number of arguments.
105 * @param argv Pointer to the arguments array.
106 * @return The result of the call.
107 */
108REDUCT_API reduct_handle_t reduct_eval_call(struct reduct* reduct, reduct_handle_t callable, size_t argc,
109 reduct_handle_t* argv);
110
111/**
112 * @brief Calls a Reduct callable (closure or native) with variadic arguments.
113 *
114 * @param reduct The Reduct instance.
115 * @param callable The callable item handle.
116 * @param argc The number of arguments.
117 * @param ... The arguments (as reduct_handle_t).
118 * @return The result of the call.
119 */
120REDUCT_API reduct_handle_t reduct_eval_call_v(struct reduct* reduct, reduct_handle_t callable, size_t argc, ...);
121
122/** @} */
123
124#endif
#define REDUCT_API
Definition defs.h:24
Compiled function.
REDUCT_API reduct_handle_t reduct_eval_string(struct reduct *reduct, const char *str, size_t len, reduct_optimize_flags_t optimize)
Parses, builds, optimizes, emits and evaluates a string.
REDUCT_API reduct_handle_t reduct_eval_file(struct reduct *reduct, const char *path, reduct_optimize_flags_t optimize)
Parses, builds, optimizes, emits and evaluates a file.
REDUCT_API reduct_handle_t reduct_eval_call_v(struct reduct *reduct, reduct_handle_t callable, size_t argc,...)
Calls a Reduct callable (closure or native) with variadic arguments.
REDUCT_API void reduct_eval_local_deinit(reduct_eval_local_t *local)
Deinitialize a local eval state.
REDUCT_API reduct_handle_t reduct_eval(struct reduct *reduct, reduct_handle_t handle)
Evaluates a handle.
REDUCT_API reduct_handle_t reduct_eval_call(struct reduct *reduct, reduct_handle_t callable, size_t argc, reduct_handle_t *argv)
Calls a Reduct callable (closure or native) with arguments.
REDUCT_API void reduct_eval_local_init(reduct_eval_local_t *local)
Initialize a local eval state.
uint32_t reduct_inst_t
Instruction type.
Definition inst.h:52
reduct_optimize_flags_t
Optimization flags.
Definition optimize.h:24
Handle management.
Bytecode optimization.
Evaluation frame structure.
Definition eval.h:29
uint32_t base
The base register, where the functions registers start.
Definition eval.h:33
struct reduct_closure * closure
The closure being evaluated.
Definition eval.h:30
uint32_t prevRegCount
The previous register count to restore upon return.
Definition eval.h:34
reduct_inst_t * ip
The current instruction pointer.
Definition eval.h:31
reduct_handle_t * constants
Cached pointer to closure constants.
Definition eval.h:32
Per-thread eval-related state structure.
Definition eval.h:42
size_t regCount
Definition eval.h:47
reduct_handle_t * regs
Definition eval.h:46
size_t regCapacity
Definition eval.h:48
size_t frameCapacity
Definition eval.h:45
struct reduct_eval_frame * frames
Definition eval.h:43
size_t frameCount
Definition eval.h:44
Handle type.
Definition defs.h:119