From db1b9c8dcb0d115217a33c2fe8e0760d49143e11 Mon Sep 17 00:00:00 2001 From: kartofen Date: Sat, 13 Sep 2025 15:24:28 +0300 Subject: ast nearly build and proper errors --- demos/sample-files/gram-skeleton.c | 62 +++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 11 deletions(-) (limited to 'demos/sample-files/gram-skeleton.c') diff --git a/demos/sample-files/gram-skeleton.c b/demos/sample-files/gram-skeleton.c index 4e40c14..9898c6b 100644 --- a/demos/sample-files/gram-skeleton.c +++ b/demos/sample-files/gram-skeleton.c @@ -24,10 +24,10 @@ static void *xalloc(size_t sz) { #include "parts/precedence.h" #include "util/list.h" -static inline struct list_head *list_new_head(struct list_head *head, struct list_head *new) +static inline struct list_head *list_new_head(struct list_head *list, struct list_head *newhead) { - if(head) list_add(new, head); - return new; + newhead->next = list; + return newhead; } struct ptr_entry { @@ -79,7 +79,7 @@ struct list_head *prod_new(char *iden, struct list_head *actionlist) entry->ptrlist = actionlist; }) -void handle_type(struct list_head *terminals, struct list_head *nonterminals) +void handle_enum(struct list_head *terminals, struct list_head *nonterminals) { printf("#include \"parts/symbol.h\"\n"); printf("enum symbol { "); @@ -107,6 +107,12 @@ void handle_type(struct list_head *terminals, struct list_head *nonterminals) } +static inline char *substring(char *str, size_t sub_end); +void handle_stype(char *action) +{ + printf("char *stack_item_type = \"%s\";\n", substring(action+1, strlen(action)-2)); +} + void handle_prec(struct list_head *preclist) { printf("#include \"parts/precedence.h\"\n"); @@ -142,8 +148,7 @@ void handle_prod(struct list_head *prodlist) printf("struct production *grammar = (struct production[]){\n"); list_for_each_entry(struct strnptr_entry, e1, list, prodlist) { if(productions == 0) - list_add(list_get_tail(list_entry(e1->ptrlist, struct strnptr_entry, list)->ptrlist), - ptr_new("END_INPUT")); + list_get_tail(list_entry(e1->ptrlist, struct strnptr_entry, list)->ptrlist)->next = ptr_new("END_INPUT"); productions += list_len(e1->ptrlist); list_for_each_entry(struct strnptr_entry, e2, list, e1->ptrlist) { @@ -171,7 +176,8 @@ void handle_prod(struct list_head *prodlist) #define action_new(idenlist, action) (intptr_t)action_new((struct list_head *)idenlist, (char *)action) #define prod_new(iden, actionlist) (intptr_t)prod_new((char *)iden, (struct list_head *)actionlist) -#define handle_type(terminals, nonterminals) handle_type((struct list_head *)terminals, (struct list_head *)nonterminals); +#define handle_enum(terminals, nonterminals) handle_enum((struct list_head *)terminals, (struct list_head *)nonterminals); +#define handle_stype(action) handle_stype((char *)action); #define handle_prec(preclist) handle_prec((struct list_head *)preclist); #define handle_prod(prodlist) handle_prod((struct list_head *)prodlist); @@ -188,7 +194,7 @@ struct token { static char *next_token(char *str); symbol token_sym(struct token *t) { return t->s; } -intptr_t token_val(struct token *t) { return t->v; } +intptr_t token_val(struct token *t) { return (intptr_t)&t->v; } static char *input; @@ -234,10 +240,43 @@ static inline char *_strdup(char *str) return memcpy(xalloc(strlen(str) + 1), str, strlen(str)+1); } +static inline size_t tillch(char *str, size_t len, char ch) +{ + for(size_t i = 0; i < len; i++) if(str[i] == ch) return i; + return len; +} + +static inline char *escapeNL(char *str) +{ +// static char new[256]; +// char *new_ptr = new; + +// #define addch(ch) if(new_ptr - new > sizeof(new) - 1) { \ +// fprintf(stderr, "ERROR: escapeNL cap reached"); return NULL; \ +// } else *new_ptr++ = ch; + +// for(size_t i = 0; i < strlen(str); i++) +// if(str[i] == '\n') { +// addch('\\'); addch('n'); +// continue; +// } else addch(str[i]); + +// *new_ptr = '\0'; +// return new; + + // temp + for(size_t i = 0; i < strlen(str); i++) if(str[i] == '\n') str[i] = ' '; + return str; +} + static inline char *substring(char *str, size_t sub_end) { - static char sub[128]; - if(sub_end+1 > sizeof(sub)) return NULL; + // static char sub[256]; + static char sub[512]; + if(sub_end+1 > sizeof(sub)) { + fprintf(stderr, "ERROR: substring cap reached"); + return NULL; + } sub[sub_end] = '\0'; return memcpy(sub, str, sub_end); @@ -282,6 +321,7 @@ static char *next_token(char *str) else if(strcmp(s, "left") == 0) tok.s = LEFT; else if(strcmp(s, "right") == 0) tok.s = RIGHT; else if(strcmp(s, "noprec") == 0) tok.s = NOPREC; + else if(strcmp(s, "stacktype") == 0) tok.s = STYPE; else { fprintf(stderr, "ERROR: Unknown directive '-%s'\n", s); goto fail; } break; case '{': @@ -290,7 +330,7 @@ static char *next_token(char *str) else if(str[off] == '{') c++; else if(str[off] == '}') c--; tok.s = ACTION; - tok.v = (intptr_t)strdup(substring(str, off)); + tok.v = (intptr_t)strdup(escapeNL(substring(str, off))); break; } } else if(isalpha(c0)) { // iden or named symbol -- cgit v1.2.3