Kestrel Interface
Loading...
Searching...
No Matches
kest_eff_section.c
Go to the documentation of this file.
1#include "kest_int.h"
2
3#ifndef PRINTLINES_ALLOWED
4#define PRINTLINES_ALLOWED 1
5#endif
6
7static const char *FNAME = "kest_eff_section.c";
8
10{
11 if (!str)
12 return 0;
13
14 if (strcmp(str, "INFO") == 0) return 1;
15 if (strcmp(str, "RESOURCES") == 0) return 1;
16 if (strcmp(str, "PARAMETERS") == 0) return 1;
17 if (strcmp(str, "SETTINGS") == 0) return 1;
18 if (strcmp(str, "DEFS") == 0) return 1;
19 if (strcmp(str, "CODE") == 0) return 1;
20
21 return 0;
22}
23
24int get_section_start_score(char *str, int current_score)
25{
26 if (!str)
27 return 0;
28
29 if (token_is_char(str, '\n')) return 1;
30
31 switch (current_score)
32 {
33 case 0: return 0;
34 case 1: return (token_is_char(str, '.')) ? 2 : 0;
35 case 2: return (token_is_valid_section_name(str)) ? 3 : 0;
36 }
37
38 return 0;
39}
40
42{
43 if (!list || !sect | !ps)
44 return ERR_NULL_PTR;
45
47
48 if (!sec) return ERR_BAD_ARGS;
49
50 kest_dictionary *dict = sec->dict;
51 kest_parameter *param = NULL;
52
53 if (!dict)
54 return ERR_BAD_ARGS;
55
56 for (int i = 0; i < dict->n_entries; i++)
57 {
58 if (dict->entries[i].type == DICT_ENTRY_TYPE_SUBDICT)
59 {
60 param = kest_extract_parameter_from_dict(ps, sect, dict->entries[i].value.val_dict);
61
62 if (param)
63 kest_parameter_pll_safe_append(list, param);
64 }
65 }
66
67 return NO_ERROR;
68}
69
71{
72 if (!list || !sect | !ps)
73 return ERR_NULL_PTR;
74
76
77 if (!sec) return ERR_BAD_ARGS;
78
79 kest_dictionary *dict = sec->dict;
80 kest_setting *setting = NULL;
81
82 if (!dict)
83 return ERR_BAD_ARGS;
84
85 for (int i = 0; i < dict->n_entries; i++)
86 {
87 if (dict->entries[i].type == DICT_ENTRY_TYPE_SUBDICT)
88 {
89 setting = kest_extract_setting_from_dict(ps, sect, dict->entries[i].value.val_dict);
90
91 if (setting)
92 {
93 KEST_PRINTF("Obtained setting \"%s\"; adding to list...\n", setting->name_internal);
94 kest_setting_pll_safe_append(list, setting);
95 }
96 }
97 }
98
99 return NO_ERROR;
100}
101
102int kest_resources_section_extract(kest_eff_parsing_state *ps, kest_dsp_resource_pll **list, kest_ast_node *sect)
103{
104 if (!list || !sect || !ps)
105 return ERR_NULL_PTR;
106
108
109 if (!sec) return ERR_BAD_ARGS;
110
111 kest_dictionary *dict = sec->dict;
112 kest_dsp_resource *res= NULL;
113
114 if (!dict)
115 return ERR_BAD_ARGS;
116
117 for (int i = 0; i < dict->n_entries; i++)
118 {
119 if (dict->entries[i].type == DICT_ENTRY_TYPE_SUBDICT)
120 {
121 res = kest_extract_resource_from_dict(ps, sect, dict->entries[i].value.val_dict);
122
123 if (!res)
124 {
125 kest_parser_error_at_node(ps, sect, "Failed to interpret resource \"%s\"", dict->entries[i].name);
126 return ERR_BAD_ARGS;
127 }
128
129 if (res)
130 kest_dsp_resource_pll_safe_append(list, res);
131 }
132 }
133
134 return NO_ERROR;
135}
136
138{
139 KEST_PRINTF("kest_defs_section_extract(ps = %p, scope = %p, sect = %p)\n", ps, scope, sect);
140 if (!scope || !sect || !ps)
141 return ERR_NULL_PTR;
142
144
145 if (!sec) return ERR_BAD_ARGS;
146
147 kest_dictionary *dict = sec->dict;
149
150 if (!dict)
151 return ERR_BAD_ARGS;
152
153 KEST_PRINTF("dict exists and has %d entries.\n", dict->n_entries);
154
155 int ret_val = NO_ERROR;
156 for (int i = 0; i < dict->n_entries; i++)
157 {
158 KEST_PRINTF("Entry %d, name: \"%s\", type: %s\n", i, dict->entries[i].name, kest_dict_entry_type_to_string(dict->entries[i].type));
159 if (dict->entries[i].type == DICT_ENTRY_TYPE_EXPR)
160 {
161 KEST_PRINTF("Expression. Adding to defs...\n");
162 nexpr = kest_alloc(sizeof(kest_named_expression));
163
164 if (!nexpr)
165 return ERR_ALLOC_FAIL;
166
167 nexpr->name = kest_strndup(dict->entries[i].name, 64);
168 nexpr->expr = dict->entries[i].value.val_expr;
169
170 ret_val = kest_named_expression_pll_safe_append(&ps->def_exprs, nexpr);
171
172 if (ret_val != NO_ERROR)
173 {
174 KEST_PRINTF("Error adding to defs: %s\n", kest_error_code_to_string(ret_val));
175 return ret_val;
176 }
177
178 ret_val = kest_expr_scope_add_expr(ps->scope, dict->entries[i].name, dict->entries[i].value.val_expr);
179 }
180 else
181 {
182 kest_parser_error_at_line(ps, dict->entries[i].line, ".DEFS section entry DEFS.%s is not an expression\n", dict->entries[i].name);
183 }
184 }
185
186 return NO_ERROR;
187}
188
189int kest_dictionary_section_lookup_str(kest_ast_node *section, const char *name, const char **result)
190{
191 if (!section) return ERR_NULL_PTR;
192 if (section->type != KEST_AST_NODE_SECTION) return ERR_BAD_ARGS;
193
195
196 if (!sec || !sec->dict) return ERR_BAD_ARGS;
197
198 return kest_dictionary_lookup_str(sec->dict, name, result);
199}
200
201int kest_dictionary_section_lookup_float(kest_ast_node *section, const char *name, float *result)
202{
203 if (!section) return ERR_NULL_PTR;
204 if (section->type != KEST_AST_NODE_SECTION) return ERR_BAD_ARGS;
205
207
208 if (!sec || !sec->dict) return ERR_BAD_ARGS;
209
210 return kest_dictionary_lookup_float(sec->dict, name, result);
211}
212
213int kest_dictionary_section_lookup_int(kest_ast_node *section, const char *name, int *result)
214{
215 if (!section) return ERR_NULL_PTR;
216 if (section->type != KEST_AST_NODE_SECTION) return ERR_BAD_ARGS;
217
219
220 if (!sec || !sec->dict) return ERR_BAD_ARGS;
221
222 return kest_dictionary_lookup_int(sec->dict, name, result);
223}
224
226{
227 if (!section) return ERR_NULL_PTR;
228 if (section->type != KEST_AST_NODE_SECTION) return ERR_BAD_ARGS;
229
231
232 if (!sec || !sec->dict) return ERR_BAD_ARGS;
233
234 return kest_dictionary_lookup_expr(sec->dict, name, result);
235}
236
238{
239 if (!section) return ERR_NULL_PTR;
240 if (section->type != KEST_AST_NODE_SECTION) return ERR_BAD_ARGS;
241
243
244 if (!sec || !sec->dict) return ERR_BAD_ARGS;
245
246 return kest_dictionary_lookup_dict(sec->dict, name, result);
247}
248
250{
251 if (!ps || !section)
252 return ERR_NULL_PTR;
253
254 if (section->type != KEST_AST_NODE_SECTION)
255 return ERR_BAD_ARGS;
256
258
259 kest_token_ll *tokens = sec->tokens;
260
261 if (!tokens)
262 return ERR_BAD_ARGS;
263
264 ps->current_token = tokens->next;
265
266 int ret_val = kest_parse_dictionary(ps, &sec->dict, sec->name);
267
268 KEST_PRINTF("Parsed dictionary section. Result:\n");
269
270 print_dict(sec->dict);
271
272 return ret_val;
273}
274
275
277{
278 if (!ps || !section)
279 return ERR_NULL_PTR;
280
281 if (section->type != KEST_AST_NODE_SECTION)
282 return ERR_BAD_ARGS;
283
285 kest_token_ll *tokens = sec->tokens;
286
287 if (!tokens)
288 return ERR_BAD_ARGS;
289
290 ps->current_token = tokens->next;
291
292 int ret_val = kest_parse_asm(ps);
293
294 return ret_val;
295}
void * kest_alloc(size_t size)
Definition kest_alloc.c:11
char * kest_strndup(const char *str, size_t n)
Definition kest_alloc.c:73
int kest_parse_asm(kest_eff_parsing_state *ps)
kest_setting * kest_extract_setting_from_dict(kest_eff_parsing_state *ps, kest_ast_node *dict_node, kest_dictionary *dict)
kest_parameter * kest_extract_parameter_from_dict(kest_eff_parsing_state *ps, kest_ast_node *dict_node, kest_dictionary *dict)
kest_dsp_resource * kest_extract_resource_from_dict(kest_eff_parsing_state *ps, kest_ast_node *dict_node, kest_dictionary *dict)
int kest_dictionary_lookup_dict(kest_dictionary *dict, const char *name, kest_dictionary **result)
int kest_dictionary_lookup_int(kest_dictionary *dict, const char *name, int *result)
void print_dict(kest_dictionary *dict)
int kest_dictionary_lookup_str(kest_dictionary *dict, const char *name, const char **result)
const char * kest_dict_entry_type_to_string(int type)
int kest_dictionary_lookup_expr(kest_dictionary *dict, const char *name, kest_expression **result)
int kest_parse_dictionary(kest_eff_parsing_state *ps, kest_dictionary **result, const char *name)
int kest_dictionary_lookup_float(kest_dictionary *dict, const char *name, float *result)
#define DICT_ENTRY_TYPE_SUBDICT
#define DICT_ENTRY_TYPE_EXPR
void kest_parser_error_at_node(kest_eff_parsing_state *ps, kest_ast_node *node, const char *msg,...)
void kest_parser_error_at_line(kest_eff_parsing_state *ps, int line, const char *msg,...)
#define KEST_AST_NODE_SECTION
int kest_dictionary_section_lookup_int(kest_ast_node *section, const char *name, int *result)
int kest_parse_dictionary_section(kest_eff_parsing_state *ps, kest_ast_node *section)
int kest_resources_section_extract(kest_eff_parsing_state *ps, kest_dsp_resource_pll **list, kest_ast_node *sect)
int kest_parameters_section_extract(kest_eff_parsing_state *ps, kest_parameter_pll **list, kest_ast_node *sect)
int kest_dictionary_section_lookup_str(kest_ast_node *section, const char *name, const char **result)
int kest_parse_code_section(kest_eff_parsing_state *ps, kest_ast_node *section)
int token_is_valid_section_name(char *str)
int kest_settings_section_extract(kest_eff_parsing_state *ps, kest_setting_pll **list, kest_ast_node *sect)
int kest_dictionary_section_lookup_expr(kest_ast_node *section, const char *name, kest_expression **result)
int kest_dictionary_section_lookup_float(kest_ast_node *section, const char *name, float *result)
int kest_defs_section_extract(kest_eff_parsing_state *ps, kest_expr_scope *scope, struct kest_ast_node *sect)
int kest_dictionary_section_lookup_dict(kest_ast_node *section, const char *name, kest_dictionary **result)
int get_section_start_score(char *str, int current_score)
const char * kest_error_code_to_string(int error_code)
#define ERR_ALLOC_FAIL
#define ERR_BAD_ARGS
#define NO_ERROR
#define ERR_NULL_PTR
int kest_expr_scope_add_expr(kest_expr_scope *scope, const char *name, struct kest_expression *expr)
#define KEST_PRINTF(...)
Definition kest_printf.h:10
int token_is_char(char *str, char c)
union kest_dictionary_entry::@135165320273153367332213364063306072146165325153 value
int line
const char * name
struct kest_dictionary * val_dict
int type
kest_expression * val_expr
kest_dictionary_entry * entries
struct kest_dictionary * dict
kest_expr_scope * scope
kest_token_ll * current_token
kest_named_expression_pll * def_exprs
kest_expression * expr
const char * name_internal
struct kest_token_ll * next