-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.c
More file actions
183 lines (156 loc) · 5 KB
/
eval.c
File metadata and controls
183 lines (156 loc) · 5 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
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "eval.h"
#define id(T) ((T)->var.id)
#define symbol(T) ((T)->lam.symbol)
#define body(T) ((T)->lam.body)
#define left(T) ((T)->app.left)
#define right(T) ((T)->app.right)
char* copy_string(const char* string) {
size_t len = strlen(string);
char* copy = malloc(len+1);
memcpy(copy, string, len+1);
return copy;
}
Term* term_var(char* symbol, Term* id) {
Term* term = malloc(sizeof(Term));
*term = (Term){.var = {VAR, symbol, id}};
return term;
}
Term* term_lam(char* symbol, Term* body) {
Term* term = malloc(sizeof(Term));
*term = (Term){.lam = {LAM, symbol, body}};
return term;
}
Term* term_app(Term* left, Term* right) {
Term* term = malloc(sizeof(Term));
*term = (Term){.app = {APP, left, right}};
return term;
}
void free_term(Term* term) {
if (term->kind == VAR) {
free(symbol(term));
} else if (term->kind == LAM) {
free(symbol(term));
free_term(body(term));
} else if (term->kind == APP) {
free_term(left(term));
free_term(right(term));
}
free(term);
}
Term* copy_term(Term* term, Map* map) {
if (term->kind == VAR) {
while (map != NULL) {
if (map->key == id(term)) return term_var(copy_string(symbol(term)), map->term);
map = map->next;
}
return term_var(copy_string(symbol(term)), id(term));
} else if (term->kind == LAM) {
Term* lam = term_lam(copy_string(symbol(term)), NULL);
Map new = {map, term, lam};
body(lam) = copy_term(body(term), &new);
return lam;
} else if (term->kind == APP) {
return term_app(
copy_term(left(term), map),
copy_term(right(term), map)
);
}
return NULL;
}
void print_term_impl(Term* term, Env* env, int lam_braces, int app_braces) {
if (term->kind == VAR) {
size_t count = 0;
while (env != NULL) {
if (strcmp(env->symbol, symbol(term)) == 0) count += 1;
if (env->term == id(term)) count = 0;
env = env->next;
}
printf("%s", symbol(term));
for (size_t i = 0; i < count; i++) printf("'");
} else if (term->kind == LAM) {
Env new = {env, symbol(term), term};
size_t count = 0;
while (env != NULL) {
if (strcmp(env->symbol, symbol(term)) == 0) count += 1;
env = env->next;
}
if (lam_braces) printf("(");
printf("λ");
printf("%s", symbol(term));
for (size_t i = 0; i < count; i++) printf("'");
printf(". ");
print_term_impl(body(term), &new, 0, 0);
if (lam_braces) printf(")");
} else if (term->kind == APP) {
if (app_braces) printf("(");
print_term_impl(left(term), env, 1, 0);
printf(" ");
print_term_impl(right(term), env, lam_braces, 1);
if (app_braces) printf(")");
}
}
void print_term(Term* term) {
print_term_impl(term, NULL, 0, 0);
printf("\n");
}
void bind_term(Term* term, Env* env) {
if (term->kind == VAR) {
while (env != NULL && strcmp(env->symbol, symbol(term)) != 0) {
env = env->next;
}
if (env != NULL) {
id(term) = env->term;
} else {
id(term) = NULL;
}
} else if (term->kind == LAM) {
Env new = {env, symbol(term), term};
bind_term(body(term), &new);
} else if (term->kind == APP) {
bind_term(left(term), env);
bind_term(right(term), env);
}
}
Term* eval_term(Term* term, Map* map, Env* env, int eval, int strong, int strict, size_t* count) {
for (;;) {
if (term->kind == VAR) {
Map* current = map;
while (current != NULL) {
if (current->key == id(term)) {
free_term(term);
term = copy_term(current->term, NULL);
break;
}
current = current->next;
}
}
while (term->kind == VAR && id(term) == NULL) {
Env* current = env;
while (current != NULL && strcmp(current->symbol, symbol(term)) != 0) {
current = current->next;
}
if (current == NULL) {
break;
}
free_term(term);
term = copy_term(current->term, NULL);
}
if (term->kind == LAM) body(term) = eval_term(body(term), map, env, eval && strong, strong, strict, count);
if (term->kind != APP) return term;
left(term) = eval_term(left(term), map, env, eval, strong, strict, count);
right(term) = eval_term(right(term), map, env, eval && strict, strong, strict, count);
if (!eval || left(term)->kind != LAM) return term;
*count += 1;
Map new = {map, left(term), right(term)};
Term* temp = eval_term(body(left(term)), &new, env, 0, strong, strict, count);
free_term(right(term));
free(symbol(left(term)));
free(left(term));
free(term);
term = temp;
}
return term;
}