Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
Handle

Detailed Description

A handle is a lightweight reference to a Reduct item, with the ability to cache various flags from its referenced item or the integer/float value of an atom using Tagged Pointers (NaN Boxing).

64-bit Handle Bit Layout

The top 16 bits are used as a type tag. The remaining 48 bits represent the payload (either a 48-bit integer or a 48-bit pointer).

Tag (16 bits) Payload (48 bits)
0x0000 Item Pointer (reduct_item_t*)
0x0006 Integer Value (48-bit signed)
0x0007...FFFF Float (Shifted IEEE 754 double)
See also
Wikipedia Tagged pointer

Data Structures

struct  reduct_promotion_t
 Promotion result for numeric operations. More...
 

Macros

#define REDUCT_HANDLE_NONE   0x0000000000000000ULL
 Invalid handle constant.
 
#define REDUCT_HANDLE_OFFSET_FLOAT   0x0007000000000000ULL
 Offset used for encoding doubles.
 
#define REDUCT_HANDLE_TAG_INT   0x0006000000000000ULL
 Tag for integer handles.
 
#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_MASK_FLAGS   0x00000000000000FFULL
 Mask for flags (not stored in handle anymore).
 
#define REDUCT_HANDLE_FROM_INT(_val)   (REDUCT_HANDLE_TAG_INT | ((reduct_handle_t)(_val) & REDUCT_HANDLE_MASK_VAL))
 Create a handle from an integer.
 
#define REDUCT_HANDLE_FROM_FLOAT(_val)
 Create a handle from a float.
 
#define REDUCT_HANDLE_FROM_ITEM(_ptr)    (REDUCT_HANDLE_TAG_ITEM | ((reduct_handle_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_IS_INT(_handle)   (((*(_handle)) & REDUCT_HANDLE_MASK_TAG) == REDUCT_HANDLE_TAG_INT)
 Check if a handle is an integer.
 
#define REDUCT_HANDLE_IS_FLOAT(_handle)   ((*(_handle)) >= REDUCT_HANDLE_OFFSET_FLOAT)
 Check if a handle is a float.
 
#define REDUCT_HANDLE_IS_INT_SHAPED(_handle)
 Check if a handle is an integer of references a integer shaped item.
 
#define REDUCT_HANDLE_IS_FLOAT_SHAPED(_handle)
 Check if a handle is a float or references a float shaped item.
 
#define REDUCT_HANDLE_GET_TYPE(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) ? REDUCT_HANDLE_TO_ITEM(_handle)->type : REDUCT_ITEM_TYPE_ATOM)
 Get the type of the item referenced by the handle, or REDUCT_ITEM_TYPE_ATOM if not an item.
 
#define REDUCT_HANDLE_IS_NUMBER_SHAPED(_handle)    (REDUCT_HANDLE_IS_INT_SHAPED(_handle) || REDUCT_HANDLE_IS_FLOAT_SHAPED(_handle))
 Check if a handle is a number or references a number shaped item.
 
#define REDUCT_HANDLE_IS_ITEM(_handle)   (((*(_handle)) & REDUCT_HANDLE_MASK_TAG) == REDUCT_HANDLE_TAG_ITEM)
 Check if a handle is an item.
 
#define REDUCT_HANDLE_IS_ATOM(_handle)   (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_ATOM)
 Check if a handle is an atom.
 
#define REDUCT_HANDLE_IS_LIST(_handle)   (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_LIST)
 Check if a handle is a list.
 
#define REDUCT_HANDLE_IS_FUNCTION(_handle)   (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_FUNCTION)
 Check if a handle is a function.
 
#define REDUCT_HANDLE_IS_CLOSURE(_handle)   (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_CLOSURE)
 Check if a handle is a closure.
 
#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(_handle)    (REDUCT_HANDLE_IS_ITEM(_handle) && (REDUCT_HANDLE_GET_FLAGS(_handle) & REDUCT_ITEM_FLAG_NATIVE))
 Check if a handle is a native function.
 
#define REDUCT_HANDLE_IS_CALLABLE(_handle)   (REDUCT_HANDLE_IS_LAMBDA(_handle) || REDUCT_HANDLE_IS_NATIVE(_handle))
 Check if a handle is callable.
 
#define REDUCT_HANDLE_TO_INT(_handle)   (((reduct_int64_t)((*(_handle)) << 16)) >> 16)
 Get the integer value of a handle.
 
#define REDUCT_HANDLE_TO_FLOAT(_handle)
 Get the float value of a handle.
 
#define REDUCT_HANDLE_TO_ITEM(_handle)   ((reduct_item_t*)(void*)((*(_handle)) & REDUCT_HANDLE_MASK_PTR))
 Get the item pointer of a handle.
 
#define REDUCT_HANDLE_GET_FLAGS(_handle)   (REDUCT_HANDLE_IS_ITEM(_handle) ? REDUCT_HANDLE_TO_ITEM(_handle)->flags : 0)
 Get flags from an item handle.
 
#define REDUCT_HANDLE_FALSE()   REDUCT_HANDLE_FROM_INT(0)
 Constant false handle.
 
#define REDUCT_HANDLE_TRUE()   REDUCT_HANDLE_FROM_INT(1)
 Constant true handle.
 
#define REDUCT_HANDLE_FROM_BOOL(_cond)   ((_cond) ? REDUCT_HANDLE_TRUE() : REDUCT_HANDLE_FALSE())
 Create a boolean handle from a C condition.
 
#define REDUCT_HANDLE_COMPARE_FAST(_reduct, _a, _b, _op)
 Compare two handles using a given operator with a fast path for integers and floats.
 
#define REDUCT_HANDLE_ARITHMETIC_FAST(_reduct, _a, _b, _c, _op)
 Perform a arithmetic operation on two handles with a fast path for integers and floats.
 
#define REDUCT_HANDLE_IS_TRUTHY(_handle)
 Check if a handle is truthy.
 

Enumerations

enum  reduct_promotion_type_t { REDUCT_PROMOTION_TYPE_NONE = 0 , REDUCT_PROMOTION_TYPE_INT = 1 , REDUCT_PROMOTION_TYPE_FLOAT = 2 }
 Promotion types for numeric operations. More...
 

Functions

REDUCT_API void reduct_handle_ensure_item (struct reduct *reduct, reduct_handle_t *handle)
 Ensure that a handle is an item handle.
 
REDUCT_API struct reduct_item * reduct_handle_item (struct reduct *reduct, reduct_handle_t *handle)
 Ensure that a handle is an item and return the pointer.
 
REDUCT_API void reduct_handle_promote (struct reduct *reduct, reduct_handle_t *a, reduct_handle_t *b, reduct_promotion_t *out)
 Promote two handles to a common numeric type.
 
REDUCT_API reduct_bool_t 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 reduct_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 reduct_handle_t reduct_handle_nil (struct reduct *reduct)
 Get the constant nil handle.
 
REDUCT_API reduct_handle_t reduct_handle_pi (struct reduct *reduct)
 Get the constant PI handle.
 
REDUCT_API reduct_handle_t reduct_handle_e (struct reduct *reduct)
 Get the constant E handle.
 
REDUCT_API void reduct_handle_get_string_params (struct reduct *reduct, reduct_handle_t *handle, char **outStr, reduct_size_t *outLen)
 Get the string pointer and length from an atom handle.
 

Macro Definition Documentation

◆ REDUCT_HANDLE_NONE

#define REDUCT_HANDLE_NONE   0x0000000000000000ULL

Invalid handle constant.

Definition at line 36 of file handle.h.

◆ REDUCT_HANDLE_OFFSET_FLOAT

#define REDUCT_HANDLE_OFFSET_FLOAT   0x0007000000000000ULL

Offset used for encoding doubles.

Definition at line 38 of file handle.h.

◆ REDUCT_HANDLE_TAG_INT

#define REDUCT_HANDLE_TAG_INT   0x0006000000000000ULL

Tag for integer handles.

Definition at line 40 of file handle.h.

◆ REDUCT_HANDLE_TAG_ITEM

#define REDUCT_HANDLE_TAG_ITEM   0x0000000000000000ULL

Tag for item handles.

Definition at line 41 of file handle.h.

◆ REDUCT_HANDLE_MASK_TAG

#define REDUCT_HANDLE_MASK_TAG   0xFFFF000000000000ULL

Mask for handle tag bits.

Definition at line 43 of file handle.h.

◆ REDUCT_HANDLE_MASK_VAL

#define REDUCT_HANDLE_MASK_VAL   0x0000FFFFFFFFFFFFULL

Mask for handle value bits.

Definition at line 44 of file handle.h.

◆ REDUCT_HANDLE_MASK_PTR

#define REDUCT_HANDLE_MASK_PTR   REDUCT_HANDLE_MASK_VAL

Mask for item pointer bits.

Definition at line 45 of file handle.h.

◆ REDUCT_HANDLE_MASK_FLAGS

#define REDUCT_HANDLE_MASK_FLAGS   0x00000000000000FFULL

Mask for flags (not stored in handle anymore).

Definition at line 46 of file handle.h.

◆ REDUCT_HANDLE_FROM_INT

#define REDUCT_HANDLE_FROM_INT (   _val)    (REDUCT_HANDLE_TAG_INT | ((reduct_handle_t)(_val) & REDUCT_HANDLE_MASK_VAL))

Create a handle from an integer.

Parameters
_valThe integer value.
Returns
The handle.

Definition at line 54 of file handle.h.

◆ REDUCT_HANDLE_FROM_FLOAT

#define REDUCT_HANDLE_FROM_FLOAT (   _val)
Value:
(((union { \
double d; \
}){.d = (_val)}) \
.u + \
reduct_uint64_t reduct_handle_t
Handle type.
Definition defs.h:189
#define REDUCT_HANDLE_OFFSET_FLOAT
Offset used for encoding doubles.
Definition handle.h:38

Create a handle from a float.

Parameters
_valThe float value.
Returns
The handle.

Definition at line 62 of file handle.h.

◆ REDUCT_HANDLE_FROM_ITEM

#define REDUCT_HANDLE_FROM_ITEM (   _ptr)     (REDUCT_HANDLE_TAG_ITEM | ((reduct_handle_t)(void*)(_ptr) & REDUCT_HANDLE_MASK_PTR))

Create a handle from an item pointer.

Parameters
_ptrThe pointer to the reduct_item_t.
Returns
The handle.

Definition at line 76 of file handle.h.

◆ REDUCT_HANDLE_FROM_ATOM

#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.

Parameters
_atomThe pointer to the reduct_atom_t.
Returns
The handle.

Definition at line 85 of file handle.h.

◆ REDUCT_HANDLE_FROM_LIST

#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.

Parameters
_listThe pointer to the reduct_list_t.
Returns
The handle.

Definition at line 93 of file handle.h.

◆ REDUCT_HANDLE_FROM_FUNCTION

#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.

Parameters
_funcThe pointer to the reduct_function_t.
Returns
The handle.

Definition at line 101 of file handle.h.

◆ REDUCT_HANDLE_FROM_CLOSURE

#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.

Parameters
_closureThe pointer to the reduct_closure_t.
Returns
The handle.

Definition at line 109 of file handle.h.

◆ REDUCT_HANDLE_IS_INT

#define REDUCT_HANDLE_IS_INT (   _handle)    (((*(_handle)) & REDUCT_HANDLE_MASK_TAG) == REDUCT_HANDLE_TAG_INT)

Check if a handle is an integer.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is an integer, zero otherwise.

Definition at line 118 of file handle.h.

◆ REDUCT_HANDLE_IS_FLOAT

#define REDUCT_HANDLE_IS_FLOAT (   _handle)    ((*(_handle)) >= REDUCT_HANDLE_OFFSET_FLOAT)

Check if a handle is a float.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is a float, zero otherwise.

Definition at line 126 of file handle.h.

◆ REDUCT_HANDLE_IS_INT_SHAPED

#define REDUCT_HANDLE_IS_INT_SHAPED (   _handle)
Value:
#define REDUCT_HANDLE_IS_INT(_handle)
Check if a handle is an integer.
Definition handle.h:118
#define REDUCT_HANDLE_GET_FLAGS(_handle)
Get flags from an item handle.
Definition handle.h:265
#define REDUCT_ITEM_FLAG_INT_SHAPED
Item is an integer shaped atom.
Definition item.h:39
#define REDUCT_ITEM_FLAG_QUOTED
Item is a quoted atom.
Definition item.h:43

Check if a handle is an integer of references a integer shaped item.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is integer shaped, zero otherwise.

Definition at line 134 of file handle.h.

◆ REDUCT_HANDLE_IS_FLOAT_SHAPED

#define REDUCT_HANDLE_IS_FLOAT_SHAPED (   _handle)
Value:
#define REDUCT_HANDLE_IS_FLOAT(_handle)
Check if a handle is a float.
Definition handle.h:126
#define REDUCT_ITEM_FLAG_FLOAT_SHAPED
Item is a float shaped atom.
Definition item.h:40

Check if a handle is a float or references a float shaped item.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is float shaped, zero otherwise.

Definition at line 144 of file handle.h.

◆ REDUCT_HANDLE_GET_TYPE

#define REDUCT_HANDLE_GET_TYPE (   _handle)     (REDUCT_HANDLE_IS_ITEM(_handle) ? REDUCT_HANDLE_TO_ITEM(_handle)->type : REDUCT_ITEM_TYPE_ATOM)

Get the type of the item referenced by the handle, or REDUCT_ITEM_TYPE_ATOM if not an item.

Parameters
_handlePointer to the handle.

Definition at line 153 of file handle.h.

◆ REDUCT_HANDLE_IS_NUMBER_SHAPED

#define REDUCT_HANDLE_IS_NUMBER_SHAPED (   _handle)     (REDUCT_HANDLE_IS_INT_SHAPED(_handle) || REDUCT_HANDLE_IS_FLOAT_SHAPED(_handle))

Check if a handle is a number or references a number shaped item.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is a number, zero otherwise.

Definition at line 162 of file handle.h.

◆ REDUCT_HANDLE_IS_ITEM

#define REDUCT_HANDLE_IS_ITEM (   _handle)    (((*(_handle)) & REDUCT_HANDLE_MASK_TAG) == REDUCT_HANDLE_TAG_ITEM)

Check if a handle is an item.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is an item, zero otherwise.

Definition at line 171 of file handle.h.

◆ REDUCT_HANDLE_IS_ATOM

#define REDUCT_HANDLE_IS_ATOM (   _handle)    (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_ATOM)

Check if a handle is an atom.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is an atom, zero otherwise.

Definition at line 179 of file handle.h.

◆ REDUCT_HANDLE_IS_LIST

#define REDUCT_HANDLE_IS_LIST (   _handle)    (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_LIST)

Check if a handle is a list.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is a list, zero otherwise.

Definition at line 187 of file handle.h.

◆ REDUCT_HANDLE_IS_FUNCTION

#define REDUCT_HANDLE_IS_FUNCTION (   _handle)    (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_FUNCTION)

Check if a handle is a function.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is a function, zero otherwise.

Definition at line 195 of file handle.h.

◆ REDUCT_HANDLE_IS_CLOSURE

#define REDUCT_HANDLE_IS_CLOSURE (   _handle)    (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_CLOSURE)

Check if a handle is a closure.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is a closure, zero otherwise.

Definition at line 203 of file handle.h.

◆ REDUCT_HANDLE_IS_LAMBDA

#define REDUCT_HANDLE_IS_LAMBDA (   _handle)    (REDUCT_HANDLE_IS_FUNCTION(_handle) || REDUCT_HANDLE_IS_CLOSURE(_handle))

Check if a handle is a lambda.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is a lambda, zero otherwise.

Definition at line 211 of file handle.h.

◆ REDUCT_HANDLE_IS_NATIVE

#define REDUCT_HANDLE_IS_NATIVE (   _handle)     (REDUCT_HANDLE_IS_ITEM(_handle) && (REDUCT_HANDLE_GET_FLAGS(_handle) & REDUCT_ITEM_FLAG_NATIVE))

Check if a handle is a native function.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is a native function, zero otherwise.

Definition at line 219 of file handle.h.

◆ REDUCT_HANDLE_IS_CALLABLE

#define REDUCT_HANDLE_IS_CALLABLE (   _handle)    (REDUCT_HANDLE_IS_LAMBDA(_handle) || REDUCT_HANDLE_IS_NATIVE(_handle))

Check if a handle is callable.

Parameters
_handlePointer to the handle.
Returns
Non-zero if the handle is callable, zero otherwise.

Definition at line 228 of file handle.h.

◆ REDUCT_HANDLE_TO_INT

#define REDUCT_HANDLE_TO_INT (   _handle)    (((reduct_int64_t)((*(_handle)) << 16)) >> 16)

Get the integer value of a handle.

Parameters
_handlePointer to the handle.
Returns
The integer value.

Definition at line 236 of file handle.h.

◆ REDUCT_HANDLE_TO_FLOAT

#define REDUCT_HANDLE_TO_FLOAT (   _handle)
Value:
(((union { \
double d; \
}){.u = (*(_handle)) - REDUCT_HANDLE_OFFSET_FLOAT}) \
.d)

Get the float value of a handle.

Parameters
_handlePointer to the handle.
Returns
The float value.

Definition at line 244 of file handle.h.

◆ REDUCT_HANDLE_TO_ITEM

#define REDUCT_HANDLE_TO_ITEM (   _handle)    ((reduct_item_t*)(void*)((*(_handle)) & REDUCT_HANDLE_MASK_PTR))

Get the item pointer of a handle.

Parameters
_handlePointer to the handle.
Returns
The item pointer.

Definition at line 257 of file handle.h.

◆ REDUCT_HANDLE_GET_FLAGS

#define REDUCT_HANDLE_GET_FLAGS (   _handle)    (REDUCT_HANDLE_IS_ITEM(_handle) ? REDUCT_HANDLE_TO_ITEM(_handle)->flags : 0)

Get flags from an item handle.

Parameters
_handlePointer to the handle.
Returns
The flags stored in the handle.

Definition at line 265 of file handle.h.

◆ REDUCT_HANDLE_FALSE

#define REDUCT_HANDLE_FALSE ( )    REDUCT_HANDLE_FROM_INT(0)

Constant false handle.

Definition at line 267 of file handle.h.

◆ REDUCT_HANDLE_TRUE

#define REDUCT_HANDLE_TRUE ( )    REDUCT_HANDLE_FROM_INT(1)

Constant true handle.

Definition at line 269 of file handle.h.

◆ REDUCT_HANDLE_FROM_BOOL

#define REDUCT_HANDLE_FROM_BOOL (   _cond)    ((_cond) ? REDUCT_HANDLE_TRUE() : REDUCT_HANDLE_FALSE())

Create a boolean handle from a C condition.

Parameters
_condThe condition to evaluate.

Definition at line 276 of file handle.h.

◆ REDUCT_HANDLE_COMPARE_FAST

#define REDUCT_HANDLE_COMPARE_FAST (   _reduct,
  _a,
  _b,
  _op 
)
Value:
(((((*(_a)) ^ REDUCT_HANDLE_TAG_INT) | ((*(_b)) ^ REDUCT_HANDLE_TAG_INT)) & REDUCT_HANDLE_MASK_TAG) == 0 \
: (reduct_handle_compare(_reduct, _a, _b) _op 0)))
#define REDUCT_HANDLE_TO_INT(_handle)
Get the integer value of a handle.
Definition handle.h:236
#define REDUCT_HANDLE_TAG_INT
Tag for integer handles.
Definition handle.h:40
#define REDUCT_HANDLE_TO_FLOAT(_handle)
Get the float value of a handle.
Definition handle.h:244
#define REDUCT_HANDLE_MASK_TAG
Mask for handle tag bits.
Definition handle.h:43
REDUCT_API reduct_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).

Compare two handles using a given operator with a fast path for integers and floats.

Parameters
_reductThe Reduct structure.
_aThe first handle.
_bThe second handle.
_opThe comparison operator (e.g., <, >, <=, >=, etc.).
Returns
The result of the comparison.

Definition at line 287 of file handle.h.

◆ REDUCT_HANDLE_ARITHMETIC_FAST

#define REDUCT_HANDLE_ARITHMETIC_FAST (   _reduct,
  _a,
  _b,
  _c,
  _op 
)
Value:
do \
{ \
reduct_handle_t _bVal = *(_b); \
reduct_handle_t _cVal = *(_c); \
{ \
} \
else if (REDUCT_HANDLE_IS_FLOAT(&_bVal) && REDUCT_HANDLE_IS_FLOAT(&_cVal)) \
{ \
} \
else \
{ \
reduct_handle_promote(_reduct, _b, _c, &prom); \
{ \
*(_a) = REDUCT_HANDLE_FROM_INT(prom.a.intVal _op prom.b.intVal); \
} \
else \
{ \
*(_a) = REDUCT_HANDLE_FROM_FLOAT(prom.a.floatVal _op prom.b.floatVal); \
} \
} \
} while (0)
#define REDUCT_LIKELY(_x)
Definition defs.h:117
#define REDUCT_HANDLE_FROM_FLOAT(_val)
Create a handle from a float.
Definition handle.h:62
#define REDUCT_HANDLE_FROM_INT(_val)
Create a handle from an integer.
Definition handle.h:54
@ REDUCT_PROMOTION_TYPE_INT
Definition handle.h:374
Promotion result for numeric operations.
Definition handle.h:383
reduct_int64_t intVal
Definition handle.h:386
reduct_float_t floatVal
Definition handle.h:387
union reduct_promotion_t::@7 a
union reduct_promotion_t::@8 b
reduct_promotion_type_t type
Definition handle.h:384

Perform a arithmetic operation on two handles with a fast path for integers and floats.

Parameters
_reductThe Reduct structure.
_aThe target handle.
_bThe first handle.
_cThe second handle
_opThe arithmetic operator, (e.g., +, -, *, etc.)

Definition at line 303 of file handle.h.

◆ REDUCT_HANDLE_IS_TRUTHY

#define REDUCT_HANDLE_IS_TRUTHY (   _handle)
Value:
? (REDUCT_HANDLE_TO_INT(_handle) != 0) \
: (REDUCT_HANDLE_IS_FLOAT(_handle) \
? (REDUCT_HANDLE_TO_FLOAT(_handle) != 0.0) \
: (REDUCT_HANDLE_IS_ITEM(_handle) \
? ((*(_handle)) != REDUCT_HANDLE_NONE && \
@ REDUCT_FALSE
Definition defs.h:137
#define REDUCT_HANDLE_IS_ITEM(_handle)
Check if a handle is an item.
Definition handle.h:171
#define REDUCT_HANDLE_TO_ITEM(_handle)
Get the item pointer of a handle.
Definition handle.h:257
#define REDUCT_HANDLE_NONE
Invalid handle constant.
Definition handle.h:36
#define REDUCT_ITEM_FLAG_FALSY
Item is falsy.
Definition item.h:38

Check if a handle is truthy.

Parameters
_handlePointer to the handle.
Returns
REDUCT_TRUE if the handle is truthy, REDUCT_FALSE otherwise.

Definition at line 338 of file handle.h.

Enumeration Type Documentation

◆ reduct_promotion_type_t

Promotion types for numeric operations.

Enumerator
REDUCT_PROMOTION_TYPE_NONE 
REDUCT_PROMOTION_TYPE_INT 
REDUCT_PROMOTION_TYPE_FLOAT 

Definition at line 371 of file handle.h.

Function Documentation

◆ reduct_handle_ensure_item()

REDUCT_API void reduct_handle_ensure_item ( struct reduct *  reduct,
reduct_handle_t handle 
)

Ensure that a handle is an item handle.

If the handle is an integer or float, it will be upgraded to an item handle by looking up a corresponding atom.

Parameters
reductThe Reduct structure.
handleThe handle to ensure.
Here is the caller graph for this function:

◆ reduct_handle_item()

REDUCT_API struct reduct_item * reduct_handle_item ( struct reduct *  reduct,
reduct_handle_t handle 
)

Ensure that a handle is an item and return the pointer.

Parameters
reductThe Reduct structure.
handleThe handle.
Returns
The item pointer.
Here is the caller graph for this function:

◆ reduct_handle_promote()

REDUCT_API void reduct_handle_promote ( struct reduct *  reduct,
reduct_handle_t a,
reduct_handle_t b,
reduct_promotion_t out 
)

Promote two handles to a common numeric type.

If both handles are numeric, they are promoted to the highest precision type (float if either is a float, otherwise integer).

Parameters
reductThe Reduct structure.
aThe first handle.
bThe second handle.
outThe promotion result structure.

Definition at line 54 of file handle_impl.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reduct_handle_is_equal()

REDUCT_API reduct_bool_t 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.

Parameters
reductThe Reduct structure.
aThe first handle, will be upgraded.
bThe second handle, will be upgraded.
Returns
REDUCT_TRUE if the items are strictly equal, REDUCT_FALSE otherwise.
Here is the caller graph for this function:

◆ reduct_handle_compare()

REDUCT_API reduct_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).

Useful for sorting or range checks.

Parameters
reductThe Reduct structure.
aThe first handle.
bThe second handle.
Returns
A negative value if a < b, zero if a == b, and a positive value if a > b.
Here is the caller graph for this function:

◆ reduct_handle_nil()

REDUCT_API reduct_handle_t reduct_handle_nil ( struct reduct *  reduct)

Get the constant nil handle.

Parameters
reductPointer to the Reduct structure.
Returns
The nil handle.
Here is the caller graph for this function:

◆ reduct_handle_pi()

REDUCT_API reduct_handle_t reduct_handle_pi ( struct reduct *  reduct)

Get the constant PI handle.

Parameters
reductPointer to the Reduct structure.
Returns
The PI handle.

◆ reduct_handle_e()

REDUCT_API reduct_handle_t reduct_handle_e ( struct reduct *  reduct)

Get the constant E handle.

Parameters
reductPointer to the Reduct structure.
Returns
The E handle.

◆ reduct_handle_get_string_params()

REDUCT_API void reduct_handle_get_string_params ( struct reduct *  reduct,
reduct_handle_t handle,
char **  outStr,
reduct_size_t outLen 
)

Get the string pointer and length from an atom handle.

Parameters
reductThe Reduct structure.
handleThe handle to the atom.
outStrPointer to store the string pointer.
outLenPointer to store the string length.
Here is the caller graph for this function: