summaryrefslogtreecommitdiffstats
path: root/asm.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2023-07-25 01:59:25 +0200
committerLouis Burda <quent.burda@gmail.com>2023-07-25 01:59:25 +0200
commite32b439cfe7ca72098b2ebe9e1e93f97b7c10ada (patch)
tree3b7f06a2f897dd786b5ff2d01fa13d57057d33da /asm.c
parent40d8eb449ed072b47bfbe953a191708f761c53a0 (diff)
downloadtis100-e32b439cfe7ca72098b2ebe9e1e93f97b7c10ada.tar.gz
tis100-e32b439cfe7ca72098b2ebe9e1e93f97b7c10ada.zip
Add inotify-based interactive file reload and fix asm parsing
Diffstat (limited to 'asm.c')
-rw-r--r--asm.c59
1 files changed, 35 insertions, 24 deletions
diff --git a/asm.c b/asm.c
index f08a515..eca2c69 100644
--- a/asm.c
+++ b/asm.c
@@ -85,12 +85,11 @@ tok_to_optype(enum asm_tok tok)
static size_t
strlcat_op_name(char *buf, struct tpu_inst_op *op, size_t n)
{
- size_t len;
+ char hhbuf[4];
if (op->type == OP_LIT) {
- len = strlen(buf);
- snprintf(buf + len, n - len, "%hhu", op->lit);
- return op->lit;
+ snprintf(hhbuf, 4, "%hhu", op->lit);
+ return strdcat(buf, hhbuf, n);
} else if (op->type == OP_LABEL) {
return strdcat(buf, op->label, n);
} else {
@@ -130,7 +129,7 @@ tok_next(struct asm_tokenizer *tok)
len = strlen(s);
if (len && s[len-1] != '\n' && !feof(tok->file))
- die("tis_load: line %lu too long", tok->lineno);
+ die("load: line %lu too long", tok->lineno);
if (len && s[len-1] == '\n') s[len-1] = '\0';
tok->lineno += 1;
@@ -142,9 +141,10 @@ tok_next(struct asm_tokenizer *tok)
s = tok->linebuf + tok->off;
len = strspn(s, WHITESPACE);
tok->off += len;
- s += len;
- tok->tokstr = s;
- if (!*s) return TOK_NL;
+ if (!s[len]) return TOK_NL;
+ tok->tokstr = (s += len);
+
+ if (*s == ',') die("load: line %lu, misaligned comma", tok->lineno);
len = strcspn(s, WHITESPACE ",");
tok->off += len;
@@ -153,6 +153,8 @@ tok_next(struct asm_tokenizer *tok)
tok->off += 1;
}
+ //printf("> %s\n", tok->tokstr);
+
if (!strcasecmp(s, "stdin")) {
return TOK_STDIN;
} else if (!strcasecmp(s, "stdout")) {
@@ -219,7 +221,7 @@ tok_next(struct asm_tokenizer *tok)
} else if (strspn(s, NAMEALPH) == strlen(s)) {
return TOK_NAME;
} else {
- die("tis_load: line %lu, invalid token '%s'", tok->lineno, s);
+ die("load: line %lu, invalid token '%s'", tok->lineno, s);
}
}
@@ -274,12 +276,15 @@ tis_load(struct tis *tis, const char *filepath)
char *label;
size_t i;
+ tis_deinit(tis);
+ tis_init(tis);
+
stdin_x = stdin_y = 0;
stdout_x = stdout_y = 0;
tokenizer.filepath = filepath;
tokenizer.file = fopen(filepath, "r");
- if (!tokenizer.file) die("tis_load: fopen '%s':", filepath);
+ if (!tokenizer.file) die("load: fopen '%s':", filepath);
tokenizer.lineno = 0;
tokenizer.off = 0;
@@ -331,7 +336,9 @@ tis_load(struct tis *tis, const char *filepath)
TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN,
TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, TOK_NL, -1);
if (op1_tok == TOK_NL) {
- tpu_add_inst(tpu, inst, -1, 0, -1, 0);
+ if (!tpu_add_inst(tpu, inst, -1, 0, -1, 0))
+ die("load: line %lu, invalid instruction",
+ tokenizer.lineno);
break;
}
@@ -343,17 +350,21 @@ tis_load(struct tis *tis, const char *filepath)
TOK_NIL, TOK_LEFT, TOK_RIGHT, TOK_UP, TOK_DOWN,
TOK_ANY, TOK_LAST, TOK_LIT, TOK_NAME, TOK_NL, -1);
if (op2_tok == TOK_NL) {
- tpu_add_inst(tpu, inst, (int) op1,
- op1_lit, -1, 0);
+ if (!tpu_add_inst(tpu, inst, (int) op1,
+ op1_lit, -1, 0))
+ die("load: line %lu, invalid instruction",
+ tokenizer.lineno);
break;
}
op2 = tok_to_optype(op2_tok);
if (op2 == OP_LIT)
op2_lit = str_to_lit(tokenizer.tokstr);
+ if (!tpu_add_inst(tpu, inst, (int) op1, op1_lit,
+ (int) op2, op2_lit))
+ die("load: line %lu, invalid instruction",
+ tokenizer.lineno);
tok_next_in(&tokenizer, TOK_NL, -1);
- tpu_add_inst(tpu, inst, (int) op1, op1_lit,
- (int) op2, op2_lit);
break;
case TOK_COMMENT:
tok_next_in(&tokenizer, TOK_NL, -1);
@@ -361,7 +372,7 @@ tis_load(struct tis *tis, const char *filepath)
case TOK_LABEL:
label = strdup(tokenizer.tokstr);
if (!label_map_add(&tpu->labels, label, tpu->inst_cnt))
- die("tis_load: line %lu, duplicate label",
+ die("load: line %lu, duplicate label",
tokenizer.lineno);
tok_next_in(&tokenizer, TOK_NL, -1);
break;
@@ -373,10 +384,10 @@ tis_load(struct tis *tis, const char *filepath)
}
if (stdin_x == 0 || stdin_y == 0)
- die("tis_load: stdin tpu not set");
+ die("load: stdin tpu not set");
if (stdout_x == 0 || stdout_y == 0)
- die("tis_load: stdout tpu not set");
+ die("load: stdout tpu not set");
for (i = 0; i < TPU_MAP_BUCKETS; i++) {
for (link = tis->tpu_map.buckets[i]; link; link = link->next) {
@@ -385,7 +396,7 @@ tis_load(struct tis *tis, const char *filepath)
if (link->x == stdin_x && link->y == stdin_y) {
port = &link->tpu->ports[DIR_UP];
if (port->attached)
- die("tis_load: stdin port in use");
+ die("load: stdin port in use");
port->attached = true;
port->dst_tpu = NULL;
port->dst_port = &tis->stdin_port;
@@ -398,7 +409,7 @@ tis_load(struct tis *tis, const char *filepath)
if (link->x == stdout_x && link->y == stdout_y) {
port = &link->tpu->ports[DIR_DOWN];
if (port->attached)
- die("tis_load: stdout port in use");
+ die("load: stdout port in use");
port->attached = true;
port->dst_tpu = NULL;
port->dst_port = &tis->stdout_port;
@@ -411,11 +422,11 @@ tis_load(struct tis *tis, const char *filepath)
}
if (!tis->stdin_port.attached)
- die("tis_load: stdin tpu (X%i Y%i) not found",
+ die("load: stdin tpu (X%i Y%i) not found",
stdin_x, stdin_y);
if (!tis->stdout_port.attached)
- die("tis_load: stdout tpu (X%i Y%i) not found",
+ die("load: stdout tpu (X%i Y%i) not found",
stdout_x, stdout_y);
fclose(tokenizer.file);
@@ -424,10 +435,10 @@ tis_load(struct tis *tis, const char *filepath)
disallowed:
if (tok == TOK_NAME) {
- die("tis_load: line %lu, unexpected token '%s'",
+ die("load: line %lu, unexpected token '%s'",
tokenizer.lineno, tokenizer.tokstr);
} else {
- die("tis_load: line %lu, token %s not allowed here",
+ die("load: line %lu, token %s not allowed here",
tokenizer.lineno, tok_reprs[tok]);
}
}