core: Split-off cache, display, reader.

* mes.c: Remove cache, display, reader functions.
* cache.c: New file.
* display.c: New file.
* reader.c: New file.
This commit is contained in:
Jan Nieuwenhuizen 2016-11-21 09:36:32 +01:00
parent 1614d13439
commit f26c7222b2
8 changed files with 791 additions and 733 deletions

View file

@ -5,10 +5,10 @@ default: all
./configure
OUT:=out
#CFLAGS:=-std=c99 -O3 -finline-functions
CFLAGS:=-std=c99 -O3 -finline-functions
#CFLAGS:=-std=c99 -O0
#CFLAGS:=-pg -std=c99 -O0
CFLAGS:=-std=c99 -O0 -g
#CFLAGS:=-std=c99 -O0 -g
export BOOT
ifneq ($(BOOT),)
@ -24,11 +24,14 @@ all: mes
mes.o: mes.c
mes.o: mes.c mes.h mes.i mes.environment.i mes.symbols.i
mes.o: cache.c cache.h cache.i cache.environment.i
mes.o: define.c define.h define.i define.environment.i
mes.o: display.c display.h display.i display.environment.i
mes.o: lib.c lib.h lib.i lib.environment.i
mes.o: math.c math.h math.i math.environment.i
mes.o: posix.c posix.h posix.i posix.environment.i
mes.o: quasiquote.c quasiquote.h quasiquote.i quasiquote.environment.i
mes.o: reader.c reader.h reader.i reader.environment.i
mes.o: string.c string.h string.i string.environment.i
mes.o: type.c type.h type.i type.environment.i

103
cache.c Normal file
View file

@ -0,0 +1,103 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#define CACHE_SIZE 30
#define ENV_HEAD 15
#if! ENV_CACHE
SCM cache_invalidate (SCM x){}
SCM cache_invalidate_range (SCM p,SCM a){}
SCM cache_save (SCM p){}
SCM cache_lookup (SCM x){}
#else // ENV_CACHE
SCM env_cache_cars[CACHE_SIZE];
SCM env_cache_cdrs[CACHE_SIZE];
int cache_threshold = 0;
SCM
cache_save (SCM p)
{
int n = g_cells[car (p)].hits;
if (n < cache_threshold) return cell_unspecified;
int j = -1;
for (int i=0; i < CACHE_SIZE; i++) {
if (!env_cache_cars[i]) {
j = i;
break;
}
if (env_cache_cars[i] == car (p)) return cell_unspecified;
if (n > 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) ///((internal))
{
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

144
display.c Normal file
View file

@ -0,0 +1,144 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
SCM display_helper (FILE*, SCM , bool, char const*, bool);
SCM
display (SCM x) ///((arity . n))
{
SCM e = car (x);
SCM p = cdr (x);
int fd = 1;
if (TYPE (p) == PAIR && TYPE (car (p)) == NUMBER) fd = HITS (car (p));
FILE *f = fd == 1 ? stdout : stderr;
return display_helper (f, e, false, "", false);
}
SCM
newline (SCM p) ///((arity . n))
{
int fd = 1;
if (TYPE (p) == PAIR && TYPE (car (p)) == NUMBER) fd = VALUE (car (p));
FILE *f = fd == 1 ? stdout : stderr;
fputs ("\n", f);
return cell_unspecified;
}
SCM
display_ (FILE* f, SCM x)
{
return display_helper (f, x, false, "", false);
}
SCM
display_helper (FILE* f, SCM x, bool cont, char const *sep, bool quote)
{
SCM r;
fprintf (f, "%s", sep);
switch (TYPE (x))
{
case CHAR:
{
char const *name = 0;
if (VALUE (x) == char_nul.value) name = char_nul.name;
else if (VALUE (x) == char_backspace.value) name = char_backspace.name;
else if (VALUE (x) == char_tab.value) name = char_tab.name;
else if (VALUE (x) == char_newline.value) name = char_newline.name;
else if (VALUE (x) == char_vt.value) name = char_vt.name;
else if (VALUE (x) == char_page.value) name = char_page.name;
else if (VALUE (x) == char_return.value) name = char_return.name;
else if (VALUE (x) == char_space.value) name = char_space.name;
if (name) fprintf (f, "#\\%s", name);
else fprintf (f, "#\\%c", VALUE (x));
break;
}
case MACRO:
fprintf (f, "(*macro* ");
display_helper (f, g_cells[x].macro, cont, sep, quote);
fprintf (f, ")");
break;
case NUMBER: fprintf (f, "%d", VALUE (x)); break;
case PAIR:
{
if (car (x) == cell_circular) {
fprintf (f, "(*circ* . #-1#)");
return cell_unspecified;
}
if (car (x) == cell_closure) {
fprintf (f, "(*closure* . #-1#)");
return cell_unspecified;
}
if (car (x) == cell_symbol_quote) {
fprintf (f, "'");
return display_helper (f, car (cdr (x)), cont, "", true);
}
if (!cont) fprintf (f, "(");
display_ (f, car (x));
if (cdr (x) && TYPE (cdr (x)) == PAIR)
display_helper (f, cdr (x), true, " ", false);
else if (cdr (x) != cell_nil) {
fprintf (f, " . ");
display_ (f, cdr (x));
}
if (!cont) fprintf (f, ")");
break;
}
case VECTOR:
{
fprintf (f, "#(");
for (int i = 0; i < LENGTH (x); i++) {
if (TYPE (VECTOR (x)+i) == VECTOR
|| (TYPE (VECTOR (x)+i) == REF
&& TYPE (REF (VECTOR (x)+i)) == VECTOR))
fprintf (f, "%s#(...)", i ? " " : "");
else
display_helper (f,VECTOR (x)+i, false, i ? " " : "", false);
}
fprintf (f, ")");
break;
}
case REF: display_helper (f, g_cells[x].ref, cont, "", true); break;
case FUNCTION:
{
fprintf (f, "#<procedure ");
SCM p = STRING (x);
if (p < 0 || p >= g_free.value || TYPE (p) != PAIR)
fprintf (f, "%s", NAME (x));
else
display_ (f, STRING (x));
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 (TYPE (car (p)) == CHAR);
fputc (VALUE (car (p)), f);
p = cdr (p);
}
}
else if (TYPE (x) != PAIR && NAME (x)) fprintf (f, "%s", NAME (x));
}
return cell_unspecified;
}

941
mes.c

File diff suppressed because it is too large Load diff

View file

@ -55,6 +55,7 @@
(mes-use-module (mes elf))
(mes-use-module (mes libc-i386))))
(gc)
(define c-parser
(lalr-parser

View file

@ -35,6 +35,15 @@ string_to_cstring (SCM s)
return buf;
}
SCM
force_output (SCM p) ///((arity . n))
{
int fd = 1;
if (TYPE (p) == PAIR && TYPE (car (p)) == NUMBER) fd = VALUE (car (p));
FILE *f = fd == 1 ? stdout : stderr;
fflush (f);
}
SCM
open_input_file (SCM file_name)
{

287
reader.c Normal file
View file

@ -0,0 +1,287 @@
/* -*-comment-start: "//";comment-end:""-*-
* Mes --- Maxwell Equations of Software
* Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
SCM
peek_char ()
{
return make_char (peekchar ());
}
SCM
read_char ()
{
return make_char (getchar ());
}
SCM
unread_char (SCM c)
{
return ungetchar (VALUE (c));
}
SCM
unget_char (SCM c)
{
assert (TYPE (c) == NUMBER || TYPE (c) == CHAR);
ungetchar (VALUE (c));
return c;
}
int
read_line_comment (int c)
{
if (c == '\n') return c;
return read_line_comment (getchar ());
}
int
read_block_comment (int c)
{
if (c == '!' && peekchar () == '#') return getchar ();
return read_block_comment (getchar ());
}
SCM lookup_char (int c, SCM a);
SCM
read_word (int c, SCM w, SCM a)
{
if (c == EOF && w == cell_nil) return cell_nil;
if (c == '\n' && w == cell_nil) return read_word (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 read_word ('\n', w, a);
if (c == '"' && w == cell_nil) return read_string ();
if (c == '"') {ungetchar (c); return lookup (w, a);}
if (c == '(' && w == cell_nil) return read_list (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 (STRING (cell_symbol_unquote_splicing), a),
cons (read_word (getchar (), w, a),
cell_nil));}
if ((c == '\''
|| c == '`'
|| c == ',')
&& w == cell_nil) {return cons (lookup_char (c, a),
cons (read_word (getchar (), w, a),
cell_nil));}
if (c == '#' && peekchar () == ',' && w == cell_nil) {
getchar ();
if (peekchar () == '@'){getchar (); return cons (lookup (STRING (cell_symbol_unsyntax_splicing), a),
cons (read_word (getchar (), w, a),
cell_nil));}
return cons (lookup (STRING (cell_symbol_unsyntax), a), cons (read_word (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 (read_word (getchar (), w, a), cell_nil));}
if (c == ';') {read_line_comment (c); return read_word ('\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 (read_list (a));}
if (c == '#' && peekchar () == '!') {getchar (); read_block_comment (getchar ()); return read_word (getchar (), w, a);}
return read_word (getchar (), append2 (w, cons (make_char (c), cell_nil)), a);
}
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
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
append_char (SCM x, int i)
{
return append2 (x, cons (make_char (i), cell_nil));
}
SCM
read_string ()
{
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 (read_line_comment (c));
if (c == '#' && peekchar () == '!') {getchar (); read_block_comment (getchar ()); return eat_whitespace (getchar ());}
return c;
}
SCM
read_list (SCM a)
{
int c = getchar ();
c = eat_whitespace (c);
if (c == ')') return cell_nil;
SCM w = read_word (c, cell_nil, a);
if (w == cell_dot)
return car (read_list (a));
return cons (w, read_list (a));
}
SCM
read_env (SCM a)
{
return read_word (getchar (), cell_nil, a);
}
SCM
lookup (SCM s, SCM a)
{
if (isdigit (VALUE (car (s))) || (VALUE (car (s)) == '-' && cdr (s) != cell_nil)) {
SCM p = s;
int sign = 1;
if (VALUE (car (s)) == '-') {
sign = -1;
p = cdr (s);
}
int n = 0;
while (p != cell_nil && isdigit (VALUE (car (p)))) {
n *= 10;
n += VALUE (car (p)) - '0';
p = cdr (p);
}
if (p == cell_nil) return make_number (n * sign);
}
SCM x = internal_lookup_symbol (s);
if (x) return x;
if (cdr (s) == cell_nil) {
if (VALUE (car (s)) == '\'') return cell_symbol_quote;
if (VALUE (car (s)) == '`') return cell_symbol_quasiquote;
if (VALUE (car (s)) == ',') return cell_symbol_unquote;
}
else if (cddr (s) == cell_nil) {
if (VALUE (car (s)) == ',' && VALUE (cadr (s)) == '@') return cell_symbol_unquote_splicing;
if (VALUE (car (s)) == '#' && VALUE (cadr (s)) == '\'') return cell_symbol_syntax;
if (VALUE (car (s)) == '#' && VALUE (cadr (s)) == '`') return cell_symbol_quasisyntax;
if (VALUE (car (s)) == '#' && VALUE (cadr (s)) == ',') return cell_symbol_unsyntax;
}
else if (cdddr (s) == cell_nil) {
if (VALUE (car (s)) == '#' && VALUE (cadr (s)) == ',' && VALUE (caddr (s)) == '@') return cell_symbol_unsyntax_splicing;
if (VALUE (car (s)) == 'E' && VALUE (cadr (s)) == 'O' && VALUE (caddr (s)) == 'F') {
fprintf (stderr, "mes: got EOF\n");
return cell_nil; // `EOF': eval program, which may read stdin
}
}
return internal_make_symbol (s);
}
SCM
lookup_char (int c, SCM a)
{
return lookup (cons (make_char (c), cell_nil), a);
}
SCM
list_of_char_equal_p (SCM a, SCM b)
{
while (a != cell_nil && b != cell_nil && VALUE (car (a)) == VALUE (car (b))) {
assert (TYPE (car (a)) == CHAR);
assert (TYPE (car (b)) == 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 = STRING (car (x));
if (p < 0 || p >= g_free.value || TYPE (p) != PAIR)
STRING (car (x)) = cstring_to_list (NAME (car (x)));
if (list_of_char_equal_p (STRING (car (x)), s) == cell_t) break;
x = cdr (x);
}
if (x) x = car (x);
return x;
}

View file

@ -75,7 +75,7 @@ exit $?
(define local-answer 41))
(pass-if-equal "begin 2" 41 (begin local-answer))
;; (if (not guile?)
;; (pass-if-equal "load" 42 (begin (load "tests/data/load.scm") the-answer)))
(if (not guile?)
(pass-if-equal "load" 42 (begin (load "tests/data/load.scm") the-answer)))
(result 'report)