-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sexp.c
237 lines (213 loc) · 3.46 KB
/
Sexp.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* S-expression reading code courtesy of Prabhakar Ragde, Professor at the
University of Waterloo.
From C Learning Materials, from the course CS146
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "Sexp.h"
#include "Helper.h"
#include "dbg.h"
extern FILE *input;
/* read_token: Read an S-expression token
*/
struct token read_token()
{
struct token t;
skip_whitespace();
int c = getc(input);
if( c == EOF )
{
t.tag = TEOF;
}
else if( c == '(' )
{
t.tag = LPAR;
}
else if( c == ')' )
{
t.tag = RPAR;
}
else if( isgraph(c) && !isdigit(c) )
{
t.tag = STR;
t.str = read_id(c);
debug("Read string: %s", t.str);
}
else if( isdigit(c) )
{
t.tag = INT;
t.num = read_nat(c);
debug("Read string: %d", t.num);
}
else
{
printf( "bad character: %c\n", c );
abort();
}
return t;
}
/* read_nat: Read a natural number, starting with the digit c
*/
int read_nat( int c )
{
int ans = c - '0';
while( isdigit( peekchar() ) )
{
c = getc(input);
ans = ans * 10 + c - '0';
}
return ans;
}
/* read_id: Read a symbol, starting with the character c
*/
char *read_id( int c )
{
char buf[100], *newstr;
buf[0] = c;
for( int i = 1; i < 100; i++ )
{
c = peekchar();
if( isalpha(c) || isdigit(c) )
{
buf[i] = getc(input);
}
else
{
buf[i] = '\0';
newstr = malloc( strlen(buf) + 1 );
check_mem(newstr);
strcpy( newstr, buf );
return newstr;
}
}
error:
return NULL;
}
/* miread: Read an s-expression into a node structure
Supported list elements:
-List
-Natural numbers
-Symbols
*/
struct node *miread()
{
struct token t;
struct node *newn;
t = read_token();
if( t.tag == TEOF )
{
return NULL;
}
else if ( t.tag == LPAR )
{
debug("Reading a list.");
newn = malloc(NSIZE);
check_mem(newn);
newn->tag = LST;
newn->sublst = read_list();
return newn;
}
else if( t.tag == RPAR )
{
printf( "unmatched right parenthesis\n" );
abort();
}
else
{
token_to_node(t);
}
error:
return NULL;
}
/* token_to_node: Convert a token structure into a node structure
*/
struct node *token_to_node( struct token t )
{
struct node *newn;
newn = malloc(NSIZE);
check_mem(newn);
if( t.tag == INT )
{
newn->tag = NUM;
newn->num = t.num;
}
else
{
newn->tag = NAME;
newn->str = t.str;
}
return newn;
error:
return NULL;
}
/* read_list: Read a list into a node structure
*/
struct node *read_list()
{
struct token t;
struct node *newn, *ans = NULL, **last = &ans;
while(1)
{
t = read_token();
if( t.tag == RPAR )
{
*last = NULL;
return ans;
}
else if( t.tag == LPAR )
{
newn = malloc( NSIZE );
if( newn == NULL )
{
printf( "Error: malloc out of memory\n" );
abort();
}
newn->tag = LST;
newn->sublst = read_list();
}
else if( t.tag == TEOF )
{
printf( "Unfinished list\n" );
abort();
}
else
{
newn = token_to_node( t );
}
*last = newn;
last = &newn->rest;
}
return newn;
}
/* free_sexp: free all the memory allocated by reading an s-expression
*/
void free_sexp( struct node *sexp )
{
if( sexp != NULL )
{
switch( sexp->tag )
{
case NAME:
free( sexp->str );
sexp->str = NULL;
break;
case NUM:
break;
case LST:
free_sexp( sexp->sublst );
sexp->sublst = NULL;
break;
default:
sentinel("Non-existent tag: %d", sexp->tag);
break;
}
free_sexp( sexp->rest );
sexp->rest = NULL;
free( sexp );
sexp = NULL;
}
error:
return;
}