git @ Cat's Eye Technologies Tamsin / e72c0b4
Fix build, but not compiler(s) yet. Chris Pressey 10 years ago
3 changed file(s) with 34 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
161161 }
162162 }
163163
164 /* TODO: rewrite to not use term_add_subterm */
165 void tamsin_mkterm_r(struct term *t, const struct term *list) {
164 void tamsin_mkterm_r(struct termlist **tl, const struct term *list) {
166165 if (term_atom_cstring_equal(list, "list") && list->subterms != NULL) {
167 tamsin_mkterm_r(t, list->subterms->next->term);
168 term_add_subterm(t, list->subterms->term);
166 tamsin_mkterm_r(tl, list->subterms->next->term);
167 termlist_add_term(tl, list->subterms->term);
169168 }
170169 }
171170
172171 const struct term *tamsin_mkterm(const struct term *atom,
173172 const struct term *list) {
174 struct term *t = term_new_atom(atom->atom, atom->size);
175 tamsin_mkterm_r(t, list);
176 return t;
173 struct termlist *tl = NULL;
174
175 tamsin_mkterm_r(&tl, list);
176
177 return term_new_constructor(atom->atom, atom->size, tl);
177178 }
178179
179180 const struct term *tamsin_reverse(const struct term *list, const struct term *sentinel) {
181182 const struct term *head = list; /* save */
182183
183184 while (list->subterms != NULL && term_atoms_equal(list, head)) {
184 const struct term *new = term_new_atom(head->atom, head->size);
185
185 const struct term *new;
186 struct termlist *tl = NULL;
187
186188 /*term_fput(list, stderr);
187189 fprintf(stderr, "\n");*/
188190
189 term_add_subterm(new, res);
190 term_add_subterm(new, list->subterms->term);
191 termlist_add_term(&tl, res);
192 termlist_add_term(&tl, list->subterms->term);
193 new = term_new_constructor(head->atom, head->size, tl);
191194 res = new;
195
192196 if (list->subterms->next == NULL) {
193197 break;
194198 }
2424 const char *atom;
2525 size_t size;
2626 int index;
27 struct term_list *subterms;
27 struct termlist *subterms;
2828 };
2929
30 struct term_list {
30 struct termlist {
3131 const struct term *term;
32 struct term_list *next;
32 struct termlist *next;
3333 };
3434
3535 /*
4444 const struct term *term_new_atom_from_char(char c);
4545
4646 const struct term *term_new_constructor(const char *, size_t,
47 struct term_list *);
47 struct termlist *);
48 void termlist_add_term(struct termlist **, const struct term *);
4849
4950 const struct term *term_new_variable(const char *, size_t, int);
5051
4545 return t;
4646 }
4747
48 void termlist_add_term(struct term_list **tl, const struct term *term) {
49 struct term_list *new_tl;
50
51 new_tl = malloc(sizeof(struct term_list));
52 new_tl->term = term;
53 new_tl->next = *tl;
54 *tl = new_tl;
55 }
56
5748 const struct term *term_new_atom_from_char(char c) {
5849 char s[2];
5950
6859 }
6960
7061 const struct term *term_new_constructor(const char *tag, size_t size,
71 struct term_list *subterms)
62 struct termlist *subterms)
7263 {
7364 struct term *t = malloc(sizeof(struct term));
7465 char *text = malloc(size);
8071 t->subterms = subterms;
8172
8273 return t;
74 }
75
76 void termlist_add_term(struct termlist **tl, const struct term *term) {
77 struct termlist *new_tl;
78
79 new_tl = malloc(sizeof(struct termlist));
80 new_tl->term = term;
81 new_tl->next = *tl;
82 *tl = new_tl;
8383 }
8484
8585 const struct term *term_new_variable(const char *name, size_t size, int index) {
134134 const struct term COMMA = { ", ", 2, -1, NULL };
135135
136136 const struct term *term_flatten(const struct term *t) {
137 struct term_list *tl;
137 struct termlist *tl;
138138
139139 if (t->subterms == NULL) { /* it's an atom */
140140 return t;
246246 }
247247
248248 const struct term *term_repr(const struct term *t) {
249 struct term_list *tl;
249 struct termlist *tl;
250250
251251 if (t->subterms == NULL) { /* it's an atom */
252252 return term_escape_atom(t);
267267
268268 int term_equal(const struct term *pattern, const struct term *ground)
269269 {
270 struct term_list *tl1, *tl2;
270 struct termlist *tl1, *tl2;
271271
272272 assert(pattern->index == -1);
273273 assert(ground->index == -1);
297297 int term_match_unifier(const struct term *pattern, const struct term *ground,
298298 const struct term **variables)
299299 {
300 struct term_list *tl1, *tl2;
300 struct termlist *tl1, *tl2;
301301
302302 if (pattern->index >= 0) {
303303 variables[pattern->index] = ground;