-
Notifications
You must be signed in to change notification settings - Fork 0
/
e5-10.c
436 lines (396 loc) · 9.78 KB
/
e5-10.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
/* トークンの最大長 */
#define MAXOP 100
/* トークンのタイプ */
#define NUMBER '0'
#define IDENTIFIER 'i'
#define ERROR 'e'
#define VAR_REF 'v'
#define ASSIGN 'a'
int getop(char []);
void push(double);
double pop(void);
double top(void);
void call_func(const char *name);
int reference(char letter, double *out);
double assign(char letter, double value);
int _getline(char s[], int lim);
int getch(void);
void ungetch(int);
void execute_line(void);
void show_prompt(void);
/* 数学関数のシグネチャー */
typedef double (* D)(double);
typedef double (* DD)(double,double);
typedef double (* DI)(double,int);
typedef double (* DDp)(double,double*);
typedef double (* DIp)(double,int*);
/* シグネチャのタイプ。十の位はアリティー */
typedef enum {
S_D = 10,
S_DD = 21,
S_DI = 22,
S_DDp = 13,
S_DIp = 14
} signature_t;
/* 数学関数エントリー */
struct FUNC {
char *name;
signature_t signature;
union {
/* キャストのない、リテラル初期化の為に */
void *p;
D d;
DD dd;
DI di;
DDp ddp;
DIp dip;
} pt;
};
#include <limits.h>
double var[2 << CHAR_BIT] = { 0.0 };
int var_assigned[2 << CHAR_BIT] = { 0 };
void unget_string(char *str)
{
if (*str != '\0') {
unget_string(str+1);
ungetch(*str);
}
}
int main(int argc, char **argv)
{
char **zeroth = argv;
ungetch('\n');
argv += argc - 1;
while (argv != zeroth) {
ungetch(' ');
unget_string(*argv);
argv--;
}
execute_line();
return 0;
}
void show_prompt(void)
{
extern int sp;
int i;
for (i = 0; i < sp; ++i)
putchar('.');
printf("> ");
}
void execute_line(void)
{
int type;
double op2;
double tmp, tmp2;
extern int sp;
char s[MAXOP];
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else
printf("error: zero divisor\n");
break;
case '%':
op2 = pop();
push((int) pop() % (int) op2);
break;
case '\n':
printf("\t\t%.8g\n", assign('$', pop()));
return; /* 一行全部処理したのでリターン */
case IDENTIFIER:
if (s[1] == '\0') {
switch (s[0]) {
case 'p':
tmp = pop();
printf("\t%.8g\n", tmp);
push(tmp);
break;
case 'c':
tmp = pop();
push(tmp); push(tmp);
break;
case 'x':
tmp = pop(); tmp2 = pop();
push(tmp); push(tmp2);
break;
case 'e':
while (sp != 0)
pop();
break;
default:
goto FunctionCall;
}
goto NextToken;
}
FunctionCall:
call_func(s);
break;
case VAR_REF:
if (reference(s[1], &tmp) == 0) {
push(tmp);
} else {
printf("undefined variable %c\n", s[1]);
goto Error;
}
break;
case ASSIGN:
assign(s[1], top());
break;
case ERROR:
Error:
{
int c;
// while ((c = getch()) != '\n' || c != EOF)
// ;
}
break;
default:
printf("error: unknown command %s\n", s);
break;
}
NextToken:;
}
}
int _getline(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c=getchar()) !=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int arity(signature_t s)
{
return s/10;
}
void call_func(const char *name)
{
static struct FUNC func[] = {
{"sin", S_D, sin },
{"cos", S_D, cos },
{"tan", S_D, tan },
{"asin", S_D, asin },
{"acos", S_D, acos },
{"atan", S_D, atan },
{"atan2", S_DD, atan2 },
{"sinh", S_D, sinh },
{"cosh", S_D, cosh },
{"tanh", S_D, tanh },
{"exp", S_D, exp },
{"log", S_D, log },
{"log10", S_D, log10 },
{"pow", S_DD, pow },
{"sqrt", S_D, sqrt },
{"ceil", S_D, ceil },
{"floor", S_D, floor },
{"fabs", S_D, fabs },
{"ldexp", S_DI, ldexp },
{"frexp", S_DIp, frexp },
{"modf", S_DDp, modf },
{"fmod", S_DD, fmod },
{NULL, 0, NULL }
};
int i;
double t, u;
double ret;
for (i = 0; func[i].name != NULL; ++i) {
if (strcmp(func[i].name, name) == 0) {
switch (func[i].signature) {
case S_D:
t = pop();
push( ret = func[i].pt.d(t) );
break;
case S_DD:
t = pop();
u = pop();
push( ret = func[i].pt.dd(u,t) );
break;
case S_DI:
t = pop();
u = pop();
push( ret = func[i].pt.di(u,t) );
break;
case S_DDp:
t = pop();
push( ret = func[i].pt.ddp(t,&u) );
push(u);
break;
case S_DIp:
{
int n;
t = pop();
push( ret = func[i].pt.dip(t,&n) );
push(n);
}
break;
default:
printf("error: unknown signature\n");
break;
}
if ((ret == HUGE_VAL || ret == -HUGE_VAL)
&& errno == ERANGE) {
printf("error: range error\n");
}
return;
}
}
printf("error: unknown function %s\n", name);
return;
}
#define MAXVAL 100
int sp = 0;
double val[MAXVAL];
void push(double f)
{
if (sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full, can't push %g\n", f);
}
double pop(void)
{
if (sp > 0)
return val[--sp];
else {
printf("error: stack empty\n");
return 0.0;
}
}
double top(void)
{
if (sp > 0)
return val[sp-1];
else {
printf("error: stack empty\n");
return 0.0;
}
}
#include <ctype.h>
/*
* 標準入力より次のトークンを得る。
* s にはトークンが入り、返り値はトークンが演算子ならば
* その文字、数字ならば '0'。
*/
int getop(char s[])
{
int i, c;
static int pb = -1;
int tmp;
/* 空白を読みとばし、*/
while ((s[0] = c =
(pb != -1 ? tmp = pb, pb = -1, tmp : getch())) == ' ' || c == '\t')
;
s[1] = '\0';
if (c == EOF)
return EOF;
/* 最初の文字が s[0] と c に入った。 */
if (!isdigit(c) && c != '.' && c != '-') {
/* わからない記号か、識別子の先頭 */
if (isalpha(c)) {
for (i = 1; isalnum(c = (pb != -1 ? tmp = pb, pb = -1, tmp : getch())); ++i)
s[i] = c;
if (c != EOF)
pb = c;
s[i] = '\0';
return IDENTIFIER;
} else if (c == '=') {
/* 次の文字がアルファベットだったら ASSIGN */
/* さもなくば、文法エラー */
if (isalpha(c = (pb != -1 ? tmp = pb, pb = -1, tmp : getch()))) {
s[1] = c;
s[2] = '\0';
return ASSIGN;
} else { /* EOF でも */
printf("error: variable letter expected after =\n");
return ERROR;
}
} else if (c == '$') {
if (isalpha(c = (pb != -1 ? pb : getch())) || c == '$') {
s[1] = c;
s[2] = '\0';
return VAR_REF;
} else { /* EOF でも */
printf("error: variable name expected after $\n");
return ERROR;
}
} else if (c == '+' || c == '*' || c == '/' || c == '%' || c == '\n') {
return c;
} else {
printf("error: parse error on '%c'\n", c);
return ERROR;
}
}
i = 0;
if (isdigit(c) || c == '-')
while (isdigit(s[++i] = c = getch()))
;
if (c == '.')
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (s[0] == '-' && s[1] == '\0') {
pb = c;
return '-';
}
if (c != EOF)
pb = c;
return NUMBER;
}
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : EOF;
}
void ungetch(int c)
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
/*
* 変数を参照する。変数が定義されていれば 0 を返し、
* *out にその値を設定する。
*/
#include <assert.h>
int reference(char letter, double *out)
{
if (var_assigned[letter]) {
*out = var[letter];
return 0;
} else {
return -1;
}
}
double assign(char letter, double value)
{
var[letter] = value;
var_assigned[letter] = 1;
return value;
}