-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp.c
571 lines (527 loc) · 14.4 KB
/
lisp.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
FILE * fp;
int c; // current
int flag_list;
/* ==== ==== ==== type ==== ==== ==== */
enum {
TNIL = 0,
TUNIT,
TENV,
TTRUE,
TFALSE,
TSYMBOL,
TCONS,
TLIST,
TFUN,
TINT,
TREAL
};
struct cell_s;
typedef struct cell_s {
int type_;
union {
// data
int int_;
float real_;
char * symbol_;
struct cell_s * car_;
};
struct cell_s * cdr_;
} Cell;
static Cell * Nil = &(Cell){ TNIL, .int_ = 0 };
static Cell * TRUE = &(Cell){ TTRUE, .int_ = 1 };
static Cell * FALSE = &(Cell){ TFALSE, .int_ = 0 };
static const char symbols[] = "+-*/!?=<>_:\\%#~&";
/* ==== ==== ==== ==== ==== ==== ==== */
/* ---- ---- make cell ---- ---- */
static Cell * make_cell (Cell * cell) {
Cell * r;
if ((r = malloc(1 * sizeof(Cell))) == NULL) {
printf("[error]: malloc\n");
exit(-1);
}
*r = *cell;
return r;
}
static Cell * cell_cons (Cell * cell) { return make_cell(&(Cell){ TCONS, .car_ = cell }); }
static Cell * cell_list (Cell * cell) { return make_cell(&(Cell){ TLIST, .car_ = cell }); }
static Cell * cell_int (int a) { return make_cell(&(Cell){ TINT, .int_=a }); }
static Cell * cell_real (float a) { return make_cell(&(Cell){ TREAL, .real_=a}); }
static Cell * cell_symbol (char * a) {
Cell * r = make_cell(&(Cell){ TSYMBOL, .int_ = 0 });
r->symbol_ = malloc( sizeof(char) * (strlen(a)+1) );
strcpy(r->symbol_, a);
return r;
}
/* ---- ---- ---- ---- ---- ---- */
/* ---- ---- lex tools ---- ---- */
#define next \
do { c = fgetc(fp); } while(0)
#define skip \
do { next; } while(c != '\n')
static inline int show_next (void) {
int a = fgetc(fp);
ungetc(a, fp);
return a;
}
static Cell * parse_num (int a, int neg) {
int int_or_real = 0;
int b = a - '0'; // int
float br = (float)(a - '0'); // real
while (isdigit(show_next())){
next;
b = b * 10 + (int)(c - '0'); // int
br = br * 10 + (float)(c - '0'); // real
}
if (show_next() == '.') {
int_or_real = 1;
next;
int k = 0;
while(isdigit(show_next())) {
next;
k++;
br = br * 10.0 + (float)(c - '0'); // real
}
for(int p = 0; p < k; p++) {
br = br / 10.0;
}
}
if (neg == 1) {
if (int_or_real == 0)
b = -b;
else
br = -br;
}
if (int_or_real == 0)
return cell_int(b);
else
return cell_real(br);
}
static Cell * parse_symbol (char a) {
char buf[256];
buf[0] = a;
int s = 1;
while (isalpha(show_next()) || isdigit(show_next()) || strchr(symbols, show_next())) {
next;
buf[s++] = c;
}
buf[s] = '\0';
return cell_symbol(buf);
}
/* ==== ==== ==== parser ==== ==== ==== */
static Cell * parse (void) {
for(;;) {
next;
if (c == ';') { // comment
skip;
continue;
}
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
continue;
if (c == EOF)
return Nil;
if (c == '-' && isdigit(show_next())) {
next;
Cell * r = parse_num(c, 1);
r->cdr_ = parse();
return r;
}
if (isdigit(c)) {
Cell * r = parse_num(c, 0);
r->cdr_ = parse();
return r;
}
if (isalpha(c) || strchr(symbols,c)) {
Cell * r = parse_symbol(c);
r->cdr_ = parse();
return r;
}
if (c == '\'' && show_next() == '(') {
next;
flag_list = 1;
Cell * r = cell_list(parse());
flag_list = 0;
r->cdr_ = parse();
return r;
}
if (c == '(') {
Cell * r;
if (flag_list == 1) r = cell_list(parse());
else r = cell_cons(parse());
r->cdr_ = parse();
return r;
}
if (c == ')') {
return Nil;
}
}
}
/* ==== ==== ==== ====== ==== ==== ==== */
/* ==== ==== ==== eval ==== ==== ==== */
static inline Cell * plus_eval (Cell*, Cell**);
static inline Cell * minus_eval (Cell*, Cell**);
static inline Cell * time_eval (Cell*, Cell**);
static inline Cell * divid_eval (Cell*, Cell**);
static inline Cell * mod_eval (Cell*, Cell**);
static inline Cell * great_eval (Cell*, Cell**);
static inline Cell * less_eval (Cell*, Cell**);
static inline Cell * equal_eval (Cell*, Cell**);
static inline Cell * and_eval (Cell*, Cell**);
static inline Cell * or_eval (Cell*, Cell**);
static inline Cell * if_eval (Cell*, Cell**);
static inline Cell * car_eval (Cell*, Cell**);
static inline Cell * cdr_eval (Cell*, Cell**);
static inline Cell * list_eval (Cell*, Cell**);
static inline Cell * def_eval (Cell*, Cell**);
static inline Cell * lambda_eval (Cell*, Cell**);
static inline Cell * print_eval (Cell*, Cell**);
static Cell * eval (Cell*, Cell**);
static Cell * set_lambda_args (Cell * name_l_, Cell * v_l_, Cell ** env ) {
Cell * local_env = *env;
Cell * name_l;
Cell * v_l;
for (name_l = name_l_, v_l = v_l_; name_l!=Nil && v_l!=Nil; name_l = name_l->cdr_, v_l = v_l->cdr_) {
Cell * A = make_cell(&(Cell){ TCONS, .car_=name_l->car_, .cdr_=eval(v_l,env) });
Cell * new_env = make_cell(&(Cell){ TCONS, .car_=A, .cdr_=local_env });
local_env = new_env;
}
return local_env;
}
#define primitive(n,s) \
do{if (strcmp(cell->symbol_,#s)==0) return n##_eval(args, env);}while(0)
static Cell * apply (Cell * cell, Cell * args, Cell ** env) {
switch(cell->type_){
case TSYMBOL:
primitive(plus, +);
primitive(minus, -);
primitive(time, *);
primitive(divid, /);
primitive(mod, mod);
primitive(great, >);
primitive(less, <);
primitive(equal, =);
primitive(and, and);
primitive(or, or);
primitive(if, if);
primitive(car, car);
primitive(cdr, cdr);
primitive(list, list);
primitive(def, define);
primitive(lambda, lambda);
primitive(print, print);
break;
case TFUN:{
Cell * local_env = *env;
local_env = set_lambda_args(cell->car_, args, env);
return eval(cell->cdr_, &local_env);
}
}
return Nil;
}
static Cell * find_symbol (Cell * cell, Cell ** env) {
for (Cell * E = *env; E != Nil; E = E->cdr_ ) {
if (strcmp(cell->symbol_,E->car_->symbol_)==0) {
return E->car_->cdr_;
}
}
return cell;
}
static Cell * eval (Cell * cell, Cell ** env) {
switch(cell->type_){
case TUNIT: case TENV:
break;
case TNIL: case TTRUE: case TFALSE: case TINT: case TREAL: case TFUN: case TLIST:
return cell;
case TCONS:
return apply( eval(cell->car_,env), cell->car_->cdr_, env);
case TSYMBOL:
return find_symbol(cell, env);
}
return eval(cell->cdr_, env);
}
// (+ _ ...)
static inline Cell * plus_eval (Cell * cell, Cell ** env) {
int result = 0;
float result_r = 0;
int type;
Cell * Tp = eval(cell, env);
if (Tp->type_ == TINT) // setting type && culc
result += Tp->int_;
else if (Tp->type_ == TREAL)
result_r += Tp->real_;
type = Tp->type_;
for (Cell * p = cell->cdr_; p != Nil; p = p->cdr_) { // culc
Cell * T = eval(p, env);
if (T->type_ == TINT && type == TINT)
result += T->int_;
else if (T->type_ == TREAL && type == TREAL)
result_r += T->real_;
else
perror("type error in '+'.");
}
if (type == TINT)
return cell_int(result);
else
return cell_real(result_r);
}
// (- _ ...)
static inline Cell * minus_eval (Cell * cell, Cell ** env) {
Cell * p = eval(cell,env);
int result;
float result_r;
if (p->type_ == TINT) // check type
result = p->int_;
else if (p->type_ == TREAL)
result_r = p->real_;
int type = p->type_;
for (p = cell->cdr_; p != Nil; p = p->cdr_) { // culc
Cell * T = eval(p, env);
if (T->type_ == TINT && type == TINT)
result -= T->int_;
else if (T->type_ == TREAL && type == TREAL)
result_r -= T->real_;
else
perror("type error in '-'.");
}
if (type == TINT)
return cell_int(result);
else
return cell_real(result_r);
}
// (* _ ...)
static inline Cell * time_eval (Cell * cell, Cell ** env) {
int result = 1;
float result_r = 1.0;
Cell * Tp = eval(cell, env);
if (Tp->type_ == TINT)
result *= Tp->int_;
else if (Tp->type_ == TREAL)
result_r *= Tp->real_;
int type = Tp->type_;
for (Cell * p = cell->cdr_; p != Nil; p = p->cdr_) {
Cell * T = eval(p, env);
if (T->type_ == TINT && type == TINT){
if (T->int_ != 0)
result *= T->int_;
else
return cell_int(0);
}
else if (T->type_ == TREAL && type == TREAL) {
if (T->real_ != 0)
result_r *= T->real_;
else
return cell_real(0);
}
else
perror("type error in '*'.");
}
if (type == TINT)
return cell_int(result);
else
return cell_real(result_r);
}
// (/ _ ...)
static inline Cell * divid_eval (Cell * cell, Cell ** env) {
Cell * p = eval(cell,env);
int type = p->type_;
int result;
float result_r;
if (type == TINT)
result = p->int_;
else if (type == TREAL)
result_r = p->real_;
for (p = cell->cdr_; p != Nil; p = p->cdr_){
Cell * T = eval(p,env);
if (T->type_ == TINT && type == TINT) {
if (T->int_ != 0)
result /= T->int_;
else{
perror("divided not have 0");
}
}else if (T->type_ == TREAL && type == TREAL) {
if (T->real_ != 0.0)
result_r /= T->real_;
else{
perror("divided not have 0");
}
}
else
printf("type error in '/'.");
}
if (type == TINT)
return cell_int(result);
else
return cell_real(result_r);
}
static inline Cell * mod_eval (Cell * cell, Cell ** env) {
Cell * L = eval(cell, env);
Cell * R = eval(cell->cdr_, env);
int type = L->type_;
if (type == TINT && R->type_ == TINT)
return cell_int(L->int_ % R->int_);
else
perror("type errpr in 'mod'.");
return Nil;
}
// (> _ _)
static inline Cell * great_eval (Cell * cell, Cell ** env) {
Cell * L = eval(cell, env);
Cell * R = eval(cell->cdr_, env);
if (L->type_ == TINT && R->type_ == TINT)
return (L->int_ > R->int_) ? TRUE : FALSE;
else if (L->type_ == TREAL && R->type_ == TREAL)
return (L->real_ > R->real_) ? TRUE : FALSE;
perror("type error in '>'");
return Nil;
}
// (< _ _)
static inline Cell * less_eval (Cell * cell, Cell ** env) {
Cell * L = eval(cell, env);
Cell * R = eval(cell->cdr_, env);
if (L->type_ == TINT && R->type_ == TINT)
return (L->int_ < R->int_) ? TRUE : FALSE;
else if (L->type_ == TREAL && R->type_ ==TREAL)
return (L->real_ < R->real_) ? TRUE : FALSE;
perror("type error in '<'");
return Nil;
}
// (= _ _)
static inline Cell * equal_eval (Cell * cell, Cell ** env) {
Cell * L = eval(cell, env);
Cell * R = eval(cell->cdr_, env);
if (L->type_ == TINT && R->type_ == TINT)
return (L->int_ == R->int_) ? TRUE : FALSE;
else if (L->type_ == TREAL && R->type_ == TREAL)
return (L->real_ == R->real_) ? TRUE : FALSE;
printf("equal error\n");
return Nil;
}
// (and _ ...)
static inline Cell * and_eval (Cell * cell, Cell ** env) {
for (Cell * p = cell; p != Nil; p=p->cdr_) {
Cell * T = eval(p, env);
if (T == FALSE)
return FALSE;
}
return TRUE;
}
// (or _ ...)
static inline Cell * or_eval (Cell * cell, Cell ** env) {
for (Cell * p = cell; p != Nil; p=p->cdr_) {
Cell * T = eval(p, env);
if (T == TRUE)
return TRUE;
}
return FALSE;
}
// (if _ _ _)
static inline Cell * if_eval (Cell * cell, Cell ** env) {
Cell * p = eval(cell,env);
if (p == TRUE) return eval (cell->cdr_, env);
if (p == FALSE) return eval (cell->cdr_->cdr_, env);
printf("if error\n");
return Nil;
}
// (car _)
static inline Cell * car_eval (Cell * cell, Cell ** env) {
return cell->car_;
}
// (cdr _)
static inline Cell * cdr_eval (Cell * cell, Cell ** env) {
return cell->car_->cdr_;
}
// (lambda (_ ...) _)
static inline Cell * lambda_eval (Cell * cell, Cell ** env) {
return make_cell(&(Cell){ TFUN, .car_=cell->car_, .cdr_=cell->cdr_ });
}
// (list ...)
static inline Cell * list_eval (Cell * cell, Cell ** env) {
Cell * r = eval(cell,env);
Cell * p = r;
for (Cell * t = cell->cdr_; t != Nil; t = t->cdr_) {
r->cdr_ = eval(t, env);
r = r->cdr_;
}
return cell_list(p);
}
// (define _ _)
static inline Cell * def_eval (Cell * cell, Cell ** env) {
Cell * new_env = NULL;
if (cell->type_ != TCONS) { // (define var body)
new_env = cell_cons(cell);
new_env->car_->cdr_ = eval(cell->cdr_, env);
new_env->cdr_ = *env;
*env = new_env;
} else { // (define (fname lvar) body)
Cell * fname = cell->car_;
Cell * lvars = cell->car_->cdr_;
Cell * body = cell->cdr_;
fname->cdr_ = make_cell(&(Cell){ TFUN, .car_=lvars, .cdr_=body });
new_env = cell_cons(fname);
new_env->cdr_ = *env;
*env = new_env;
}
return new_env;
}
// (print _)
static inline void print_eval_iter (Cell * cell, Cell ** env) {
switch(cell->type_) {
case TNIL: printf("nil"); break;
case TTRUE: printf("#t"); break;
case TFALSE: printf("#f"); break;
case TINT: printf("%d", cell->int_); break;
case TREAL: printf("%f", cell->real_); break;
case TFUN: printf("lambda function"); break;
case TCONS: print_eval_iter(eval(cell, env), env); break;
case TLIST: {
printf("(");
for (Cell * r = cell->car_; r != Nil; r = r->cdr_) {
print_eval_iter(r, env);
if (r->cdr_ != Nil) printf(" ");
}
printf(")");
break;
}
case TSYMBOL:
print_eval_iter(find_symbol(cell, env), env); break;
case TENV:
printf("env"); break;
default:
printf("print nothing");
}
}
static inline Cell * print_eval (Cell * cell, Cell ** env) {
print_eval_iter(cell, env);
printf("\n");
return make_cell(&(Cell){TUNIT});
}
/* ==== ==== ==== ==== ==== ==== ==== */
/* ==== ==== ==== main loop ==== ==== ==== */
static void file_read_mode(char* argv[], Cell* E) {
fp = fopen(argv[1], "r");
Cell * R = parse();
do {
eval(R, &E);
R = R->cdr_;
} while( R != Nil );
}
int main (int argc, char* argv[])
{
Cell * E = Nil;
flag_list = 0;
if (argc <= 1) {
perror("no input file.");
exit(1);
}
if (argv[1]){
if (strcmp( ".scm" ,strstr(argv[1],".") ) != 0)
perror("file format is not .scm");
else {
file_read_mode(argv, E);
}
}
return 0;
}