From: Lukas Krickl Date: Sun, 3 Dec 2023 12:32:12 +0000 (+0100) Subject: WIP: moved : removal code to symbolset X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=82bcaf2f46ecde795e529ae741e71c3f6d627ef2;p=ulas%2F.git WIP: moved : removal code to symbolset --- diff --git a/include/ulas.h b/include/ulas.h index c942e13..dba2476 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -420,7 +420,7 @@ struct ulas_sym *ulas_symbolresolve(const char *name, int *rc); // the function to auto-detect the scope // if a label starts with @ the current scope is used, otherwise 0 is used // if the symbol already exists -1 is returned -int ulas_symbolset(const char *name, int scope, struct ulas_tok tok, +int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok, int constant); // tokenisze according to pre-defined rules diff --git a/src/test.c b/src/test.c index e1c97c0..b939583 100644 --- a/src/test.c +++ b/src/test.c @@ -3,7 +3,6 @@ #include #define ULAS_TOKMAX 64 -#define ULAS_SYMNAMEMAX 256 #define TESTBEGIN(name) printf("[test %s]\n", (name)); #define TESTEND(name) printf("[%s ok]\n", (name)); @@ -312,8 +311,11 @@ void test_asminstr(void) { void test_symscope(void) { TESTBEGIN("symscope"); + // auto-scope ASSERT_SYMSCOPE(0, "t1:", -1, 1); ASSERT_SYMSCOPE(-1, "t1:", -1, 1); + ASSERT_SYMSCOPE(0, "@t1:", -1, 1); + ASSERT_SYMSCOPE(-1, "@t1:", -1, 1); TESTEND("symscope"); } diff --git a/src/ulas.c b/src/ulas.c index 6466d64..833b1e8 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -211,19 +211,12 @@ int ulas_islabelname(const char *tok, unsigned long n) { return tok[n - 1] == ':' && ulas_isname(tok, n - 1); } -struct ulas_sym *ulas_symbolresolve(const char *cname, int *rc) { - char name[ULAS_SYMNAMEMAX]; - long len = strlen(cname); - assert(len < ULAS_SYMNAMEMAX); - strncpy(name, cname, len); - if (name[len - 1] == ':') { - name[len - 1] = '\0'; - } - +struct ulas_sym *ulas_symbolresolve(const char *name, int *rc) { for (int i = 0; i < ulas.syms.len; i++) { struct ulas_sym *sym = &ulas.syms.buf[i]; // when scope is the same as the current one, or scope 0 (global) - if ((sym->scope & ulas.scope) == 0 && strcmp(name, sym->name) == 0) { + if ((sym->scope == 0 || sym->scope == ulas.scope) && + strcmp(name, sym->name) == 0) { return sym; } } @@ -231,8 +224,17 @@ struct ulas_sym *ulas_symbolresolve(const char *cname, int *rc) { return NULL; } -int ulas_symbolset(const char *name, int scope, struct ulas_tok tok, +int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok, int constant) { + // remove : from name + char name[ULAS_SYMNAMEMAX]; + long len = strlen(cname); + assert(len < ULAS_SYMNAMEMAX); + strncpy(name, cname, len); + if (name[len - 1] == ':') { + name[len - 1] = '\0'; + } + int rc = 0; int resolve_rc = 0; @@ -254,12 +256,6 @@ int ulas_symbolset(const char *name, int scope, struct ulas_tok tok, // def new symbol struct ulas_sym new_sym = {strdup(name), tok, scope, ulas.pass, constant}; - // last char of name has to be : so we trim it away - long len = strlen(new_sym.name); - assert(len < ULAS_SYMNAMEMAX); - if (new_sym.name[len - 1] == ':') { - new_sym.name[len - 1] = '\0'; - } ulas_symbufpush(&ulas.syms, new_sym); } else if (exisitng->lastdefin != ulas.pass) { // redefine if not defined this pass