-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
74 lines (66 loc) · 1.16 KB
/
io.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
#include "store.c"
#define MAX_LINES 100
char *get_input(FILE *fp, const char *pmt)
{
char *t;
size_t n;
if (fp == stdin)
return readline(pmt);
t = NULL;
if (getline(&t, &n, fp) == -1)
return NULL;
return t;
}
char *read_input(FILE *fp)
{
int n, i;
char *s, *lines[MAX_LINES];
const char *pmt = "> ";
n = i = 0;
while (i < MAX_LINES && (s = get_input(fp, pmt))) {
lines[i++] = trim(s);
pmt = ".. ";
if ((n += oc_diff(s)) <= 0)
break;
}
if ((s || fp != stdin) && i > 0)
s = concat(lines, i);
if (!s || i > 1) {
while (--i >= 0)
free(lines[i]);
}
return s;
}
bool printatm(Cell *cp, bool dot)
{
if (!is_atom(cp))
return false;
putchar(dot ? CELL_SEP : 0);
fputs(cp->atm->sym, stdout);
return true;
}
void printexp(Cell *cp)
{
if (!cp || printatm(cp, false))
return;
putchar(CELL_BEG);
printexp(cp->car);
putchar(CELL_SEP);
printexp(cp->cdr);
putchar(CELL_END);
}
void print(Cell *cp)
{
Cell *root;
if (!cp || printatm(cp, false))
return;
putchar(LIST_BEG);
for (root = cp; !is_null(cp); cp = cdr(cp)) {
if (printatm(cp, true))
break;
if (cp != root)
putchar(LIST_SEP);
print(car(cp));
}
putchar(LIST_END);
}