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/>.
35 #define FIXED_PRIMITIVES 1
39 int ARENA_SIZE = 1000000;
41 int ARENA_SIZE = 100000;
43 int MAX_ARENA_SIZE = 20000000;
47 enum type_t {CHAR, FUNCTION, MACRO, NUMBER, PAIR, SPECIAL, STRING, SYMBOL, REF, VALUES, VECTOR, BROKEN_HEART};
48 typedef SCM (*function0_t) (void);
49 typedef SCM (*function1_t) (SCM);
50 typedef SCM (*function2_t) (SCM, SCM);
51 typedef SCM (*function3_t) (SCM, SCM, SCM);
52 typedef SCM (*functionn_t) (SCM);
53 typedef struct function_t {
55 function0_t function0;
56 function1_t function1;
57 function2_t function2;
58 function3_t function3;
59 functionn_t functionn;
64 typedef struct scm_t {
83 scm scm_nil = {SPECIAL, "()"};
84 scm scm_f = {SPECIAL, "#f"};
85 scm scm_t = {SPECIAL, "#t"};
86 scm scm_dot = {SPECIAL, "."};
87 scm scm_undefined = {SPECIAL, "*undefined*"};
88 scm scm_unspecified = {SPECIAL, "*unspecified*"};
89 scm scm_closure = {SPECIAL, "*closure*"};
90 scm scm_circular = {SPECIAL, "*circular*"};
95 scm scm_begin = {SPECIAL, "*begin*"};
97 scm scm_symbol_lambda = {SYMBOL, "lambda"};
98 scm scm_symbol_begin = {SYMBOL, "begin"};
99 scm scm_symbol_if = {SYMBOL, "if"};
100 scm scm_symbol_define = {SYMBOL, "define"};
101 scm scm_symbol_define_macro = {SYMBOL, "define-macro"};
102 scm scm_symbol_set_x = {SYMBOL, "set!"};
104 scm scm_symbol_quote = {SYMBOL, "quote"};
105 scm scm_symbol_quasiquote = {SYMBOL, "quasiquote"};
106 scm scm_symbol_unquote = {SYMBOL, "unquote"};
107 scm scm_symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
109 scm scm_symbol_sc_expand = {SYMBOL, "sc-expand"};
110 scm scm_symbol_expand_macro = {SYMBOL, "expand-macro"};
111 scm scm_symbol_sc_expander_alist = {SYMBOL, "*sc-expander-alist*"};
112 scm scm_symbol_noexpand = {SYMBOL, "noexpand"};
113 scm scm_symbol_syntax = {SYMBOL, "syntax"};
114 scm scm_symbol_quasisyntax = {SYMBOL, "quasisyntax"};
115 scm scm_symbol_unsyntax = {SYMBOL, "unsyntax"};
116 scm scm_symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
118 scm scm_symbol_call_with_values = {SYMBOL, "call-with-values"};
119 scm scm_symbol_current_module = {SYMBOL, "current-module"};
120 scm scm_symbol_primitive_load = {SYMBOL, "primitive-load"};
121 scm scm_symbol_read_input_file = {SYMBOL, "read-input-file"};
123 scm scm_symbol_the_unquoters = {SYMBOL, "*the-unquoters*"};
125 scm scm_symbol_car = {SYMBOL, "car"};
126 scm scm_symbol_cdr = {SYMBOL, "cdr"};
127 scm scm_symbol_null_p = {SYMBOL, "null?"};
128 scm scm_symbol_eq_p = {SYMBOL, "eq?"};
129 scm scm_symbol_cons = {SYMBOL, "cons"};
131 scm char_eof = {CHAR, .name="*eof*", .value=-1};
132 scm char_nul = {CHAR, .name="nul", .value=0};
133 scm char_backspace = {CHAR, .name="backspace", .value=8};
134 scm char_tab = {CHAR, .name="tab", .value=9};
135 scm char_newline = {CHAR, .name="newline", .value=10};
136 scm char_vt = {CHAR, .name="vt", .value=11};
137 scm char_page = {CHAR, .name="page", .value=12};
138 scm char_return = {CHAR, .name="return", .value=13};
139 scm char_space = {CHAR, .name="space", .value=32};
141 scm g_free = {NUMBER, .value=0};
145 #include "mes.symbols.h"
153 function functions[200];
159 SCM r1 = 0; // param 1
160 SCM r2 = 0; // param 2
161 SCM r3 = 0; // param 3
169 #include "quasiquote.h"
174 #define CAR(x) g_cells[x].car
175 #define CDR(x) g_cells[x].cdr
176 #define HITS(x) g_cells[x].hits
177 #define LENGTH(x) g_cells[x].length
178 #define NAME(x) g_cells[x].name
179 #define STRING(x) g_cells[x].string
180 #define TYPE(x) g_cells[x].type
181 #define MACRO(x) g_cells[x].macro
182 #define REF(x) g_cells[x].ref
183 #define VALUE(x) g_cells[x].value
184 #define VECTOR(x) g_cells[x].vector
185 #define FUNCTION(x) functions[g_cells[x].function]
186 #define NCAR(x) g_news[x].car
187 #define NTYPE(x) g_news[x].type
189 #define CAAR(x) CAR (CAR (x))
190 #define CDAR(x) CDR (CAR (x))
191 #define CAAR(x) CAR (CAR (x))
192 #define CADAR(x) CAR (CDR (CAR (x)))
193 #define CADDR(x) CAR (CDR (CDR (x)))
194 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
195 #define CADR(x) CAR (CDR (x))
197 SCM display_ (FILE* f, SCM x);
198 SCM vm_call (function0_t f, SCM p1, SCM p2, SCM a);
203 assert (g_free.value + n < ARENA_SIZE);
204 SCM x = g_free.value;
210 make_cell (SCM type, SCM car, SCM cdr)
213 assert (TYPE (type) == NUMBER);
214 TYPE (x) = VALUE (type);
215 if (VALUE (type) == CHAR || VALUE (type) == NUMBER) {
216 if (car) CAR (x) = CAR (car);
217 if (cdr) CDR (x) = CDR (cdr);
218 } else if (VALUE (type) == FUNCTION) {
219 if (car) CAR (x) = car;
220 if (cdr) CDR (x) = CDR (cdr);
231 g_cells[tmp_num].value = PAIR;
232 return make_cell (tmp_num, x, y);
238 assert (TYPE (x) == PAIR);
245 assert (TYPE (x) == PAIR);
253 || (TYPE (x) == CHAR && TYPE (y) == CHAR
254 && VALUE (x) == VALUE (y))
255 || (TYPE (x) == NUMBER && TYPE (y) == NUMBER
256 && VALUE (x) == VALUE (y)))
261 set_car_x (SCM x, SCM e)
263 assert (TYPE (x) == PAIR);
265 return cell_unspecified;
269 set_cdr_x (SCM x, SCM e)
271 assert (TYPE (x) == PAIR);
273 return cell_unspecified;
277 set_env_x (SCM x, SCM e, SCM a)
279 SCM p = assert_defined (x, assq (x, a));
280 return set_cdr_x (p, e);
286 return cons (cell_symbol_quote, x);
292 return cons (cell_symbol_quasiquote, x);
298 return cons (cell_symbol_quasisyntax, x);
302 pairlis (SCM x, SCM y, SCM a)
306 if (pair_p (x) == cell_f)
307 return cons (cons (x, y), a);
308 return cons (cons (car (x), car (y)),
309 pairlis (cdr (x), cdr (y), a));
315 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f)
317 if (TYPE (a) == BROKEN_HEART || TYPE (CAR (a)) == BROKEN_HEART)
318 fprintf (stderr, "oops, broken heart\n");
321 return a != cell_nil ? car (a) : cell_f;
325 assq_ref_cache (SCM x, SCM a)
328 if (x == cell_f) return cell_undefined;
333 assert_defined (SCM x, SCM e)
335 if (e == cell_undefined)
337 fprintf (stderr, "eval: unbound variable:");
338 display_ (stderr, x);
339 fprintf (stderr, "\n");
340 assert (!"unbound variable");
348 if (r1 == cell_nil) return cell_nil;
349 if (TYPE (r1) != PAIR) return eval_env (r1, r0);
350 r2 = eval_env (car (r1), r0);
351 r1 = evlis_env (cdr (r1), r0);
352 return cons (r2, r1);
358 return vm_call (vm_begin_env, r1, cell_undefined, r0);
362 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
364 SCM cl = cons (cons (cell_closure, x), x);
369 return vm_call_lambda ();
375 if (TYPE (r1) != PAIR)
377 if (TYPE (r1) == FUNCTION) return call (r1, r2);
378 if (r1 == cell_symbol_call_with_values)
379 return call_with_values_env (car (r2), cadr (r2), r0);
380 if (r1 == cell_symbol_current_module) return r0;
385 case cell_symbol_lambda:
387 SCM args = cadr (r1);
388 SCM body = cddr (r1);
389 SCM p = pairlis (args, r2, r0);
390 return call_lambda (body, p, p, r0);
394 SCM args = caddr (r1);
395 SCM body = cdddr (r1);
398 SCM p = pairlis (args, r2, aa);
399 return call_lambda (body, p, aa, r0);
402 case cell_symbol_label:
403 return apply_env (caddr (r1), r2, cons (cons (cadr (r1), caddr (r1)), r0));
406 SCM e = eval_env (r1, r0);
407 char const* type = 0;
408 if (e == cell_f || e == cell_t) type = "bool";
409 if (TYPE (e) == CHAR) type = "char";
410 if (TYPE (e) == NUMBER) type = "number";
411 if (TYPE (e) == STRING) type = "string";
412 if (e == cell_unspecified) type = "*unspecified*";
413 if (e == cell_undefined) type = "*undefined*";
416 fprintf (stderr, "cannot apply: %s: ", type);
417 display_ (stderr, e);
418 fprintf (stderr, " [");
419 display_ (stderr, r1);
420 fprintf (stderr, "]\n");
421 assert (!"cannot apply");
423 return apply_env (e, r2, r0);
436 case cell_symbol_car: return car (eval_env (CADR (r1), r0));
437 case cell_symbol_cdr: return cdr (eval_env (CADR (r1), r0));
438 case cell_symbol_cons: {SCM m = evlis_env (CDR (r1), r0);
439 return cons (CAR (m), CADR (m));}
440 case cell_symbol_null_p: return null_p (eval_env (CADR (r1), r0));
441 #endif // FIXED_PRIMITIVES
442 case cell_symbol_quote: return cadr (r1);
444 case cell_symbol_syntax: return r1;
446 case cell_symbol_begin: return begin_env (r1, r0);
447 case cell_symbol_lambda:
448 return make_closure (cadr (r1), cddr (r1), assq (cell_closure, r0));
449 case cell_closure: return r1;
450 case cell_symbol_if: return if_env (cdr (r1), r0);
452 case cell_symbol_define: return define_env (r1, r0);
453 case cell_symbol_define_macro: return define_env (r1, r0);
456 case cell_symbol_set_x: {
457 SCM x = eval_env (caddr (r1), r0); return set_env_x (cadr (r1), x, r0);
461 case cell_symbol_unquote: return eval_env (cadr (r1), r0);
462 case cell_symbol_quasiquote: return eval_quasiquote (cadr (r1), add_unquoters (r0));
465 case cell_symbol_unsyntax: return eval_env (cadr (r1), r0);
466 case cell_symbol_quasisyntax: return eval_quasisyntax (cadr (r1), add_unsyntaxers (r0));
469 SCM x = expand_macro_env (r1, r0);
470 if (x != r1) return eval_env (x, r0);
471 SCM m = evlis_env (CDR (r1), r0);
472 return apply_env (car (r1), m, r0);
476 case SYMBOL: return assert_defined (r1, assq_ref_cache (r1, r0));
482 vm_expand_macro_env ()
484 if (TYPE (CAR (r1)) == STRING && string_to_symbol (CAR (r1)) == cell_symbol_noexpand)
489 if (TYPE (r1) == PAIR
490 && (macro = lookup_macro (car (r1), r0)) != cell_f)
491 return apply_env (macro, CDR (r1), r0);
492 else if (TYPE (r1) == PAIR
493 && TYPE (CAR (r1)) == SYMBOL
494 && ((expanders = assq_ref_cache (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
495 && ((macro = assq (CAR (r1), expanders)) != cell_f))
497 SCM sc_expand = assq_ref_cache (cell_symbol_expand_macro, r0);
498 if (sc_expand != cell_undefined && sc_expand != cell_f)
499 r1 = apply_env (sc_expand, cons (r1, cell_nil), r0);
507 SCM r = cell_unspecified;
508 while (r1 != cell_nil) {
509 if (TYPE (r1) == PAIR && TYPE (CAR (r1)) == PAIR)
511 if (caar (r1) == cell_symbol_begin)
512 r1 = append2 (cdar (r1), cdr (r1));
513 else if (caar (r1) == cell_symbol_primitive_load)
515 SCM f = read_input_file_env (r0);
516 r1 = append2 (f, cdr (r1));
519 r = eval_env (car (r1), r0);
528 SCM x = eval_env (car (r1), r0);
530 return eval_env (cadr (r1), r0);
531 if (cddr (r1) != cell_nil)
532 return eval_env (caddr (r1), r0);
533 return cell_unspecified;
537 vm_call_with_values_env ()
539 SCM v = apply_env (r1, cell_nil, r0);
540 if (TYPE (v) == VALUES)
542 return apply_env (r2, v, r0);
548 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
549 && x != cell_nil && TYPE (CAR (x)) == VALUES)
550 x = cons (CADAR (x), CDR (x));
551 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
552 && x != cell_nil && TYPE (CDR (x)) == PAIR && TYPE (CADR (x)) == VALUES)
553 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
554 switch (FUNCTION (fn).arity)
556 case 0: return FUNCTION (fn).function0 ();
557 case 1: return FUNCTION (fn).function1 (car (x));
558 case 2: return FUNCTION (fn).function2 (car (x), cadr (x));
559 case 3: return FUNCTION (fn).function3 (car (x), cadr (x), caddr (x));
560 case -1: return FUNCTION (fn).functionn (x);
562 return cell_unspecified;
568 SCM frame = car (stack);
579 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
580 stack = cons (frame, stack);
588 vm_call (function0_t f, SCM p1, SCM p2, SCM a)
590 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
591 stack = cons (frame, stack);
595 if (g_free.value + GC_SAFETY > ARENA_SIZE)
599 frame = gc_frame (stack);
605 evlis_env (SCM m, SCM a)
607 return vm_call (vm_evlis_env, m, cell_undefined, a);
611 apply_env (SCM fn, SCM x, SCM a)
613 return vm_call (vm_apply_env, fn, x, a);
617 eval_env (SCM e, SCM a)
619 return vm_call (vm_eval_env, e, cell_undefined, a);
623 expand_macro_env (SCM e, SCM a)
625 return vm_call (vm_expand_macro_env, e, cell_undefined, a);
629 begin_env (SCM e, SCM a)
631 return vm_call (vm_begin_env, e, cell_undefined, a);
635 if_env (SCM e, SCM a)
637 return vm_call (vm_if_env, e, cell_undefined, a);
641 call_with_values_env (SCM producer, SCM consumer, SCM a)
643 return vm_call (vm_call_with_values_env, producer, consumer, a);
647 append2 (SCM x, SCM y)
649 if (x == cell_nil) return y;
650 assert (TYPE (x) == PAIR);
651 return cons (car (x), append2 (cdr (x), y));
655 append (SCM x) ///((arity . n))
657 if (x == cell_nil) return cell_nil;
658 return append2 (car (x), append (cdr (x)));
664 g_cells[tmp_num].value = CHAR;
665 g_cells[tmp_num2].value = x;
666 return make_cell (tmp_num, tmp_num2, tmp_num2);
670 make_function (SCM name, SCM id, SCM arity)
672 g_cells[tmp_num3].value = FUNCTION;
673 function *f = (function*)malloc (sizeof (function));
674 f->arity = VALUE (arity);
675 g_cells[tmp_num4].value = (long)f;
676 return make_cell (tmp_num3, name, tmp_num4);
680 make_macro (SCM name, SCM x)
682 g_cells[tmp_num].value = MACRO;
683 return make_cell (tmp_num, STRING (name), x);
689 g_cells[tmp_num].value = NUMBER;
690 g_cells[tmp_num2].value = x;
691 return make_cell (tmp_num, tmp_num2, tmp_num2);
697 g_cells[tmp_num].value = REF;
698 return make_cell (tmp_num, x, x);
704 g_cells[tmp_num].value = STRING;
705 return make_cell (tmp_num, x, 0);
709 cstring_to_list (char const* s)
714 p = cons (make_char (s[i]), p);
721 return x == cell_nil ? cell_t : cell_f;
725 internal_make_symbol (SCM s)
727 g_cells[tmp_num].value = SYMBOL;
728 SCM x = make_cell (tmp_num, s, 0);
729 g_symbols = cons (x, g_symbols);
736 SCM x = internal_lookup_symbol (s);
737 return x ? x : internal_make_symbol (s);
744 g_cells[tmp_num].value = VECTOR;
746 SCM x = make_cell (tmp_num, k, v);
747 for (int i=0; i<k; i++) g_cells[v+i] = g_cells[vector_entry (cell_unspecified)];
752 values (SCM x) ///((arity . n))
760 vector_length (SCM x)
762 assert (TYPE (x) == VECTOR);
763 return make_number (LENGTH (x));
767 vector_ref (SCM x, SCM i)
769 assert (TYPE (x) == VECTOR);
770 assert (VALUE (i) < LENGTH (x));
771 SCM e = VECTOR (x) + VALUE (i);
772 if (TYPE (e) == REF) e = g_cells[e].ref;
773 if (TYPE (e) == CHAR) e = make_char (VALUE (e));
774 if (TYPE (e) == NUMBER) e = make_number (VALUE (e));
779 vector_entry (SCM x) {
780 if (TYPE (x) == PAIR || TYPE (x) == SPECIAL || TYPE (x) == STRING || TYPE (x) == SYMBOL || TYPE (x) == VECTOR) x = make_ref (x);
785 vector_set_x (SCM x, SCM i, SCM e)
787 assert (TYPE (x) == VECTOR);
788 assert (VALUE (i) < LENGTH (x));
789 g_cells[VECTOR (x)+g_cells[i].value] = g_cells[vector_entry (e)];
790 return cell_unspecified;
794 list_to_vector (SCM x)
796 VALUE (tmp_num) = VALUE (length (x));
797 SCM v = make_vector (tmp_num);
799 while (x != cell_nil)
801 g_cells[p++] = g_cells[vector_entry (car (x))];
811 return getc (g_stdin);
817 return ungetc (c, g_stdin);
831 return make_number (peekchar ());
837 return make_number (getchar ());
843 return ungetchar (VALUE (i));
847 write_char (SCM x) ///((arity . n))
852 if (TYPE (p) == PAIR && TYPE (car (p)) == NUMBER) fd = VALUE (car (p));
853 FILE *f = fd == 1 ? stdout : stderr;
854 assert (TYPE (c) == NUMBER || TYPE (c) == CHAR);
855 fputc (VALUE (c), f);
860 symbol_to_list (SCM x)
862 assert (TYPE (x) == SYMBOL);
867 char_to_integer (SCM x)
869 assert (TYPE (x) == CHAR);
870 return make_number (VALUE (x));
874 integer_to_char (SCM x)
876 assert (TYPE (x) == NUMBER);
877 return make_char (VALUE (x));
881 make_tmps (scm* cells)
883 tmp = g_free.value++;
884 cells[tmp].type = CHAR;
885 tmp_num = g_free.value++;
886 cells[tmp_num].type = NUMBER;
887 tmp_num2 = g_free.value++;
888 cells[tmp_num2].type = NUMBER;
889 tmp_num3 = g_free.value++;
890 cells[tmp_num3].type = NUMBER;
891 tmp_num4 = g_free.value++;
892 cells[tmp_num4].type = NUMBER;
897 bool g_debug = false;
903 void *p = realloc (g_cells-1, 2*ARENA_SIZE*sizeof(scm));
906 if (g_debug) fprintf (stderr, "cannot up arena: %s: arena=%d\n", strerror (errno), 2*ARENA_SIZE);
907 return cell_unspecified;
917 if (g_debug) fprintf (stderr, "***gc[%d]...", g_free.value);
919 if (g_cells < g_news && ARENA_SIZE < MAX_ARENA_SIZE) gc_up_arena ();
920 for (int i=g_free.value; i<g_symbol_max; i++)
923 g_symbols = gc_copy (g_symbols);
924 SCM new = gc_copy (stack);
925 if (g_debug) fprintf (stderr, "new=%d\n", new, stack);
933 while (scan < g_free.value)
935 if (NTYPE (scan) == MACRO
936 || NTYPE (scan) == PAIR
937 || NTYPE (scan) == REF
939 || NTYPE (scan) == SPECIAL
940 || NTYPE (scan) == STRING
941 || NTYPE (scan) == SYMBOL)
943 SCM car = gc_copy (g_news[scan].car);
944 gc_relocate_car (scan, car);
946 if ((NTYPE (scan) == MACRO
947 || NTYPE (scan) == PAIR
948 || NTYPE (scan) == VALUES)
949 && g_news[scan].cdr) // allow for 0 terminated list of symbols
951 SCM cdr = gc_copy (g_news[scan].cdr);
952 gc_relocate_cdr (scan, cdr);
962 if (TYPE (old) == BROKEN_HEART) return g_cells[old].car;
963 SCM new = g_free.value++;
964 g_news[new] = g_cells[old];
965 if (NTYPE (new) == VECTOR)
967 g_news[new].vector = g_free.value;
968 for (int i=0; i<LENGTH (old); i++)
969 g_news[g_free.value++] = g_cells[VECTOR (old)+i];
971 g_cells[old].type = BROKEN_HEART;
972 g_cells[old].car = new;
977 gc_relocate_car (SCM new, SCM car)
979 g_news[new].car = car;
980 return cell_unspecified;
984 gc_relocate_cdr (SCM new, SCM cdr)
986 g_news[new].cdr = cdr;
987 return cell_unspecified;
993 scm *cells = g_cells;
996 if (g_debug) fprintf (stderr, " => jam[%d]\n", g_free.value);
1003 fprintf (stderr, "cells: ");
1005 display_ (stderr, -1);
1006 fprintf (stderr, "\n");
1009 fprintf (stderr, "news: ");
1011 display_ (stderr, -1);
1012 fprintf (stderr, "\n");
1015 return cell_unspecified;
1018 //
\f Environment setup
1020 acons (SCM key, SCM value, SCM alist)
1022 return cons (cons (key, value), alist);
1026 add_environment (SCM a, char const *name, SCM x)
1028 return acons (make_symbol (cstring_to_list (name)), x, a);
1034 g_cells = (scm *)malloc (2*ARENA_SIZE*sizeof(scm));
1035 g_cells[0].type = VECTOR;
1036 g_cells[0].length = 1000;
1037 g_cells[0].vector = 0;
1039 g_cells[0].type = CHAR;
1040 g_cells[0].value = 'c';
1046 g_news = g_cells-1 + ARENA_SIZE;
1047 g_news[0].type = VECTOR;
1048 g_news[0].length = 1000;
1049 g_news[0].vector = 0;
1051 g_news[0].type = CHAR;
1052 g_news[0].value = 'n';
1056 mes_symbols () ///((internal))
1061 #include "mes.symbols.i"
1063 g_symbol_max = g_free.value;
1064 make_tmps (g_cells);
1067 for (int i=1; i<g_symbol_max; i++)
1068 g_symbols = cons (i, g_symbols);
1073 a = acons (cell_symbol_label, cell_t, a);
1075 a = acons (cell_symbol_begin, cell_begin, a);
1076 a = add_environment (a, "sc-expand", cell_f);
1077 a = acons (cell_closure, a, a);
1079 internal_lookup_symbol (cell_nil);
1085 mes_builtins (SCM a)
1090 #include "display.i"
1094 #include "quasiquote.i"
1099 #include "define.environment.i"
1100 #include "display.environment.i"
1101 #include "lib.environment.i"
1102 #include "math.environment.i"
1103 #include "mes.environment.i"
1104 #include "posix.environment.i"
1105 //#include "quasiquote.environment.i"
1106 #include "reader.environment.i"
1107 #include "string.environment.i"
1108 #include "type.environment.i"
1111 SCM cell_unquote = assq_ref_cache (cell_symbol_unquote, a);
1112 SCM cell_unquote_splicing = assq_ref_cache (cell_symbol_unquote_splicing, a);
1113 SCM the_unquoters = cons (cons (cell_symbol_unquote, cell_unquote),
1114 cons (cons (cell_symbol_unquote_splicing, cell_unquote_splicing),
1116 a = acons (cell_symbol_the_unquoters, the_unquoters, a);
1119 a = add_environment (a, "*dot*", cell_dot);
1120 a = add_environment (a, "*foo-bar-baz*", cell_nil); // FIXME: some off-by one?
1126 mes_stack (SCM a) ///((internal))
1132 stack = cons (cell_nil, cell_nil);
1137 mes_environment () ///((internal))
1139 SCM a = mes_symbols ();
1140 return mes_stack (a);
1144 make_lambda (SCM args, SCM body)
1146 return cons (cell_symbol_lambda, cons (args, body));
1150 make_closure (SCM args, SCM body, SCM a)
1152 return cons (cell_closure, cons (cons (cell_circular, a), cons (args, body)));
1156 lookup_macro (SCM x, SCM a)
1158 if (TYPE (x) != SYMBOL) return cell_f;
1159 SCM m = assq_ref_cache (x, a);
1160 if (macro_p (m) == cell_t) return MACRO (m);
1165 read_input_file_env_ (SCM e, SCM a)
1167 if (e == cell_nil) return e;
1168 return cons (e, read_input_file_env_ (read_env (a), a));
1172 read_input_file_env (SCM a)
1176 return read_input_file_env_ (read_env (r0), r0);
1178 return apply_env (cell_symbol_read_input_file, cell_nil, r0);
1182 load_env (SCM a) ///((internal))
1186 g_stdin = fopen ("module/mes/read-0.mes", "r");
1187 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mes", "r");
1189 if (!g_function) r0 = mes_builtins (r0);
1190 r3 = read_input_file_env (r0);
1196 bload_env (SCM a) ///((internal))
1198 g_stdin = fopen ("module/mes/read-0.mo", "r");
1199 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
1200 char *p = (char*)g_cells;
1201 assert (getchar () == 'M');
1202 assert (getchar () == 'E');
1203 assert (getchar () == 'S');
1204 stack = getchar () << 8;
1205 stack += getchar ();
1212 g_free.value = (p-(char*)g_cells) / sizeof (scm);
1217 r0 = mes_builtins (r0);
1225 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
1226 stack = cons (frame, stack);
1229 char *p = (char*)g_cells;
1230 fputc ('M', stdout);
1231 fputc ('E', stdout);
1232 fputc ('S', stdout);
1233 fputc (stack >> 8, stdout);
1234 fputc (stack % 256, stdout);
1235 for (int i=0; i<g_free.value * sizeof(scm); i++)
1236 fputc (*p++, stdout);
1242 #include "display.c"
1246 #include "quasiquote.c"
1251 main (int argc, char *argv[])
1253 g_debug = getenv ("MES_DEBUG");
1254 if (getenv ("MES_ARENA")) ARENA_SIZE = atoi (getenv ("MES_ARENA"));
1255 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
1256 if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.3\n");
1258 r0 = mes_environment ();
1259 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1260 ? bload_env (r0) : load_env (r0);
1261 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1262 display_ (stderr, begin_env (program, r0));
1265 if (g_debug) fprintf (stderr, "\nstats: [%d]\n", g_free.value);