|
Reduct
v1.0.4-3-gdaf0d70
A functional and immutable language.
|
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).
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) |
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. | |
| #define REDUCT_HANDLE_NONE 0x0000000000000000ULL |
| #define REDUCT_HANDLE_OFFSET_FLOAT 0x0007000000000000ULL |
| #define REDUCT_HANDLE_TAG_INT 0x0006000000000000ULL |
| #define REDUCT_HANDLE_TAG_ITEM 0x0000000000000000ULL |
| #define REDUCT_HANDLE_MASK_TAG 0xFFFF000000000000ULL |
| #define REDUCT_HANDLE_MASK_VAL 0x0000FFFFFFFFFFFFULL |
| #define REDUCT_HANDLE_MASK_PTR REDUCT_HANDLE_MASK_VAL |
| #define REDUCT_HANDLE_MASK_FLAGS 0x00000000000000FFULL |
| #define REDUCT_HANDLE_FROM_INT | ( | _val | ) | (REDUCT_HANDLE_TAG_INT | ((reduct_handle_t)(_val) & REDUCT_HANDLE_MASK_VAL)) |
| #define REDUCT_HANDLE_FROM_FLOAT | ( | _val | ) |
Create a handle from a float.
| _val | The float value. |
| #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.
| _ptr | The pointer to the reduct_item_t. |
| #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.
| _atom | The pointer to the reduct_atom_t. |
| #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.
| _list | The pointer to the reduct_list_t. |
| #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.
| _func | The pointer to the reduct_function_t. |
| #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.
| _closure | The pointer to the reduct_closure_t. |
| #define REDUCT_HANDLE_IS_INT | ( | _handle | ) | (((*(_handle)) & REDUCT_HANDLE_MASK_TAG) == REDUCT_HANDLE_TAG_INT) |
| #define REDUCT_HANDLE_IS_FLOAT | ( | _handle | ) | ((*(_handle)) >= REDUCT_HANDLE_OFFSET_FLOAT) |
| #define REDUCT_HANDLE_IS_INT_SHAPED | ( | _handle | ) |
Check if a handle is an integer of references a integer shaped item.
| _handle | Pointer to the handle. |
| #define REDUCT_HANDLE_IS_FLOAT_SHAPED | ( | _handle | ) |
Check if a handle is a float or references a float shaped item.
| _handle | Pointer to the handle. |
| #define REDUCT_HANDLE_GET_TYPE | ( | _handle | ) | (REDUCT_HANDLE_IS_ITEM(_handle) ? REDUCT_HANDLE_TO_ITEM(_handle)->type : REDUCT_ITEM_TYPE_ATOM) |
| #define REDUCT_HANDLE_IS_NUMBER_SHAPED | ( | _handle | ) | (REDUCT_HANDLE_IS_INT_SHAPED(_handle) || REDUCT_HANDLE_IS_FLOAT_SHAPED(_handle)) |
| #define REDUCT_HANDLE_IS_ITEM | ( | _handle | ) | (((*(_handle)) & REDUCT_HANDLE_MASK_TAG) == REDUCT_HANDLE_TAG_ITEM) |
| #define REDUCT_HANDLE_IS_ATOM | ( | _handle | ) | (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_ATOM) |
| #define REDUCT_HANDLE_IS_LIST | ( | _handle | ) | (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_LIST) |
| #define REDUCT_HANDLE_IS_FUNCTION | ( | _handle | ) | (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_FUNCTION) |
| #define REDUCT_HANDLE_IS_CLOSURE | ( | _handle | ) | (REDUCT_HANDLE_GET_TYPE(_handle) == REDUCT_ITEM_TYPE_CLOSURE) |
| #define REDUCT_HANDLE_IS_LAMBDA | ( | _handle | ) | (REDUCT_HANDLE_IS_FUNCTION(_handle) || REDUCT_HANDLE_IS_CLOSURE(_handle)) |
| #define REDUCT_HANDLE_IS_NATIVE | ( | _handle | ) | (REDUCT_HANDLE_IS_ITEM(_handle) && (REDUCT_HANDLE_GET_FLAGS(_handle) & REDUCT_ITEM_FLAG_NATIVE)) |
| #define REDUCT_HANDLE_IS_CALLABLE | ( | _handle | ) | (REDUCT_HANDLE_IS_LAMBDA(_handle) || REDUCT_HANDLE_IS_NATIVE(_handle)) |
| #define REDUCT_HANDLE_TO_INT | ( | _handle | ) | (((reduct_int64_t)((*(_handle)) << 16)) >> 16) |
| #define REDUCT_HANDLE_TO_FLOAT | ( | _handle | ) |
Get the float value of a handle.
| _handle | Pointer to the handle. |
| #define REDUCT_HANDLE_TO_ITEM | ( | _handle | ) | ((reduct_item_t*)(void*)((*(_handle)) & REDUCT_HANDLE_MASK_PTR)) |
| #define REDUCT_HANDLE_GET_FLAGS | ( | _handle | ) | (REDUCT_HANDLE_IS_ITEM(_handle) ? REDUCT_HANDLE_TO_ITEM(_handle)->flags : 0) |
| #define REDUCT_HANDLE_FALSE | ( | ) | REDUCT_HANDLE_FROM_INT(0) |
| #define REDUCT_HANDLE_TRUE | ( | ) | REDUCT_HANDLE_FROM_INT(1) |
| #define REDUCT_HANDLE_FROM_BOOL | ( | _cond | ) | ((_cond) ? REDUCT_HANDLE_TRUE() : REDUCT_HANDLE_FALSE()) |
| #define REDUCT_HANDLE_COMPARE_FAST | ( | _reduct, | |
| _a, | |||
| _b, | |||
| _op | |||
| ) |
Compare two handles using a given operator with a fast path for integers and floats.
| _reduct | The Reduct structure. |
| _a | The first handle. |
| _b | The second handle. |
| _op | The comparison operator (e.g., <, >, <=, >=, etc.). |
| #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.
| _reduct | The Reduct structure. |
| _a | The target handle. |
| _b | The first handle. |
| _c | The second handle |
| _op | The arithmetic operator, (e.g., +, -, *, etc.) |
| #define REDUCT_HANDLE_IS_TRUTHY | ( | _handle | ) |
Check if a handle is truthy.
| _handle | Pointer to the handle. |
REDUCT_TRUE if the handle is truthy, REDUCT_FALSE otherwise. | 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.
| reduct | The Reduct structure. |
| handle | The handle to ensure. |
| 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 | The Reduct structure. |
| handle | The handle. |
| 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).
| reduct | The Reduct structure. |
| a | The first handle. |
| b | The second handle. |
| out | The promotion result structure. |
Definition at line 54 of file handle_impl.h.
| 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 | The Reduct structure. |
| a | The first handle, will be upgraded. |
| b | The second handle, will be upgraded. |
REDUCT_TRUE if the items are strictly equal, REDUCT_FALSE otherwise. | 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.
| reduct | The Reduct structure. |
| a | The first handle. |
| b | The second handle. |
| REDUCT_API reduct_handle_t reduct_handle_nil | ( | struct reduct * | reduct | ) |
Get the constant nil handle.
| reduct | Pointer to the Reduct structure. |
| REDUCT_API reduct_handle_t reduct_handle_pi | ( | struct reduct * | reduct | ) |
Get the constant PI handle.
| reduct | Pointer to the Reduct structure. |
| REDUCT_API reduct_handle_t reduct_handle_e | ( | struct reduct * | reduct | ) |
Get the constant E handle.
| reduct | Pointer to the Reduct structure. |
| 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.
| reduct | The Reduct structure. |
| handle | The handle to the atom. |
| outStr | Pointer to store the string pointer. |
| outLen | Pointer to store the string length. |