Reduct  v4.2.1-1-g0b59ae3
A functional and immutable language.
Loading...
Searching...
No Matches
Evaluation

Detailed Description

The evaluator is a register-based virtual machine executing the bytecode produced by the emitter.

Warning
Calls are zero-copy such that the callee's frame is placed directly over the caller's argument registers, so arguments become the callee's starting registers, and tail calls reuse the current frame.
Registers and frames are held in arrays that are realloc'd as frames are pushed and popped. Any pointer into them, most notably the argv pointer passed to native functions, is only valid for the duration of the call that produced it. Native functions that call back into Reduct must copy the handles they need out of argv before doing so.

Data Structures

struct  reduct_eval_frame_t
 Evaluation frame structure. More...
 
struct  reduct_eval_local_t
 Per-thread eval-related state structure. More...
 

Macros

#define REDUCT_EVAL_REGS_INITIAL   64
 The initial amount of registers.
 
#define REDUCT_EVAL_REGS_GROWTH_FACTOR   2
 The growth factor of the registers array.
 
#define REDUCT_EVAL_FRAMES_INITIAL   32
 The initial size of the frames array.
 
#define REDUCT_EVAL_FRAMES_GROWTH_FACTOR   2
 The growth factor of the frames array.
 

Functions

REDUCT_API void reduct_eval_local_init (reduct_eval_local_t *local)
 Initialize a local eval state.
 
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_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_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_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 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.
 

Macro Definition Documentation

◆ REDUCT_EVAL_REGS_INITIAL

#define REDUCT_EVAL_REGS_INITIAL   64

The initial amount of registers.

Definition at line 27 of file eval.h.

◆ REDUCT_EVAL_REGS_GROWTH_FACTOR

#define REDUCT_EVAL_REGS_GROWTH_FACTOR   2

The growth factor of the registers array.

Definition at line 28 of file eval.h.

◆ REDUCT_EVAL_FRAMES_INITIAL

#define REDUCT_EVAL_FRAMES_INITIAL   32

The initial size of the frames array.

Definition at line 30 of file eval.h.

◆ REDUCT_EVAL_FRAMES_GROWTH_FACTOR

#define REDUCT_EVAL_FRAMES_GROWTH_FACTOR   2

The growth factor of the frames array.

Definition at line 31 of file eval.h.

Function Documentation

◆ reduct_eval_local_init()

REDUCT_API void reduct_eval_local_init ( reduct_eval_local_t *  local)

Initialize a local eval state.

Parameters
localPointer to the local eval state to initialize.

◆ reduct_eval_local_deinit()

REDUCT_API void reduct_eval_local_deinit ( reduct_eval_local_t *  local)

Deinitialize a local eval state.

Parameters
localPointer to the local eval state to deinitialize.

◆ reduct_eval()

REDUCT_API reduct_handle_t reduct_eval ( struct reduct *  reduct,
reduct_handle_t  handle 
)

Evaluates a handle.

If the handle is a compiled function then it will be interpreted, otherwise, it will first be compiled into a function.

Warning
May be called re-entrantly, including from within a native function. Doing so may grow the register and frame arrays, invalidating any pointers previously obtained into them.
Parameters
reductThe Reduct instance.
handleThe handle to evaluate.
Returns
The result of the evaluation as a Reduct handle.

◆ reduct_eval_file()

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.

Warning
May be called re-entrantly, including from within a native function. Doing so may grow the register and frame arrays, invalidating any pointers previously obtained into them.
Parameters
reductThe Reduct instance.
pathThe path to the file.
flagsOptimization flags to control which optimizations are applied.
Returns
The result of the evaluation.

◆ reduct_eval_string()

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.

Warning
May be called re-entrantly, including from within a native function. Doing so may grow the register and frame arrays, invalidating any pointers previously obtained into them.
Parameters
reductThe Reduct instance.
strThe string to evaluate.
lenThe length of the string.
flagsOptimization flags to control which optimizations are applied.
Returns
The result of the evaluation.

◆ reduct_eval_call()

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.

Warning
May be called re-entrantly, including from within a native function. Doing so may grow the register and frame arrays, invalidating any pointers previously obtained into them.
Parameters
reductThe Reduct instance.
callableThe callable item handle.
argcThe number of arguments.
argvPointer to the arguments array.
Returns
The result of the call.

◆ reduct_eval_call_v()

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.

Warning
May be called re-entrantly, including from within a native function. Doing so may grow the register and frame arrays, invalidating any pointers previously obtained into them.
Parameters
reductThe Reduct instance.
callableThe callable item handle.
argcThe number of arguments.
...The arguments (as reduct_handle_t).
Returns
The result of the call.