From 154da4b2a8da5b7a288d9c91c94b62cf132b56ba Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Wed, 15 Nov 2023 14:21:41 +0100 Subject: [PATCH] WIP: expressions and values --- include/ulas.h | 68 +++++++++++++++++++++++++++----------------------- src/ulas.c | 22 ++++++++++++++++ 2 files changed, 59 insertions(+), 31 deletions(-) diff --git a/include/ulas.h b/include/ulas.h index cb5b5c9..24263e0 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -63,18 +63,6 @@ extern FILE *ulasin; extern FILE *ulasout; extern FILE *ulaserr; -// NULL for int based lookup -#define INULL (-1) - -// expression index in the expression list -typedef int iexpr; - -// token index in the token list -typedef int itok; - -// string index in the string list -typedef int istr; - struct ulas_expr; struct ulas_tok; @@ -174,25 +162,33 @@ struct ulas_preproc { * Tokens */ -enum ulas_toks { - ULAS_TOKLITERAL, - ULAS_TOKINT, - ULAS_TOKFLOAT, - ULAS_TOKCHAR, - ULAS_TOKSTRING -}; +enum ulas_toks { ULAS_TOKLITERAL, ULAS_TOKINT, ULAS_TOKCHAR, ULAS_TOKSTRING }; + +// primitive data types +enum ulas_type { ULAS_INT, ULAS_STR }; -struct ulas_tokliteral { - istr literal; +// data type value +union ulas_val { + int int_value; + char *str_value; }; -union ulas_tokdat { - struct ulas_tokliteral literal; +// literal value +struct ulas_lit { + enum ulas_type type; + union ulas_val val; }; struct ulas_tok { enum ulas_toks type; - union ulas_tokdat dat; + struct ulas_lit lit; +}; + +// the token buffer is a dynamically allocated token store +struct ulas_tokbuf { + struct ulas_tok *buf; + long len; + long maxlen; }; /** @@ -200,7 +196,8 @@ struct ulas_tok { */ struct ulas_sym { - istr name; + char *name; + struct ulas_lit lit; }; /** @@ -216,18 +213,18 @@ struct ulas_sym { enum ulas_exprs { ULAS_EXPUNARY, ULAS_EXPBINARY, ULAS_EXPLITERAL }; struct ulas_expunary { - iexpr left; - itok op; + struct ulas_expr *expr; + struct ulas_tok *op; }; struct ulas_expbinary { - iexpr left; - iexpr right; - itok op; + struct ulas_expr *left; + struct ulas_expr *right; + struct ulas_tok *op; }; struct ulas_expliteral { - itok tok; + struct ulas_tok *tok; }; union ulas_expdat { @@ -334,6 +331,15 @@ int ulas_preprocline(struct ulas_preproc *pp, FILE *dst, FILE *src, char *ulas_preprocexpand(struct ulas_preproc *pp, const char *raw_line, size_t *n); +/** + * Literals, tokens and expressions + */ + +// convert literal to its int value +int ulas_litint(struct ulas_lit *lit, int *rc); +// convert literal to its char value +char *ulas_litchar(struct ulas_lit *lit, int *rc); + /** * Assembly step */ diff --git a/src/ulas.c b/src/ulas.c index 8ef3730..970ad08 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -712,6 +712,28 @@ fail: return rc; } +/** + * Literals, tokens and expressions + */ + +int ulas_litint(struct ulas_lit *lit, int *rc) { + if (lit->type != ULAS_INT) { + *rc = -1; + return 0; + } + + return lit->val.int_value; +} + +char *ulas_litchar(struct ulas_lit *lit, int *rc) { + if (lit->type != ULAS_STR) { + *rc = -1; + return NULL; + } + + return lit->val.str_value; +} + /** * Assembly step */ -- 2.30.2