変数名の整理
[YACASL2.git] / src / word.c
index 0e80452..9371758 100644 (file)
@@ -1,9 +1,22 @@
 #include "word.h"
 
+/* wordのエラー定義 */
+CERR cerr_word[] = {
+    { 114, "not integer" },
+    { 115, "not hex" },
+    { 116, "out of hex range" },
+};
+
+bool addcerrlist_word()
+{
+    return addcerrlist(ARRAYSIZE(cerr_word), cerr_word);
+}
+
 /* 10進数の文字列をWORD値に変換 */
 WORD n2word(const char *str)
 {
     assert(isdigit(*str) || *str == '-');
+
     char *check;
     int n;
     /* WORD値に変換 */
@@ -23,6 +36,7 @@ WORD n2word(const char *str)
 WORD h2word(const char *str)
 {
     assert(*str == '#');
+
     WORD word = 0x0;
     char *check;
     str++;
@@ -42,6 +56,7 @@ WORD h2word(const char *str)
 /* 10進数または16進数の文字列をWORD値に変換 */
 WORD nh2word(const char *str)
 {
+    addcerrlist_word();
     WORD word;
     if(!isdigit(*str) && *str != '-' && *str != '#') {
         setcerr(114, str);    /* not integer */
@@ -58,7 +73,7 @@ WORD nh2word(const char *str)
 /* WORD値を10進数の文字列に変換 */
 char *word2n(WORD word)
 {
-    char *p = malloc(6), *q = malloc(6);
+    char *p = malloc_chk(6, "word2n.p"), *q = malloc_chk(6, "word2n.q");
     int i = 0, j;
     do{
         *(p + i++) = word % 10 + '0';
@@ -74,8 +89,7 @@ char *word2n(WORD word)
 char *word2bit(const WORD word)
 {
     WORD mask = 0x8000;
-    char *bit, *p;
-    bit = malloc(16 + 1);
+    char *bit = malloc_chk(16 + 1, "word2bit.bit"), *p;
     p = bit;
     do {
         *p++ = (word & mask) ? '1' : '0';
@@ -83,3 +97,23 @@ char *word2bit(const WORD word)
     *p = '\0';
     return bit;
 }
+
+/* WORD値を解析して表示 */
+void print_dumpword(WORD word, bool logicalmode)
+{
+    if(logicalmode == true) {
+        fprintf(stdout, "%6d", word);
+    } else {
+        fprintf(stdout, "%6d", (signed short)word);
+    }
+    fprintf(stdout, " = #%04X = %s", word, word2bit(word));
+    /* 「文字の組」の符号表に記載された文字と、改行(CR)/タブを表示 */
+    if(word >= 0x20 && word <= 0x7E) {
+        fprintf(stdout, " = \'%c\'", word);
+    } else if(word == 0xA) {
+        fprintf(stdout, " = \'\\n\'");
+    } else if(word == '\t') {
+        fprintf(stdout, " = \'\\t\'");
+    }
+    fprintf(stdout, "\n");
+}