Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
item_impl.h
Go to the documentation of this file.
1#ifndef REDUCT_ITEM_IMPL_H
2#define REDUCT_ITEM_IMPL_H 1
3
4#include "closure.h"
5#include "core.h"
6#include "function.h"
7#include "gc.h"
8#include "item.h"
9
10static inline void reduct_item_init(reduct_item_t* item)
11{
13 item->flags = 0;
14 item->retainCount = 0;
15 item->input = REDUCT_NULL;
16 item->position = 0;
17}
18
20{
21 REDUCT_ASSERT(reduct != REDUCT_NULL);
22
23 reduct_item_t* item = reduct->freeList;
24 reduct->freeList = item->free;
25 return item;
26}
27
29{
30 REDUCT_ASSERT(reduct != REDUCT_NULL);
31
32 if (REDUCT_UNLIKELY(reduct->freeList == REDUCT_NULL))
33 {
34 reduct_gc_if_needed(reduct);
35 }
36
37 if (reduct->freeList != REDUCT_NULL)
38 {
39 return reduct_item_pop_free_list(reduct);
40 }
41
44 if (reduct->block == REDUCT_NULL)
45 {
46 block = &reduct->firstBlock;
47 }
48 else
49 {
50 block = REDUCT_MALLOC(sizeof(reduct_item_block_t));
51 if (block == REDUCT_NULL)
52 {
53 REDUCT_ERROR_INTERNAL(reduct, "out of memory");
54 }
55 }
56 reduct->blocksAllocated++;
57
58 for (reduct_size_t i = 0; i < REDUCT_ITEM_BLOCK_MAX; i++)
59 {
60 reduct_item_init(&block->items[i]);
61 }
62
63 for (reduct_size_t i = 1; i < REDUCT_ITEM_BLOCK_MAX - 1; i++)
64 {
65 block->items[i].free = &block->items[i + 1];
66 }
68
69 reduct->freeList = &block->items[1];
70 block->next = reduct->block;
71 reduct->block = block;
72
73 item = &block->items[0];
74 return item;
75}
76
78{
79 REDUCT_ASSERT(reduct != REDUCT_NULL);
81
82 switch (item->type)
83 {
85 {
86 reduct_atom_deinit(reduct, &item->atom);
87 break;
88 }
90 {
92 break;
93 }
95 {
97 break;
98 }
99 default:
100 break;
101 }
102
103 reduct_item_init(item);
104
105#ifndef NDEBUG
106 REDUCT_MEMSET(&item->atom, 0xFE, sizeof(reduct_item_t) - offsetof(reduct_item_t, atom));
107#endif
108
109 item->free = reduct->freeList;
110 reduct->freeList = item;
111}
112
114{
116 {
117 return item->atom.integerValue;
118 }
120 {
121 return (reduct_int64_t)item->atom.floatValue;
122 }
123 return 0;
124}
125
127{
129 {
130 return item->atom.floatValue;
131 }
133 {
134 return (reduct_float_t)item->atom.integerValue;
135 }
136 return 0.0;
137}
138
140{
141 switch (type)
142 {
144 return "none";
146 return "atom";
148 return "list";
150 return "function";
152 return "closure";
154 return "list node";
155 default:
156 return "unknown";
157 }
158}
159
160#endif
Closure management.
Core definitions and structures.
#define REDUCT_MALLOC(_size)
Definition defs.h:27
size_t reduct_size_t
Definition defs.h:100
#define REDUCT_UNLIKELY(_x)
Definition defs.h:118
int64_t reduct_int64_t
Definition defs.h:92
double reduct_float_t
Definition defs.h:102
#define REDUCT_ASSERT(_cond)
Definition defs.h:25
#define REDUCT_MEMSET(_ptr, _val, _size)
Definition defs.h:32
#define REDUCT_API
Definition defs.h:7
#define REDUCT_NULL
Definition defs.h:23
Compiled function.
Garbage collection.
REDUCT_API void reduct_atom_deinit(struct reduct *reduct, reduct_atom_t *atom)
Deinitialize an atom.
REDUCT_API void reduct_closure_deinit(reduct_closure_t *closure)
Deinitialize a closure structure.
Definition closure_impl.h:8
#define REDUCT_ERROR_INTERNAL(_reduct,...)
Throw an internal error using the jump buffer in the error structure.
Definition error.h:183
REDUCT_API void reduct_function_deinit(reduct_function_t *func)
Deinitialize a function structure.
REDUCT_API void reduct_gc_if_needed(reduct_t *reduct)
Optionally run the garbage collector if the number of allocated blocks exceeds the threshold.
Definition gc_impl.h:96
#define REDUCT_ITEM_TYPE_LIST
A list.
Definition item.h:28
REDUCT_API const char * reduct_item_type_str(reduct_item_type_t type)
Get the string representation of an Reduct item type.
Definition item_impl.h:139
#define REDUCT_ITEM_TYPE_ATOM
An atom.
Definition item.h:27
REDUCT_API reduct_float_t reduct_item_get_float(reduct_item_t *item)
Get the float value of an item if it is number shaped.
Definition item_impl.h:126
#define REDUCT_ITEM_TYPE_FUNCTION
A function.
Definition item.h:29
REDUCT_API reduct_int64_t reduct_item_get_int(reduct_item_t *item)
Get the integer value of an item if it is number shaped.
Definition item_impl.h:113
#define REDUCT_ITEM_FLAG_INT_SHAPED
Item is an integer shaped atom.
Definition item.h:39
reduct_uint8_t reduct_item_type_t
Item type enumeration.
Definition item.h:25
#define REDUCT_ITEM_FLAG_FLOAT_SHAPED
Item is a float shaped atom.
Definition item.h:40
#define REDUCT_ITEM_TYPE_NONE
No type.
Definition item.h:26
#define REDUCT_ITEM_TYPE_CLOSURE
A closure.
Definition item.h:30
#define REDUCT_ITEM_BLOCK_MAX
The maximum number of items in a block.
Definition item.h:80
#define REDUCT_ITEM_TYPE_LIST_NODE
A list node.
Definition item.h:31
Item management.
static void reduct_item_init(reduct_item_t *item)
Definition item_impl.h:10
REDUCT_API void reduct_item_free(reduct_t *reduct, reduct_item_t *item)
Definition item_impl.h:77
REDUCT_API reduct_item_t * reduct_item_new(reduct_t *reduct)
Definition item_impl.h:28
static reduct_item_t * reduct_item_pop_free_list(reduct_t *reduct)
Definition item_impl.h:19
reduct_int64_t integerValue
Pre-computed integer value, item must have REDUCT_ITEM_FLAG_INT_SHAPED.
Definition atom.h:55
reduct_float_t floatValue
Pre-computed float value, item must have REDUCT_ITEM_FLAG_FLOAT_SHAPED.
Definition atom.h:56
Item block structure.
Definition item.h:89
struct reduct_item_block * next
Definition item.h:90
reduct_item_t items[REDUCT_ITEM_BLOCK_MAX]
Definition item.h:92
Item structure.
Definition item.h:57
reduct_uint32_t position
The position in the input buffer where the item was parsed.
Definition item.h:59
reduct_closure_t closure
A closure.
Definition item.h:70
struct reduct_input * input
The parsed input that created this item.
Definition item.h:58
reduct_function_t function
A function.
Definition item.h:69
reduct_item_flags_t flags
Flags for the item.
Definition item.h:60
struct reduct_item * free
The next free item in the free list.
Definition item.h:71
reduct_atom_t atom
An atom.
Definition item.h:66
reduct_uint16_t retainCount
The reference count for GC retention.
Definition item.h:62
reduct_item_type_t type
The type of the item.
Definition item.h:61
State structure.
Definition core.h:61
reduct_item_block_t * block
Definition core.h:64
reduct_item_block_t firstBlock
Definition core.h:68
struct reduct_item * freeList
Definition core.h:65
reduct_size_t blocksAllocated
Definition core.h:62