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
36 #define MES_MINI 0 // 1 for gc-2a.test, gc-3.test
39 int ARENA_SIZE = 400000000; // need this much for scripts/mescc.mes
40 //int ARENA_SIZE = 300000000; // need this much for tests/match.scm
41 //int ARENA_SIZE = 30000000; // need this much for tests/record.scm
42 //int ARENA_SIZE = 500000; // enough for tests/scm.test
43 //int ARENA_SIZE = 60000; // enough for tests/base.test
44 int GC_SAFETY = 10000;
47 //int ARENA_SIZE = 500; // MINI
48 int ARENA_SIZE = 4000; // MES_MINI, gc-3.test
49 //int ARENA_SIZE = 10000; // gc-2a.test
50 //int ARENA_SIZE = 18000; // gc-2.test -->KRAK
51 //int ARENA_SIZE = 23000; // gc-2.test OK
52 // int GC_SAFETY = 1000;
53 // int GC_FREE = 1000;
59 enum type_t {CHAR, FUNCTION, MACRO, NUMBER, PAIR, SPECIAL, STRING, SYMBOL, REF, VALUES, VECTOR, BROKEN_HEART};
60 typedef SCM (*function0_t) (void);
61 typedef SCM (*function1_t) (SCM);
62 typedef SCM (*function2_t) (SCM, SCM);
63 typedef SCM (*function3_t) (SCM, SCM, SCM);
64 typedef SCM (*functionn_t) (SCM);
65 typedef struct function_t {
67 function0_t function0;
68 function1_t function1;
69 function2_t function2;
70 function3_t function3;
71 functionn_t functionn;
76 typedef struct scm_t {
95 #include "mes.symbols.h"
101 #include "quasiquote.h"
105 SCM display_ (FILE* f, SCM x);
106 SCM display_helper (FILE*, SCM , bool, char const*, bool);
111 SCM r1 = 0; // param 1
112 SCM r2 = 0; // param 2
113 SCM r3 = 0; // param 3
115 scm scm_nil = {SPECIAL, "()"};
116 scm scm_f = {SPECIAL, "#f"};
117 scm scm_t = {SPECIAL, "#t"};
118 scm scm_dot = {SPECIAL, "."};
119 scm scm_undefined = {SPECIAL, "*undefined*"};
120 scm scm_unspecified = {SPECIAL, "*unspecified*"};
121 scm scm_closure = {SPECIAL, "*closure*"};
122 scm scm_circular = {SPECIAL, "*circular*"};
127 scm scm_begin = {SPECIAL, "*begin*"};
129 scm scm_symbol_lambda = {SYMBOL, "lambda"};
130 scm scm_symbol_begin = {SYMBOL, "begin"};
131 scm scm_symbol_if = {SYMBOL, "if"};
132 scm scm_symbol_define = {SYMBOL, "define"};
133 scm scm_symbol_define_macro = {SYMBOL, "define-macro"};
134 scm scm_symbol_set_x = {SYMBOL, "set!"};
136 scm scm_symbol_quote = {SYMBOL, "quote"};
137 scm scm_symbol_quasiquote = {SYMBOL, "quasiquote"};
138 scm scm_symbol_unquote = {SYMBOL, "unquote"};
139 scm scm_symbol_unquote_splicing = {SYMBOL, "unquote-splicing"};
141 scm scm_symbol_sc_expand = {SYMBOL, "sc-expand"};
142 scm scm_symbol_expand_macro = {SYMBOL, "expand-macro"};
143 scm scm_symbol_sc_expander_alist = {SYMBOL, "*sc-expander-alist*"};
144 scm scm_symbol_noexpand = {SYMBOL, "noexpand"};
145 scm scm_symbol_syntax = {SYMBOL, "syntax"};
146 scm scm_symbol_quasisyntax = {SYMBOL, "quasisyntax"};
147 scm scm_symbol_unsyntax = {SYMBOL, "unsyntax"};
148 scm scm_symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"};
150 scm scm_symbol_call_with_values = {SYMBOL, "call-with-values"};
151 scm scm_symbol_current_module = {SYMBOL, "current-module"};
152 scm scm_symbol_primitive_load = {SYMBOL, "primitive-load"};
154 scm char_nul = {CHAR, .name="nul", .value=0};
155 scm char_backspace = {CHAR, .name="backspace", .value=8};
156 scm char_tab = {CHAR, .name="tab", .value=9};
157 scm char_newline = {CHAR, .name="newline", .value=10};
158 scm char_vt = {CHAR, .name="vt", .value=11};
159 scm char_page = {CHAR, .name="page", .value=12};
160 scm char_return = {CHAR, .name="return", .value=13};
161 scm char_space = {CHAR, .name="space", .value=32};
163 scm g_free = {NUMBER, .value=0};
167 #define CAR(x) g_cells[x].car
168 #define CDR(x) g_cells[x].cdr
169 #define CAAR(x) CAR (CAR (x))
170 #define CDAR(x) CDR (CAR (x))
171 #define CAAR(x) CAR (CAR (x))
172 #define CADAR(x) CAR (CDR (CAR (x)))
173 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
174 #define CADR(x) CAR (CDR (x))
175 #define LENGTH(x) g_cells[x].length
176 #define STRING(x) g_cells[x].string
177 #define TYPE(x) g_cells[x].type
178 #define MACRO(x) g_cells[x].macro
179 #define VALUE(x) g_cells[x].value
180 #define VECTOR(x) g_cells[x].vector
182 #define NCAR(x) g_news[x].car
183 #define NTYPE(x) g_news[x].type
188 return g_cells[x].type;
194 assert (g_cells[x].type == PAIR);
195 return g_cells[x].car;
201 assert (g_cells[x].type == PAIR);
202 return g_cells[x].cdr;
208 return g_cells[x].value;
215 assert (g_free.value + n < ARENA_SIZE);
216 SCM x = g_free.value;
220 return (SCM )malloc(n*sizeof (scm));
227 assert (g_free.value + n < ARENA_SIZE);
228 SCM x = g_free.value;
237 g_news = (scm *)malloc (ARENA_SIZE*sizeof(scm));
238 g_news[0].type = VECTOR;
239 g_news[0].length = 1000;
240 g_news[0].vector = 0;
242 g_news[0].type = CHAR;
243 g_news[0].value = 'n';
250 fprintf (stderr, "***gc[%d]...", g_free.value);
254 for (int i=g_free.value; i<g_start; i++)
256 symbols = gc_copy (symbols);
257 SCM new = gc_copy (stack);
258 fprintf (stderr, "new=%d, start=%d\n", new, stack);
266 while (scan < g_free.value)
268 if (NTYPE (scan) == MACRO
269 || NTYPE (scan) == PAIR
270 || NTYPE (scan) == REF
272 || ((NTYPE (scan) == SPECIAL && TYPE (NCAR (scan)) == PAIR)
273 || (NTYPE (scan) == STRING && TYPE (NCAR (scan)) == PAIR)
274 || (NTYPE (scan) == SYMBOL && TYPE (NCAR (scan)) == PAIR)))
276 SCM car = gc_copy (g_news[scan].car);
277 gc_relocate_car (scan, car);
279 if ((NTYPE (scan) == MACRO
280 || NTYPE (scan) == PAIR
281 || NTYPE (scan) == VALUES)
282 && g_news[scan].cdr) // allow for 0 terminated list of symbols
284 SCM cdr = gc_copy (g_news[scan].cdr);
285 gc_relocate_cdr (scan, cdr);
295 if (type (old) == BROKEN_HEART) return g_cells[old].car;
296 SCM new = g_free.value++;
297 g_news[new] = g_cells[old];
298 if (NTYPE (new) == VECTOR)
300 g_news[new].vector = g_free.value;
301 for (int i=0; i<LENGTH (old); i++)
302 g_news[g_free.value++] = g_cells[VECTOR (old)+i];
304 g_cells[old].type = BROKEN_HEART;
305 g_cells[old].car = new;
310 gc_relocate_car (SCM new, SCM car)
312 g_news[new].car = car;
313 return cell_unspecified;
317 gc_relocate_cdr (SCM new, SCM cdr)
319 g_news[new].cdr = cdr;
320 return cell_unspecified;
326 scm *cells = g_cells;
329 fprintf (stderr, " => jam[%d]\n", g_free.value);
336 fprintf (stderr, "cells: ");
338 display_ (stderr, -1);
339 fprintf (stderr, "\n");
342 fprintf (stderr, "news: ");
344 display_ (stderr, -1);
345 fprintf (stderr, "\n");
348 return cell_unspecified;
352 gc_make_cell (SCM type, SCM car, SCM cdr)
354 SCM x = gc_alloc (1);
355 assert (g_cells[type].type == NUMBER);
356 g_cells[x].type = value (type);
357 if (value (type) == CHAR || value (type) == NUMBER) {
358 if (car) g_cells[x].car = g_cells[car].car;
359 if (cdr) g_cells[x].cdr = g_cells[cdr].cdr;
361 g_cells[x].car = car;
362 g_cells[x].cdr = cdr;
374 gc_make_vector (SCM n)
376 g_cells[tmp_num].value = VECTOR;
377 SCM v = gc_alloc (value (n));
378 SCM x = gc_make_cell (tmp_num, (SCM)(long)value (n), v);
379 for (int i=0; i<value (n); i++) g_cells[x+i].vector = vector_entry (cell_unspecified);
384 make_cell (SCM type, SCM car, SCM cdr)
387 assert (g_cells[type].type == NUMBER);
388 g_cells[x].type = VALUE (type);
389 if (VALUE (type) == CHAR || VALUE (type) == NUMBER) {
390 if (car) g_cells[x].car = g_cells[car].car;
391 if (cdr) g_cells[x].cdr = g_cells[cdr].cdr;
393 g_cells[x].car = car;
394 g_cells[x].cdr = cdr;
402 g_cells[tmp_num].value = PAIR;
403 return make_cell (tmp_num, x, y);
410 || (g_cells[x].type == CHAR && g_cells[y].type == CHAR
411 && VALUE (x) == VALUE (y))
412 || (g_cells[x].type == NUMBER && g_cells[y].type == NUMBER
413 && VALUE (x) == VALUE (y)))
418 set_car_x (SCM x, SCM e)
420 assert (g_cells[x].type == PAIR);
422 return cell_unspecified;
426 set_cdr_x (SCM x, SCM e)
428 assert (g_cells[x].type == PAIR);
429 cache_invalidate (cdr (x));
431 return cell_unspecified;
435 set_env_x (SCM x, SCM e, SCM a)
437 cache_invalidate (x);
438 SCM p = assert_defined (x, assq (x, a));
439 return set_cdr_x (p, e);
445 return cons (cell_symbol_quote, x);
451 return cons (cell_symbol_quasiquote, x);
457 return cons (cell_symbol_quasisyntax, x);
461 pairlis (SCM x, SCM y, SCM a)
465 if (pair_p (x) == cell_f)
466 return cons (cons (x, y), a);
467 return cons (cons (car (x), car (y)),
468 pairlis (cdr (x), cdr (y), a));
474 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f)
476 if (g_cells[a].type == BROKEN_HEART || g_cells[CAR (a)].type == BROKEN_HEART)
477 fprintf (stderr, "oops, broken heart\n");
480 return a != cell_nil ? car (a) : cell_f;
484 #define CACHE_SIZE 30
489 assq_ref_cache (SCM x, SCM a)
492 if (x == cell_f) return cell_undefined;
495 SCM cache_invalidate (SCM x){}
496 SCM cache_invalidate_range (SCM p,SCM a){}
497 SCM cache_save (SCM p){}
498 SCM cache_lookup (SCM x){}
502 SCM env_cache_cars[CACHE_SIZE];
503 SCM env_cache_cdrs[CACHE_SIZE];
504 int cache_threshold = 0;
508 int n = g_cells[car (p)].hits;
509 if (n < cache_threshold) return cell_unspecified;
511 for (int i=0; i < CACHE_SIZE; i++) {
512 if (!env_cache_cars[i]) {
516 if (env_cache_cars[i] == car (p)) return cell_unspecified;
517 if (n > g_cells[env_cache_cars[i]].hits) {
518 n = g_cells[env_cache_cars[i]].hits;
523 cache_threshold = g_cells[car (p)].hits;
524 env_cache_cars[j] = car (p);
525 env_cache_cdrs[j] = cdr (p);
527 return cell_unspecified;
533 for (int i=0; i < CACHE_SIZE; i++) {
534 if (!env_cache_cars[i]) break;
535 if (env_cache_cars[i] == x) return env_cache_cdrs[i];
537 return cell_undefined;
541 cache_invalidate (SCM x)
543 for (int i=0; i < CACHE_SIZE; i++) {
544 if (env_cache_cars[i] == x) {
545 env_cache_cars[i] = 0;
549 return cell_unspecified;
553 cache_invalidate_range (SCM p, SCM a)
556 cache_invalidate (caar (p));
559 return cell_unspecified;
563 assq_ref_cache (SCM x, SCM a)
566 SCM c = cache_lookup (x);
567 if (c != cell_undefined) return c;
569 while (a != cell_nil && x != CAAR (a)) {i++;a = cdr (a);}
570 if (a == cell_nil) return cell_undefined;
571 if (i>ENV_HEAD) cache_save (car (a));
577 assert_defined (SCM x, SCM e)
579 if (e == cell_undefined)
581 fprintf (stderr, "eval: unbound variable:");
582 display_ (stderr, x);
583 fprintf (stderr, "\n");
584 assert (!"unbound variable");
592 SCM frame = car (stack);
603 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
604 stack = cons (frame, stack);
612 vm_call (function0_t f, SCM p1, SCM p2, SCM a)
614 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
615 stack = cons (frame, stack);
619 if (f == vm_if_env && g_free.value + GC_SAFETY > ARENA_SIZE)
621 cache_invalidate_range (r0, cell_nil);
627 frame = gc_frame (stack);
633 evlis_env (SCM m, SCM a)
635 return vm_call (vm_evlis_env, m, cell_undefined, a);
639 apply_env (SCM fn, SCM x, SCM a)
641 return vm_call (vm_apply_env, fn, x, a);
645 eval_env (SCM e, SCM a)
647 return vm_call (vm_eval_env, e, cell_undefined, a);
651 expand_macro_env (SCM e, SCM a)
653 return vm_call (vm_expand_macro_env, e, cell_undefined, a);
657 begin_env (SCM e, SCM a)
659 return vm_call (vm_begin_env, e, cell_undefined, a);
663 if_env (SCM e, SCM a)
665 return vm_call (vm_if_env, e, cell_undefined, a);
669 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
671 SCM cl = cons (cons (cell_closure, x), x);
676 cache_invalidate_range (r0, g_cells[r3].cdr);
677 SCM r = vm_call_lambda ();
678 cache_invalidate_range (r0, g_cells[r3].cdr);
685 if (r1 == cell_nil) return cell_nil;
686 if (type (r1) != PAIR) return eval_env (r1, r0);
687 r2 = eval_env (car (r1), r0);
688 r1 = evlis_env (cdr (r1), r0);
689 return cons (r2, r1);
695 return vm_call (vm_begin_env, r1, cell_undefined, r0);
701 if (type (r1) != PAIR)
703 if (type (r1) == FUNCTION) return call (r1, r2);
704 if (r1 == cell_symbol_call_with_values)
705 return call_with_values_env (car (r2), cadr (r2), r0);
706 if (r1 == cell_symbol_current_module) return r0;
708 else if (car (r1) == cell_symbol_lambda) {
709 SCM args = cadr (r1);
710 SCM body = cddr (r1);
711 SCM p = pairlis (args, r2, r0);
712 return call_lambda (body, p, p, r0);
714 // cache_invalidate_range (r2, g_cells[r0].cdr);
715 // SCM r = begin_env (cddr (r1), cons (cons (cell_closure, p), p));
716 // cache_invalidate_range (r2, g_cells[r0].cdr);
719 else if (car (r1) == cell_closure) {
720 SCM args = caddr (r1);
721 SCM body = cdddr (r1);
724 SCM p = pairlis (args, r2, aa);
725 return call_lambda (body, p, aa, r0);
728 // cache_invalidate_range (r2, g_cells[r3].cdr);
729 // SCM r = begin_env (body, cons (cons (cell_closure, p), p));
730 // cache_invalidate_range (r2, g_cells[r3].cdr);
734 else if (car (r1) == cell_symbol_label)
735 return apply_env (caddr (r1), r2, cons (cons (cadr (r1), caddr (r1)), r0));
737 SCM e = eval_env (r1, r0);
738 char const* type = 0;
739 if (e == cell_f || e == cell_t) type = "bool";
740 if (g_cells[e].type == CHAR) type = "char";
741 if (g_cells[e].type == NUMBER) type = "number";
742 if (g_cells[e].type == STRING) type = "string";
743 if (e == cell_unspecified) type = "*unspecified*";
744 if (e == cell_undefined) type = "*undefined*";
747 fprintf (stderr, "cannot apply: %s: ", type);
748 display_ (stderr, e);
749 fprintf (stderr, " [");
750 display_ (stderr, r1);
751 fprintf (stderr, "]\n");
752 assert (!"cannot apply");
754 return apply_env (e, r2, r0);
757 SCM cstring_to_list (char const* s);
766 if (car (r1) == cell_symbol_quote)
769 if (car (r1) == cell_symbol_syntax)
772 if (car (r1) == cell_symbol_begin)
773 return begin_env (r1, r0);
774 if (car (r1) == cell_symbol_lambda)
775 return make_closure (cadr (r1), cddr (r1), assq (cell_closure, r0));
776 if (car (r1) == cell_closure)
778 if (car (r1) == cell_symbol_if)
779 return if_env (cdr (r1), r0);
781 if (car (r1) == cell_symbol_define)
782 return define_env (r1, r0);
783 if (car (r1) == cell_symbol_define_macro)
784 return define_env (r1, r0);
785 if (car (r1) == cell_symbol_primitive_load)
786 return load_env (r0);
788 if (car (r1) == cell_symbol_define) {
789 fprintf (stderr, "C DEFINE: ");
791 g_cells[cadr (r1)].type == SYMBOL
792 ? g_cells[cadr (r1)].string
793 : g_cells[caadr (r1)].string);
794 fprintf (stderr, "\n");
796 assert (car (r1) != cell_symbol_define);
797 assert (car (r1) != cell_symbol_define_macro);
800 if (car (r1) == cell_symbol_set_x)
801 return set_env_x (cadr (r1), eval_env (caddr (r1), r0), r0);
803 assert (car (r1) != cell_symbol_set_x);
806 if (car (r1) == cell_symbol_unquote)
807 return eval_env (cadr (r1), r0);
808 if (car (r1) == cell_symbol_quasiquote)
809 return eval_quasiquote (cadr (r1), add_unquoters (r0));
812 if (car (r1) == cell_symbol_unsyntax)
813 return eval_env (cadr (r1), r0);
814 if (car (r1) == cell_symbol_quasisyntax)
815 return eval_quasisyntax (cadr (r1), add_unsyntaxers (r0));
817 SCM x = expand_macro_env (r1, r0);
819 return eval_env (x, r0);
820 SCM m = evlis_env (g_cells[r1].cdr, r0);
821 return apply_env (car (r1), m, r0);
823 case SYMBOL: return assert_defined (r1, assq_ref_cache (r1, r0));
829 vm_expand_macro_env ()
831 if (TYPE (CAR (r1)) == STRING && string_to_symbol (CAR (r1)) == cell_symbol_noexpand)
836 if (TYPE (r1) == PAIR
837 && (macro = lookup_macro (car (r1), r0)) != cell_f)
838 return apply_env (macro, CDR (r1), r0);
839 else if (TYPE (r1) == PAIR
840 && TYPE (CAR (r1)) == SYMBOL
841 && ((expanders = assq_ref_cache (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
842 && ((macro = assq (CAR (r1), expanders)) != cell_f))
844 SCM sc_expand = assq_ref_cache (cell_symbol_expand_macro, r0);
845 if (sc_expand != cell_undefined && sc_expand != cell_f)
846 r1 = apply_env (sc_expand, cons (r1, cell_nil), r0);
854 SCM r = cell_unspecified;
855 while (r1 != cell_nil) {
856 if (g_cells[r1].type == PAIR && g_cells[CAR (r1)].type == PAIR && caar (r1) == cell_symbol_begin)
857 r1 = append2 (cdar (r1), cdr (r1));
858 r = eval_env (car (r1), r0);
859 r1 = g_cells[r1].cdr;
867 SCM x = eval_env (car (r1), r0);
869 return eval_env (cadr (r1), r0);
870 if (cddr (r1) != cell_nil)
871 return eval_env (caddr (r1), r0);
872 return cell_unspecified;
877 display (SCM x) ///((arity . n))
882 if (g_cells[p].type == PAIR && g_cells[car (p)].type == NUMBER) fd = g_cells[car (p)].hits;
883 FILE *f = fd == 1 ? stdout : stderr;
884 return display_helper (f, e, false, "", false);
888 display_ (FILE* f, SCM x)
890 return display_helper (f, x, false, "", false);
896 if ((g_cells[fn].function->arity > 0 || g_cells[fn].function->arity == -1)
897 && x != cell_nil && TYPE (CAR (x)) == VALUES)
898 x = cons (CADAR (x), CDR (x));
899 if ((g_cells[fn].function->arity > 1 || g_cells[fn].function->arity == -1)
900 && x != cell_nil && TYPE (CDR (x)) == PAIR && TYPE (CADR (x)) == VALUES)
901 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
902 switch (g_cells[fn].function->arity)
904 case 0: return g_cells[fn].function->function0 ();
905 case 1: return g_cells[fn].function->function1 (car (x));
906 case 2: return g_cells[fn].function->function2 (car (x), cadr (x));
907 case 3: return g_cells[fn].function->function3 (car (x), cadr (x), caddr (x));
908 case -1: return g_cells[fn].function->functionn (x);
910 return cell_unspecified;
914 append2 (SCM x, SCM y)
916 if (x == cell_nil) return y;
917 assert (g_cells[x].type == PAIR);
918 return cons (car (x), append2 (cdr (x), y));
922 append (SCM x) ///((arity . n))
924 if (x == cell_nil) return cell_nil;
925 return append2 (car (x), append (cdr (x)));
931 g_cells[tmp_num].value = CHAR;
932 g_cells[tmp_num2].value = x;
933 return make_cell (tmp_num, tmp_num2, tmp_num2);
937 make_macro (SCM name, SCM x)
939 g_cells[tmp_num].value = MACRO;
940 return make_cell (tmp_num, STRING (name), x);
946 g_cells[tmp_num].value = NUMBER;
947 g_cells[tmp_num2].value = x;
948 return make_cell (tmp_num, tmp_num2, tmp_num2);
954 g_cells[tmp_num].value = REF;
955 return make_cell (tmp_num, x, x);
961 g_cells[tmp_num].value = STRING;
962 return make_cell (tmp_num, x, 0);
966 cstring_to_list (char const* s)
971 p = cons (make_char (s[i]), p);
976 list_of_char_equal_p (SCM a, SCM b)
978 while (a != cell_nil && b != cell_nil && g_cells[car (a)].value == g_cells[car (b)].value) {
979 assert (g_cells[car (a)].type == CHAR);
980 assert (g_cells[car (b)].type == CHAR);
984 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
988 internal_lookup_symbol (SCM s)
992 // .string and .name is the same field; .name is used as a handy
993 // static field initializer. A string can only be mistaken for a
994 // cell with type == PAIR for the one character long, zero-padded
996 SCM p = g_cells[car (x)].string;
997 char const* n = g_cells[car (x)].name;
998 if (p < 0 || p >= g_free.value || g_cells[p].type != PAIR)
999 g_cells[car (x)].string = cstring_to_list (g_cells[car (x)].name);
1000 if (list_of_char_equal_p (g_cells[car (x)].string, s) == cell_t) break;
1008 internal_make_symbol (SCM s)
1010 g_cells[tmp_num].value = SYMBOL;
1011 SCM x = make_cell (tmp_num, s, 0);
1012 symbols = cons (x, symbols);
1019 SCM x = internal_lookup_symbol (s);
1020 return x ? x : internal_make_symbol (s);
1027 g_cells[tmp_num].value = VECTOR;
1029 SCM x = make_cell (tmp_num, k, v);
1030 for (int i=0; i<k; i++) g_cells[v+i] = g_cells[vector_entry (cell_unspecified)];
1035 values (SCM x) ///((arity . n))
1037 SCM v = cons (0, x);
1038 g_cells[v].type = VALUES;
1043 call_with_values_env (SCM producer, SCM consumer, SCM a)
1045 SCM v = apply_env (producer, cell_nil, a);
1046 if (g_cells[v].type == VALUES)
1048 return apply_env (consumer, v, a);
1052 vector_length (SCM x)
1054 assert (g_cells[x].type == VECTOR);
1055 return make_number (LENGTH (x));
1059 vector_ref (SCM x, SCM i)
1061 assert (g_cells[x].type == VECTOR);
1062 assert (value (i) < LENGTH (x));
1063 SCM e = VECTOR (x) + value (i);
1064 if (g_cells[e].type == REF) e = g_cells[e].ref;
1065 if (g_cells[e].type == CHAR) e = make_char (value (e));
1066 if (g_cells[e].type == NUMBER) e = make_number (value (e));
1071 vector_entry (SCM x) {
1072 if (g_cells[x].type == PAIR || g_cells[x].type == SPECIAL || g_cells[x].type == STRING || g_cells[x].type == SYMBOL || g_cells[x].type == VECTOR) x = make_ref (x);
1077 vector_set_x (SCM x, SCM i, SCM e)
1079 assert (g_cells[x].type == VECTOR);
1080 assert (value (i) < LENGTH (x));
1081 g_cells[VECTOR (x)+g_cells[i].value] = g_cells[vector_entry (e)];
1082 return cell_unspecified;
1086 lookup (SCM s, SCM a)
1088 if (isdigit (value (car (s))) || (value (car (s)) == '-' && cdr (s) != cell_nil)) {
1091 if (value (car (s)) == '-') {
1096 while (p != cell_nil && isdigit (value (car (p)))) {
1098 n += value (car (p)) - '0';
1101 if (p == cell_nil) return make_number (n * sign);
1104 SCM x = internal_lookup_symbol (s);
1107 if (cdr (s) == cell_nil) {
1108 if (value (car (s)) == '\'') return cell_symbol_quote;
1109 if (value (car (s)) == '`') return cell_symbol_quasiquote;
1110 if (value (car (s)) == ',') return cell_symbol_unquote;
1112 else if (cddr (s) == cell_nil) {
1113 if (value (car (s)) == ',' && value (cadr (s)) == '@') return cell_symbol_unquote_splicing;
1114 if (value (car (s)) == '#' && value (cadr (s)) == '\'') return cell_symbol_syntax;
1115 if (value (car (s)) == '#' && value (cadr (s)) == '`') return cell_symbol_quasisyntax;
1116 if (value (car (s)) == '#' && value (cadr (s)) == ',') return cell_symbol_unsyntax;
1118 else if (cdddr (s) == cell_nil) {
1119 if (value (car (s)) == '#' && value (cadr (s)) == ',' && value (caddr (s)) == '@') return cell_symbol_unsyntax_splicing;
1120 if (value (car (s)) == 'E' && value (cadr (s)) == 'O' && value (caddr (s)) == 'F') {
1121 fprintf (stderr, "mes: got EOF\n");
1122 return cell_nil; // `EOF': eval program, which may read stdin
1126 return internal_make_symbol (s);
1130 lookup_char (int c, SCM a)
1132 return lookup (cons (make_char (c), cell_nil), a);
1136 list_to_vector (SCM x)
1138 g_cells[tmp_num].value = VALUE (length (x));
1139 SCM v = make_vector (tmp_num);
1141 while (x != cell_nil)
1143 g_cells[p++] = g_cells[vector_entry (car (x))];
1150 newline (SCM p) ///((arity . n))
1153 if (g_cells[p].type == PAIR && g_cells[car (p)].type == NUMBER) fd = g_cells[car (p)].value;
1154 FILE *f = fd == 1 ? stdout : stderr;
1156 return cell_unspecified;
1160 force_output (SCM p) ///((arity . n))
1163 if (g_cells[p].type == PAIR && g_cells[car (p)].type == NUMBER) fd = g_cells[car (p)].value;
1164 FILE *f = fd == 1 ? stdout : stderr;
1169 display_helper (FILE* f, SCM x, bool cont, char const *sep, bool quote)
1172 fprintf (f, "%s", sep);
1173 switch (g_cells[x].type)
1177 char const *name = 0;
1178 if (value (x) == char_nul.value) name = char_nul.name;
1179 else if (value (x) == char_backspace.value) name = char_backspace.name;
1180 else if (value (x) == char_tab.value) name = char_tab.name;
1181 else if (value (x) == char_newline.value) name = char_newline.name;
1182 else if (value (x) == char_vt.value) name = char_vt.name;
1183 else if (value (x) == char_page.value) name = char_page.name;
1184 else if (value (x) == char_return.value) name = char_return.name;
1185 else if (value (x) == char_space.value) name = char_space.name;
1186 if (name) fprintf (f, "#\\%s", name);
1187 else fprintf (f, "#\\%c", value (x));
1191 fprintf (f, "(*macro* ");
1192 display_helper (f, g_cells[x].macro, cont, sep, quote);
1195 case NUMBER: fprintf (f, "%d", value (x)); break;
1198 if (car (x) == cell_circular) {
1199 fprintf (f, "(*circ* . #-1#)");
1200 return cell_unspecified;
1202 if (car (x) == cell_closure) {
1203 fprintf (f, "(*closure* . #-1#)");
1204 return cell_unspecified;
1206 if (car (x) == cell_symbol_quote) {
1208 return display_helper (f, car (cdr (x)), cont, "", true);
1210 if (!cont) fprintf (f, "(");
1211 display_ (f, car (x));
1212 if (cdr (x) && g_cells[cdr (x)].type == PAIR)
1213 display_helper (f, cdr (x), true, " ", false);
1214 else if (cdr (x) != cell_nil) {
1216 display_ (f, cdr (x));
1218 if (!cont) fprintf (f, ")");
1224 for (int i = 0; i < LENGTH (x); i++) {
1225 if (g_cells[VECTOR (x)+i].type == VECTOR
1226 || (g_cells[VECTOR (x)+i].type == REF
1227 && g_cells[g_cells[VECTOR (x)+i].ref].type == VECTOR))
1228 fprintf (f, "%s#(...)", i ? " " : "");
1230 display_helper (f,VECTOR (x)+i, false, i ? " " : "", false);
1235 case REF: display_helper (f, g_cells[x].ref, cont, "", true); break;
1236 case FUNCTION: fprintf (f, "#<procedure %s>", g_cells[x].name); ;break;
1237 case BROKEN_HEART: fprintf (f, "<3"); break;
1243 while (p != cell_nil) {
1244 assert (g_cells[car (p)].type == CHAR);
1245 fputc (g_cells[car (p)].value, f);
1249 else if (g_cells[x].type != PAIR && g_cells[x].name) fprintf (f, "%s", g_cells[x].name);
1251 return cell_unspecified;
1260 return getc (g_stdin);
1266 return ungetc (c, g_stdin);
1280 return make_char (peekchar ());
1286 return make_char (getchar ());
1290 write_char (SCM x) ///((arity . n))
1295 if (g_cells[p].type == PAIR && g_cells[car (p)].type == NUMBER) fd = g_cells[car (p)].value;
1296 FILE *f = fd == 1 ? stdout : stderr;
1297 assert (g_cells[c].type == NUMBER || g_cells[c].type == CHAR);
1298 fputc (value (c), f);
1305 assert (g_cells[c].type == NUMBER || g_cells[c].type == CHAR);
1306 ungetchar (value (c));
1313 if (c == '\n') return c;
1314 return readcomment (getchar ());
1320 if (c == '!' && peekchar () == '#') return getchar ();
1321 return readblock (getchar ());
1325 readword (int c, SCM w, SCM a)
1327 if (c == EOF && w == cell_nil) return cell_nil;
1328 if (c == '\n' && w == cell_nil) return readword (getchar (), w, a);
1329 if (c == '\n' && value (car (w)) == '.' && cdr (w) == cell_nil) return cell_dot;
1330 if (c == EOF || c == '\n') return lookup (w, a);
1331 if (c == ' ') return readword ('\n', w, a);
1332 if (c == '"' && w == cell_nil) return readstring ();
1333 if (c == '"') {ungetchar (c); return lookup (w, a);}
1334 if (c == '(' && w == cell_nil) return readlist (a);
1335 if (c == '(') {ungetchar (c); return lookup (w, a);}
1336 if (c == ')' && w == cell_nil) {ungetchar (c); return cell_nil;}
1337 if (c == ')') {ungetchar (c); return lookup (w, a);}
1338 if (c == ',' && peekchar () == '@') {getchar (); return cons (lookup (g_cells[cell_symbol_unquote_splicing].string, a),
1339 cons (readword (getchar (), w, a),
1344 && w == cell_nil) {return cons (lookup_char (c, a),
1345 cons (readword (getchar (), w, a),
1347 if (c == '#' && peekchar () == ',' && w == cell_nil) {
1349 if (peekchar () == '@'){getchar (); return cons (lookup (g_cells[cell_symbol_unsyntax_splicing].string, a),
1350 cons (readword (getchar (), w, a),
1352 return cons (lookup (g_cells[cell_symbol_unsyntax].string, a), cons (readword (getchar (), w, a), cell_nil));
1354 if (c == '#' && (peekchar () == '\'' || peekchar () == '`') && w == cell_nil) {
1356 return cons (lookup (cons (make_char ('#'), cons (make_char (c), cell_nil)), a),
1357 cons (readword (getchar (), w, a), cell_nil));}
1358 if (c == ';') {readcomment (c); return readword ('\n', w, a);}
1359 if (c == '#' && peekchar () == 'x') {getchar (); return read_hex ();}
1360 if (c == '#' && peekchar () == '\\') {getchar (); return read_character ();}
1361 if (c == '#' && w == cell_nil && peekchar () == '(') {getchar (); return list_to_vector (readlist (a));}
1362 if (c == '#' && peekchar () == '(') {ungetchar (c); return lookup (w, a);}
1363 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return readword (getchar (), w, a);}
1364 return readword (getchar (), append2 (w, cons (make_char (c), cell_nil)), a);
1371 int c = peekchar ();
1372 while ((c >= '0' && c <= '9')
1373 || (c >= 'A' && c <= 'F')
1374 || (c >= 'a' && c <= 'f')) {
1376 if (c >= 'a') n += c - 'a' + 10;
1377 else if (c >= 'A') n += c - 'A' + 10;
1382 return make_number (n);
1389 if (c >= '0' && c <= '7'
1390 && peekchar () >= '0' && peekchar () <= '7') {
1392 while (peekchar () >= '0' && peekchar () <= '7') {
1394 c += getchar () - '0';
1397 else if (c >= 'a' && c <= 'z'
1398 && peekchar () >= 'a' && peekchar () <= 'z') {
1402 while (peekchar () >= 'a' && peekchar () <= 'z') {
1406 if (!strcmp (buf, char_nul.name)) c = char_nul.value;
1407 else if (!strcmp (buf, char_backspace.name)) c = char_backspace.value;
1408 else if (!strcmp (buf, char_tab.name)) c = char_tab.value;
1409 else if (!strcmp (buf, char_newline.name)) c = char_newline.value;
1410 else if (!strcmp (buf, char_vt.name)) c = char_vt.value;
1411 else if (!strcmp (buf, char_page.name)) c = char_page.value;
1412 else if (!strcmp (buf, char_return.name)) c = char_return.value;
1413 else if (!strcmp (buf, char_space.name)) c = char_space.value;
1415 fprintf (stderr, "char not supported: %s\n", buf);
1416 assert (!"char not supported");
1419 return make_char (c);
1423 append_char (SCM x, int i)
1425 return append2 (x, cons (make_char (i), cell_nil));
1434 if (c == '"') break;
1435 if (c == '\\' && peekchar () == '"') p = append_char (p, getchar ());
1436 else if (c == '\\' && peekchar () == 'n') {getchar (); p = append_char (p, '\n');}
1437 else if (c == EOF) assert (!"EOF in string");
1438 else p = append_char (p, c);
1441 return make_string (p);
1445 eat_whitespace (int c)
1447 while (c == ' ' || c == '\t' || c == '\n') c = getchar ();
1448 if (c == ';') return eat_whitespace (readcomment (c));
1449 if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return eat_whitespace (getchar ());}
1457 c = eat_whitespace (c);
1458 if (c == ')') return cell_nil;
1459 SCM w = readword (c, cell_nil, a);
1461 return car (readlist (a));
1462 return cons (w, readlist (a));
1468 return readword (getchar (), cell_nil, a);
1472 acons (SCM key, SCM value, SCM alist)
1474 return cons (cons (key, value), alist);
1478 add_environment (SCM a, char const *name, SCM x)
1480 return acons (make_symbol (cstring_to_list (name)), x, a);
1484 mes_environment () ///((internal))
1487 g_cells = (scm *)malloc (ARENA_SIZE*sizeof(scm));
1488 g_cells[0].type = VECTOR;
1489 g_cells[0].length = ARENA_SIZE - 1;
1490 g_cells[0].length = 10;
1491 g_cells[0].vector = 0;
1493 // a = add_environment (a, "%free", &g_free); hihi, gets <3 moved
1494 // a = add_environment (a, "%the-cells", g_cells);
1495 // a = add_environment (a, "%new-cells", g_news);
1497 //#include "mes.symbols.i"
1499 g_cells[0].type = CHAR;
1500 g_cells[0].value = 'c';
1501 g_free.value = 1; // 0 is tricky
1504 #include "mes.symbols.i"
1506 cell_nil = g_free.value++;
1507 g_cells[cell_nil] = scm_nil;
1508 cell_f = g_free.value++;
1509 g_cells[cell_f] = scm_f;
1510 cell_t = g_free.value++;
1511 g_cells[cell_t] = scm_t;
1512 cell_undefined = g_free.value++;
1513 g_cells[cell_undefined] = scm_undefined;
1514 cell_unspecified = g_free.value++;
1515 g_cells[cell_unspecified] = scm_unspecified;
1516 cell_closure = g_free.value++;
1517 g_cells[cell_closure] = scm_closure;
1518 cell_begin = g_free.value++;
1519 g_cells[cell_begin] = scm_begin;
1521 cell_symbol_begin = g_free.value++;
1522 g_cells[cell_symbol_begin] = scm_symbol_begin;
1524 cell_symbol_sc_expander_alist = g_free.value++;
1525 g_cells[cell_symbol_sc_expander_alist] = scm_symbol_sc_expander_alist;
1526 cell_symbol_sc_expand = g_free.value++;
1527 g_cells[cell_symbol_sc_expand] = scm_symbol_sc_expand;
1529 // cell_dot = g_free.value++;
1530 // g_cells[cell_dot] = scm_dot;
1531 // cell_circular = g_free.value++;
1532 // g_cells[cell_circular] = scm_circular;
1533 // cell_symbol_lambda = g_free.value++;
1534 // g_cells[cell_symbol_lambda] = scm_symbol_lambda;
1535 // cell_symbol_if = g_free.value++;
1536 // g_cells[cell_symbol_if] = scm_symbol_if;
1537 // cell_symbol_define = g_free.value++;
1538 // g_cells[cell_symbol_define] = scm_symbol_define;
1539 // cell_symbol_define_macro = g_free.value++;
1540 // g_cells[cell_symbol_define_macro] = scm_symbol_define_macro;
1544 SCM symbol_max = g_free.value;
1552 #include "quasiquote.i"
1557 cell_cons = g_free.value++;
1558 cell_display = g_free.value++;
1559 cell_eq_p = g_free.value++;
1560 cell_newline = g_free.value++;
1562 g_cells[cell_cons] = scm_cons;
1563 g_cells[cell_display] = scm_display;
1564 g_cells[cell_eq_p] = scm_eq_p;
1565 g_cells[cell_newline] = scm_newline;
1567 cell_make_vector = g_free.value++;
1568 g_cells[cell_make_vector] = scm_make_vector;
1572 tmp = g_free.value++;
1573 tmp_num = g_free.value++;
1574 g_cells[tmp_num].type = NUMBER;
1575 tmp_num2 = g_free.value++;
1576 g_cells[tmp_num2].type = NUMBER;
1578 g_start = g_free.value;
1581 for (int i=1; i<symbol_max; i++)
1582 symbols = cons (i, symbols);
1587 #include "define.environment.i"
1588 #include "lib.environment.i"
1589 #include "math.environment.i"
1590 #include "mes.environment.i"
1591 #include "posix.environment.i"
1592 //#include "quasiquote.environment.i"
1593 #include "string.environment.i"
1594 #include "type.environment.i"
1597 a = add_environment (a, "cons", cell_cons);
1598 a = add_environment (a, "display", cell_display);
1599 a = add_environment (a, "eq?", cell_eq_p);
1600 a = add_environment (a, "newline", cell_newline);
1602 a = add_environment (a, "make-vector", cell_make_vector);
1605 a = add_environment (a, "*", cell_multiply);
1606 a = add_environment (a, "list", cell_list);
1608 a = add_environment (a, "car", cell_car);
1609 a = add_environment (a, "cdr", cell_cdr);
1610 a = add_environment (a, "+", cell_plus);
1611 a = add_environment (a, "quote", cell_quote);
1612 a = add_environment (a, "null?", cell_null_p);
1613 a = add_environment (a, "=", cell_is_p);
1615 // a = add_environment (a, "gc", cell_gc);
1616 // a = add_environment (a, "apply-env", cell_apply_env);
1617 // a = add_environment (a, "eval-env", cell_eval_env);
1618 // a = add_environment (a, "cadr", cell_cadr);
1623 ////symbols = cons (cell_symbol_label, symbols);
1624 a = cons (cons (cell_symbol_label, cell_t), a);
1626 a = cons (cons (cell_symbol_begin, cell_begin), a);
1628 a = add_environment (a, "sc-expand", cell_f);
1630 a = cons (cons (cell_closure, a), a);
1632 internal_lookup_symbol (cell_nil);
1638 stack = cons (cell_nil, cell_nil);
1644 make_lambda (SCM args, SCM body)
1646 return cons (cell_symbol_lambda, cons (args, body));
1650 make_closure (SCM args, SCM body, SCM a)
1652 return cons (cell_closure, cons (cons (cell_circular, a), cons (args, body)));
1656 lookup_macro (SCM x, SCM a)
1658 if (g_cells[x].type != SYMBOL) return cell_f;
1659 SCM m = assq_ref_cache (x, a);
1660 if (macro_p (m) == cell_t) return MACRO (m);
1665 read_input_file_env (SCM e, SCM a)
1667 if (e == cell_nil) return e;
1668 return cons (e, read_input_file_env (read_env (a), a));
1674 SCM p = read_input_file_env (read_env (a), a);
1675 return begin_env (p, a);
1683 #include "quasiquote.c"
1687 main (int argc, char *argv[])
1689 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n");
1690 if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.2\n");
1692 SCM a = mes_environment ();
1693 display_ (stderr, load_env (a));
1695 fprintf (stderr, "\nstats: [%d]\n", g_free.value);