-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
132 lines (117 loc) · 3.81 KB
/
eval.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
#include "eval.h"
#include "repr.h"
static int rebind(Expr * const expr, const Expr * const old, const Expr * new) {
P_DEBUG("rebinding %p to %p in %p \n", (void*) old, (void*) new, (void*) expr)
if (old == new)
return 0;
switch (expr->type) {
case NULL_T:
RAISE(-1, "cant rebind null\n");
case FREE:
return 0;
case FUNC:
return rebind((Expr*) expr->data.func, old, new);
case APP:
return rebind((Expr*) expr->data.app.func, old, new)
+ rebind((Expr*) expr->data.app.arg, old, new);
case BOUND:
if (expr->data.func == old) {
expr->data.func = new;
return 1;
}
return 0;
}
}
static Expr* copyExpr(Expr * const target, const Expr * const src) {
P_DEBUG("copy %p -> %p\n", (void*) src, (void*) target)
Expr *func, *arg;
target->type = src->type;
switch (src->type) {
case NULL_T:
RAISE(NULL, "cant copy null\n");
case FREE:
case BOUND:
memcpy(target, src, sizeof(Expr));
break;
case FUNC:
MALLOC(func)
target->data.func = copyExpr(func, src->data.func);
rebind((Expr*) target->data.func, src, target);
break;
case APP:
MALLOC(func)
target->data.app.func = copyExpr(func, src->data.app.func);
MALLOC(arg)
target->data.app.arg = copyExpr(arg, src->data.app.arg);
break;
}
return target;
}
static int apply(Expr * const expr, const Expr * const func, const Expr * const arg) {
switch (expr->type) {
case NULL_T:
RAISE(-1, "cant replace null\n");
case FREE:
return 0;
case FUNC:
return apply((Expr*) expr->data.func, func, arg);
case APP:
return apply((Expr*) expr->data.app.func, func, arg)
+ apply((Expr*) expr->data.app.arg, func, arg);
case BOUND:
if (expr->data.func == func) {
copyExpr(expr, arg);
return 1;
}
return 0;
}
}
Expr* expand(Expr * const expr, int * const n) {
Expr *ret;
switch (expr->type) {
case NULL_T: RAISE(NULL, "cant evaluate null\n");
case BOUND:
case FREE:
return expr;
case FUNC:
expr->data.func = expand((Expr*) expr->data.func, n);
return expr;
case APP:
if (expr->data.app.func->type == FUNC) {
apply((Expr*) expr->data.app.func->data.func,
expr->data.app.func,
expr->data.app.arg);
ret = (Expr*) expr->data.app.func->data.func;
P_DEBUG("freeing %p\n", (void*) expr->data.app.func);
P_DEBUG("freeing %p\n", (void*) expr);
free((void*) expr->data.app.func);
freeExpr(expr->data.app.arg);
free((void*) expr);
++(*n);
return ret;
}
expr->data.app.func = expand((Expr*) expr->data.app.func, n);
expr->data.app.arg = expand((Expr*) expr->data.app.arg, n);
return expr;
}
}
Expr* eval(Expr *expr) {
int changes, total = 0, passes = -1;
do {
#ifdef DEBUG
printExprAST(expr);
printf("-------------------------------------------------\n");
#endif
changes = 0;
expr = expand((Expr*) expr, &changes);
if (!expr) return NULL;
total += changes;
++passes;
if (passes > MAX_PASSES)
RAISE(NULL, "exceeded expansion limit (%d)\n", MAX_PASSES);
} while (changes);
#ifdef DEBUG
printf("done\ndissolved %d functions in %d passes\n", total, passes);
#endif
return expr;
}