-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuf_routines.c
545 lines (505 loc) · 13 KB
/
buf_routines.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
* helper functions for fb3-2
*/
# include <stdio.h>
# include <stdlib.h>
# include <stdarg.h>
# include <string.h>
# include <math.h>
# include "buf_header.h"
/* symbol table */
/* hash a symbol */
#define NHASH 9997
struct symbol symtab[NHASH];
static unsigned
symhash(char *sym)
{
unsigned int hash = 0;
unsigned c;
while(c = *sym++) hash = hash*9 ^ c;
return hash;
}
struct symbol *
lookup(char* sym)
{
struct symbol *sp = &symtab[symhash(sym)%NHASH];
int scount = NHASH; /* how many have we looked at */
while(--scount >= 0) {
if(sp->name && !strcmp(sp->name, sym)) { return sp; }
if(!sp->name) { /* new entry */
sp->name = strdup(sym);
sp->value = 0;
sp->func = NULL;
sp->syms = NULL;
return sp;
}
if(++sp >= symtab+NHASH) sp = symtab; /* try the next entry */
}
yyerror("symbol table overflow\n");
abort(); /* tried them all, table is full */
}
struct ast *
newast(int nodetype, struct ast *l, struct ast *r)
{
struct ast *a = malloc(sizeof(struct ast));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = nodetype;
a->l = l;
a->r = r;
return a;
}
struct ast *
newnum(double d)
{
struct numval *a = malloc(sizeof(struct numval));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = 'K';
a->number = d;
return (struct ast *)a;
}
struct ast *
newcmp(int cmptype, struct ast *l, struct ast *r)
{
struct ast *a = malloc(sizeof(struct ast));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = '0' + cmptype;
a->l = l;
a->r = r;
return a;
}
struct ast *
newfunc(int functype, struct ast *l)
{
struct fncall *a = malloc(sizeof(struct fncall));
if(!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = 'F';
a->l = l;
a->functype = functype;
return (struct ast *)a;
}
struct ast *
newfunc2(int functype, struct ast *l, struct ast *r){
struct fncall2 *a = malloc(sizeof(struct fncall2));
if(!a){
yyerror("out of space");
exit(0);
}
a->nodetype = 'T';
a->l = l;
a->r = r;
a->functype = functype;
return (struct ast *)a;
}
struct ast *newcall(struct symbol *s, struct ast *l) {
struct ufncall *a = malloc(sizeof(struct ufncall));
if (!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = 'C';
a->l = l;
a->s = s;
return (struct ast *)a;
}
struct ast *newref(struct symbol *s) {
struct symref *a = malloc(sizeof(struct symref));
if (!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = 'N';
a->s = s;
return (struct ast *)a;
}
/*string node*/
struct ast *newstring(const char *value) {
struct ast_string *a = malloc(sizeof(struct ast_string));
if (!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = 'S'; // Assuming 'S' denotes a string node type
a->value = strdup(value); // Allocate memory and copy the string value
return (struct ast *)a;
}
struct ast *newasgn(struct symbol *s, struct ast *v) {
struct symasgn *a = malloc(sizeof(struct symasgn));
if (!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = '=';
a->s = s;
a->v = v;
return (struct ast *)a;
}
struct ast *newflow(int nodetype, struct ast *cond, struct ast *tl, struct ast *el) {
struct flow *a = malloc(sizeof(struct flow));
if (!a) {
yyerror("out of space");
exit(0);
}
a->nodetype = nodetype;
a->cond = cond;
a->tl = tl;
a->el = el;
return (struct ast *)a;
}
/* free a tree of ASTs */
void treefree(struct ast *a) {
switch (a->nodetype) {
/* two subtrees */
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case 'L':
treefree(a->r);
/* one subtree */
case '|':
case '!':
case 'M':
case 'C':
case 'F':
case 'T':
treefree(a->l);
/* no subtree */
case 'K':
case 'N':
case 'S':
break;
case '=':
free(((struct symasgn *)a)->v);
break;
/* up to three subtrees */
case 'I':
case 'W':
case 'R':
free(((struct flow *)a)->cond);
if (((struct flow *)a)->tl) treefree(((struct flow *)a)->tl);
if (((struct flow *)a)->el) treefree(((struct flow *)a)->el);
break;
default:
printf("internal error: free bad node %c\n", a->nodetype);
}
free(a); /* always free the node itself */
}
struct symlist *newsymlist(struct symbol *sym, struct symlist *next) {
struct symlist *sl = malloc(sizeof(struct symlist));
if (!sl) {
yyerror("out of space");
exit(0);
}
sl->sym = sym;
sl->next = next;
return sl;
}
/* free a list of symbols */
void symlistfree(struct symlist *sl) {
struct symlist *nsl;
while (sl) {
nsl = sl->next;
free(sl);
sl = nsl;
}
}
double factorial(double n){
if (n < 0) {
// Handle negative input
return -1; // Or any appropriate error indicator
}
int x;
double f = 1;
for(x=1; x<=n;x++){
f=f*x;
}
return f;
}
static double callbuiltin(struct fncall *);
static double callbuiltin2(struct fncall2 *);
static double calluser(struct ufncall *);
double eval(struct ast *a) {
double v;
char* str;
if (!a) {
yyerror("internal error, null eval");
return 0.0;
}
switch (a->nodetype) {
/* constant */
case 'K':
v = ((struct numval *)a)->number;
break;
/* name reference */
case 'N':
v = ((struct symref *)a)->s->value;
break;
case 'S':
str = ((struct ast_string *)a)->value;
v = strlen(str);
printf(str);
/* assignment */
case '=':
v = ((struct symasgn *)a)->s->value = eval(((struct symasgn *)a)->v);
break;
/* expressions */
case '+':
v = eval(a->l) + eval(a->r);
break;
case '-':
v = eval(a->l) - eval(a->r);
break;
case '*':
v = eval(a->l) * eval(a->r);
break;
case '/':
v = eval(a->l) / eval(a->r);
break;
case '%':
v = (int)eval(a->l) % (int)eval(a->r);
break;
case '^':
v = pow(eval(a->l),eval(a->r));
break;
case '|':
v = fabs(eval(a->l));
break;
case '!':
v = factorial(eval(a->l));
break;
case 'M':
v = -eval(a->l);
break;
/* comparisons */
case '1':
v = (eval(a->l) > eval(a->r)) ? 1 : 0;
break;
case '2':
v = (eval(a->l) < eval(a->r)) ? 1 : 0;
break;
case '3':
v = (eval(a->l) != eval(a->r)) ? 1 : 0;
break;
case '4':
v = (eval(a->l) == eval(a->r)) ? 1 : 0;
break;
case '5':
v = (eval(a->l) >= eval(a->r)) ? 1 : 0;
break;
case '6':
v = (eval(a->l) <= eval(a->r)) ? 1 : 0;
break;
case '7':
v = (eval(a->l) != eval(a->r)) ? 1 : 0;
/* control flow */
/* null expressions allowed in the grammar, so check for them */
/* if/then/else */
case 'I':
if (eval(((struct flow *)a)->cond) != 0) {
if (((struct flow *)a)->tl) { // true branch
v = eval(((struct flow *)a)->tl);
} else
v = 0.0; /* a default value */
} else {
if (((struct flow *)a)->el) { // false branch
v = eval(((struct flow *)a)->el);
} else
v = 0.0; /* a default value */
}
break;
/* while/do */
case 'W':
v = 0.0; /* a default value */
if (((struct flow *)a)->tl) {
while (eval(((struct flow *)a)->cond) != 0) // evaluate the condition
v = eval(((struct flow *)a)->tl); // evaluate the target statements
}
break; /* value of last statement is value of while/do */
/* list of statements */
case 'R':
v = 0.0; //default
for(int i = 0; i < (eval(((struct flow *)a)->cond)); i++){
v = eval(((struct flow *)a)->tl); //execute statement certain amount of times
}
break;
case 'L':
eval(a->l);
v = eval(a->r);
break;
case 'F':
v = callbuiltin((struct fncall *)a);
break;
case 'T':
v = callbuiltin2((struct fncall2 *)a);
break;
case 'C':
v = calluser((struct ufncall *)a);
break;
default:
printf("internal error: bad node %c\n", a->nodetype);
}
return v;
}
static double
callbuiltin2(struct fncall2 *f){
enum bifs functype = f->functype;
double a = eval(f->l);
double b = eval(f->r);
switch(functype){
case B_pow:
return pow(a,b);
case B_atan2:
return atan2(a,b);
return a;
default:
yyerror("Unknown built-in function %d", functype);
return 0.0;
}
}
//BUILT IN FUNCTIONS
static double
callbuiltin(struct fncall *f)
{
enum bifs functype = f->functype;
double v = eval(f->l);
switch(functype) {
case B_sqrt:
return sqrt(v);
case B_exp:
return exp(v);
case B_log:
return log(v);
case B_print:
printf("= %4.4g\n", v);
case B_sin:
return sin(v);
case B_cos:
return cos(v);
case B_tan:
return tan(v);
case B_asin:
return asin(v);
case B_acos:
return acos(v);
case B_atan:
return atan(v);
case B_rand:
return rand() % (int)v;
return v;
default:
yyerror("Unknown built-in function %d", functype);
return 0.0;
}
}
/* define a function */
void
dodef(struct symbol *name, struct symlist *syms, struct ast *func)
{
if(name->syms) symlistfree(name->syms);
if(name->func) treefree(name->func);
name->syms = syms;
name->func = func;
}
static double calluser(struct ufncall *f) {
struct symbol *fn = f->s; /* function name */
struct symlist *sl; /* dummy arguments */
struct ast *args = f->l; /* actual arguments */
double *oldval, *newval; /* saved arg values */
double v;
int nargs;
int i;
if (!fn->func) {
yyerror("call to undefined function", fn->name);
return 0;
}
/* count the arguments */
sl = fn->syms;
for (nargs = 0; sl; sl = sl->next)
nargs++;
/* prepare to save them */
oldval = (double *)malloc(nargs * sizeof(double));
newval = (double *)malloc(nargs * sizeof(double));
if (!oldval || !newval) {
yyerror("Out of space in %s", fn->name);
return 0.0;
}
/* evaluate the arguments */
for (i = 0; i < nargs; i++) {
if (!args) {
yyerror("too few args in call to %s", fn->name);
free(oldval);
free(newval);
return 0.0;
}
if (args->nodetype == 'L') { /* if this is a list node */
newval[i] = eval(args->l);
args = args->r;
} else { /* if it's the end of the list */
newval[i] = eval(args);
args = NULL;
}
}
/* save old values of dummies, assign new ones */
sl = fn->syms;
for (i = 0; i < nargs; i++) {
struct symbol *s = sl->sym;
oldval[i] = s->value;
s->value = newval[i];
sl = sl->next;
}
free(newval);
/* evaluate the function */
v = eval(fn->func);
/* put the real values of the dummies back */
sl = fn->syms;
for (i = 0; i < nargs; i++) {
struct symbol *s = sl->sym;
s->value = oldval[i];
sl = sl->next;
}
free(oldval);
return v;
}
void
yyerror(char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stderr, "%d: error: ", yylineno);
vfprintf(stderr, s, ap);
fprintf(stderr, "\n");
}
int
main()
{
printf("__________ _____ _____ .__ \n");
printf("\\______ \\__ ___/ ____\\/ ____\\____ | | ____ \n");
printf(" | | _/ | \\ __\\\\ __\\\\__ \\ | | / _ \\ \n");
printf(" | | \\ | /| | | | / __ \\| |_( <_> )\n");
printf(" |______ /____/ |__| |__| (____ /____/\\____/ \n");
printf(" \\/ \\/ \n");
printf(">> ");
//yylex();
return yyparse();
}