From: j8takagi Date: Sun, 28 Feb 2010 14:35:33 +0000 (+0900) Subject: エラー処理にリストを使うよう仕様変更 X-Git-Tag: v0.1~6 X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=commitdiff_plain;h=3ea92ba68cb320156d5bc7fe92f5e5fa0c150635 エラー処理にリストを使うよう仕様変更 --- diff --git a/include/cerr.h b/include/cerr.h index 828d03e..f651c7d 100644 --- a/include/cerr.h +++ b/include/cerr.h @@ -16,20 +16,29 @@ extern int cerrno; /* エラーメッセージ */ extern char *cerrmsg; -/* エラーコードリスト */ +/* エラーコード配列 */ typedef struct { int num; char *msg; } CERRARRAY; +/* エラーコードリスト */ +typedef struct _CERRLIST { + struct _CERRLIST *next; + CERRARRAY *err; +} CERRLIST; + /* エラーメッセージ */ -extern CERRARRAY cerr[]; +extern CERRLIST *cerr; enum { CERRSTRSIZE = 10, /* エラーメッセージ中に挿入できる文字列のサイズ */ CERRMSGSIZE = 70, /* エラーメッセージのサイズ */ }; +/* エラーを追加する */ +void addcerrlist(int cerrc, CERRARRAY cerrv[]); + /* エラー番号とエラーメッセージを設定 */ void setcerr(int num, const char *str); diff --git a/src/assemble.c b/src/assemble.c index 616b256..50554a7 100644 --- a/src/assemble.c +++ b/src/assemble.c @@ -65,7 +65,7 @@ bool writememory(WORD word, WORD adr, PASS pass) } if(cerrno == 0) { memory[adr] = word; - if(pass == SECOND && (&asmode)->asdetail == true) { + if(pass == SECOND && asmode.asdetail == true) { fprintf(stdout, "\t#%04X\t#%04X\n", adr, word); } status = true; @@ -423,8 +423,8 @@ bool assemble(const char *file, PASS pass) break; } lineno++; - if((pass == FIRST && (&asmode)->src == true) || - (pass == SECOND && (&asmode)->asdetail == true)) + if((pass == FIRST && asmode.src == true) || + (pass == SECOND && asmode.asdetail == true)) { printline(stdout, file, lineno, line); } diff --git a/src/casl2.c b/src/casl2.c index 03c61fc..9753c83 100644 --- a/src/casl2.c +++ b/src/casl2.c @@ -24,7 +24,7 @@ static struct option longopts[] = { }; /* エラー番号とエラーメッセージ */ -CERRARRAY cerr[] = { +CERRARRAY cerr_casl2[] = { { 101, "label already defined" }, { 102, "label table is full" }, { 103, "label not found" }, @@ -58,7 +58,6 @@ CERRARRAY cerr[] = { { 205, "Stack Pointer (SP) - cannot allocate stack buffer" }, { 206, "Address - out of COMET II memory" }, { 207, "Stack Pointer (SP) - out of COMET II memory" }, - { 0, NULL }, }; /* 指定されたファイルにアセンブル結果を書込 */ @@ -145,6 +144,8 @@ int main(int argc, char *argv[]) exit(-1); } } + /* エラーリストにerr_casl2を追加 */ + addcerrlist(ARRAYSIZE(cerr_casl2), cerr_casl2); /* ソースファイルが指定されていない場合は終了 */ if(argv[optind] == NULL) { setcerr(126, NULL); /* source file is not specified */ @@ -196,6 +197,6 @@ int main(int argc, char *argv[]) } return 0; casl2err: - fprintf(stderr, "Casl2 error - %d: %s\n", cerrno, cerrmsg); + fprintf(stderr, "CASL2 error - %d: %s\n", cerrno, cerrmsg); exit(-1); } diff --git a/src/cerr.c b/src/cerr.c index 1aeb8c4..662cc66 100644 --- a/src/cerr.c +++ b/src/cerr.c @@ -1,35 +1,66 @@ #include "cerr.h" -/* エラーメッセージ */ +/* エラー番号 */ int cerrno = 0; + +/* エラーメッセージ */ char *cerrmsg; +/* エラーリスト */ +CERRLIST *cerr; + +/* エラーリストを作成する */ +void addcerrlist(int newerrc, CERRARRAY newerrv[]) +{ + int i; + CERRLIST *p = cerr, *q; + + if(cerr != NULL) { + for(p = cerr; p != NULL; p = p->next) { + ; + } + if((p = malloc(sizeof(CERRLIST *))) == NULL) { + fprintf(stderr, "addcerrlist: cannot allocate memory\n"); + exit(-1); + } + } else if((p = cerr = malloc(sizeof(CERRLIST *))) == NULL) { + fprintf(stderr, "addcerrlist: cannot allocate memory\n"); + exit(-1); + } + for(i = 0; i < newerrc; i++) { + p->err = &(newerrv[i]); + if((p->next = malloc(sizeof(CERRLIST *))) == NULL) { + fprintf(stderr, "addcerrlist: cannot allocate memory\n"); + exit(-1); + } + q = p; + p = p->next; + } + q->next = NULL; +} + /* エラー番号とエラーメッセージを設定する */ void setcerr(int num, const char *str) { - assert(cerr != NULL && num > 0); - cerrno = num; cerrmsg = malloc(CERRMSGSIZE + 1); - if(str != NULL && strlen(str) < 10) { + if(str != NULL && strlen(str) <= CERRSTRSIZE) { sprintf(cerrmsg, "%s: %s", str, getcerrmsg(cerrno)); } else { strcpy(cerrmsg, getcerrmsg(cerrno)); } } -/* エラー番号からメッセージを返す */ +/* リストから、エラー番号に対応するメッセージを返す */ char *getcerrmsg(int num) { - assert(cerr != NULL && num > 0); - int i = 0; - CERRARRAY *ptr; + CERRLIST *p; - do { - if((ptr = &cerr[i++])->num == num) { - return ptr->msg; + for(p = cerr; p != NULL; p = p->next) { + if(num == p->err->num) { + return p->err->msg; } - } while(ptr->num > 0); + } return "unkown error"; } diff --git a/src/cmd.c b/src/cmd.c index 6bb749f..c108b46 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -71,13 +71,12 @@ bool create_cmdtype_code() cmdtabsize = cmdcodesize; cmdtype_code = malloc(cmdtabsize * sizeof(CMDCODETAB *)); for(i = 0; i < cmdcodesize; i++) { - np = malloc(sizeof(CMDCODETAB)); - if(np == NULL) { + if((np = malloc(sizeof(CMDCODETAB))) == NULL) { setcerr(122, NULL); /* cannot create hash table */ return false; } /* ハッシュ値の生成 */ - hashval = hash_cmdtype((&cmdcodearray[i])->cmd, (&cmdcodearray[i])->type); + hashval = hash_cmdtype(cmdcodearray[i].cmd, cmdcodearray[i].type); /* ハッシュ表に値を追加 */ np->next = cmdtype_code[hashval]; cmdtype_code[hashval] = np; diff --git a/src/comet2.c b/src/comet2.c index e2a9e3c..e51b0e0 100644 --- a/src/comet2.c +++ b/src/comet2.c @@ -16,7 +16,7 @@ static struct option longopts[] = { }; /* エラー番号とエラーメッセージ */ -CERRARRAY cerr[] = { +CERRARRAY cerr_comet2[] = { { 201, "Load object file - full of COMET II memory" }, { 202, "SVC input - out of Input memory" }, { 203, "SVC output - out of COMET II memory" }, @@ -24,7 +24,6 @@ CERRARRAY cerr[] = { { 205, "Stack Pointer (SP) - cannot allocate stack buffer" }, { 206, "Address - out of COMET II memory" }, { 207, "Stack Pointer (SP) - out of COMET II memory" }, - { 0, NULL }, }; /* 指定されたファイルからアセンブル結果を読込 */ @@ -52,14 +51,14 @@ int main(int argc, char *argv[]) while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) { switch(opt) { case 't': - (&execmode)->trace = true; + execmode.trace = true; break; case 'T': - (&execmode)->trace = true; - (&execmode)->logical = true; + execmode.trace = true; + execmode.logical = true; break; case 'd': - (&execmode)->dump = true; + execmode.dump = true; break; case 'M': memsize = atoi(optarg); @@ -75,6 +74,8 @@ int main(int argc, char *argv[]) exit(-1); } } + /* エラーリストにerr_comet2を追加 */ + addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2); reset(); startptr = 0; if(loadassemble(argv[optind]) == true) { diff --git a/src/dumpword.c b/src/dumpword.c index a3c6a36..622a4de 100644 --- a/src/dumpword.c +++ b/src/dumpword.c @@ -9,11 +9,10 @@ static struct option longopts[] = { {0, 0, 0, 0}, }; -CERRARRAY cerr[] = { +CERRARRAY cerr_dumpword[] = { { 114, "not integer" }, { 115, "not hex" }, { 116, "out of hex range" }, - { 0, NULL }, }; int main(int argc, char *argv[]) @@ -41,6 +40,8 @@ int main(int argc, char *argv[]) fprintf(stderr, usage, argv[0]); exit(-1); } + /* エラーリストにerr_casl2を追加 */ + addcerrlist(ARRAYSIZE(cerr_dumpword), cerr_dumpword); /* WORD値に変換 */ word = nh2word(argv[optind]); if(cerrno > 0) { diff --git a/test/unit/CERRARRAY.c b/test/unit/CERRARRAY.c index 3f3570e..9499b67 100644 --- a/test/unit/CERRARRAY.c +++ b/test/unit/CERRARRAY.c @@ -1,7 +1,7 @@ #include "cerr.h" /* エラー番号とエラーメッセージ */ -CERRARRAY cerr[] = { +CERRARRAY cerr_utest[] = { { 101, "label already defined" }, { 102, "label table is full" }, { 103, "label not found" }, @@ -34,5 +34,9 @@ CERRARRAY cerr[] = { { 205, "Stack Pointer (SP) - cannot allocate stack buffer" }, { 206, "Address - out of COMET II memory" }, { 207, "Stack Pointer (SP) - out of COMET II memory" }, - { 0, NULL }, }; + +void addcerr_utest() +{ + addcerrlist((sizeof(cerr_utest)/sizeof(cerr_utest[0])), cerr_utest); +} diff --git a/test/unit/cerrtest/Makefile b/test/unit/cerrtest/Makefile index afb6e27..6d8b231 100644 --- a/test/unit/cerrtest/Makefile +++ b/test/unit/cerrtest/Makefile @@ -1,3 +1,3 @@ UCLASS = COMMON -TESTSRCFILE = ../CERRARRAY.c cerrtest.c +TESTSRCFILE = cerrtest.c include ../TEST.mk diff --git a/test/unit/cerrtest/cerrtest.c b/test/unit/cerrtest/cerrtest.c index ed8d0ae..39f2a09 100644 --- a/test/unit/cerrtest/cerrtest.c +++ b/test/unit/cerrtest/cerrtest.c @@ -1,6 +1,42 @@ #include #include "casl2.h" +/* エラー番号とエラーメッセージ */ +static CERRARRAY cerr_utest[] = { + { 101, "label already defined" }, + { 102, "label table is full" }, + { 103, "label not found" }, + { 104, "label length is too long" }, + { 105, "no command in the line" }, + { 106, "operand count mismatch" }, + { 107, "no label in START" }, + { 108, "not command of operand \"r\"" }, + { 109, "not command of operand \"r1,r2\"" }, + { 110, "not command of operand \"r,adr[,x]\"" }, + { 111, "not command of operand \"adr[,x]\"" }, + { 112, "not command of no operand" }, + { 113, "command not defined" }, + { 114, "not integer" }, + { 115, "not hex" }, + { 116, "out of hex range" }, + { 117, "operand is too many" }, + { 118, "operand length is too long" }, + { 119, "out of COMET II memory" }, + { 120, "GR0 in operand x" }, + { 121, "cannot get operand token" }, + { 122, "cannot create hash table" }, + { 123, "unclosed quote" }, + { 124, "more than one character in literal" }, + { 125, "not GR in operand x" }, + { 201, "execute - out of COMET II memory" }, + { 202, "SVC input - out of Input memory" }, + { 203, "SVC output - out of COMET II memory" }, + { 204, "Program Register (PR) - out of COMET II memory" }, + { 205, "Stack Pointer (SP) - cannot allocate stack buffer" }, + { 206, "Address - out of COMET II memory" }, + { 207, "Stack Pointer (SP) - out of COMET II memory" }, +}; + int main(){ int i, j; int code[] = { @@ -9,9 +45,9 @@ int main(){ 121, 122, 123, 124, 201, 202, 203, 204, 205, 206, 207, 999 }; const char *str[] = {NULL, "foobar"}; - - for(i = 0; i < sizeof(str)/sizeof(str[0]); i++) { - for(j = 0; j < sizeof(code)/sizeof(code[0]); j++) { + addcerrlist(ARRAYSIZE(cerr_utest), cerr_utest); + for(i = 0; i < ARRAYSIZE(str); i++) { + for(j = 0; j < ARRAYSIZE(code); j++) { setcerr(code[j], str[i]); printf("%d: %s - %d\t%s\n", code[j], str[i], cerrno, cerrmsg); if(cerrno != 0) {