-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathyoctolisp.c
866 lines (760 loc) · 20.4 KB
/
yoctolisp.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
/*
Copyright (c) 2013 Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
// Yoctolisp: a miniature lisp interpreter written in a weekend.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
//#define GC_DEBUG
#ifdef GC_DEBUG // How many objects to allocate before triggering a GC?
#define GC_ALLOC_TRIGGER 1
#else
#define GC_ALLOC_TRIGGER 100
#endif
typedef enum {
YLISP_CELL, YLISP_STRING, YLISP_NUMBER, YLISP_BOOLEAN, YLISP_SYMBOL,
YLISP_CONTEXT, YLISP_FUNCTION, YLISP_BUILTIN
} YLispValueType;
enum {
KWD_IF, KWD_COND, KWD_QUOTE, KWD_LAMBDA, KWD_LET, KWD_DEFINE,
KWD_BEGIN, NUM_KEYWORDS
};
typedef struct _YLispValue *(*YLispBuiltin)(struct _YLispValue *args);
typedef struct _YLispValue {
unsigned int marked :1;
unsigned int type :31;
union {
unsigned int i;
char *s;
struct {
struct _YLispValue *cdr, *car;
} cell;
struct _YLispValue *symname;
struct {
struct _YLispValue *parent, *vars;
} context;
struct {
struct _YLispValue *context, *code;
} func;
YLispBuiltin builtin;
} v;
struct _YLispValue *next;
} YLispValue;
#define CAR(val) ((val)->v.cell.car)
#define CDR(val) ((val)->v.cell.cdr)
typedef enum {
TOKEN_OPEN_PAREN, TOKEN_CLOSE_PAREN, TOKEN_LITERAL, TOKEN_QUOTE,
TOKEN_ERROR, TOKEN_EOF, TOKEN_PERIOD
} YLispToken;
typedef struct {
char *buffer;
unsigned int position;
YLispValue *value;
} YLispLexer;
typedef struct _GCPinnedVariable {
YLispValue **variable;
struct _GCPinnedVariable *next;
} GCPinnedVariable;
static const char *keyword_names[] = {
"if", "cond", "quote", "lambda", "let", "define", "begin"
};
static YLispValue *values = NULL; // All values linked list
static unsigned int values_alloc_count = 0; // Allocated since last GC
static GCPinnedVariable *pinned_vars = NULL;
static YLispValue *keywords[NUM_KEYWORDS];
// Symbol table; dynamically resizing.
static YLispValue **symbols = NULL;
static unsigned int num_symbols = 0;
static YLispValue *root_context;
// A deferred call, passed up the stack (only one ever exists)
static YLispValue deferred_call;
static YLispValue *ylisp_value(YLispValueType type)
{
YLispValue *result = calloc(1, sizeof(YLispValue));
assert(result != NULL);
result->type = type;
result->next = values; values = result;
++values_alloc_count;
return result;
}
static YLispValue *ylisp_number(YLispValueType type, unsigned int val)
{
YLispValue *result = ylisp_value(type);
result->v.i = val;
return result;
}
static YLispValue *ylisp_cons(YLispValue *car, YLispValue *cdr)
{
YLispValue *result = ylisp_value(YLISP_CELL);
CAR(result) = car; CDR(result) = cdr;
return result;
}
void ylisp_print(YLispValue *value);
static void print_list(YLispValue *value, char *inner)
{
printf("(%s", inner);
while (value != NULL) {
if (value->type != YLISP_CELL) {
printf(". ");
ylisp_print(value);
break;
}
ylisp_print(CAR(value));
value = CDR(value);
if (value != NULL) {
printf(" ");
}
}
printf(")");
}
void ylisp_print(YLispValue *value)
{
if (value == NULL) {
printf("()");
} else switch (value->type) {
case YLISP_CELL:
print_list(value, "");
break;
case YLISP_STRING:
printf("\"%s\"", value->v.s);
break;
case YLISP_NUMBER:
printf("%i", value->v.i);
break;
case YLISP_BOOLEAN:
printf("#%c", value->v.i ? 't' : 'f');
break;
case YLISP_SYMBOL:
printf("%s", value->v.symname->v.s);
break;
case YLISP_CONTEXT:
printf("<context>");
break;
case YLISP_FUNCTION:
print_list(value->v.func.code, "lambda ");
break;
case YLISP_BUILTIN:
printf("<builtin>");
break;
}
}
static int ylisp_equal(YLispValue *v1, YLispValue *v2)
{
if (v1 == NULL || v2 == NULL) {
return v1 == v2;
}
if (v1->type != v2->type) {
return 0;
}
switch (v1->type) {
case YLISP_STRING:
return !strcmp(v1->v.s, v2->v.s);
case YLISP_NUMBER:
case YLISP_BOOLEAN:
return v1->v.i == v2->v.i;
default:
return v1 == v2;
}
}
static int ylisp_lt(YLispValue *v1, YLispValue *v2)
{
if (v1 == NULL || v2 == NULL || v1->type != v2->type) {
return 0;
}
switch (v1->type) {
case YLISP_STRING:
return strcmp(v1->v.s, v2->v.s) < 0;
case YLISP_NUMBER:
case YLISP_BOOLEAN:
return v1->v.i < v2->v.i;
default:
return 0;
}
}
static void mark_value(YLispValue *value)
{
if (value == NULL || value->marked) {
return;
}
value->marked = 1;
switch (value->type) {
case YLISP_CELL:
mark_value(value->v.cell.cdr);
mark_value(value->v.cell.car);
break;
case YLISP_SYMBOL:
mark_value(value->v.symname);
break;
case YLISP_CONTEXT:
mark_value(value->v.context.parent);
mark_value(value->v.context.vars);
break;
case YLISP_FUNCTION:
mark_value(value->v.func.context);
mark_value(value->v.func.code);
break;
default: break;
}
}
static void mark_roots(void)
{
GCPinnedVariable *pin;
unsigned int i;
for (i = 0; i < num_symbols; ++i) {
mark_value(symbols[i]);
}
for (pin = pinned_vars; pin != NULL; pin = pin->next) {
mark_value(*pin->variable);
}
mark_value(root_context);
}
static void free_value(YLispValue *value)
{
if (value->type == YLISP_STRING) {
free(value->v.s);
}
memset(value, 0xff, sizeof(*value)); free(value);
}
static void sweep(void)
{
YLispValue **v;
#ifdef GC_DEBUG
for (v = &values; *v != NULL; v = &(*v)->next) {
if (!(*v)->marked) {
printf("Sweep %p: ", *v);
ylisp_print(*v);
printf("\n");
}
}
#endif
for (v = &values; *v != NULL; v = &(*v)->next) {
while (!(*v)->marked) {
YLispValue *next = (*v)->next;
free_value(*v);
*v = next;
if (next == NULL)
return;
}
(*v)->marked = 0;
}
}
static void run_gc(void)
{
mark_roots();
sweep();
values_alloc_count = 0;
}
static void pin_variable(YLispValue **variable)
{
GCPinnedVariable *pinned_var = malloc(sizeof(GCPinnedVariable));
assert(pinned_var != NULL);
pinned_var->variable = variable;
pinned_var->next = pinned_vars; pinned_vars = pinned_var;
}
static void unpin_variable(YLispValue **variable)
{
GCPinnedVariable **v;
for (v = &pinned_vars; *v != NULL; v = &(*v)->next) {
if ((*v)->variable == variable) {
GCPinnedVariable *next = (*v)->next;
free(*v);
*v = next;
return;
}
}
assert(0);
}
static YLispValue *string_from_data(const char *data, size_t data_len)
{
YLispValue *value;
char *s = malloc(data_len + 1); assert(s != NULL);
memcpy(s, data, data_len); s[data_len] = '\0';
value = ylisp_value(YLISP_STRING);
value->v.s = s;
return value;
}
YLispValue *ylisp_symbol_for_name(const char *name, size_t name_len)
{
YLispValue *result, *symname;
unsigned int i;
for (i = 0; i < num_symbols; ++i) {
symname = symbols[i]->v.symname;
if (strlen(symname->v.s) == name_len
&& memcmp(symname->v.s, name, name_len) == 0) {
return symbols[i];
}
}
result = ylisp_value(YLISP_SYMBOL);
result->v.symname = string_from_data(name, name_len);
symbols = realloc(symbols, sizeof(*symbols) * (num_symbols + 1));
assert(symbols != NULL);
return symbols[num_symbols++] = result;
}
void ylisp_init_lexer(YLispLexer *lexer, char *buf)
{
lexer->buffer = buf;
lexer->position = 0;
}
static int is_sym_char(char c)
{
return !isspace(c) && strchr("()[]{}\",'`;#|\\", c) == NULL;
}
#define c lexer->buffer[lexer->position]
static YLispToken ylisp_read_string(YLispLexer *lexer)
{
unsigned int start = lexer->position;
while (c != '\0' && c != '"') {
++lexer->position;
}
if (c == '\0')
return TOKEN_ERROR;
lexer->value = string_from_data(lexer->buffer + start,
lexer->position - start);
++lexer->position;
return TOKEN_LITERAL;
}
YLispToken ylisp_read_token(YLispLexer *lexer)
{
while (c != '\0') {
if (c == ';') {
do {
++lexer->position;
if (c == '\0')
return TOKEN_ERROR;
} while (c != '\n');
} else if (!isspace(c)) {
break;
}
++lexer->position;
}
if (c == '\0')
return TOKEN_EOF;
switch (lexer->buffer[lexer->position++]) {
case '.': return TOKEN_PERIOD;
case '(': return TOKEN_OPEN_PAREN;
case ')': return TOKEN_CLOSE_PAREN;
case '\'': return TOKEN_QUOTE;
case '#':
lexer->value = ylisp_number(YLISP_BOOLEAN, c == 't');
if (c != 't' && c != 'f')
return TOKEN_ERROR;
++lexer->position;
return TOKEN_LITERAL;
case '"': return ylisp_read_string(lexer);
default: --lexer->position; break;
}
if (isdigit(c)) {
lexer->value = ylisp_value(YLISP_NUMBER);
lexer->value->v.i = 0;
while (c != '\0' && isdigit(c)) {
lexer->value->v.i = lexer->value->v.i * 10 + (c - '0');
++lexer->position;
}
return TOKEN_LITERAL;
} else if (is_sym_char(c)) {
unsigned int start = lexer->position;
while (c != '\0' && is_sym_char(c))
++lexer->position;
lexer->value = ylisp_symbol_for_name(lexer->buffer + start,
lexer->position - start);
return TOKEN_LITERAL;
} else {
return TOKEN_ERROR;
}
}
#undef c
static YLispValue *parse_from_token(YLispLexer *lexer, YLispToken token);
static YLispValue *parse_list(YLispLexer *lexer)
{
YLispValue *result, **rover=&result;
for (;;) {
YLispToken token = ylisp_read_token(lexer);
if (token == TOKEN_EOF || token == TOKEN_ERROR)
return NULL;
if (token == TOKEN_CLOSE_PAREN)
break;
// (x y . z) syntax:
if (token == TOKEN_PERIOD) {
token = ylisp_read_token(lexer);
*rover = parse_from_token(lexer, token);
token = ylisp_read_token(lexer);
if (token != TOKEN_CLOSE_PAREN)
return NULL;
return result;
}
*rover = ylisp_value(YLISP_CELL);
CAR(*rover) = parse_from_token(lexer, token);
rover = &CDR(*rover);
}
*rover = NULL; // end of list
return result;
}
static YLispValue *parse_quoted(YLispLexer *lexer)
{
YLispToken token = ylisp_read_token(lexer);
return ylisp_cons(keywords[KWD_QUOTE],
ylisp_cons(parse_from_token(lexer, token), NULL));
}
static YLispValue *parse_from_token(YLispLexer *lexer, YLispToken token)
{
switch (token) {
case TOKEN_OPEN_PAREN:
return parse_list(lexer);
case TOKEN_QUOTE:
return parse_quoted(lexer);
case TOKEN_LITERAL:
return lexer->value;
default:
break;
}
return NULL;
}
YLispValue *ylisp_parse(YLispLexer *lexer)
{
YLispToken token = ylisp_read_token(lexer);
return parse_from_token(lexer, token);
}
YLispValue *ylisp_eval(YLispValue *context, YLispValue *code);
static YLispValue *eval_variable(YLispValue *context, YLispValue *var)
{
YLispValue *v;
while (context != NULL) {
for (v = context->v.context.vars; v != NULL; v = CDR(v)) {
if (CAR(CAR(v)) == var) {
return CDR(CAR(v));
}
}
context = context->v.context.parent;
}
fprintf(stderr, "Undefined variable: %s\n",
var->v.symname->v.s);
abort();
}
static YLispValue *set_variable(YLispValue *context, YLispValue *name,
YLispValue *value)
{
YLispValue *v;
for (v = context->v.context.vars; v != NULL; v = CDR(v)) {
if (CAR(CAR(v)) == name) {
CDR(CAR(v)) = value;
return value;
}
}
context->v.context.vars = ylisp_cons(ylisp_cons(name, value),
context->v.context.vars);
return value;
}
static YLispValue *defer_eval(YLispValue *context, YLispValue *code)
{
if (code->type != YLISP_CELL) {
return ylisp_eval(context, code);
}
deferred_call.v.func.context = context;
deferred_call.v.func.code = code;
return &deferred_call;
}
static YLispValue *run_function_body(YLispValue *context, YLispValue *code)
{
for (; code != NULL; code = CDR(code)) {
if (CDR(code) == NULL) {
return defer_eval(context, CAR(code));
} else {
ylisp_eval(context, CAR(code));
}
}
return NULL;
}
static YLispValue *eval_func_args(YLispValue *context, YLispValue *code)
{
YLispValue *args = NULL, **a = &args, *c;
pin_variable(&args);
for (c = code; c != NULL; c = CDR(c)) {
*a = ylisp_value(YLISP_CELL);
CAR(*a) = ylisp_eval(context, CAR(c));
a = &CDR(*a);
}
*a = NULL;
unpin_variable(&args);
return args;
}
static YLispValue *eval_func_call(YLispValue *context, YLispValue *code)
{
YLispValue *func = ylisp_eval(context, CAR(code));
if (func->type == YLISP_BUILTIN) {
YLispValue *result, *args = eval_func_args(context, CDR(code));
pin_variable(&args);
result = func->v.builtin(args);
unpin_variable(&args);
return result;
} else if (func->type == YLISP_FUNCTION) {
YLispValue *n, *c, *result;
YLispValue *newcontext = ylisp_value(YLISP_CONTEXT);
pin_variable(&newcontext);
newcontext->v.context.parent = func->v.func.context;
newcontext->v.context.vars = NULL;
c = CDR(code);
for (n = CAR(func->v.func.code); n != NULL; n = CDR(n)) {
// varargs:
if (n->type == YLISP_SYMBOL) {
set_variable(newcontext, n,
eval_func_args(context, c));
break;
}
set_variable(newcontext, CAR(n),
ylisp_eval(context, CAR(c)));
c = CDR(c);
}
result = run_function_body(newcontext, CDR(func->v.func.code));
unpin_variable(&newcontext);
return result;
} else {
fprintf(stderr, "Invalid function call\n");
abort();
}
}
static YLispValue *eval_list_inner(YLispValue *context, YLispValue *code)
{
YLispValue *first = CAR(code);
// Special cases:
if (first == keywords[KWD_QUOTE]) {
return CAR(CDR(code));
} else if (first == keywords[KWD_IF]) {
YLispValue *val = ylisp_eval(context, CAR(CDR(code)));
assert(val != NULL && (val->type == YLISP_NUMBER
|| val->type == YLISP_BOOLEAN));
if (val->v.i) {
return defer_eval(context, CAR(CDR(CDR(code))));
} else if (CDR(CDR(CDR(code))) != NULL) {
return defer_eval(context, CAR(CDR(CDR(CDR(code)))));
}
} else if (first == keywords[KWD_COND]) {
YLispValue *tests, *val;
for (tests = CDR(code); tests != NULL; tests = CDR(tests)) {
val = ylisp_eval(context, CAR(CAR(tests)));
assert(val != NULL && (val->type == YLISP_NUMBER
|| val->type == YLISP_BOOLEAN));
if (val->v.i)
return run_function_body(context,
CDR(CAR(tests)));
}
return NULL;
} else if (first == keywords[KWD_LAMBDA]) {
YLispValue *result = ylisp_value(YLISP_FUNCTION);
result->v.func.context = context;
result->v.func.code = CDR(code);
return result;
} else if (first == keywords[KWD_DEFINE]) {
// (define (func x) ...
if (CAR(CDR(code))->type == YLISP_CELL) {
YLispValue *func = ylisp_value(YLISP_FUNCTION);
func->v.func.context = context;
func->v.func.code = ylisp_cons(CDR(CAR(CDR(code))),
CDR(CDR(code)));
return set_variable(root_context, CAR(CAR(CDR(code))),
func);
} else {
return set_variable(root_context, CAR(CDR(code)),
ylisp_eval(context, CAR(CDR(CDR(code)))));
}
} else if (first == keywords[KWD_LET]) {
YLispValue *newcontext = ylisp_value(YLISP_CONTEXT);
YLispValue *vars, *result;
newcontext->v.context.parent = context;
pin_variable(&newcontext);
for (vars = CAR(CDR(code)); vars != NULL; vars = CDR(vars)) {
set_variable(newcontext, CAR(CAR(vars)),
ylisp_eval(newcontext,
CAR(CDR(CAR(vars)))));
}
result = run_function_body(newcontext, CDR(CDR(code)));
unpin_variable(&newcontext);
return result;
} else if (first == keywords[KWD_BEGIN]) {
return run_function_body(context, CDR(code));
}
return eval_func_call(context, code);
}
static YLispValue *eval_list(YLispValue *context, YLispValue *code)
{
YLispValue *result = eval_list_inner(context, code);
// Tail call optimization: expand deferred call.
while (result == &deferred_call) {
context = deferred_call.v.func.context;
code = deferred_call.v.func.code;
pin_variable(&context);
result = eval_list_inner(context, code);
unpin_variable(&context);
}
return result;
}
YLispValue *ylisp_eval(YLispValue *context, YLispValue *code)
{
if (values_alloc_count > GC_ALLOC_TRIGGER)
run_gc();
switch (code->type) {
case YLISP_SYMBOL: // Variable
return eval_variable(context, code);
case YLISP_CELL: // Function call or other.
return eval_list(context, code);
default: // Literals.
break;
}
return code;
}
static YLispValue *builtin_add(YLispValue *args)
{
return ylisp_number(YLISP_NUMBER,
CAR(args)->v.i + CAR(CDR(args))->v.i);
}
static YLispValue *builtin_sub(YLispValue *args)
{
return ylisp_number(YLISP_NUMBER,
CAR(args)->v.i - CAR(CDR(args))->v.i);
}
static YLispValue *builtin_mul(YLispValue *args)
{
return ylisp_number(YLISP_NUMBER,
CAR(args)->v.i * CAR(CDR(args))->v.i);
}
static YLispValue *builtin_div(YLispValue *args)
{
return ylisp_number(YLISP_NUMBER,
CAR(args)->v.i / CAR(CDR(args))->v.i);
}
static YLispValue *builtin_eq(YLispValue *args)
{
return ylisp_number(YLISP_BOOLEAN,
ylisp_equal(CAR(args), CAR(CDR(args))));
}
static YLispValue *builtin_lt(YLispValue *args)
{
return ylisp_number(YLISP_BOOLEAN,
ylisp_lt(CAR(args), CAR(CDR(args))));
}
static YLispValue *builtin_and(YLispValue *args)
{
return ylisp_number(YLISP_BOOLEAN,
CAR(args)->v.i && CAR(CDR(args))->v.i);
}
static YLispValue *builtin_or(YLispValue *args)
{
return ylisp_number(YLISP_BOOLEAN,
CAR(args)->v.i || CAR(CDR(args))->v.i);
}
static YLispValue *builtin_not(YLispValue *args)
{
return ylisp_number(YLISP_BOOLEAN, !CAR(args)->v.i);
}
static YLispValue *builtin_car(YLispValue *args) { return CAR(CAR(args)); }
static YLispValue *builtin_cdr(YLispValue *args) { return CDR(CAR(args)); }
static YLispValue *builtin_cons(YLispValue *args)
{
return ylisp_cons(CAR(args), CAR(CDR(args)));
}
static YLispValue *builtin_read(YLispValue *args)
{
// TODO: Make this not suck
YLispLexer lexer;
char buf[256];
printf("> "); fflush(stdout);
fgets(buf, sizeof(buf), stdin); buf[sizeof(buf) - 1] = '\0';
ylisp_init_lexer(&lexer, buf);
return ylisp_parse(&lexer);
}
static YLispValue *builtin_eval(YLispValue *args)
{
return ylisp_eval(root_context, CAR(args));
}
static YLispValue *builtin_print(YLispValue *args)
{
ylisp_print(CAR(args)); printf("\n");
return NULL;
}
static void define_builtin(char *name, YLispBuiltin callback)
{
YLispValue *builtin = ylisp_value(YLISP_BUILTIN);
builtin->v.builtin = callback;
set_variable(root_context, ylisp_symbol_for_name(name, strlen(name)),
builtin);
}
void ylisp_init(void)
{
unsigned int i;
for (i = 0; i < NUM_KEYWORDS; ++i) {
keywords[i] = ylisp_symbol_for_name(
keyword_names[i], strlen(keyword_names[i]));
}
root_context = ylisp_value(YLISP_CONTEXT);
define_builtin("builtin-add", builtin_add);
define_builtin("builtin-sub", builtin_sub);
define_builtin("builtin-mul", builtin_mul);
define_builtin("builtin-div", builtin_div);
define_builtin("builtin-lt", builtin_lt);
define_builtin("builtin-eq", builtin_eq);
define_builtin("builtin-and", builtin_and);
define_builtin("builtin-or", builtin_or);
define_builtin("not", builtin_not);
define_builtin("car", builtin_car);
define_builtin("cdr", builtin_cdr);
define_builtin("cons", builtin_cons);
define_builtin("read", builtin_read);
define_builtin("eval", builtin_eval);
define_builtin("print", builtin_print);
}
static char *read_file(char *filename)
{
char *result;
unsigned int file_size;
FILE *fs = fopen(filename, "r");
if (fs == NULL) {
perror("fopen");
exit(-1);
}
fseek(fs, 0, SEEK_END);
file_size = ftell(fs);
fseek(fs, 0, SEEK_SET);
result = malloc(file_size + 1); assert(result != NULL);
fread(result, 1, file_size, fs);
result[file_size] = '\0';
return result;
}
static void process_file(char *filename)
{
YLispValue *code = NULL;
YLispLexer lexer;
char *infile;
infile = read_file(filename);
ylisp_init_lexer(&lexer, infile);
pin_variable(&code);
while ((code = ylisp_parse(&lexer)) != NULL) {
//ylisp_print(code); printf("\n");
ylisp_eval(root_context, code);
}
unpin_variable(&code);
}
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
exit(-1);
}
ylisp_init();
process_file("stdlib.l");
process_file(argv[1]);
return 0;
}