flush
[m6w6/ext-psi] / idl / parser.h
1 #ifndef _PSI_PARSER_H
2 #define _PSI_PARSER_H
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "parser_proc.h"
9
10 #define BSIZE 256
11
12 typedef int token_t;
13
14 typedef struct PSI_Token {
15 token_t type;
16 unsigned line;
17 size_t size;
18 char text[1];
19 } PSI_Token;
20
21 typedef struct decl_type {
22 char *name;
23 token_t type;
24 struct decl_type *real;
25 } decl_type;
26
27 static inline decl_type *init_decl_type(token_t type, char *name) {
28 decl_type *t = malloc(sizeof(*t));
29 t->type = type;
30 t->name = strdup(name);
31 return t;
32 }
33
34 static inline void free_decl_type(decl_type *type) {
35 free(type->name);
36 free(type);
37 }
38
39 typedef struct decl_typedef {
40 char *alias;
41 decl_type *type;
42 } decl_typedef;
43
44 static inline decl_typedef *init_decl_typedef(char *name, decl_type *type) {
45 decl_typedef *t = malloc(sizeof(*t));
46 t->alias = strdup(name);
47 t->type = type;
48 return t;
49 }
50
51 static inline void free_decl_typedef(decl_typedef *t) {
52 free(t->alias);
53 free_decl_type(t->type);
54 free(t);
55 }
56
57 typedef struct decl_typedefs {
58 size_t count;
59 decl_typedef **list;
60 } decl_typedefs;
61
62 static decl_typedefs *add_decl_typedef(decl_typedefs *defs, decl_typedef *def) {
63 if (!defs) {
64 defs = calloc(1, sizeof(*defs));
65 }
66 defs->list = realloc(defs->list, ++defs->count * sizeof(*defs->list));
67 defs->list[defs->count-1] = def;
68 return defs;
69 }
70
71 static void free_decl_typedefs(decl_typedefs *defs) {
72 size_t i;
73
74 for (i = 0; i < defs->count; ++i) {
75 free_decl_typedef(defs->list[i]);
76 }
77 free(defs->list);
78 free(defs);
79 }
80
81 typedef struct decl_var {
82 char *name;
83 unsigned pointer_level;
84 } decl_var;
85
86 static inline decl_var *init_decl_var(char *name, unsigned pl) {
87 decl_var *v = malloc(sizeof(*v));
88 v->name = (char *) strdup((const char *) name);
89 v->pointer_level = pl;
90 return v;
91 }
92
93 static inline void free_decl_var(decl_var *var) {
94 free(var->name);
95 free(var);
96 }
97
98 typedef struct decl_arg {
99 decl_type *type;
100 decl_var *var;
101 } decl_arg;
102
103 static inline decl_arg *init_decl_arg(decl_type *type, decl_var *var) {
104 decl_arg *arg = malloc(sizeof(*arg));
105 arg->type = type;
106 arg->var = var;
107 return arg;
108 }
109
110 static inline void free_decl_arg(decl_arg *arg) {
111 free_decl_type(arg->type);
112 free_decl_var(arg->var);
113 free(arg);
114 }
115
116 typedef struct decl_vars {
117 decl_var **vars;
118 size_t count;
119 } decl_vars;
120
121 static inline decl_vars *init_decl_vars(decl_var *var) {
122 decl_vars *vars = malloc(sizeof(*vars));
123 vars->count = 1;
124 vars->vars = malloc(sizeof(*vars->vars));
125 vars->vars[0] = var;
126 return vars;
127 }
128
129 static inline decl_vars *add_decl_var(decl_vars *vars, decl_var *var) {
130 vars->vars = realloc(vars->vars, ++vars->count * sizeof(*vars->vars));
131 vars->vars[vars->count-1] = var;
132 return vars;
133 }
134
135 static inline void free_decl_vars(decl_vars *vars) {
136 size_t i;
137
138 for (i = 0; i < vars->count; ++i) {
139 free_decl_var(vars->vars[i]);
140 }
141 free(vars->vars);
142 free(vars);
143 }
144
145 typedef struct decl_args {
146 decl_arg **args;
147 size_t count;
148 } decl_args;
149
150 static inline decl_args *init_decl_args(decl_arg *arg) {
151 decl_args *args = malloc(sizeof(*args));
152 args->count = 1;
153 args->args = malloc(sizeof(*args->args));
154 args->args[0] = arg;
155 return args;
156 }
157
158 static inline decl_args *add_decl_arg(decl_args *args, decl_arg *arg) {
159 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
160 args->args[args->count-1] = arg;
161 return args;
162 }
163
164 static inline void free_decl_args(decl_args *args) {
165 size_t i;
166
167 for (i = 0; i < args->count; ++i) {
168 free_decl_arg(args->args[i]);
169 }
170 free(args->args);
171 free(args);
172 }
173
174 typedef struct decl_abi {
175 char *convention;
176 } decl_abi;
177
178 static inline decl_abi *init_decl_abi(char *convention) {
179 decl_abi *abi = malloc(sizeof(*abi));
180 abi->convention = strdup(convention);
181 return abi;
182 }
183
184 static inline void free_decl_abi(decl_abi *abi) {
185 free(abi->convention);
186 free(abi);
187 }
188
189 typedef struct decl {
190 decl_abi *abi;
191 decl_arg *func;
192 decl_args *args;
193 void *dlptr;
194 } decl;
195
196 static inline decl* init_decl(decl_abi *abi, decl_arg *func, decl_args *args) {
197 decl *d = malloc(sizeof(*d));
198 d->abi = abi;
199 d->func = func;
200 d->args = args;
201 return d;
202 }
203
204 static inline void free_decl(decl *d) {
205 free_decl_abi(d->abi);
206 free_decl_arg(d->func);
207 free_decl_args(d->args);
208 free(d);
209 }
210
211 typedef struct decls {
212 size_t count;
213 decl **list;
214 } decls;
215
216 static inline decls *add_decl(decls *decls, decl *decl) {
217 if (!decls) {
218 decls = calloc(1, sizeof(*decls));
219 }
220 decls->list = realloc(decls->list, ++decls->count * sizeof(*decls->list));
221 decls->list[decls->count-1] = decl;
222 return decls;
223 }
224
225 static inline void free_decls(decls *decls) {
226 size_t i;
227
228 for (i = 0; i < decls->count; ++i) {
229 free_decl(decls->list[i]);
230 }
231 free(decls->list);
232 free(decls);
233 }
234
235 typedef struct impl_type {
236 char *name;
237 token_t type;
238 } impl_type;
239
240 static inline impl_type *init_impl_type(token_t type, char *name) {
241 impl_type *t = malloc(sizeof(*t));
242
243 t->type = type;
244 t->name = (char *) strdup((const char *) name);
245 return t;
246 }
247
248 static inline void free_impl_type(impl_type *type) {
249 free(type->name);
250 free(type);
251 }
252
253 typedef struct impl_var {
254 char *name;
255 unsigned reference:1;
256 } impl_var;
257
258 static inline impl_var *init_impl_var(char *name, int is_reference) {
259 impl_var *var = malloc(sizeof(*var));
260 var->name = (char *) strdup((const char *) name);
261 var->reference = is_reference;
262 return var;
263 }
264
265 static inline void free_impl_var(impl_var *var) {
266 free(var->name);
267 free(var);
268 }
269
270 typedef struct impl_def_val {
271 token_t type;
272 union {
273 int64_t digits;
274 double decimals;
275 } v;
276 unsigned is_null:1;
277 } impl_def_val;
278
279 static inline impl_def_val *init_impl_def_val() {
280 impl_def_val *def = malloc(sizeof(*def));
281 def->type = 0;
282 def->is_null = 1;
283 return def;
284 }
285
286 static inline void free_impl_def_val(impl_def_val *def) {
287 free(def);
288 }
289
290 typedef struct impl_arg {
291 impl_type *type;
292 impl_var *var;
293 impl_def_val *def;
294 } impl_arg;
295
296 static inline impl_arg *init_impl_arg(impl_type *type, impl_var *var, impl_def_val *def) {
297 impl_arg *arg = malloc(sizeof(*arg));
298 arg->type = type;
299 arg->var = var;
300 arg->def = def;
301 return arg;
302 }
303
304 static inline void free_impl_arg(impl_arg *arg) {
305 free_impl_type(arg->type);
306 free_impl_var(arg->var);
307 if (arg->def) {
308 free_impl_def_val(arg->def);
309 }
310 free(arg);
311 }
312
313 typedef struct impl_args {
314 impl_arg **args;
315 size_t count;
316 } impl_args;
317
318 static inline impl_args *init_impl_args(impl_arg *arg) {
319 impl_args *args = malloc(sizeof(*args));
320 args->args = malloc(sizeof(*args->args));
321 if (arg) {
322 args->count = 1;
323 args->args[0] = arg;
324 } else {
325 args->count = 0;
326 args->args = NULL;
327 }
328 return args;
329 }
330
331 static inline impl_args *add_impl_arg(impl_args *args, impl_arg *arg) {
332 args->args = realloc(args->args, ++args->count * sizeof(*args->args));
333 args->args[args->count-1] = arg;
334 return args;
335 }
336
337 static inline void free_impl_args(impl_args *args) {
338 size_t i;
339
340 for (i = 0; i < args->count; ++i) {
341 free_impl_arg(args->args[i]);
342 }
343 free(args->args);
344 free(args);
345 }
346
347 typedef struct impl_func {
348 char *name;
349 impl_args *args;
350 impl_type *return_type;
351 } impl_func;
352
353 static inline impl_func *init_impl_func(char *name, impl_args *args, impl_type *type) {
354 impl_func *func = malloc(sizeof(*func));
355 func->name = strdup(name);
356 func->args = args ? args : init_impl_args(NULL);
357 func->return_type = type;
358 return func;
359 }
360
361 static inline void free_impl_func(impl_func *f) {
362 free_impl_type(f->return_type);
363 free_impl_args(f->args);
364 free(f->name);
365 free(f);
366 }
367
368 typedef struct let_func {
369 token_t type;
370 char *name;
371 } let_func;
372
373 static inline let_func *init_let_func(token_t type, char *name) {
374 let_func *func = malloc(sizeof(*func));
375 func->type = type;
376 func->name = (char *) strdup((const char *) name);
377 return func;
378 }
379
380 static inline void free_let_func(let_func *func) {
381 free(func->name);
382 free(func);
383 }
384
385 typedef struct let_value {
386 let_func *func;
387 impl_var *var;
388 unsigned null_pointer_ref:1;
389 } let_value;
390
391 static inline let_value *init_let_value(let_func *func, impl_var *var, int null_pointer_ref) {
392 let_value *val = malloc(sizeof(*val));
393 val->null_pointer_ref = null_pointer_ref;
394 val->func = func;
395 val->var = var;
396 return val;
397 }
398
399 static inline void free_let_value(let_value *val) {
400 if (val->func) {
401 free_let_func(val->func);
402 }
403 if (val->var) {
404 free_impl_var(val->var);
405 }
406 free(val);
407 }
408
409 typedef struct let_stmt {
410 decl_var *var;
411 let_value *val;
412 } let_stmt;
413
414 static inline let_stmt *init_let_stmt(decl_var *var, let_value *val) {
415 let_stmt *let = malloc(sizeof(*let));
416 let->var = var;
417 let->val = val;
418 return let;
419 }
420
421 static inline void free_let_stmt(let_stmt *stmt) {
422 free_decl_var(stmt->var);
423 free_let_value(stmt->val);
424 free(stmt);
425 }
426
427 typedef struct set_func {
428 token_t type;
429 char *name;
430 } set_func;
431
432 static inline set_func *init_set_func(token_t type, char *name) {
433 set_func *func = malloc(sizeof(*func));
434 func->type = type;
435 func->name = (char *) strdup((const char *) name);
436 return func;
437 }
438
439 static inline void free_set_func(set_func *func) {
440 free(func->name);
441 free(func);
442 }
443
444 typedef struct set_value {
445 set_func *func;
446 decl_vars *vars;
447 } set_value;
448
449 static inline set_value *init_set_value(set_func *func, decl_vars *vars) {
450 set_value *val = malloc(sizeof(*val));
451 val->func = func;
452 val->vars = vars;
453 return val;
454 }
455
456 static inline void free_set_value(set_value *val) {
457 free_set_func(val->func);
458 free_decl_vars(val->vars);
459 free(val);
460 }
461
462 typedef struct set_stmt {
463 impl_var *var;
464 set_value *val;
465 } set_stmt;
466
467 static inline set_stmt *init_set_stmt(impl_var *var, set_value *val) {
468 set_stmt *set = malloc(sizeof(*set));
469 set->var = var;
470 set->val = val;
471 return set;
472 }
473
474 static inline void free_set_stmt(set_stmt *set) {
475 free_impl_var(set->var);
476 free_set_value(set->val);
477 free(set);
478 }
479
480 typedef struct ret_stmt {
481 set_func *func;
482 decl_var *decl;
483 } ret_stmt;
484
485 static inline ret_stmt *init_ret_stmt(set_func *func, decl_var *decl) {
486 ret_stmt *ret = malloc(sizeof(*ret));
487 ret->func = func;
488 ret->decl = decl;
489 return ret;
490 }
491
492 static inline void free_ret_stmt(ret_stmt *ret) {
493 free_set_func(ret->func);
494 free_decl_var(ret->decl);
495 free(ret);
496 }
497
498 typedef struct impl_stmt {
499 token_t type;
500 union {
501 let_stmt *let;
502 set_stmt *set;
503 ret_stmt *ret;
504 void *ptr;
505 } s;
506 } impl_stmt;
507
508 static inline impl_stmt *init_impl_stmt(token_t type, void *ptr) {
509 impl_stmt *stmt = malloc(sizeof(*stmt));
510 stmt->type = type;
511 stmt->s.ptr = ptr;
512 return stmt;
513 }
514
515 static inline void free_impl_stmt(impl_stmt *stmt) {
516 switch (stmt->type) {
517 case PSI_T_LET:
518 free_let_stmt(stmt->s.let);
519 break;
520 case PSI_T_SET:
521 free_set_stmt(stmt->s.set);
522 break;
523 case PSI_T_RET:
524 free_ret_stmt(stmt->s.ret);
525 break;
526 }
527 free(stmt);
528 }
529
530 typedef struct impl_stmts {
531 impl_stmt **stmts;
532 size_t count;
533 } impl_stmts;
534
535 static inline impl_stmts *init_impl_stmts(impl_stmt *stmt) {
536 impl_stmts *stmts = malloc(sizeof(*stmts));
537 stmts->count = 1;
538 stmts->stmts = malloc(sizeof(*stmts->stmts));
539 stmts->stmts[0] = stmt;
540 return stmts;
541 }
542
543 static inline impl_stmts *add_impl_stmt(impl_stmts *stmts, impl_stmt *stmt) {
544 stmts->stmts = realloc(stmts->stmts, ++stmts->count * sizeof(*stmts->stmts));
545 stmts->stmts[stmts->count-1] = stmt;
546 return stmts;
547 }
548
549 static inline void free_impl_stmts(impl_stmts *stmts) {
550 size_t i;
551
552 for (i = 0; i < stmts->count; ++i) {
553 free_impl_stmt(stmts->stmts[i]);
554 }
555 free(stmts->stmts);
556 free(stmts);
557 }
558
559 typedef struct impl {
560 impl_func *func;
561 impl_stmts *stmts;
562 } impl;
563
564 static inline impl *init_impl(impl_func *func, impl_stmts *stmts) {
565 impl *i = malloc(sizeof(*i));
566 i->func = func;
567 i->stmts = stmts;
568 return i;
569 }
570
571 static inline void free_impl(impl *impl) {
572 free_impl_func(impl->func);
573 free_impl_stmts(impl->stmts);
574 free(impl);
575 }
576
577 typedef struct impls {
578 size_t count;
579 impl **list;
580 } impls;
581
582 static impls *add_impl(impls *impls, impl *impl) {
583 if (!impls) {
584 impls = calloc(1, sizeof(*impls));
585 }
586 impls->list = realloc(impls->list, ++impls->count * sizeof(*impls->list));
587 impls->list[impls->count-1] = impl;
588 return impls;
589 }
590
591 static void free_impls(impls *impls) {
592 size_t i;
593
594 for (i = 0; i < impls->count; ++i) {
595 free_impl(impls->list[i]);
596 }
597 free(impls->list);
598 free(impls);
599 }
600
601 typedef struct PSI_Data {
602 decl_typedefs *defs;
603 decls *decls;
604 impls *impls;
605 char *lib;
606 char *fn;
607 } PSI_Data;
608
609 static inline void PSI_DataExchange(PSI_Data *dest, PSI_Data *src) {
610 memcpy(dest, src, sizeof(*dest));
611 memset(src, 0, sizeof(*src));
612 }
613
614 static inline void PSI_DataDtor(PSI_Data *data) {
615 if (data->defs) {
616 free_decl_typedefs(data->defs);
617 }
618 if (data->decls) {
619 free_decls(data->decls);
620 }
621 if (data->impls) {
622 free_impls(data->impls);
623 }
624 if (data->lib) {
625 free(data->lib);
626 }
627 if (data->fn) {
628 free(data->fn);
629 }
630 }
631
632 typedef struct PSI_Parser {
633 decl_typedefs *defs;
634 decls *decls;
635 impls *impls;
636 char *lib;
637 char *fn;
638 FILE *fp;
639 void *proc;
640 size_t line;
641 token_t num;
642 char *cur, *tok, *lim, *eof, *ctx, *mrk, buf[BSIZE];
643 } PSI_Parser;
644
645 static inline PSI_Token *PSI_TokenAlloc(PSI_Parser *P) {
646 PSI_Token *T;
647 size_t token_len;
648
649 if (P->cur <= P->tok) {
650 return NULL;
651 }
652
653 token_len = P->cur - P->tok;
654
655 T = malloc(sizeof(*T) + token_len);
656 T->type = P->num;
657 T->line = P->line;
658 T->size = token_len;
659 T->text[token_len] = 0;
660 memcpy(T->text, P->tok, token_len);
661
662 return T;
663 }
664
665 PSI_Parser *PSI_ParserInit(PSI_Parser *P, const char *filename);
666 size_t PSI_ParserFill(PSI_Parser *P, size_t n);
667 token_t PSI_ParserScan(PSI_Parser *P);
668 void PSI_ParserParse(PSI_Parser *P, PSI_Token *T);
669 void PSI_ParserDtor(PSI_Parser *P);
670 void PSI_ParserFree(PSI_Parser **P);
671
672 #endif