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 FIXED_PRIMITIVES 1
34 int ARENA_SIZE = 100000;
35 int MAX_ARENA_SIZE = 20000000;
39 enum type_t {CHAR, FUNCTION, KEYWORD, MACRO, NUMBER, PAIR, SPECIAL, STRING, SYMBOL, REF, VALUES, VECTOR, BROKEN_HEART};
40 typedef SCM (*function0_t) (void);
41 typedef SCM (*function1_t) (SCM);
42 typedef SCM (*function2_t) (SCM, SCM);
43 typedef SCM (*function3_t) (SCM, SCM, SCM);
44 typedef SCM (*functionn_t) (SCM);
45 typedef struct function_t {
47 function0_t function0;
48 function1_t function1;
49 function2_t function2;
50 function3_t function3;
51 functionn_t functionn;
56 typedef struct scm_t {
75 scm scm_nil = {SPECIAL, "()"};
76 scm scm_f = {SPECIAL, "#f"};
77 scm scm_t = {SPECIAL, "#t"};
78 scm scm_dot = {SPECIAL, "."};
79 scm scm_arrow = {SPECIAL, "=>"};
80 scm scm_undefined = {SPECIAL, "*undefined*"};
81 scm scm_unspecified = {SPECIAL, "*unspecified*"};
82 scm scm_closure = {SPECIAL, "*closure*"};
83 scm scm_circular = {SPECIAL, "*circular*"};
84 scm scm_label = {SPECIAL, "label"};
85 scm scm_begin = {SPECIAL, "*begin*"};
87 scm scm_symbol_lambda = {SYMBOL, "lambda"};
88 scm scm_symbol_begin = {SYMBOL, "begin"};
89 scm scm_symbol_if = {SYMBOL, "if"};
90 scm scm_symbol_define = {SYMBOL, "define"};
91 scm scm_symbol_define_macro = {SYMBOL, "define-macro"};
92 scm scm_symbol_set_x = {SYMBOL, "set!"};
94 scm scm_symbol_quote = {SYMBOL, "quote"};
95 scm scm_symbol_quasiquote = {SYMBOL, "quasiquote"};
96 scm scm_symbol_unquote = {SYMBOL, "unquote"};
97 scm scm_symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
99 scm scm_symbol_sc_expand = {SYMBOL, "sc-expand"};
100 scm scm_symbol_macro_expand = {SYMBOL, "macro-expand"};
101 scm scm_symbol_sc_expander_alist = {SYMBOL, "*sc-expander-alist*"};
102 scm scm_symbol_noexpand = {SYMBOL, "noexpand"};
103 scm scm_symbol_syntax = {SYMBOL, "syntax"};
104 scm scm_symbol_quasisyntax = {SYMBOL, "quasisyntax"};
105 scm scm_symbol_unsyntax = {SYMBOL, "unsyntax"};
106 scm scm_symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
108 scm scm_symbol_call_with_values = {SYMBOL, "call-with-values"};
109 scm scm_symbol_current_module = {SYMBOL, "current-module"};
110 scm scm_symbol_primitive_load = {SYMBOL, "primitive-load"};
111 scm scm_symbol_read_input_file = {SYMBOL, "read-input-file"};
113 scm scm_symbol_car = {SYMBOL, "car"};
114 scm scm_symbol_cdr = {SYMBOL, "cdr"};
115 scm scm_symbol_null_p = {SYMBOL, "null?"};
116 scm scm_symbol_eq_p = {SYMBOL, "eq?"};
117 scm scm_symbol_cons = {SYMBOL, "cons"};
119 scm char_eof = {CHAR, .name="*eof*", .value=-1};
120 scm char_nul = {CHAR, .name="nul", .value=0};
121 scm char_alarm = {CHAR, .name="alarm", .value=8};
122 scm char_backspace = {CHAR, .name="backspace", .value=8};
123 scm char_tab = {CHAR, .name="tab", .value=9};
124 scm char_newline = {CHAR, .name="newline", .value=10};
125 scm char_vtab = {CHAR, .name="vtab", .value=11};
126 scm char_page = {CHAR, .name="page", .value=12};
127 scm char_return = {CHAR, .name="return", .value=13};
128 scm char_space = {CHAR, .name="space", .value=32};
130 scm g_free = {NUMBER, .value=0};
134 #include "mes.symbols.h"
142 function functions[200];
148 SCM r1 = 0; // param 1
149 SCM r2 = 0; // param 2
150 SCM r3 = 0; // param 3
161 #define CAR(x) g_cells[x].car
162 #define CDR(x) g_cells[x].cdr
163 #define HITS(x) g_cells[x].hits
164 #define LENGTH(x) g_cells[x].length
165 #define NAME(x) g_cells[x].name
166 #define STRING(x) g_cells[x].string
167 #define TYPE(x) g_cells[x].type
168 #define MACRO(x) g_cells[x].macro
169 #define REF(x) g_cells[x].ref
170 #define VALUE(x) g_cells[x].value
171 #define VECTOR(x) g_cells[x].vector
172 #define FUNCTION(x) functions[g_cells[x].function]
173 #define NCAR(x) g_news[x].car
174 #define NTYPE(x) g_news[x].type
176 #define CAAR(x) CAR (CAR (x))
177 #define CDAR(x) CDR (CAR (x))
178 #define CAAR(x) CAR (CAR (x))
179 #define CADAR(x) CAR (CDR (CAR (x)))
180 #define CADDR(x) CAR (CDR (CDR (x)))
181 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
182 #define CADR(x) CAR (CDR (x))
184 #define MAKE_CHAR(n) make_cell (tmp_num_ (CHAR), 0, tmp_num2_ (n))
185 #define MAKE_NUMBER(n) make_cell (tmp_num_ (NUMBER), 0, tmp_num2_ (n))
187 SCM display_ (FILE* f, SCM x);
188 SCM vm_call (function0_t f, SCM p1, SCM p2, SCM a);
193 assert (g_free.value + n < ARENA_SIZE);
194 SCM x = g_free.value;
200 make_cell (SCM type, SCM car, SCM cdr)
203 assert (TYPE (type) == NUMBER);
204 TYPE (x) = VALUE (type);
205 if (VALUE (type) == CHAR || VALUE (type) == NUMBER) {
206 if (car) CAR (x) = CAR (car);
207 if (cdr) CDR (x) = CDR (cdr);
208 } else if (VALUE (type) == FUNCTION) {
209 if (car) CAR (x) = car;
210 if (cdr) CDR (x) = CDR (cdr);
221 g_cells[tmp_num].value = PAIR;
222 return make_cell (tmp_num, x, y);
228 assert (TYPE (x) == PAIR);
235 assert (TYPE (x) == PAIR);
243 || ((TYPE (x) == KEYWORD && TYPE (y) == KEYWORD
244 && STRING (x) == STRING (y)))
245 || (TYPE (x) == CHAR && TYPE (y) == CHAR
246 && VALUE (x) == VALUE (y))
247 || (TYPE (x) == NUMBER && TYPE (y) == NUMBER
248 && VALUE (x) == VALUE (y)))
253 set_car_x (SCM x, SCM e)
255 assert (TYPE (x) == PAIR);
257 return cell_unspecified;
261 set_cdr_x (SCM x, SCM e)
263 assert (TYPE (x) == PAIR);
265 return cell_unspecified;
269 set_env_x (SCM x, SCM e, SCM a)
271 SCM p = assert_defined (x, assq (x, a));
272 return set_cdr_x (p, e);
278 return cons (cell_symbol_quote, x);
284 return cons (cell_symbol_quasiquote, x);
290 return cons (cell_symbol_quasisyntax, x);
294 pairlis (SCM x, SCM y, SCM a)
298 if (pair_p (x) == cell_f)
299 return cons (cons (x, y), a);
300 return cons (cons (car (x), car (y)),
301 pairlis (cdr (x), cdr (y), a));
307 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f)
309 if (TYPE (a) == BROKEN_HEART || TYPE (CAR (a)) == BROKEN_HEART)
310 fprintf (stderr, "oops, broken heart\n");
313 return a != cell_nil ? car (a) : cell_f;
317 assq_ref_cache (SCM x, SCM a)
320 if (x == cell_f) return cell_undefined;
325 assert_defined (SCM x, SCM e)
327 if (e == cell_undefined)
329 fprintf (stderr, "eval: unbound variable:");
330 display_ (stderr, x);
331 fprintf (stderr, "\n");
332 assert (!"unbound variable");
337 enum eval_apply_t {EVLIS, APPLY, EVAL, MACRO_EXPAND, BEGIN, IF, CALL_WITH_VALUES};
338 enum eval_apply_t g_target;
341 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
343 SCM cl = cons (cons (cell_closure, x), x);
348 return cell_unspecified;
356 case EVLIS: goto evlis;
357 case APPLY: goto apply;
358 case EVAL: goto eval;
359 case MACRO_EXPAND: goto macro_expand;
360 case BEGIN: goto begin;
361 case IF: goto label_if;
362 case CALL_WITH_VALUES: goto call_with_values;
366 if (r1 == cell_nil) return cell_nil;
367 if (TYPE (r1) != PAIR) goto eval;
368 r2 = eval_env (car (r1), r0);
369 r1 = evlis_env (cdr (r1), r0);
370 return cons (r2, r1);
373 if (TYPE (r1) != PAIR)
375 if (TYPE (r1) == FUNCTION) return call (r1, r2);
376 if (r1 == cell_symbol_call_with_values)
380 goto call_with_values;
382 if (r1 == cell_symbol_current_module) return r0;
387 case cell_symbol_lambda:
389 SCM args = cadr (r1);
390 SCM body = cddr (r1);
391 SCM p = pairlis (args, r2, r0);
392 call_lambda (body, p, p, r0);
397 SCM args = caddr (r1);
398 SCM body = cdddr (r1);
401 SCM p = pairlis (args, r2, aa);
402 call_lambda (body, p, aa, r0);
406 case cell_symbol_label:
408 r0 = cons (cons (cadr (r1), caddr (r1)), r0);
414 SCM e = eval_env (r1, r0);
415 char const* type = 0;
416 if (e == cell_f || e == cell_t) type = "bool";
417 if (TYPE (e) == CHAR) type = "char";
418 if (TYPE (e) == NUMBER) type = "number";
419 if (TYPE (e) == STRING) type = "string";
420 if (e == cell_unspecified) type = "*unspecified*";
421 if (e == cell_undefined) type = "*undefined*";
424 fprintf (stderr, "cannot apply: %s: ", type);
425 display_ (stderr, e);
426 fprintf (stderr, " [");
427 display_ (stderr, r1);
428 fprintf (stderr, "]\n");
429 assert (!"cannot apply");
442 case cell_symbol_car: return car (eval_env (CADR (r1), r0));
443 case cell_symbol_cdr: return cdr (eval_env (CADR (r1), r0));
444 case cell_symbol_cons: {SCM m = evlis_env (CDR (r1), r0);
445 return cons (CAR (m), CADR (m));}
446 case cell_symbol_null_p: return null_p (eval_env (CADR (r1), r0));
447 #endif // FIXED_PRIMITIVES
448 case cell_symbol_quote: return cadr (r1);
449 case cell_symbol_begin: goto begin;
450 case cell_symbol_lambda:
451 return make_closure (cadr (r1), cddr (r1), assq (cell_closure, r0));
452 case cell_closure: return r1;
453 case cell_symbol_if: {r1=cdr (r1); goto label_if;}
454 case cell_symbol_set_x: {
455 SCM x = eval_env (caddr (r1), r0); return set_env_x (cadr (r1), x, r0);
458 SCM x = macro_expand_env (r1, r0);
461 if (TYPE (x) == PAIR)
463 set_cdr_x (r1, cdr (x));
464 set_car_x (r1, car (x));
469 SCM m = evlis_env (CDR (r1), r0);
476 case SYMBOL: return assert_defined (r1, assq_ref_cache (r1, r0));
481 if (TYPE (CAR (r1)) == STRING && string_to_symbol (CAR (r1)) == cell_symbol_noexpand)
486 if (TYPE (r1) == PAIR
487 && (macro = lookup_macro (car (r1), r0)) != cell_f)
493 else if (TYPE (r1) == PAIR
494 && TYPE (CAR (r1)) == SYMBOL
495 && ((expanders = assq_ref_cache (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
496 && ((macro = assq (CAR (r1), expanders)) != cell_f))
498 SCM sc_expand = assq_ref_cache (cell_symbol_macro_expand, r0);
499 if (sc_expand != cell_undefined && sc_expand != cell_f)
501 r2 = cons (r1, cell_nil);
510 r = cell_unspecified;
511 while (r1 != cell_nil) {
512 if (TYPE (r1) == PAIR && TYPE (CAR (r1)) == PAIR)
514 if (caar (r1) == cell_symbol_begin)
515 r1 = append2 (cdar (r1), cdr (r1));
516 else if (caar (r1) == cell_symbol_primitive_load)
518 SCM f = read_input_file_env (r0);
519 r1 = append2 (f, cdr (r1));
522 if (CDR (r1) == cell_nil)
527 r = eval_env (car (r1), r0);
534 x = eval_env (car (r1), r0);
540 if (cddr (r1) != cell_nil)
545 return cell_unspecified;
549 v = apply_env (r1, cell_nil, r0);
550 if (TYPE (v) == VALUES)
560 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
561 && x != cell_nil && TYPE (CAR (x)) == VALUES)
562 x = cons (CADAR (x), CDR (x));
563 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
564 && x != cell_nil && TYPE (CDR (x)) == PAIR && TYPE (CADR (x)) == VALUES)
565 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
566 switch (FUNCTION (fn).arity)
568 case 0: return FUNCTION (fn).function0 ();
569 case 1: return FUNCTION (fn).function1 (car (x));
570 case 2: return FUNCTION (fn).function2 (car (x), cadr (x));
571 case 3: return FUNCTION (fn).function3 (car (x), cadr (x), caddr (x));
572 case -1: return FUNCTION (fn).functionn (x);
574 return cell_unspecified;
580 SCM frame = car (stack);
591 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
592 stack = cons (frame, stack);
600 vm_call (function0_t f, SCM p1, SCM p2, SCM a)
602 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
603 stack = cons (frame, stack);
607 if (g_free.value + GC_SAFETY > ARENA_SIZE)
611 frame = gc_frame (stack);
617 evlis_env (SCM m, SCM a)
620 return vm_call (eval_apply, m, cell_undefined, a);
624 apply_env (SCM fn, SCM x, SCM a)
627 return vm_call (eval_apply, fn, x, a);
631 eval_env (SCM e, SCM a)
634 return vm_call (eval_apply, e, cell_undefined, a);
638 macro_expand_env (SCM e, SCM a)
640 g_target = MACRO_EXPAND;
641 return vm_call (eval_apply, e, cell_undefined, a);
645 begin_env (SCM e, SCM a)
648 return vm_call (eval_apply, e, cell_undefined, a);
652 if_env (SCM e, SCM a)
655 return vm_call (eval_apply, e, cell_undefined, a);
659 call_with_values_env (SCM producer, SCM consumer, SCM a)
661 g_target = CALL_WITH_VALUES;
662 return vm_call (eval_apply, producer, consumer, a);
666 append2 (SCM x, SCM y)
668 if (x == cell_nil) return y;
669 assert (TYPE (x) == PAIR);
670 return cons (car (x), append2 (cdr (x), y));
674 append (SCM x) ///((arity . n))
676 if (x == cell_nil) return cell_nil;
677 if (cdr (x) == cell_nil) return car (x);
678 return append2 (car (x), append (cdr (x)));
684 g_cells[tmp_num].value = x;
691 g_cells[tmp_num2].value = x;
698 g_cells[tmp_num].value = REF;
699 return make_cell (tmp_num, x, x);
705 g_cells[tmp_num].value = STRING;
706 return make_cell (tmp_num, x, 0);
710 cstring_to_list (char const* s)
715 p = cons (MAKE_CHAR (s[i]), p);
722 return x == cell_nil ? cell_t : cell_f;
728 g_cells[tmp_num].value = SYMBOL;
729 SCM x = make_cell (tmp_num, s, 0);
730 g_symbols = cons (x, g_symbols);
737 SCM x = lookup_symbol_ (s);
738 return x ? x : make_symbol_ (s);
745 g_cells[tmp_num].value = VECTOR;
747 SCM x = make_cell (tmp_num, k, v);
748 for (int i=0; i<k; i++) g_cells[v+i] = g_cells[vector_entry (cell_unspecified)];
753 values (SCM x) ///((arity . n))
761 vector_length (SCM x)
763 assert (TYPE (x) == VECTOR);
764 return MAKE_NUMBER (LENGTH (x));
768 vector_ref (SCM x, SCM i)
770 assert (TYPE (x) == VECTOR);
771 assert (VALUE (i) < LENGTH (x));
772 SCM e = VECTOR (x) + VALUE (i);
773 if (TYPE (e) == REF) e = g_cells[e].ref;
774 if (TYPE (e) == CHAR) e = MAKE_CHAR (VALUE (e));
775 if (TYPE (e) == NUMBER) e = MAKE_NUMBER (VALUE (e));
780 vector_entry (SCM x) {
781 if (TYPE (x) == PAIR || TYPE (x) == SPECIAL || TYPE (x) == STRING || TYPE (x) == SYMBOL || TYPE (x) == VECTOR) x = make_ref (x);
786 vector_set_x (SCM x, SCM i, SCM e)
788 assert (TYPE (x) == VECTOR);
789 assert (VALUE (i) < LENGTH (x));
790 g_cells[VECTOR (x)+g_cells[i].value] = g_cells[vector_entry (e)];
791 return cell_unspecified;
795 list_to_vector (SCM x)
797 VALUE (tmp_num) = VALUE (length (x));
798 SCM v = make_vector (tmp_num);
800 while (x != cell_nil)
802 g_cells[p++] = g_cells[vector_entry (car (x))];
812 return getc (g_stdin);
818 return ungetc (c, g_stdin);
832 return MAKE_NUMBER (peekchar ());
838 return MAKE_NUMBER (getchar ());
844 ungetchar (VALUE (i));
849 write_char (SCM x) ///((arity . n))
854 if (TYPE (p) == PAIR && TYPE (car (p)) == NUMBER) fd = VALUE (car (p));
855 FILE *f = fd == 1 ? stdout : stderr;
856 assert (TYPE (c) == NUMBER || TYPE (c) == CHAR);
857 fputc (VALUE (c), f);
862 symbol_to_list (SCM x)
864 assert (TYPE (x) == SYMBOL);
869 char_to_integer (SCM x)
871 assert (TYPE (x) == CHAR);
872 return MAKE_NUMBER (VALUE (x));
876 integer_to_char (SCM x)
878 assert (TYPE (x) == NUMBER);
879 return MAKE_CHAR (VALUE (x));
883 make_tmps (scm* cells)
885 tmp = g_free.value++;
886 cells[tmp].type = CHAR;
887 tmp_num = g_free.value++;
888 cells[tmp_num].type = NUMBER;
889 tmp_num2 = g_free.value++;
890 cells[tmp_num2].type = NUMBER;
891 tmp_num3 = g_free.value++;
892 cells[tmp_num3].type = NUMBER;
893 tmp_num4 = g_free.value++;
894 cells[tmp_num4].type = NUMBER;
899 bool g_debug = false;
905 void *p = realloc (g_cells-1, 2*ARENA_SIZE*sizeof(scm));
908 if (g_debug) fprintf (stderr, "cannot up arena: %s: arena=%d\n", strerror (errno), 2*ARENA_SIZE);
909 return cell_unspecified;
919 if (g_debug) fprintf (stderr, "***gc[%d]...", g_free.value);
921 if (g_cells < g_news && ARENA_SIZE < MAX_ARENA_SIZE) gc_up_arena ();
922 for (int i=g_free.value; i<g_symbol_max; i++)
925 g_symbols = gc_copy (g_symbols);
926 SCM new = gc_copy (stack);
927 if (g_debug) fprintf (stderr, "new=%d\n", new, stack);
935 while (scan < g_free.value)
937 if (NTYPE (scan) == KEYWORD
938 || NTYPE (scan) == MACRO
939 || NTYPE (scan) == PAIR
940 || NTYPE (scan) == REF
942 || NTYPE (scan) == SPECIAL
943 || NTYPE (scan) == STRING
944 || NTYPE (scan) == SYMBOL)
946 SCM car = gc_copy (g_news[scan].car);
947 gc_relocate_car (scan, car);
949 if ((NTYPE (scan) == MACRO
950 || NTYPE (scan) == PAIR
951 || NTYPE (scan) == VALUES)
952 && g_news[scan].cdr) // allow for 0 terminated list of symbols
954 SCM cdr = gc_copy (g_news[scan].cdr);
955 gc_relocate_cdr (scan, cdr);
965 if (TYPE (old) == BROKEN_HEART) return g_cells[old].car;
966 SCM new = g_free.value++;
967 g_news[new] = g_cells[old];
968 if (NTYPE (new) == VECTOR)
970 g_news[new].vector = g_free.value;
971 for (int i=0; i<LENGTH (old); i++)
972 g_news[g_free.value++] = g_cells[VECTOR (old)+i];
974 g_cells[old].type = BROKEN_HEART;
975 g_cells[old].car = new;
980 gc_relocate_car (SCM new, SCM car)
982 g_news[new].car = car;
983 return cell_unspecified;
987 gc_relocate_cdr (SCM new, SCM cdr)
989 g_news[new].cdr = cdr;
990 return cell_unspecified;
996 scm *cells = g_cells;
999 if (g_debug) fprintf (stderr, " => jam[%d]\n", g_free.value);
1006 fprintf (stderr, "cells: ");
1008 display_ (stderr, -1);
1009 fprintf (stderr, "\n");
1012 fprintf (stderr, "news: ");
1014 display_ (stderr, -1);
1015 fprintf (stderr, "\n");
1018 return cell_unspecified;
1021 //
\f Environment setup
1023 acons (SCM key, SCM value, SCM alist)
1025 return cons (cons (key, value), alist);
1029 add_environment (SCM a, char const *name, SCM x)
1031 return acons (make_symbol (cstring_to_list (name)), x, a);
1037 g_cells = (scm *)malloc (2*ARENA_SIZE*sizeof(scm));
1038 g_cells[0].type = VECTOR;
1039 g_cells[0].length = 1000;
1040 g_cells[0].vector = 0;
1042 g_cells[0].type = CHAR;
1043 g_cells[0].value = 'c';
1049 g_news = g_cells-1 + ARENA_SIZE;
1050 g_news[0].type = VECTOR;
1051 g_news[0].length = 1000;
1052 g_news[0].vector = 0;
1054 g_news[0].type = CHAR;
1055 g_news[0].value = 'n';
1059 mes_symbols () ///((internal))
1064 #include "mes.symbols.i"
1066 g_symbol_max = g_free.value;
1067 make_tmps (g_cells);
1070 for (int i=1; i<g_symbol_max; i++)
1071 g_symbols = cons (i, g_symbols);
1075 #include "mes.symbol-names.i"
1078 a = acons (cell_symbol_label, cell_t, a);
1080 a = acons (cell_symbol_begin, cell_begin, a);
1081 a = add_environment (a, "sc-expand", cell_f);
1082 a = acons (cell_closure, a, a);
1088 mes_builtins (SCM a)
1092 #include "display.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 "reader.environment.i"
1106 #include "string.environment.i"
1107 #include "type.environment.i"
1109 a = add_environment (a, "*dot*", cell_dot);
1110 a = add_environment (a, "*foo-bar-baz*", cell_nil); // FIXME: some off-by one?
1116 mes_stack (SCM a) ///((internal))
1122 stack = cons (cell_nil, cell_nil);
1127 mes_environment () ///((internal))
1129 SCM a = mes_symbols ();
1130 return mes_stack (a);
1134 make_lambda (SCM args, SCM body)
1136 return cons (cell_symbol_lambda, cons (args, body));
1140 make_closure (SCM args, SCM body, SCM a)
1142 return cons (cell_closure, cons (cons (cell_circular, a), cons (args, body)));
1146 lookup_macro (SCM x, SCM a)
1148 if (TYPE (x) != SYMBOL) return cell_f;
1149 SCM m = assq_ref_cache (x, a);
1150 if (macro_p (m) == cell_t) return MACRO (m);
1155 read_input_file_env_ (SCM e, SCM a)
1157 if (e == cell_nil) return e;
1158 return cons (e, read_input_file_env_ (read_env (a), a));
1162 read_input_file_env (SCM a)
1165 if (assq_ref_cache (cell_symbol_read_input_file, r0) != cell_undefined)
1166 return apply_env (cell_symbol_read_input_file, cell_nil, r0);
1167 return read_input_file_env_ (read_env (r0), r0);
1171 load_env (SCM a) ///((internal))
1174 g_stdin = fopen ("module/mes/read-0.mes", "r");
1175 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mes", "r");
1176 if (!g_function) r0 = mes_builtins (r0);
1177 r3 = read_input_file_env (r0);
1183 bload_env (SCM a) ///((internal))
1185 g_stdin = fopen ("module/mes/read-0.mo", "r");
1186 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
1187 char *p = (char*)g_cells;
1188 assert (getchar () == 'M');
1189 assert (getchar () == 'E');
1190 assert (getchar () == 'S');
1191 stack = getchar () << 8;
1192 stack += getchar ();
1199 g_free.value = (p-(char*)g_cells) / sizeof (scm);
1204 r0 = mes_builtins (r0);
1212 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
1213 stack = cons (frame, stack);
1216 char *p = (char*)g_cells;
1217 fputc ('M', stdout);
1218 fputc ('E', stdout);
1219 fputc ('S', stdout);
1220 fputc (stack >> 8, stdout);
1221 fputc (stack % 256, stdout);
1222 for (int i=0; i<g_free.value * sizeof(scm); i++)
1223 fputc (*p++, stdout);
1228 #include "display.c"
1236 main (int argc, char *argv[])
1238 g_debug = getenv ("MES_DEBUG");
1239 if (getenv ("MES_ARENA")) ARENA_SIZE = atoi (getenv ("MES_ARENA"));
1240 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
1241 if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.3\n");
1243 r0 = mes_environment ();
1244 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1245 ? bload_env (r0) : load_env (r0);
1246 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1247 display_ (stderr, begin_env (program, r0));
1250 if (g_debug) fprintf (stderr, "\nstats: [%d]\n", g_free.value);