commit 41760436d528552d64122bb0c837f4d8274a0bdd
parent f621bed8f9bf20eca167d7cfa40840992c26da09
Author: Louis Burda <quent.burda@gmail.com>
Date: Tue, 25 Jul 2023 05:20:54 +0200
Fix label_map case insensitivity and allow numbers in label names
Diffstat:
3 files changed, 10 insertions(+), 7 deletions(-)
diff --git a/asm.c b/asm.c
@@ -6,7 +6,9 @@
#include <string.h>
#include <stdint.h>
-#define NAMEALPH "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
+#define TEXTALPH "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
+#define NUMALPH "0123456789"
+#define NAMEALPH TEXTALPH NUMALPH
#define WHITESPACE " \t\v\r\n,"
enum asm_tok {
@@ -178,14 +180,16 @@ tok_next(struct asm_tokenizer *tok)
} else if (*s == '#') {
tok->off += strlen(tok->linebuf + tok->off);
return TOK_COMMENT;
- } else if (len && strspn(s, NAMEALPH) == len-1 && s[len-1] == ':') {
+ } else if (len && strchr(TEXTALPH, *s)
+ && strspn(s, NAMEALPH) == len-1 && s[len-1] == ':') {
s[len-1] = '\0';
return TOK_LABEL;
} else if (*s == 'X' && atoi(s+1) > 0) {
return TOK_XPOS;
} else if (*s == 'Y' && atoi(s+1) > 0) {
return TOK_YPOS;
- } else if (strspn(s, NAMEALPH) == strlen(s)) {
+ } else if (strchr(TEXTALPH, *s)
+ && strspn(s, NAMEALPH) == strlen(s)) {
return TOK_NAME;
} else {
die("load: line %lu, invalid token '%s'", tok->lineno, s);
diff --git a/test/test.asm b/test/test.asm
@@ -6,9 +6,11 @@ tpu X1 Y1
end
tpu X2 Y1
+LABEL8:
mov LEFT, ACC
add 1
mov ACC, RIGHT
+ jmp LABEL8
end
tpu X3 Y1
diff --git a/tpu.c b/tpu.c
@@ -89,7 +89,7 @@ label_map_link_pos(struct label_map *map, const char *name)
struct label_map_link **link;
link = &map->buckets[djb_hash(name) % LABEL_MAP_BUCKETS];
- while (*link && strcmp((*link)->label, name))
+ while (*link && strcasecmp((*link)->label, name))
link = &(*link)->next;
return link;
@@ -99,7 +99,6 @@ bool
label_map_add(struct label_map *map, const char *name, size_t pc)
{
struct label_map_link **pos, *link;
- char *c;
pos = label_map_link_pos(map, name);
if (*pos) return false;
@@ -110,8 +109,6 @@ label_map_add(struct label_map *map, const char *name, size_t pc)
if (!link) die("malloc:");
link->label = strdup(name);
if (!link->label) die("strdup:");
- for (c = link->label; *c; c++)
- *c = (char) tolower(*c);
link->pc = pc;
link->next = NULL;