// 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
#include <assert.h>
#define ULAS_TOKMAX 64
-#define ULAS_SYMNAMEMAX 256
#define TESTBEGIN(name) printf("[test %s]\n", (name));
#define TESTEND(name) printf("[%s ok]\n", (name));
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");
}
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;
}
}
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;
// 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