lexLeo
Loading...
Searching...
No Matches
ast.h
1// src/core/ast/include/ast.h
2
3#ifndef AST_H
4#define AST_H
5
6#include <stddef.h>
7#include <stdbool.h>
8
9typedef enum {
10 AST_TYPE_BINDING,
11 AST_TYPE_READING,
12 AST_TYPE_WRITING,
13 AST_TYPE_TRANSLATION_UNIT,
14 AST_TYPE_BLOCK_ITEMS,
15 AST_TYPE_BLOCK,
16 AST_TYPE_PARAMETERS,
17 AST_TYPE_LIST_OF_PARAMETERS,
18 AST_TYPE_FUNCTION,
19 AST_TYPE_FUNCTION_DEFINITION,
20 AST_TYPE_ARGUMENTS,
21 AST_TYPE_LIST_OF_ARGUMENTS,
22 AST_TYPE_FUNCTION_CALL,
23 AST_TYPE_QUOTE,
24 AST_TYPE_EVAL,
25 AST_TYPE_NEGATION,
26 AST_TYPE_ADDITION,
27 AST_TYPE_SUBTRACTION,
28 AST_TYPE_MULTIPLICATION,
29 AST_TYPE_DIVISION,
30 AST_TYPE_SYMBOL,
31 AST_TYPE_SET,
32 AST_TYPE_CONDITIONAL_BLOCK,
33 AST_TYPE_WHILE_BLOCK,
34
35 // leaf type
36 AST_TYPE_DATA_WRAPPER,
37 AST_TYPE_ERROR,
38 AST_TYPE_NB_TYPES,
39} ast_type;
40
41#define AST_MAX_ARITY 16
42
43typedef enum {
44 TYPE_INT,
45 TYPE_STRING, // can be code snippet
46 TYPE_SYMBOL_NAME,
47 TYPE_SYMBOL, // can be unbound or bound to another symbol
48} data_type;
49
50typedef enum {
51 AST_ERROR_CODE_INT_NODE_CREATION_FAILED,
52 AST_ERROR_CODE_STRING_NODE_CREATION_FAILED,
53 AST_ERROR_CODE_SYMBOL_NAME_NODE_CREATION_FAILED,
54 AST_ERROR_CODE_BINDING_NODE_CREATION_FAILED,
55 AST_ERROR_CODE_READING_NODE_CREATION_FAILED,
56 AST_ERROR_CODE_WRITING_NODE_CREATION_FAILED,
57 AST_ERROR_CODE_TRANSLATION_UNIT_NODE_CREATION_FAILED,
58 AST_ERROR_CODE_TRANSLATION_UNIT_APPEND_FAILED,
59 AST_ERROR_CODE_BLOCK_ITEMS_NODE_CREATION_FAILED,
60 AST_ERROR_CODE_BLOCK_ITEMS_APPEND_FAILED,
61 AST_ERROR_CODE_BLOCK_NODE_CREATION_FAILED,
62 AST_ERROR_CODE_PARAMETERS_NODE_CREATION_FAILED,
63 AST_ERROR_CODE_PARAMETERS_APPEND_FAILED,
64 AST_ERROR_CODE_LIST_OF_PARAMETERS_NODE_CREATION_FAILED,
65 AST_ERROR_CODE_FUNCTION_NODE_CREATION_FAILED,
66 AST_ERROR_CODE_FUNCTION_DEFINITION_NODE_CREATION_FAILED,
67 AST_ERROR_CODE_ARGUMENTS_NODE_CREATION_FAILED,
68 AST_ERROR_CODE_ARGUMENTS_APPEND_FAILED,
69 AST_ERROR_CODE_LIST_OF_ARGUMENTS_NODE_CREATION_FAILED,
70 AST_ERROR_CODE_FUNCTION_CALL_NODE_CREATION_FAILED,
71 AST_ERROR_CODE_QUOTE_NODE_CREATION_FAILED,
72 AST_ERROR_CODE_EVAL_NODE_CREATION_FAILED,
73 AST_ERROR_CODE_NEGATION_NODE_CREATION_FAILED,
74 AST_ERROR_CODE_ADDITION_NODE_CREATION_FAILED,
75 AST_ERROR_CODE_SUBTRACTION_NODE_CREATION_FAILED,
76 AST_ERROR_CODE_MULTIPLICATION_NODE_CREATION_FAILED,
77 AST_ERROR_CODE_DIVISION_NODE_CREATION_FAILED,
78 AST_ERROR_CODE_SYMBOL_NODE_CREATION_FAILED,
79 AST_ERROR_CODE_SET_NODE_CREATION_FAILED,
80 AST_ERROR_CODE_CONDITIONAL_BLOCK_NODE_CREATION_FAILED,
81 AST_ERROR_CODE_WHILE_BLOCK_NODE_CREATION_FAILED,
82 // ...,
83 AST_UNRETRIEVABLE_ERROR_CODE,
84} ast_error_type;
85
86// forward declaration to handle cross-dependency
87typedef struct symbol symbol;
88
89typedef struct ast_children_t {
90 size_t children_nb;
91 size_t capacity;
92 struct ast **children;
94
95typedef struct {
96 data_type type;
97 union {
98 int int_value;
99 char *string_value;
100 symbol *symbol_value;
101 //function *function_value;
102 } data;
103} typed_data;
104
105#define MAXIMUM_ERROR_MESSAGE_LENGTH 255
106typedef struct {
107 ast_error_type code;
108 char *message;
109 bool is_sentinel; // true for the static fallback node
110 // YYLTYPE loc; // later?
111} error_info;
112
113typedef struct ast {
114 ast_type type;
115 union {
116 ast_children_t *children;
117 typed_data *data;
118 error_info *error;
119 };
120} ast;
121
122ast *ast_error_sentinel(void);
123
124typed_data *ast_create_typed_data_int(int i);
125
129void ast_destroy_typed_data_int(typed_data *typed_data_int);
130
131typed_data *ast_create_typed_data_string(const char *s);
132
136void ast_destroy_typed_data_string(typed_data *typed_data_string);
137
138typed_data *ast_create_typed_data_symbol_name(const char *s);
139
143void ast_destroy_typed_data_symbol_name(typed_data *typed_data_symbol_name);
144
145// borrowed symbol*: not freed by the caller nor by the AST
146// lifetime managed by the runtime_session
147typed_data *ast_create_typed_data_symbol(symbol *s);
148
153void ast_destroy_typed_data_symbol(typed_data *typed_data_symbol);
154
155// the caller must not free data
156ast *ast_create_typed_data_wrapper(typed_data *data);
157
162void ast_destroy_typed_data_wrapper(ast *ast_data_wrapper);
163
164ast *ast_create_int_node(int i);
165
166// Copies `str` internally (strdup). The AST owns the copy.
167// The caller remains responsible for the original pointer (if any).
168ast *ast_create_string_node(const char *str);
169
170// Copies `str` internally (strdup). The AST owns the copy.
171ast *ast_create_symbol_name_node(const char *str);
172
173// Borrowed symbol pointer (NOT owned by the AST).
174// lifetime of symbol is managed by runtime_session.
175ast *ast_create_symbol_node(symbol *sym);
176
177ast *ast_create_error_node(ast_error_type code, const char *message); // client code is responsible for message
178void ast_destroy_error_node(ast *ast_error_node); // client code is responsible for providing either NULL or a correctly formed ast of type AST_TYPE_ERROR
179ast *ast_create_error_node_or_sentinel(ast_error_type code, const char *message); // client code is responsible for message
180
181// caller is responsible for children_nb correctness
182ast_children_t *ast_create_ast_children_arr(size_t children_nb, ast **children);
183
184// client code is responsible for the argument number correctness
185ast_children_t *ast_create_ast_children_var(size_t children_nb,...);
186
187// client code is responsible for children_nb correctness
188void ast_destroy_ast_children(ast_children_t *ast_children);
189
190// return false on reallocation error or true otherwise ; client code is responsible for children_nb and capacity field values correctness
191bool ast_children_reserve(ast_children_t *ast_children, size_t capacity);
192
193// returns true on succees and false on error ; client code is responsible for passing NULL or well-formed asts with children_nb and capacity correctness
194bool ast_children_append_take(ast *parent, ast *child);
195
196// client code is responsible for providing a correctly formed ast_children
197ast *ast_create_children_node(ast_type type, ast_children_t *ast_children);
198
199// client code is responsible for children_nb and capacity values correctness
200ast *ast_create_children_node_arr(ast_type type, size_t children_nb, ast **children);
201
202// client code is responsible for children_nb and capacity values correctness
203ast *ast_create_children_node_var(ast_type type, size_t children_nb,...);
204
205void ast_destroy_children_node(ast *children_node);
206
207// the caller is responsible for passing either NULL or a well-formed ast pointer
208void ast_destroy(ast *root);
209
210bool ast_type_has_children(ast_type type);
211bool ast_can_have_children(ast *a);
212bool ast_has_any_child(ast *a);
213bool ast_is_data_of(const ast *a, data_type dt);
214
215char *ast_serialize(ast *root);
216ast *ast_deserialize(char *);
217
218// DEBUG TOOLS
219const char *ast_type_to_string(ast_type t);
220void ast_print(const ast *root);
221void ast_print_limited(const ast *root, int max_depth);
222
223#endif //AST_H
Definition ast.h:89
Definition ast.h:113
Definition ast.h:106
Definition interpreter.c:408
Definition ast.h:95