aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-05-22 23:09:56 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-05-22 23:09:56 +0100
commit5c6d23c0fee17ded445315a0b0ed7afadbf9455f (patch)
treeb81d5888f7b830d36fd86acf2da57fa9a03d2ba0
parentdb37956d3b98b27726035286afc4dd2fd13506b5 (diff)
downloadtr8vm-5c6d23c0fee17ded445315a0b0ed7afadbf9455f.tar.gz
tr8vm-5c6d23c0fee17ded445315a0b0ed7afadbf9455f.zip
wlen wasn't needed
-rw-r--r--tr8as.c211
1 files changed, 52 insertions, 159 deletions
diff --git a/tr8as.c b/tr8as.c
index 8e9fc1b..dd2c160 100644
--- a/tr8as.c
+++ b/tr8as.c
@@ -178,32 +178,33 @@ static char * skip_whitespace(char *c)
return c;
}
-static uint8_t next_string(As *as, char **c, char *word, uint8_t *wlen)
+static uint8_t next_string(As *as, char **c, char *word)
{
+ uint8_t wlen = 0;
+
*c = skip_whitespace(*c);
- *wlen = 0;
if (**c != '"')
return 0;
(*c)++;
while (**c && **c != '"')
{
- word[(*wlen)++] = **c;
+ word[wlen++] = **c;
(*c)++;
- if (*wlen == MAX_ID)
+ if (wlen == MAX_ID)
{
- word[*wlen - 1] = 0;
- *wlen = 0;
+ word[wlen - 1] = 0;
return error_l("String is too long", &as->loc, word);
}
}
- word[*wlen] = 0;
+ word[wlen] = 0;
/* the closing quote */
- (*c)++;
+ if (**c == '"')
+ (*c)++;
- return 1;
+ return wlen > 0;
}
static uint8_t isspecial(char c)
@@ -245,25 +246,25 @@ static uint8_t new_define(As *as, char *id, char *value)
return 1;
}
-static uint8_t next_word(As *as, char **c, char *word, uint8_t *wlen)
+static uint8_t next_word(As *as, char **c, char *word)
{
+ uint8_t wlen = 0;
+
*c = skip_whitespace(*c);
- *wlen = 0;
while (**c && (isalnum(**c) || isspecial(**c)))
{
- word[(*wlen)++] = **c;
+ word[wlen++] = **c;
(*c)++;
- if (*wlen == MAX_ID)
+ if (wlen == MAX_ID)
{
- word[*wlen - 1] = 0;
- *wlen = 0;
+ word[wlen - 1] = 0;
return error_l("Invalid input (too long)", &as->loc, word);
}
}
- word[*wlen] = 0;
+ word[wlen] = 0;
- return 1;
+ return wlen > 0;
}
static uint8_t next_imm(As *as, char *word, uint16_t *n)
@@ -496,14 +497,10 @@ static uint8_t emit_imm(As *as, uint16_t imm)
static uint8_t parse_org(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
/* .org imm */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected immediate");
if (!next_imm(as, word, &as->addr))
@@ -521,7 +518,6 @@ static uint8_t parse_include(As *as, char **c)
{
/* XXX: may be support longer filenames */
char word[MAX_ID + 1];
- uint8_t wlen;
char line[MAX_LINE];
Location old;
FILE *fd;
@@ -529,10 +525,7 @@ static uint8_t parse_include(As *as, char **c)
/* .include "filename" */
- if (!next_string(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_string(as, c, word))
return error_l("Syntax error", &as->loc, "expected string");
fd = fopen(word, "rb");
@@ -570,17 +563,13 @@ static uint8_t parse_incpng(As *as, char **c)
{
/* XXX: may be support longer filenames */
char word[MAX_ID + 1];
- uint8_t wlen;
/* .incpng "filename" */
- if (!next_string(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_string(as, c, word))
return error_l("Syntax error", &as->loc, "expected string");
- if (strcasecmp(word + wlen - 4, ".png"))
+ if (strcasecmp(word + strlen(word) - 4, ".png"))
return error_l("Invalid input", &as->loc, "expected PNG file");
int x, y, n;
@@ -629,16 +618,12 @@ static uint8_t parse_incbin(As *as, char **c)
{
/* XXX: may be support longer filenames */
char word[MAX_ID + 1];
- uint8_t wlen;
FILE *fd;
size_t size;
/* .incbin "filename" */
- if (!next_string(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_string(as, c, word))
return error_l("Syntax error", &as->loc, "expected string");
fd = fopen(word, "rb");
@@ -676,20 +661,13 @@ static uint8_t parse_equ(As *as, char **c)
{
char id[MAX_ID + 1];
char value[MAX_ID + 1];
- uint8_t wlen;
/* .equ label imm */
- if (!next_word(as, c, id, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, id))
return error_l("Syntax error", &as->loc, "expected id");
- if (!next_word(as, c, value, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, value))
return error_l("Syntax error", &as->loc, "expected value");
return new_define(as, id, value);
@@ -730,17 +708,13 @@ static uint8_t parse_str(As *as, char **c)
static uint8_t parse_db(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint16_t imm = 0xff;
/* .db imm [, imm] */
while (1)
{
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected immediate");
if (next_imm(as, word, &imm))
@@ -772,17 +746,13 @@ static uint8_t parse_db(As *as, char **c)
static uint8_t parse_dw(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint16_t imm;
/* .dw imm [, imm] */
while (1)
{
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected immediate");
if (!next_imm(as, word, &imm)
@@ -811,15 +781,11 @@ static uint8_t parse_dw(As *as, char **c)
static uint8_t parse_ds(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint16_t count = 0xff, imm = 0xff;
/* .ds count, imm */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected count");
if (!next_imm(as, word, &count))
@@ -830,10 +796,7 @@ static uint8_t parse_ds(As *as, char **c)
return error_l("Syntax error", &as->loc, "expected ,");
(*c)++;
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected immediate");
if (next_imm(as, word, &imm))
@@ -919,13 +882,9 @@ static uint8_t parse_ret(As *as, char **c)
static uint8_t parse_r1_r2_or_imm(As *as, char **c, uint8_t *r1, uint8_t *r2, uint16_t *imm)
{
char word[MAX_ID + 1];
- uint8_t wlen;
/* ? r1, ? */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
*r1 = parse_register(word);
@@ -937,10 +896,7 @@ static uint8_t parse_r1_r2_or_imm(As *as, char **c, uint8_t *r1, uint8_t *r2, ui
return error_l("Syntax error", &as->loc, "expected ,");
(*c)++;
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register or immediate");
/* ? r1, r2 */
@@ -964,13 +920,9 @@ static uint8_t parse_r1_r2_or_imm(As *as, char **c, uint8_t *r1, uint8_t *r2, ui
static uint8_t parse_r1_imm(As *as, char **c, uint8_t *r1, uint16_t *imm)
{
char word[MAX_ID + 1];
- uint8_t wlen;
/* ? r1, imm */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
*r1 = parse_register(word);
@@ -982,10 +934,7 @@ static uint8_t parse_r1_imm(As *as, char **c, uint8_t *r1, uint16_t *imm)
return error_l("Syntax error", &as->loc, "expected ,");
(*c)++;
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected immediate");
if (!isdigit(*word))
@@ -1171,14 +1120,10 @@ static uint8_t parse_rol(As *as, char **c)
static uint8_t parse_push(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint8_t r1;
/* PUSH r1 */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r1 = parse_register(word);
@@ -1196,14 +1141,10 @@ static uint8_t parse_push(As *as, char **c)
static uint8_t parse_port(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint8_t r1, r2;
/* PORT r1, r2 */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r1 = parse_register(word);
@@ -1215,10 +1156,7 @@ static uint8_t parse_port(As *as, char **c)
return error_l("Syntax error", &as->loc, "expected ,");
(*c)++;
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r2 = parse_register(word);
@@ -1231,14 +1169,10 @@ static uint8_t parse_port(As *as, char **c)
static uint8_t parse_pop(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint8_t r1;
/* POP r1 */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r1 = parse_register(word);
@@ -1256,14 +1190,10 @@ static uint8_t parse_pop(As *as, char **c)
static uint8_t parse_xsp(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint8_t r1;
/* XSP r1 */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r1 = parse_register(word);
@@ -1276,14 +1206,10 @@ static uint8_t parse_xsp(As *as, char **c)
static uint8_t parse_inc(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint8_t r1;
/* INC r1 */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r1 = parse_register(word);
@@ -1296,14 +1222,10 @@ static uint8_t parse_inc(As *as, char **c)
static uint8_t parse_dec(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint8_t r1;
/* DEC r1 */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r1 = parse_register(word);
@@ -1376,13 +1298,9 @@ static uint8_t parse_bni(As *as, char **c)
static uint8_t parse_indirect(As *as, char **c, uint8_t *r1, uint8_t *r2, uint16_t *imm)
{
char word[MAX_ID + 1];
- uint8_t wlen;
/* [r1:r2] */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
*r1 = parse_register(word);
@@ -1399,10 +1317,7 @@ static uint8_t parse_indirect(As *as, char **c, uint8_t *r1, uint8_t *r2, uint16
return error_l("Syntax error", &as->loc, "expected +");
(*c)++;
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected immediate");
if (!next_imm(as, word, imm))
@@ -1427,10 +1342,7 @@ static uint8_t parse_indirect(As *as, char **c, uint8_t *r1, uint8_t *r2, uint16
return error_l("Syntax error", &as->loc, "expected :");
(*c)++;
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
*r2 = parse_register(word);
@@ -1448,7 +1360,6 @@ static uint8_t parse_indirect(As *as, char **c, uint8_t *r1, uint8_t *r2, uint16
static uint8_t parse_jmp(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint16_t imm = 0xffff;
uint8_t r1, r2;
@@ -1464,10 +1375,7 @@ static uint8_t parse_jmp(As *as, char **c)
return emit(as, 9, r1, 0, (r2 << 6) | FL);
}
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected label or immediate");
/* JMP imm */
@@ -1482,7 +1390,6 @@ static uint8_t parse_jmp(As *as, char **c)
static uint8_t parse_call(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint16_t imm = 0xffff;
uint8_t r1, r2;
@@ -1498,10 +1405,7 @@ static uint8_t parse_call(As *as, char **c)
return emit(as, 9, r1, 0, (r2 << 6));
}
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected label or immediate");
/* CALL imm */
@@ -1516,7 +1420,6 @@ static uint8_t parse_call(As *as, char **c)
static uint8_t parse_ld(As *as, char **c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint16_t imm = 0xff;
uint8_t r1, r2, r3;
@@ -1536,10 +1439,7 @@ static uint8_t parse_ld(As *as, char **c)
return error_l("Syntax error", &as->loc, "expected ,");
(*c)++;
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r3 = parse_register(word);
@@ -1553,10 +1453,7 @@ static uint8_t parse_ld(As *as, char **c)
}
/* LD r1, ? */
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register");
r1 = parse_register(word);
@@ -1583,10 +1480,7 @@ static uint8_t parse_ld(As *as, char **c)
return emit(as, 2, r2, r3, r1 << 6);
}
- if (!next_word(as, c, word, &wlen))
- return 0;
-
- if (wlen == 0)
+ if (!next_word(as, c, word))
return error_l("Syntax error", &as->loc, "expected register or immediate");
/* LD r1, r2 */
@@ -1664,7 +1558,6 @@ static InstParse insts[] =
static uint8_t parse(As *as, char *c)
{
char word[MAX_ID + 1];
- uint8_t wlen;
uint8_t i;
c = skip_whitespace(c);
@@ -1673,13 +1566,13 @@ static uint8_t parse(As *as, char *c)
if (*c == ';')
return 1;
- if (!next_word(as, &c, word, &wlen))
- return 0;
-
/* empty line */
- if (wlen == 0)
+ if (!*c)
return 1;
+ if (!next_word(as, &c, word))
+ return 0;
+
/* new label */
if (*c == ':')
{