mini-mes: Merge with mes.c: lib.c, math.c, posix.c.

* mes.c: Include math.c after posix.c.
  (assert_defined, check_formals, check_apply, load_env,
  bload_env): Move from lib.c
* scaffold/mini-mes.c: Include mini-lib.h, lib.c., mini-math.h,
  math.c, mini-posix.h, posix.c.
  (greater_p, less_p, is_p, minus, plus, divide, modulo, multiply,
  logior, ash): Remove.
  (ungetchar, peekchar, peek_byte, read_byte, write_byte,
  string_to_cstring, getenv_, open_input_file, current_input_port,
  set_current_input_port, force_output): Remove.
  (mes_builtins): include mini-lib.i, mini-lib.environment.i.
  mini-math.i, mini-math.environment.i mini-posix.i,
  mini-posix.environment.i.
* GNUmakefile (guile-mini-mes): Add dependencies.
This commit is contained in:
Jan Nieuwenhuizen 2017-04-08 15:01:24 +02:00
parent 2deca502ed
commit da3ccf9703
6 changed files with 250 additions and 640 deletions

View file

@ -180,6 +180,9 @@ mini-mes: scaffold/mini-mes.c
guile-mini-mes: module/language/c99/compiler.mes # and others... guile-mini-mes: module/language/c99/compiler.mes # and others...
guile-mini-mes: mini-mes.h mini-mes.i mini-mes.environment.i mini-mes.symbols.i guile-mini-mes: mini-mes.h mini-mes.i mini-mes.environment.i mini-mes.symbols.i
guile-mini-mes: gc.c mini-gc.h mini-gc.i mini-gc.environment.i guile-mini-mes: gc.c mini-gc.h mini-gc.i mini-gc.environment.i
guile-mini-mes: lib.c mini-lib.h mini-lib.i mini-lib.environment.i
guile-mini-mes: math.c mini-math.h mini-math.i mini-math.environment.i
guile-mini-mes: posix.c mini-posix.h mini-posix.i mini-posix.environment.i
guile-mini-mes: vector.c mini-vector.h mini-vector.i mini-vector.environment.i guile-mini-mes: vector.c mini-vector.h mini-vector.i mini-vector.environment.i
guile-mini-mes: mlibc.c mstart.c guile-mini-mes: mlibc.c mstart.c
guile-mini-mes: GNUmakefile guile-mini-mes: GNUmakefile

276
lib.c
View file

@ -18,31 +18,63 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
//MINI_MES
// SCM
// length (SCM x)
// {
// int n = 0;
// while (x != cell_nil)
// {
// n++;
// if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
// x = cdr (x);
// }
// return MAKE_NUMBER (n);
// }
SCM fdisplay_ (SCM,FILE*);
int g_depth; int g_depth;
#if _POSIX_SOURCE
char const*
itoa (int x)
{
static char buf[10];
char *p = buf+9;
*p-- = 0;
int sign = x < 0;
if (sign)
x = -x;
do
{
*p-- = '0' + (x % 10);
x = x / 10;
} while (x);
if (sign)
*p-- = '-';
return p+1;
}
// from mlib.c
#define fputs fdputs
int
fdputs (char const* s, int fd)
{
int i = strlen (s);
write (fd, s, i);
return 0;
}
#ifdef putc
#undef putc
#endif
#define putc fdputc
int
fdputc (int c, int fd)
{
write (fd, (char*)&c, 1);
return 0;
}
#endif
SCM fdisplay_ (SCM, int);
SCM SCM
display_helper (SCM x, int cont, char* sep, FILE *fd) display_helper (SCM x, int cont, char* sep, int fd)
{ {
fputs (sep, fd); fputs (sep, fd);
if (g_depth == 0) return cell_unspecified; if (g_depth == 0) return cell_unspecified;
g_depth = g_depth - 1; g_depth = g_depth - 1;
switch (TYPE (x)) switch (TYPE (x))
{ {
case TCHAR: case TCHAR:
@ -54,7 +86,11 @@ display_helper (SCM x, int cont, char* sep, FILE *fd)
case TFUNCTION: case TFUNCTION:
{ {
fputs ("#<procedure ", fd); fputs ("#<procedure ", fd);
#if __GNUC__
char const *p = "?"; char const *p = "?";
#else
char *p = "?";
#endif
if (FUNCTION (x).name != 0) if (FUNCTION (x).name != 0)
p = FUNCTION (x).name; p = FUNCTION (x).name;
fputs (p, fd); fputs (p, fd);
@ -93,7 +129,7 @@ display_helper (SCM x, int cont, char* sep, FILE *fd)
break; break;
} }
case TSPECIAL: case TSPECIAL:
#if __NYACC__ #if __MESC__
// FIXME // FIXME
//{} //{}
{ {
@ -107,7 +143,7 @@ display_helper (SCM x, int cont, char* sep, FILE *fd)
} }
#endif #endif
case TSTRING: case TSTRING:
#if __NYACC__ #if __MESC__
// FIXME // FIXME
{ {
SCM t = CAR (x); SCM t = CAR (x);
@ -146,18 +182,18 @@ SCM
display_ (SCM x) display_ (SCM x)
{ {
g_depth = 5; g_depth = 5;
return display_helper (x, 0, "", stdout); return display_helper (x, 0, "", STDOUT);
} }
SCM SCM
display_error_ (SCM x) display_error_ (SCM x)
{ {
g_depth = 5; g_depth = 5;
return display_helper (x, 0, "", stderr); return display_helper (x, 0, "", STDERR);
} }
SCM SCM
fdisplay_ (SCM x, FILE *fd) ///((internal)) fdisplay_ (SCM x, int fd) ///((internal))
{ {
g_depth = 5; g_depth = 5;
return display_helper (x, 0, "", fd); return display_helper (x, 0, "", fd);
@ -170,198 +206,14 @@ exit_ (SCM x) ///((name . "exit"))
exit (VALUE (x)); exit (VALUE (x));
} }
//MINI_MES
// char const*
// string_to_cstring (SCM s)
// {
// static char buf[1024];
// char *p = buf;
// s = STRING (s);
// while (s != cell_nil)
// {
// *p++ = VALUE (car (s));
// s = cdr (s);
// }
// *p = 0;
// return buf;
// }
// SCM
// error (SCM key, SCM x)
// {
// SCM throw;
// if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
// return apply (throw, cons (key, cons (x, cell_nil)), r0);
// assert (!"error");
// }
SCM
assert_defined (SCM x, SCM e) ///((internal))
{
if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
return e;
}
SCM
check_formals (SCM f, SCM formals, SCM args) ///((internal))
{
int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
int alen = VALUE (length (args));
if (alen != flen && alen != -1 && flen != -1)
{
char buf[1024];
sprintf (buf, "apply: wrong number of arguments; expected: %d, got: %d: ", flen, alen);
SCM e = MAKE_STRING (cstring_to_list (buf));
return error (cell_symbol_wrong_number_of_args, cons (e, f));
}
return cell_unspecified;
}
SCM
check_apply (SCM f, SCM e) ///((internal))
{
char const* type = 0;
if (f == cell_f || f == cell_t) type = "bool";
if (f == cell_nil) type = "nil";
if (f == cell_unspecified) type = "*unspecified*";
if (f == cell_undefined) type = "*undefined*";
if (TYPE (f) == TCHAR) type = "char";
if (TYPE (f) == TNUMBER) type = "number";
if (TYPE (f) == TSTRING) type = "string";
if (type)
{
char buf[1024];
sprintf (buf, "cannot apply: %s:", type);
fprintf (stderr, " [");
display_error_ (e);
fprintf (stderr, "]\n");
SCM e = MAKE_STRING (cstring_to_list (buf));
return error (cell_symbol_wrong_type_arg, cons (e, f));
}
return cell_unspecified;
}
char const*
itoa (int x)
{
static char buf[10];
char *p = buf+9;
*p-- = 0;
int sign = x < 0;
if (sign)
x = -x;
do
{
*p-- = '0' + (x % 10);
x = x / 10;
} while (x);
if (sign)
*p-- = '-';
return p+1;
}
//FILE *g_stdin;
int
dump ()
{
fputs ("program r2=", stderr);
display_error_ (r2);
fputs ("\n", stderr);
r1 = g_symbols;
gc_push_frame ();
gc ();
gc_peek_frame ();
char *p = (char*)g_cells;
fputc ('M', stdout);
fputc ('E', stdout);
fputc ('S', stdout);
fputc (g_stack >> 8, stdout);
fputc (g_stack % 256, stdout);
// See HACKING, simple crafted dump for tiny-mes.c
if (getenv ("MES_TINY"))
{
TYPE (9) = 0x2d2d2d2d;
CAR (9) = 0x2d2d2d2d;
CDR (9) = 0x3e3e3e3e;
TYPE (10) = TPAIR;
CAR (10) = 11;
CDR (10) = 12;
TYPE (11) = TCHAR;
CAR (11) = 0x58585858;
CDR (11) = 65;
TYPE (12) = TPAIR;
CAR (12) = 13;
CDR (12) = 1;
TYPE (13) = TCHAR;
CAR (11) = 0x58585858;
CDR (13) = 66;
TYPE (14) = 0x3c3c3c3c;
CAR (14) = 0x2d2d2d2d;
CDR (14) = 0x2d2d2d2d;
g_free = 15;
}
for (int i=0; i<g_free * sizeof(struct scm); i++)
fputc (*p++, stdout);
return 0;
}
SCM
load_env (SCM a) ///((internal))
{
r0 = a;
g_stdin = open ("module/mes/read-0.mes", O_RDONLY);
g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mes", O_RDONLY);
if (!g_function) r0 = mes_builtins (r0);
r2 = read_input_file_env (r0);
g_stdin = STDIN;
return r2;
}
SCM
bload_env (SCM a) ///((internal))
{
#if MES_MINI
g_stdin = fopen ("module/mes/read-0-32.mo", O_RDONLY);
#else
g_stdin = open ("module/mes/read-0.mo", O_RDONLY);
g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mo", O_RDONLY);
#endif
char *p = (char*)g_cells;
assert (getchar () == 'M');
assert (getchar () == 'E');
assert (getchar () == 'S');
g_stack = getchar () << 8;
g_stack += getchar ();
int c = getchar ();
while (c != EOF)
{
*p++ = c;
c = getchar ();
}
g_free = (p-(char*)g_cells) / sizeof (struct scm);
gc_peek_frame ();
g_symbols = r1;
g_stdin = STDIN;
r0 = mes_builtins (r0);
return r2;
}
SCM SCM
xassq (SCM x, SCM a) ///for speed in core only xassq (SCM x, SCM a) ///for speed in core only
{ {
while (a != cell_nil && x != CDAR (a)) a = CDR (a); while (a != cell_nil && x != CDAR (a)) a = CDR (a);
return a != cell_nil ? CAR (a) : cell_f; return a != cell_nil ? CAR (a) : cell_f;
} }
#if _POSIX_SOURCE
#undef fdputs
#undef fdputc
#endif

92
mes.c
View file

@ -1081,13 +1081,103 @@ mes_environment () ///((internal))
} }
int g_stdin; int g_stdin;
#include "math.c"
#include "posix.c" #include "posix.c"
#include "math.c"
#include "lib.c" #include "lib.c"
#include "reader.c" #include "reader.c"
#include "gc.c" #include "gc.c"
#include "vector.c" #include "vector.c"
// extra lib
SCM
assert_defined (SCM x, SCM e) ///((internal))
{
if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
return e;
}
SCM
check_formals (SCM f, SCM formals, SCM args) ///((internal))
{
int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
int alen = VALUE (length (args));
if (alen != flen && alen != -1 && flen != -1)
{
char buf[1024];
sprintf (buf, "apply: wrong number of arguments; expected: %d, got: %d: ", flen, alen);
SCM e = MAKE_STRING (cstring_to_list (buf));
return error (cell_symbol_wrong_number_of_args, cons (e, f));
}
return cell_unspecified;
}
SCM
check_apply (SCM f, SCM e) ///((internal))
{
char const* type = 0;
if (f == cell_f || f == cell_t) type = "bool";
if (f == cell_nil) type = "nil";
if (f == cell_unspecified) type = "*unspecified*";
if (f == cell_undefined) type = "*undefined*";
if (TYPE (f) == TCHAR) type = "char";
if (TYPE (f) == TNUMBER) type = "number";
if (TYPE (f) == TSTRING) type = "string";
if (type)
{
char buf[1024];
sprintf (buf, "cannot apply: %s:", type);
fprintf (stderr, " [");
display_error_ (e);
fprintf (stderr, "]\n");
SCM e = MAKE_STRING (cstring_to_list (buf));
return error (cell_symbol_wrong_type_arg, cons (e, f));
}
return cell_unspecified;
}
SCM
load_env (SCM a) ///((internal))
{
r0 = a;
g_stdin = open ("module/mes/read-0.mes", O_RDONLY);
g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mes", O_RDONLY);
if (!g_function) r0 = mes_builtins (r0);
r2 = read_input_file_env (r0);
g_stdin = STDIN;
return r2;
}
SCM
bload_env (SCM a) ///((internal))
{
#if MES_MINI
g_stdin = fopen ("module/mes/read-0-32.mo", O_RDONLY);
#else
g_stdin = open ("module/mes/read-0.mo", O_RDONLY);
g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mo", O_RDONLY);
#endif
char *p = (char*)g_cells;
assert (getchar () == 'M');
assert (getchar () == 'E');
assert (getchar () == 'S');
g_stack = getchar () << 8;
g_stack += getchar ();
int c = getchar ();
while (c != EOF)
{
*p++ = c;
c = getchar ();
}
g_free = (p-(char*)g_cells) / sizeof (struct scm);
gc_peek_frame ();
g_symbols = r1;
g_stdin = STDIN;
r0 = mes_builtins (r0);
return r2;
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {

32
posix.c
View file

@ -18,18 +18,13 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
#if 0 int g_stdin;
#include <fcntl.h>
FILE *g_stdin;
#else
#if _POSIX_SOURCE #if _POSIX_SOURCE
int open (char const *s, int mode); int open (char const *s, int mode);
int read (int fd, void* buf, size_t n); int read (int fd, void* buf, size_t n);
void write (int fd, char const* s, int n); void write (int fd, char const* s, int n);
#endif
int g_stdin;
#define O_RDONLY 0 #define O_RDONLY 0
#define STDIN 0 #define STDIN 0
@ -65,8 +60,9 @@ getchar ()
return i; return i;
} }
#define ungetc fdungetc
int int
fd_ungetc (int c, int fd) fdungetc (int c, int fd)
{ {
assert (ungetc_char < 2); assert (ungetc_char < 2);
ungetc_buf[++ungetc_char] = c; ungetc_buf[++ungetc_char] = c;
@ -77,7 +73,7 @@ fd_ungetc (int c, int fd)
int int
ungetchar (int c) ungetchar (int c)
{ {
return fd_ungetc (c, g_stdin); return ungetc (c, g_stdin);
} }
int int
@ -114,7 +110,8 @@ write_byte (SCM x) ///((arity . n))
SCM p = cdr (x); SCM p = cdr (x);
int fd = 1; int fd = 1;
if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER) fd = VALUE (car (p)); if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER) fd = VALUE (car (p));
#if _POSIX_SOURCE #if 0
//_POSIX_SOURCE
FILE *f = fd == 1 ? stdout : stderr; FILE *f = fd == 1 ? stdout : stderr;
fputc (VALUE (c), f); fputc (VALUE (c), f);
#else #else
@ -127,11 +124,13 @@ write_byte (SCM x) ///((arity . n))
return c; return c;
} }
char string_to_cstring_buf[1024];
char const* char const*
string_to_cstring (SCM s) string_to_cstring (SCM s)
{ {
static char buf[1024]; //static char buf[1024];
char *p = buf; //char *p = buf;
char *p = string_to_cstring_buf;
s = STRING(s); s = STRING(s);
while (s != cell_nil) while (s != cell_nil)
{ {
@ -139,14 +138,19 @@ string_to_cstring (SCM s)
s = cdr (s); s = cdr (s);
} }
*p = 0; *p = 0;
return buf; //return buf;
return string_to_cstring_buf;
} }
SCM SCM
getenv_ (SCM s) ///((name . "getenv")) getenv_ (SCM s) ///((name . "getenv"))
{ {
#if _POSIX_SOURCE
char *p = getenv (string_to_cstring (s)); char *p = getenv (string_to_cstring (s));
return p ? MAKE_STRING (cstring_to_list (p)) : cell_f; return p ? MAKE_STRING (cstring_to_list (p)) : cell_f;
#else
return cell_t;
#endif
} }
SCM SCM
@ -164,18 +168,20 @@ current_input_port ()
SCM SCM
set_current_input_port (SCM port) set_current_input_port (SCM port)
{ {
g_stdin = VALUE (port); g_stdin = VALUE (port) ? VALUE (port) : STDIN;
return current_input_port (); return current_input_port ();
} }
SCM SCM
force_output (SCM p) ///((arity . n)) force_output (SCM p) ///((arity . n))
{ {
#if 0
int fd = 1; int fd = 1;
if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER) fd = VALUE (car (p)); if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER) fd = VALUE (car (p));
#if _POSIX_SOURCE #if _POSIX_SOURCE
FILE *f = fd == 1 ? stdout : stderr; FILE *f = fd == 1 ? stdout : stderr;
fflush (f); fflush (f);
#endif
#endif #endif
return cell_unspecified; return cell_unspecified;
} }

View file

@ -18,6 +18,12 @@
* along with Mes. If not, see <http://www.gnu.org/licenses/>. * along with Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
#if _POSIX_SOURCE
#undef fputs
#undef fdputs
#undef fdputc
#endif
SCM SCM
___end_of_mes___ () ___end_of_mes___ ()
{ {
@ -112,3 +118,55 @@ lookup_ (SCM s, SCM a)
return lookup_symbol_ (s); return lookup_symbol_ (s);
} }
//FILE *g_stdin;
int
dump ()
{
fputs ("program r2=", stderr);
display_error_ (r2);
fputs ("\n", stderr);
r1 = g_symbols;
gc_push_frame ();
gc ();
gc_peek_frame ();
char *p = (char*)g_cells;
fputc ('M', stdout);
fputc ('E', stdout);
fputc ('S', stdout);
fputc (g_stack >> 8, stdout);
fputc (g_stack % 256, stdout);
// See HACKING, simple crafted dump for tiny-mes.c
if (getenv ("MES_TINY"))
{
TYPE (9) = 0x2d2d2d2d;
CAR (9) = 0x2d2d2d2d;
CDR (9) = 0x3e3e3e3e;
TYPE (10) = TPAIR;
CAR (10) = 11;
CDR (10) = 12;
TYPE (11) = TCHAR;
CAR (11) = 0x58585858;
CDR (11) = 65;
TYPE (12) = TPAIR;
CAR (12) = 13;
CDR (12) = 1;
TYPE (13) = TCHAR;
CAR (11) = 0x58585858;
CDR (13) = 66;
TYPE (14) = 0x3c3c3c3c;
CAR (14) = 0x2d2d2d2d;
CDR (14) = 0x2d2d2d2d;
g_free = 15;
}
for (int i=0; i<g_free * sizeof(struct scm); i++)
fputc (*p++, stdout);
return 0;
}

View file

@ -169,11 +169,11 @@ struct function g_functions[200];
int g_function = 0; int g_function = 0;
#include "mini-gc.h" #include "mini-gc.h"
// #include "lib.h" #include "mini-lib.h"
// #include "math.h" #include "mini-math.h"
#include "mini-mes.h" #include "mini-mes.h"
// #include "posix.h" #include "mini-posix.h"
// #include "reader.h" // #include "mini-reader.h"
#include "mini-vector.h" #include "mini-vector.h"
@ -1023,404 +1023,9 @@ make_tmps (struct scm* cells)
return 0; return 0;
} }
// Posix #include "posix.c"
int #include "math.c"
ungetchar (int c) #include "lib.c"
{
return ungetc (c, g_stdin);
}
int
peekchar ()
{
int c = getchar ();
ungetchar (c);
return c;
}
SCM
peek_byte ()
{
return MAKE_NUMBER (peekchar ());
}
SCM
read_byte ()
{
return MAKE_NUMBER (getchar ());
}
SCM
unread_byte (SCM i)
{
ungetchar (VALUE (i));
return i;
}
SCM
write_byte (SCM x) ///((arity . n))
{
SCM c = car (x);
SCM p = cdr (x);
int fd = 1;
if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER) fd = VALUE (car (p));
//FILE *f = fd == 1 ? stdout : stderr;
#if __GNUC__
assert (TYPE (c) == TNUMBER || TYPE (c) == TCHAR);
#endif
// fputc (VALUE (c), f);
char cc = VALUE (c);
write (1, (char*)&cc, fd);
return c;
}
char string_to_cstring_buf[1024];
char const*
string_to_cstring (SCM s)
{
//static char buf[1024];
//char *p = buf;
char *p = string_to_cstring_buf;
s = STRING(s);
while (s != cell_nil)
{
*p++ = VALUE (car (s));
s = cdr (s);
}
*p = 0;
//return buf;
return string_to_cstring_buf;
}
SCM
getenv_ (SCM s) ///((name . "getenv"))
{
#if 0
char *p = getenv (string_to_cstring (s));
return p ? MAKE_STRING (cstring_to_list (p)) : cell_f;
#else
return cell_t;
#endif
}
SCM
open_input_file (SCM file_name)
{
return MAKE_NUMBER (open (string_to_cstring (file_name), O_RDONLY));
// char *s = string_to_cstring (file_name);
// int x = open (s, 0);
// return MAKE_NUMBER (x);
}
SCM
current_input_port ()
{
return MAKE_NUMBER (g_stdin);
}
SCM
set_current_input_port (SCM port)
{
g_stdin = VALUE (port) ? VALUE (port) : STDIN;
return current_input_port ();
}
SCM
force_output (SCM p) ///((arity . n))
{
#if 0
//FIXME
int fd = 1;
if (TYPE (p) == TPAIR && TYPE (car (p)) == TNUMBER) fd = VALUE (car (p));
FILE *f = fd == 1 ? stdout : stderr;
fflush (f);
#endif
return cell_unspecified;
}
// Math
SCM
greater_p (SCM x) ///((name . ">") (arity . n))
{
int n = INT_MAX;
while (x != cell_nil)
{
assert (TYPE (car (x)) == TNUMBER);
if (VALUE (car (x)) >= n) return cell_f;
n = VALUE (car (x));
x = cdr (x);
}
return cell_t;
}
SCM
less_p (SCM x) ///((name . "<") (arity . n))
{
int n = INT_MIN;
while (x != cell_nil)
{
assert (TYPE (car (x)) == TNUMBER);
if (VALUE (car (x)) <= n) return cell_f;
n = VALUE (car (x));
x = cdr (x);
}
return cell_t;
}
SCM
is_p (SCM x) ///((name . "=") (arity . n))
{
if (x == cell_nil) return cell_t;
assert (TYPE (car (x)) == TNUMBER);
int n = VALUE (car (x));
x = cdr (x);
while (x != cell_nil)
{
if (VALUE (car (x)) != n) return cell_f;
x = cdr (x);
}
return cell_t;
}
SCM
minus (SCM x) ///((name . "-") (arity . n))
{
SCM a = car (x);
assert (TYPE (a) == TNUMBER);
int n = VALUE (a);
x = cdr (x);
if (x == cell_nil)
n = -n;
while (x != cell_nil)
{
assert (TYPE (car (x)) == TNUMBER);
n -= VALUE (car (x));
x = cdr (x);
}
return MAKE_NUMBER (n);
}
SCM
plus (SCM x) ///((name . "+") (arity . n))
{
int n = 0;
while (x != cell_nil)
{
assert (TYPE (car (x)) == TNUMBER);
n += VALUE (car (x));
x = cdr (x);
}
return MAKE_NUMBER (n);
}
SCM
divide (SCM x) ///((name . "/") (arity . n))
{
int n = 1;
if (x != cell_nil) {
assert (TYPE (car (x)) == TNUMBER);
n = VALUE (car (x));
x = cdr (x);
}
while (x != cell_nil)
{
assert (TYPE (car (x)) == TNUMBER);
n /= VALUE (car (x));
x = cdr (x);
}
return MAKE_NUMBER (n);
}
SCM
modulo (SCM a, SCM b)
{
assert (TYPE (a) == TNUMBER);
assert (TYPE (b) == TNUMBER);
int x = VALUE (a);
while (x < 0) x += VALUE (b);
return MAKE_NUMBER (x % VALUE (b));
}
SCM
multiply (SCM x) ///((name . "*") (arity . n))
{
int n = 1;
while (x != cell_nil)
{
assert (TYPE (car (x)) == TNUMBER);
n *= VALUE (car (x));
x = cdr (x);
}
return MAKE_NUMBER (n);
}
SCM
logior (SCM x) ///((arity . n))
{
int n = 0;
while (x != cell_nil)
{
assert (TYPE (car (x)) == TNUMBER);
n |= VALUE (car (x));
x = cdr (x);
}
return MAKE_NUMBER (n);
}
SCM
ash (SCM n, SCM count)
{
assert (TYPE (n) == TNUMBER);
assert (TYPE (count) == TNUMBER);
int cn = VALUE (n);
int ccount = VALUE (count);
return MAKE_NUMBER ((ccount < 0) ? cn >> -ccount : cn << ccount);
}
// Lib [rest of]
int g_depth;
SCM
display_helper (SCM x, int cont, char* sep, int fd)
{
fputs (sep, fd);
if (g_depth == 0) return cell_unspecified;
g_depth = g_depth - 1;
switch (TYPE (x))
{
case TCHAR:
{
fputs ("#\\", fd);
putc (VALUE (x), fd);
break;
}
case TFUNCTION:
{
fputs ("#<procedure ", fd);
char *p = "?";
if (FUNCTION (x).name != 0)
p = FUNCTION (x).name;
fputs (p, fd);
fputs ("[", fd);
fputs (itoa (CDR (x)), fd);
fputs (",", fd);
fputs (itoa (x), fd);
fputs ("]>", fd);
break;
}
case TMACRO:
{
fputs ("#<macro ", fd);
display_helper (cdr (x), cont, "", fd);
fputs (">", fd);
break;
}
case TNUMBER:
{
fputs (itoa (VALUE (x)), fd);
break;
}
case TPAIR:
{
if (!cont) fputs ("(", fd);
if (x && x != cell_nil) fdisplay_ (CAR (x), fd);
if (CDR (x) && TYPE (CDR (x)) == TPAIR)
display_helper (CDR (x), 1, " ", fd);
else if (CDR (x) && CDR (x) != cell_nil)
{
if (TYPE (CDR (x)) != TPAIR)
fputs (" . ", fd);
fdisplay_ (CDR (x), fd);
}
if (!cont) fputs (")", fd);
break;
}
case TSPECIAL:
#if __NYACC__
// FIXME
//{}
{
SCM t = CAR (x);
while (t && t != cell_nil)
{
putc (VALUE (CAR (t)), fd);
t = CDR (t);
}
break;
}
#endif
case TSTRING:
#if __NYACC__
// FIXME
{
SCM t = CAR (x);
while (t && t != cell_nil)
{
putc (VALUE (CAR (t)), fd);
t = CDR (t);
}
break;
}
#endif
case TSYMBOL:
{
SCM t = CAR (x);
while (t && t != cell_nil)
{
putc (VALUE (CAR (t)), fd);
t = CDR (t);
}
break;
}
default:
{
fputs ("<", fd);
fputs (itoa (TYPE (x)), fd);
fputs (":", fd);
fputs (itoa (x), fd);
fputs (">", fd);
break;
}
}
return 0;
}
SCM
display_ (SCM x)
{
g_depth = 5;
return display_helper (x, 0, "", STDOUT);
}
SCM
display_error_ (SCM x)
{
g_depth = 5;
return display_helper (x, 0, "", STDERR);
}
SCM
fdisplay_ (SCM x, int fd) ///((internal))
{
g_depth = 5;
return display_helper (x, 0, "", fd);
}
SCM
exit_ (SCM x) ///((name . "exit"))
{
assert (TYPE (x) == TNUMBER);
exit (VALUE (x));
}
SCM
xassq (SCM x, SCM a) ///for speed in core only
{
while (a != cell_nil && x != CDAR (a)) a = CDR (a);
return a != cell_nil ? CAR (a) : cell_f;
}
// Jam Collector // Jam Collector
SCM g_symbol_max; SCM g_symbol_max;
@ -1552,19 +1157,19 @@ mes_builtins (SCM a) ///((internal))
#include "mini-mes.i" #include "mini-mes.i"
// Do not sort: Order of these includes define builtins // Do not sort: Order of these includes define builtins
// #include "lib.i" #include "mini-posix.i"
// #include "math.i" #include "mini-math.i"
// #include "posix.i" #include "mini-lib.i"
#include "mini-vector.i" #include "mini-vector.i"
#include "mini-gc.i" #include "mini-gc.i"
// #include "reader.i" // #include "mini-reader.i"
#include "mini-gc.environment.i" #include "mini-gc.environment.i"
// #include "lib.environment.i" #include "mini-lib.environment.i"
// #include "math.environment.i" #include "mini-math.environment.i"
#include "mini-mes.environment.i" #include "mini-mes.environment.i"
// #include "posix.environment.i" #include "mini-posix.environment.i"
// #include "reader.environment.i" // #include "mini-reader.environment.i"
#include "mini-vector.environment.i" #include "mini-vector.environment.i"
return a; return a;
@ -1631,10 +1236,6 @@ bload_env (SCM a) ///((internal))
return r2; return r2;
} }
// #include "math.c"
// #include "posix.c"
// #include "lib.c"
// #include "reader.c"
#include "vector.c" #include "vector.c"
#include "gc.c" #include "gc.c"