summaryrefslogtreecommitdiff
path: root/lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c60
1 files changed, 42 insertions, 18 deletions
diff --git a/lexer.c b/lexer.c
index 14bf1c4..19a53ce 100644
--- a/lexer.c
+++ b/lexer.c
@@ -6,11 +6,12 @@ uint16_t sym_count = 0;
token *tokens = NULL;
token *last_tok = NULL;
symbol *locals = NULL;
+symbol *last_loc = NULL;
symbol *cur_sym = 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 || islocal == 2) ? symbols : locals;
+ symbol *s = (!islocal) ? symbols : locals;
uint8_t flag = 0;
for (; s; s = s->next, i++) {
if (!useid && name[0] != s->name[0]) {
@@ -39,20 +40,29 @@ symbol *mksymbol(const char *name, uint64_t val, uint8_t def, uint8_t islocal, u
}
}
size_t str_size = strlen(name)+1;
- s = malloc(sizeof(*s));
+ s = malloc(sizeof(symbol));
+ s->local = NULL;
+ if (!islocal) {
+ (last_sym) ? (last_sym->next = s) : (symbols = s);
+ } else {
+ (last_loc) ? (last_loc->next = s) : (locals = s);
+ }
s->name = malloc(str_size);
s->def = def;
s->val = val;
s->count = 0;
memcpy(s->name, name, str_size);
- s->next = (!islocal) ? symbols : locals;
+ s->next = NULL;
s->id = sym_count++;
+ (!islocal) ? (last_sym = s) : (last_loc = s);
if (!islocal) {
s->local = NULL;
- symbols = s;
+ /*if (def) {
+ locals = NULL;
+ last_loc = NULL;
+ }*/
} else {
cur_sym->count++;
- locals = s;
}
defined = 0;
if (dbg) {
@@ -73,12 +83,13 @@ symbol *get_sym(const char *name, uint64_t val, token *t, uint8_t islocal, uint8
if (dbg) {
printf("get_sym(): oof, symbol %s, does not exist, yet.\n", name);
}
- fixup *f = malloc(sizeof(*f));
- f->next = fixups;
+ fixup *f = malloc(sizeof(fixup));
+ (last_fix) ? (last_fix->next = f) : (fixups = f);
f->adr = val;
f->t = t;
f->s = s;
- fixups = f;
+ f->next = NULL;
+ last_fix = f;
fixup_cnt++;
return NULL;
}
@@ -263,8 +274,14 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
offset++;
if ((ptok == PTOK_S && toupper(str[i+1]) == 'P') || (ptok == PTOK_P && toupper(str[i+1]) == 'C')) {
offset++;
+ } else if (ptok == PTOK_S || ptok == PTOK_P) {
+
}
switch (get_ptok(str[i+offset], dbg)) {
+ case PTOK_X :
+ case PTOK_Y :
+ case PTOK_S :
+ case PTOK_P :
case PTOK_ALPHA :
case PTOK_NUMBER: ptok = PTOK_ALPHA; break;
}
@@ -277,7 +294,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
i += j;
if (!isop) {
- for (k = 0; k < 6; k++) {
+ for (k = 0; k < 7; k++) {
if (tolower(lexeme[0]) == dir_t[k][0] && !strcasecmp(lexeme, dir_t[k])) {
lex_type = TOK_DIR;
break;
@@ -353,8 +370,8 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lt->sym = get_sym(sym, address, lt, islocal, dbg);
}
if (!islocal) {
- cur_sym = symbols;
- locals = cur_sym->local;
+ cur_sym = last_sym;
+ /*last_loc = NULL;*/
}
islocal = 0;
isfixup += (lt->sym == NULL);
@@ -489,8 +506,12 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
isfixup += (lt->sym == NULL);
}
if (!islocal) {
- cur_sym = symbols;
- locals = cur_sym->local;
+ cur_sym = last_sym;
+ cur_sym->local = NULL;
+ locals = NULL;
+ last_loc = NULL;
+ } else if (cur_sym->local == NULL) {
+ cur_sym->local = locals;
}
islocal = 0;
if (dbg) {
@@ -535,10 +556,10 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
break;
case PTOK_ALPHA:
- while (!isdelm2(str[i], dbg)) {
- lexeme[j++] = str[i++];
- }
+ for (; !isdelm2(str[i+j], dbg); j++);
+ memcpy(lexeme, str+i, j);
lexeme[j] = '\0';
+ i += j;
isch = 0;
isop = 0;
if (j == 3 && str[i] != ':') {
@@ -555,12 +576,15 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
}
if (!isop) {
+ uint8_t spaces = 0;
+ for (; isdelm(str[i+spaces], dbg) == 16; spaces++);
+ if (get_ptok(str[i+spaces], dbg) == PTOK_COLON) {
+ islocal = (lex_type == TOK_LOCAL);
+ }
lex_type = TOK_SYM;
l->count++;
t = make_token(lex_type, islocal, 0, "", NULL);
memcpy(sym, lexeme, j+1);
- uint8_t spaces = 0;
- for (; isdelm(str[i+spaces], dbg) == 16; spaces++);
if (dbg) {
printf("lex(): spaces: %u\n", spaces);
}