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/>.
33 enum type {CHAR, MACRO, NUMBER, PAIR, SCM, STRING, SYMBOL, REF, VALUES, VECTOR,
34 FUNCTION0, FUNCTION1, FUNCTION2, FUNCTION3, FUNCTIONn};
36 typedef struct scm_t* (*function0_t) (void);
37 typedef struct scm_t* (*function1_t) (struct scm_t*);
38 typedef struct scm_t* (*function2_t) (struct scm_t*, struct scm_t*);
39 typedef struct scm_t* (*function3_t) (struct scm_t*, struct scm_t*, struct scm_t*);
40 typedef struct scm_t* (*functionn_t) (struct scm_t*);
42 typedef struct scm_t {
53 function0_t function0;
54 function1_t function1;
55 function2_t function2;
56 function3_t function3;
57 functionn_t functionn;
65 #include "define.environment.h"
66 #include "lib.environment.h"
67 #include "math.environment.h"
68 #include "mes.environment.h"
69 #include "quasiquote.environment.h"
70 #include "string.environment.h"
71 #include "type.environment.h"
73 scm *display_ (FILE* f, scm *x);
74 scm *display_helper (FILE*, scm*, bool, char const*, bool);
76 scm scm_nil = {SCM, "()"};
77 scm scm_dot = {SCM, "."};
78 scm scm_f = {SCM, "#f"};
79 scm scm_t = {SCM, "#t"};
80 scm scm_undefined = {SCM, "*undefined*"};
81 scm scm_unspecified = {SCM, "*unspecified*"};
82 scm scm_closure = {SCM, "*closure*"};
83 scm scm_circular = {SCM, "*circular*"};
88 scm scm_lambda = {SCM, "lambda"};
90 scm symbol_begin = {SCM, "begin"};
91 scm symbol_if = {SCM, "if"};
92 scm symbol_define = {SCM, "define"};
93 scm symbol_define_macro = {SCM, "define-macro"};
94 scm symbol_set_x = {SCM, "set!"};
96 scm symbol_quote = {SYMBOL, "quote"};
97 scm symbol_quasiquote = {SYMBOL, "quasiquote"};
98 scm symbol_unquote = {SYMBOL, "unquote"};
99 scm symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
101 scm symbol_sc_expand = {SYMBOL, "sc-expand"};
102 scm symbol_syntax = {SYMBOL, "syntax"};
103 scm symbol_quasisyntax = {SYMBOL, "quasisyntax"};
104 scm symbol_unsyntax = {SYMBOL, "unsyntax"};
105 scm symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
107 scm symbol_call_with_values = {SYMBOL, "call-with-values"};
108 scm symbol_current_module = {SYMBOL, "current-module"};
111 scm char_nul = {CHAR, .name="nul", .value=0};
112 scm char_backspace = {CHAR, .name="backspace", .value=8};
113 scm char_tab = {CHAR, .name="tab", .value=9};
114 scm char_newline = {CHAR, .name="newline", .value=10};
115 scm char_vt = {CHAR, .name="vt", .value=11};
116 scm char_page = {CHAR, .name="page", .value=12};
117 scm char_return = {CHAR, .name="return", .value=13};
118 scm char_space = {CHAR, .name="space", .value=32};
125 assert (x->type == PAIR);
132 assert (x->type == PAIR);
139 return (scm*)malloc (n * sizeof (scm));
143 make_cell (scm *type, scm *car, scm *cdr)
146 assert (type->type == NUMBER);
147 x->type = type->value;
148 if (type->value == CHAR || type->value == NUMBER) {
149 if (car) x->car = car->car;
150 if (cdr) x->cdr = cdr->cdr;
159 cons (scm *x, scm *y)
161 scm t = {NUMBER, .value=PAIR};
162 return make_cell (&t, x, y);
166 eq_p (scm *x, scm *y)
169 || (x->type == CHAR && y->type == CHAR
170 && x->value == y->value)
171 || (x->type == NUMBER && y->type == NUMBER
172 && x->value == y->value))
177 set_car_x (scm *x, scm *e)
179 assert (x->type == PAIR);
181 return &scm_unspecified;
185 set_cdr_x (scm *x, scm *e)
187 assert (x->type == PAIR);
188 cache_invalidate (x->cdr);
190 return &scm_unspecified;
194 set_env_x (scm *x, scm *e, scm *a)
196 cache_invalidate (x);
197 scm *p = assq (x, a);
200 fprintf (stderr, "set!: unbound variable:");
201 display_ (stderr, x);
202 fprintf (stderr, "\n");
203 assert (!"unbound variable");
205 return set_cdr_x (p, e);
211 return cons (&symbol_quote, x);
217 return cons (&symbol_quasiquote, x);
223 return cons (&symbol_quasisyntax, x);
227 pairlis (scm *x, scm *y, scm *a)
231 if (pair_p (x) == &scm_f)
232 return cons (cons (x, y), a);
233 return cons (cons (car (x), car (y)),
234 pairlis (cdr (x), cdr (y), a));
238 assq (scm *x, scm *a)
240 while (a != &scm_nil && eq_p (x, a->car->car) == &scm_f) a = a->cdr;
241 return a != &scm_nil ? a->car : &scm_f;
245 #define CACHE_SIZE 30
250 assq_ref_cache (scm *x, scm *a)
253 if (x == &scm_f) return &scm_f;
256 scm*cache_invalidate (scm*x){}
257 scm*cache_invalidate_range (scm*p,scm*a){}
258 scm*cache_save (scm*p){}
259 scm*cache_lookup (scm*x){}
263 scm *env_cache_cars[CACHE_SIZE];
264 scm *env_cache_cdrs[CACHE_SIZE];
265 int cache_threshold = 0;
269 int n = p->car->hits;
270 if (n < cache_threshold) return &scm_unspecified;
272 for (int i=0; i < CACHE_SIZE; i++) {
273 if (!env_cache_cars[i]) {
277 if (env_cache_cars[i] == p->car) return &scm_unspecified;
278 if (n > env_cache_cars[i]->hits) {
279 n = env_cache_cars[i]->hits;
284 cache_threshold = p->car->hits;
285 env_cache_cars[j] = p->car;
286 env_cache_cdrs[j] = p->cdr;
288 return &scm_unspecified;
292 cache_lookup (scm *x)
294 for (int i=0; i < CACHE_SIZE; i++) {
295 if (!env_cache_cars[i]) break;
296 if (env_cache_cars[i] == x) return env_cache_cdrs[i];
298 return &scm_undefined;
302 cache_invalidate (scm *x)
304 for (int i=0; i < CACHE_SIZE; i++) {
305 if (env_cache_cars[i] == x) {
306 env_cache_cars[i] = 0;
310 return &scm_unspecified;
314 cache_invalidate_range (scm *p, scm *a)
317 cache_invalidate (p->car->car);
320 return &scm_unspecified;
324 assq_ref_cache (scm *x, scm *a)
327 scm *c = cache_lookup (x);
328 if (c != &scm_undefined) return c;
330 while (a != &scm_nil && x != a->car->car) {i++;a = a->cdr;}
331 if (a == &scm_nil) return &scm_undefined;
332 if (i>ENV_HEAD) cache_save (a->car);
338 evlis_env (scm *m, scm *a)
340 if (m == &scm_nil) return &scm_nil;
341 if (m->type != PAIR) return builtin_eval (m, a);
342 scm *e = builtin_eval (car (m), a);
343 return cons (e, evlis_env (cdr (m), a));
347 apply_env (scm *fn, scm *x, scm *a)
349 if (fn->type != PAIR)
351 if (fn == &scm_car) return x->car->car;
352 if (fn == &scm_cdr) return x->car->cdr;
353 if (builtin_p (fn) == &scm_t)
355 if (eq_p (fn, &symbol_call_with_values) == &scm_t)
356 return call (&scm_call_with_values_env, append2 (x, cons (a, &scm_nil)));
357 if (fn == &symbol_current_module) return a;
359 else if (fn->car == &scm_lambda) {
360 scm *p = pairlis (cadr (fn), x, a);
361 cache_invalidate_range (p, a->cdr);
362 scm *r = begin (cddr (fn), cons (cons (&scm_closure, p), p));
363 cache_invalidate_range (p, a->cdr);
366 else if (fn->car == &scm_closure) {
367 scm *args = caddr (fn);
368 scm *body = cdddr (fn);
371 scm *p = pairlis (args, x, a);
372 cache_invalidate_range (p, a->cdr);
373 scm *r = begin (body, cons (cons (&scm_closure, p), p));
374 cache_invalidate_range (p, a->cdr);
378 else if (fn->car == &scm_label)
379 return apply_env (caddr (fn), x, cons (cons (cadr (fn), caddr (fn)), a));
381 scm *efn = builtin_eval (fn, a);
382 if (efn == &scm_f || efn == &scm_t) assert (!"apply bool");
383 if (efn->type == NUMBER) assert (!"apply number");
384 if (efn->type == STRING) assert (!"apply string");
385 return apply_env (efn, x, a);
389 builtin_eval (scm *e, scm *a)
391 if (builtin_p (e) == &scm_t) return e;
392 if (e->type == SCM) return e;
394 e = expand_macro_env (e, a);
396 if (e->type == SYMBOL) {
397 scm *y = assq_ref_cache (e, a);
398 if (y == &scm_undefined) {
399 fprintf (stderr, "eval: unbound variable:");
400 display_ (stderr, e);
401 fprintf (stderr, "\n");
402 assert (!"unbound variable");
406 else if (e->type != PAIR)
408 else if (e->car->type != PAIR)
410 if (e->car == &symbol_quote)
412 if (e->car == &symbol_syntax)
414 if (e->car == &symbol_begin)
416 if (e->car == &scm_lambda)
417 return make_closure (cadr (e), cddr (e), assq (&scm_closure, a));
418 if (e->car == &scm_closure)
420 if (e->car == &symbol_if)
421 return builtin_if (cdr (e), a);
423 if (e->car == &symbol_define)
424 return define (e, a);
425 if (e->car == &symbol_define_macro)
426 return define (e, a);
428 if (e->car == &symbol_define) {
429 fprintf (stderr, "C DEFINE: ");
431 e->cdr->car->type == SYMBOL
432 ? e->cdr->car->string
433 : e->cdr->car->car->string);
434 fprintf (stderr, "\n");
436 assert (e->car != &symbol_define);
437 assert (e->car != &symbol_define_macro);
439 if (e->car == &symbol_set_x)
440 return set_env_x (cadr (e), builtin_eval (caddr (e), a), a);
442 if (e->car == &symbol_unquote)
443 return builtin_eval (cadr (e), a);
444 if (e->car == &symbol_quasiquote)
445 return eval_quasiquote (cadr (e), add_unquoters (a));
446 if (e->car == &symbol_unsyntax)
447 return builtin_eval (cadr (e), a);
448 if (e->car == &symbol_quasisyntax)
449 return eval_quasisyntax (cadr (e), add_unsyntaxers (a));
452 return apply_env (e->car, evlis_env (e->cdr, a), a);
456 expand_macro_env (scm *e, scm *a)
460 && (macro = lookup_macro (e->car, a)) != &scm_f)
461 return expand_macro_env (apply_env (macro, e->cdr, a), a);
466 begin (scm *e, scm *a)
468 scm *r = &scm_unspecified;
469 while (e != &scm_nil) {
470 r = builtin_eval (e->car, a);
477 builtin_if (scm *e, scm *a)
479 if (builtin_eval (car (e), a) != &scm_f)
480 return builtin_eval (cadr (e), a);
481 if (cddr (e) != &scm_nil)
482 return builtin_eval (caddr (e), a);
483 return &scm_unspecified;
489 display (scm *x) ///((args . n))
494 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->hits;
495 FILE *f = fd == 1 ? stdout : stderr;
496 return display_helper (f, e, false, "", false);
500 display_ (FILE* f, scm *x)
502 return display_helper (f, x, false, "", false);
506 call (scm *fn, scm *x)
508 if (fn->type == FUNCTION0)
509 return fn->function0 ();
510 if (x != &scm_nil && x->car->type == VALUES)
511 x = cons (x->car->cdr->car, x->cdr);
512 if (fn->type == FUNCTION1)
513 return fn->function1 (car (x));
514 if (x != &scm_nil && x->cdr->car->type == VALUES)
515 x = cons (x->car, cons (x->cdr->car->cdr->car, x->cdr));
516 if (fn->type == FUNCTION2)
517 return fn->function2 (car (x), cadr (x));
518 if (fn->type == FUNCTION3)
519 return fn->function3 (car (x), cadr (x), caddr (x));
520 if (fn->type == FUNCTIONn)
521 return fn->functionn (x);
522 return &scm_unspecified;
526 append2 (scm *x, scm *y)
528 if (x == &scm_nil) return y;
529 assert (x->type == PAIR);
530 return cons (car (x), append2 (cdr (x), y));
534 append (scm *x) ///((args . n))
536 if (x == &scm_nil) return &scm_nil;
537 return append2 (car (x), append (cdr (x)));
543 scm t = {NUMBER, .value=CHAR};
544 scm n = {NUMBER, .value=x};
545 return make_cell (&t, &n, &n);
549 make_macro (scm *name, scm *x)
551 scm t = {NUMBER, .value=MACRO};
552 return make_cell (&t, name->string, x);
558 scm t = {NUMBER, .value=NUMBER};
559 scm n = {NUMBER, .value=x};
560 return make_cell (&t, &n, &n);
566 scm t = {NUMBER, .value=REF};
567 return make_cell (&t, x, x);
573 scm t = {NUMBER, .value=STRING};
574 return make_cell (&t, x, 0);
578 cstring_to_list (char const* s)
582 p = append2 (p, cons (make_char (*s++), &scm_nil));
589 list_of_char_equal_p (scm *a, scm *b)
591 while (a != &scm_nil && b != &scm_nil && a->car->value == b->car->value) {
592 assert (a->car->type == CHAR);
593 assert (b->car->type == CHAR);
597 return (a == &scm_nil && b == &scm_nil) ? &scm_t : &scm_f;
601 internal_lookup_symbol (scm *s)
605 // .string and .name is the same field; .name is used as a handy
606 // static field initializer. A string can only be mistaken for a
607 // cell with type == PAIR for the one character long, zero-padded
609 if (x->car->string->type != PAIR)
610 x->car->string = cstring_to_list (x->car->name);
611 if (list_of_char_equal_p (x->car->string, s) == &scm_t) break;
619 internal_make_symbol (scm *s)
621 scm t = {NUMBER, .value=SYMBOL};
622 scm *x = make_cell (&t, s, 0);
623 symbols = cons (x, symbols);
630 scm *x = internal_lookup_symbol (s);
631 return x ? x : internal_make_symbol (s);
637 scm t = {NUMBER, .value=VECTOR};
638 scm *v = alloc (n->value);
639 scm *x = make_cell (&t, (scm*)(long)n->value, v);
640 for (int i=0; i<n->value; i++) x->vector[i] = *vector_entry (&scm_unspecified);
645 values (scm *x) ///((args . n))
647 scm *v = cons (0, x);
653 call_with_values_env (scm *producer, scm *consumer, scm *a)
655 scm *v = apply_env (producer, &scm_nil, a);
656 if (v->type == VALUES)
658 return apply_env (consumer, v, a);
662 vector_length (scm *x)
664 assert (x->type == VECTOR);
665 return make_number (x->length);
669 vector_ref (scm *x, scm *i)
671 assert (x->type == VECTOR);
672 assert (i->value < x->length);
673 scm *e = &x->vector[i->value];
674 if (e->type == REF) e = e->ref;
675 if (e->type == CHAR) e = make_char (e->value);
676 if (e->type == NUMBER) e = make_number (e->value);
681 vector_entry (scm *x) {
682 if (x->type == PAIR || x->type == SCM || x->type == STRING || x->type == SYMBOL || x->type == VECTOR) x = make_ref (x);
687 vector_set_x (scm *x, scm *i, scm *e)
689 assert (x->type == VECTOR);
690 assert (i->value < x->length);
691 x->vector[i->value] = *vector_entry (e);
692 return &scm_unspecified;
696 lookup (scm *s, scm *a)
698 if (isdigit (s->car->value) || (s->car->value == '-' && s->cdr != &scm_nil)) {
701 if (s->car->value == '-') {
706 while (p != &scm_nil && isdigit (p->car->value)) {
708 n += p->car->value - '0';
711 if (p == &scm_nil) return make_number (n * sign);
714 scm *x = internal_lookup_symbol (s);
717 if (s->cdr == &scm_nil) {
718 if (s->car->value == '\'') return &symbol_quote;
719 if (s->car->value == '`') return &symbol_quasiquote;
720 if (s->car->value == ',') return &symbol_unquote;
722 else if (s->cdr->cdr == &scm_nil) {
723 if (s->car->value == ',' && s->cdr->car->value == '@') return &symbol_unquote_splicing;
724 if (s->car->value == '#' && s->cdr->car->value == '\'') return &symbol_syntax;
725 if (s->car->value == '#' && s->cdr->car->value == '`') return &symbol_quasisyntax;
726 if (s->car->value == '#' && s->cdr->car->value == ',') return &symbol_unsyntax;
728 else if (s->cdr->cdr->cdr == &scm_nil) {
729 if (s->car->value == '#' && s->cdr->car->value == ',' && s->cdr->cdr->car->value == '@') return &symbol_unsyntax_splicing;
730 if (s->car->value == 'E' && s->cdr->car->value == 'O' && s->cdr->cdr->car->value == 'F') {
731 fprintf (stderr, "mes: got EOF\n");
732 return &scm_nil; // `EOF': eval program, which may read stdin
736 return internal_make_symbol (s);
740 lookup_char (int c, scm *a)
742 return lookup (cons (make_char (c), &scm_nil), a);
746 list_to_vector (scm *x)
748 scm n = {NUMBER, .value=length (x)->value};
749 scm *v = make_vector (&n);
751 while (x != &scm_nil)
753 *p++ = *vector_entry (car (x));
760 newline (scm *p) ///((args . n))
763 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
764 FILE *f = fd == 1 ? stdout : stderr;
766 return &scm_unspecified;
770 force_output (scm *p) ///((args . n))
773 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
774 FILE *f = fd == 1 ? stdout : stderr;
779 display_helper (FILE* f, scm *x, bool cont, char const *sep, bool quote)
782 fprintf (f, "%s", sep);
783 if (x->type == CHAR && x->value == char_nul.value) fprintf (f, "#\\%s", char_nul.name);
784 else if (x->type == CHAR && x->value == char_backspace.value) fprintf (f, "#\\%s", char_backspace.name);
785 else if (x->type == CHAR && x->value == char_tab.value) fprintf (f, "#\\%s", char_tab.name);
786 else if (x->type == CHAR && x->value == char_newline.value) fprintf (f, "#\\%s", char_newline.name);
787 else if (x->type == CHAR && x->value == char_vt.value) fprintf (f, "#\\%s", char_vt.name);
788 else if (x->type == CHAR && x->value == char_page.value) fprintf (f, "#\\%s", char_page.name);
789 else if (x->type == CHAR && x->value == char_return.value) fprintf (f, "#\\%s", char_return.name);
790 else if (x->type == CHAR && x->value == char_space.value) fprintf (f, "#\\%s", char_space.name);
791 else if (x->type == CHAR) fprintf (f, "#\\%c", x->value);
792 else if (x->type == MACRO) {
793 fprintf (f, "(*macro* ");
794 display_helper (f, x->macro, cont, sep, quote);
797 else if (x->type == NUMBER) fprintf (f, "%d", x->value);
798 else if (x->type == PAIR) {
799 if (car (x) == &scm_circular) {
800 fprintf (f, "(*circ* . #-1#)");
801 return &scm_unspecified;
803 if (car (x) == &scm_closure) {
804 fprintf (f, "(*closure* . #-1#)");
805 return &scm_unspecified;
807 if (car (x) == &scm_quote) {
809 return display_helper (f, car (cdr (x)), cont, "", true);
811 if (!cont) fprintf (f, "(");
812 display_ (f, car (x));
813 if (cdr (x)->type == PAIR)
814 display_helper (f, cdr (x), true, " ", false);
815 else if (cdr (x) != &scm_nil) {
817 display_ (f, cdr (x));
819 if (!cont) fprintf (f, ")");
821 else if (x->type == VECTOR) {
822 fprintf (f, "#(", x->length);
823 for (int i = 0; i < x->length; i++) {
824 if (x->vector[i].type == VECTOR
825 || (x->vector[i].type == REF
826 && x->vector[i].ref->type == VECTOR))
827 fprintf (f, "%s#(...)", i ? " " : "");
829 display_helper (f, &x->vector[i], false, i ? " " : "", false);
833 else if (x->type == REF) display_helper (f, x->ref, cont, "", true);
834 else if (builtin_p (x) == &scm_t) fprintf (f, "#<procedure %s>", x->name);
835 else if (x->type != PAIR && x->string) {
838 while (p != &scm_nil) {
839 assert (p->car->type == CHAR);
840 fputc (p->car->value, f);
844 else if (x->type != PAIR && x->name) fprintf (f, "%s", x->name);
846 return &scm_unspecified;
854 return ungetc (c, stdin);
868 return make_char (peekchar ());
874 return make_char (getchar ());
878 write_char (scm *x) ///((args . n))
883 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
884 FILE *f = fd == 1 ? stdout : stderr;
885 assert (c->type == NUMBER || c->type == CHAR);
893 assert (c->type == NUMBER || c->type == CHAR);
894 ungetchar (c->value);
901 if (c == '\n') return c;
902 return readcomment (getchar ());
908 if (c == '!' && peekchar () == '#') return getchar ();
909 return readblock (getchar ());
913 readword (int c, scm *w, scm *a)
915 if (c == EOF && w == &scm_nil) return &scm_nil;
916 if (c == '\n' && w == &scm_nil) return readword (getchar (), w, a);
917 if (c == '\n' && w->car->value == '.' && w->cdr == &scm_nil) return &scm_dot;
918 if (c == EOF || c == '\n') return lookup (w, a);
919 if (c == ' ') return readword ('\n', w, a);
920 if (c == '"' && w == &scm_nil) return readstring ();
921 if (c == '"') {ungetchar (c); return lookup (w, a);}
922 if (c == '(' && w == &scm_nil) return readlist (a);
923 if (c == '(') {ungetchar (c); return lookup (w, a);}
924 if (c == ')' && w == &scm_nil) {ungetchar (c); return &scm_nil;}
925 if (c == ')') {ungetchar (c); return lookup (w, a);}
926 if (c == ',' && peekchar () == '@') {getchar (); return cons (lookup (symbol_unquote_splicing.string, a),
927 cons (readword (getchar (), w, a),
932 && w == &scm_nil) {return cons (lookup_char (c, a),
933 cons (readword (getchar (), w, a),
935 if (c == '#' && peekchar () == ',' && w == &scm_nil) {
937 if (peekchar () == '@'){getchar (); return cons (lookup (symbol_unsyntax_splicing.string, a),
938 cons (readword (getchar (), w, a),
940 return cons (lookup (symbol_unsyntax.string, a), cons (readword (getchar (), w, a), &scm_nil));
943 && (peekchar () == '\''
944 || peekchar () == '`')
945 && w == &scm_nil) {return cons (lookup (cons (make_char ('#'), cons (make_char (getchar ()), &scm_nil)), a),
946 cons (readword (getchar (), w, a),
948 if (c == ';') {readcomment (c); return readword ('\n', w, a);}
949 if (c == '#' && peekchar () == 'x') {getchar (); return read_hex ();}
950 if (c == '#' && peekchar () == '\\') {getchar (); return read_character ();}
951 if (c == '#' && w == &scm_nil && peekchar () == '(') {getchar (); return list_to_vector (readlist (a));}
952 if (c == '#' && peekchar () == '(') {ungetchar (c); return lookup (w, a);}
953 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return readword (getchar (), w, a);}
954 return readword (getchar (), append2 (w, cons (make_char (c), &scm_nil)), a);
962 while ((c >= '0' && c <= '9')
963 || (c >= 'A' && c <= 'F')
964 || (c >= 'a' && c <= 'f')) {
966 if (c >= 'a') n += c - 'a' + 10;
967 else if (c >= 'A') n += c - 'A' + 10;
972 return make_number (n);
979 if (c >= '0' && c <= '7'
980 && peekchar () >= '0' && peekchar () <= '7') {
982 while (peekchar () >= '0' && peekchar () <= '7') {
984 c += getchar () - '0';
987 else if (c >= 'a' && c <= 'z'
988 && peekchar () >= 'a' && peekchar () <= 'z') {
992 while (peekchar () >= 'a' && peekchar () <= 'z') {
996 if (!strcmp (buf, char_nul.name)) c = char_nul.value;
997 else if (!strcmp (buf, char_backspace.name)) c = char_backspace.value;
998 else if (!strcmp (buf, char_tab.name)) c = char_tab.value;
999 else if (!strcmp (buf, char_newline.name)) c = char_newline.value;
1000 else if (!strcmp (buf, char_vt.name)) c = char_vt.value;
1001 else if (!strcmp (buf, char_page.name)) c = char_page.value;
1002 else if (!strcmp (buf, char_return.name)) c = char_return.value;
1003 else if (!strcmp (buf, char_space.name)) c = char_space.value;
1005 fprintf (stderr, "char not supported: %s\n", buf);
1006 assert (!"char not supported");
1009 return make_char (c);
1013 append_char (scm *x, int i)
1015 return append2 (x, cons (make_char (i), &scm_nil));
1024 if (c == '"') break;
1025 if (c == '\\' && peekchar () == '"') p = append_char (p, getchar ());
1026 else if (c == '\\' && peekchar () == 'n') {getchar (); p = append_char (p, '\n');}
1027 else if (c == EOF) assert (!"EOF in string");
1028 else p = append_char (p, c);
1031 return make_string (p);
1035 eat_whitespace (int c)
1037 while (c == ' ' || c == '\t' || c == '\n') c = getchar ();
1038 if (c == ';') return eat_whitespace (readcomment (c));
1039 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return eat_whitespace (getchar ());}
1047 c = eat_whitespace (c);
1048 if (c == ')') return &scm_nil;
1049 scm *w = readword (c, &scm_nil, a);
1051 return car (readlist (a));
1052 return cons (w, readlist (a));
1058 return readword (getchar (), &scm_nil, a);
1062 add_environment (scm *a, char const *name, scm *x)
1064 return cons (cons (make_symbol (cstring_to_list (name)), x), a);
1068 mes_environment () ///((internal))
1072 #include "mes.symbols.i"
1075 symbols = cons (&scm_label, symbols);
1076 a = cons (cons (&scm_label, &scm_t), a);
1079 #include "string.environment.i"
1080 #include "math.environment.i"
1081 #include "lib.environment.i"
1082 #include "mes.environment.i"
1083 #include "define.environment.i"
1084 #include "type.environment.i"
1086 a = cons (cons (&scm_closure, a), a);
1091 make_lambda (scm *args, scm *body)
1093 return cons (&scm_lambda, cons (args, body));
1097 make_closure (scm *args, scm *body, scm *a)
1099 return cons (&scm_closure, cons (cons (&scm_circular, a), cons (args, body)));
1103 lookup_macro (scm *x, scm *a)
1105 if (x->type != SYMBOL) return &scm_f;
1106 scm *m = assq_ref_cache (x, a);
1107 if (macro_p (m) == &scm_t) return m->macro;
1112 read_file_env (scm *e, scm *a)
1114 if (e == &scm_nil) return e;
1115 return cons (e, read_file_env (read_env (a), a));
1119 load_file_env (scm *a)
1121 return begin (read_file_env (read_env (a), a), a);
1128 #include "quasiquote.c"
1132 main (int argc, char *argv[])
1134 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
1135 if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.1\n");
1136 scm *a = mes_environment ();
1137 display_ (stderr, load_file_env (a));