forked from zeotrope/anicca
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.c
More file actions
98 lines (87 loc) · 2.31 KB
/
util.c
File metadata and controls
98 lines (87 loc) · 2.31 KB
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "anicca.h"
#include "char.h"
#include "memory.h"
#include "function.h"
#include "verb.h"
#include "lexer.h"
#include "parser.h"
#include "util.h"
VO print(A y) {
C *cv; I yn=AN(y), *iv; D *dv; Z *zv; V *vv; A *bv;
if (!y) { printf("NULL"); R; }
switch (AT(y)) {
case BOOL: { cv=BAV(y); DO(yn, printf("%d ", (I)cv[i])); break; }
case CHAR: { cv=CAV(y); DO(yn, printf("%c", cv[i])); break; }
case INT: { iv=IAV(y); DO(yn, printf("%d ", iv[i])); break; }
case FLT: { dv=DAV(y); DO(yn, printf("%lf ",dv[i])); break; }
case CMPX: {
zv=ZAV(y); DO(yn, printf("%lfj%lf ",ZR(zv[i]),ZI(zv[i]))); break;
}
/* TODO?: fancy line drawings */
case BOX: {
bv = AAV(y);
DO(AN(y), printf("(<");
print(bv[i]);
printf(")%s", (i+1 == AN(y))?"":",")
);
break;
}
case VERB: case ADV: case CONJ: {
vv = VAV(y);
printf("%c", vv->id);
if (VF(vv)) { print(vv->f);
if (VG(vv)) { print(vv->g);
if (VH(vv)) { print(vv->h); };
}
};
break;
}
case MARK: { break; }
case LPAR: { printf("LPAR"); break; }
case RPAR: { printf("RPAR"); break; }
default: { printf("HUH?"); break; }
}
}
VO println(A y) {
if (!(AT(y)&MARK)) { print(y); printf("\n"); }
}
VO a_init(VO) {
zero=sbool(0); one=sbool(1);
ten=sint(10);
zone=scmpx(0,1);
mark = ga(MARK, 0, 0, NULL);
lpar = ga(LPAR, 0, 0, NULL);
rpar = ga(RPAR, 0, 0, NULL);
}
I a_strtoi(I n, C *s, C **e) { R (I)a_strtod(n,s,e); }
D a_strtod(I n, C *s, C**e) { I si=1; D v, p;
if (*s==CUNDS) { si=-1; s++; }
for (v=0;isdigit(*s);) { v=(10*v)+(*s++-'0'); }
if (*s=='.') { s++; }
for (p=1;isdigit(*s);) { v=(10*v)+(*s++-'0'); p*=10; }
*e=s;
R si*(v/p);
}
A eval(const C *str) {
A w, y, z;
w = gstr(strlen(str)+1, str);
y = tokens(w);
z = parse(y);
a_free(w); a_free(y);
R z;
}
VO a_repl(const C *s) {
C *v, str[100]; A z;
while (1) {
printf("%s", s);
v = fgets(str, sizeof(str), stdin);
if (!v) break;
v = strndup(str, strlen(str)-1); /* remove carriage return */
println(z = eval(v));
free(v);
}
}