1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of Mes.
7 * Mes is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
12 * Mes is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
21 #define STRING_MAX 2048
32 #define BUILTIN_QUASIQUOTE 1 // 6x speedup for mescc
35 enum type {CHAR, MACRO, NUMBER, PAIR, SCM, STRING, SYMBOL, VALUES, VECTOR,
36 FUNCTION0, FUNCTION1, FUNCTION2, FUNCTION3, FUNCTIONn};
38 typedef struct scm_t* (*function0_t) (void);
39 typedef struct scm_t* (*function1_t) (struct scm_t*);
40 typedef struct scm_t* (*function2_t) (struct scm_t*, struct scm_t*);
41 typedef struct scm_t* (*function3_t) (struct scm_t*, struct scm_t*, struct scm_t*);
42 typedef struct scm_t* (*functionn_t) (struct scm_t*);
44 typedef struct scm_t {
53 function0_t function0;
54 function1_t function1;
55 function2_t function2;
56 function3_t function3;
57 functionn_t functionn;
60 struct scm_t** vector;
64 scm temp_number = {NUMBER, .name="nul", .value=0};
69 scm *display_ (FILE* f, scm *x); //internal
70 scm *display_helper (FILE*, scm*, bool, char const*, bool);
72 scm scm_nil = {SCM, "()"};
73 scm scm_dot = {SCM, "."};
74 scm scm_f = {SCM, "#f"};
75 scm scm_t = {SCM, "#t"};
76 scm scm_undefined = {SCM, "*undefined*"};
77 scm scm_unspecified = {SCM, "*unspecified*"};
78 scm scm_closure = {SCM, "*closure*"};
79 scm scm_circular = {SCM, "*circular*"};
84 scm scm_lambda = {SCM, "lambda"};
86 scm symbol_begin = {SCM, "begin"};
87 scm symbol_if = {SCM, "if"};
88 scm symbol_define = {SCM, "define"};
89 scm symbol_define_macro = {SCM, "define-macro"};
90 scm symbol_set_x = {SCM, "set!"};
92 scm symbol_quote = {SYMBOL, "quote"};
93 scm symbol_quasiquote = {SYMBOL, "quasiquote"};
94 scm symbol_unquote = {SYMBOL, "unquote"};
95 scm symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
97 scm symbol_sc_expand = {SYMBOL, "sc-expand"};
98 scm symbol_syntax = {SYMBOL, "syntax"};
99 scm symbol_quasisyntax = {SYMBOL, "quasisyntax"};
100 scm symbol_unsyntax = {SYMBOL, "unsyntax"};
101 scm symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
103 scm symbol_call_with_values = {SYMBOL, "call-with-values"};
104 scm symbol_current_module = {SYMBOL, "current-module"};
107 scm char_nul = {CHAR, .name="nul", .value=0};
108 scm char_backspace = {CHAR, .name="backspace", .value=8};
109 scm char_tab = {CHAR, .name="tab", .value=9};
110 scm char_newline = {CHAR, .name="newline", .value=10};
111 scm char_vt = {CHAR, .name="vt", .value=11};
112 scm char_page = {CHAR, .name="page", .value=12};
113 scm char_return = {CHAR, .name="return", .value=13};
114 scm char_space = {CHAR, .name="space", .value=32};
118 #define ATOM_P(x) (x->type == PAIR ? &scm_f : &scm_t)
128 assert (x->type == PAIR);
135 assert (x->type == PAIR);
140 cons (scm *x, scm *y)
142 scm *p = (scm*)malloc (sizeof (scm));
151 || (x->type == CHAR && y->type == CHAR \
152 && x->value == y->value) \
153 || (x->type == NUMBER && y->type == NUMBER \
154 && x->value == y->value)) \
158 eq_p (scm *x, scm *y)
166 return x->type == MACRO ? &scm_t : &scm_f;
172 return x == &scm_nil ? &scm_t : &scm_f;
175 #define PAIR_P(x) (x->type == PAIR ? &scm_t : &scm_f)
183 set_car_x (scm *x, scm *e)
185 assert (x->type == PAIR);
187 return &scm_unspecified;
191 set_cdr_x (scm *x, scm *e)
193 assert (x->type == PAIR);
194 cache_invalidate (x->cdr);
196 return &scm_unspecified;
200 set_env_x (scm *x, scm *e, scm *a)
202 cache_invalidate (x);
203 return set_cdr_x (assq (x, a), e);
209 return cons (&symbol_quote, x);
215 return cons (&symbol_quasiquote, x);
218 #if BUILTIN_QUASIQUOTE
220 unquote (scm *x) //int must not add to environment
222 return cons (&symbol_unquote, x);
224 scm *unquote (scm *x);
225 scm scm_unquote = {FUNCTION1, .name="unquote", .function1=&unquote};
228 unquote_splicing (scm *x) //int must not add to environment
230 return cons (&symbol_unquote_splicing, x);
232 scm *unquote_splicing (scm *x);
233 scm scm_unquote_splicing = {FUNCTION1, .name="unquote-splicing", .function1=&unquote_splicing};
234 #endif // BUILTIN_QUASIQUOTE
238 return cons (&symbol_syntax, x);
244 return cons (&symbol_quasisyntax, x);
248 unsyntax (scm *x) //int must not add to environment
250 return cons (&symbol_unsyntax, x);
252 scm *unsyntax (scm *x);
253 scm scm_unsyntax = {FUNCTION1, .name="unsyntax", .function1=&unsyntax};
256 unsyntax_splicing (scm *x) //int must not add to environment
258 return cons (&symbol_unsyntax_splicing, x);
260 scm *unsyntax_splicing (scm *x);
261 scm scm_unsyntax_splicing = {FUNCTION1, .name="unsyntax-splicing", .function1=&unsyntax_splicing};
266 // Derived, non-primitives
267 scm *caar (scm *x) {return car (car (x));}
268 scm *cadr (scm *x) {return car (cdr (x));}
269 scm *cdar (scm *x) {return cdr (car (x));}
270 scm *cddr (scm *x) {return cdr (cdr (x));}
271 scm *caaar (scm *x) {return car (car (car (x)));}
272 scm *caadr (scm *x) {return car (car (cdr (x)));}
273 scm *caddr (scm *x) {return car (cdr (cdr (x)));}
274 scm *cdadr (scm *x) {return cdr (car (cdr (x)));}
275 scm *cadar (scm *x) {return car (cdr (car (x)));}
276 scm *cddar (scm *x) {return cdr (cdr (car (x)));}
277 scm *cdddr (scm *x) {return cdr (cdr (cdr (x)));}
280 pairlis (scm *x, scm *y, scm *a)
284 if (atom_p (x) == &scm_t)
285 return cons (cons (x, y), a);
286 return cons (cons (car (x), car (y)),
287 pairlis (cdr (x), cdr (y), a));
291 assq (scm *x, scm *a)
293 while (a != &scm_nil && EQ_P (x, a->car->car) == &scm_f) a = a->cdr;
294 return a != &scm_nil ? a->car : &scm_f;
298 #define CACHE_SIZE 30
303 assq_ref_cache (scm *x, scm *a) //internal
306 if (x == &scm_f) return &scm_f;
309 scm*cache_invalidate (scm*x){}
310 scm*cache_invalidate_range (scm*p,scm*a){}
311 scm*cache_save (scm*p){}
312 scm*cache_lookup (scm*x){}
316 scm *env_cache_cars[CACHE_SIZE];
317 scm *env_cache_cdrs[CACHE_SIZE];
318 int cache_threshold = 0;
322 int n = p->car->value;
323 if (n < cache_threshold) return &scm_unspecified;
325 for (int i=0; i < CACHE_SIZE; i++) {
326 if (!env_cache_cars[i]) {
330 if (env_cache_cars[i] == p->car) return &scm_unspecified;
331 if (n > env_cache_cars[i]->value) {
332 n = env_cache_cars[i]->value;
337 cache_threshold = p->car->value;
338 env_cache_cars[j] = p->car;
339 env_cache_cdrs[j] = p->cdr;
341 return &scm_unspecified;
345 cache_lookup (scm *x)
347 for (int i=0; i < CACHE_SIZE; i++) {
348 if (!env_cache_cars[i]) break;
349 if (env_cache_cars[i] == x) return env_cache_cdrs[i];
351 return &scm_undefined;
355 cache_invalidate (scm *x)
357 for (int i=0; i < CACHE_SIZE; i++) {
358 if (env_cache_cars[i] == x) {
359 env_cache_cars[i] = 0;
363 return &scm_unspecified;
367 cache_invalidate_range (scm *p, scm *a)
370 cache_invalidate (p->car->car);
373 return &scm_unspecified;
377 assq_ref_cache (scm *x, scm *a)
380 scm *c = cache_lookup (x);
381 if (c != &scm_undefined) return c;
383 while (a != &scm_nil && x != a->car->car) {i++;a = a->cdr;}
384 if (a == &scm_nil) return &scm_undefined;
385 if (i>ENV_HEAD) cache_save (a->car);
391 evlis_env (scm *m, scm *a)
393 if (m == &scm_nil) return &scm_nil;
394 if (m->type != PAIR) return builtin_eval (m, a);
395 scm *e = builtin_eval (car (m), a);
396 return cons (e, evlis_env (cdr (m), a));
400 apply_env (scm *fn, scm *x, scm *a)
402 if (fn->type != PAIR)
404 if (fn == &scm_car) return x->car->car;
405 if (fn == &scm_cdr) return x->car->cdr;
406 if (builtin_p (fn) == &scm_t)
408 if (eq_p (fn, &symbol_call_with_values) == &scm_t)
409 return call (&scm_call_with_values_env, append2 (x, cons (a, &scm_nil)));
410 if (fn == &symbol_current_module) return a;
412 else if (fn->car == &scm_lambda) {
413 scm *p = pairlis (cadr (fn), x, a);
414 cache_invalidate_range (p, a->cdr);
415 scm *r = builtin_eval (cons (&symbol_begin, cddr (fn)), cons (cons (&scm_closure, p), p));
416 cache_invalidate_range (p, a->cdr);
419 else if (fn->car == &scm_closure) {
420 scm *args = caddr (fn);
421 scm *body = cdddr (fn);
424 scm *p = pairlis (args, x, a);
425 cache_invalidate_range (p, a->cdr);
426 scm *r = builtin_eval (cons (&symbol_begin, body), cons (cons (&scm_closure, p), p));
427 cache_invalidate_range (p, a->cdr);
431 else if (fn->car == &scm_label)
432 return apply_env (caddr (fn), x, cons (cons (cadr (fn), caddr (fn)), a));
434 scm *efn = builtin_eval (fn, a);
435 if (efn == &scm_f || efn == &scm_t) assert (!"apply bool");
436 if (efn->type == NUMBER) assert (!"apply number");
437 if (efn->type == STRING) assert (!"apply string");
438 return apply_env (efn, x, a);
442 builtin_eval (scm *e, scm *a)
444 if (builtin_p (e) == &scm_t) return e;
445 if (internal_p (e) == &scm_t) return e;
447 e = expand_macro_env (e, a);
449 if (e->type == SYMBOL) {
450 scm *y = assq_ref_cache (e, a);
451 if (y == &scm_undefined) {
452 fprintf (stderr, "eval: unbound variable: %s\n", e->name);
453 assert (!"unbound variable");
457 else if (e->type != PAIR)
459 else if (e->car->type != PAIR)
461 if (e->car == &symbol_quote)
463 if (e->car == &symbol_syntax)
465 if (e->car == &symbol_begin)
467 if (e->car == &scm_lambda)
468 return make_closure (cadr (e), cddr (e), assq (&scm_closure, a));
469 if (e->car == &scm_closure)
471 if (e->car == &symbol_if)
472 return builtin_if (cdr (e), a);
474 if (e->car == &symbol_define)
475 return define (e, a);
476 if (e->car == &symbol_define_macro)
477 return define (e, a);
479 if (e->car == &symbol_define) {
480 fprintf (stderr, "C DEFINE: %s\n", e->cdr->car->type == SYMBOL
482 : e->cdr->car->car->name);
484 assert (e->car != &symbol_define);
485 assert (e->car != &symbol_define_macro);
487 if (e->car == &symbol_set_x)
488 return set_env_x (cadr (e), builtin_eval (caddr (e), a), a);
489 #if BUILTIN_QUASIQUOTE
490 if (e->car == &symbol_unquote)
491 return builtin_eval (cadr (e), a);
492 if (e->car == &symbol_quasiquote)
493 return eval_quasiquote (cadr (e), add_unquoters (a));
494 if (e->car == &symbol_unsyntax)
495 return builtin_eval (cadr (e), a);
496 if (e->car == &symbol_quasisyntax)
497 return eval_quasisyntax (cadr (e), add_unsyntaxers (a));
498 #endif //BUILTIN_QUASIQUOTE
500 return apply_env (e->car, evlis_env (e->cdr, a), a);
504 expand_macro_env (scm *e, scm *a)
508 && (macro = lookup_macro (e->car, a)) != &scm_f)
509 return expand_macro_env (apply_env (macro, e->cdr, a), a);
514 begin (scm *e, scm *a)
516 scm *r = &scm_unspecified;
517 while (e != &scm_nil) {
518 r = builtin_eval (e->car, a);
525 builtin_if (scm *e, scm *a)
527 if (builtin_eval (car (e), a) != &scm_f)
528 return builtin_eval (cadr (e), a);
529 if (cddr (e) != &scm_nil)
530 return builtin_eval (caddr (e), a);
531 return &scm_unspecified;
534 #if BUILTIN_QUASIQUOTE
536 eval_quasiquote (scm *e, scm *a)
538 if (e == &scm_nil) return e;
539 else if (atom_p (e) == &scm_t) return e;
540 else if (eq_p (car (e), &symbol_unquote) == &scm_t)
541 return builtin_eval (cadr (e), a);
542 else if (e->type == PAIR && e->car->type == PAIR
543 && eq_p (caar (e), &symbol_unquote_splicing) == &scm_t)
544 return append2 (builtin_eval (cadar (e), a), eval_quasiquote (cdr (e), a));
545 return cons (eval_quasiquote (car (e), a), eval_quasiquote (cdr (e), a));
549 eval_quasisyntax (scm *e, scm *a)
551 if (e == &scm_nil) return e;
552 else if (atom_p (e) == &scm_t) return e;
553 else if (eq_p (car (e), &symbol_unsyntax) == &scm_t)
554 return builtin_eval (cadr (e), a);
555 else if (e->type == PAIR && e->car->type == PAIR
556 && eq_p (caar (e), &symbol_unsyntax_splicing) == &scm_t)
557 return append2 (builtin_eval (cadar (e), a), eval_quasisyntax (cdr (e), a));
558 return cons (eval_quasisyntax (car (e), a), eval_quasisyntax (cdr (e), a));
562 scm*add_unquoters (scm *a){}
563 scm*add_unsyntaxers (scm *a){}
564 scm*eval_unsyntax (scm *e, scm *a){}
565 scm*eval_quasiquote (scm *e, scm *a){}
566 scm*eval_quasisyntax (scm *e, scm *a){}
567 #endif // BUILTIN_QUASIQUOTE
574 return (x->type == FUNCTION0
575 || x->type == FUNCTION1
576 || x->type == FUNCTION2
577 || x->type == FUNCTION3
578 || x->type == FUNCTIONn)
585 return (x == &scm_t || x == &scm_f) ? &scm_t : &scm_f;
591 return x->type == CHAR ? &scm_t : &scm_f;
597 return x->type == NUMBER ? &scm_t : &scm_f;
603 return x->type == STRING ? &scm_t : &scm_f;
609 return x->type == SCM ? &scm_t : &scm_f;
615 return x->type == SYMBOL ? &scm_t : &scm_f;
621 return x->type == VECTOR ? &scm_t : &scm_f;
625 display (scm *x/*...*/)
630 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
631 FILE *f = fd == 1 ? stdout : stderr;
632 return display_helper (f, e, false, "", false);
636 display_ (FILE* f, scm *x) //internal
638 return display_helper (f, x, false, "", false);
642 call (scm *fn, scm *x)
644 if (fn->type == FUNCTION0)
645 return fn->function0 ();
646 if (x->car->type == VALUES)
647 x = cons (x->car->cdr->car, &scm_nil);
648 if (fn->type == FUNCTION1)
649 return fn->function1 (car (x));
650 if (fn->type == FUNCTION2)
651 return fn->function2 (car (x), cadr (x));
652 if (fn->type == FUNCTION3)
653 return fn->function3 (car (x), cadr (x), caddr (x));
654 if (fn->type == FUNCTIONn)
655 return fn->functionn (x);
656 return &scm_unspecified;
660 append2 (scm *x, scm *y)
662 if (x == &scm_nil) return y;
663 assert (x->type == PAIR);
664 return cons (car (x), append2 (cdr (x), y));
668 append (scm *x/*...*/)
670 if (x == &scm_nil) return &scm_nil;
671 return append2 (car (x), append (cdr (x)));
677 scm *p = (scm*)malloc (sizeof (scm));
684 make_macro (scm *name, scm *x)
686 scm *p = (scm*)malloc (sizeof (scm));
689 p->name = name->name;
696 scm *p = (scm*)malloc (sizeof (scm));
703 make_string (char const *s)
705 scm *p = (scm*)malloc (sizeof (scm));
707 p->name = strdup (s);
714 internal_lookup_symbol (char const *s)
717 while (x && strcmp (s, x->car->name)) x = x->cdr;
723 internal_make_symbol (char const *s)
725 scm *x = (scm*)malloc (sizeof (scm));
727 x->name = strdup (s);
729 symbols = cons (x, symbols);
734 make_symbol (char const *s)
736 scm *x = internal_lookup_symbol (s);
737 return x ? x : internal_make_symbol (s);
743 scm *p = (scm*)malloc (sizeof (scm));
745 p->length = n->value;
746 p->vector = (scm**)malloc (n->value * sizeof (scm*));
747 for (int i=0; i<n->value; i++) p->vector[i] = &scm_unspecified;
752 string (scm *x/*...*/)
754 char buf[STRING_MAX] = "";
756 while (x != &scm_nil)
759 assert (s->type == CHAR);
763 return make_string (buf);
767 string_append (scm *x/*...*/)
769 char buf[STRING_MAX] = "";
771 while (x != &scm_nil)
774 assert (s->type == STRING);
775 strcat (buf, s->name);
778 return make_string (buf);
782 list_to_string (scm *x)
784 char buf[STRING_MAX] = "";
786 while (x != &scm_nil)
789 assert (s->type == CHAR);
794 return make_string (buf);
798 string_length (scm *x)
800 assert (x->type == STRING);
801 return make_number (strlen (x->name));
805 string_ref (scm *x, scm *k)
807 assert (x->type == STRING);
808 assert (k->type == NUMBER);
809 return make_char (x->name[k->value]);
813 substring (scm *x/*...*/)
815 assert (x->type == PAIR);
816 assert (x->car->type == STRING);
817 char const *s = x->car->name;
818 assert (x->cdr->car->type == NUMBER);
819 int start = x->cdr->car->value;
820 int end = strlen (s);
821 if (x->cdr->cdr->type == PAIR) {
822 assert (x->cdr->cdr->car->type == NUMBER);
823 assert (x->cdr->cdr->car->value <= end);
824 end = x->cdr->cdr->car->value;
826 char buf[STRING_MAX];
827 strncpy (buf, s+start, end - start);
829 return make_string (buf);
836 while (x != &scm_nil)
841 return make_number (n);
847 //if (x != &scm_nil && cdr (x) != &scm_nil)
848 //return last_pair (cdr (x));
849 while (x != &scm_nil && cdr (x) != &scm_nil)
855 builtin_list (scm *x/*...*/)
861 values (scm *x/*...*/)
863 scm *v = cons (0, x);
869 call_with_values_env (scm *producer, scm *consumer, scm *a)
871 scm *v = apply_env (producer, &scm_nil, a);
872 if (v->type == VALUES)
874 return apply_env (consumer, v, a);
878 vector_length (scm *x)
880 assert (x->type == VECTOR);
881 return make_number (x->length);
885 vector_ref (scm *x, scm *i)
887 assert (x->type == VECTOR);
888 assert (i->value < x->length);
889 return x->vector[i->value];
893 vector_set_x (scm *x, scm *i, scm *e)
895 assert (x->type == VECTOR);
896 assert (i->value < x->length);
897 x->vector[i->value] = e;
898 return &scm_unspecified;
902 lookup (char const *s, scm *a)
904 if (isdigit (*s) || (*s == '-' && isdigit (*(s+1))))
905 return make_number (atoi (s));
908 x = internal_lookup_symbol (s);
911 if (*s == '\'') return &symbol_quote;
912 if (*s == '`') return &symbol_quasiquote;
913 if (*s == ',' && *(s+1) == '@') return &symbol_unquote_splicing;
914 if (*s == ',') return &symbol_unquote;
916 if (*s == '#' && *(s+1) == '\'') return &symbol_syntax;
917 if (*s == '#' && *(s+1) == '`') return &symbol_quasisyntax;
918 if (*s == '#' && *(s+1) == ',' && *(s+2) == '@') return &symbol_unsyntax_splicing;
919 if (*s == '#' && *(s+1) == ',') return &symbol_unsyntax;
921 if (!strcmp (s, "EOF")) {
922 fprintf (stderr, "mes: got EOF\n");
923 return &scm_nil; // `EOF': eval program, which may read stdin
926 return internal_make_symbol (s);
930 lookup_char (int c, scm *a)
935 return lookup (buf, a);
939 list2str (scm *l) // char*
941 static char buf[STRING_MAX];
943 while (l != &scm_nil) {
945 assert (c->type == NUMBER);
954 list_to_vector (scm *x)
956 temp_number.value = length (x)->value;
957 scm *v = make_vector (&temp_number);
959 while (x != &scm_nil)
968 integer_to_char (scm *x)
970 assert (x->type == NUMBER);
971 return make_char (x->value);
975 char_to_integer (scm *x)
977 assert (x->type == CHAR);
978 return make_number (x->value);
982 number_to_string (scm *x)
984 assert (x->type == NUMBER);
985 char buf[STRING_MAX];
986 sprintf (buf,"%d", x->value);
987 return make_string (buf);
991 builtin_exit (scm *x)
993 assert (x->type == NUMBER);
998 string_to_symbol (scm *x)
1000 assert (x->type == STRING);
1001 return make_symbol (x->name);
1005 symbol_to_string (scm *x)
1007 assert (x->type == SYMBOL);
1008 return make_string (x->name);
1012 vector_to_list (scm *v)
1015 for (int i = 0; i < v->length; i++)
1016 x = append2 (x, cons (v->vector[i], &scm_nil));
1021 newline (scm *p/*...*/)
1024 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
1025 FILE *f = fd == 1 ? stdout : stderr;
1027 return &scm_unspecified;
1031 force_output (scm *p/*...*/)
1034 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
1035 FILE *f = fd == 1 ? stdout : stderr;
1040 display_helper (FILE* f, scm *x, bool cont, char const *sep, bool quote)
1043 fprintf (f, "%s", sep);
1044 if (x->type == CHAR && x->value == char_nul.value) fprintf (f, "#\\%s", char_nul.name);
1045 else if (x->type == CHAR && x->value == char_backspace.value) fprintf (f, "#\\%s", char_backspace.name);
1046 else if (x->type == CHAR && x->value == char_tab.value) fprintf (f, "#\\%s", char_tab.name);
1047 else if (x->type == CHAR && x->value == char_newline.value) fprintf (f, "#\\%s", char_newline.name);
1048 else if (x->type == CHAR && x->value == char_vt.value) fprintf (f, "#\\%s", char_vt.name);
1049 else if (x->type == CHAR && x->value == char_page.value) fprintf (f, "#\\%s", char_page.name);
1050 else if (x->type == CHAR && x->value == char_return.value) fprintf (f, "#\\%s", char_return.name);
1051 else if (x->type == CHAR && x->value == char_space.value) fprintf (f, "#\\%s", char_space.name);
1052 else if (x->type == CHAR) fprintf (f, "#\\%c", x->value);
1053 else if (x->type == MACRO) {
1054 fprintf (f, "(*macro* ");
1055 display_helper (f, x->macro, cont, sep, quote);
1058 else if (x->type == NUMBER) fprintf (f, "%d", x->value);
1059 else if (x->type == PAIR) {
1060 if (car (x) == &scm_circular) {
1061 fprintf (f, "(*circ* . #-1#)");
1062 return &scm_unspecified;
1064 if (car (x) == &scm_closure) {
1065 fprintf (f, "(*closure* . #-1#)");
1066 return &scm_unspecified;
1068 if (car (x) == &scm_quote) {
1070 return display_helper (f, car (cdr (x)), cont, "", true);
1072 if (!cont) fprintf (f, "(");
1073 display_ (f, car (x));
1074 if (cdr (x)->type == PAIR)
1075 display_helper (f, cdr (x), true, " ", false);
1076 else if (cdr (x) != &scm_nil) {
1078 display_ (f, cdr (x));
1080 if (!cont) fprintf (f, ")");
1082 else if (x->type == VECTOR) {
1083 fprintf (f, "#(", x->length);
1084 for (int i = 0; i < x->length; i++) {
1085 if (x->vector[i]->type == VECTOR)
1086 fprintf (f, "%s#(...)", i ? " " : "");
1088 display_helper (f, x->vector[i], false, i ? " " : "", false);
1092 else if (builtin_p (x) == &scm_t) fprintf (f, "#<procedure %s>", x->name);
1093 else if (atom_p (x) == &scm_t) fprintf (f, "%s", x->name);
1095 return &scm_unspecified;
1101 ungetchar (int c) //int
1103 return ungetc (c, stdin);
1117 return make_char (peekchar ());
1123 return make_char (getchar ());
1127 write_char (scm *x/*...*/)
1132 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
1133 FILE *f = fd == 1 ? stdout : stderr;
1134 assert (c->type == NUMBER || c->type == CHAR);
1135 fputc (c->value, f);
1142 assert (c->type == NUMBER || c->type == CHAR);
1143 ungetchar (c->value);
1150 if (c == '\n') return c;
1151 return readcomment (getchar ());
1157 if (c == '!' && peekchar () == '#') return getchar ();
1158 return readblock (getchar ());
1162 readword (int c, char *w, scm *a)
1164 if (c == EOF && !w) return &scm_nil;
1165 if (c == '\n' && !w) return readword (getchar (), w, a);
1166 if (c == '\n' && *w == '.' && w[1] == 0) return &scm_dot;
1167 if (c == EOF || c == '\n') return lookup (w, a);
1168 if (c == ' ') return readword ('\n', w, a);
1169 if (c == '"' && !w) return readstring ();
1170 if (c == '"') {ungetchar (c); return lookup (w, a);}
1171 if (c == '(' && !w) return readlist (a);
1172 if (c == '(') {ungetchar (c); return lookup (w, a);}
1173 if (c == ')' && !w) {ungetchar (c); return &scm_nil;}
1174 if (c == ')') {ungetchar (c); return lookup (w, a);}
1175 if (c == ',' && peekchar () == '@') {getchar (); return cons (lookup (",@", a),
1176 cons (readword (getchar (), w, a),
1181 && !w) {return cons (lookup_char (c, a),
1182 cons (readword (getchar (), w, a),
1184 if (c == '#' && peekchar () == ',' && !w) {
1186 if (peekchar () == '@'){getchar (); return cons (lookup ("#,@", a),
1187 cons (readword (getchar (), w, a),
1189 return cons (lookup ("#,", a), cons (readword (getchar (), w, a), &scm_nil));
1192 && (peekchar () == '\''
1193 || peekchar () == '`')
1194 && !w) {char buf[3] = "#"; buf[1] = getchar (); return cons (lookup (buf, a),
1195 cons (readword (getchar (), w, a),
1197 if (c == ';') {readcomment (c); return readword ('\n', w, a);}
1198 if (c == '#' && peekchar () == 'x') {getchar (); return read_hex ();}
1199 if (c == '#' && peekchar () == '\\') {getchar (); return read_character ();}
1200 if (c == '#' && !w && peekchar () == '(') {getchar (); return list_to_vector (readlist (a));}
1201 if (c == '#' && peekchar () == '(') {ungetchar (c); return lookup (w, a);}
1202 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return readword (getchar (), w, a);}
1203 char buf[STRING_MAX] = {0};
1205 char *p = w ? w + strlen (w) : buf;
1208 return readword (getchar (), w ? w : buf, a);
1215 int c = peekchar ();
1216 while ((c >= '0' && c <= '9')
1217 || (c >= 'A' && c <= 'F')
1218 || (c >= 'a' && c <= 'f')) {
1220 if (c >= 'a') n += c - 'a' + 10;
1221 else if (c >= 'A') n += c - 'A' + 10;
1226 return make_number (n);
1233 if (c >= '0' && c <= '7'
1234 && peekchar () >= '0' && peekchar () <= '7') {
1236 while (peekchar () >= '0' && peekchar () <= '7') {
1238 c += getchar () - '0';
1241 else if (c >= 'a' && c <= 'z'
1242 && peekchar () >= 'a' && peekchar () <= 'z') {
1243 char buf[STRING_MAX];
1246 while (peekchar () >= 'a' && peekchar () <= 'z') {
1250 if (!strcmp (buf, char_nul.name)) c = char_nul.value;
1251 else if (!strcmp (buf, char_backspace.name)) c = char_backspace.value;
1252 else if (!strcmp (buf, char_tab.name)) c = char_tab.value;
1253 else if (!strcmp (buf, char_newline.name)) c = char_newline.value;
1254 else if (!strcmp (buf, char_vt.name)) c = char_vt.value;
1255 else if (!strcmp (buf, char_page.name)) c = char_page.value;
1256 else if (!strcmp (buf, char_return.name)) c = char_return.value;
1257 else if (!strcmp (buf, char_space.name)) c = char_space.value;
1259 fprintf (stderr, "char not supported: %s\n", buf);
1260 assert (!"char not supported");
1263 return make_char (c);
1269 char buf[STRING_MAX];
1273 if (c == '"') break;
1274 if (c == '\\' && peekchar () == '"') *p++ = getchar ();
1275 else if (c == '\\' && peekchar () == 'n') {getchar (); *p++ = '\n';}
1276 else if (c == EOF) assert (!"EOF in string");
1281 return make_string (buf);
1285 eat_whitespace (int c)
1287 while (c == ' ' || c == '\t' || c == '\n') c = getchar ();
1288 if (c == ';') return eat_whitespace (readcomment (c));
1289 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return eat_whitespace (getchar ());}
1297 c = eat_whitespace (c);
1298 if (c == ')') return &scm_nil;
1299 scm *w = readword (c, 0, a);
1301 return car (readlist (a));
1302 return cons (w, readlist (a));
1308 return readword (getchar (), 0, a);
1312 greater_p (scm *x/*...*/)
1315 while (x != &scm_nil)
1317 assert (x->car->type == NUMBER);
1318 if (x->car->value >= n) return &scm_f;
1326 less_p (scm *x/*...*/)
1329 while (x != &scm_nil)
1331 assert (x->car->type == NUMBER);
1332 if (x->car->value <= n) return &scm_f;
1340 is_p (scm *x/*...*/)
1342 if (x == &scm_nil) return &scm_t;
1343 assert (x->car->type == NUMBER);
1344 int n = x->car->value;
1346 while (x != &scm_nil)
1348 if (x->car->value != n) return &scm_f;
1355 minus (scm *x/*...*/)
1358 assert (a->type == NUMBER);
1363 while (x != &scm_nil)
1365 assert (x->car->type == NUMBER);
1369 return make_number (n);
1373 plus (scm *x/*...*/)
1376 while (x != &scm_nil)
1378 assert (x->car->type == NUMBER);
1382 return make_number (n);
1386 divide (scm *x/*...*/)
1389 if (x != &scm_nil) {
1390 assert (x->car->type == NUMBER);
1394 while (x != &scm_nil)
1396 assert (x->car->type == NUMBER);
1400 return make_number (n);
1404 modulo (scm *a, scm *b)
1406 assert (a->type == NUMBER);
1407 assert (b->type == NUMBER);
1408 return make_number (a->value % b->value);
1412 multiply (scm *x/*...*/)
1415 while (x != &scm_nil)
1417 assert (x->car->type == NUMBER);
1421 return make_number (n);
1425 logior (scm *x/*...*/)
1428 while (x != &scm_nil)
1430 assert (x->car->type == NUMBER);
1434 return make_number (n);
1437 scm *add_environment (scm *a, char const *name, scm *x);
1439 #if BUILTIN_QUASIQUOTE
1441 add_unquoters (scm *a)
1443 a = cons (cons (&symbol_unquote, &scm_unquote), a);
1444 a = cons (cons (&symbol_unquote_splicing, &scm_unquote_splicing), a);
1449 add_unsyntaxers (scm *a)
1451 a = cons (cons (&symbol_unsyntax, &scm_unsyntax), a);
1452 a = cons (cons (&symbol_unsyntax_splicing, &scm_unsyntax_splicing), a);
1455 #endif // BUILTIN_QUASIQUOTE
1458 add_environment (scm *a, char const *name, scm *x)
1460 return cons (cons (make_symbol (name), x), a);
1468 #include "symbols.i"
1471 symbols = cons (&scm_label, symbols);
1472 a = cons (cons (&scm_label, &scm_t), a);
1475 a = cons (cons (&scm_f, &scm_f), a);
1476 a = cons (cons (&scm_nil, &scm_nil), a);
1477 a = cons (cons (&scm_t, &scm_t), a);
1478 a = cons (cons (&scm_unspecified, &scm_unspecified), a);
1479 a = cons (cons (&symbol_begin, &symbol_begin), a);
1480 a = cons (cons (&symbol_quote, &scm_quote), a);
1481 a = cons (cons (&symbol_syntax, &scm_syntax), a);
1484 #include "environment.i"
1486 a = add_environment (a, "display", &scm_display);
1487 a = add_environment (a, "newline", &scm_newline);
1489 a = cons (cons (&scm_closure, a), a);
1494 make_lambda (scm *args, scm *body)
1496 return cons (&scm_lambda, cons (args, body));
1500 make_closure (scm *args, scm *body, scm *a)
1502 return cons (&scm_closure, cons (cons (&scm_circular, a), cons (args, body)));
1507 define (scm *x, scm *a)
1510 scm *name = cadr (x);
1511 if (name->type != PAIR)
1512 e = builtin_eval (caddr (x), cons (cons (cadr (x), cadr (x)), a));
1515 scm *p = pairlis (cadr (x), cadr (x), a);
1516 e = builtin_eval (make_lambda (cdadr (x), cddr (x)), p);
1518 if (eq_p (car (x), &symbol_define_macro) == &scm_t)
1519 e = make_macro (name, e);
1520 scm *entry = cons (name, e);
1521 scm *aa = cons (entry, &scm_nil);
1522 set_cdr_x (aa, cdr (a));
1524 scm *cl = assq (&scm_closure, a);
1529 scm*define (scm *x, scm *a){}
1533 define_macro (scm *x, scm *a)
1538 lookup_macro (scm *x, scm *a)
1540 if (x->type != SYMBOL) return &scm_f;
1541 scm *m = assq_ref_cache (x, a);
1542 if (macro_p (m) == &scm_t) return m->macro;
1547 read_file (scm *e, scm *a)
1549 if (e == &scm_nil) return e;
1551 scm *x = cons (e, read_file (read_env (a), a));
1552 display_ (stderr, x);
1554 return cons (e, read_file (read_env (a), a));
1559 main (int argc, char *argv[])
1561 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
1562 if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.0\n");
1563 scm *a = mes_environment ();
1564 display_ (stderr, builtin_eval (cons (&symbol_begin, read_file (read_env (a), a)), a));