From: Lukas Krickl Date: Mon, 27 Nov 2023 18:25:38 +0000 (+0100) Subject: WIP: symbol buffer X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=2f8a5f9738271e8178859f0de3e09195ba630a8a;p=ulas%2F.git WIP: symbol buffer --- diff --git a/include/ulas.h b/include/ulas.h index fc9756e..35773e2 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -148,6 +148,27 @@ struct ulas_exprbuf { unsigned long maxlen; }; +/** + * Symbols + */ + +enum ulas_syms { ULAS_SYM_FORWARD, ULAS_SYM_DECL }; + +enum ulas_symres { ULAS_EGLOBALUNRESOLVE = 1, ULAS_ELOCALUNRESOLVE = 2 }; + +struct ulas_sym { + char *name; + struct ulas_tok tok; + enum ulas_syms type; +}; + +// holds all currently defned symbols +struct ulas_symbuf { + struct ulas_sym *buf; + unsigned long len; + unsigned long maxlen; +}; + /** * Assembly context */ @@ -162,6 +183,7 @@ struct ulas { // current token stream struct ulas_tokbuf toks; struct ulas_exprbuf exprs; + struct ulas_symbuf syms; unsigned int address; @@ -223,20 +245,6 @@ struct ulas_preproc { struct ulas_str macrobuf; }; -/** - * Symbols - */ - -enum ulas_syms { ULAS_SYM_FORWARD, ULAS_SYM_DECL }; - -enum ulas_symres { ULAS_EGLOBALUNRESOLVE = 1, ULAS_ELOCALUNRESOLVE = 2 }; - -struct ulas_sym { - char *name; - struct ulas_tok tok; - enum ulas_syms type; -}; - /** * Expressions * @@ -461,6 +469,9 @@ char *ulas_valstr(struct ulas_tok *lit, enum ulas_symres flags, int *rc); struct ulas_tokbuf ulas_tokbuf(void); +// TODO: maybe we could macro all those buf functions into a single more +// "geneirc" thing... + // pushes new token, returns newly added index int ulas_tokbufpush(struct ulas_tokbuf *tb, struct ulas_tok tok); struct ulas_tok *ulas_tokbufget(struct ulas_tokbuf *tb, int i); @@ -475,6 +486,12 @@ struct ulas_expr *ulas_exprbufget(struct ulas_exprbuf *eb, int i); void ulas_exprbufclear(struct ulas_exprbuf *eb); void ulas_exprbuffree(struct ulas_exprbuf *eb); +struct ulas_symbuf ulas_symbuf(void); +int ulas_symbufpush(struct ulas_symbuf *sb, struct ulas_sym sym); +struct ulas_sym *ulas_symbufget(struct ulas_symbuf *sb, int i); +void ulas_symbufclear(struct ulas_symbuf *sb); +void ulas_symbuffree(struct ulas_symbuf *sb); + /** * Assembly step */ diff --git a/src/ulas.c b/src/ulas.c index cacc4b7..ac2452f 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -38,12 +38,14 @@ void ulas_init(struct ulas_config cfg) { ulas.toks = ulas_tokbuf(); ulas.exprs = ulas_exprbuf(); + ulas.syms = ulas_symbuf(); } void ulas_free(void) { ulas_strfree(&ulas.tok); ulas_tokbuffree(&ulas.toks); ulas_exprbuffree(&ulas.exprs); + ulas_symbuffree(&ulas.syms); } int ulas_icntr(void) { return ulas.icntr++; } @@ -995,12 +997,16 @@ int ulas_tokbufpush(struct ulas_tokbuf *tb, struct ulas_tok tok) { return tb->len++; } +void ulas_tokfree(struct ulas_tok *t) { + if (t->type == ULAS_SYMBOL || t->type == ULAS_STR) { + free(t->val.strv); + } +} + void ulas_tokbufclear(struct ulas_tokbuf *tb) { for (long i = 0; i < tb->len; i++) { struct ulas_tok *t = &tb->buf[i]; - if (t->type == ULAS_SYMBOL || t->type == ULAS_STR) { - free(t->val.strv); - } + ulas_tokfree(t); } tb->len = 0; } @@ -1046,6 +1052,48 @@ void ulas_exprbufclear(struct ulas_exprbuf *eb) { eb->len = 0; } void ulas_exprbuffree(struct ulas_exprbuf *eb) { free(eb->buf); } +struct ulas_symbuf ulas_symbuf(void) { + struct ulas_symbuf sb; + memset(&sb, 0, sizeof(sb)); + + sb.maxlen = 10; + sb.buf = malloc(sizeof(struct ulas_sym) * sb.maxlen); + + return sb; +} + +int ulas_symbufpush(struct ulas_symbuf *sb, struct ulas_sym sym) { + if (sb->len >= sb->maxlen) { + sb->maxlen *= 2; + void *newbuf = realloc(sb->buf, sb->maxlen * sizeof(struct ulas_sym)); + if (!newbuf) { + ULASPANIC("%s\n", strerror(errno)); + } + sb->buf = newbuf; + } + + sb->buf[sb->len] = sym; + return sb->len++; +} + +struct ulas_sym *ulas_symbufget(struct ulas_symbuf *sb, int i) { + if (i >= sb->len) { + return NULL; + } + + return &sb->buf[i]; +} + +void ulas_symbufclear(struct ulas_symbuf *sb) { + for (long i = 0; i < sb->len; i++) { + struct ulas_sym *s = &sb->buf[i]; + ulas_tokfree(&s->tok); + } + sb->len = 0; +} + +void ulas_symbuffree(struct ulas_symbuf *sb) { free(sb->buf); } + /** * Assembly step */