From: Lukas Krickl Date: Sat, 18 Nov 2023 06:27:38 +0000 (+0100) Subject: Added tests for symbol resolver X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=2562972a35408dd85fc7a2f48cf2501ab0212119;p=ulas%2F.git Added tests for symbol resolver --- diff --git a/src/test.c b/src/test.c index ce9d929..c8b18d5 100644 --- a/src/test.c +++ b/src/test.c @@ -163,6 +163,25 @@ void test_preproc(void) { free(tok.lit.val.strv); \ } +#define ASSERT_SYMBOL_TOTOK(expected_val, expected_rc, token) \ + { \ + int rc = 0; \ + struct ulas_tok tok = ulas_totok((token), strlen(token), &rc); \ + assert((expected_rc) == rc); \ + assert(tok.type == ULAS_TOKSYMBOL); \ + assert(tok.lit.type == ULAS_STR); \ + assert(strcmp((expected_val), tok.lit.val.strv) == 0); \ + free(tok.lit.val.strv); \ + } + +#define ASSERT_UNEXPECTED_TOTOK(expected_rc, token) \ + { \ + int rc = 0; \ + ulas_totok((token), strlen(token), &rc); \ + assert((expected_rc) == rc); \ + } + + void test_totok(void) { TESTBEGIN("totok"); @@ -189,6 +208,12 @@ void test_totok(void) { // unterminated string ASSERT_STR_TOTOK("test\n\"123\"", -1, "\"test\\n\\\"123\\\""); + // symbols + ASSERT_SYMBOL_TOTOK("_symbol123", 0, "_symbol123"); + ASSERT_SYMBOL_TOTOK("symbol123", 0, "symbol123"); + + ASSERT_UNEXPECTED_TOTOK(-1, "1symbol123"); + TESTEND("totok"); } diff --git a/src/ulas.c b/src/ulas.c index 2031b74..b61fe4e 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -336,13 +336,13 @@ struct ulas_tok ulas_totok(char *buf, unsigned long n, int *rc) { } buf++; break; - } else if (ulas_isname(buf, n)) { - // literal. we can resolve it now - // because literals need to be able to be resolved - // for every line, unless they are a label! - // TODO: read and unescape striing between " and " + } else if (ulas_isname(buf - 1, n)) { + // literal token + // we resolve it later, will need to malloc here for now tok.type = ULAS_TOKSYMBOL; tok.lit.type = ULAS_STR; + tok.lit.val.strv = strndup(buf - 1, n); + buf += n - 1; } else { ULASERR("Unexpected token: %s\n", buf); *rc = -1;