scope: Added .beginscope and .endscope
authorLukas Krickl <lukas@krickl.dev>
Wed, 19 Mar 2025 21:28:38 +0000 (22:28 +0100)
committerLukas Krickl <lukas@krickl.dev>
Wed, 19 Mar 2025 21:28:38 +0000 (22:28 +0100)
man/ulas.5
src/ulas.c
src/ulas.h
tests/t0.bin
tests/t0.s
tests/t0_dasm.s

index f2cc9270726860e559724ea1a151bd40c5424cdb..f37da733070c859c5b9e66255c9a79bc6149600c 100644 (file)
@@ -153,6 +153,14 @@ The special value $ will evaluate to the current address.
   Sets the current bank.
   This is only used for symbol files.
 
+.SH beginscope 
+  \.beginscope
+  Starts a new scope
+
+.SH endscope
+  \.endscope
+  Returns to the previous scope
+
 .SH SEE ALSO
 
   ulas(1) for a rference on how to use ulas.
index 79d89ef5ea16806dc76bc503e70bbfed08a21716..36b077c840ebf90aa119d9d1b03d3d55255cb682 100644 (file)
@@ -394,8 +394,8 @@ int ulas_symbolout_mlbloc(FILE *dst, long addr) {
   }
 
   // fallback
-  // if no previous section was declared we fall back to the 
-  // old default output 
+  // if no previous section was declared we fall back to the
+  // old default output
   switch (ulas.arch.type) {
   case ULAS_ARCH_SM83:
     if (addr >= 0x0000 && addr <= 0x7FFF) {
@@ -442,7 +442,7 @@ int ulas_symbolout(FILE *dst, struct ulas_sym *s) {
     break;
   case ULAS_SYM_FMT_SYM:
     fprintf(dst, "%02d:", ulas.bank);
-    
+
     switch (s->tok.type) {
     case ULAS_INT:
       fprintf(dst, "%x", ulas_valint(&s->tok, &rc));
@@ -2487,8 +2487,8 @@ int ulas_asmdirsection(FILE *dst, FILE *src, const char **line,
   return 0;
 }
 
-int ulas_asmdirbank(FILE *dst, FILE *src, const char **line,
-                       unsigned long n, int *rc) {
+int ulas_asmdirbank(FILE *dst, FILE *src, const char **line, unsigned long n,
+                    int *rc) {
   ULAS_EVALEXPRS(ulas.bank = ulas_intexpr(line, strnlen(*line, n), rc));
   return *rc;
 }
@@ -2529,24 +2529,16 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
   }
 
   if (ulas.tok.buf[0] == ULAS_TOK_ASMDIR_BEGIN) {
-    const char *dirstrs[] = {ULAS_ASMSTR_ORG,
-                             ULAS_ASMSTR_SET,
-                             ULAS_ASMSTR_BYTE,
-                             ULAS_ASMSTR_STR,
-                             ULAS_ASMSTR_FILL,
-                             ULAS_ASMSTR_PAD,
-                             ULAS_ASMSTR_INCBIN,
-                             ULAS_ASMSTR_DEF,
-                             ULAS_ASMSTR_CHKSM,
-                             ULAS_ASMSTR_ADV,
-                             ULAS_ASMSTR_SET_ENUM_DEF,
-                             ULAS_ASMSTR_DEFINE_ENUM,
-                             ULAS_ASMSTR_SETCHRCODE,
-                             ULAS_ASMSTR_CHR,
-                             ULAS_ASMSTR_REP,
-                             ULAS_ASMSTR_SECTION,
-                             ULAS_ASMSTR_BANK,
-                             NULL};
+    const char *dirstrs[] = {ULAS_ASMSTR_ORG,          ULAS_ASMSTR_SET,
+                             ULAS_ASMSTR_BYTE,         ULAS_ASMSTR_STR,
+                             ULAS_ASMSTR_FILL,         ULAS_ASMSTR_PAD,
+                             ULAS_ASMSTR_INCBIN,       ULAS_ASMSTR_DEF,
+                             ULAS_ASMSTR_CHKSM,        ULAS_ASMSTR_ADV,
+                             ULAS_ASMSTR_SET_ENUM_DEF, ULAS_ASMSTR_DEFINE_ENUM,
+                             ULAS_ASMSTR_SETCHRCODE,   ULAS_ASMSTR_CHR,
+                             ULAS_ASMSTR_REP,          ULAS_ASMSTR_SECTION,
+                             ULAS_ASMSTR_BANK,         ULAS_ASMSTR_BEGIN_SCOPE,
+                             ULAS_ASMSTR_END_SCOPE,    NULL};
     enum ulas_asmdir dirs[] = {
         ULAS_ASMDIR_ORG,          ULAS_ASMDIR_SET,
         ULAS_ASMDIR_BYTE,         ULAS_ASMDIR_STR,
@@ -2555,7 +2547,9 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
         ULAS_ASMDIR_CHKSM,        ULAS_ASMDIR_ADV,
         ULAS_ASMDIR_SET_ENUM_DEF, ULAS_ASMDIR_DEFINE_ENUM,
         ULAS_ASMDIR_SETCHRCODE,   ULAS_ASMDIR_CHR,
-        ULAS_ASMDIR_REP,          ULAS_ASMDIR_SECTION, ULAS_ASMDIR_BANK};
+        ULAS_ASMDIR_REP,          ULAS_ASMDIR_SECTION,
+        ULAS_ASMDIR_BANK,         ULAS_ASMDIR_BEGIN_SCOPE,
+        ULAS_ASMDIR_END_SCOPE};
 
     enum ulas_asmdir dir = ULAS_ASMDIR_NONE;
 
@@ -2624,7 +2618,13 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
       break;
     case ULAS_ASMDIR_BANK:
       rc = ulas_asmdirbank(dst, src, &line, n, &rc);
-      break; 
+      break;
+    case ULAS_ASMDIR_BEGIN_SCOPE:
+      ulas.scope++;
+      break;
+    case ULAS_ASMDIR_END_SCOPE:
+      ulas.scope--;
+      break;
     case ULAS_ASMDIR_PAD:
       // TODO: pad is the same as .fill n, $ - n
     case ULAS_ASMDIR_NONE:
index 1260b11086efec809a66cff38555ecbe7bfcb1e8..5d301c48c4322f73abe029b2e4c3c74d28a5c598 100644 (file)
@@ -79,6 +79,8 @@ extern unsigned long defslen;
 #define ULAS_ASMSTR_REP ".rep"
 #define ULAS_ASMSTR_SECTION ".section"
 #define ULAS_ASMSTR_BANK ".bank"
+#define ULAS_ASMSTR_BEGIN_SCOPE ".beginscope"
+#define ULAS_ASMSTR_END_SCOPE ".endscope"
 
 // configurable tokens
 #define ULAS_TOK_COMMENT ';'
@@ -473,7 +475,15 @@ enum ulas_asmdir {
 
   // .bank <banknum>
   // sets the current bank number
-  ULAS_ASMDIR_BANK
+  ULAS_ASMDIR_BANK,
+
+  // .beginscope 
+  // incements scope index by 1
+  ULAS_ASMDIR_BEGIN_SCOPE,
+
+  // .endscope 
+  // decrements scope index by 1
+  ULAS_ASMDIR_END_SCOPE,
 };
 
 #define ULAS_INSTRTOKMAX 16
index aa1249716fb9bb1cc0ec9caa78676685e161935d..28305ddd4df59ae91de988c84f4b02ee06e19475 100644 (file)
Binary files a/tests/t0.bin and b/tests/t0.bin differ
index 77e90b1c3a2c8c9c0af845b281e24e102bdd8529..41c83d86ff7b3ffa3dbd75cd6297b2f7dee4764c 100644 (file)
@@ -182,3 +182,23 @@ toplevel 5, 6
 #endmacro 
 
 defarg DEFARG, 2
+
+test_scoped:
+  nop
+  jp @redef
+  jp @postdef
+@redef:
+.beginscope
+  jp @redef
+  jp @postdef
+@postdef:
+@redef:
+  nop
+  nop
+.endscope
+  jp @redef
+  jp @postdef
+@postdef:
+@behin_scope:
+  nop
+
index bddc3eff18f0812f28aed49612590ddcbc8f6c1d..3a4aa2bd6d1514a045fbffda3ea90869dd5ad0c2 100644 (file)
   halt 
   ld [bc], a
   ld [bc], a
-.db 0x1
+  ld bc, 0xc300
+.db 0xe4
+  ld bc, 0xf2c3
+  ld bc, 0xeac3
+  ld bc, 0xeac3
+  ld bc, 0x0
+  jp 0x1e4
+  jp 0x1f2
+.db 0x0