-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.c
179 lines (140 loc) · 3.42 KB
/
execute.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
#include <stdlib.h>
#include "error.h"
#include "execute.h"
static Cons *f_append(Cons *a, Cons *b)
{
if (a == NULL) return b;
Cons *ret = new_Cons(a->a, NIL);
Cons *l = ret;
while (a->b != NIL) {
a = next(a);
l->b = pnew_Cons(a->a, NIL);
l = next(l);
}
l->b = make_Cons(b);
return ret;
}
/**
* Zjisti zda list l je aspon count dlouhy. Do other
* nastavi odkaz na (count+1)-ty prvek.
*/
static int list_length_ok(Cons *l, int count, Cons **other)
{
while (l != NULL && count) {
count--; l = next(l);
}
*other = l;
return count == 0;
}
/**
* insert_params run
*/
static t_point ip_run(t_point *exp_params, t_point kam)
{
t_point p1, p2;
if (kam == NIL) return NIL;
else if (type_match(kam, CONS)) {
p1 = ip_run(exp_params, get_Cons(kam)->a);
p2 = ip_run(exp_params, get_Cons(kam)->b);
return pnew_Cons(p1, p2);
}
else if (is_Param(kam))
return exp_params[get_Num((t_point) get_Thunk(kam)->params) - 1];
else if (type_match(kam, THUNK)) {
p1 = ip_run(exp_params, get_Thunk(kam)->function);
p2 = ip_run(exp_params, make_Cons(get_Thunk(kam)->params));
return pnew_Thunk(p1, get_Cons(p2));
}
return kam;
}
t_point insert_params(Cons *params, Function *kam)
{
int count = kam->params_count + (kam->more_params ? 1 : 0);
t_point exp_params[count];
t_point l;
for (int i=0; i<kam->params_count; i++) {
exp_params[i] = params->a;
params = next(params);
}
// doplneni posledniho parametru pro neomezeny pocet parametru
if (kam->more_params)
exp_params[count-1] = make_Cons(params);
l = ip_run(exp_params, kam->body.structure);
return l;
}
static inline t_point result(Thunk *t, int *done)
{
if (is_Func(t->function)) {
Function *f = get_Func(t->function);
Cons *other_params = NULL;
if (list_length_ok(t->params, f->params_count, &other_params)) {
*done = 0;
// prebytek parametru
if (f->more_params) other_params = NULL;
if (f->built_in)
t->function = f->body.link(t->params);
else
t->function = insert_params(t->params, f);
t->params = other_params;
if (other_params == NULL)
return t->function;
else
return make_Thunk(t);
} else {
*done = 1;
return make_Thunk(t);
}
}
if (type_match(t->function, THUNK) && !is_Param(t->function)) {
*done = 0;
Thunk *t2 = get_Thunk(t->function);
t->function = t2->function;
t->params = f_append(t2->params, t->params);
return make_Thunk(t);
}
// nejde ani o volani funkce ani o nevyhodnoceny Thunk
if (t->params == NULL)
return t->function;
else
ERROR(TOO_MANY_PARAMS);
}
t_point resolve_Thunk(t_point s)
{
static t_point filo[FILO_DEPTH];
static int seen[FILO_DEPTH];
static int akt = 0;
int start = akt;
filo[akt] = s;
seen[akt] = 0;
while (1) {
s = filo[akt];
if (!is_Thunk(s)) {
if (akt == start) break;
goto dalsi;
}
if (seen[akt]) {
int zaloha = akt;
filo[akt] = result(get_Thunk(s), &seen[akt]);
// pokud se tohle pokazi tak je nekde neco hodne spatne
if (zaloha != akt) ERROR(INNER_ERROR);
// nic se nezmenilo
if ((s == filo[akt]) && seen[akt]) {
if (akt == start) break;
akt--;
}
continue;
}
// jeste jsme prvek nevideli && je to Thunk
// -> dame funkci na zasobnik a vysledek vyhodnotime pozdeji
else {
seen[akt] = 1;
if (++akt >= FILO_DEPTH) ERROR(TOO_DEEP_RECURSION);
filo[akt] = get_Thunk(s)->function;
seen[akt] = 0;
continue;
}
dalsi:
if (--akt < start) ERROR(INNER_ERROR);
}
return filo[akt];
}