Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
core_impl.h
Go to the documentation of this file.
1#include "error.h"
2#ifndef REDUCT_CORE_IMPL_H
3#define REDUCT_CORE_IMPL_H 1
4
5#include "atom.h"
6#include "core.h"
7#include "eval.h"
8#include "gc.h"
9#include "item.h"
10
12{
13 reduct_t* reduct = REDUCT_CALLOC(1, sizeof(reduct_t));
14 if (reduct == REDUCT_NULL)
15 {
16 REDUCT_ERROR_THROW(error, REDUCT_NULL, INTERNAL, "out of memory");
17 }
18 reduct->error = error;
19
21
27
30 reduct->trueItem->flags &= ~REDUCT_ITEM_FLAG_FALSY;
31
32 reduct_constant_register(reduct, "true", reduct->trueItem);
33 reduct_constant_register(reduct, "false", reduct->falseItem);
34 reduct_constant_register(reduct, "nil", reduct->nilItem);
35 reduct_constant_register(reduct, "pi", reduct->piItem);
36 reduct_constant_register(reduct, "e", reduct->eItem);
37
39
40 reduct->evalState = REDUCT_NULL;
41 reduct->argc = 0;
42 reduct->argv = REDUCT_NULL;
43
44 return reduct;
45}
46
48{
49 if (reduct == REDUCT_NULL)
50 {
51 return;
52 }
53
54 reduct_item_block_t* block = reduct->block;
55 while (block != REDUCT_NULL)
56 {
57 reduct_item_block_t* next = block->next;
58 for (int i = 0; i < REDUCT_ITEM_BLOCK_MAX; i++)
59 {
60 reduct_item_t* item = &block->items[i];
61 reduct_item_free(reduct, item);
62 }
63
64 if (block != &reduct->firstBlock)
65 {
66 REDUCT_FREE(block);
67 }
68 block = next;
69 }
70
71 reduct_input_t* input = reduct->input;
72 while (input != REDUCT_NULL)
73 {
74 reduct_input_t* next = input->prev;
75 if (input->flags & REDUCT_INPUT_FLAG_OWNED)
76 {
77 REDUCT_FREE((void*)input->buffer);
78 }
79 if (input != &reduct->firstInput)
80 {
81 REDUCT_FREE(input);
82 }
83 input = next;
84 }
85
86 if (reduct->evalState != REDUCT_NULL)
87 {
89 REDUCT_FREE(reduct->evalState);
90 }
91
92 REDUCT_FREE(reduct);
93}
94
95REDUCT_API void reduct_args_set(reduct_t* reduct, int argc, char** argv)
96{
97 reduct->argc = argc;
98 reduct->argv = argv;
99}
100
101REDUCT_API void reduct_constant_register(reduct_t* reduct, const char* name, reduct_item_t* item)
102{
103 REDUCT_ASSERT(reduct != REDUCT_NULL);
104 REDUCT_ASSERT(name != REDUCT_NULL);
105 REDUCT_ASSERT(item != REDUCT_NULL);
106
107 if (reduct->constantCount >= REDUCT_CONSTANTS_MAX)
108 {
109 REDUCT_ERROR_THROW(reduct->error, item, INTERNAL, "too many constants");
110 }
111
112 reduct_size_t len = REDUCT_STRLEN(name);
113 reduct_atom_t* atom = reduct_atom_lookup(reduct, name, len, REDUCT_ATOM_LOOKUP_NONE);
114
115 reduct->constants[reduct->constantCount].name = atom;
116 reduct->constants[reduct->constantCount].item = item;
117 reduct->constantCount++;
118
120 REDUCT_GC_RETAIN_ITEM(reduct, item);
121}
122
124 const char* path, reduct_input_flags_t flags)
125{
126 REDUCT_ASSERT(reduct != REDUCT_NULL);
127 REDUCT_ASSERT(buffer != REDUCT_NULL);
128 REDUCT_ASSERT(path != REDUCT_NULL);
129
130 reduct_input_t* input;
131 if (reduct->input == REDUCT_NULL)
132 {
133 input = &reduct->firstInput;
134 }
135 else
136 {
137 input = REDUCT_MALLOC(sizeof(reduct_input_t));
138 if (input == REDUCT_NULL)
139 {
140 REDUCT_ERROR_INTERNAL(reduct, "out of memory");
141 }
142 }
143 input->prev = reduct->input;
144 input->buffer = buffer;
145 input->end = buffer + length;
146 input->flags = flags;
147 REDUCT_STRNCPY(input->path, path, REDUCT_PATH_MAX - 1);
148 input->path[REDUCT_PATH_MAX - 1] = '\0';
149 reduct->input = input;
150 return input;
151}
152
153#endif
Atom representation and operations.
Core definitions and structures.
REDUCT_API void reduct_constant_register(reduct_t *reduct, const char *name, reduct_item_t *item)
Definition core_impl.h:101
#define REDUCT_MALLOC(_size)
Definition defs.h:27
#define REDUCT_PI
PI constant.
Definition defs.h:143
#define REDUCT_STRLEN(s)
Definition defs.h:38
size_t reduct_size_t
Definition defs.h:100
#define REDUCT_CONTAINER_OF(_ptr, _type, _member)
Container of macro.
Definition defs.h:184
#define REDUCT_E
E constant.
Definition defs.h:148
#define REDUCT_ASSERT(_cond)
Definition defs.h:25
#define REDUCT_STRNCPY(dest, src, n)
Definition defs.h:36
#define REDUCT_PATH_MAX
Maximum path length for Reduct.
Definition defs.h:173
#define REDUCT_FREE(_ptr)
Definition defs.h:30
#define REDUCT_API
Definition defs.h:7
#define REDUCT_CALLOC(_nmemb, _size)
Definition defs.h:28
#define REDUCT_NULL
Definition defs.h:23
Error handling and reporting.
Virtual machine evaluation.
Garbage collection.
REDUCT_API reduct_atom_t * reduct_atom_lookup(struct reduct *reduct, const char *str, reduct_size_t len, reduct_atom_lookup_flags_t flags)
Lookup an atom in the Reduct structure.
REDUCT_API reduct_atom_t * reduct_atom_lookup_int(struct reduct *reduct, reduct_int64_t value)
Lookup an atom by integer value.
REDUCT_API reduct_atom_t * reduct_atom_lookup_float(struct reduct *reduct, reduct_float_t value)
Lookup an atom by float value.
@ REDUCT_ATOM_LOOKUP_NONE
No flags.
Definition atom.h:39
REDUCT_API reduct_t * reduct_new(reduct_error_t *error)
Create a new Reduct structure.
Definition core_impl.h:11
#define REDUCT_CONSTANTS_MAX
Maximum amount of predefined constants.
Definition core.h:20
#define REDUCT_GC_THRESHOLD_INITIAL
Initial blocks allocated threshold for garbage collection.
Definition core.h:22
reduct_input_flags_t
Input flags.
Definition core.h:28
REDUCT_API reduct_input_t * reduct_input_new(reduct_t *reduct, const char *buffer, reduct_size_t length, const char *path, reduct_input_flags_t flags)
Create a new input structure and push it onto the input stack.
Definition core_impl.h:123
REDUCT_API void reduct_free(reduct_t *reduct)
Free the Reduct structure.
Definition core_impl.h:47
REDUCT_API void reduct_args_set(reduct_t *reduct, int argc, char **argv)
Set the command line arguments for the Reduct structure.
Definition core_impl.h:95
@ REDUCT_INPUT_FLAG_OWNED
The input buffer is owned by the input structure and should be freed.
Definition core.h:30
#define REDUCT_ERROR_THROW(_error, _item, _type,...)
Throw an error using the jump buffer in the error structure.
Definition error.h:125
#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_eval_state_deinit(reduct_eval_state_t *state)
Deinitialize an evaluation state structure.
Definition eval_impl.h:36
#define REDUCT_GC_RETAIN_ITEM(_reduct, _item)
Retain a item, preventing it from being collected by the GC.
Definition gc.h:56
REDUCT_API void reduct_intrinsic_register_all(struct reduct *reduct)
Registers all Reduct intrinsics with the given Reduct instance.
REDUCT_API void reduct_item_free(struct reduct *reduct, reduct_item_t *item)
Free an Reduct item.
#define REDUCT_ITEM_FLAG_FALSY
Item is falsy.
Definition item.h:38
#define REDUCT_ITEM_BLOCK_MAX
The maximum number of items in a block.
Definition item.h:80
Item management.
REDUCT_API reduct_list_t * reduct_list_new(struct reduct *reduct)
Create a new editable list.
Atom structure.
Definition atom.h:48
struct reduct_atom * name
Definition core.h:52
struct reduct_item * item
Definition core.h:53
Error structure.
Definition error.h:37
Input structure.
Definition core.h:38
char path[REDUCT_PATH_MAX]
Definition core.h:43
const char * end
Definition core.h:41
struct reduct_input * prev
Definition core.h:39
const char * buffer
Definition core.h:40
reduct_input_flags_t flags
Definition core.h:42
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_item_flags_t flags
Flags for the item.
Definition item.h:60
State structure.
Definition core.h:61
reduct_item_t * trueItem
Definition core.h:70
reduct_size_t gcThreshold
Definition core.h:63
reduct_error_t * error
Definition core.h:78
reduct_item_block_t * block
Definition core.h:64
struct reduct_eval_state * evalState
Definition core.h:79
reduct_constant_t constants[REDUCT_CONSTANTS_MAX]
Definition core.h:76
reduct_item_block_t firstBlock
Definition core.h:68
reduct_input_t * input
Definition core.h:66
reduct_input_t firstInput
Definition core.h:69
reduct_item_t * piItem
Definition core.h:73
reduct_item_t * nilItem
Definition core.h:72
char ** argv
Definition core.h:81
reduct_item_t * eItem
Definition core.h:74
reduct_uint32_t constantCount
Definition core.h:77
int argc
Definition core.h:80
reduct_item_t * falseItem
Definition core.h:71