/* -*-comment-start: "//";comment-end:""-*- * Mes --- Maxwell Equations of Software * Copyright © 2016 Jan Nieuwenhuizen * * This file is part of Mes. * * Mes is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or (at * your option) any later version. * * Mes is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Mes. If not, see . */ #define _GNU_SOURCE #include #include #include #include #include #include #include #define DEBUG 0 #define QUASIQUOTE 1 //#define QUASISYNTAX 0 #define GC 1 #define MES_FULL 1 #define MES_MINI 0 // 1 for gc-2a.test, gc-3.test #if MES_FULL int ARENA_SIZE = 400000000; // need this much for scripts/mescc.mes //int ARENA_SIZE = 300000000; // need this much for tests/match.scm //int ARENA_SIZE = 30000000; // need this much for tests/record.scm //int ARENA_SIZE = 500000; // enough for tests/scm.test //int ARENA_SIZE = 60000; // enough for tests/base.test int GC_SAFETY = 10000; int GC_FREE = 20000; #else //int ARENA_SIZE = 500; // MINI int ARENA_SIZE = 4000; // MES_MINI, gc-3.test //int ARENA_SIZE = 10000; // gc-2a.test //int ARENA_SIZE = 18000; // gc-2.test -->KRAK //int ARENA_SIZE = 23000; // gc-2.test OK // int GC_SAFETY = 1000; // int GC_FREE = 1000; int GC_SAFETY = 10; int GC_FREE = 10; #endif typedef long SCM; enum type_t {CHAR, FUNCTION, MACRO, NUMBER, PAIR, SPECIAL, STRING, SYMBOL, REF, VALUES, VECTOR, BROKEN_HEART}; typedef SCM (*function0_t) (void); typedef SCM (*function1_t) (SCM); typedef SCM (*function2_t) (SCM, SCM); typedef SCM (*function3_t) (SCM, SCM, SCM); typedef SCM (*functionn_t) (SCM); typedef struct function_t { union { function0_t function0; function1_t function1; function2_t function2; function3_t function3; functionn_t functionn; }; int arity; } function; struct scm_t; typedef struct scm_t { enum type_t type; union { char const *name; SCM string; SCM car; SCM ref; int length; }; union { int value; int function; SCM cdr; SCM macro; SCM vector; int hits; }; } scm; function functions[200]; int g_function = 0; #include "mes.symbols.h" #include "define.h" #include "lib.h" #include "math.h" #include "mes.h" #include "posix.h" #include "quasiquote.h" #include "string.h" #include "type.h" SCM display_ (FILE* f, SCM x); SCM display_helper (FILE*, SCM , bool, char const*, bool); SCM symbols = 0; SCM stack = 0; SCM r0 = 0; // a/env SCM r1 = 0; // param 1 SCM r2 = 0; // param 2 SCM r3 = 0; // param 3 scm scm_nil = {SPECIAL, "()"}; scm scm_f = {SPECIAL, "#f"}; scm scm_t = {SPECIAL, "#t"}; scm scm_dot = {SPECIAL, "."}; scm scm_undefined = {SPECIAL, "*undefined*"}; scm scm_unspecified = {SPECIAL, "*unspecified*"}; scm scm_closure = {SPECIAL, "*closure*"}; scm scm_circular = {SPECIAL, "*circular*"}; #if BOOT scm scm_label = { SPECIAL, "label"}; #endif scm scm_begin = {SPECIAL, "*begin*"}; scm scm_symbol_lambda = {SYMBOL, "lambda"}; scm scm_symbol_begin = {SYMBOL, "begin"}; scm scm_symbol_if = {SYMBOL, "if"}; scm scm_symbol_define = {SYMBOL, "define"}; scm scm_symbol_define_macro = {SYMBOL, "define-macro"}; scm scm_symbol_set_x = {SYMBOL, "set!"}; scm scm_symbol_quote = {SYMBOL, "quote"}; scm scm_symbol_quasiquote = {SYMBOL, "quasiquote"}; scm scm_symbol_unquote = {SYMBOL, "unquote"}; scm scm_symbol_unquote_splicing = {SYMBOL, "unquote-splicing"}; scm scm_symbol_sc_expand = {SYMBOL, "sc-expand"}; scm scm_symbol_expand_macro = {SYMBOL, "expand-macro"}; scm scm_symbol_sc_expander_alist = {SYMBOL, "*sc-expander-alist*"}; scm scm_symbol_noexpand = {SYMBOL, "noexpand"}; scm scm_symbol_syntax = {SYMBOL, "syntax"}; scm scm_symbol_quasisyntax = {SYMBOL, "quasisyntax"}; scm scm_symbol_unsyntax = {SYMBOL, "unsyntax"}; scm scm_symbol_unsyntax_splicing = {SYMBOL, "unsyntax-splicing"}; scm scm_symbol_call_with_values = {SYMBOL, "call-with-values"}; scm scm_symbol_current_module = {SYMBOL, "current-module"}; scm scm_symbol_primitive_load = {SYMBOL, "primitive-load"}; scm char_nul = {CHAR, .name="nul", .value=0}; scm char_backspace = {CHAR, .name="backspace", .value=8}; scm char_tab = {CHAR, .name="tab", .value=9}; scm char_newline = {CHAR, .name="newline", .value=10}; scm char_vt = {CHAR, .name="vt", .value=11}; scm char_page = {CHAR, .name="page", .value=12}; scm char_return = {CHAR, .name="return", .value=13}; scm char_space = {CHAR, .name="space", .value=32}; scm g_free = {NUMBER, .value=0}; scm *g_cells; scm *g_news = 0; #define CAR(x) g_cells[x].car #define CDR(x) g_cells[x].cdr #define CAAR(x) CAR (CAR (x)) #define CDAR(x) CDR (CAR (x)) #define CAAR(x) CAR (CAR (x)) #define CADAR(x) CAR (CDR (CAR (x))) #define CDADAR(x) CAR (CDR (CAR (CDR (x)))) #define CADR(x) CAR (CDR (x)) #define LENGTH(x) g_cells[x].length #define STRING(x) g_cells[x].string #define TYPE(x) g_cells[x].type #define MACRO(x) g_cells[x].macro #define VALUE(x) g_cells[x].value #define VECTOR(x) g_cells[x].vector #define FUNCTION(x) functions[g_cells[x].function] #define NCAR(x) g_news[x].car #define NTYPE(x) g_news[x].type enum type_t type (SCM x) { return g_cells[x].type; } SCM car (SCM x) { assert (g_cells[x].type == PAIR); return g_cells[x].car; } SCM cdr (SCM x) { assert (g_cells[x].type == PAIR); return g_cells[x].cdr; } long value (SCM x) { return g_cells[x].value; } SCM alloc (int n) { #if GC assert (g_free.value + n < ARENA_SIZE); SCM x = g_free.value; g_free.value += n; return x; #else return (SCM )malloc(n*sizeof (scm)); #endif } SCM gc_alloc (int n) { assert (g_free.value + n < ARENA_SIZE); SCM x = g_free.value; g_free.value += n; return x; } SCM g_start; scm * gc_news () { g_news = (scm *)malloc (ARENA_SIZE*sizeof(scm)); g_news[0].type = VECTOR; g_news[0].length = 1000; g_news[0].vector = 0; g_news++; g_news[0].type = CHAR; g_news[0].value = 'n'; return g_news; } SCM gc () { fprintf (stderr, "***gc[%d]...", g_free.value); g_free.value = 1; if (!g_news) gc_news (); for (int i=g_free.value; i jam[%d]\n", g_free.value); return stack; } SCM gc_show () { fprintf (stderr, "cells: "); scm *t = g_cells; display_ (stderr, -1); fprintf (stderr, "\n"); if (g_news) { fprintf (stderr, "news: "); g_cells = g_news; display_ (stderr, -1); fprintf (stderr, "\n"); } g_cells = t; return cell_unspecified; } SCM gc_make_cell (SCM type, SCM car, SCM cdr) { SCM x = gc_alloc (1); assert (g_cells[type].type == NUMBER); g_cells[x].type = value (type); if (value (type) == CHAR || value (type) == NUMBER) { if (car) g_cells[x].car = g_cells[car].car; if (cdr) g_cells[x].cdr = g_cells[cdr].cdr; } else { g_cells[x].car = car; g_cells[x].cdr = cdr; } return x; } SCM tmp; SCM tmp_num; SCM tmp_num2; SCM tmp_num3; SCM tmp_num4; SCM gc_make_vector (SCM n) { g_cells[tmp_num].value = VECTOR; SCM v = gc_alloc (value (n)); SCM x = gc_make_cell (tmp_num, (SCM)(long)value (n), v); for (int i=0; i g_cells[env_cache_cars[i]].hits) { n = g_cells[env_cache_cars[i]].hits; j = i; } } if (j >= 0) { cache_threshold = g_cells[car (p)].hits; env_cache_cars[j] = car (p); env_cache_cdrs[j] = cdr (p); } return cell_unspecified; } SCM cache_lookup (SCM x) { for (int i=0; i < CACHE_SIZE; i++) { if (!env_cache_cars[i]) break; if (env_cache_cars[i] == x) return env_cache_cdrs[i]; } return cell_undefined; } SCM cache_invalidate (SCM x) { for (int i=0; i < CACHE_SIZE; i++) { if (env_cache_cars[i] == x) { env_cache_cars[i] = 0; break; } } return cell_unspecified; } SCM cache_invalidate_range (SCM p, SCM a) { do { cache_invalidate (caar (p)); p = cdr (p); } while (p != a); return cell_unspecified; } SCM assq_ref_cache (SCM x, SCM a) { g_cells[x].hits++; SCM c = cache_lookup (x); if (c != cell_undefined) return c; int i = 0; while (a != cell_nil && x != CAAR (a)) {i++;a = cdr (a);} if (a == cell_nil) return cell_undefined; if (i>ENV_HEAD) cache_save (car (a)); return cdar (a); } #endif // ENV_CACHE SCM assert_defined (SCM x, SCM e) { if (e == cell_undefined) { fprintf (stderr, "eval: unbound variable:"); display_ (stderr, x); fprintf (stderr, "\n"); assert (!"unbound variable"); } return e; } SCM gc_frame (SCM stack) { SCM frame = car (stack); r1 = car (frame); r2 = cadr (frame); r3 = caddr (frame); r0 = cadddr (frame); return frame; } SCM gc_stack (SCM a) { SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil)))); stack = cons (frame, stack); stack = gc (stack); gc_frame (stack); stack = cdr (stack); return stack; } SCM vm_call (function0_t f, SCM p1, SCM p2, SCM a) { SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil)))); stack = cons (frame, stack); r1 = p1; r2 = p2; r0 = a; if (f == vm_if_env && g_free.value + GC_SAFETY > ARENA_SIZE) { cache_invalidate_range (r0, cell_nil); gc_stack (stack); frame = car (stack); } SCM r = f (); frame = gc_frame (stack); stack = cdr (stack); return r; } SCM evlis_env (SCM m, SCM a) { return vm_call (vm_evlis_env, m, cell_undefined, a); } SCM apply_env (SCM fn, SCM x, SCM a) { return vm_call (vm_apply_env, fn, x, a); } SCM eval_env (SCM e, SCM a) { return vm_call (vm_eval_env, e, cell_undefined, a); } SCM expand_macro_env (SCM e, SCM a) { return vm_call (vm_expand_macro_env, e, cell_undefined, a); } SCM begin_env (SCM e, SCM a) { return vm_call (vm_begin_env, e, cell_undefined, a); } SCM if_env (SCM e, SCM a) { return vm_call (vm_if_env, e, cell_undefined, a); } SCM call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal)) { SCM cl = cons (cons (cell_closure, x), x); r1 = e; r0 = cl; r2 = a; r3 = aa; cache_invalidate_range (r0, g_cells[r3].cdr); SCM r = vm_call_lambda (); cache_invalidate_range (r0, g_cells[r3].cdr); return r; } SCM vm_evlis_env () { if (r1 == cell_nil) return cell_nil; if (type (r1) != PAIR) return eval_env (r1, r0); r2 = eval_env (car (r1), r0); r1 = evlis_env (cdr (r1), r0); return cons (r2, r1); } SCM vm_call_lambda () { return vm_call (vm_begin_env, r1, cell_undefined, r0); } SCM vm_apply_env () { if (type (r1) != PAIR) { if (type (r1) == FUNCTION) return call (r1, r2); if (r1 == cell_symbol_call_with_values) return call_with_values_env (car (r2), cadr (r2), r0); if (r1 == cell_symbol_current_module) return r0; } else if (car (r1) == cell_symbol_lambda) { SCM args = cadr (r1); SCM body = cddr (r1); SCM p = pairlis (args, r2, r0); return call_lambda (body, p, p, r0); // r2 = p; // cache_invalidate_range (r2, g_cells[r0].cdr); // SCM r = begin_env (cddr (r1), cons (cons (cell_closure, p), p)); // cache_invalidate_range (r2, g_cells[r0].cdr); // return r; } else if (car (r1) == cell_closure) { SCM args = caddr (r1); SCM body = cdddr (r1); SCM aa = cdadr (r1); aa = cdr (aa); SCM p = pairlis (args, r2, aa); return call_lambda (body, p, aa, r0); // r2 = p; // r3 = aa; // cache_invalidate_range (r2, g_cells[r3].cdr); // SCM r = begin_env (body, cons (cons (cell_closure, p), p)); // cache_invalidate_range (r2, g_cells[r3].cdr); // return r; } #if BOOT else if (car (r1) == cell_symbol_label) return apply_env (caddr (r1), r2, cons (cons (cadr (r1), caddr (r1)), r0)); #endif SCM e = eval_env (r1, r0); char const* type = 0; if (e == cell_f || e == cell_t) type = "bool"; if (g_cells[e].type == CHAR) type = "char"; if (g_cells[e].type == NUMBER) type = "number"; if (g_cells[e].type == STRING) type = "string"; if (e == cell_unspecified) type = "*unspecified*"; if (e == cell_undefined) type = "*undefined*"; if (type) { fprintf (stderr, "cannot apply: %s: ", type); display_ (stderr, e); fprintf (stderr, " ["); display_ (stderr, r1); fprintf (stderr, "]\n"); assert (!"cannot apply"); } return apply_env (e, r2, r0); } SCM cstring_to_list (char const* s); SCM vm_eval_env () { switch (type (r1)) { case PAIR: { if (car (r1) == cell_symbol_quote) return cadr (r1); #if QUASISYNTAX if (car (r1) == cell_symbol_syntax) return r1; #endif if (car (r1) == cell_symbol_begin) return begin_env (r1, r0); if (car (r1) == cell_symbol_lambda) return make_closure (cadr (r1), cddr (r1), assq (cell_closure, r0)); if (car (r1) == cell_closure) return r1; if (car (r1) == cell_symbol_if) return if_env (cdr (r1), r0); #if !BOOT if (car (r1) == cell_symbol_define) return define_env (r1, r0); if (car (r1) == cell_symbol_define_macro) return define_env (r1, r0); if (car (r1) == cell_symbol_primitive_load) return load_env (r0); #else if (car (r1) == cell_symbol_define) { fprintf (stderr, "C DEFINE: "); display_ (stderr, g_cells[cadr (r1)].type == SYMBOL ? g_cells[cadr (r1)].string : g_cells[caadr (r1)].string); fprintf (stderr, "\n"); } assert (car (r1) != cell_symbol_define); assert (car (r1) != cell_symbol_define_macro); #endif #if 1 //!BOOT if (car (r1) == cell_symbol_set_x) return set_env_x (cadr (r1), eval_env (caddr (r1), r0), r0); #else assert (car (r1) != cell_symbol_set_x); #endif #if QUASIQUOTE if (car (r1) == cell_symbol_unquote) return eval_env (cadr (r1), r0); if (car (r1) == cell_symbol_quasiquote) return eval_quasiquote (cadr (r1), add_unquoters (r0)); #endif //QUASIQUOTE #if QUASISYNTAX if (car (r1) == cell_symbol_unsyntax) return eval_env (cadr (r1), r0); if (car (r1) == cell_symbol_quasisyntax) return eval_quasisyntax (cadr (r1), add_unsyntaxers (r0)); #endif //QUASISYNTAX SCM x = expand_macro_env (r1, r0); if (x != r1) return eval_env (x, r0); SCM m = evlis_env (g_cells[r1].cdr, r0); return apply_env (car (r1), m, r0); } case SYMBOL: return assert_defined (r1, assq_ref_cache (r1, r0)); default: return r1; } } SCM vm_expand_macro_env () { if (TYPE (CAR (r1)) == STRING && string_to_symbol (CAR (r1)) == cell_symbol_noexpand) return cadr (r1); SCM macro; SCM expanders; if (TYPE (r1) == PAIR && (macro = lookup_macro (car (r1), r0)) != cell_f) return apply_env (macro, CDR (r1), r0); else if (TYPE (r1) == PAIR && TYPE (CAR (r1)) == SYMBOL && ((expanders = assq_ref_cache (cell_symbol_sc_expander_alist, r0)) != cell_undefined) && ((macro = assq (CAR (r1), expanders)) != cell_f)) { SCM sc_expand = assq_ref_cache (cell_symbol_expand_macro, r0); if (sc_expand != cell_undefined && sc_expand != cell_f) r1 = apply_env (sc_expand, cons (r1, cell_nil), r0); } return r1; } SCM vm_begin_env () { SCM r = cell_unspecified; while (r1 != cell_nil) { if (g_cells[r1].type == PAIR && g_cells[CAR (r1)].type == PAIR && caar (r1) == cell_symbol_begin) r1 = append2 (cdar (r1), cdr (r1)); r = eval_env (car (r1), r0); r1 = g_cells[r1].cdr; } return r; } SCM vm_if_env () { SCM x = eval_env (car (r1), r0); if (x != cell_f) return eval_env (cadr (r1), r0); if (cddr (r1) != cell_nil) return eval_env (caddr (r1), r0); return cell_unspecified; } //Helpers SCM display (SCM x) ///((arity . n)) { SCM e = car (x); SCM p = cdr (x); int fd = 1; if (g_cells[p].type == PAIR && g_cells[car (p)].type == NUMBER) fd = g_cells[car (p)].hits; FILE *f = fd == 1 ? stdout : stderr; return display_helper (f, e, false, "", false); } SCM display_ (FILE* f, SCM x) { return display_helper (f, x, false, "", false); } SCM call (SCM fn, SCM x) { if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1) && x != cell_nil && TYPE (CAR (x)) == VALUES) x = cons (CADAR (x), CDR (x)); if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1) && x != cell_nil && TYPE (CDR (x)) == PAIR && TYPE (CADR (x)) == VALUES) x = cons (CAR (x), cons (CDADAR (x), CDR (x))); switch (FUNCTION (fn).arity) { case 0: return FUNCTION (fn).function0 (); case 1: return FUNCTION (fn).function1 (car (x)); case 2: return FUNCTION (fn).function2 (car (x), cadr (x)); case 3: return FUNCTION (fn).function3 (car (x), cadr (x), caddr (x)); case -1: return FUNCTION (fn).functionn (x); } return cell_unspecified; } SCM append2 (SCM x, SCM y) { if (x == cell_nil) return y; assert (g_cells[x].type == PAIR); return cons (car (x), append2 (cdr (x), y)); } SCM append (SCM x) ///((arity . n)) { if (x == cell_nil) return cell_nil; return append2 (car (x), append (cdr (x))); } SCM make_char (int x) { g_cells[tmp_num].value = CHAR; g_cells[tmp_num2].value = x; return make_cell (tmp_num, tmp_num2, tmp_num2); } SCM make_function (SCM name, SCM id, SCM arity) { g_cells[tmp_num3].value = FUNCTION; // function fun_read_byte = {.function0=&read_byte, .arity=0}; // scm scm_read_byte = {FUNCTION, .name="read-int", .function=&fun_read_byte}; // SCM cell_read_byte = 93; function *f = (function*)malloc (sizeof (function)); f->arity = VALUE (arity); g_cells[tmp_num4].value = (long)f; return make_cell (tmp_num3, name, tmp_num4); } SCM make_macro (SCM name, SCM x) { g_cells[tmp_num].value = MACRO; return make_cell (tmp_num, STRING (name), x); } SCM make_number (int x) { g_cells[tmp_num].value = NUMBER; g_cells[tmp_num2].value = x; return make_cell (tmp_num, tmp_num2, tmp_num2); } SCM make_ref (SCM x) { g_cells[tmp_num].value = REF; return make_cell (tmp_num, x, x); } SCM make_string (SCM x) { g_cells[tmp_num].value = STRING; return make_cell (tmp_num, x, 0); } SCM cstring_to_list (char const* s) { SCM p = cell_nil; int i = strlen (s); while (i--) p = cons (make_char (s[i]), p); return p; } SCM list_of_char_equal_p (SCM a, SCM b) { while (a != cell_nil && b != cell_nil && g_cells[car (a)].value == g_cells[car (b)].value) { assert (g_cells[car (a)].type == CHAR); assert (g_cells[car (b)].type == CHAR); a = cdr (a); b = cdr (b); } return (a == cell_nil && b == cell_nil) ? cell_t : cell_f; } SCM internal_lookup_symbol (SCM s) { SCM x = symbols; while (x) { // .string and .name is the same field; .name is used as a handy // static field initializer. A string can only be mistaken for a // cell with type == PAIR for the one character long, zero-padded // #\etx. SCM p = g_cells[car (x)].string; char const* n = g_cells[car (x)].name; if (p < 0 || p >= g_free.value || g_cells[p].type != PAIR) g_cells[car (x)].string = cstring_to_list (g_cells[car (x)].name); if (list_of_char_equal_p (g_cells[car (x)].string, s) == cell_t) break; x = cdr (x); } if (x) x = car (x); return x; } SCM internal_make_symbol (SCM s) { g_cells[tmp_num].value = SYMBOL; SCM x = make_cell (tmp_num, s, 0); symbols = cons (x, symbols); return x; } SCM make_symbol (SCM s) { SCM x = internal_lookup_symbol (s); return x ? x : internal_make_symbol (s); } SCM make_vector (SCM n) { int k = VALUE (n); g_cells[tmp_num].value = VECTOR; SCM v = alloc (k); SCM x = make_cell (tmp_num, k, v); for (int i=0; i= g_free.value || g_cells[p].type != PAIR) fprintf (f, "%s", g_cells[x].name); else display_ (f, g_cells[x].string); fprintf (f, ">"); break; } case BROKEN_HEART: fprintf (f, "<3"); break; default: if (STRING (x)) { SCM p = STRING (x); assert (p); while (p != cell_nil) { assert (g_cells[car (p)].type == CHAR); fputc (g_cells[car (p)].value, f); p = cdr (p); } } else if (g_cells[x].type != PAIR && g_cells[x].name) fprintf (f, "%s", g_cells[x].name); } return cell_unspecified; } // READ FILE *g_stdin; int getchar () { return getc (g_stdin); } int ungetchar (int c) { return ungetc (c, g_stdin); } int peekchar () { int c = getchar (); ungetchar (c); return c; } SCM peek_char () { return make_char (peekchar ()); } SCM read_char () { return make_char (getchar ()); } SCM write_char (SCM x) ///((arity . n)) { SCM c = car (x); SCM p = cdr (x); int fd = 1; if (g_cells[p].type == PAIR && g_cells[car (p)].type == NUMBER) fd = g_cells[car (p)].value; FILE *f = fd == 1 ? stdout : stderr; assert (g_cells[c].type == NUMBER || g_cells[c].type == CHAR); fputc (value (c), f); return c; } SCM unget_char (SCM c) { assert (g_cells[c].type == NUMBER || g_cells[c].type == CHAR); ungetchar (value (c)); return c; } int readcomment (int c) { if (c == '\n') return c; return readcomment (getchar ()); } int readblock (int c) { if (c == '!' && peekchar () == '#') return getchar (); return readblock (getchar ()); } SCM readword (int c, SCM w, SCM a) { if (c == EOF && w == cell_nil) return cell_nil; if (c == '\n' && w == cell_nil) return readword (getchar (), w, a); if (c == '\n' && value (car (w)) == '.' && cdr (w) == cell_nil) return cell_dot; if (c == EOF || c == '\n') return lookup (w, a); if (c == ' ') return readword ('\n', w, a); if (c == '"' && w == cell_nil) return readstring (); if (c == '"') {ungetchar (c); return lookup (w, a);} if (c == '(' && w == cell_nil) return readlist (a); if (c == '(') {ungetchar (c); return lookup (w, a);} if (c == ')' && w == cell_nil) {ungetchar (c); return cell_nil;} if (c == ')') {ungetchar (c); return lookup (w, a);} if (c == ',' && peekchar () == '@') {getchar (); return cons (lookup (g_cells[cell_symbol_unquote_splicing].string, a), cons (readword (getchar (), w, a), cell_nil));} if ((c == '\'' || c == '`' || c == ',') && w == cell_nil) {return cons (lookup_char (c, a), cons (readword (getchar (), w, a), cell_nil));} if (c == '#' && peekchar () == ',' && w == cell_nil) { getchar (); if (peekchar () == '@'){getchar (); return cons (lookup (g_cells[cell_symbol_unsyntax_splicing].string, a), cons (readword (getchar (), w, a), cell_nil));} return cons (lookup (g_cells[cell_symbol_unsyntax].string, a), cons (readword (getchar (), w, a), cell_nil)); } if (c == '#' && (peekchar () == '\'' || peekchar () == '`') && w == cell_nil) { c = getchar (); return cons (lookup (cons (make_char ('#'), cons (make_char (c), cell_nil)), a), cons (readword (getchar (), w, a), cell_nil));} if (c == ';') {readcomment (c); return readword ('\n', w, a);} if (c == '#' && peekchar () == 'x') {getchar (); return read_hex ();} if (c == '#' && peekchar () == '\\') {getchar (); return read_character ();} if (c == '#' && w == cell_nil && peekchar () == '(') {getchar (); return list_to_vector (readlist (a));} if (c == '#' && peekchar () == '(') {ungetchar (c); return lookup (w, a);} if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return readword (getchar (), w, a);} return readword (getchar (), append2 (w, cons (make_char (c), cell_nil)), a); } SCM read_hex () { int n = 0; int c = peekchar (); while ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) { n <<= 4; if (c >= 'a') n += c - 'a' + 10; else if (c >= 'A') n += c - 'A' + 10; else n+= c - '0'; getchar (); c = peekchar (); } return make_number (n); } SCM read_character () { int c = getchar (); if (c >= '0' && c <= '7' && peekchar () >= '0' && peekchar () <= '7') { c = c - '0'; while (peekchar () >= '0' && peekchar () <= '7') { c <<= 3; c += getchar () - '0'; } } else if (c >= 'a' && c <= 'z' && peekchar () >= 'a' && peekchar () <= 'z') { char buf[10]; char *p = buf; *p++ = c; while (peekchar () >= 'a' && peekchar () <= 'z') { *p++ = getchar (); } *p = 0; if (!strcmp (buf, char_nul.name)) c = char_nul.value; else if (!strcmp (buf, char_backspace.name)) c = char_backspace.value; else if (!strcmp (buf, char_tab.name)) c = char_tab.value; else if (!strcmp (buf, char_newline.name)) c = char_newline.value; else if (!strcmp (buf, char_vt.name)) c = char_vt.value; else if (!strcmp (buf, char_page.name)) c = char_page.value; else if (!strcmp (buf, char_return.name)) c = char_return.value; else if (!strcmp (buf, char_space.name)) c = char_space.value; else { fprintf (stderr, "char not supported: %s\n", buf); assert (!"char not supported"); } } return make_char (c); } SCM append_char (SCM x, int i) { return append2 (x, cons (make_char (i), cell_nil)); } SCM readstring () { SCM p = cell_nil; int c = getchar (); while (true) { if (c == '"') break; if (c == '\\' && peekchar () == '"') p = append_char (p, getchar ()); else if (c == '\\' && peekchar () == 'n') {getchar (); p = append_char (p, '\n');} else if (c == EOF) assert (!"EOF in string"); else p = append_char (p, c); c = getchar (); } return make_string (p); } int eat_whitespace (int c) { while (c == ' ' || c == '\t' || c == '\n') c = getchar (); if (c == ';') return eat_whitespace (readcomment (c)); if (c == '#' && peekchar () == '!') {getchar (); readblock (getchar ()); return eat_whitespace (getchar ());} return c; } SCM readlist (SCM a) { int c = getchar (); c = eat_whitespace (c); if (c == ')') return cell_nil; SCM w = readword (c, cell_nil, a); if (w == cell_dot) return car (readlist (a)); return cons (w, readlist (a)); } SCM read_env (SCM a) { return readword (getchar (), cell_nil, a); } SCM acons (SCM key, SCM value, SCM alist) { return cons (cons (key, value), alist); } SCM add_environment (SCM a, char const *name, SCM x) { return acons (make_symbol (cstring_to_list (name)), x, a); } SCM mes_environment () ///((internal)) { // setup GC g_cells = (scm *)malloc (ARENA_SIZE*sizeof(scm)); g_cells[0].type = VECTOR; g_cells[0].length = ARENA_SIZE - 1; g_cells[0].length = 10; g_cells[0].vector = 0; g_cells++; // a = add_environment (a, "%free", &g_free); hihi, gets <3 moved // a = add_environment (a, "%the-cells", g_cells); // a = add_environment (a, "%new-cells", g_news); //#include "mes.symbols.i" g_cells[0].type = CHAR; g_cells[0].value = 'c'; g_free.value = 1; // 0 is tricky #if !MES_MINI #include "mes.symbols.i" #else // MES_MINI cell_nil = g_free.value++; g_cells[cell_nil] = scm_nil; cell_f = g_free.value++; g_cells[cell_f] = scm_f; cell_t = g_free.value++; g_cells[cell_t] = scm_t; cell_undefined = g_free.value++; g_cells[cell_undefined] = scm_undefined; cell_unspecified = g_free.value++; g_cells[cell_unspecified] = scm_unspecified; cell_closure = g_free.value++; g_cells[cell_closure] = scm_closure; cell_begin = g_free.value++; g_cells[cell_begin] = scm_begin; cell_symbol_begin = g_free.value++; g_cells[cell_symbol_begin] = scm_symbol_begin; cell_symbol_sc_expander_alist = g_free.value++; g_cells[cell_symbol_sc_expander_alist] = scm_symbol_sc_expander_alist; cell_symbol_sc_expand = g_free.value++; g_cells[cell_symbol_sc_expand] = scm_symbol_sc_expand; // cell_dot = g_free.value++; // g_cells[cell_dot] = scm_dot; // cell_circular = g_free.value++; // g_cells[cell_circular] = scm_circular; // cell_symbol_lambda = g_free.value++; // g_cells[cell_symbol_lambda] = scm_symbol_lambda; // cell_symbol_if = g_free.value++; // g_cells[cell_symbol_if] = scm_symbol_if; // cell_symbol_define = g_free.value++; // g_cells[cell_symbol_define] = scm_symbol_define; // cell_symbol_define_macro = g_free.value++; // g_cells[cell_symbol_define_macro] = scm_symbol_define_macro; #endif // MES_MINI SCM symbol_max = g_free.value; #if MES_FULL #include "define.i" #include "lib.i" #include "math.i" #include "mes.i" #include "posix.i" #include "quasiquote.i" #include "string.i" #include "type.i" #else cell_cons = g_free.value++; cell_display = g_free.value++; cell_eq_p = g_free.value++; cell_newline = g_free.value++; g_cells[cell_cons] = scm_cons; g_cells[cell_display] = scm_display; g_cells[cell_eq_p] = scm_eq_p; g_cells[cell_newline] = scm_newline; cell_make_vector = g_free.value++; g_cells[cell_make_vector] = scm_make_vector; #endif tmp = g_free.value++; tmp_num = g_free.value++; g_cells[tmp_num].type = NUMBER; tmp_num2 = g_free.value++; g_cells[tmp_num2].type = NUMBER; g_start = g_free.value; symbols = 0; for (int i=1; i 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes < FILE\n"); if (argc > 1 && !strcmp (argv[1], "--version")) return puts ("Mes 0.2\n"); g_stdin = stdin; SCM a = mes_environment (); display_ (stderr, load_env (a)); fputs ("", stderr); fprintf (stderr, "\nstats: [%d]\n", g_free.value); return 0; }