summaryrefslogtreecommitdiff
path: root/lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c88
1 files changed, 43 insertions, 45 deletions
diff --git a/lexer.c b/lexer.c
index 699589c..63eef47 100644
--- a/lexer.c
+++ b/lexer.c
@@ -118,22 +118,27 @@ uint16_t reslv_fixups(uint8_t dbg) {
uint16_t get_comment(const char *com, uint8_t dbg) {
uint16_t i = 0;
- uint8_t iscom = 0;
- for (; i < comidx; i++) {
- if (comment[i] == NULL || iscom) {
+ for (; comment[i] && i < comidx; i++) {
+ if (com[0] == comment[i][0] && !strcmp(com, comment[i])) {
break;
- } else if (com[0] == comment[i][0]) {
- iscom = !strcmp(com, comment[i]);
}
}
- if (comment[i] == NULL || i == comidx) {
+ if (comment[i] == NULL) {
if (dbg) {
printf("get_comment(): oof, the index $%04X is NULL.\n", i);
printf("get_comment(): oof, the comment \"%s\", was not found in the comment table.\n", com);
}
- return 0xFFFF;
+ size_t size = strlen(com)+1;
+ comment[comidx] = malloc(size);
+ memcpy(comment[comidx], com, size);
+ return comidx++;
+
}
if (dbg) {
+ if (strcmp(com, comment[i])) {
+ printf("get_comment(): oof, the comment \"%s\" is somehow not in the comment table, even though it should be at index $%04X.\n", com, i);
+ }
+ printf("get_comment(): The return value of strcmp(com, comment[$%04X]) is %i.\n", i, strcmp(com, comment[i]));
printf("get_comment(): Found comment \"%s\", in the table, at index $%04X.\n", com, i);
}
return i;
@@ -182,7 +187,7 @@ line *find_line(uint32_t ln, uint8_t dbg) {
return l;
}
-uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
+uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg) {
char sym[0x100];
uint16_t i = 0;
uint16_t j = 0;
@@ -212,9 +217,11 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
uint8_t space = 0;
uint8_t tab = 0;
- uint8_t isstart = 1;
uint8_t fall = 0;
uint8_t done = 0;
+
+
+ /*uint8_t is_newcom = 0;*/
line *l = NULL;
token *st = NULL;
token *t = NULL;
@@ -241,10 +248,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
l->tok = NULL;
l->next = NULL;
l->count = 0;
- l->espace = 0;
- l->etab = 0;
- l->sspace = 0;
- l->stab = 0;
+ l->bline = bline;
last_line = l;
}
@@ -263,13 +267,6 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
if (dbg) {
printf("lex(): tab: %u, space: %u\n", tab, space);
}
- if (isstart) {
- l->stab = tab;
- l->sspace = space;
- if (dbg) {
- printf("lex(): starting tabs: %u, starting spaces: %u\n", l->stab, l->sspace);
- }
- }
if (isdelm(str[i], dbg) == 16) {
for (; isdelm(str[i], dbg) == 16; i++);
}
@@ -306,7 +303,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
}
l->count++;
- t = make_token(lex_type, k, 0, "", NULL);
+ t = make_token(lex_type, k, space, tab, 0, "", NULL);
} else {
lex_type = TOK_RS;
switch (tolower(lexeme[j-1])) {
@@ -324,7 +321,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
break;
}
l->count++;
- t = make_token(lex_type, rs, 0, "", NULL);
+ t = make_token(lex_type, rs, space, tab, 0, "", NULL);
isop = 0;
}
break;
@@ -353,7 +350,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
lex_type = TOK_STRING;
l->count++;
- t = make_token(lex_type, 0, 0, string[strid], NULL);
+ t = make_token(lex_type, 0, space, tab, 0, string[strid], NULL);
break;
case PTOK_DOLLAR:
case PTOK_PERCENT:
@@ -385,7 +382,8 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
}
l->count++;
- t = make_token(lex_type, 0, value, "", NULL);
+ t = make_token(lex_type, 0, space, tab, value, "", NULL);
+ t->digits = (lt->id != TOK_SYM) ? j : 0;
break;
case PTOK_SQUOTE:
i++;
@@ -413,7 +411,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
lex_type = TOK_CHAR;
l->count++;
- t = make_token(lex_type, 0, ch, "", NULL);
+ t = make_token(lex_type, 0, space, tab, ch, "", NULL);
break;
case PTOK_LBRACK:
case PTOK_HASH :
@@ -421,6 +419,8 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lex_type = (ptok == PTOK_LBRACK) ? TOK_IND : TOK_IMM;
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:
@@ -436,7 +436,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
case PTOK_LT : value = (get_ptok(str[i+1], dbg) == PTOK_LT) ? (EXPR_LSHFT) : (EXPR_HIGH); break;
}
l->count++;
- t = make_token(lex_type, value, 0, "", NULL);
+ t = make_token(lex_type, value, space, tab, 0, "", NULL);
memset(lexeme, 0, strlen(lexeme)+1);
lexeme[j++] = str[i];
if (value == EXPR_LSHFT || value == EXPR_RSHFT) {
@@ -448,6 +448,8 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lex_type = TOK_SYM;
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_RBRACK:
i++;
@@ -471,6 +473,8 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j+2] = '\0';
lex_type = TOK_BREG;
l->tok->type = BREG;
+ (t) ? (t->subspace = space) : (lt->subspace = space);
+ (t) ? (t->subtab = tab) : (lt->subtab = tab);
break;
case PTOK_X:
case PTOK_Y:
@@ -499,13 +503,17 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
case PTOK_P: of = 2; break;
}
lex_type = TOK_OF;
- t = make_token(lex_type, of, 0, "", NULL);
+ t = make_token(lex_type, of, space, tab, 0, "", NULL);
break;
case PTOK_AT:
memset(lexeme, 0, strlen(lexeme)+1);
lexeme[j] = '@';
islocal = 1;
lex_type = TOK_LOCAL;
+ if (lt || t) {
+ (t) ? (t->subspace = space) : (lt->subspace = space);
+ (t) ? (t->subtab = tab) : (lt->subtab = tab);
+ }
break;
case PTOK_COLON:
i++;
@@ -547,18 +555,18 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lexeme[j] = '\0';
i += j;
comid = get_comment(lexeme, dbg);
+ /*is_newcom = (comid == 0xFFFF);
if (comid == 0xFFFF) {
- /*if (line != lineidx && l[line].com != 0xFFFF) {
+ if (line != lineidx && l[line].com != 0xFFFF) {
comid = l[line].com;
} else {
comid = comidx;
- }*/
+ }
comid = comidx;
comment[comid] = malloc(j+1);
memcpy(comment[comid], lexeme, j+1);
comidx++;
- } else {
- }
+ }*/
if (dbg) {
printf("lex(): com[0x%04X]: %s\n", comid, comment[comid]);
}
@@ -566,9 +574,9 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lex_type = TOK_COMMENT;
l->count++;
if (j) {
- t = make_token(lex_type, 0, 0, comment[comid], NULL);
+ t = make_token(lex_type, 0, space, tab, 0, comment[comid], NULL);
} else {
- t = make_token(lex_type, 0, 0, "" , NULL);
+ t = make_token(lex_type, 0, space, tab, 0, "" , NULL);
}
break;
@@ -586,7 +594,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
lex_type = TOK_OPCODE;
isop = 1;
l->count++;
- t = make_token(lex_type, 0xFF, k, "", NULL);
+ t = make_token(lex_type, 0xFF, space, tab, k, "", NULL);
break;
}
}
@@ -600,7 +608,7 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
}
lex_type = TOK_SYM;
l->count++;
- t = make_token(lex_type, islocal, 0, "", NULL);
+ t = make_token(lex_type, islocal, space, tab, 0, "", NULL);
memcpy(sym, lexeme, j+1);
if (dbg) {
printf("lex(): spaces: %u\n", spaces);
@@ -622,22 +630,12 @@ uint64_t lex(char *str, uint64_t address, uint8_t dbg) {
if (dbg) {
printf("lex(): lexeme: %s, lex_type: %s\n", lexeme, (lex_type != 0xFF) ? lex_tok[lex_type] : "TOK_NONE");
}
- isstart = 0;
j = 0;
if (lex_type == TOK_OPCODE && !isop) {
j = 0;
} else if (lex_type == TOK_EXPR || !isdelm2(str[i], dbg)) {
i++;
}
- if (lex_type == TOK_COMMENT) {
- if (!isstart) {
- l->etab = tab;
- l->espace = space;
- if (dbg) {
- printf("lex(): ending tabs: %u, ending spaces: %u\n", l->etab, l->espace);
- }
- }
- }
switch (lex_type) {
default:
lex_type = 0xFF;