]> j8takagi.net git repositories - yacasl2.git/commitdiff
リテラルをリテラルの一覧を表示する機能を追加
authorj8takagi <j8takagi@nifty.com>
Wed, 8 Jul 2026 12:17:51 +0000 (21:17 +0900)
committerj8takagi <j8takagi@nifty.com>
Wed, 8 Jul 2026 12:17:51 +0000 (21:17 +0900)
-l/オプションでラベル一覧を表示するときに、リテラル一覧も表示

include/assemble.h
src/assemble.c
src/label.c
test/system/casl2_smoke/hello/0.txt

index c824cff8979de65b471250b6ca6355f74561768b..08ece61ed5db81f6f6ae31281f67898db8a0c458 100644 (file)
@@ -102,6 +102,12 @@ bool addlabel(const char *prog, const char *label, WORD adr);
  */
 void printlabel();
 
+/**
+ * @brief リテラルを表示する
+ *
+ */
+void printliteral();
+
 /**
  * @brief ラベル表を解放する
  *
index e24f0a8a5a7e91baba389a77304bfb759de9912a..0762485501f5a7e906b8568a127e3359bdc5cf38 100644 (file)
@@ -66,10 +66,9 @@ void writememory(WORD word, WORD adr, PASS pass);
  * @brief 文字をメモリに書き込む
  *
  * @param *str アドレスを表す文字列。リテラル/10進定数/16進定数/アドレス定数が含まれる
- * @param literal リテラルの場合はtrue
  * @param pass アセンブラが何回目かを表す数
  */
-void writestr(const char *str, bool literal, PASS pass);
+void writestr(const char *str, PASS pass);
 
 /**
  * @brief DC命令を書込
@@ -264,6 +263,7 @@ static CERR cerr_assemble[] = {
     { 122, "cannot create hash table" },
     { 124, "more than one character in literal" },
     { 125, "not GR in operand x" },
+    { 128, "syntax error in literal" },
 };
 
 /**
@@ -342,8 +342,24 @@ WORD getliteral(const char *str, PASS pass)
 {
     assert(str[0] == '=');
     str++;
-    WORD val = (str[0] == '\'') ? (WORD)str[1] : nh2word(str);
+    WORD val = 0;
     WORD adr = 0;
+
+    if(str[0] == '\'') {
+        if(str[1] == '\'' && str[2] != '\'') {
+            setcerr(128, str);    /* syntax error in literal */
+            return 0;
+        } else if((str[1] == '\'' && str[2] == '\'' && str[3] == '\0') || str[2] == '\0' ) {
+            setcerr(123, str);    /* unclosed quote */
+            return 0;
+        } else if((str[1] == '\'' && str[2] == '\'' && str[3] != '\'') || str[2] != '\'') {
+            setcerr(124, str);    /* more than one character in literal */
+            return 0;
+        }
+        val = (WORD)str[1];
+    } else {
+        val = nh2word(str);
+    }
     /* PASS FIRSTでは、lptrがまだ確定していない(ENDで初めて確定する)ため、
      * 重複排除の登録・検索は行わず、従来通りダミーの書き込みのみ行う */
     if(pass == FIRST) {
@@ -379,29 +395,18 @@ void writememory(WORD word, WORD adr, PASS pass)
     }
 }
 
-void writestr(const char *str, bool literal, PASS pass)
+void writestr(const char *str, PASS pass)
 {
     assert(str[0] == '\'');
-    bool lw = false;
 
-    /* 「'」の場合、1文字スキップし、次の文字が「'」でなければ正常終了 */
+    /* 「'」1文字スキップし、次の文字が「'」でなければ正常終了 */
     for(int i = 1; str[i] != '\'' || str[++i] == '\''; i++) {
         /* 「'」が閉じないまま文字列が終了した場合はエラー */
         if(!str[i]) {
             setcerr(123, str);    /* unclosed quote */
             break;
         }
-        if(literal == true && lw == true) {
-            setcerr(124, str);    /* more than one character in literal */
-            break;
-        }
-        /*リテラルの場合はリテラル領域に書込 */
-        if(literal == true) {
-            writememory(str[i], (asptr->lptr)++, pass);
-            lw = true;
-        } else {
-            writememory(str[i], (asptr->ptr)++, pass);
-        }
+        writememory(str[i], (asptr->ptr)++, pass);
     }
 }
 
@@ -410,7 +415,7 @@ void writedc(const char *str, PASS pass)
     WORD adr = 0;
 
     if(*str == '\'') {
-        writestr(str, false, pass);
+        writestr(str, pass);
     } else {
         if(str[0] == '#' || isdigit(str[0]) || str[0] == '-') {
             adr = nh2word(str);
@@ -776,11 +781,15 @@ bool assemble(int filec, char *filev[], WORD adr)
                 goto asfin;
             }
         }
-        if(pass == FIRST && asmode.label == true) {
-            fprintf(stdout, "\nLabel::::\n");
-            printlabel();
-            if(asmode.onlylabel == true) {
-                break;
+        if(asmode.label == true) {
+            if(pass == FIRST) {
+                printlabel();
+                if(asmode.onlylabel == true) {
+                    break;
+                }
+            }
+            if(pass == SECOND) {
+                printliteral();
             }
         }
     }
index 385922ddc26167fe8c8bcd85d10f677d046929d5..842e34050e210b1209e054e2c11db9dc60a7670e 100644 (file)
@@ -29,6 +29,15 @@ unsigned labelhash(const char *prog, const char *label);
  */
 int compare_adr(const void *a, const void *b);
 
+/**
+ * 定義されているラベルまたはリテラルの一覧を表示する
+ *
+ * @return ラベルが同一の場合は0、異なる場合は0以外
+ *
+ * @param literal リテラルの場合はtrue、それ以外はfalse
+ */
+void printlabel_full(bool literal);
+
 /**
  * @brief ラベル数
  */
@@ -80,6 +89,37 @@ int compare_adr(const void *a, const void *b)
     return (**(LABELARRAY **)a).adr - (**(LABELARRAY **)b).adr;
 }
 
+void printlabel_full(bool literal)
+{
+    int s = 0;
+    LABELTAB *p = NULL;
+    LABELARRAY **l = NULL;
+
+    l = calloc_chk(labelcnt, sizeof(LABELARRAY *), "printlabel_full");
+    for(int i = 0; i < LABELTABSIZE; i++) {
+        for(p = labels[i]; p != NULL; p = p->next) {
+            assert(p->label->label != NULL);
+            if((literal == true && p->label->label[0] == '=') || (literal == false && p->label->label[0] != '=')) {
+                l[s++] = p->label;
+            }
+        }
+    }
+    if(s > 0) {
+        qsort(l, s, sizeof(*l), compare_adr);
+        fprintf(stdout, "\n%s::::\n", (literal == true) ? "Literal" : "Label");
+        for(int i = 0; i < s; i++) {
+            if(l[i]->prog[0]) {
+                fprintf(stdout, "%s.", l[i]->prog);
+            }
+            fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr);
+        }
+        if(literal == true) {
+            fprintf(stdout, "\n");
+        }
+    }
+    FREE(l);
+}
+
 /* assemble.hで定義された関数群 */
 void addcerrlist_label()
 {
@@ -134,25 +174,12 @@ bool addlabel(const char *prog, const char *label, WORD adr)
 
 void printlabel()
 {
-    int s = 0;
-    LABELTAB *p = NULL;
-    LABELARRAY **l = {NULL};
+    printlabel_full(false);
+}
 
-    l = calloc_chk(labelcnt, sizeof(LABELARRAY **), "labels");
-    for(int i = 0; i < LABELTABSIZE; i++) {
-        for(p = labels[i]; p != NULL; p = p->next) {
-            assert(p->label != NULL);
-            l[s++] = p->label;
-        }
-    }
-    qsort(l, s, sizeof(*l), compare_adr);
-    for(int i = 0; i < s; i++) {
-        if(l[i]->prog[0]) {
-            fprintf(stdout, "%s.", l[i]->prog);
-        }
-        fprintf(stdout, "%s ---> #%04X\n", l[i]->label, l[i]->adr);
-    }
-    FREE(l);
+void printliteral()
+{
+    printlabel_full(true);
 }
 
 void freelabel()
index b53f2dc17965cbb2a47aa5ba5ed84a04e3cec41d..4df3ce902a285b8323cbf77401cad683699bcd1f 100644 (file)
@@ -54,4 +54,9 @@ Assemble ../../../../as/sample/hello.casl (1)
 ../../../../as/sample/hello.casl:    5:LEN     DC      13
        #0020   #000D
 ../../../../as/sample/hello.casl:    6:        END
+
+Literal::::
+=000A ---> #0021
+=0001 ---> #0022
+
 Hello, World!