-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.c
116 lines (94 loc) · 1.93 KB
/
term.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
/*
* term.c
*
* Copyright (C) 2007 Slawomir Maludzinski
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include "term.h"
term * term_new(int type, char * name)
{
return term_new_list(type, name, NULL);
}
term * term_new_list(int type, char * name, List * terms)
{
term * t;
t = malloc(sizeof(term));
if (t == NULL)
{
fprintf(stderr, "cannot allocate term\n");
}
t->type = type;
t->name = name;
t->terms = terms;
return t;
}
void term_delete(term * t)
{
if (t == NULL)
{
return;
}
list_delete(t->terms);
free(t->name);
free(t);
}
void term_deallocator(void * data)
{
term_delete((term *)data);
}
void term_print(term * t)
{
if (t == NULL)
{
return;
}
term_print_rec(t);
}
void term_print_rec(term * t)
{
printf("%s", t->name);
if (t->terms != NULL)
{
printf("(");
term_list_print_rec(t->terms);
printf(")");
}
}
void term_list_print(List * l)
{
if (l == NULL)
{
return;
}
term_list_print_rec(l);
}
void term_list_print_rec(List * l)
{
char first = 1;
ListIterator iter = list_iterator_last(l);
while (!list_iterator_is_last(iter))
{
if (first == 0)
{
printf(", ");
}
term_print_rec((term *)list_iterator_data(iter));
first = 0;
list_iterator_previous(&iter);
}
}