8 #include "parser_proc.h"
14 typedef struct PSI_Token
{
21 typedef struct decl_type
{
24 struct decl_type
*real
;
27 static inline decl_type
*init_decl_type(token_t type
, char *name
) {
28 decl_type
*t
= malloc(sizeof(*t
));
30 t
->name
= strdup(name
);
34 static inline void free_decl_type(decl_type
*type
) {
39 typedef struct decl_typedef
{
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
);
51 static inline void free_decl_typedef(decl_typedef
*t
) {
53 free_decl_type(t
->type
);
57 typedef struct decl_typedefs
{
62 static decl_typedefs
*add_decl_typedef(decl_typedefs
*defs
, decl_typedef
*def
) {
64 defs
= calloc(1, sizeof(*defs
));
66 defs
->list
= realloc(defs
->list
, ++defs
->count
* sizeof(*defs
->list
));
67 defs
->list
[defs
->count
-1] = def
;
71 static void free_decl_typedefs(decl_typedefs
*defs
) {
74 for (i
= 0; i
< defs
->count
; ++i
) {
75 free_decl_typedef(defs
->list
[i
]);
81 typedef struct decl_var
{
83 unsigned pointer_level
;
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
;
93 static inline void free_decl_var(decl_var
*var
) {
98 typedef struct decl_arg
{
103 static inline decl_arg
*init_decl_arg(decl_type
*type
, decl_var
*var
) {
104 decl_arg
*arg
= malloc(sizeof(*arg
));
110 static inline void free_decl_arg(decl_arg
*arg
) {
111 free_decl_type(arg
->type
);
112 free_decl_var(arg
->var
);
116 typedef struct decl_vars
{
121 static inline decl_vars
*init_decl_vars(decl_var
*var
) {
122 decl_vars
*vars
= malloc(sizeof(*vars
));
124 vars
->vars
= malloc(sizeof(*vars
->vars
));
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
;
135 static inline void free_decl_vars(decl_vars
*vars
) {
138 for (i
= 0; i
< vars
->count
; ++i
) {
139 free_decl_var(vars
->vars
[i
]);
145 typedef struct decl_args
{
150 static inline decl_args
*init_decl_args(decl_arg
*arg
) {
151 decl_args
*args
= malloc(sizeof(*args
));
153 args
->args
= malloc(sizeof(*args
->args
));
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
;
164 static inline void free_decl_args(decl_args
*args
) {
167 for (i
= 0; i
< args
->count
; ++i
) {
168 free_decl_arg(args
->args
[i
]);
174 typedef struct decl_abi
{
178 static inline decl_abi
*init_decl_abi(char *convention
) {
179 decl_abi
*abi
= malloc(sizeof(*abi
));
180 abi
->convention
= strdup(convention
);
184 static inline void free_decl_abi(decl_abi
*abi
) {
185 free(abi
->convention
);
189 typedef struct decl
{
196 static inline decl
* init_decl(decl_abi
*abi
, decl_arg
*func
, decl_args
*args
) {
197 decl
*d
= malloc(sizeof(*d
));
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
);
211 typedef struct decls
{
216 static inline decls
*add_decl(decls
*decls
, decl
*decl
) {
218 decls
= calloc(1, sizeof(*decls
));
220 decls
->list
= realloc(decls
->list
, ++decls
->count
* sizeof(*decls
->list
));
221 decls
->list
[decls
->count
-1] = decl
;
225 static inline void free_decls(decls
*decls
) {
228 for (i
= 0; i
< decls
->count
; ++i
) {
229 free_decl(decls
->list
[i
]);
235 typedef struct impl_type
{
240 static inline impl_type
*init_impl_type(token_t type
, char *name
) {
241 impl_type
*t
= malloc(sizeof(*t
));
244 t
->name
= (char *) strdup((const char *) name
);
248 static inline void free_impl_type(impl_type
*type
) {
253 typedef struct impl_var
{
255 unsigned reference
:1;
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
;
265 static inline void free_impl_var(impl_var
*var
) {
270 typedef struct impl_def_val
{
279 static inline impl_def_val
*init_impl_def_val() {
280 impl_def_val
*def
= malloc(sizeof(*def
));
286 static inline void free_impl_def_val(impl_def_val
*def
) {
290 typedef struct impl_arg
{
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
));
304 static inline void free_impl_arg(impl_arg
*arg
) {
305 free_impl_type(arg
->type
);
306 free_impl_var(arg
->var
);
308 free_impl_def_val(arg
->def
);
313 typedef struct impl_args
{
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
));
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
;
337 static inline void free_impl_args(impl_args
*args
) {
340 for (i
= 0; i
< args
->count
; ++i
) {
341 free_impl_arg(args
->args
[i
]);
347 typedef struct impl_func
{
350 impl_type
*return_type
;
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
;
361 static inline void free_impl_func(impl_func
*f
) {
362 free_impl_type(f
->return_type
);
363 free_impl_args(f
->args
);
368 typedef struct let_func
{
373 static inline let_func
*init_let_func(token_t type
, char *name
) {
374 let_func
*func
= malloc(sizeof(*func
));
376 func
->name
= (char *) strdup((const char *) name
);
380 static inline void free_let_func(let_func
*func
) {
385 typedef struct let_value
{
388 unsigned null_pointer_ref
:1;
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
;
399 static inline void free_let_value(let_value
*val
) {
401 free_let_func(val
->func
);
404 free_impl_var(val
->var
);
409 typedef struct let_stmt
{
414 static inline let_stmt
*init_let_stmt(decl_var
*var
, let_value
*val
) {
415 let_stmt
*let
= malloc(sizeof(*let
));
421 static inline void free_let_stmt(let_stmt
*stmt
) {
422 free_decl_var(stmt
->var
);
423 free_let_value(stmt
->val
);
427 typedef struct set_func
{
432 static inline set_func
*init_set_func(token_t type
, char *name
) {
433 set_func
*func
= malloc(sizeof(*func
));
435 func
->name
= (char *) strdup((const char *) name
);
439 static inline void free_set_func(set_func
*func
) {
444 typedef struct set_value
{
449 static inline set_value
*init_set_value(set_func
*func
, decl_vars
*vars
) {
450 set_value
*val
= malloc(sizeof(*val
));
456 static inline void free_set_value(set_value
*val
) {
457 free_set_func(val
->func
);
458 free_decl_vars(val
->vars
);
462 typedef struct set_stmt
{
467 static inline set_stmt
*init_set_stmt(impl_var
*var
, set_value
*val
) {
468 set_stmt
*set
= malloc(sizeof(*set
));
474 static inline void free_set_stmt(set_stmt
*set
) {
475 free_impl_var(set
->var
);
476 free_set_value(set
->val
);
480 typedef struct ret_stmt
{
485 static inline ret_stmt
*init_ret_stmt(set_func
*func
, decl_var
*decl
) {
486 ret_stmt
*ret
= malloc(sizeof(*ret
));
492 static inline void free_ret_stmt(ret_stmt
*ret
) {
493 free_set_func(ret
->func
);
494 free_decl_var(ret
->decl
);
498 typedef struct impl_stmt
{
508 static inline impl_stmt
*init_impl_stmt(token_t type
, void *ptr
) {
509 impl_stmt
*stmt
= malloc(sizeof(*stmt
));
515 static inline void free_impl_stmt(impl_stmt
*stmt
) {
516 switch (stmt
->type
) {
518 free_let_stmt(stmt
->s
.let
);
521 free_set_stmt(stmt
->s
.set
);
524 free_ret_stmt(stmt
->s
.ret
);
530 typedef struct impl_stmts
{
535 static inline impl_stmts
*init_impl_stmts(impl_stmt
*stmt
) {
536 impl_stmts
*stmts
= malloc(sizeof(*stmts
));
538 stmts
->stmts
= malloc(sizeof(*stmts
->stmts
));
539 stmts
->stmts
[0] = stmt
;
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
;
549 static inline void free_impl_stmts(impl_stmts
*stmts
) {
552 for (i
= 0; i
< stmts
->count
; ++i
) {
553 free_impl_stmt(stmts
->stmts
[i
]);
559 typedef struct impl
{
564 static inline impl
*init_impl(impl_func
*func
, impl_stmts
*stmts
) {
565 impl
*i
= malloc(sizeof(*i
));
571 static inline void free_impl(impl
*impl
) {
572 free_impl_func(impl
->func
);
573 free_impl_stmts(impl
->stmts
);
577 typedef struct impls
{
582 static impls
*add_impl(impls
*impls
, impl
*impl
) {
584 impls
= calloc(1, sizeof(*impls
));
586 impls
->list
= realloc(impls
->list
, ++impls
->count
* sizeof(*impls
->list
));
587 impls
->list
[impls
->count
-1] = impl
;
591 static void free_impls(impls
*impls
) {
594 for (i
= 0; i
< impls
->count
; ++i
) {
595 free_impl(impls
->list
[i
]);
601 typedef struct PSI_Data
{
609 static inline void PSI_DataExchange(PSI_Data
*dest
, PSI_Data
*src
) {
610 memcpy(dest
, src
, sizeof(*dest
));
611 memset(src
, 0, sizeof(*src
));
614 static inline void PSI_DataDtor(PSI_Data
*data
) {
616 free_decl_typedefs(data
->defs
);
619 free_decls(data
->decls
);
622 free_impls(data
->impls
);
632 typedef struct PSI_Parser
{
642 char *cur
, *tok
, *lim
, *eof
, *ctx
, *mrk
, buf
[BSIZE
];
645 static inline PSI_Token
*PSI_TokenAlloc(PSI_Parser
*P
) {
649 if (P
->cur
<= P
->tok
) {
653 token_len
= P
->cur
- P
->tok
;
655 T
= malloc(sizeof(*T
) + token_len
);
659 T
->text
[token_len
] = 0;
660 memcpy(T
->text
, P
->tok
, token_len
);
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
);