From: Lukas Krickl Date: Wed, 19 Mar 2025 21:40:54 +0000 (+0100) Subject: scope: Reworked begin and endscope to use icounter X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=b05a7b710287403b6eba45499bebd797e4c6d50b;p=ulas%2F.git scope: Reworked begin and endscope to use icounter --- diff --git a/TODO.md b/TODO.md index dc8ec80..228c744 100644 --- a/TODO.md +++ b/TODO.md @@ -7,4 +7,4 @@ - Add failing tests for most failure cases - Assert `.section` in full test - Add `.bank.` directive allowing users to set the current bank number (ulas.bank) which can be used for symbol files. -- Add scoped labels in macros +- Allow nested begin and end scope calls diff --git a/man/ulas.5 b/man/ulas.5 index f37da73..30b093f 100644 --- a/man/ulas.5 +++ b/man/ulas.5 @@ -155,7 +155,8 @@ The special value $ will evaluate to the current address. .SH beginscope \.beginscope - Starts a new scope + Starts a new scope. + .beginscope should not be nested! .SH endscope \.endscope diff --git a/src/ulas.c b/src/ulas.c index 36b077c..9c26509 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -2620,10 +2620,18 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) { rc = ulas_asmdirbank(dst, src, &line, n, &rc); break; case ULAS_ASMDIR_BEGIN_SCOPE: - ulas.scope++; + if (ulas.prev_scope != 0) { + ULASPANIC("nested .beginscope detected\n"); + } + ulas.prev_scope = ulas.scope; + ulas.scope = -1 - ulas_icntr(); break; case ULAS_ASMDIR_END_SCOPE: - ulas.scope--; + if (ulas.prev_scope == 0) { + ULASPANIC("calling .endscope without starting a scope\n"); + } + ulas.scope = ulas.prev_scope; + ulas.prev_scope = 0; break; case ULAS_ASMDIR_PAD: // TODO: pad is the same as .fill n, $ - n diff --git a/src/ulas.h b/src/ulas.h index 5d301c4..4fcb0fb 100644 --- a/src/ulas.h +++ b/src/ulas.h @@ -310,6 +310,9 @@ struct ulas { // current scope index // each global-label increments the scope int scope; + // temporary storage for .beginscope + // and .endscope + int prev_scope; // internal counter // used whenever a new unique number might be needed