-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsexp.c
532 lines (465 loc) · 8.97 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
typedef unsigned int symbol_t;
struct pair_s; /* Predeclaration */
typedef struct pair_s pair_t;
#define MAX_SYMBOLS 512
#define MAX_SYMBOL_LEN 512 /* The length including the terminating \0 */
static char *symbol_table[MAX_SYMBOLS];
symbol_t symbol_idx;
typedef enum {
NIL_TYPE,
PAIR_TYPE,
SYMBOL_TYPE,
INTEGER_TYPE,
STRING_TYPE
} object_type_t;
typedef struct {
object_type_t type;
union {
pair_t *pair;
symbol_t symbol;
int integer;
char *string;
};
} object_t;
struct pair_s {
object_t *head;
object_t *tail;
};
const symbol_t NULL_SYMBOL = 0;
object_t nil_; // TODO initialize
object_t *nil;
object_t *make_symbol_object(symbol_t s);
void write(object_t *a);
bool nil_p(object_t *o);
bool symbol_p(object_t *o);
bool pair_p(object_t *o);
bool string_p(object_t *o);
bool integer_p(object_t *i);
object_t *head(object_t *o);
object_t *tail(object_t *o);
void nl() { printf("\n"); }
void fatal_error(const char *message);
void
init_nil() {
nil_.type = NIL_TYPE;
nil = &nil_;
}
void toupper_str(char *str) {
int i;
for (i = 0;
i < MAX_SYMBOL_LEN && str[i] != '\0';
++i) {
str[i] = toupper(str[i]);
}
}
symbol_t
insert_symbol(char *str) {
if (symbol_idx < MAX_SYMBOLS) {
/* FIXME: unsafe if str is not null terminated */
int len = strlen(str)+1; /* +1 for \0 */
char *new_mem = (char *)calloc(len, sizeof(char));
if (new_mem == NULL) {
fprintf(stderr, "Could not allocate\n");
return NULL_SYMBOL;
} else {
symbol_table[symbol_idx] = new_mem;
strncpy(symbol_table[symbol_idx], str, len);
toupper_str(symbol_table[symbol_idx]);
return symbol_idx++;
}
} else {
return NULL_SYMBOL;
}
}
void
init_symbol_table() {
for (int i = 0; i < MAX_SYMBOLS; ++i) {
symbol_table[i] = NULL;
}
symbol_idx = 0;
insert_symbol("NULL_SYMBOL");
}
symbol_t
find_symbol(char *str) {
char buf[MAX_SYMBOL_LEN];
strncpy(buf, str, MAX_SYMBOL_LEN);
toupper_str(buf);
for (int i = 0; i < symbol_idx; ++i) {
if (strncmp(buf, symbol_table[i], MAX_SYMBOL_LEN) == 0) {
return i;
}
}
return NULL_SYMBOL;
}
symbol_t
intern_symbol(char *sym_name) {
symbol_t s = find_symbol(sym_name);
if (s != NULL_SYMBOL) {
return s;
} else {
/* Nothing found */
s = insert_symbol(sym_name);
if (s == NULL_SYMBOL) {
fatal_error("ERROR: symbol table full.");
} else {
return s;
}
}
}
char *
symbol_str(symbol_t s) {
if (s < MAX_SYMBOLS) {
return symbol_table[s];
} else {
return NULL;
}
}
object_t *
intern(char *name) {
return make_symbol_object(intern_symbol(name));
}
pair_t *
pair(object_t *head, object_t *tail) {
pair_t *c = (pair_t *)malloc(sizeof(pair_t));
if (c == NULL) {
fatal_error("Error: Could not allocate pair cell.");
} else {
c->head = head;
c->tail = tail;
return c;
}
}
object_t *
make_symbol_object(symbol_t s) {
object_t *a = (object_t *)malloc(sizeof(object_t));
if (a == NULL) {
fatal_error("Error: Could not allocate object.");
} else {
a->type = SYMBOL_TYPE;
a->symbol = s;
return a;
}
}
/* Does not unintern it. i.e it will still be present in symbol table */
void
free_symbol_object(object_t *o) {
free(o);
}
object_t *
make_integer_object(int i) {
object_t *a = (object_t *)malloc(sizeof(object_t));
if (a == NULL) {
fatal_error("Error: Could not allocate object.");
} else {
a->type = INTEGER_TYPE;
a->integer = i;
return a;
}
}
void
free_integer_object(object_t *o) {
free(o);
}
object_t *
make_pair_object(pair_t *c) {
object_t *a = (object_t *)malloc(sizeof(object_t));
if (a == NULL) {
fatal_error("Error: Could not allocate object.");
} else {
a->type = PAIR_TYPE;
a->pair = c;
return a;
}
}
void
free_pair_object(object_t *o) {
free(o->pair);
free(o);
}
object_t *
make_string_object(char *str) {
object_t *o = (object_t *)malloc(sizeof(object_t));
if (o == NULL) {
fatal_error("Error: Could not allocate object.");
} else {
o->type = STRING_TYPE;
int str_len = strlen(str);
char *new_str = (char *)calloc(str_len+1, sizeof(char));
if (str == NULL) {
fatal_error("Error: Could not allocate object.");
}
strncpy(new_str, str, str_len);
o->string = str;
return o;
}
}
void
free_string_object(object_t *o) {
free(o->string);
free(o);
}
void
free_object(object_t *o) {
switch (o->type) {
case NIL_TYPE:
break;
case PAIR_TYPE:
free_pair_object(o);
break;
case SYMBOL_TYPE:
free_symbol_object(o);
break;
case INTEGER_TYPE:
free_integer_object(o);
break;
case STRING_TYPE:
free_string_object(o);
break;
default:
printf("Error: Unknown type object 0x%p.", o);
}
}
void
free_object_rec(object_t *o) {
if (pair_p(o)) {
free_object(head(o));
free_object(tail(o));
} else {
free_object(o);
}
}
object_t *
cons(object_t *head, object_t *tail) {
return make_pair_object(pair(head, tail));
}
void
write_pair(pair_t *c) {
printf("(");
write(c->head);
printf(" . ");
write(c->tail);
printf(")");
}
bool
nil_p(object_t *o) {
return (o->type == NIL_TYPE);
}
bool
pair_p(object_t *o) {
return (o->type == PAIR_TYPE);
}
bool
symbol_p(object_t *o) {
return (o->type == SYMBOL_TYPE);
}
bool
integer_p(object_t *o) {
return (o->type == INTEGER_TYPE);
}
bool
string_p(object_t *o) {
return (o->type == STRING_TYPE);
}
object_t *
head(object_t *o) {
assert(pair_p(o));
return o->pair->head;
}
object_t *
tail(object_t *o) {
assert(pair_p(o));
return o->pair->tail;
}
void
fatal_error(const char *message) {
fprintf(stderr, message);
nl();
exit(-1);
}
bool
proper_list_p(object_t *l) {
if (!pair_p(l)) {
return false;
} else if (nil_p(tail(l))) {
return true;
} else {
return proper_list_p(tail(l));
}
}
bool
dotted_pair_p(object_t *p) {
if (!pair_p(p)) {
return false;
} else if (nil_p(tail(p))) {
return false;
} else {
return true;
}
}
bool
dotted_list_p(object_t *l) {
if (! pair_p(l)) {
return false;
} else {
if (nil_p(tail(l))) { /* It's a proper list */
return false;
} else if (! pair_p(tail(l))) { /* dotted pair */
return true;
} else {
return dotted_list_p(tail(l));
}
}
}
void
write(object_t *a) {
switch (a->type) {
case NIL_TYPE:
printf("NIL");
break;
case PAIR_TYPE:
write_pair(a->pair);
break;
case SYMBOL_TYPE:
printf("%s", symbol_str(a->symbol));
break;
case INTEGER_TYPE:
printf("%d", a->integer);
break;
case STRING_TYPE:
printf("%s", a->string);
break;
default:
printf("Error: Unknown type object 0x%p.", a);
}
}
void
write_proper_list(object_t *l) {
printf("(");
do {
write(head(l));
l = tail(l);
if (!nil_p(l)) {
printf(" ");
} else {
break;
}
} while (true);
printf(")");
}
char sexp[] = "(foo (bar 1 2 3) \"faz\")";
bool
symbol_char_p(char c) {
return (c != '(' &&
c != ')' &&
c != ' ' &&
c != '\n' &&
c != '\r' &&
c != '\f' &&
c != '\b' &&
c != '\a' &&
c != '\"');
}
object_t *
read_symbol(const char *str, int *count) {
char buffer[MAX_SYMBOL_LEN] = {0};
char *bufptr = buffer;
while (*str != '\0') {
if (symbol_char_p(*str)) {
*bufptr = *str;
(*count)++;
str++;
bufptr++;
} else {
break;
}
}
return intern(buffer);
}
/* FIXME: bullshit since '-' is only allowed at beginning */
bool
integer_char_p(char c) {
return (isdigit(c) || c == '-');
}
/* FIXME: not actually robust */
object_t *
read_integer(const char *str, int *count) {
char buffer[MAX_SYMBOL_LEN] = {0};
char *bufptr = buffer;
while (*str != '\0') {
if (integer_char_p(*str)) {
*bufptr = *str;
(*count)++;
str++;
bufptr++;
} else {
break;
}
}
return make_integer_object(atoi(buffer));
}
object_t *
read_atom(const char *str, int *count) {
if (symbol_char_p(*str)) {
return read_symbol(str, count);
} else if (integer_char_p(*str)) {
return read_integer(str, count);
} else {
fatal_error("Error: Not implemented");
}
}
object_t *
read(const char *str, int *count) {
object_t *current_pair, *car, *cdr;
do {
switch (*str) {
case '(':
++str;
(*count)++;
car = read(str, count);
case ')':
++str;
}
} while (str != '\0');
}
int
main() {
object_t *foo, *bar, *baz, *ten, *c;
init_symbol_table();
init_nil();
foo = intern("foo");
bar = intern("bar");
baz = intern("baz");
ten = make_integer_object(10);
c = make_string_object("tets");
c = cons(ten, nil);
c = cons(baz, c);
c = cons(bar, c);
c = cons(foo, c);
/* write(nil); */
/* printf(" %d %d", proper_list_p(nil), dotted_list_p(nil)); nl(); */
/* write(c); */
/* printf(" %d %d", proper_list_p(c), dotted_list_p(c)); nl(); */
/* write_proper_list(c); nl(); */
/* c = cons(foo, bar); */
/* write(c); */
/* printf(" %d %d", proper_list_p(c), dotted_list_p(c)); nl(); */
/* c = cons(foo, nil); */
/* write(c); */
/* printf(" %d %d", proper_list_p(c), dotted_list_p(c)); nl(); */
int read = 0;
c = read_atom("foo bar", &read);
write(c);
printf(" %d\n", read);
free_object_rec(c);
c = read_atom("2100 ", &read);
write(c);
printf(" %d\n", read);
free_object_rec(c);
return 0;
}