summaryrefslogtreecommitdiff
path: root/lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c506
1 files changed, 338 insertions, 168 deletions
diff --git a/lexer.c b/lexer.c
index bb3701a..57a7e14 100644
--- a/lexer.c
+++ b/lexer.c
@@ -10,97 +10,193 @@ symbol *last_loc = NULL;
symbol *cur_sym = NULL;
symbol *struct_sym = NULL;
+
line *tmp_line = NULL;
-symbol *mksymbol(const char *name, uint64_t val, uint8_t def, uint8_t islocal, uint8_t useid, uint16_t id, uint8_t dbg) {
- uint16_t i = 0;
- symbol *s = (!islocal) ? symbols : locals;
- uint8_t flag = 0;
- for (; s; s = s->next, i++) {
- if (!useid && name[0] != s->name[0]) {
- continue;
- }
- flag = (useid) ? (id == s->id) : !strcmp(name, s->name);
- if (flag) {
- if (def) {
- if (s->def) {
- if (dbg) {
- printf("mksymbol(): oof, you cannot redefine the symbol: %s\n", name);
+symbol *find_symbol(symbol *root, const char *name, int depth, uint8_t dbg) {
+ symbol *s = root;
+ symbol *ret = NULL;
+ char *tmp = malloc(strlen(name)+1);
+ memcpy(tmp, name, strlen(name)+1);
+ char *scope_name = strtok_r(tmp, ".", &tmp);
+ size_t name_len = strlen(scope_name);
+ for (; s != NULL; s = s->next) {
+ size_t sym_name_len = strlen(s->name);
+ if (name_len == sym_name_len && scope_name[0] == s->name[0] && !strcmp(scope_name, s->name)) {
+ if (depth) {
+ if (s->down) {
+ ret = find_symbol(s->down, (tmp == NULL) ? scope_name : tmp, depth-1, dbg);
+ if (ret) {
+ return ret;
}
- defined = 1;
- } else {
- defined = 0;
- }
- s->def = def;
- s->val = val;
- s->id = i;
- if (dbg) {
- printf("mksymbol(): def: %u, val: $%016"PRIX64", name: %s\n", def, val, name);
- printf("mksymbol(): i: $%X, id: $%04X\n", i, s->id);
}
+ } else {
+ return s;
}
- return s;
}
}
- size_t str_size = strlen(name)+1;
- s = malloc(sizeof(symbol));
- s->down = NULL;
- if (!islocal) {
- (last_sym) ? (last_sym->next = s) : (symbols = s);
- if (last_sym) {
- last_sym->next->prev = last_sym;
- last_sym->next->up = last_sym->up;
- last_sym->next->down = NULL;
+ return NULL;
+}
+
+int add_symbol(symbol *sym, const char *name, symbol **root, symbol **lsym, symbol **rloc, symbol **lloc, symbol **csym, int csym_depth, int depth, uint8_t dbg) {
+ symbol *s = (!depth) ? ((*lsym) ? *lsym : *root): ((*lloc) ? *lloc : *rloc);
+ sym->next = NULL;
+ sym->up = NULL;
+ sym->down = NULL;
+ sym->prev = NULL;
+ if (depth && depth >= csym_depth) {
+ //s = *csym;
+ s = find_symbol(*root, name, depth-1, dbg);
+ depth -= (!csym_depth) ? depth-1 : csym_depth;
+ }
+ int j = 0;
+ int is_new_scope = 0;
+ for (int i = 0; i < depth; i++) {
+ if (s) {
+ if (s->down != NULL) {
+ s->down->up = (s->down->up == NULL) ? s : s->down->up;
+ s = s->down;
+ } else if (j) {
+ return 0;
+ } else {
+ int is_first_entry = ((s->up == NULL) || (s->next == NULL && s->up));
+ is_new_scope = (depth <= 1 && is_first_entry);
+ j++;
+ }
} else {
- symbols->prev = NULL;
- symbols->up = NULL;
- symbols->down = NULL;
+ is_new_scope = 1;
+ /*return 0;*/
}
- } else {
- (last_loc) ? (last_loc->next = s) : (locals = s);
- if (last_loc) {
- last_loc->next->prev = last_loc;
- last_loc->next->up = last_loc->up;
- last_loc->next->down = NULL;
+ }
+ for (; s != NULL && s->next != NULL; s = s->next) {
+ if (dbg) {
+ printf("s: %p, s->next: %p, s->prev: %p\n", s, s->next, s->prev);
+ }
+ }
+ if ((is_new_scope && *lsym) || *lloc || *csym) {
+ /*
+ if (is_new_scope) {
+ s = *lsym;
} else {
- locals->prev = NULL;
- locals->down = NULL;
+ s = (!depth) ? *lsym : *lloc;
+ }*/
+ if (is_new_scope && sym->depth > s->depth) {
+ s->down = sym;
+ s->down->up = s;
+ s->down->prev = NULL;
+ } else {
+ s->next = sym;
+ }
+ if (dbg) {
+ for (symbol *tmp = s; tmp && tmp->prev; tmp = tmp->prev) {
+ printf("add_symbol(): s: %p, s->name: %s, s->next: %p, s->prev: %p, s->up: %p, s->down: %p\n", tmp, tmp->name, tmp->next, tmp->prev, tmp->up, tmp->down);
+ }
+ }
+ sym->next = NULL;
+ if (s->next == sym) {
+ sym->prev = s;
+ if (sym->depth) {
+ sym->up = s->up;
+ }
+ }
+ sym->down = NULL;
+
+ }
+
+ if ((*root && (*root)->prev)) {
+ (*root)->prev = NULL;
+ }
+
+ if ((*rloc && (*rloc)->prev)) {
+ (*rloc)->prev = NULL;
+ }
+ if (!depth) {
+ if (*lsym == NULL) {
+ *root = sym;
+ sym->next = NULL;
+ sym->prev = NULL;
+ sym->up = NULL;
+ sym->down = NULL;
+ }
+ *lsym = sym;
+ *rloc = NULL;
+ *lloc = NULL;
+ } else {
+ if (is_new_scope) {
+ *lloc = NULL;
+ }
+ if (*lloc == NULL) {
+ *rloc = sym;
+ sym->next = NULL;
+ sym->prev = NULL;
+ sym->down = NULL;
}
+ *lloc = sym;
}
+ *csym = sym;
+ return 1;
+}
+
+char *find_deepest_scope(const char *name) {
+ int i;
+ for (i = strlen(name); i >= 0 && name[i] != '.'; i--);
+ return (char *)name+(i+1);
+}
+
+symbol *mksymbol(const char *name, uint64_t val, uint8_t def, int depth, uint8_t use_scope, uint16_t id, uint8_t dbg) {
+ uint16_t i = 0;
+ symbol *s = find_symbol(symbols, (char *)name, depth, dbg);
+ char *scope_name = (!use_scope) ? find_deepest_scope(name) : (char *)name;
+ if (s) {
+ if (def) {
+ if (s->def) {
+ if (dbg) {
+ printf("mksymbol(): oof, you cannot redefine the symbol: %s\n", scope_name);
+ }
+ defined = 1;
+ } else {
+ defined = 0;
+ }
+ s->def = def;
+ s->val = val;
+ s->id = i;
+ if (dbg) {
+ printf("mksymbol(): def: %u, val: $%016"PRIX64", name: %s\n", def, val, scope_name);
+ printf("mksymbol(): i: $%X, id: $%04X\n", i, s->id);
+ }
+ }
+ return s;
+ }
+ size_t str_size = strlen(scope_name)+1;
+
+ s = malloc(sizeof(symbol));
s->name = malloc(str_size);
s->def = def;
s->val = val;
s->count = 0;
s->isstruct = 0;
- memcpy(s->name, name, str_size);
+ s->depth = depth;
s->next = NULL;
+ s->up = NULL;
+ s->down = NULL;
+ s->prev = NULL;
s->id = sym_count++;
- (!islocal) ? (last_sym = s) : (last_loc = s);
- if (!islocal) {
- s->down = NULL;
- /*if (def) {
- locals = NULL;
- last_loc = NULL;
- }*/
- } else {
- cur_sym->count++;
- }
+
+ memcpy(s->name, scope_name, str_size);
defined = 0;
if (dbg) {
- printf("mksymbol(): def: %u, val: $%016"PRIX64", name: %s, id: $%04X\n", def, val, name, sym_count-1);
+ printf("mksymbol(): def: %u, val: $%016"PRIX64", name: %s, id: $%04X\n", def, val, scope_name, sym_count-1);
}
return s;
}
uint16_t fixup_cnt = 0;
-symbol *get_sym(const char *name, uint64_t val, token *t, uint8_t islocal, uint8_t dbg) {
- symbol *s = mksymbol(name, 0, 0, islocal, 0, 0, dbg);
+symbol *get_sym(const char *name, uint64_t val, token *t, uint8_t depth, uint8_t use_scope, uint8_t dbg) {
+ symbol *s = mksymbol(name, val, 0, depth, use_scope, use_scope, dbg);
if (dbg) {
printf("get_sym(): Symbol ID: $%X.\n", s->id);
}
- if (s->def) {
- return s;
- } else {
+ if (!s->def) {
if (dbg) {
printf("get_sym(): oof, symbol %s, does not exist, yet.\n", name);
}
@@ -112,28 +208,8 @@ symbol *get_sym(const char *name, uint64_t val, token *t, uint8_t islocal, uint8
f->next = NULL;
last_fix = f;
fixup_cnt++;
- return NULL;
}
-}
-
-symbol *find_member(char *name, symbol* root, uint8_t dbg) {
- /*for (; root->up; root = root->up);*/
- symbol *s = root;
- if (s->down == NULL && s->up != NULL) {
- s = s->up;
- }
- do {
- s = s->down;
- for (symbol *m = s; m; m = m->next) {
- size_t len1 = strlen(name);
- size_t len2 = strlen(m->name);
- if (len1 == len2 && name[0] == m->name[0] && !strcmp(name, m->name)) {
- return m;
- }
- }
- for (;s && s->next && !s->down; s = s->next);
- } while (s && s->down);
- return NULL;
+ return s;
}
uint16_t reslv_fixups(uint8_t dbg) {
@@ -228,16 +304,62 @@ line *find_line(uint32_t ln, uint8_t dbg) {
return l;
}
+char *mk_scope_name(symbol *csym, int depth, const char *name, uint8_t dbg) {
+ size_t len = strlen(name);
+ size_t name_len = len;
+ symbol *s = csym;
+ char **scopes;
+ if (depth) {
+ scopes = malloc(sizeof(char *)*depth);
+ }
+
+ for (int i = depth; i && s; i--) {
+ if (dbg) {
+ printf("mk_scope_name(): s->depth: %i\n", s->depth);
+ }
+ s = (s->depth >= i && s->up) ? s->up : s;
+ //s = (i < 2 && s->up) ? s->up : s;
+ len += strlen(s->name);
+ scopes[i-1] = s->name;
+ }
+
+ len += depth;
+ char *scope_name = malloc(len+1);
+ char *tmp = scope_name;
+ memset(tmp, 0, len+1);
+ for (int i = 0; i < depth; i++) {
+ size_t name_len = strlen(scopes[i]);
+ memcpy(tmp, scopes[i], name_len);
+ tmp += name_len;
+ *tmp++ = '.';
+ if (dbg) {
+ printf("mk_scope_name(): scope_name: %s\n", scope_name);
+ }
+ }
+ memcpy(tmp, name, name_len);
+ if (dbg) {
+ printf("mk_scope_name(): scope_name: %s\n", scope_name);
+ }
+ return scope_name;
+}
+
int is_struct = 0;
int is_anon = 0;
-void create_struct(symbol *c_sym, line *l, token *t, token *lt, char *name, uint8_t dbg) {
- uint8_t ismember = !(is_struct == 1 && lt && lt->id == TOK_DIR);
- mksymbol(name, 0, 1, ismember, 0, 0, dbg);
+void create_struct(symbol *c_sym, line *l, token *t, token *lt, const char *name, uint8_t dbg) {
+ int depth = is_struct-is_anon;
+ uint8_t ismember = !(depth == 1 && lt && lt->id == TOK_DIR);
+ int is_new_scope = (lt && lt->id == TOK_DIR);
+ depth -= is_new_scope;
+
+ char *struct_name = mk_scope_name(c_sym, depth, name, dbg);
+ symbol *s = mksymbol(struct_name, 0, 1, depth, 0, 0, dbg);
+ int is_sym_added = add_symbol(s, struct_name, &symbols, &last_sym, &locals, &last_loc, &c_sym, 0, depth, dbg);
+ t->sym = get_sym(struct_name, 0, t, depth, 0, dbg);
+
if (isfixup) {
isfixup = reslv_fixups(dbg);
}
- t->sym = get_sym(name, 0, t, ismember, dbg);
if (lt && lt->id == TOK_DIR) {
t->sym->isstruct = 1;
t->id = (lt->type == DIR_STRUCT) ? TOK_STRUCT : TOK_UNION;
@@ -257,26 +379,24 @@ void create_struct(symbol *c_sym, line *l, token *t, token *lt, char *name, uint
} else {
c_sym->down = locals;
}
- } else {
- if (lt && lt->id == TOK_DIR) {
- if (lt->type == DIR_UNION || lt->type == DIR_STRUCT) {
- c_sym->down = locals;
- c_sym->down->up = c_sym;
- last_loc->up = c_sym;
- c_sym = last_loc;
- locals = NULL;
- last_loc = NULL;
- }
- }
}
cur_sym = c_sym;
}
void end_struct(symbol *c_sym, symbol *s_sym, uint8_t dbg) {
int skip = 0;
- if (/*s_sym &&*/ is_anon > 0) {
+ if (is_anon > 0) {
if ((c_sym && c_sym->isanon) || (c_sym->up && !c_sym->up->isanon) || (c_sym && s_sym->isanon)) {
- is_anon--;
+ int depth = is_struct-is_anon;
+ if ((depth > 0 || (is_struct > 1 && is_anon >= is_struct)) && c_sym->depth > depth) {
+ for (; c_sym->depth > is_struct-is_anon && c_sym->up; c_sym = c_sym->up) {
+ if (dbg) {
+ printf("end_struct(): c_sym->depth: %i, is_struct-is_anon: %i\n", c_sym->depth, is_struct-is_anon);
+ }
+ }
+ } else if (c_sym->depth) {
+ is_anon--;
+ }
} else if (is_struct <= 0) {
is_anon = 0;
}
@@ -289,15 +409,21 @@ void end_struct(symbol *c_sym, symbol *s_sym, uint8_t dbg) {
s->up = c_sym;
}
if (dbg) {
- printf("s: %p, s->up: %p, c_sym: %p, last_loc: %p\n", s, s->up, c_sym, last_loc);
+ printf("end_struct(): s: %p, s->up: %p, c_sym: %p, last_loc: %p\n", s, s->up, c_sym, last_loc);
}
}
- if (c_sym->down == NULL) {
- c_sym->down = locals;
- }
}
if ((is_anon <= 0 || is_struct <= 0)) {
- for (s_sym = c_sym; /*s_sym &&*/ s_sym->prev && !s_sym->isanon; s_sym = s_sym->prev);
+ if (dbg) {
+ for (symbol *s = c_sym; s->next && !s->isanon; s = s->next) {
+ printf("end_struct(): %p, s->next: %p, s->prev: %p, s->isanon: %i\n", s, s->next, s->prev, s->isanon);
+ }
+ }
+ for (s_sym = c_sym; s_sym->prev && !s_sym->isanon; s_sym = s_sym->prev) {
+ if (dbg) {
+ printf("end_struct(): s_sym: %p, s_sym->next: %p, s_sym->prev: %p, s_sym->isanon: %i\n", s_sym, s_sym->next, s_sym->prev, s_sym->isanon);
+ }
+ }
struct_sym = s_sym;
}
if ((is_struct-is_anon) > 0 && !skip) {
@@ -307,7 +433,7 @@ void end_struct(symbol *c_sym, symbol *s_sym, uint8_t dbg) {
s->up = c_sym->up;
}
if (dbg) {
- printf("s: %p, s->up: %p, c_sym->up: %p, last_loc: %p\n", s, s->up, c_sym->up, last_loc);
+ printf("end_struct(): s: %p, s->up: %p, c_sym->up: %p, last_loc: %p\n", s, s->up, c_sym->up, last_loc);
}
}
if (c_sym->up) {
@@ -318,6 +444,74 @@ void end_struct(symbol *c_sym, symbol *s_sym, uint8_t dbg) {
}
}
+fixup *find_fixup(fixup *root, const char *name, int depth, uint8_t dbg) {
+ size_t name_len = strlen(name);
+ for (fixup *f = root; f; f = f->next) {
+ symbol *s = f->s;
+ if (s && s->name) {
+ if (s->depth == depth) {
+ size_t sym_name_len = strlen(s->name);
+ if (name_len == sym_name_len && name[0] == s->name[0] && !strcmp(name, s->name)) {
+ if (!s->def) {
+ return f;
+ }
+ }
+ }
+ }
+ }
+ return NULL;
+}
+
+void resolve_symbol_names(line *l, const char *name, symbol *sym, int depth, uint8_t dbg) {
+ size_t name_len = strlen(name);
+ for (token *t = l->tok; t; t = t->next) {
+ size_t sym_name_len;
+ switch (t->id) {
+ case TOK_LABEL:
+ case TOK_SYM:
+ if (t->sym) {
+ sym_name_len = strlen(t->sym->name);
+ if (name_len == sym_name_len && name[0] == t->sym->name[0] && !strcmp(name, t->sym->name)) {
+ if (t->sym->depth == depth && !t->sym->def) {
+ t->sym = sym;
+ }
+ }
+ }
+ break;
+ }
+ }
+ if (l->next) {
+ resolve_symbol_names(l->next, name, sym, depth, dbg);
+ }
+}
+
+void new_symbol(token *t, const char *name, uint64_t value, int depth, uint8_t dbg) {
+ size_t name_len = strlen(name);
+ char *scope_name = mk_scope_name(cur_sym, depth, name, dbg);
+ symbol *s;
+ fixup *f = find_fixup(fixups, scope_name, depth, dbg);
+ if (f == NULL) {
+ s = mksymbol(scope_name, value, 1, depth, 0, 0, dbg);
+ } else {
+ s = f->s;
+ resolve_symbol_names(lines, scope_name, s, depth, dbg);
+ s->def = 1;
+ s->val = value;
+ free(s->name);
+ s->name = NULL;
+ s->name = malloc(name_len+1);
+ memcpy(s->name, name, name_len+1);
+ }
+ int is_sym_added = add_symbol(s, scope_name, &symbols, &last_sym, &locals, &last_loc, &cur_sym, 0, depth, dbg);
+ if (isfixup) {
+ isfixup = reslv_fixups(dbg);
+ }
+ if (t) {
+ t->sym = get_sym(scope_name, value, t, depth, 0, dbg);
+ isfixup += (t->sym == NULL);
+ }
+}
+
uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
char sym[0x100];
uint16_t i = 0;
@@ -337,7 +531,9 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
uint8_t of = 0;
uint8_t base = 0;
- uint8_t islocal = 0;
+ int depth = 0;
+
+ int is_inst = 0;
uint8_t isop = 0;
int num = 0;
@@ -355,13 +551,11 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
uint8_t done = 0;
- /*uint8_t is_newcom = 0;*/
line *l = NULL;
token *st = NULL;
token *t = NULL;
token *lt = NULL;
symbol *tmp_sym = NULL;
- symbol *tsym = NULL;
while (isdigit(str[i]) && isdelm(str[i], dbg) != 16) {
lnum[j++] = str[i++];
@@ -412,6 +606,7 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
if (((ptok == PTOK_S || ptok == PTOK_B) && toupper(str[i+1]) == 'P') || (ptok == PTOK_P && toupper(str[i+1]) == 'C')) {
offset++;
}
+ int is_alpha = 0;
switch (get_ptok(str[i+offset], dbg)) {
case PTOK_B :
case PTOK_E :
@@ -424,7 +619,7 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
case PTOK_D :
case PTOK_F :
case PTOK_R :
- case PTOK_ALPHA : ptok = PTOK_ALPHA; break;
+ case PTOK_ALPHA : ptok = PTOK_ALPHA; is_alpha = 1; break;
case PTOK_NUMBER:
if (ptok == PTOK_R) {
char reg_num[3];
@@ -435,20 +630,28 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
reg_num[isnum] = '\0';
if (isnum == 2) {
int regnum = strtoul(reg_num, NULL, 10);
- ptok = (regnum < 11 || regnum > 15) ? PTOK_ALPHA : ptok;
+ if (regnum < 11 || regnum > 15) {
+ ptok = PTOK_ALPHA;
+ is_alpha = 1;
+ }
} else {
ptok = PTOK_ALPHA;
+ is_alpha = 1;
}
} else {
ptok = PTOK_ALPHA;
+ is_alpha = 1;
}
break;
}
- if ((ptok == PTOK_S && str[i+1] && toupper(str[i+1]) != 'P') || (ptok == PTOK_P && toupper(str[i+1]) != 'C')) {
+ if (ptok == PTOK_P && toupper(str[i+1]) != 'C') {
ptok = PTOK_ALPHA;
+ is_alpha = 1;
}
+
+ ptok = (!is_inst && !is_alpha) ? PTOK_ALPHA : ptok;
}
- /*i = ptok_handler[ptok](str, i, lex_type, l, t, dbg);*/
+
switch (ptok) {
case PTOK_DOT:
i++;
@@ -463,9 +666,10 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
uint16_t tmp = j;
for (j = 0; isdelm(str[i+j], dbg) & 16; j++);
uint8_t ret = get_ptok(str[i+j], dbg);
+ ret = (is_altok(ret, dbg)) ? PTOK_ALPHA : ret;
j = tmp;
- if ((k == DIR_STRUCT || k == DIR_UNION) && (ret != PTOK_ALPHA || (is_anon && ret == PTOK_ALPHA))) {
+ if ((k == DIR_STRUCT || k == DIR_UNION) && ret != PTOK_ALPHA) {
is_anon++;
}
is_struct += (k == DIR_STRUCT || k == DIR_UNION);
@@ -556,16 +760,8 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
i += j;
value = strtoull(lexeme, NULL, base);
if (lt->id == TOK_SYM) {
- tsym = mksymbol(sym, value, 1, islocal, 0, 0, dbg);
- if (lt) {
- lt->sym = get_sym(sym, address, lt, islocal, dbg);
- }
- if (!islocal) {
- cur_sym = last_sym;
- }
- tsym = NULL;
- islocal = 0;
- isfixup += (lt->sym == NULL);
+ new_symbol(lt, sym, value, depth, dbg);
+ depth = 0;
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
@@ -606,20 +802,17 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
break;
case PTOK_LBRACK:
case PTOK_HASH :
- /*l->tok->type = (ptok == PTOK_LBRACK) ? IND : IMM;
- lex_type = (ptok == PTOK_LBRACK) ? TOK_IND : TOK_IMM;*/
lex_type = TOK_MEM;
value = (ptok == PTOK_LBRACK) ? MEM_IND : MEM_IMM;
l->count++;
t = make_token(lex_type, value, space, tab, 0, "", NULL);
lex_type = (ptok == PTOK_LBRACK) ? TOK_IND : TOK_IMM;
+ t->subtype = (t->subtype == 0xFF && lex_subtype != 0xFF) ? lex_subtype : t->subtype;
if (lex_subtype != 0xFF) {
lex_subtype = 0xFF;
}
memset(lexeme, 0, strlen(lexeme)+1);
lexeme[j++] = str[i];
- /*(t) ? (t->subspace = space) : (lt->subspace = space);
- (t) ? (t->subtab = tab) : (lt->subtab = tab);*/
break;
case PTOK_PLUS:
case PTOK_MINUS:
@@ -695,7 +888,6 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
lexeme[j+1] = (ptok == PTOK_R || ((ptok == PTOK_S || ptok == PTOK_B) && get_ptok(str[i], dbg) == PTOK_P)) ? str[i++] : '\0';
lexeme[j+2] = (ptok == PTOK_R) ? str[i++] : '\0';
lexeme[j+3] = '\0';
- /*lex_subtype = (lex_type == TOK_CSV && lt && lt->subtype != TOK_CSV) ? lex_type : lex_subtype;*/
lex_type = TOK_REG;
switch (ptok) {
case PTOK_A: value = REG_A; break;
@@ -719,17 +911,11 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
t = make_token(lex_type, value, space, tab, 0, "", NULL);
t->subtype = (t->subtype == 0xFF && lex_subtype != 0xFF) ? lex_subtype : t->subtype;
lex_subtype = 0xFF;
- /*(t) ? (t->subspace = space) : (lt->subspace = space);
- (t) ? (t->subtab = tab) : (lt->subtab = tab);*/
break;
case PTOK_P:
lexeme[j] = str[i++];
lexeme[j+1] = (str[i] != ',') ? str[i++] : '\0';
lexeme[j+2] = '\0';
- /*switch (ptok) {
- case PTOK_S: of = 1; break;
- case PTOK_P: of = 2; break;
- }*/
of = 2;
lex_type = TOK_OF;
l->count++;
@@ -737,8 +923,8 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
break;
case PTOK_AT:
memset(lexeme, 0, strlen(lexeme)+1);
+ for (char *tmp = str+i; *tmp++ == '@'; depth++);
lexeme[j] = '@';
- islocal = 1;
lex_type = TOK_LOCAL;
if (lt || t) {
(t) ? (t->subspace = space) : (lt->subspace = space);
@@ -750,26 +936,12 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
lexeme[j] = ':';
lexeme[j+1] = '\0';
lex_type = TOK_LABEL;
- tsym = mksymbol(sym, address, 1, islocal, 0, 0, dbg);
- if (isfixup) {
- isfixup = reslv_fixups(dbg);
- }
if (lt) {
lt->id = lex_type;
- lt->type = islocal;
- lt->sym = get_sym(sym, address, t, islocal, dbg);
- isfixup += (lt->sym == NULL);
- }
- if (!islocal) {
- cur_sym = last_sym;
- locals = NULL;
- last_loc = NULL;
- } else if (cur_sym->down == NULL && cur_sym == last_sym) {
- cur_sym->down = locals;
- cur_sym->down->up = cur_sym;
+ lt->type = depth;
}
- tsym = NULL;
- islocal = 0;
+ new_symbol(lt, sym, address, depth, dbg);
+ depth = 0;
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
@@ -812,7 +984,7 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
break;
case PTOK_ALPHA:
- for (; !isdelm2(str[i+j], dbg); j++);
+ for (; !isdelm2(str[i+j], dbg) || (is_inst && str[i+j] == '.'); j++);
memcpy(lexeme, str+i, j);
lexeme[j] = '\0';
i += j;
@@ -836,6 +1008,7 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
lex_type = (is_ext) ? TOK_EXTOP : lex_type;
lex_type = (is_ortho) ? TOK_ORTHO : lex_type;
isop = 1;
+ is_inst = 1;
l->count++;
t = make_token(lex_type, 0xFF, space, tab, k, "", NULL);
break;
@@ -860,37 +1033,34 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
for (; isdelm(str[i+spaces], dbg) == 16; spaces++);
uint8_t ret = get_ptok(str[i+spaces], dbg);
if (ret == PTOK_COLON || ret == PTOK_EQU) {
- islocal = (lex_type == TOK_LOCAL);
+ depth = (lex_type == TOK_LOCAL);
}
lex_type = TOK_SYM;
l->count++;
- t = make_token(lex_type, islocal, space, tab, 0, "", NULL);
+ t = make_token(lex_type, depth, space, tab, 0, "", NULL);
memcpy(sym, lexeme, j+1);
if (dbg) {
printf("lex(): spaces: %u\n", spaces);
}
if (is_struct) {
create_struct(cur_sym, l, t, lt, sym, dbg);
- islocal = 0;
+ depth = 0;
} else if ((str[i+spaces] != ':' && str[i+spaces] != '=')) {
- uint8_t sym_struct = 0;
symbol *s;
- /*tmp_sym = (s && s->isstruct) ? NULL : tmp_sym;*/
- if (tmp_sym) {
- t->sym = find_member(lexeme, tmp_sym, dbg);
- tmp_sym = NULL;
- } else {
- t->sym = get_sym(lexeme, address, t, islocal, dbg);
- }
+ int scope_depth;
+ char *tmp = lexeme;
+ for (scope_depth = 0; *tmp; scope_depth += (*tmp++ == '.'));
+ char *scope_name = (!scope_depth) ? mk_scope_name(cur_sym, depth, lexeme, dbg) : lexeme;
+ t->sym = get_sym(scope_name, address, t, (scope_depth) ? scope_depth : depth, 1, dbg);
isfixup += (t && t->sym == NULL);
- islocal = 0;
+ depth = 0;
if (dbg) {
printf("lex(): isfixup: %u\n", isfixup);
}
}
- if (!is_struct && t && t->sym && t->sym->isstruct) {
+ /*if (!is_struct && t && t->sym && t->sym->isstruct) {
tmp_sym = t->sym;
- }
+ }*/
}
}
break;