summaryrefslogtreecommitdiff
path: root/lexer.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2020-12-04 15:20:28 -0500
committermrb0nk500 <b0nk@b0nk.xyz>2020-12-04 15:20:28 -0500
commit96393257a43ac52f2b911594d106741245dec5f0 (patch)
tree6b9d11c50ed3cd1920444c4dd0ee4027814ef4bd /lexer.c
parent83ce1151ee1f06ae6b1c5c1018cc2489494e5ea4 (diff)
- Started work on writing the new version of the
assembler. - Did alot of stuff in the emulator. - Did alot of stuff in the SuB Suite.
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c186
1 files changed, 100 insertions, 86 deletions
diff --git a/lexer.c b/lexer.c
index f8280a0..9e9d2e0 100644
--- a/lexer.c
+++ b/lexer.c
@@ -228,8 +228,95 @@ line *find_line(uint32_t ln, uint8_t dbg) {
return l;
}
-uint8_t is_struct = 0;
-uint8_t is_anon = 0;
+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);
+ 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;
+ tmp_line = l;
+ } else {
+ t->id = TOK_MEMBER;
+ t->sym->isanon = (is_anon > 0);
+ }
+ isfixup += (t->sym == NULL);
+ int is_top = (c_sym == NULL);
+ c_sym = (!ismember && !c_sym) ? last_sym : c_sym;
+ if (!ismember) {
+ if (!is_top) {
+ c_sym = t->sym;
+ locals = NULL;
+ last_loc = NULL;
+ } 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 (is_anon > 0) {
+ if ((c_sym && c_sym->isanon) || (c_sym->up && !c_sym->up->isanon) || (c_sym && s_sym->isanon)) {
+ is_anon--;
+ } else if (is_struct <= 0) {
+ is_anon = 0;
+ }
+ skip = (!is_anon);
+ }
+ if (((is_struct-is_anon) > 0 && !skip) || (is_anon <= 0 && is_struct <= 0)) {
+ symbol *s;
+ for (s = locals; s; s = s->next) {
+ if (s->up == NULL) {
+ 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);
+ }
+ }
+ if (c_sym->down == NULL) {
+ c_sym->down = locals;
+ }
+ }
+ if ((is_anon <= 0 || is_struct <= 0)) {
+ for (s_sym = c_sym; s_sym->prev && !s_sym->isanon; s_sym = s_sym->prev);
+ struct_sym = s_sym;
+ }
+ if ((is_struct-is_anon) > 0 && !skip) {
+ symbol *s = c_sym;
+ for (; s->prev; s = s->prev) {
+ if (s->up == NULL && c_sym->up) {
+ 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);
+ }
+ }
+ if (c_sym->up) {
+ cur_sym = c_sym->up;
+ }
+ for (locals = locals->up; locals->prev; locals = locals->prev);
+ for (last_loc = locals; last_loc->next; last_loc = last_loc->next);
+ }
+}
uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
char sym[0x100];
@@ -321,7 +408,7 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
uint8_t ptok = get_ptok(str[i], dbg);
if (is_altok(ptok, dbg)) {
offset++;
- if ((ptok == PTOK_S && toupper(str[i+1]) == 'P') || (ptok == PTOK_P && toupper(str[i+1]) == 'C')) {
+ if (((ptok == PTOK_S && toupper(str[i+1]) == 'P') || (ptok == PTOK_P && toupper(str[i+1]) == 'C'))) {
offset++;
}
switch (get_ptok(str[i+offset], dbg)) {
@@ -331,6 +418,11 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
case PTOK_Y :
case PTOK_S :
case PTOK_P :
+ case PTOK_A :
+ case PTOK_C :
+ case PTOK_D :
+ case PTOK_F :
+ case PTOK_R :
case PTOK_ALPHA :
case PTOK_NUMBER: ptok = PTOK_ALPHA; break;
}
@@ -338,6 +430,7 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
ptok = PTOK_ALPHA;
}
}
+ /*i = ptok_handler[ptok](str, i, lex_type, l, t, dbg);*/
switch (ptok) {
case PTOK_DOT:
i++;
@@ -353,54 +446,14 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
for (j = 0; isdelm(str[i+j], dbg) & 16; j++);
uint8_t ret = get_ptok(str[i+j], dbg);
j = tmp;
+
if ((k == DIR_STRUCT || k == DIR_UNION) && (ret != PTOK_ALPHA || (is_anon && ret == PTOK_ALPHA))) {
is_anon++;
}
is_struct += (k == DIR_STRUCT || k == DIR_UNION);
is_struct -= (k == DIR_ENDSTRUCT || k == DIR_ENDUNION);
if ((k == DIR_ENDSTRUCT || k == DIR_ENDUNION)) {
- int skip = 0;
- if ((int)is_anon > 0) {
- if ((cur_sym && cur_sym->isanon) || (cur_sym->up && !cur_sym->up->isanon) || (struct_sym && struct_sym->isanon)) {
- is_anon--;
- } else if ((int)is_struct <= 0) {
- is_anon = 0;
- }
- skip = (!is_anon);
- }
- if (((int)(is_struct-is_anon) > 0 && !skip) || ((int)is_anon <= 0 && (int)is_struct <= 0)) {
- symbol *s;
- for (s = locals; s; s = s->next) {
- if (s->up == NULL) {
- s->up = cur_sym;
- }
- if (dbg) {
- printf("s: %p, s->up: %p, cur_sym: %p, last_loc: %p\n", s, s->up, cur_sym, last_loc);
- }
- }
- if (cur_sym->down == NULL) {
- cur_sym->down = locals;
- }
- }
- if (((int)is_anon <= 0 || (int)is_struct <= 0)) {
- for (struct_sym = cur_sym; struct_sym->prev && !struct_sym->isanon; struct_sym = struct_sym->prev);
- }
- if ((int)(is_struct-is_anon) > 0 && !skip) {
- symbol *s = cur_sym;
- for (; s->prev; s = s->prev) {
- if (s->up == NULL && cur_sym->up) {
- s->up = cur_sym->up;
- }
- if (dbg) {
- printf("s: %p, s->up: %p, cur_sym->up: %p, last_loc: %p\n", s, s->up, cur_sym->up, last_loc);
- }
- }
- if (cur_sym->up) {
- cur_sym = cur_sym->up;
- }
- for (locals = locals->up; locals->prev; locals = locals->prev);
- for (last_loc = locals; last_loc->next; last_loc = last_loc->next);
- }
+ end_struct(cur_sym, struct_sym, dbg);
}
break;
}
@@ -592,12 +645,9 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
lexeme[j] = str[i++];
lexeme[j+1] = '\0';
lexeme[j+2] = '\0';
- if (lex_type != TOK_IND && lex_type != TOK_CSV) {
- break;
- }
switch (ptok) {
case PTOK_E: l->tok->type = (lex_type == TOK_IND) ? EIND : l->tok->type; break;
- case PTOK_X: l->tok->type = (lex_type == TOK_IND) ? INDX : ZMX; break;
+ case PTOK_X: l->tok->type = (l->tok->type == IND) ? INDX : ZMX; break;
case PTOK_Y: l->tok->type = (lex_type == TOK_IND) ? INDY : ZMY; break;
}
break;
@@ -732,43 +782,7 @@ uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
printf("lex(): spaces: %u\n", spaces);
}
if (is_struct) {
- islocal = !(is_struct == 1 && lt && lt->id == TOK_DIR);
- mksymbol(sym, 0, 1, islocal, 0, 0, dbg);
- if (isfixup) {
- isfixup = reslv_fixups(dbg);
- }
- t->sym = get_sym(sym, 0, t, islocal, dbg);
- if (lt && lt->id == TOK_DIR) {
- t->sym->isstruct = 1;
- t->id = (lt->type == DIR_STRUCT) ? TOK_STRUCT : TOK_UNION;
- tmp_line = l;
- } else {
- t->id = TOK_MEMBER;
- t->sym->isanon = (is_anon > 0);
- }
- isfixup += (t->sym == NULL);
- int is_top = (cur_sym == NULL);
- cur_sym = (!islocal && !cur_sym) ? last_sym : cur_sym;
- if (!islocal) {
- if (!is_top) {
- cur_sym = t->sym;
- locals = NULL;
- last_loc = NULL;
- } else {
- cur_sym->down = locals;
- }
- } else {
- if (lt && lt->id == TOK_DIR) {
- if (lt->type == DIR_UNION || lt->type == DIR_STRUCT) {
- cur_sym->down = locals;
- cur_sym->down->up = cur_sym;
- last_loc->up = cur_sym;
- cur_sym = last_loc;
- locals = NULL;
- last_loc = NULL;
- }
- }
- }
+ create_struct(cur_sym, l, t, lt, sym, dbg);
islocal = 0;
} else if ((str[i+spaces] != ':' && str[i+spaces] != '=')) {
uint8_t sym_struct = 0;