aboutsummaryrefslogtreecommitdiff
path: root/demos/sample-files/gram-skeleton.c
diff options
context:
space:
mode:
authorkartofen <kartofen.mail.0@protonmail.com>2025-09-13 15:24:28 +0300
committerkartofen <kartofen.mail.0@protonmail.com>2025-09-13 15:24:28 +0300
commitdb1b9c8dcb0d115217a33c2fe8e0760d49143e11 (patch)
treec93743adff3d78ea066c14879b7d2bfeb3ce42fb /demos/sample-files/gram-skeleton.c
parent46e786db9d1b48b8fbc3502e36f093b755f3e09f (diff)
ast nearly build and proper errors
Diffstat (limited to 'demos/sample-files/gram-skeleton.c')
-rw-r--r--demos/sample-files/gram-skeleton.c62
1 files changed, 51 insertions, 11 deletions
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