From 65637862923b39237bc860d0aa9c420dda6be4ab Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Wed, 28 Feb 2024 10:45:23 +0100 Subject: [PATCH] Added unnamed labels Unnamed labels can be declared using just a :. Unnamed labels ignore redefinition rules and just get added to the symbol list regardless. They currently only serve as markers, however in the future they can be reached using + and - symbols to jump to the next or previous label respectively. --- src/ulas.c | 17 ++++++++++++----- src/ulas.h | 2 -- tests/t0.bin | Bin 215 -> 216 bytes tests/t0.s | 5 +++++ tests/t0_dasm.s | 1 + 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/ulas.c b/src/ulas.c index a5783c8..917858c 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -265,7 +265,7 @@ int ulas_isname(const char *tok, unsigned long n) { } int ulas_islabelname(const char *tok, unsigned long n) { - return tok[n - 1] == ':' && ulas_isname(tok, n - 1); + return tok[n - 1] == ':' && (ulas_isname(tok, n - 1) || n == 1); } struct ulas_sym *ulas_symbolresolve(const char *name, int scope, int *rc) { @@ -311,7 +311,7 @@ int ulas_symbolset(const char *cname, int scope, struct ulas_tok tok, ulas.scope++; } - if (!existing) { + if (!existing || (name[0] == '\0' && len == 1)) { // def new symbol struct ulas_sym new_sym = {strndup(name, len), tok, scope, ulas.pass, constant}; @@ -340,7 +340,11 @@ int ulas_symbolout(FILE *dst, struct ulas_sym *s) { } int rc = 0; - fprintf(dst, "%s = ", s->name); + if (!s->name || s->name[0] == '\0') { + fprintf(dst, " = "); + } else { + fprintf(dst, "%s = ", s->name); + } switch (s->tok.type) { case ULAS_INT: fprintf(dst, "0x%x", ulas_valint(&s->tok, &rc)); @@ -1470,7 +1474,8 @@ int ulas_parsecmp(int *i); int ulas_parseun(int *i) { struct ulas_tok *t = ulas_tokbufget(&ulas.toks, *i); - if (t && (t->type == '!' || t->type == '-' || t->type == '~' || t->type == '+')) { + if (t && + (t->type == '!' || t->type == '-' || t->type == '~' || t->type == '+')) { int op = *i; *i += 1; int right = ulas_parseun(i); @@ -2336,7 +2341,9 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { if (ulas_islabelname(ulas.tok.buf, strlen(ulas.tok.buf))) { instr_start = line; struct ulas_tok label_tok = {ULAS_INT, {(int)ulas.address}}; - ulas_symbolset(ulas.tok.buf, -1, label_tok, 1); + if (ulas_symbolset(ulas.tok.buf, -1, label_tok, 1) == -1) { + return -1; + } ulas_tok(&ulas.tok, &line, n); // is next token empty? if (ulas_istokend(&ulas.tok)) { diff --git a/src/ulas.h b/src/ulas.h index fe29e7f..8c58880 100644 --- a/src/ulas.h +++ b/src/ulas.h @@ -248,7 +248,6 @@ struct ulas_preproc { struct ulas_str macrobuf; }; - struct ulas { struct ulas_preproc pp; char *filename; @@ -427,7 +426,6 @@ enum ulas_asmdir { ULAS_ASMDIR_REP, }; - #define ULAS_INSTRTOKMAX 16 #define ULAS_INSTRDATMAX 16 diff --git a/tests/t0.bin b/tests/t0.bin index 371ef142ee4dd29c40eb82131274098a23b74568..4f18f9a43542ebf2402d537bcb4dba529ac78012 100644 GIT binary patch delta 8 Pcmcc4c!P1mb;dFP5g7xJ delta 6 Ncmcb?c%5;=bpQ%+0~r7S diff --git a/tests/t0.s b/tests/t0.s index fe5ffc8..18f76f8 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -155,3 +155,8 @@ nextlevel $2 #endmacro toplevel 5, 6 + +: +: + halt + diff --git a/tests/t0_dasm.s b/tests/t0_dasm.s index a83a590..0ea1f10 100644 --- a/tests/t0_dasm.s +++ b/tests/t0_dasm.s @@ -120,3 +120,4 @@ dec b inc b .db 0x6 +.db 0x76 -- 2.30.2