-
Notifications
You must be signed in to change notification settings - Fork 7
/
rpn.c
342 lines (297 loc) · 6.08 KB
/
rpn.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
/*
* rpn - Mycroft <[email protected]>
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <math.h>
#include <sys/types.h>
#include <assert.h>
#include "rpn.h"
int base = DEFBASE, stop = 0;
struct metastack *M = NULL;
int stackmode = 0;
int padcount = 0;
static void process(char *);
extern int repeat;
struct object *
top(void) {
return(M->t);
}
static void
push(struct object *obj)
{
if (M->t == NULL) {
M->t = M->b = obj;
M->t->prev = M->t->next = NULL;
} else {
M->t->prev = obj;
obj->next = M->t;
M->t = obj;
M->t->prev = NULL;
}
M->d++;
}
void
pushnum(double num)
{
struct object *obj;
if ((obj = malloc(sizeof *obj)) == NULL) {
perror("Error: malloc");
exit(1);
}
obj->num = num;
push(obj);
}
unsigned
countstack(void)
{
unsigned cnt = 0;
struct object *o = NULL;
for(o = top(); o; o = o->next)
cnt += 1;
return cnt;
}
struct object *
pop(void)
{
struct object *obj;
obj = M->t;
if ((M->t = M->t->next) != NULL)
M->t->prev = NULL;
else
M->b = NULL;
M->d--;
return obj;
}
double
peeknthnum(unsigned off)
{
struct object *o = top();
while(off--)
o = o->next;
return(o->num);
}
double
popnum(void)
{
double num;
struct object *obj;
obj = pop();
num = obj->num;
free(obj);
return num;
}
void
popobj(struct object *obj)
{
if (obj == top())
pop();
else {
if (obj == M->b)
M->b = obj->prev;
else
obj->next->prev = obj->prev;
obj->prev->next = obj->next;
M->d--;
}
free(obj);
}
static void
printnum(unsigned long num, int base, int padto)
{
static char str[sizeof num * CHAR_BIT], *ptr = str;
static char nums[] = "0123456789abcdefghijklmnopqrstuvwxyz";
int padc = 0;
do
*ptr++ = nums[num % base];
while ((num /= base) != 0);
padc = padto - (ptr - str);
while(padc > 0)
padc--, putchar('0');
while (ptr > str) {
putchar(*--ptr);
if(base == 2 && ((unsigned long)ptr % 4) == 0)
putchar('.');
}
putchar(' ');
}
static void
printstk(char *prompt)
{
struct object *obj;
for (obj = M->b; obj != NULL; obj = obj->prev) {
if (base == 10)
printf("%.12g ", obj->num);
else
printnum(obj->num, base, padcount);
if(stackmode && obj->prev)
putchar('\n');
}
fputs(prompt, stdout);
}
static void
eval(char *cmd)
{
char *operation;
static int doingmacro = 0;
long numargs;
static char prevcmd[MAXSIZE] = { '\0' };
struct command *cmdptr;
if (!doingmacro) {
if (strcmp(cmd, ".") == 0 && prevcmd[0])
cmd = prevcmd;
else {
strncpy(prevcmd, cmd, MAXSIZE-1);
cmd[MAXSIZE-1] = 0;
}
}
if ((operation = findmacro(cmd)) != NULL) {
doingmacro = 1;
process(operation);
doingmacro = 0;
} else if ((cmdptr = findcmd(cmd)) != NULL) {
if (cmdptr->numargs == -1) {
if (top() == NULL)
numargs = 1;
else if (top()->num < 0)
numargs = -1;
else
numargs = top()->num + 1;
} else
numargs = cmdptr->numargs;
if (numargs == -1 || M->d < numargs)
error(ERR_ARGC);
else
cmdptr->function();
} else
error(ERR_UNKNOWNCMD);
}
#define isnum(s) (isdigit(s[0]) \
|| ((s[0] == '-' || s[0] == '.') && isdigit(s[1])) \
|| (s[0] == '-' && s[1] == '.' && isdigit(s[2])))
#define isnotfloat(s) ((s[0] == '0' && s[1] != '.') \
|| (s[0] == '-' && s[1] == '0' && s[2] != '.'))
static void
process(char *str)
{
int x;
char *suffix, word[100];
char *tmp, *tmp2;
while (*str != '\0') {
while (isspace(*str))
str++;
if (*str == '\0')
break;
for (x = 0; *str != '\0' && !isspace(*str); x++)
word[x] = *str++;
word[x] = '\0';
if ((suffix = strchr(word, BASECHAR)) != NULL) {
*suffix++ = '\0';
tmp = tmp2 = word;
while (*tmp2 != '\0') {
if (*tmp2 == ',') tmp2++;
else *tmp++ = *tmp2++;
}
*tmp++ = *tmp2++;
if (word[0] == '-')
pushnum(strtol(word, NULL, atoi(suffix)));
else
pushnum(strtoul(word, NULL, atoi(suffix)));
} else if (isnum(word)) {
tmp = tmp2 = word;
while (*tmp2 != '\0') {
if (*tmp2 == ',') tmp2++;
else *tmp++ = *tmp2++;
}
*tmp++ = *tmp2++;
if (isnotfloat(word)) {
if (word[0] == '-')
pushnum(strtol(word, &suffix, 0));
else
pushnum(strtoul(word, &suffix, 0));
} else
pushnum(strtod(word, &suffix));
if (*suffix != '\0')
process(suffix);
} else {
for (x = repeat, repeat = 1; x > 0; x--) {
eval(word);
if (stop) {
stop = 0;
return;
}
}
}
}
}
int isatty(int);
static void
pushstack(void) {
struct metastack *m = malloc(sizeof *m);
struct object *o = top();
m->n = M;
M = m;
if(o)
pushnum(o->num);
}
static void
freestack(struct metastack *m) {
struct object *o = NULL;
for(o = m->b; o; o = o->next)
free(o);
free(m);
}
static void
popstack(void) {
struct object *o = top();
if(M->n) {
struct metastack *m = M;
M = M->n;
if(o)
pushnum(o->num);
freestack(m);
}
}
static void
init(void) {
struct command pushs = { "pushs", 0, pushstack },
pops = { "pops", 0, popstack };
addcommand(&pushs);
addcommand(&pops);
srand(time(NULL));
init_macros();
M = malloc(sizeof(*M));
M->t = NULL;
M->b = NULL;
M->d = 0;
M->n = NULL;
}
int
main(int argc, char *argv[])
{
init();
if (argc > 1) {
int x;
for (x = 1; x < argc; x++)
process(argv[x]);
printstk("\n");
} else {
int interactive = isatty(0);
char buf[1000];
if (interactive)
printstk("> ");
while (fgets(buf, sizeof buf, stdin) != NULL) {
process(buf);
if (interactive)
printstk("> ");
}
if (!interactive)
printstk("\n");
}
return 0;
}