From a73f911a403f5516578a10360c6cd88900348fed Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 20 Nov 2023 21:38:58 +0100 Subject: [PATCH] Added comment support to instruction parser --- src/ulas.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/ulas.c b/src/ulas.c index 33c0ae5..b57f15d 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -1345,6 +1345,16 @@ int ulas_intexpr(const char **line, unsigned long n, int *rc) { #define ULAS_ISINSTR(tok, name, n) (strncmp(tok, name, n) == 0) +int ulas_istokend(struct ulas_str *tok) { + long len = strnlen(tok->buf, tok->maxlen); + // skip comments though, they are not trailing tokens! + if (len > 0 && tok->buf[0] != ULAS_TOK_COMMENT) { + return 0; + } + + return 1; +} + // assembles an instruction, writes bytes into dst // returns bytes written or -1 on error int ulas_asminstr(char *dst, unsigned long max, const char *line, @@ -1355,6 +1365,10 @@ int ulas_asminstr(char *dst, unsigned long max, const char *line, return -1; } + if (ulas_istokend(&ulas.tok)) { + return 0; + } + if (ulas_tok(&ulas.tok, &line, n) == -1) { ULASERR("Expected label or instruction\n"); return -1; @@ -1380,6 +1394,14 @@ int ulas_asminstr(char *dst, unsigned long max, const char *line, return -1; } + // check for trailing + if (ulas_tok(&ulas.tok, &line, n) > 0) { + if (!ulas_istokend(&ulas.tok)) { + ULASERR("Trailing token '%s'\n", ulas.tok.buf); + return -1; + } + } + return rc; } -- 2.30.2