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/>.
32 //#define QUASISYNTAX 0
34 enum type {CHAR, MACRO, NUMBER, PAIR, SCM, STRING, SYMBOL, REF, 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 {
54 function0_t function0;
55 function1_t function1;
56 function2_t function2;
57 function3_t function3;
58 functionn_t functionn;
66 #include "define.environment.h"
67 #include "lib.environment.h"
68 #include "math.environment.h"
69 #include "mes.environment.h"
70 #include "quasiquote.environment.h"
71 #include "string.environment.h"
72 #include "type.environment.h"
74 scm *display_ (FILE* f, scm *x);
75 scm *display_helper (FILE*, scm*, bool, char const*, bool);
77 scm scm_nil = {SCM, "()"};
78 scm scm_dot = {SCM, "."};
79 scm scm_f = {SCM, "#f"};
80 scm scm_t = {SCM, "#t"};
81 scm scm_undefined = {SCM, "*undefined*"};
82 scm scm_unspecified = {SCM, "*unspecified*"};
83 scm scm_closure = {SCM, "*closure*"};
84 scm scm_circular = {SCM, "*circular*"};
89 scm scm_lambda = {SCM, "lambda"};
91 scm symbol_begin = {SCM, "begin"};
92 scm symbol_if = {SCM, "if"};
93 scm symbol_define = {SCM, "define"};
94 scm symbol_define_macro = {SCM, "define-macro"};
95 scm symbol_set_x = {SCM, "set!"};
97 scm symbol_quote = {SYMBOL, "quote"};
98 scm symbol_quasiquote = {SYMBOL, "quasiquote"};
99 scm symbol_unquote = {SYMBOL, "unquote"};
100 scm symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
102 scm symbol_sc_expand = {SYMBOL, "sc-expand"};
103 scm symbol_syntax = {SYMBOL, "syntax"};
104 scm symbol_quasisyntax = {SYMBOL, "quasisyntax"};
105 scm symbol_unsyntax = {SYMBOL, "unsyntax"};
106 scm symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
108 scm symbol_call_with_values = {SYMBOL, "call-with-values"};
109 scm symbol_current_module = {SYMBOL, "current-module"};
112 scm char_nul = {CHAR, .name="nul", .value=0};
113 scm char_backspace = {CHAR, .name="backspace", .value=8};
114 scm char_tab = {CHAR, .name="tab", .value=9};
115 scm char_newline = {CHAR, .name="newline", .value=10};
116 scm char_vt = {CHAR, .name="vt", .value=11};
117 scm char_page = {CHAR, .name="page", .value=12};
118 scm char_return = {CHAR, .name="return", .value=13};
119 scm char_space = {CHAR, .name="space", .value=32};
126 assert (x->type == PAIR);
133 assert (x->type == PAIR);
140 return (scm*)malloc (n * sizeof (scm));
144 make_cell (scm *type, scm *car, scm *cdr)
147 assert (type->type == NUMBER);
148 x->type = type->value;
149 if (type->value == CHAR || type->value == NUMBER) {
150 if (car) x->car = car->car;
151 if (cdr) x->cdr = cdr->cdr;
160 cons (scm *x, scm *y)
162 scm t = {NUMBER, .value=PAIR};
163 return make_cell (&t, x, y);
167 eq_p (scm *x, scm *y)
170 || (x->type == CHAR && y->type == CHAR
171 && x->value == y->value)
172 || (x->type == NUMBER && y->type == NUMBER
173 && x->value == y->value))
178 set_car_x (scm *x, scm *e)
180 assert (x->type == PAIR);
182 return &scm_unspecified;
186 set_cdr_x (scm *x, scm *e)
188 assert (x->type == PAIR);
189 cache_invalidate (x->cdr);
191 return &scm_unspecified;
195 set_env_x (scm *x, scm *e, scm *a)
197 cache_invalidate (x);
198 scm *p = assq (x, a);
201 fprintf (stderr, "set!: unbound variable:");
202 display_ (stderr, x);
203 fprintf (stderr, "\n");
204 assert (!"unbound variable");
206 return set_cdr_x (p, e);
212 return cons (&symbol_quote, x);
218 return cons (&symbol_quasiquote, x);
224 return cons (&symbol_quasisyntax, x);
228 pairlis (scm *x, scm *y, scm *a)
232 if (pair_p (x) == &scm_f)
233 return cons (cons (x, y), a);
234 return cons (cons (car (x), car (y)),
235 pairlis (cdr (x), cdr (y), a));
239 assq (scm *x, scm *a)
241 while (a != &scm_nil && eq_p (x, a->car->car) == &scm_f) a = a->cdr;
242 return a != &scm_nil ? a->car : &scm_f;
246 #define CACHE_SIZE 30
251 assq_ref_cache (scm *x, scm *a)
254 if (x == &scm_f) return &scm_undefined;
257 scm*cache_invalidate (scm*x){}
258 scm*cache_invalidate_range (scm*p,scm*a){}
259 scm*cache_save (scm*p){}
260 scm*cache_lookup (scm*x){}
264 scm *env_cache_cars[CACHE_SIZE];
265 scm *env_cache_cdrs[CACHE_SIZE];
266 int cache_threshold = 0;
270 int n = p->car->hits;
271 if (n < cache_threshold) return &scm_unspecified;
273 for (int i=0; i < CACHE_SIZE; i++) {
274 if (!env_cache_cars[i]) {
278 if (env_cache_cars[i] == p->car) return &scm_unspecified;
279 if (n > env_cache_cars[i]->hits) {
280 n = env_cache_cars[i]->hits;
285 cache_threshold = p->car->hits;
286 env_cache_cars[j] = p->car;
287 env_cache_cdrs[j] = p->cdr;
289 return &scm_unspecified;
293 cache_lookup (scm *x)
295 for (int i=0; i < CACHE_SIZE; i++) {
296 if (!env_cache_cars[i]) break;
297 if (env_cache_cars[i] == x) return env_cache_cdrs[i];
299 return &scm_undefined;
303 cache_invalidate (scm *x)
305 for (int i=0; i < CACHE_SIZE; i++) {
306 if (env_cache_cars[i] == x) {
307 env_cache_cars[i] = 0;
311 return &scm_unspecified;
315 cache_invalidate_range (scm *p, scm *a)
318 cache_invalidate (p->car->car);
321 return &scm_unspecified;
325 assq_ref_cache (scm *x, scm *a)
328 scm *c = cache_lookup (x);
329 if (c != &scm_undefined) return c;
331 while (a != &scm_nil && x != a->car->car) {i++;a = a->cdr;}
332 if (a == &scm_nil) return &scm_undefined;
333 if (i>ENV_HEAD) cache_save (a->car);
339 evlis_env (scm *m, scm *a)
341 if (m == &scm_nil) return &scm_nil;
342 if (m->type != PAIR) return builtin_eval (m, a);
343 scm *e = builtin_eval (car (m), a);
344 return cons (e, evlis_env (cdr (m), a));
348 apply_env (scm *fn, scm *x, scm *a)
350 if (fn->type != PAIR)
352 if (fn == &scm_car) return x->car->car;
353 if (fn == &scm_cdr) return x->car->cdr;
354 if (builtin_p (fn) == &scm_t)
356 if (eq_p (fn, &symbol_call_with_values) == &scm_t)
357 return call (&scm_call_with_values_env, append2 (x, cons (a, &scm_nil)));
358 if (fn == &symbol_current_module) return a;
360 else if (fn->car == &scm_lambda) {
361 scm *p = pairlis (cadr (fn), x, a);
362 cache_invalidate_range (p, a->cdr);
363 scm *r = begin (cddr (fn), cons (cons (&scm_closure, p), p));
364 cache_invalidate_range (p, a->cdr);
367 else if (fn->car == &scm_closure) {
368 scm *args = caddr (fn);
369 scm *body = cdddr (fn);
372 scm *p = pairlis (args, x, a);
373 cache_invalidate_range (p, a->cdr);
374 scm *r = begin (body, cons (cons (&scm_closure, p), p));
375 cache_invalidate_range (p, a->cdr);
379 else if (fn->car == &scm_label)
380 return apply_env (caddr (fn), x, cons (cons (cadr (fn), caddr (fn)), a));
382 scm *efn = builtin_eval (fn, a);
383 if (efn == &scm_f || efn == &scm_t) assert (!"apply bool");
384 if (efn->type == NUMBER) assert (!"apply number");
385 if (efn->type == STRING) assert (!"apply string");
386 if (efn == &scm_unspecified) assert (!"apply *unspecified*");
387 return apply_env (efn, x, a);
391 builtin_eval (scm *e, scm *a)
393 if (builtin_p (e) == &scm_t) return e;
394 if (e->type == SCM) return e;
396 e = expand_macro_env (e, a);
398 if (e->type == SYMBOL) {
399 scm *y = assq_ref_cache (e, a);
400 if (y == &scm_undefined) {
401 fprintf (stderr, "eval: unbound variable:");
402 display_ (stderr, e);
403 fprintf (stderr, "\n");
404 assert (!"unbound variable");
408 else if (e->type != PAIR)
410 else if (e->car->type != PAIR)
412 if (e->car == &symbol_quote)
415 if (e->car == &symbol_syntax)
418 if (e->car == &symbol_begin)
420 if (e->car == &scm_lambda)
421 return make_closure (cadr (e), cddr (e), assq (&scm_closure, a));
422 if (e->car == &scm_closure)
424 if (e->car == &symbol_if)
425 return builtin_if (cdr (e), a);
427 if (e->car == &symbol_define)
428 return define (e, a);
429 if (e->car == &symbol_define_macro)
430 return define (e, a);
432 if (e->car == &symbol_define) {
433 fprintf (stderr, "C DEFINE: ");
435 e->cdr->car->type == SYMBOL
436 ? e->cdr->car->string
437 : e->cdr->car->car->string);
438 fprintf (stderr, "\n");
440 assert (e->car != &symbol_define);
441 assert (e->car != &symbol_define_macro);
443 if (e->car == &symbol_set_x)
444 return set_env_x (cadr (e), builtin_eval (caddr (e), a), a);
446 if (e->car == &symbol_unquote)
447 return builtin_eval (cadr (e), a);
448 if (e->car == &symbol_quasiquote)
449 return eval_quasiquote (cadr (e), add_unquoters (a));
452 if (e->car == &symbol_unsyntax)
453 return builtin_eval (cadr (e), a);
454 if (e->car == &symbol_quasisyntax)
455 return eval_quasisyntax (cadr (e), add_unsyntaxers (a));
458 return apply_env (e->car, evlis_env (e->cdr, a), a);
462 expand_macro_env (scm *e, scm *a)
466 && (macro = lookup_macro (e->car, a)) != &scm_f)
467 return expand_macro_env (apply_env (macro, e->cdr, a), a);
472 begin (scm *e, scm *a)
474 scm *r = &scm_unspecified;
475 while (e != &scm_nil) {
476 r = builtin_eval (e->car, a);
483 builtin_if (scm *e, scm *a)
485 if (builtin_eval (car (e), a) != &scm_f)
486 return builtin_eval (cadr (e), a);
487 if (cddr (e) != &scm_nil)
488 return builtin_eval (caddr (e), a);
489 return &scm_unspecified;
495 display (scm *x) ///((args . n))
500 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->hits;
501 FILE *f = fd == 1 ? stdout : stderr;
502 return display_helper (f, e, false, "", false);
506 display_ (FILE* f, scm *x)
508 return display_helper (f, x, false, "", false);
512 call (scm *fn, scm *x)
514 if (fn->type == FUNCTION0)
515 return fn->function0 ();
516 if (x != &scm_nil && x->car->type == VALUES)
517 x = cons (x->car->cdr->car, x->cdr);
518 if (fn->type == FUNCTION1)
519 return fn->function1 (car (x));
520 if (x != &scm_nil && x->cdr->car->type == VALUES)
521 x = cons (x->car, cons (x->cdr->car->cdr->car, x->cdr));
522 if (fn->type == FUNCTION2)
523 return fn->function2 (car (x), cadr (x));
524 if (fn->type == FUNCTION3)
525 return fn->function3 (car (x), cadr (x), caddr (x));
526 if (fn->type == FUNCTIONn)
527 return fn->functionn (x);
528 return &scm_unspecified;
532 append2 (scm *x, scm *y)
534 if (x == &scm_nil) return y;
535 assert (x->type == PAIR);
536 return cons (car (x), append2 (cdr (x), y));
540 append (scm *x) ///((args . n))
542 if (x == &scm_nil) return &scm_nil;
543 return append2 (car (x), append (cdr (x)));
549 scm t = {NUMBER, .value=CHAR};
550 scm n = {NUMBER, .value=x};
551 return make_cell (&t, &n, &n);
555 make_macro (scm *name, scm *x)
557 scm t = {NUMBER, .value=MACRO};
558 return make_cell (&t, name->string, x);
564 scm t = {NUMBER, .value=NUMBER};
565 scm n = {NUMBER, .value=x};
566 return make_cell (&t, &n, &n);
572 scm t = {NUMBER, .value=REF};
573 return make_cell (&t, x, x);
579 scm t = {NUMBER, .value=STRING};
580 return make_cell (&t, x, 0);
584 cstring_to_list (char const* s)
588 p = append2 (p, cons (make_char (*s++), &scm_nil));
595 list_of_char_equal_p (scm *a, scm *b)
597 while (a != &scm_nil && b != &scm_nil && a->car->value == b->car->value) {
598 assert (a->car->type == CHAR);
599 assert (b->car->type == CHAR);
603 return (a == &scm_nil && b == &scm_nil) ? &scm_t : &scm_f;
607 internal_lookup_symbol (scm *s)
611 // .string and .name is the same field; .name is used as a handy
612 // static field initializer. A string can only be mistaken for a
613 // cell with type == PAIR for the one character long, zero-padded
615 if (x->car->string->type != PAIR)
616 x->car->string = cstring_to_list (x->car->name);
617 if (list_of_char_equal_p (x->car->string, s) == &scm_t) break;
625 internal_make_symbol (scm *s)
627 scm t = {NUMBER, .value=SYMBOL};
628 scm *x = make_cell (&t, s, 0);
629 symbols = cons (x, symbols);
636 scm *x = internal_lookup_symbol (s);
637 return x ? x : internal_make_symbol (s);
643 scm t = {NUMBER, .value=VECTOR};
644 scm *v = alloc (n->value);
645 scm *x = make_cell (&t, (scm*)(long)n->value, v);
646 for (int i=0; i<n->value; i++) x->vector[i] = *vector_entry (&scm_unspecified);
651 values (scm *x) ///((args . n))
653 scm *v = cons (0, x);
659 call_with_values_env (scm *producer, scm *consumer, scm *a)
661 scm *v = apply_env (producer, &scm_nil, a);
662 if (v->type == VALUES)
664 return apply_env (consumer, v, a);
668 vector_length (scm *x)
670 assert (x->type == VECTOR);
671 return make_number (x->length);
675 vector_ref (scm *x, scm *i)
677 assert (x->type == VECTOR);
678 assert (i->value < x->length);
679 scm *e = &x->vector[i->value];
680 if (e->type == REF) e = e->ref;
681 if (e->type == CHAR) e = make_char (e->value);
682 if (e->type == NUMBER) e = make_number (e->value);
687 vector_entry (scm *x) {
688 if (x->type == PAIR || x->type == SCM || x->type == STRING || x->type == SYMBOL || x->type == VECTOR) x = make_ref (x);
693 vector_set_x (scm *x, scm *i, scm *e)
695 assert (x->type == VECTOR);
696 assert (i->value < x->length);
697 x->vector[i->value] = *vector_entry (e);
698 return &scm_unspecified;
702 lookup (scm *s, scm *a)
704 if (isdigit (s->car->value) || (s->car->value == '-' && s->cdr != &scm_nil)) {
707 if (s->car->value == '-') {
712 while (p != &scm_nil && isdigit (p->car->value)) {
714 n += p->car->value - '0';
717 if (p == &scm_nil) return make_number (n * sign);
720 scm *x = internal_lookup_symbol (s);
723 if (s->cdr == &scm_nil) {
724 if (s->car->value == '\'') return &symbol_quote;
725 if (s->car->value == '`') return &symbol_quasiquote;
726 if (s->car->value == ',') return &symbol_unquote;
728 else if (s->cdr->cdr == &scm_nil) {
729 if (s->car->value == ',' && s->cdr->car->value == '@') return &symbol_unquote_splicing;
730 if (s->car->value == '#' && s->cdr->car->value == '\'') return &symbol_syntax;
731 if (s->car->value == '#' && s->cdr->car->value == '`') return &symbol_quasisyntax;
732 if (s->car->value == '#' && s->cdr->car->value == ',') return &symbol_unsyntax;
734 else if (s->cdr->cdr->cdr == &scm_nil) {
735 if (s->car->value == '#' && s->cdr->car->value == ',' && s->cdr->cdr->car->value == '@') return &symbol_unsyntax_splicing;
736 if (s->car->value == 'E' && s->cdr->car->value == 'O' && s->cdr->cdr->car->value == 'F') {
737 fprintf (stderr, "mes: got EOF\n");
738 return &scm_nil; // `EOF': eval program, which may read stdin
742 return internal_make_symbol (s);
746 lookup_char (int c, scm *a)
748 return lookup (cons (make_char (c), &scm_nil), a);
752 list_to_vector (scm *x)
754 scm n = {NUMBER, .value=length (x)->value};
755 scm *v = make_vector (&n);
757 while (x != &scm_nil)
759 *p++ = *vector_entry (car (x));
766 newline (scm *p) ///((args . n))
769 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
770 FILE *f = fd == 1 ? stdout : stderr;
772 return &scm_unspecified;
776 force_output (scm *p) ///((args . n))
779 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
780 FILE *f = fd == 1 ? stdout : stderr;
785 display_helper (FILE* f, scm *x, bool cont, char const *sep, bool quote)
788 fprintf (f, "%s", sep);
789 if (x->type == CHAR && x->value == char_nul.value) fprintf (f, "#\\%s", char_nul.name);
790 else if (x->type == CHAR && x->value == char_backspace.value) fprintf (f, "#\\%s", char_backspace.name);
791 else if (x->type == CHAR && x->value == char_tab.value) fprintf (f, "#\\%s", char_tab.name);
792 else if (x->type == CHAR && x->value == char_newline.value) fprintf (f, "#\\%s", char_newline.name);
793 else if (x->type == CHAR && x->value == char_vt.value) fprintf (f, "#\\%s", char_vt.name);
794 else if (x->type == CHAR && x->value == char_page.value) fprintf (f, "#\\%s", char_page.name);
795 else if (x->type == CHAR && x->value == char_return.value) fprintf (f, "#\\%s", char_return.name);
796 else if (x->type == CHAR && x->value == char_space.value) fprintf (f, "#\\%s", char_space.name);
797 else if (x->type == CHAR) fprintf (f, "#\\%c", x->value);
798 else if (x->type == MACRO) {
799 fprintf (f, "(*macro* ");
800 display_helper (f, x->macro, cont, sep, quote);
803 else if (x->type == NUMBER) fprintf (f, "%d", x->value);
804 else if (x->type == PAIR) {
805 if (car (x) == &scm_circular) {
806 fprintf (f, "(*circ* . #-1#)");
807 return &scm_unspecified;
809 if (car (x) == &scm_closure) {
810 fprintf (f, "(*closure* . #-1#)");
811 return &scm_unspecified;
813 if (car (x) == &scm_quote) {
815 return display_helper (f, car (cdr (x)), cont, "", true);
817 if (!cont) fprintf (f, "(");
818 display_ (f, car (x));
819 if (cdr (x)->type == PAIR)
820 display_helper (f, cdr (x), true, " ", false);
821 else if (cdr (x) != &scm_nil) {
823 display_ (f, cdr (x));
825 if (!cont) fprintf (f, ")");
827 else if (x->type == VECTOR) {
828 fprintf (f, "#(", x->length);
829 for (int i = 0; i < x->length; i++) {
830 if (x->vector[i].type == VECTOR
831 || (x->vector[i].type == REF
832 && x->vector[i].ref->type == VECTOR))
833 fprintf (f, "%s#(...)", i ? " " : "");
835 display_helper (f, &x->vector[i], false, i ? " " : "", false);
839 else if (x->type == REF) display_helper (f, x->ref, cont, "", true);
840 else if (builtin_p (x) == &scm_t) fprintf (f, "#<procedure %s>", x->name);
841 else if (x->type != PAIR && x->string) {
844 while (p != &scm_nil) {
845 assert (p->car->type == CHAR);
846 fputc (p->car->value, f);
850 else if (x->type != PAIR && x->name) fprintf (f, "%s", x->name);
852 return &scm_unspecified;
860 return ungetc (c, stdin);
874 return make_char (peekchar ());
880 return make_char (getchar ());
884 write_char (scm *x) ///((args . n))
889 if (p->type == PAIR && p->car->type == NUMBER) fd = p->car->value;
890 FILE *f = fd == 1 ? stdout : stderr;
891 assert (c->type == NUMBER || c->type == CHAR);
899 assert (c->type == NUMBER || c->type == CHAR);
900 ungetchar (c->value);
907 if (c == '\n') return c;
908 return readcomment (getchar ());
914 if (c == '!' && peekchar () == '#') return getchar ();
915 return readblock (getchar ());
919 readword (int c, scm *w, scm *a)
921 if (c == EOF && w == &scm_nil) return &scm_nil;
922 if (c == '\n' && w == &scm_nil) return readword (getchar (), w, a);
923 if (c == '\n' && w->car->value == '.' && w->cdr == &scm_nil) return &scm_dot;
924 if (c == EOF || c == '\n') return lookup (w, a);
925 if (c == ' ') return readword ('\n', w, a);
926 if (c == '"' && w == &scm_nil) return readstring ();
927 if (c == '"') {ungetchar (c); return lookup (w, a);}
928 if (c == '(' && w == &scm_nil) return readlist (a);
929 if (c == '(') {ungetchar (c); return lookup (w, a);}
930 if (c == ')' && w == &scm_nil) {ungetchar (c); return &scm_nil;}
931 if (c == ')') {ungetchar (c); return lookup (w, a);}
932 if (c == ',' && peekchar () == '@') {getchar (); return cons (lookup (symbol_unquote_splicing.string, a),
933 cons (readword (getchar (), w, a),
938 && w == &scm_nil) {return cons (lookup_char (c, a),
939 cons (readword (getchar (), w, a),
941 if (c == '#' && peekchar () == ',' && w == &scm_nil) {
943 if (peekchar () == '@'){getchar (); return cons (lookup (symbol_unsyntax_splicing.string, a),
944 cons (readword (getchar (), w, a),
946 return cons (lookup (symbol_unsyntax.string, a), cons (readword (getchar (), w, a), &scm_nil));
949 && (peekchar () == '\''
950 || peekchar () == '`')
951 && w == &scm_nil) {return cons (lookup (cons (make_char ('#'), cons (make_char (getchar ()), &scm_nil)), a),
952 cons (readword (getchar (), w, a),
954 if (c == ';') {readcomment (c); return readword ('\n', w, a);}
955 if (c == '#' && peekchar () == 'x') {getchar (); return read_hex ();}
956 if (c == '#' && peekchar () == '\\') {getchar (); return read_character ();}
957 if (c == '#' && w == &scm_nil && peekchar () == '(') {getchar (); return list_to_vector (readlist (a));}
958 if (c == '#' && peekchar () == '(') {ungetchar (c); return lookup (w, a);}
959 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return readword (getchar (), w, a);}
960 return readword (getchar (), append2 (w, cons (make_char (c), &scm_nil)), a);
968 while ((c >= '0' && c <= '9')
969 || (c >= 'A' && c <= 'F')
970 || (c >= 'a' && c <= 'f')) {
972 if (c >= 'a') n += c - 'a' + 10;
973 else if (c >= 'A') n += c - 'A' + 10;
978 return make_number (n);
985 if (c >= '0' && c <= '7'
986 && peekchar () >= '0' && peekchar () <= '7') {
988 while (peekchar () >= '0' && peekchar () <= '7') {
990 c += getchar () - '0';
993 else if (c >= 'a' && c <= 'z'
994 && peekchar () >= 'a' && peekchar () <= 'z') {
998 while (peekchar () >= 'a' && peekchar () <= 'z') {
1002 if (!strcmp (buf, char_nul.name)) c = char_nul.value;
1003 else if (!strcmp (buf, char_backspace.name)) c = char_backspace.value;
1004 else if (!strcmp (buf, char_tab.name)) c = char_tab.value;
1005 else if (!strcmp (buf, char_newline.name)) c = char_newline.value;
1006 else if (!strcmp (buf, char_vt.name)) c = char_vt.value;
1007 else if (!strcmp (buf, char_page.name)) c = char_page.value;
1008 else if (!strcmp (buf, char_return.name)) c = char_return.value;
1009 else if (!strcmp (buf, char_space.name)) c = char_space.value;
1011 fprintf (stderr, "char not supported: %s\n", buf);
1012 assert (!"char not supported");
1015 return make_char (c);
1019 append_char (scm *x, int i)
1021 return append2 (x, cons (make_char (i), &scm_nil));
1030 if (c == '"') break;
1031 if (c == '\\' && peekchar () == '"') p = append_char (p, getchar ());
1032 else if (c == '\\' && peekchar () == 'n') {getchar (); p = append_char (p, '\n');}
1033 else if (c == EOF) assert (!"EOF in string");
1034 else p = append_char (p, c);
1037 return make_string (p);
1041 eat_whitespace (int c)
1043 while (c == ' ' || c == '\t' || c == '\n') c = getchar ();
1044 if (c == ';') return eat_whitespace (readcomment (c));
1045 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return eat_whitespace (getchar ());}
1053 c = eat_whitespace (c);
1054 if (c == ')') return &scm_nil;
1055 scm *w = readword (c, &scm_nil, a);
1057 return car (readlist (a));
1058 return cons (w, readlist (a));
1064 return readword (getchar (), &scm_nil, a);
1068 add_environment (scm *a, char const *name, scm *x)
1070 return cons (cons (make_symbol (cstring_to_list (name)), x), a);
1074 mes_environment () ///((internal))
1078 #include "mes.symbols.i"
1081 symbols = cons (&scm_label, symbols);
1082 a = cons (cons (&scm_label, &scm_t), a);
1085 #include "string.environment.i"
1086 #include "math.environment.i"
1087 #include "lib.environment.i"
1088 #include "mes.environment.i"
1089 #include "define.environment.i"
1090 #include "type.environment.i"
1092 a = cons (cons (&scm_closure, a), a);
1097 make_lambda (scm *args, scm *body)
1099 return cons (&scm_lambda, cons (args, body));
1103 make_closure (scm *args, scm *body, scm *a)
1105 return cons (&scm_closure, cons (cons (&scm_circular, a), cons (args, body)));
1109 lookup_macro (scm *x, scm *a)
1111 if (x->type != SYMBOL) return &scm_f;
1112 scm *m = assq_ref_cache (x, a);
1113 if (macro_p (m) == &scm_t) return m->macro;
1118 read_file_env (scm *e, scm *a)
1120 if (e == &scm_nil) return e;
1121 return cons (e, read_file_env (read_env (a), a));
1125 load_file_env (scm *a)
1127 return begin (read_file_env (read_env (a), a), a);
1134 #include "quasiquote.c"
1138 main (int argc, char *argv[])
1140 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
1141 if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.1\n");
1142 scm *a = mes_environment ();
1143 display_ (stderr, load_file_env (a));