Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
handle.h File Reference

Handle management. More...

#include <reduct/atom.h>
#include <reduct/defs.h>
#include <reduct/error.h>
#include <reduct/item.h>
#include <reduct/standard.h>
#include <math.h>
Include dependency graph for handle.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define REDUCT_HANDLE_OFFSET_NUMBER   0x0007000000000000ULL
 Offset used for encoding doubles.
 
#define REDUCT_HANDLE_TAG_ITEM   0x0000000000000000ULL
 Tag for item handles.
 
#define REDUCT_HANDLE_MASK_TAG   0xFFFF000000000000ULL
 Mask for handle tag bits.
 
#define REDUCT_HANDLE_MASK_VAL   0x0000FFFFFFFFFFFFULL
 Mask for handle value bits.
 
#define REDUCT_HANDLE_MASK_PTR   REDUCT_HANDLE_MASK_VAL
 Mask for item pointer bits.
 
#define REDUCT_HANDLE_NUMBER_WIDTH   64
 The bit width used for shift operations on numbers.
 
#define REDUCT_HANDLE_FROM_NUMBER(_val)
 Create a handle from a number.
 
#define REDUCT_HANDLE_FROM_ITEM(_ptr)    ((reduct_handle_t){REDUCT_HANDLE_TAG_ITEM | ((uintptr_t)(void*)(_ptr) & REDUCT_HANDLE_MASK_PTR)})
 Create a handle from an item pointer.
 
#define REDUCT_HANDLE_FROM_ATOM(_atom)   REDUCT_HANDLE_FROM_ITEM(REDUCT_CONTAINER_OF(_atom, reduct_item_t, atom))
 Create a handle from an atom pointer.
 
#define REDUCT_HANDLE_FROM_LIST(_list)   REDUCT_HANDLE_FROM_ITEM(REDUCT_CONTAINER_OF(_list, reduct_item_t, list))
 Create a handle from a list pointer.
 
#define REDUCT_HANDLE_FROM_FUNCTION(_func)   REDUCT_HANDLE_FROM_ITEM(REDUCT_CONTAINER_OF(_func, reduct_item_t, function))
 Create a handle from a function pointer.
 
#define REDUCT_HANDLE_FROM_CLOSURE(_closure)    REDUCT_HANDLE_FROM_ITEM(REDUCT_CONTAINER_OF(_closure, reduct_item_t, closure))
 Create a handle from a closure pointer.
 
#define REDUCT_HANDLE_FROM_RVSDG_NODE(_node)    REDUCT_HANDLE_FROM_ITEM(REDUCT_CONTAINER_OF(_node, reduct_item_t, rvsdgNode))
 Create a handle from an IR node pointer.
 
#define REDUCT_HANDLE_FROM_RVSDG_EDGE(_edge)    REDUCT_HANDLE_FROM_ITEM(REDUCT_CONTAINER_OF(_edge, reduct_item_t, rvsdgEdge))
 Create a handle from an IR edge pointer.
 
#define REDUCT_HANDLE_FROM_BOOL(_reduct, _cond)   ((_cond) ? REDUCT_HANDLE_TRUE() : REDUCT_HANDLE_FALSE(_reduct))
 Create a boolean handle from a C condition.
 
#define REDUCT_HANDLE_FROM_FUTURE(_future)   REDUCT_HANDLE_FROM_ITEM(REDUCT_CONTAINER_OF(_future, reduct_item_t, future))
 Create a handle from a future pointer.
 
#define REDUCT_HANDLE_IS_NUMBER(_handle)   (((_handle)._value) >= REDUCT_HANDLE_OFFSET_NUMBER)
 Check if a handle is a number.
 
#define REDUCT_HANDLE_IS_NUMBER_SHAPED(_handle)
 Check if a handle is a number or references a number shaped item.
 
#define REDUCT_HANDLE_GET_TYPE_STRING(_handle)   (reduct_handle_type_string(reduct_handle_get_type(_handle)))
 Get the string representation of the type of the item referenced by the handle.
 
#define REDUCT_HANDLE_IS_NIL(_handle)   (REDUCT_HANDLE_IS_LIST(_handle) && REDUCT_HANDLE_TO_LIST(_handle)->length == 0)
 Check if a handle is nil (an empty list).
 
#define REDUCT_HANDLE_IS_EMPTY(_handle)    (REDUCT_HANDLE_IS_NIL(_handle) || (REDUCT_HANDLE_IS_ATOM(_handle) && REDUCT_HANDLE_TO_ATOM(_handle)->length == 0))
 Check if a handle is empty (nil or empty atom).
 
#define REDUCT_HANDLE_IS_ITEM(_handle)   ((((_handle)._value) & REDUCT_HANDLE_MASK_TAG) == REDUCT_HANDLE_TAG_ITEM)
 Check if a handle is an item.
 
#define REDUCT_HANDLE_IS_ATOM(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) && REDUCT_HANDLE_TO_ITEM(_handle)->type == REDUCT_ITEM_TYPE_ATOM)
 Check if a handle is an atom.
 
#define REDUCT_HANDLE_IS_LIST(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) && REDUCT_HANDLE_TO_ITEM(_handle)->type == REDUCT_ITEM_TYPE_LIST)
 Check if a handle is a list.
 
#define REDUCT_HANDLE_IS_FUNCTION(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) && REDUCT_HANDLE_TO_ITEM(_handle)->type == REDUCT_ITEM_TYPE_FUNCTION)
 Check if a handle is a function.
 
#define REDUCT_HANDLE_IS_CLOSURE(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) && REDUCT_HANDLE_TO_ITEM(_handle)->type == REDUCT_ITEM_TYPE_CLOSURE)
 Check if a handle is a closure.
 
#define REDUCT_HANDLE_IS_RVSDG_NODE(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) && REDUCT_HANDLE_TO_ITEM(_handle)->type == REDUCT_ITEM_TYPE_RVSDG_NODE)
 Check if a handle is an IR node.
 
#define REDUCT_HANDLE_IS_RVSDG_EDGE(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) && REDUCT_HANDLE_TO_ITEM(_handle)->type == REDUCT_ITEM_TYPE_RVSDG_EDGE)
 Check if a handle is an IR edge.
 
#define REDUCT_HANDLE_IS_FUTURE(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) && REDUCT_HANDLE_TO_ITEM(_handle)->type == REDUCT_ITEM_TYPE_FUTURE)
 Check if a handle is a future.
 
#define REDUCT_HANDLE_IS_LAMBDA(_handle)   (REDUCT_HANDLE_IS_FUNCTION(_handle) || REDUCT_HANDLE_IS_CLOSURE(_handle))
 Check if a handle is a lambda.
 
#define REDUCT_HANDLE_IS_NATIVE(_reduct, _handle)    (REDUCT_HANDLE_IS_ATOM(_handle) && reduct_atom_is_native(_reduct, REDUCT_HANDLE_TO_ATOM(_handle)))
 Check if a handle is a native function.
 
#define REDUCT_HANDLE_IS_INTRINSIC(_reduct, _handle)    (REDUCT_HANDLE_IS_ATOM(_handle) && reduct_atom_is_intrinsic(_reduct, REDUCT_HANDLE_TO_ATOM(_handle)))
 Check if a handle is an intrinsic function.
 
#define REDUCT_HANDLE_IS_CALLABLE(_reduct, _handle)    (REDUCT_HANDLE_IS_LAMBDA(_handle) || REDUCT_HANDLE_IS_NATIVE(_reduct, _handle))
 Check if a handle is callable.
 
#define REDUCT_HANDLE_IS_ATOM_LIKE(_handle)   (REDUCT_HANDLE_IS_NUMBER(_handle) || REDUCT_HANDLE_IS_ATOM(_handle))
 Check if a handle either is an atom, or could be represented by an atom item.
 
#define REDUCT_HANDLE_TO_NUMBER(_handle)
 Get the number value of a handle.
 
#define REDUCT_HANDLE_TO_ITEM(_handle)   ((reduct_item_t*)(void*)(((_handle)._value) & REDUCT_HANDLE_MASK_PTR))
 Get the item pointer of a handle.
 
#define REDUCT_HANDLE_TO_ATOM(_handle)   (&REDUCT_HANDLE_TO_ITEM(_handle)->atom)
 Get the atom pointer of a handle.
 
#define REDUCT_HANDLE_TO_LIST(_handle)   (&REDUCT_HANDLE_TO_ITEM(_handle)->list)
 Get the list pointer of a handle.
 
#define REDUCT_HANDLE_TO_FUNCTION(_handle)   (&REDUCT_HANDLE_TO_ITEM(_handle)->function)
 Get the function pointer of a handle.
 
#define REDUCT_HANDLE_TO_CLOSURE(_handle)   (&REDUCT_HANDLE_TO_ITEM(_handle)->closure)
 Get the closure pointer of a handle.
 
#define REDUCT_HANDLE_TO_RVSDG_NODE(_handle)   (&REDUCT_HANDLE_TO_ITEM(_handle)->rvsdgNode)
 Get the IR node pointer of a handle.
 
#define REDUCT_HANDLE_TO_RVSDG_EDGE(_handle)   (&REDUCT_HANDLE_TO_ITEM(_handle)->rvsdgEdge)
 Get the IR edge pointer of a handle.
 
#define REDUCT_HANDLE_TO_FUTURE(_handle)   (&REDUCT_HANDLE_TO_ITEM(_handle)->future)
 Get the future pointer of a handle.
 
#define REDUCT_HANDLE_TO_BOOL(_handle)   (REDUCT_HANDLE_IS_TRUTHY(_handle) ? true : false)
 Get the boolean value of a handle.
 
#define REDUCT_HANDLE_CREATE_LIST(_reduct, _length)   REDUCT_HANDLE_FROM_LIST(reduct_list_new(_reduct, _length))
 Create a list handle.
 
#define REDUCT_HANDLE_CREATE_HANDLES(_reduct, _count, _handles)    REDUCT_HANDLE_FROM_LIST(reduct_list_new_handles(_reduct, _count, _handles))
 Create a list handle from an array of handles.
 
#define REDUCT_HANDLE_CREATE_ALIST(_reduct, _count, ...)    REDUCT_HANDLE_FROM_LIST(reduct_list_new_alist(_reduct, _count, __VA_ARGS__))
 Create a list handle of pairs (key-value) from a variable number of pairs.
 
#define REDUCT_HANDLE_CREATE_ATOM(_reduct, _len)   REDUCT_HANDLE_FROM_ATOM(reduct_atom_new(_reduct, _len))
 Create an atom handle with a reserved size.
 
#define REDUCT_HANDLE_CREATE_STRING(_reduct, _str)    REDUCT_HANDLE_FROM_ATOM(reduct_atom_lookup(_reduct, _str, strlen(_str), REDUCT_ATOM_LOOKUP_QUOTED))
 Create an atom handle from a string.
 
#define REDUCT_HANDLE_CREATE_SYMBOL(_reduct, _str)    REDUCT_HANDLE_FROM_ATOM(reduct_atom_lookup(_reduct, _str, strlen(_str), REDUCT_ATOM_LOOKUP_NONE))
 Create an interned atom handle from a string.
 
#define REDUCT_HANDLE_CREATE_NUMBER(_reduct, _val)   REDUCT_HANDLE_FROM_ATOM(reduct_atom_new_number(_reduct, _val))
 Create an atom handle from a number.
 
#define REDUCT_HANDLE_CREATE_NATIVE(_reduct, _fn)   REDUCT_HANDLE_FROM_ATOM(reduct_atom_new_native(_reduct, _fn))
 Create an atom handle from a native function.
 
#define REDUCT_HANDLE_CREATE_FUTURE(_reduct, _callable, _argc, _argv)    REDUCT_HANDLE_FROM_FUTURE(reduct_future_new(_reduct, _callable, _argc, _argv))
 Create a future handle.
 
#define REDUCT_HANDLE_FOR_EACH(_handle, _list)   REDUCT_LIST_FOR_EACH(_handle, REDUCT_HANDLE_TO_LIST(_list))
 Macro for iterating over all elements in a list handle.
 
#define REDUCT_HANDLE_JOIN(_reduct, _handle)    (REDUCT_HANDLE_IS_FUTURE(_handle) ? reduct_future_join(_reduct, REDUCT_HANDLE_TO_FUTURE(_handle)) : (_handle))
 Get the value of the future referenced by the handle or the handle itself.
 
#define REDUCT_HANDLE_NIL(_reduct)   ((_reduct)->global->nil)
 Get the constant nil handle.
 
#define REDUCT_HANDLE_FALSE(_reduct)   REDUCT_HANDLE_NIL(_reduct)
 Get the constant false (nil) handle.
 
#define REDUCT_HANDLE_TRUE()   REDUCT_HANDLE_FROM_NUMBER(1.0)
 Constant true handle.
 
#define REDUCT_HANDLE_PI()   REDUCT_HANDLE_FROM_NUMBER(REDUCT_PI)
 Constant pi handle.
 
#define REDUCT_HANDLE_E()   REDUCT_HANDLE_FROM_NUMBER(REDUCT_E)
 Constant e handle.
 
#define REDUCT_HANDLE_INF()   REDUCT_HANDLE_FROM_NUMBER(REDUCT_INF)
 Constant infinity handle.
 
#define REDUCT_HANDLE_NAN()   REDUCT_HANDLE_FROM_NUMBER(REDUCT_NAN)
 Constant not a number handle.
 
#define REDUCT_HANDLE_COMPARE_FAST(_reduct, _a, _b, _op)
 Compare two handles using a given operator with a fast path for numbers.
 
#define REDUCT_HANDLE_ARITHMETIC_FAST(_reduct, _a, _b, _c, _op)
 Perform a arithmetic operation on two handles with a fast path for numbers.
 
#define REDUCT_HANDLE_DIV_FAST(_reduct, _a, _b, _c)
 Perform a division operation on two handles with a fast path for numbers.
 
#define REDUCT_HANDLE_MOD_FAST(_reduct, _a, _b, _c)
 Perform a modulo operation on two handles.
 
#define REDUCT_HANDLE_BITWISE_FAST(_reduct, _a, _b, _c, _op)
 Perform a bitwise operation on two handles.
 
#define REDUCT_HANDLE_IS_TRUTHY(_handle)    (!REDUCT_HANDLE_IS_LIST(_handle) || REDUCT_HANDLE_TO_LIST(_handle)->length != 0)
 Check if a handle is truthy.
 
#define REDUCT_HANDLE_RETAIN(_reduct, _handle)
 Retain a handle, preventing its referenced item from being collected by the garbage collector.
 
#define REDUCT_HANDLE_RELEASE(_reduct, _handle)
 Release a handle, allowing its referenced item to be collected by the garbage collector.
 
#define REDUCT_HANDLE_FREE(_reduct, _handle)
 Free the item that a handle is referencing.
 

Enumerations

enum  reduct_handle_type_t {
  REDUCT_HANDLE_TYPE_NONE = 0 , REDUCT_HANDLE_TYPE_NUMBER , REDUCT_HANDLE_TYPE_ATOM , REDUCT_HANDLE_TYPE_LIST ,
  REDUCT_HANDLE_TYPE_FUNCTION , REDUCT_HANDLE_TYPE_CLOSURE , REDUCT_HANDLE_TYPE_ARENA , REDUCT_HANDLE_TYPE_RVSDG_NODE ,
  REDUCT_HANDLE_TYPE_RVSDG_EDGE , REDUCT_HANDLE_TYPE_FUTURE , REDUCT_HANDLE_TYPE_UNKNOWN
}
 High-level handle types. More...
 

Functions

REDUCT_API reduct_handle_type_t reduct_handle_get_type (reduct_handle_t handle)
 Get the high-level type of a handle.
 
REDUCT_API const char * reduct_handle_type_string (reduct_handle_type_t type)
 Get the string name of a handle type.
 
REDUCT_API void reduct_handle_ensure_item (struct reduct *reduct, reduct_handle_t *handle)
 Ensure that a handle is an item handle.
 
static REDUCT_ALWAYS_INLINE struct reduct_item * reduct_handle_as_item (struct reduct *reduct, reduct_handle_t handle)
 Ensure that a handle is an item and return the pointer.
 
static REDUCT_ALWAYS_INLINE int64_t reduct_handle_as_int (struct reduct *reduct, reduct_handle_t handle)
 Retrieve the integer representation of the handle.
 
static REDUCT_ALWAYS_INLINE double reduct_handle_as_number (struct reduct *reduct, reduct_handle_t handle)
 Retrieve the number representation of the handle.
 
static REDUCT_ALWAYS_INLINE reduct_atom_treduct_handle_as_atom (struct reduct *reduct, reduct_handle_t handle)
 Retrieve the atom pointer of the handle.
 
REDUCT_API bool reduct_handle_is_equal (struct reduct *reduct, reduct_handle_t a, reduct_handle_t b)
 Check if two items are exactly equal string-wise or structurally.
 
REDUCT_API int64_t reduct_handle_compare (struct reduct *reduct, reduct_handle_t a, reduct_handle_t b)
 Compare two items for ordering (less than, equal, or greater than).
 
REDUCT_API void reduct_handle_atom_string (struct reduct *reduct, reduct_handle_t *handle, const char **outStr, size_t *outLen)
 Get the string pointer and length from an atom handle.
 
REDUCT_API reduct_handle_t reduct_handle_nth (struct reduct *reduct, reduct_handle_t handle, size_t index)
 Get the element at the specified index from a list or atom handle.
 
REDUCT_API size_t reduct_handle_len (struct reduct *reduct, reduct_handle_t handle)
 Get the length of a handle (list elements or atom characters).
 
REDUCT_API bool reduct_handle_is_str (struct reduct *reduct, reduct_handle_t handle, const char *str)
 Check if an atom handle is equal to a string.
 

Detailed Description

Handle management.

Definition in file handle.h.