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, KEYWORD, 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_arrow = {SPECIAL, "=>"};
88 scm scm_undefined = {SPECIAL, "*undefined*"};
89 scm scm_unspecified = {SPECIAL, "*unspecified*"};
90 scm scm_closure = {SPECIAL, "*closure*"};
91 scm scm_circular = {SPECIAL, "*circular*"};
96 scm scm_begin = {SPECIAL, "*begin*"};
98 scm scm_symbol_lambda = {SYMBOL, "lambda"};
99 scm scm_symbol_begin = {SYMBOL, "begin"};
100 scm scm_symbol_if = {SYMBOL, "if"};
101 scm scm_symbol_define = {SYMBOL, "define"};
102 scm scm_symbol_define_macro = {SYMBOL, "define-macro"};
103 scm scm_symbol_set_x = {SYMBOL, "set!"};
105 scm scm_symbol_quote = {SYMBOL, "quote"};
106 scm scm_symbol_quasiquote = {SYMBOL, "quasiquote"};
107 scm scm_symbol_unquote = {SYMBOL, "unquote"};
108 scm scm_symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
110 scm scm_symbol_sc_expand = {SYMBOL, "sc-expand"};
111 scm scm_symbol_expand_macro = {SYMBOL, "expand-macro"};
112 scm scm_symbol_sc_expander_alist = {SYMBOL, "*sc-expander-alist*"};
113 scm scm_symbol_noexpand = {SYMBOL, "noexpand"};
114 scm scm_symbol_syntax = {SYMBOL, "syntax"};
115 scm scm_symbol_quasisyntax = {SYMBOL, "quasisyntax"};
116 scm scm_symbol_unsyntax = {SYMBOL, "unsyntax"};
117 scm scm_symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
119 scm scm_symbol_call_with_values = {SYMBOL, "call-with-values"};
120 scm scm_symbol_current_module = {SYMBOL, "current-module"};
121 scm scm_symbol_primitive_load = {SYMBOL, "primitive-load"};
122 scm scm_symbol_read_input_file = {SYMBOL, "read-input-file"};
124 scm scm_symbol_the_unquoters = {SYMBOL, "*the-unquoters*"};
125 scm scm_symbol_the_unsyntaxers = {SYMBOL, "*the-unsyntaxers*"};
127 scm scm_symbol_car = {SYMBOL, "car"};
128 scm scm_symbol_cdr = {SYMBOL, "cdr"};
129 scm scm_symbol_null_p = {SYMBOL, "null?"};
130 scm scm_symbol_eq_p = {SYMBOL, "eq?"};
131 scm scm_symbol_cons = {SYMBOL, "cons"};
133 scm char_eof = {CHAR, .name="*eof*", .value=-1};
134 scm char_nul = {CHAR, .name="nul", .value=0};
135 scm char_alarm = {CHAR, .name="alarm", .value=8};
136 scm char_backspace = {CHAR, .name="backspace", .value=8};
137 scm char_tab = {CHAR, .name="tab", .value=9};
138 scm char_newline = {CHAR, .name="newline", .value=10};
139 scm char_vtab = {CHAR, .name="vtab", .value=11};
140 scm char_page = {CHAR, .name="page", .value=12};
141 scm char_return = {CHAR, .name="return", .value=13};
142 scm char_space = {CHAR, .name="space", .value=32};
144 scm g_free = {NUMBER, .value=0};
148 #include "mes.symbols.h"
156 function functions[200];
162 SCM r1 = 0; // param 1
163 SCM r2 = 0; // param 2
164 SCM r3 = 0; // param 3
172 #include "quasiquote.h"
177 #define CAR(x) g_cells[x].car
178 #define CDR(x) g_cells[x].cdr
179 #define HITS(x) g_cells[x].hits
180 #define LENGTH(x) g_cells[x].length
181 #define NAME(x) g_cells[x].name
182 #define STRING(x) g_cells[x].string
183 #define TYPE(x) g_cells[x].type
184 #define MACRO(x) g_cells[x].macro
185 #define REF(x) g_cells[x].ref
186 #define VALUE(x) g_cells[x].value
187 #define VECTOR(x) g_cells[x].vector
188 #define FUNCTION(x) functions[g_cells[x].function]
189 #define NCAR(x) g_news[x].car
190 #define NTYPE(x) g_news[x].type
192 #define CAAR(x) CAR (CAR (x))
193 #define CDAR(x) CDR (CAR (x))
194 #define CAAR(x) CAR (CAR (x))
195 #define CADAR(x) CAR (CDR (CAR (x)))
196 #define CADDR(x) CAR (CDR (CDR (x)))
197 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
198 #define CADR(x) CAR (CDR (x))
200 SCM display_ (FILE* f, SCM x);
201 SCM vm_call (function0_t f, SCM p1, SCM p2, SCM a);
206 assert (g_free.value + n < ARENA_SIZE);
207 SCM x = g_free.value;
213 make_cell (SCM type, SCM car, SCM cdr)
216 assert (TYPE (type) == NUMBER);
217 TYPE (x) = VALUE (type);
218 if (VALUE (type) == CHAR || VALUE (type) == NUMBER) {
219 if (car) CAR (x) = CAR (car);
220 if (cdr) CDR (x) = CDR (cdr);
221 } else if (VALUE (type) == FUNCTION) {
222 if (car) CAR (x) = car;
223 if (cdr) CDR (x) = CDR (cdr);
234 g_cells[tmp_num].value = PAIR;
235 return make_cell (tmp_num, x, y);
241 assert (TYPE (x) == PAIR);
248 assert (TYPE (x) == PAIR);
256 || ((TYPE (x) == KEYWORD && TYPE (y) == KEYWORD
257 && STRING (x) == STRING (y)))
258 || (TYPE (x) == CHAR && TYPE (y) == CHAR
259 && VALUE (x) == VALUE (y))
260 || (TYPE (x) == NUMBER && TYPE (y) == NUMBER
261 && VALUE (x) == VALUE (y)))
266 set_car_x (SCM x, SCM e)
268 assert (TYPE (x) == PAIR);
270 return cell_unspecified;
274 set_cdr_x (SCM x, SCM e)
276 assert (TYPE (x) == PAIR);
278 return cell_unspecified;
282 set_env_x (SCM x, SCM e, SCM a)
284 SCM p = assert_defined (x, assq (x, a));
285 return set_cdr_x (p, e);
291 return cons (cell_symbol_quote, x);
297 return cons (cell_symbol_quasiquote, x);
303 return cons (cell_symbol_quasisyntax, x);
307 pairlis (SCM x, SCM y, SCM a)
311 if (pair_p (x) == cell_f)
312 return cons (cons (x, y), a);
313 return cons (cons (car (x), car (y)),
314 pairlis (cdr (x), cdr (y), a));
320 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f)
322 if (TYPE (a) == BROKEN_HEART || TYPE (CAR (a)) == BROKEN_HEART)
323 fprintf (stderr, "oops, broken heart\n");
326 return a != cell_nil ? car (a) : cell_f;
330 assq_ref_cache (SCM x, SCM a)
333 if (x == cell_f) return cell_undefined;
338 assert_defined (SCM x, SCM e)
340 if (e == cell_undefined)
342 fprintf (stderr, "eval: unbound variable:");
343 display_ (stderr, x);
344 fprintf (stderr, "\n");
345 assert (!"unbound variable");
353 if (r1 == cell_nil) return cell_nil;
354 if (TYPE (r1) != PAIR) return eval_env (r1, r0);
355 r2 = eval_env (car (r1), r0);
356 r1 = evlis_env (cdr (r1), r0);
357 return cons (r2, r1);
363 return vm_call (vm_begin_env, r1, cell_undefined, r0);
367 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
369 SCM cl = cons (cons (cell_closure, x), x);
374 return vm_call_lambda ();
380 if (TYPE (r1) != PAIR)
382 if (TYPE (r1) == FUNCTION) return call (r1, r2);
383 if (r1 == cell_symbol_call_with_values)
384 return call_with_values_env (car (r2), cadr (r2), r0);
385 if (r1 == cell_symbol_current_module) return r0;
390 case cell_symbol_lambda:
392 SCM args = cadr (r1);
393 SCM body = cddr (r1);
394 SCM p = pairlis (args, r2, r0);
395 return call_lambda (body, p, p, r0);
399 SCM args = caddr (r1);
400 SCM body = cdddr (r1);
403 SCM p = pairlis (args, r2, aa);
404 return call_lambda (body, p, aa, r0);
407 case cell_symbol_label:
408 return apply_env (caddr (r1), r2, cons (cons (cadr (r1), caddr (r1)), r0));
411 SCM e = eval_env (r1, r0);
412 char const* type = 0;
413 if (e == cell_f || e == cell_t) type = "bool";
414 if (TYPE (e) == CHAR) type = "char";
415 if (TYPE (e) == NUMBER) type = "number";
416 if (TYPE (e) == STRING) type = "string";
417 if (e == cell_unspecified) type = "*unspecified*";
418 if (e == cell_undefined) type = "*undefined*";
421 fprintf (stderr, "cannot apply: %s: ", type);
422 display_ (stderr, e);
423 fprintf (stderr, " [");
424 display_ (stderr, r1);
425 fprintf (stderr, "]\n");
426 assert (!"cannot apply");
428 return apply_env (e, r2, r0);
441 case cell_symbol_car: return car (eval_env (CADR (r1), r0));
442 case cell_symbol_cdr: return cdr (eval_env (CADR (r1), r0));
443 case cell_symbol_cons: {SCM m = evlis_env (CDR (r1), r0);
444 return cons (CAR (m), CADR (m));}
445 case cell_symbol_null_p: return null_p (eval_env (CADR (r1), r0));
446 #endif // FIXED_PRIMITIVES
447 case cell_symbol_quote: return cadr (r1);
449 case cell_symbol_syntax: return cadr (r1);
451 case cell_symbol_begin: return begin_env (r1, r0);
452 case cell_symbol_lambda:
453 return make_closure (cadr (r1), cddr (r1), assq (cell_closure, r0));
454 case cell_closure: return r1;
455 case cell_symbol_if: return if_env (cdr (r1), r0);
457 case cell_symbol_define: return define_env (r1, r0);
458 case cell_symbol_define_macro: return define_env (r1, r0);
461 case cell_symbol_set_x: {
462 SCM x = eval_env (caddr (r1), r0); return set_env_x (cadr (r1), x, r0);
466 case cell_symbol_unquote: return eval_env (cadr (r1), r0);
467 case cell_symbol_quasiquote: return eval_quasiquote (cadr (r1), add_unquoters (r0));
470 case cell_symbol_unsyntax: return eval_env (cadr (r1), r0);
471 case cell_symbol_quasisyntax: return eval_quasisyntax (cadr (r1), add_unsyntaxers (r0));
474 SCM x = expand_macro_env (r1, r0);
475 if (x != r1) return eval_env (x, r0);
476 SCM m = evlis_env (CDR (r1), r0);
477 return apply_env (car (r1), m, r0);
481 case SYMBOL: return assert_defined (r1, assq_ref_cache (r1, r0));
487 vm_expand_macro_env ()
489 if (TYPE (CAR (r1)) == STRING && string_to_symbol (CAR (r1)) == cell_symbol_noexpand)
494 if (TYPE (r1) == PAIR
495 && (macro = lookup_macro (car (r1), r0)) != cell_f)
496 return apply_env (macro, CDR (r1), r0);
497 else if (TYPE (r1) == PAIR
498 && TYPE (CAR (r1)) == SYMBOL
499 && ((expanders = assq_ref_cache (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
500 && ((macro = assq (CAR (r1), expanders)) != cell_f))
502 SCM sc_expand = assq_ref_cache (cell_symbol_expand_macro, r0);
503 if (sc_expand != cell_undefined && sc_expand != cell_f)
504 r1 = apply_env (sc_expand, cons (r1, cell_nil), r0);
512 SCM r = cell_unspecified;
513 while (r1 != cell_nil) {
514 if (TYPE (r1) == PAIR && TYPE (CAR (r1)) == PAIR)
516 if (caar (r1) == cell_symbol_begin)
517 r1 = append2 (cdar (r1), cdr (r1));
518 else if (caar (r1) == cell_symbol_primitive_load)
520 SCM f = read_input_file_env (r0);
521 r1 = append2 (f, cdr (r1));
524 r = eval_env (car (r1), r0);
533 SCM x = eval_env (car (r1), r0);
535 return eval_env (cadr (r1), r0);
536 if (cddr (r1) != cell_nil)
537 return eval_env (caddr (r1), r0);
538 return cell_unspecified;
542 vm_call_with_values_env ()
544 SCM v = apply_env (r1, cell_nil, r0);
545 if (TYPE (v) == VALUES)
547 return apply_env (r2, v, r0);
553 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
554 && x != cell_nil && TYPE (CAR (x)) == VALUES)
555 x = cons (CADAR (x), CDR (x));
556 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
557 && x != cell_nil && TYPE (CDR (x)) == PAIR && TYPE (CADR (x)) == VALUES)
558 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
559 switch (FUNCTION (fn).arity)
561 case 0: return FUNCTION (fn).function0 ();
562 case 1: return FUNCTION (fn).function1 (car (x));
563 case 2: return FUNCTION (fn).function2 (car (x), cadr (x));
564 case 3: return FUNCTION (fn).function3 (car (x), cadr (x), caddr (x));
565 case -1: return FUNCTION (fn).functionn (x);
567 return cell_unspecified;
573 SCM frame = car (stack);
584 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
585 stack = cons (frame, stack);
593 vm_call (function0_t f, SCM p1, SCM p2, SCM a)
595 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
596 stack = cons (frame, stack);
600 if (g_free.value + GC_SAFETY > ARENA_SIZE)
604 frame = gc_frame (stack);
610 evlis_env (SCM m, SCM a)
612 return vm_call (vm_evlis_env, m, cell_undefined, a);
616 apply_env (SCM fn, SCM x, SCM a)
618 return vm_call (vm_apply_env, fn, x, a);
622 eval_env (SCM e, SCM a)
624 return vm_call (vm_eval_env, e, cell_undefined, a);
628 expand_macro_env (SCM e, SCM a)
630 return vm_call (vm_expand_macro_env, e, cell_undefined, a);
634 begin_env (SCM e, SCM a)
636 return vm_call (vm_begin_env, e, cell_undefined, a);
640 if_env (SCM e, SCM a)
642 return vm_call (vm_if_env, e, cell_undefined, a);
646 call_with_values_env (SCM producer, SCM consumer, SCM a)
648 return vm_call (vm_call_with_values_env, producer, consumer, a);
652 append2 (SCM x, SCM y)
654 if (x == cell_nil) return y;
655 assert (TYPE (x) == PAIR);
656 return cons (car (x), append2 (cdr (x), y));
660 append (SCM x) ///((arity . n))
662 if (x == cell_nil) return cell_nil;
663 return append2 (car (x), append (cdr (x)));
669 g_cells[tmp_num].value = CHAR;
670 g_cells[tmp_num2].value = x;
671 return make_cell (tmp_num, tmp_num2, tmp_num2);
675 make_function (SCM name, SCM id, SCM arity)
677 g_cells[tmp_num3].value = FUNCTION;
678 function *f = (function*)malloc (sizeof (function));
679 f->arity = VALUE (arity);
680 g_cells[tmp_num4].value = (long)f;
681 return make_cell (tmp_num3, name, tmp_num4);
687 SCM x = internal_lookup_symbol (s);
688 x = x ? x : internal_make_symbol (s);
689 g_cells[tmp_num].value = KEYWORD;
690 return make_cell (tmp_num, STRING (x), 0);
694 make_macro (SCM name, SCM x)
696 g_cells[tmp_num].value = MACRO;
697 return make_cell (tmp_num, STRING (name), x);
703 g_cells[tmp_num].value = NUMBER;
704 g_cells[tmp_num2].value = x;
705 return make_cell (tmp_num, tmp_num2, tmp_num2);
711 g_cells[tmp_num].value = REF;
712 return make_cell (tmp_num, x, x);
718 g_cells[tmp_num].value = STRING;
719 return make_cell (tmp_num, x, 0);
723 cstring_to_list (char const* s)
728 p = cons (make_char (s[i]), p);
735 return x == cell_nil ? cell_t : cell_f;
739 internal_make_symbol (SCM s)
741 g_cells[tmp_num].value = SYMBOL;
742 SCM x = make_cell (tmp_num, s, 0);
743 g_symbols = cons (x, g_symbols);
750 SCM x = internal_lookup_symbol (s);
751 return x ? x : internal_make_symbol (s);
758 g_cells[tmp_num].value = VECTOR;
760 SCM x = make_cell (tmp_num, k, v);
761 for (int i=0; i<k; i++) g_cells[v+i] = g_cells[vector_entry (cell_unspecified)];
766 values (SCM x) ///((arity . n))
774 vector_length (SCM x)
776 assert (TYPE (x) == VECTOR);
777 return make_number (LENGTH (x));
781 vector_ref (SCM x, SCM i)
783 assert (TYPE (x) == VECTOR);
784 assert (VALUE (i) < LENGTH (x));
785 SCM e = VECTOR (x) + VALUE (i);
786 if (TYPE (e) == REF) e = g_cells[e].ref;
787 if (TYPE (e) == CHAR) e = make_char (VALUE (e));
788 if (TYPE (e) == NUMBER) e = make_number (VALUE (e));
793 vector_entry (SCM x) {
794 if (TYPE (x) == PAIR || TYPE (x) == SPECIAL || TYPE (x) == STRING || TYPE (x) == SYMBOL || TYPE (x) == VECTOR) x = make_ref (x);
799 vector_set_x (SCM x, SCM i, SCM e)
801 assert (TYPE (x) == VECTOR);
802 assert (VALUE (i) < LENGTH (x));
803 g_cells[VECTOR (x)+g_cells[i].value] = g_cells[vector_entry (e)];
804 return cell_unspecified;
808 list_to_vector (SCM x)
810 VALUE (tmp_num) = VALUE (length (x));
811 SCM v = make_vector (tmp_num);
813 while (x != cell_nil)
815 g_cells[p++] = g_cells[vector_entry (car (x))];
825 return getc (g_stdin);
831 return ungetc (c, g_stdin);
845 return make_number (peekchar ());
851 return make_number (getchar ());
857 ungetchar (VALUE (i));
862 write_char (SCM x) ///((arity . n))
867 if (TYPE (p) == PAIR && TYPE (car (p)) == NUMBER) fd = VALUE (car (p));
868 FILE *f = fd == 1 ? stdout : stderr;
869 assert (TYPE (c) == NUMBER || TYPE (c) == CHAR);
870 fputc (VALUE (c), f);
875 symbol_to_list (SCM x)
877 assert (TYPE (x) == SYMBOL);
882 char_to_integer (SCM x)
884 assert (TYPE (x) == CHAR);
885 return make_number (VALUE (x));
889 integer_to_char (SCM x)
891 assert (TYPE (x) == NUMBER);
892 return make_char (VALUE (x));
896 make_tmps (scm* cells)
898 tmp = g_free.value++;
899 cells[tmp].type = CHAR;
900 tmp_num = g_free.value++;
901 cells[tmp_num].type = NUMBER;
902 tmp_num2 = g_free.value++;
903 cells[tmp_num2].type = NUMBER;
904 tmp_num3 = g_free.value++;
905 cells[tmp_num3].type = NUMBER;
906 tmp_num4 = g_free.value++;
907 cells[tmp_num4].type = NUMBER;
912 bool g_debug = false;
918 void *p = realloc (g_cells-1, 2*ARENA_SIZE*sizeof(scm));
921 if (g_debug) fprintf (stderr, "cannot up arena: %s: arena=%d\n", strerror (errno), 2*ARENA_SIZE);
922 return cell_unspecified;
932 if (g_debug) fprintf (stderr, "***gc[%d]...", g_free.value);
934 if (g_cells < g_news && ARENA_SIZE < MAX_ARENA_SIZE) gc_up_arena ();
935 for (int i=g_free.value; i<g_symbol_max; i++)
938 g_symbols = gc_copy (g_symbols);
939 SCM new = gc_copy (stack);
940 if (g_debug) fprintf (stderr, "new=%d\n", new, stack);
948 while (scan < g_free.value)
950 if (NTYPE (scan) == KEYWORD
951 || NTYPE (scan) == MACRO
952 || NTYPE (scan) == PAIR
953 || NTYPE (scan) == REF
955 || NTYPE (scan) == SPECIAL
956 || NTYPE (scan) == STRING
957 || NTYPE (scan) == SYMBOL)
959 SCM car = gc_copy (g_news[scan].car);
960 gc_relocate_car (scan, car);
962 if ((NTYPE (scan) == MACRO
963 || NTYPE (scan) == PAIR
964 || NTYPE (scan) == VALUES)
965 && g_news[scan].cdr) // allow for 0 terminated list of symbols
967 SCM cdr = gc_copy (g_news[scan].cdr);
968 gc_relocate_cdr (scan, cdr);
978 if (TYPE (old) == BROKEN_HEART) return g_cells[old].car;
979 SCM new = g_free.value++;
980 g_news[new] = g_cells[old];
981 if (NTYPE (new) == VECTOR)
983 g_news[new].vector = g_free.value;
984 for (int i=0; i<LENGTH (old); i++)
985 g_news[g_free.value++] = g_cells[VECTOR (old)+i];
987 g_cells[old].type = BROKEN_HEART;
988 g_cells[old].car = new;
993 gc_relocate_car (SCM new, SCM car)
995 g_news[new].car = car;
996 return cell_unspecified;
1000 gc_relocate_cdr (SCM new, SCM cdr)
1002 g_news[new].cdr = cdr;
1003 return cell_unspecified;
1009 scm *cells = g_cells;
1012 if (g_debug) fprintf (stderr, " => jam[%d]\n", g_free.value);
1019 fprintf (stderr, "cells: ");
1021 display_ (stderr, -1);
1022 fprintf (stderr, "\n");
1025 fprintf (stderr, "news: ");
1027 display_ (stderr, -1);
1028 fprintf (stderr, "\n");
1031 return cell_unspecified;
1034 //
\f Environment setup
1036 acons (SCM key, SCM value, SCM alist)
1038 return cons (cons (key, value), alist);
1042 add_environment (SCM a, char const *name, SCM x)
1044 return acons (make_symbol (cstring_to_list (name)), x, a);
1050 g_cells = (scm *)malloc (2*ARENA_SIZE*sizeof(scm));
1051 g_cells[0].type = VECTOR;
1052 g_cells[0].length = 1000;
1053 g_cells[0].vector = 0;
1055 g_cells[0].type = CHAR;
1056 g_cells[0].value = 'c';
1062 g_news = g_cells-1 + ARENA_SIZE;
1063 g_news[0].type = VECTOR;
1064 g_news[0].length = 1000;
1065 g_news[0].vector = 0;
1067 g_news[0].type = CHAR;
1068 g_news[0].value = 'n';
1072 mes_symbols () ///((internal))
1077 #include "mes.symbols.i"
1079 g_symbol_max = g_free.value;
1080 make_tmps (g_cells);
1083 for (int i=1; i<g_symbol_max; i++)
1084 g_symbols = cons (i, g_symbols);
1089 a = acons (cell_symbol_label, cell_t, a);
1091 a = acons (cell_symbol_begin, cell_begin, a);
1092 a = add_environment (a, "sc-expand", cell_f);
1093 a = acons (cell_closure, a, a);
1095 internal_lookup_symbol (cell_nil);
1101 mes_builtins (SCM a)
1106 #include "display.i"
1110 #include "quasiquote.i"
1115 #include "define.environment.i"
1116 #include "display.environment.i"
1117 #include "lib.environment.i"
1118 #include "math.environment.i"
1119 #include "mes.environment.i"
1120 #include "posix.environment.i"
1121 //#include "quasiquote.environment.i"
1122 #include "reader.environment.i"
1123 #include "string.environment.i"
1124 #include "type.environment.i"
1127 SCM cell_unquote = assq_ref_cache (cell_symbol_unquote, a);
1128 SCM cell_unquote_splicing = assq_ref_cache (cell_symbol_unquote_splicing, a);
1129 SCM the_unquoters = cons (cons (cell_symbol_unquote, cell_unquote),
1130 cons (cons (cell_symbol_unquote_splicing, cell_unquote_splicing),
1132 a = acons (cell_symbol_the_unquoters, the_unquoters, a);
1135 SCM cell_unsyntax = assq_ref_cache (cell_symbol_unsyntax, a);
1136 SCM cell_unsyntax_splicing = assq_ref_cache (cell_symbol_unsyntax_splicing, a);
1137 SCM the_unsyntaxers = cons (cons (cell_symbol_unsyntax, cell_unsyntax),
1138 cons (cons (cell_symbol_unsyntax_splicing, cell_unsyntax_splicing),
1140 a = acons (cell_symbol_the_unsyntaxers, the_unsyntaxers, a);
1143 a = add_environment (a, "*dot*", cell_dot);
1144 a = add_environment (a, "*foo-bar-baz*", cell_nil); // FIXME: some off-by one?
1150 mes_stack (SCM a) ///((internal))
1156 stack = cons (cell_nil, cell_nil);
1161 mes_environment () ///((internal))
1163 SCM a = mes_symbols ();
1164 return mes_stack (a);
1168 make_lambda (SCM args, SCM body)
1170 return cons (cell_symbol_lambda, cons (args, body));
1174 make_closure (SCM args, SCM body, SCM a)
1176 return cons (cell_closure, cons (cons (cell_circular, a), cons (args, body)));
1180 lookup_macro (SCM x, SCM a)
1182 if (TYPE (x) != SYMBOL) return cell_f;
1183 SCM m = assq_ref_cache (x, a);
1184 if (macro_p (m) == cell_t) return MACRO (m);
1189 read_input_file_env_ (SCM e, SCM a)
1191 if (e == cell_nil) return e;
1192 return cons (e, read_input_file_env_ (read_env (a), a));
1196 read_input_file_env (SCM a)
1200 return read_input_file_env_ (read_env (r0), r0);
1202 return apply_env (cell_symbol_read_input_file, cell_nil, r0);
1206 load_env (SCM a) ///((internal))
1210 g_stdin = fopen ("module/mes/read-0.mes", "r");
1211 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mes", "r");
1213 if (!g_function) r0 = mes_builtins (r0);
1214 r3 = read_input_file_env (r0);
1220 bload_env (SCM a) ///((internal))
1222 g_stdin = fopen ("module/mes/read-0.mo", "r");
1223 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
1224 char *p = (char*)g_cells;
1225 assert (getchar () == 'M');
1226 assert (getchar () == 'E');
1227 assert (getchar () == 'S');
1228 stack = getchar () << 8;
1229 stack += getchar ();
1236 g_free.value = (p-(char*)g_cells) / sizeof (scm);
1241 r0 = mes_builtins (r0);
1249 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
1250 stack = cons (frame, stack);
1253 char *p = (char*)g_cells;
1254 fputc ('M', stdout);
1255 fputc ('E', stdout);
1256 fputc ('S', stdout);
1257 fputc (stack >> 8, stdout);
1258 fputc (stack % 256, stdout);
1259 for (int i=0; i<g_free.value * sizeof(scm); i++)
1260 fputc (*p++, stdout);
1266 #include "display.c"
1270 #include "quasiquote.c"
1275 main (int argc, char *argv[])
1277 g_debug = getenv ("MES_DEBUG");
1278 if (getenv ("MES_ARENA")) ARENA_SIZE = atoi (getenv ("MES_ARENA"));
1279 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
1280 if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.3\n");
1282 r0 = mes_environment ();
1283 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1284 ? bload_env (r0) : load_env (r0);
1285 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1286 display_ (stderr, begin_env (program, r0));
1289 if (g_debug) fprintf (stderr, "\nstats: [%d]\n", g_free.value);