Mercurial > hg > audiostuff
comparison intercom/gsm/add_test.c @ 2:13be24d74cd2
import intercom-0.4.1
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 09:57:52 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:9cadc470e3da | 2:13be24d74cd2 |
---|---|
1 /* | |
2 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische | |
3 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for | |
4 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE. | |
5 * | |
6 * Updated by KIRCHHERR (FI/DBP Telekom) for Alpha VMS/Apx: included unixio.h | |
7 */ | |
8 | |
9 /* $Header: /home/kbs/jutta/src/gsm/gsm-1.0/add-test/RCS/add_test.c,v 1.1 1992/10/28 00:09:10 jutta Exp $ */ | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <string.h> | |
14 | |
15 #ifdef VMS | |
16 #include <unixio.h> | |
17 #endif | |
18 | |
19 #define GSM_IMPLEMENTATION | |
20 #include "private.h" | |
21 #include "gsm.h" | |
22 | |
23 #include "add.c" | |
24 | |
25 int interactive = 1; | |
26 | |
27 char *opname; | |
28 longword L_op1, L_op2, L_expect; | |
29 word op1, op2, expect; | |
30 int do_expect; | |
31 | |
32 void help() | |
33 { | |
34 puts(" add a b sub a b mult a b div a b"); | |
35 puts("L_add A B L_sub A B L_mult A B mult_r a b"); | |
36 puts(""); | |
37 puts("abs a norm a >> a b << a b"); | |
38 puts(" L_>> A B L_<< A B"); | |
39 | |
40 } | |
41 | |
42 char *strtek P2((str, sep), char *str, char *sep) | |
43 { | |
44 | |
45 static char *S = (char *) 0; | |
46 char *c, *base; | |
47 | |
48 if (str) | |
49 S = str; | |
50 | |
51 if (!S || !*S) | |
52 return (char *) 0; | |
53 | |
54 /* Skip delimiters. | |
55 */ | |
56 while (*S) { | |
57 for (c = sep; *c && *c != *S; c++); | |
58 if (*c) | |
59 *S++ = 0; | |
60 else | |
61 break; | |
62 } | |
63 | |
64 base = S; | |
65 | |
66 /* Skip non-delimiters. | |
67 */ | |
68 for (base = S; *S; S++) { | |
69 | |
70 for (c = sep; *c; c++) | |
71 if (*c == *S) { | |
72 *S++ = 0; | |
73 return base; | |
74 } | |
75 } | |
76 | |
77 return base == S ? (char *) 0 : base; | |
78 } | |
79 | |
80 long value P1((s), char *s) | |
81 { | |
82 switch (*s) { | |
83 case '-': | |
84 switch (s[1]) { | |
85 case '\0': | |
86 return MIN_WORD; | |
87 case '-': | |
88 return MIN_LONGWORD; | |
89 default: | |
90 break; | |
91 } | |
92 break; | |
93 | |
94 case '+': | |
95 switch (s[1]) { | |
96 case '\0': | |
97 return MAX_WORD; | |
98 case '+': | |
99 return MAX_LONGWORD; | |
100 default: | |
101 break; | |
102 } | |
103 default: | |
104 break; | |
105 } | |
106 | |
107 return strtol(s, (char **) 0, 0); | |
108 } | |
109 | |
110 char *parse P1((buf), char *buf) | |
111 { | |
112 char *s, *a; | |
113 long l; | |
114 | |
115 if ((a = strchr(buf, '=')) != NULL) | |
116 *a++ = 0; | |
117 | |
118 opname = s = strtek(buf, " \t("); | |
119 if (!s) | |
120 return (char *) 0; | |
121 | |
122 op1 = op2 = L_op1 = L_op2 = 0; | |
123 | |
124 if ((s = strtek((char *) 0, "( \t,")) != NULL) { | |
125 op1 = L_op1 = value(s); | |
126 if ((s = strtek((char *) 0, ", \t)")) != NULL) | |
127 op2 = L_op2 = value(s); | |
128 } | |
129 | |
130 if (a) { | |
131 do_expect = 1; | |
132 while (*a == ' ' || *a == '\t') | |
133 a++; | |
134 expect = L_expect = value(a); | |
135 } | |
136 | |
137 return opname; | |
138 } | |
139 | |
140 void fprint_word P2((f, w), FILE * f, word w) | |
141 { | |
142 if (!w) | |
143 putc('0', f); | |
144 else | |
145 fprintf(f, "0x%4.4x (%d%s)", | |
146 (unsigned int) w, | |
147 (int) w, w == MIN_WORD ? "/-" : (w == MAX_WORD ? "/+" : "")); | |
148 } | |
149 | |
150 void print_word P1((w), word w) | |
151 { | |
152 fprint_word(stdout, w); | |
153 } | |
154 | |
155 void fprint_longword P2((f, w), FILE * f, longword w) | |
156 { | |
157 if (!w) | |
158 putc('0', f); | |
159 else | |
160 fprintf(f, "0x%8.8x (%ld%s)", | |
161 w, w, w == MIN_WORD ? "/-" | |
162 : (w == MAX_WORD ? "/+" | |
163 : (w == (longword) MIN_LONGWORD ? "/--" | |
164 : (w == (longword) MAX_LONGWORD ? "/++" : "")))); | |
165 } | |
166 | |
167 void print_longword P1((w), longword w) | |
168 { | |
169 fprint_longword(stdout, w); | |
170 } | |
171 | |
172 void do_longword P1((w), longword w) | |
173 { | |
174 if (interactive) | |
175 print_longword(w); | |
176 if (do_expect) { | |
177 if (w != L_expect) { | |
178 if (!interactive) | |
179 fprint_longword(stderr, w); | |
180 fprintf(stderr, " != %s (%ld, %ld) -- expected ", | |
181 opname, L_op1, L_op2); | |
182 fprint_longword(stderr, L_expect); | |
183 putc('\n', stderr); | |
184 } | |
185 } else if (interactive) | |
186 putchar('\n'); | |
187 } | |
188 | |
189 void do_word P1((w), word w) | |
190 { | |
191 if (interactive) | |
192 print_word(w); | |
193 if (do_expect) { | |
194 if (w != expect) { | |
195 if (!interactive) | |
196 fprint_word(stderr, w); | |
197 fprintf(stderr, " != %s (%ld, %ld) -- expected ", | |
198 opname, L_op1, L_op2); | |
199 fprint_word(stderr, expect); | |
200 putc('\n', stderr); | |
201 } | |
202 } else if (interactive) | |
203 putchar('\n'); | |
204 } | |
205 | |
206 int main(argc, argv) | |
207 int argc; | |
208 char *argv[]; | |
209 { | |
210 char buf[299]; | |
211 char *c; | |
212 FILE *datain; | |
213 | |
214 if (argc > 1) | |
215 datain = fopen(argv[1], "r"); | |
216 else | |
217 datain = stdin; | |
218 | |
219 if (datain == NULL) { | |
220 perror("Invalid input file!\n"); | |
221 exit(1); | |
222 } | |
223 | |
224 interactive = isatty(fileno(datain)); | |
225 | |
226 for (;;) { | |
227 if (interactive) | |
228 fprintf(stderr, "? "); | |
229 | |
230 if (!fgets(buf, sizeof(buf), datain)) | |
231 exit(0); | |
232 if ((c = strchr(buf, '\n')) != NULL) | |
233 *c = 0; | |
234 | |
235 if (*buf == ';' || *buf == '#') | |
236 continue; | |
237 if (*buf == '\'') { | |
238 puts(buf + 1); | |
239 continue; | |
240 } | |
241 if (*buf == '\"') { | |
242 fprintf(stderr, "%s\n", buf + 1); | |
243 continue; | |
244 } | |
245 | |
246 c = parse(buf); | |
247 | |
248 if (!c) | |
249 continue; | |
250 if (!strcmp(c, "add")) { | |
251 do_word(gsm_add(op1, op2)); | |
252 continue; | |
253 } | |
254 if (!strcmp(c, "sub")) { | |
255 do_word(gsm_sub(op1, op2)); | |
256 continue; | |
257 } | |
258 if (!strcmp(c, "mult")) { | |
259 do_word(gsm_mult(op1, op2)); | |
260 continue; | |
261 } | |
262 if (!strcmp(c, "mult_r")) { | |
263 do_word(gsm_mult_r(op1, op2)); | |
264 continue; | |
265 } | |
266 if (!strcmp(c, "abs")) { | |
267 do_word(gsm_abs(op1)); | |
268 continue; | |
269 } | |
270 if (!strcmp(c, "div")) { | |
271 do_word(gsm_div(op1, op2)); | |
272 continue; | |
273 } | |
274 if (!strcmp(c, "norm")) { | |
275 do_word(gsm_norm(L_op1)); | |
276 continue; | |
277 } | |
278 if (!strcmp(c, "<<")) { | |
279 do_word(gsm_asl(op1, op2)); | |
280 continue; | |
281 } | |
282 if (!strcmp(c, ">>")) { | |
283 do_word(gsm_asr(op1, op2)); | |
284 continue; | |
285 } | |
286 if (!strcmp(c, "L_mult")) { | |
287 do_longword(gsm_L_mult(op1, op2)); | |
288 continue; | |
289 } | |
290 if (!strcmp(c, "L_add")) { | |
291 do_longword(gsm_L_add(L_op1, L_op2)); | |
292 continue; | |
293 } | |
294 if (!strcmp(c, "L_sub")) { | |
295 do_longword(gsm_L_sub(L_op1, L_op2)); | |
296 continue; | |
297 } | |
298 if (!strcmp(c, "L_<<")) { | |
299 do_longword(gsm_L_asl(L_op1, L_op2)); | |
300 continue; | |
301 } | |
302 if (!strcmp(c, "L_>>")) { | |
303 do_longword(gsm_L_asr(L_op1, L_op2)); | |
304 continue; | |
305 } | |
306 help(); | |
307 } | |
308 } |