From: Lukas Krickl Date: Wed, 15 Nov 2023 14:29:36 +0000 (+0100) Subject: WIP: token buffer X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=5e485eb9409cf01772199ea107caf3a80db5fa88;p=ulas%2F.git WIP: token buffer --- diff --git a/include/ulas.h b/include/ulas.h index 24263e0..2fdabda 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -85,6 +85,39 @@ struct ulas_str { size_t maxlen; }; +/** + * Tokens + */ + +enum ulas_toks { ULAS_TOKLITERAL, ULAS_TOKINT, ULAS_TOKCHAR, ULAS_TOKSTRING }; + +// primitive data types +enum ulas_type { ULAS_INT, ULAS_STR }; + +// data type value +union ulas_val { + int int_value; + char *str_value; +}; + +// literal value +struct ulas_lit { + enum ulas_type type; + union ulas_val val; +}; + +struct ulas_tok { + enum ulas_toks type; + struct ulas_lit lit; +}; + +// the token buffer is a dynamically allocated token store +struct ulas_tokbuf { + struct ulas_tok *buf; + long len; + long maxlen; +}; + /** * Assembly context */ @@ -96,6 +129,9 @@ struct ulas { // holds the current token struct ulas_str tok; + // current token stream + struct ulas_tokbuf toks; + unsigned int address; // internal counter @@ -158,39 +194,6 @@ struct ulas_preproc { struct ulas_str macrobuf; }; -/** - * Tokens - */ - -enum ulas_toks { ULAS_TOKLITERAL, ULAS_TOKINT, ULAS_TOKCHAR, ULAS_TOKSTRING }; - -// primitive data types -enum ulas_type { ULAS_INT, ULAS_STR }; - -// data type value -union ulas_val { - int int_value; - char *str_value; -}; - -// literal value -struct ulas_lit { - enum ulas_type type; - union ulas_val val; -}; - -struct ulas_tok { - enum ulas_toks type; - struct ulas_lit lit; -}; - -// the token buffer is a dynamically allocated token store -struct ulas_tokbuf { - struct ulas_tok *buf; - long len; - long maxlen; -}; - /** * Symbols */ @@ -340,6 +343,11 @@ int ulas_litint(struct ulas_lit *lit, int *rc); // convert literal to its char value char *ulas_litchar(struct ulas_lit *lit, int *rc); +struct ulas_tokbuf ulas_tokbuf(void); +void ulas_tokbufpush(struct ulas_tokbuf *tb, struct ulas_tok tok); +void ulas_tokbufclear(struct ulas_tokbuf *tb); +void ulas_tokbuffree(struct ulas_tokbuf *tb); + /** * Assembly step */ diff --git a/src/ulas.c b/src/ulas.c index 970ad08..b8e853d 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -31,9 +31,14 @@ void ulas_init(struct ulas_config cfg) { if (cfg.argc) { ulas.filename = cfg.argv[0]; } + + ulas.toks = ulas_tokbuf(); } -void ulas_free(void) { ulas_strfree(&ulas.tok); } +void ulas_free(void) { + ulas_strfree(&ulas.tok); + ulas_tokbuffree(&ulas.toks); +} int ulas_icntr(void) { return ulas.icntr++; } @@ -734,6 +739,36 @@ char *ulas_litchar(struct ulas_lit *lit, int *rc) { return lit->val.str_value; } +struct ulas_tokbuf ulas_tokbuf(void) { + struct ulas_tokbuf tb; + memset(&tb, 0, sizeof(tb)); + + tb.maxlen = 5; + tb.buf = malloc(tb.maxlen * sizeof(struct ulas_tok)); + tb.len = 0; + + return tb; +} + +void ulas_tokbufpush(struct ulas_tokbuf *tb, struct ulas_tok tok) { + if (tb->len >= tb->maxlen) { + tb->maxlen += 5; + void *n = realloc(tb->buf, tb->maxlen * sizeof(struct ulas_tok)); + if (!n) { + ULASPANIC("%s\n", strerror(errno)); + } + + tb->buf = n; + } + + tb->buf[tb->len] = tok; + tb->len++; +} + +void ulas_tokbufclear(struct ulas_tokbuf *tb) { tb->len = 0; } + +void ulas_tokbuffree(struct ulas_tokbuf *tb) { free(tb->buf); } + /** * Assembly step */