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
34 enum type {CHAR, MACRO, NUMBER, PAIR, SCM, STRING, SYMBOL, VALUES, VECTOR,
35 FUNCTION0, FUNCTION1, FUNCTION2, FUNCTION3, FUNCTIONn};
37 typedef struct scm_t* (*function0_t) (void);
38 typedef struct scm_t* (*function1_t) (struct scm_t*);
39 typedef struct scm_t* (*function2_t) (struct scm_t*, struct scm_t*);
40 typedef struct scm_t* (*function3_t) (struct scm_t*, struct scm_t*, struct scm_t*);
41 typedef struct scm_t* (*functionn_t) (struct scm_t*);
43 typedef struct scm_t {
52 function0_t function0;
53 function1_t function1;
54 function2_t function2;
55 function3_t function3;
56 functionn_t functionn;
59 struct scm_t** vector;
63 scm temp_number = {NUMBER, .name="nul", .value=0};
65 #include "type.environment.h"
66 #include "quasiquote.environment.h"
67 #include "mes.environment.h"
69 scm *display_ (FILE* f, scm *x);
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};
121 assert (x->type == PAIR);
128 assert (x->type == PAIR);
133 cons (scm *x, scm *y)
135 scm *p = (scm*)malloc (sizeof (scm));
143 eq_p (scm *x, scm *y)
146 || (x->type == CHAR && y->type == CHAR
147 && x->value == y->value)
148 || (x->type == NUMBER && y->type == NUMBER
149 && x->value == y->value))
154 set_car_x (scm *x, scm *e)
156 assert (x->type == PAIR);
158 return &scm_unspecified;
162 set_cdr_x (scm *x, scm *e)
164 assert (x->type == PAIR);
165 cache_invalidate (x->cdr);
167 return &scm_unspecified;
171 set_env_x (scm *x, scm *e, scm *a)
173 cache_invalidate (x);
174 return set_cdr_x (assq (x, a), e);
180 return cons (&symbol_quote, x);
186 return cons (&symbol_quasiquote, x);
192 return cons (&symbol_quasisyntax, x);
196 #include "quasiquote.c"
200 // Derived, non-primitives
201 scm *caar (scm *x) {return car (car (x));}
202 scm *cadr (scm *x) {return car (cdr (x));}
203 scm *cdar (scm *x) {return cdr (car (x));}
204 scm *cddr (scm *x) {return cdr (cdr (x));}
205 scm *caaar (scm *x) {return car (car (car (x)));}
206 scm *caadr (scm *x) {return car (car (cdr (x)));}
207 scm *caddr (scm *x) {return car (cdr (cdr (x)));}
208 scm *cdadr (scm *x) {return cdr (car (cdr (x)));}
209 scm *cadar (scm *x) {return car (cdr (car (x)));}
210 scm *cddar (scm *x) {return cdr (cdr (car (x)));}
211 scm *cdddr (scm *x) {return cdr (cdr (cdr (x)));}
214 pairlis (scm *x, scm *y, scm *a)
218 if (pair_p (x) == &scm_f)
219 return cons (cons (x, y), a);
220 return cons (cons (car (x), car (y)),
221 pairlis (cdr (x), cdr (y), a));
225 assq (scm *x, scm *a)
227 while (a != &scm_nil && eq_p (x, a->car->car) == &scm_f) a = a->cdr;
228 return a != &scm_nil ? a->car : &scm_f;
232 #define CACHE_SIZE 30
237 assq_ref_cache (scm *x, scm *a)
240 if (x == &scm_f) return &scm_f;
243 scm*cache_invalidate (scm*x){}
244 scm*cache_invalidate_range (scm*p,scm*a){}
245 scm*cache_save (scm*p){}
246 scm*cache_lookup (scm*x){}
250 scm *env_cache_cars[CACHE_SIZE];
251 scm *env_cache_cdrs[CACHE_SIZE];
252 int cache_threshold = 0;
256 int n = p->car->value;
257 if (n < cache_threshold) return &scm_unspecified;
259 for (int i=0; i < CACHE_SIZE; i++) {
260 if (!env_cache_cars[i]) {
264 if (env_cache_cars[i] == p->car) return &scm_unspecified;
265 if (n > env_cache_cars[i]->value) {
266 n = env_cache_cars[i]->value;
271 cache_threshold = p->car->value;
272 env_cache_cars[j] = p->car;
273 env_cache_cdrs[j] = p->cdr;
275 return &scm_unspecified;
279 cache_lookup (scm *x)
281 for (int i=0; i < CACHE_SIZE; i++) {
282 if (!env_cache_cars[i]) break;
283 if (env_cache_cars[i] == x) return env_cache_cdrs[i];
285 return &scm_undefined;
289 cache_invalidate (scm *x)
291 for (int i=0; i < CACHE_SIZE; i++) {
292 if (env_cache_cars[i] == x) {
293 env_cache_cars[i] = 0;
297 return &scm_unspecified;
301 cache_invalidate_range (scm *p, scm *a)
304 cache_invalidate (p->car->car);
307 return &scm_unspecified;
311 assq_ref_cache (scm *x, scm *a)
314 scm *c = cache_lookup (x);
315 if (c != &scm_undefined) return c;
317 while (a != &scm_nil && x != a->car->car) {i++;a = a->cdr;}
318 if (a == &scm_nil) return &scm_undefined;
319 if (i>ENV_HEAD) cache_save (a->car);
325 evlis_env (scm *m, scm *a)
327 if (m == &scm_nil) return &scm_nil;
328 if (m->type != PAIR) return builtin_eval (m, a);
329 scm *e = builtin_eval (car (m), a);
330 return cons (e, evlis_env (cdr (m), a));
334 apply_env (scm *fn, scm *x, scm *a)
336 if (fn->type != PAIR)
338 if (fn == &scm_car) return x->car->car;
339 if (fn == &scm_cdr) return x->car->cdr;
340 if (builtin_p (fn) == &scm_t)
342 if (eq_p (fn, &symbol_call_with_values) == &scm_t)
343 return call (&scm_call_with_values_env, append2 (x, cons (a, &scm_nil)));
344 if (fn == &symbol_current_module) return a;
346 else if (fn->car == &scm_lambda) {
347 scm *p = pairlis (cadr (fn), x, a);
348 cache_invalidate_range (p, a->cdr);
349 scm *r = builtin_eval (cons (&symbol_begin, cddr (fn)), cons (cons (&scm_closure, p), p));
350 cache_invalidate_range (p, a->cdr);
353 else if (fn->car == &scm_closure) {
354 scm *args = caddr (fn);
355 scm *body = cdddr (fn);
358 scm *p = pairlis (args, x, a);
359 cache_invalidate_range (p, a->cdr);
360 scm *r = builtin_eval (cons (&symbol_begin, body), cons (cons (&scm_closure, p), p));
361 cache_invalidate_range (p, a->cdr);
365 else if (fn->car == &scm_label)
366 return apply_env (caddr (fn), x, cons (cons (cadr (fn), caddr (fn)), a));
368 scm *efn = builtin_eval (fn, a);
369 if (efn == &scm_f || efn == &scm_t) assert (!"apply bool");
370 if (efn->type == NUMBER) assert (!"apply number");
371 if (efn->type == STRING) assert (!"apply string");
372 return apply_env (efn, x, a);
376 builtin_eval (scm *e, scm *a)
378 if (builtin_p (e) == &scm_t) return e;
379 if (e->type == SCM) return e;
381 e = expand_macro_env (e, a);
383 if (e->type == SYMBOL) {
384 scm *y = assq_ref_cache (e, a);
385 if (y == &scm_undefined) {
386 fprintf (stderr, "eval: unbound variable: %s\n", e->name);
387 assert (!"unbound variable");
391 else if (e->type != PAIR)
393 else if (e->car->type != PAIR)
395 if (e->car == &symbol_quote)
397 if (e->car == &symbol_syntax)
399 if (e->car == &symbol_begin)
401 if (e->car == &scm_lambda)
402 return make_closure (cadr (e), cddr (e), assq (&scm_closure, a));
403 if (e->car == &scm_closure)
405 if (e->car == &symbol_if)
406 return builtin_if (cdr (e), a);
408 if (e->car == &symbol_define)
409 return define (e, a);
410 if (e->car == &symbol_define_macro)
411 return define (e, a);
413 if (e->car == &symbol_define) {
414 fprintf (stderr, "C DEFINE: %s\n", e->cdr->car->type == SYMBOL
416 : e->cdr->car->car->name);
418 assert (e->car != &symbol_define);
419 assert (e->car != &symbol_define_macro);
421 if (e->car == &symbol_set_x)
422 return set_env_x (cadr (e), builtin_eval (caddr (e), a), a);
424 if (e->car == &symbol_unquote)
425 return builtin_eval (cadr (e), a);
426 if (e->car == &symbol_quasiquote)
427 return eval_quasiquote (cadr (e), add_unquoters (a));
428 if (e->car == &symbol_unsyntax)
429 return builtin_eval (cadr (e), a);
430 if (e->car == &symbol_quasisyntax)
431 return eval_quasisyntax (cadr (e), add_unsyntaxers (a));
434 return apply_env (e->car, evlis_env (e->cdr, a), a);
438 expand_macro_env (scm *e, scm *a)
442 && (macro = lookup_macro (e->car, a)) != &scm_f)
443 return expand_macro_env (apply_env (macro, e->cdr, a), a);
448 begin (scm *e, scm *a)
450 scm *r = &scm_unspecified;
451 while (e != &scm_nil) {
452 r = builtin_eval (e->car, a);
459 builtin_if (scm *e, scm *a)
461 if (builtin_eval (car (e), a) != &scm_f)
462 return builtin_eval (cadr (e), a);
463 if (cddr (e) != &scm_nil)
464 return builtin_eval (caddr (e), a);
465 return &scm_unspecified;
471 display (scm *x) ///((args . n))
476 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
477 FILE *f = fd == 1 ? stdout : stderr;
478 return display_helper (f, e, false, "", false);
482 display_ (FILE* f, scm *x)
484 return display_helper (f, x, false, "", false);
488 call (scm *fn, scm *x)
490 if (fn->type == FUNCTION0)
491 return fn->function0 ();
492 if (x->car->type == VALUES)
493 x = cons (x->car->cdr->car, &scm_nil);
494 if (fn->type == FUNCTION1)
495 return fn->function1 (car (x));
496 if (fn->type == FUNCTION2)
497 return fn->function2 (car (x), cadr (x));
498 if (fn->type == FUNCTION3)
499 return fn->function3 (car (x), cadr (x), caddr (x));
500 if (fn->type == FUNCTIONn)
501 return fn->functionn (x);
502 return &scm_unspecified;
506 append2 (scm *x, scm *y)
508 if (x == &scm_nil) return y;
509 assert (x->type == PAIR);
510 return cons (car (x), append2 (cdr (x), y));
514 append (scm *x) ///((args . n))
516 if (x == &scm_nil) return &scm_nil;
517 return append2 (car (x), append (cdr (x)));
523 scm *p = (scm*)malloc (sizeof (scm));
530 make_macro (scm *name, scm *x)
532 scm *p = (scm*)malloc (sizeof (scm));
535 p->name = name->name;
542 scm *p = (scm*)malloc (sizeof (scm));
549 make_string (char const *s)
551 scm *p = (scm*)malloc (sizeof (scm));
553 p->name = strdup (s);
560 internal_lookup_symbol (char const *s)
563 while (x && strcmp (s, x->car->name)) x = x->cdr;
569 internal_make_symbol (char const *s)
571 scm *x = (scm*)malloc (sizeof (scm));
573 x->name = strdup (s);
575 symbols = cons (x, symbols);
580 make_symbol (char const *s)
582 scm *x = internal_lookup_symbol (s);
583 return x ? x : internal_make_symbol (s);
589 scm *p = (scm*)malloc (sizeof (scm));
591 p->length = n->value;
592 p->vector = (scm**)malloc (n->value * sizeof (scm*));
593 for (int i=0; i<n->value; i++) p->vector[i] = &scm_unspecified;
598 string (scm *x) ///((args . n))
600 char buf[STRING_MAX] = "";
602 while (x != &scm_nil)
605 assert (s->type == CHAR);
609 return make_string (buf);
613 string_append (scm *x) ///((args . n))
615 char buf[STRING_MAX] = "";
617 while (x != &scm_nil)
620 assert (s->type == STRING);
621 strcat (buf, s->name);
624 return make_string (buf);
628 list_to_string (scm *x)
630 char buf[STRING_MAX] = "";
632 while (x != &scm_nil)
635 assert (s->type == CHAR);
640 return make_string (buf);
644 string_length (scm *x)
646 assert (x->type == STRING);
647 return make_number (strlen (x->name));
651 string_ref (scm *x, scm *k)
653 assert (x->type == STRING);
654 assert (k->type == NUMBER);
655 return make_char (x->name[k->value]);
659 substring (scm *x) ///((args . n))
661 assert (x->type == PAIR);
662 assert (x->car->type == STRING);
663 char const *s = x->car->name;
664 assert (x->cdr->car->type == NUMBER);
665 int start = x->cdr->car->value;
666 int end = strlen (s);
667 if (x->cdr->cdr->type == PAIR) {
668 assert (x->cdr->cdr->car->type == NUMBER);
669 assert (x->cdr->cdr->car->value <= end);
670 end = x->cdr->cdr->car->value;
672 char buf[STRING_MAX];
673 strncpy (buf, s+start, end - start);
675 return make_string (buf);
682 while (x != &scm_nil)
687 return make_number (n);
693 //if (x != &scm_nil && cdr (x) != &scm_nil)
694 //return last_pair (cdr (x));
695 while (x != &scm_nil && cdr (x) != &scm_nil)
701 builtin_list (scm *x) ///((args . n))
707 values (scm *x) ///((args . n))
709 scm *v = cons (0, x);
715 call_with_values_env (scm *producer, scm *consumer, scm *a)
717 scm *v = apply_env (producer, &scm_nil, a);
718 if (v->type == VALUES)
720 return apply_env (consumer, v, a);
724 vector_length (scm *x)
726 assert (x->type == VECTOR);
727 return make_number (x->length);
731 vector_ref (scm *x, scm *i)
733 assert (x->type == VECTOR);
734 assert (i->value < x->length);
735 return x->vector[i->value];
739 vector_set_x (scm *x, scm *i, scm *e)
741 assert (x->type == VECTOR);
742 assert (i->value < x->length);
743 x->vector[i->value] = e;
744 return &scm_unspecified;
748 lookup (char const *s, scm *a)
750 if (isdigit (*s) || (*s == '-' && isdigit (*(s+1))))
751 return make_number (atoi (s));
754 x = internal_lookup_symbol (s);
757 if (*s == '\'') return &symbol_quote;
758 if (*s == '`') return &symbol_quasiquote;
759 if (*s == ',' && *(s+1) == '@') return &symbol_unquote_splicing;
760 if (*s == ',') return &symbol_unquote;
762 if (*s == '#' && *(s+1) == '\'') return &symbol_syntax;
763 if (*s == '#' && *(s+1) == '`') return &symbol_quasisyntax;
764 if (*s == '#' && *(s+1) == ',' && *(s+2) == '@') return &symbol_unsyntax_splicing;
765 if (*s == '#' && *(s+1) == ',') return &symbol_unsyntax;
767 if (!strcmp (s, "EOF")) {
768 fprintf (stderr, "mes: got EOF\n");
769 return &scm_nil; // `EOF': eval program, which may read stdin
772 return internal_make_symbol (s);
776 lookup_char (int c, scm *a)
781 return lookup (buf, a);
787 static char buf[STRING_MAX];
789 while (l != &scm_nil) {
791 assert (c->type == NUMBER);
800 list_to_vector (scm *x)
802 temp_number.value = length (x)->value;
803 scm *v = make_vector (&temp_number);
805 while (x != &scm_nil)
814 integer_to_char (scm *x)
816 assert (x->type == NUMBER);
817 return make_char (x->value);
821 char_to_integer (scm *x)
823 assert (x->type == CHAR);
824 return make_number (x->value);
828 number_to_string (scm *x)
830 assert (x->type == NUMBER);
831 char buf[STRING_MAX];
832 sprintf (buf,"%d", x->value);
833 return make_string (buf);
837 builtin_exit (scm *x)
839 assert (x->type == NUMBER);
844 string_to_symbol (scm *x)
846 assert (x->type == STRING);
847 return make_symbol (x->name);
851 symbol_to_string (scm *x)
853 assert (x->type == SYMBOL);
854 return make_string (x->name);
858 vector_to_list (scm *v)
861 for (int i = 0; i < v->length; i++)
862 x = append2 (x, cons (v->vector[i], &scm_nil));
867 newline (scm *p) ///((args . n))
870 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
871 FILE *f = fd == 1 ? stdout : stderr;
873 return &scm_unspecified;
877 force_output (scm *p) ///((args . n))
880 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
881 FILE *f = fd == 1 ? stdout : stderr;
886 display_helper (FILE* f, scm *x, bool cont, char const *sep, bool quote)
889 fprintf (f, "%s", sep);
890 if (x->type == CHAR && x->value == char_nul.value) fprintf (f, "#\\%s", char_nul.name);
891 else if (x->type == CHAR && x->value == char_backspace.value) fprintf (f, "#\\%s", char_backspace.name);
892 else if (x->type == CHAR && x->value == char_tab.value) fprintf (f, "#\\%s", char_tab.name);
893 else if (x->type == CHAR && x->value == char_newline.value) fprintf (f, "#\\%s", char_newline.name);
894 else if (x->type == CHAR && x->value == char_vt.value) fprintf (f, "#\\%s", char_vt.name);
895 else if (x->type == CHAR && x->value == char_page.value) fprintf (f, "#\\%s", char_page.name);
896 else if (x->type == CHAR && x->value == char_return.value) fprintf (f, "#\\%s", char_return.name);
897 else if (x->type == CHAR && x->value == char_space.value) fprintf (f, "#\\%s", char_space.name);
898 else if (x->type == CHAR) fprintf (f, "#\\%c", x->value);
899 else if (x->type == MACRO) {
900 fprintf (f, "(*macro* ");
901 display_helper (f, x->macro, cont, sep, quote);
904 else if (x->type == NUMBER) fprintf (f, "%d", x->value);
905 else if (x->type == PAIR) {
906 if (car (x) == &scm_circular) {
907 fprintf (f, "(*circ* . #-1#)");
908 return &scm_unspecified;
910 if (car (x) == &scm_closure) {
911 fprintf (f, "(*closure* . #-1#)");
912 return &scm_unspecified;
914 if (car (x) == &scm_quote) {
916 return display_helper (f, car (cdr (x)), cont, "", true);
918 if (!cont) fprintf (f, "(");
919 display_ (f, car (x));
920 if (cdr (x)->type == PAIR)
921 display_helper (f, cdr (x), true, " ", false);
922 else if (cdr (x) != &scm_nil) {
924 display_ (f, cdr (x));
926 if (!cont) fprintf (f, ")");
928 else if (x->type == VECTOR) {
929 fprintf (f, "#(", x->length);
930 for (int i = 0; i < x->length; i++) {
931 if (x->vector[i]->type == VECTOR)
932 fprintf (f, "%s#(...)", i ? " " : "");
934 display_helper (f, x->vector[i], false, i ? " " : "", false);
938 else if (builtin_p (x) == &scm_t) fprintf (f, "#<procedure %s>", x->name);
939 else if (pair_p (x) == &scm_f) fprintf (f, "%s", x->name);
941 return &scm_unspecified;
949 return ungetc (c, stdin);
963 return make_char (peekchar ());
969 return make_char (getchar ());
973 write_char (scm *x) ///((args . n))
978 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
979 FILE *f = fd == 1 ? stdout : stderr;
980 assert (c->type == NUMBER || c->type == CHAR);
988 assert (c->type == NUMBER || c->type == CHAR);
989 ungetchar (c->value);
996 if (c == '\n') return c;
997 return readcomment (getchar ());
1003 if (c == '!' && peekchar () == '#') return getchar ();
1004 return readblock (getchar ());
1008 readword (int c, char *w, scm *a)
1010 if (c == EOF && !w) return &scm_nil;
1011 if (c == '\n' && !w) return readword (getchar (), w, a);
1012 if (c == '\n' && *w == '.' && w[1] == 0) return &scm_dot;
1013 if (c == EOF || c == '\n') return lookup (w, a);
1014 if (c == ' ') return readword ('\n', w, a);
1015 if (c == '"' && !w) return readstring ();
1016 if (c == '"') {ungetchar (c); return lookup (w, a);}
1017 if (c == '(' && !w) return readlist (a);
1018 if (c == '(') {ungetchar (c); return lookup (w, a);}
1019 if (c == ')' && !w) {ungetchar (c); return &scm_nil;}
1020 if (c == ')') {ungetchar (c); return lookup (w, a);}
1021 if (c == ',' && peekchar () == '@') {getchar (); return cons (lookup (",@", a),
1022 cons (readword (getchar (), w, a),
1027 && !w) {return cons (lookup_char (c, a),
1028 cons (readword (getchar (), w, a),
1030 if (c == '#' && peekchar () == ',' && !w) {
1032 if (peekchar () == '@'){getchar (); return cons (lookup ("#,@", a),
1033 cons (readword (getchar (), w, a),
1035 return cons (lookup ("#,", a), cons (readword (getchar (), w, a), &scm_nil));
1038 && (peekchar () == '\''
1039 || peekchar () == '`')
1040 && !w) {char buf[3] = "#"; buf[1] = getchar (); return cons (lookup (buf, a),
1041 cons (readword (getchar (), w, a),
1043 if (c == ';') {readcomment (c); return readword ('\n', w, a);}
1044 if (c == '#' && peekchar () == 'x') {getchar (); return read_hex ();}
1045 if (c == '#' && peekchar () == '\\') {getchar (); return read_character ();}
1046 if (c == '#' && !w && peekchar () == '(') {getchar (); return list_to_vector (readlist (a));}
1047 if (c == '#' && peekchar () == '(') {ungetchar (c); return lookup (w, a);}
1048 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return readword (getchar (), w, a);}
1049 char buf[STRING_MAX] = {0};
1051 char *p = w ? w + strlen (w) : buf;
1054 return readword (getchar (), w ? w : buf, a);
1061 int c = peekchar ();
1062 while ((c >= '0' && c <= '9')
1063 || (c >= 'A' && c <= 'F')
1064 || (c >= 'a' && c <= 'f')) {
1066 if (c >= 'a') n += c - 'a' + 10;
1067 else if (c >= 'A') n += c - 'A' + 10;
1072 return make_number (n);
1079 if (c >= '0' && c <= '7'
1080 && peekchar () >= '0' && peekchar () <= '7') {
1082 while (peekchar () >= '0' && peekchar () <= '7') {
1084 c += getchar () - '0';
1087 else if (c >= 'a' && c <= 'z'
1088 && peekchar () >= 'a' && peekchar () <= 'z') {
1089 char buf[STRING_MAX];
1092 while (peekchar () >= 'a' && peekchar () <= 'z') {
1096 if (!strcmp (buf, char_nul.name)) c = char_nul.value;
1097 else if (!strcmp (buf, char_backspace.name)) c = char_backspace.value;
1098 else if (!strcmp (buf, char_tab.name)) c = char_tab.value;
1099 else if (!strcmp (buf, char_newline.name)) c = char_newline.value;
1100 else if (!strcmp (buf, char_vt.name)) c = char_vt.value;
1101 else if (!strcmp (buf, char_page.name)) c = char_page.value;
1102 else if (!strcmp (buf, char_return.name)) c = char_return.value;
1103 else if (!strcmp (buf, char_space.name)) c = char_space.value;
1105 fprintf (stderr, "char not supported: %s\n", buf);
1106 assert (!"char not supported");
1109 return make_char (c);
1115 char buf[STRING_MAX];
1119 if (c == '"') break;
1120 if (c == '\\' && peekchar () == '"') *p++ = getchar ();
1121 else if (c == '\\' && peekchar () == 'n') {getchar (); *p++ = '\n';}
1122 else if (c == EOF) assert (!"EOF in string");
1127 return make_string (buf);
1131 eat_whitespace (int c)
1133 while (c == ' ' || c == '\t' || c == '\n') c = getchar ();
1134 if (c == ';') return eat_whitespace (readcomment (c));
1135 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return eat_whitespace (getchar ());}
1143 c = eat_whitespace (c);
1144 if (c == ')') return &scm_nil;
1145 scm *w = readword (c, 0, a);
1147 return car (readlist (a));
1148 return cons (w, readlist (a));
1154 return readword (getchar (), 0, a);
1158 greater_p (scm *x) ///((name . ">") (args . n))
1161 while (x != &scm_nil)
1163 assert (x->car->type == NUMBER);
1164 if (x->car->value >= n) return &scm_f;
1172 less_p (scm *x) ///((name . "<") (args . n))
1175 while (x != &scm_nil)
1177 assert (x->car->type == NUMBER);
1178 if (x->car->value <= n) return &scm_f;
1186 is_p (scm *x) ///((name . "=") (args . n))
1188 if (x == &scm_nil) return &scm_t;
1189 assert (x->car->type == NUMBER);
1190 int n = x->car->value;
1192 while (x != &scm_nil)
1194 if (x->car->value != n) return &scm_f;
1201 minus (scm *x) ///((name . "-") (args . n))
1204 assert (a->type == NUMBER);
1209 while (x != &scm_nil)
1211 assert (x->car->type == NUMBER);
1215 return make_number (n);
1219 plus (scm *x) ///((name . "+") (args . n))
1222 while (x != &scm_nil)
1224 assert (x->car->type == NUMBER);
1228 return make_number (n);
1232 divide (scm *x) ///((name . "/") (args . n))
1235 if (x != &scm_nil) {
1236 assert (x->car->type == NUMBER);
1240 while (x != &scm_nil)
1242 assert (x->car->type == NUMBER);
1246 return make_number (n);
1250 modulo (scm *a, scm *b)
1252 assert (a->type == NUMBER);
1253 assert (b->type == NUMBER);
1254 return make_number (a->value % b->value);
1258 multiply (scm *x) ///((name . "*") (args . n))
1261 while (x != &scm_nil)
1263 assert (x->car->type == NUMBER);
1267 return make_number (n);
1271 logior (scm *x) ///((args . n))
1274 while (x != &scm_nil)
1276 assert (x->car->type == NUMBER);
1280 return make_number (n);
1284 add_environment (scm *a, char const *name, scm *x)
1286 return cons (cons (make_symbol (name), x), a);
1290 mes_environment () ///((internal))
1294 #include "mes.symbols.i"
1297 symbols = cons (&scm_label, symbols);
1298 a = cons (cons (&scm_label, &scm_t), a);
1301 a = cons (cons (&scm_f, &scm_f), a);
1302 a = cons (cons (&scm_nil, &scm_nil), a);
1303 a = cons (cons (&scm_t, &scm_t), a);
1304 a = cons (cons (&scm_unspecified, &scm_unspecified), a);
1305 a = cons (cons (&symbol_begin, &symbol_begin), a);
1306 a = cons (cons (&symbol_quote, &scm_quote), a);
1307 a = cons (cons (&symbol_syntax, &scm_syntax), a);
1309 #include "mes.environment.i"
1310 #include "type.environment.i"
1312 a = cons (cons (&scm_closure, a), a);
1317 make_lambda (scm *args, scm *body)
1319 return cons (&scm_lambda, cons (args, body));
1323 make_closure (scm *args, scm *body, scm *a)
1325 return cons (&scm_closure, cons (cons (&scm_circular, a), cons (args, body)));
1330 define (scm *x, scm *a)
1333 scm *name = cadr (x);
1334 if (name->type != PAIR)
1335 e = builtin_eval (caddr (x), cons (cons (cadr (x), cadr (x)), a));
1338 scm *p = pairlis (cadr (x), cadr (x), a);
1339 e = builtin_eval (make_lambda (cdadr (x), cddr (x)), p);
1341 if (eq_p (car (x), &symbol_define_macro) == &scm_t)
1342 e = make_macro (name, e);
1343 scm *entry = cons (name, e);
1344 scm *aa = cons (entry, &scm_nil);
1345 set_cdr_x (aa, cdr (a));
1347 scm *cl = assq (&scm_closure, a);
1352 scm*define (scm *x, scm *a){}
1356 define_macro (scm *x, scm *a)
1361 lookup_macro (scm *x, scm *a)
1363 if (x->type != SYMBOL) return &scm_f;
1364 scm *m = assq_ref_cache (x, a);
1365 if (macro_p (m) == &scm_t) return m->macro;
1370 read_file (scm *e, scm *a)
1372 if (e == &scm_nil) return e;
1374 scm *x = cons (e, read_file (read_env (a), a));
1375 display_ (stderr, x);
1377 return cons (e, read_file (read_env (a), a));
1382 main (int argc, char *argv[])
1384 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
1385 if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.0\n");
1386 scm *a = mes_environment ();
1387 display_ (stderr, builtin_eval (cons (&symbol_begin, read_file (read_env (a), a)), a));