YACAL2: CASL2処理系
[YACASL2.git] / src / cerr.c
1 #include "casl2.h"
2
3 /* エラーコードリスト */
4 typedef struct {
5     int num;
6     char *msg;
7 } CERRARRAY;
8 CERRARRAY cerr[] = {
9     { 101, "label already defined" },
10     { 102, "label table is full" },
11     { 103, "label not found" },
12     { 104, "label length is too long" },
13     { 105, "no command in the line" },
14     { 106, "operand count mismatch" },
15     { 107, "no label in START" },
16     { 108, "not command of operand \"r\"" },
17     { 109, "not command of operand \"r1,r2\"" },
18     { 110, "not command of operand \"r,adr[,x]\"" },
19     { 111, "not command of operand \"adr[,x]\"" },
20     { 112, "not command of no operand" },
21     { 113, "command not defined" },
22     { 114, "not integer" },
23     { 115, "not hex" },
24     { 116, "out of hex range" },
25     { 117, "operand is too many" },
26     { 118, "operand length is too long" },
27     { 119, "out of COMET II memory" },
28     { 120, "GR0 in operand x" },
29     { 121, "cannot get operand token" },
30     { 122, "cannot create hash table" },
31     { 201, "execute - out of COMET II memory" },
32     { 202, "SVC input - out of Input memory" },
33     { 203, "SVC output - out of COMET II memory" },
34     { 204, "Program Register (PR) - out of COMET II memory" },
35     { 205, "Stack Pointer (SP) - cannot allocate stack buffer" },
36     { 206, "Address - out of COMET II memory" }
37 };
38
39 /* WORD値を文字列に変換 */
40 char *wtoa(WORD word)
41 {
42     char *p = malloc(6), *q = malloc(6);
43     int i = 0, j;
44     do{
45         *(p + i++) = word % 10 + '0';
46     } while((word /= 10) > 0);
47     for(j = 0; j < i; j++) {
48         *(q + j) = *(p + (i - 1) - j);
49     }
50     *(q + j + 1) = '\0';
51     return q;
52 }
53
54 /* エラー番号とエラーメッセージを設定する */
55 void setcerr(int num, const char *val)
56 {
57     cerrno = num;
58     cerrmsg = malloc(256);
59     if(val != NULL) {
60         strcpy(cerrmsg, val);
61         strcat(cerrmsg, ": ");
62         strcat(cerrmsg, getcerrmsg(cerrno));
63     } else {
64         strcpy(cerrmsg, getcerrmsg(cerrno));
65     }
66 }
67
68 /* エラー番号からメッセージを返す */
69 char *getcerrmsg(int num)
70 {
71     assert(num > 0);
72     int i;
73     for(i = 0; i < ARRAYSIZE(cerr); i++) {
74         if((&cerr[i])->num == num) {
75             return (&cerr[i])->msg;
76         }
77     }
78     return "unkown error";
79 }
80
81 /* エラーを解放する */
82 void freecerr()
83 {
84     assert(cerrno > 0);
85     cerrno = 0;
86     if(strlen(cerrmsg) > 0) {
87         free(cerrmsg);
88     }
89 }