2016-10-22 18:51:32 +00:00
|
|
|
/* -*-comment-start: "//";comment-end:""-*-
|
2018-07-22 12:24:36 +00:00
|
|
|
* GNU Mes --- Maxwell Equations of Software
|
2020-04-19 11:25:32 +00:00
|
|
|
* Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
2016-10-22 18:51:32 +00:00
|
|
|
*
|
2018-07-22 12:24:36 +00:00
|
|
|
* This file is part of GNU Mes.
|
2016-10-22 18:51:32 +00:00
|
|
|
*
|
2018-07-22 12:24:36 +00:00
|
|
|
* GNU Mes is free software; you can redistribute it and/or modify it
|
2016-10-22 18:51:32 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2018-07-22 12:24:36 +00:00
|
|
|
* GNU Mes is distributed in the hope that it will be useful, but
|
2016-10-22 18:51:32 +00:00
|
|
|
* 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
|
2018-07-22 12:24:36 +00:00
|
|
|
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
2016-10-22 18:51:32 +00:00
|
|
|
*/
|
|
|
|
|
2020-07-19 06:53:46 +00:00
|
|
|
/** Commentary:
|
|
|
|
Scheme library functions not used by the eval/apply core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Code: */
|
|
|
|
|
2019-06-08 13:36:22 +00:00
|
|
|
#include "mes/lib.h"
|
|
|
|
#include "mes/mes.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
core: Remove struct definitions for builtins, drop snarfing.
After making a change to the list of builtin functions, run
cat src/*.i
and move the into
src/mes.c:mes_builtins ()
and, or also after changing the list of fixed symbols in src/mes.c:mes_symbols (), do
cat src/*.h > src/builtins.h
* build-aux/build.sh.in: Remove snarfing.
* build-aux/bootstrap.sh.in: Likewise.
* mes/module/mes/display.mes (display):
* mes/module/mes/type-0.mes (cell:type-alist): Remove <cell:function>.
(function?, builtin?): Remove.
* src/builtins.h: New file.
* src/mes.c (TFUNCTION): Remove.
(struct function): Remove.
(apply_builtin): Rewrite from call.
(mes_builtins): Rewrite.
(init_builtin, make_builtin_type, make_builtin, builtin_name,
builtin_arity, builtin, builtin_p, builtin_printer): New function.
2019-01-04 08:55:16 +00:00
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
type_ (struct scm *x)
|
core: Split-out core.c.
* src/mes.c (assoc_string, car, cdr, list, null_p, eq_p, values, acons,
length__, length, error, append2, append_reverse, reverse_x_, assq,
assoc): Move to ...
* src/core.c: New file.
* build-aux/configure-lib.sh (mes_SOURCES): Add it.
* simple.make (MES_SOURCES): Likewise.
* build-aux/snarf.sh: Likewise.
* include/mes/builtins.h: Update.
* src/builtins.c (mes_builtins): Update.
2020-07-19 06:49:23 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
return make_number (x->type);
|
core: Split-out core.c.
* src/mes.c (assoc_string, car, cdr, list, null_p, eq_p, values, acons,
length__, length, error, append2, append_reverse, reverse_x_, assq,
assoc): Move to ...
* src/core.c: New file.
* build-aux/configure-lib.sh (mes_SOURCES): Add it.
* simple.make (MES_SOURCES): Likewise.
* build-aux/snarf.sh: Likewise.
* include/mes/builtins.h: Update.
* src/builtins.c (mes_builtins): Update.
2020-07-19 06:49:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
car_ (struct scm *x)
|
core: Split-out core.c.
* src/mes.c (assoc_string, car, cdr, list, null_p, eq_p, values, acons,
length__, length, error, append2, append_reverse, reverse_x_, assq,
assoc): Move to ...
* src/core.c: New file.
* build-aux/configure-lib.sh (mes_SOURCES): Add it.
* simple.make (MES_SOURCES): Likewise.
* build-aux/snarf.sh: Likewise.
* include/mes/builtins.h: Update.
* src/builtins.c (mes_builtins): Update.
2020-07-19 06:49:23 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *a = x->car;
|
|
|
|
if (x->type == TPAIR)
|
core: Split-out core.c.
* src/mes.c (assoc_string, car, cdr, list, null_p, eq_p, values, acons,
length__, length, error, append2, append_reverse, reverse_x_, assq,
assoc): Move to ...
* src/core.c: New file.
* build-aux/configure-lib.sh (mes_SOURCES): Add it.
* simple.make (MES_SOURCES): Likewise.
* build-aux/snarf.sh: Likewise.
* include/mes/builtins.h: Update.
* src/builtins.c (mes_builtins): Update.
2020-07-19 06:49:23 +00:00
|
|
|
return a;
|
core: Use casting functions.
Silence all casting errors by using casting functions.
* src/cc.c (cast_charp_to_scmp, cast_charp_to_scmpp,
cast_voidp_to_charp, cast_scmp_to_long, cast_scmp_to_charp): New function.
* src/m2.c (cast_charp_to_scmp, cast_charp_to_scmpp,
cast_voidp_to_charp, cast_scmp_to_long, cast_scmp_to_charp): New function.
* include/mes/mes.h: Declare them.
(struct scm): Add car_value, cdr_value, function.
(g_continuations): Change to long.
* src/mes.c: Silence all casting errors by using casting functions.
2020-10-18 13:44:49 +00:00
|
|
|
return make_number (cast_scmp_to_long (a));
|
core: Split-out core.c.
* src/mes.c (assoc_string, car, cdr, list, null_p, eq_p, values, acons,
length__, length, error, append2, append_reverse, reverse_x_, assq,
assoc): Move to ...
* src/core.c: New file.
* build-aux/configure-lib.sh (mes_SOURCES): Add it.
* simple.make (MES_SOURCES): Likewise.
* build-aux/snarf.sh: Likewise.
* include/mes/builtins.h: Update.
* src/builtins.c (mes_builtins): Update.
2020-07-19 06:49:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
cdr_ (struct scm *x)
|
core: Split-out core.c.
* src/mes.c (assoc_string, car, cdr, list, null_p, eq_p, values, acons,
length__, length, error, append2, append_reverse, reverse_x_, assq,
assoc): Move to ...
* src/core.c: New file.
* build-aux/configure-lib.sh (mes_SOURCES): Add it.
* simple.make (MES_SOURCES): Likewise.
* build-aux/snarf.sh: Likewise.
* include/mes/builtins.h: Update.
* src/builtins.c (mes_builtins): Update.
2020-07-19 06:49:23 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *d = x->cdr;
|
|
|
|
if (x->type == TPAIR || x->type == TCLOSURE)
|
core: Split-out core.c.
* src/mes.c (assoc_string, car, cdr, list, null_p, eq_p, values, acons,
length__, length, error, append2, append_reverse, reverse_x_, assq,
assoc): Move to ...
* src/core.c: New file.
* build-aux/configure-lib.sh (mes_SOURCES): Add it.
* simple.make (MES_SOURCES): Likewise.
* build-aux/snarf.sh: Likewise.
* include/mes/builtins.h: Update.
* src/builtins.c (mes_builtins): Update.
2020-07-19 06:49:23 +00:00
|
|
|
return d;
|
core: Use casting functions.
Silence all casting errors by using casting functions.
* src/cc.c (cast_charp_to_scmp, cast_charp_to_scmpp,
cast_voidp_to_charp, cast_scmp_to_long, cast_scmp_to_charp): New function.
* src/m2.c (cast_charp_to_scmp, cast_charp_to_scmpp,
cast_voidp_to_charp, cast_scmp_to_long, cast_scmp_to_charp): New function.
* include/mes/mes.h: Declare them.
(struct scm): Add car_value, cdr_value, function.
(g_continuations): Change to long.
* src/mes.c: Silence all casting errors by using casting functions.
2020-10-18 13:44:49 +00:00
|
|
|
return make_number (cast_scmp_to_long (d));
|
core: Split-out core.c.
* src/mes.c (assoc_string, car, cdr, list, null_p, eq_p, values, acons,
length__, length, error, append2, append_reverse, reverse_x_, assq,
assoc): Move to ...
* src/core.c: New file.
* build-aux/configure-lib.sh (mes_SOURCES): Add it.
* simple.make (MES_SOURCES): Likewise.
* build-aux/snarf.sh: Likewise.
* include/mes/builtins.h: Update.
* src/builtins.c (mes_builtins): Update.
2020-07-19 06:49:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
xassq (struct scm *x, struct scm *a) /* For speed in core. */
|
mescc: Run module/base-0.mes.
* gc.c: New file.
* vector.c: New file.
* mes.c: Remove vector and gc functions, include vector.c, gc.c.
* GNUmakefile (mes.o): Add gc, vector dependencies.
* scaffold/mini-mes.c (eval_apply): Support primitive-load through
read_input_file.
(getenv_, open_input_file, current_input_port,
set_current_input_port force_output, exit_, values, arity_, xassq,
is_p, minus, plus, divide, modulo multiply, logior, ash): New function.
(mes_symbols): Add symbols %gnuc, %mesc.
* scaffold/mini-mes.c (): New functions.
* scaffold/b-0.mes: New file.
* scaffold/t-0.mes: New file.
2017-03-26 19:13:01 +00:00
|
|
|
{
|
2020-07-16 19:20:17 +00:00
|
|
|
while (a != cell_nil)
|
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
if (x == a->car->cdr)
|
|
|
|
return a->car;
|
|
|
|
a = a->cdr;
|
2020-07-16 19:20:17 +00:00
|
|
|
}
|
2020-04-19 11:25:32 +00:00
|
|
|
return cell_f;
|
mescc: Run module/base-0.mes.
* gc.c: New file.
* vector.c: New file.
* mes.c: Remove vector and gc functions, include vector.c, gc.c.
* GNUmakefile (mes.o): Add gc, vector dependencies.
* scaffold/mini-mes.c (eval_apply): Support primitive-load through
read_input_file.
(getenv_, open_input_file, current_input_port,
set_current_input_port force_output, exit_, values, arity_, xassq,
is_p, minus, plus, divide, modulo multiply, logior, ash): New function.
(mes_symbols): Add symbols %gnuc, %mesc.
* scaffold/mini-mes.c (): New functions.
* scaffold/b-0.mes: New file.
* scaffold/t-0.mes: New file.
2017-03-26 19:13:01 +00:00
|
|
|
}
|
2018-04-05 09:03:09 +00:00
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
memq (struct scm *x, struct scm *a)
|
2018-04-05 09:03:09 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
int t = x->type;
|
2019-05-18 11:27:42 +00:00
|
|
|
if (t == TCHAR || t == TNUMBER)
|
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
long v = x->value;
|
2020-07-16 19:23:38 +00:00
|
|
|
while (a != cell_nil)
|
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
if (v == a->car->value)
|
2020-07-16 19:23:38 +00:00
|
|
|
return a;
|
2020-10-18 09:34:32 +00:00
|
|
|
a = a->cdr;
|
2020-07-16 19:23:38 +00:00
|
|
|
}
|
|
|
|
return cell_f;
|
2019-05-18 11:27:42 +00:00
|
|
|
}
|
2020-07-16 19:23:38 +00:00
|
|
|
if (t == TKEYWORD)
|
2019-05-18 11:27:42 +00:00
|
|
|
{
|
2020-07-16 19:23:38 +00:00
|
|
|
while (a != cell_nil)
|
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
if (a->car->type == TKEYWORD)
|
|
|
|
if (string_equal_p (x, a->car) == cell_t)
|
2020-07-16 19:23:38 +00:00
|
|
|
return a;
|
2020-10-18 09:34:32 +00:00
|
|
|
a = a->cdr;
|
2020-07-16 19:23:38 +00:00
|
|
|
}
|
|
|
|
return cell_f;
|
2019-05-18 11:27:42 +00:00
|
|
|
}
|
2020-07-16 19:23:38 +00:00
|
|
|
while (a != cell_nil)
|
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
if (x == a->car)
|
2020-07-16 19:23:38 +00:00
|
|
|
return a;
|
2020-10-18 09:34:32 +00:00
|
|
|
a = a->cdr;
|
2020-07-16 19:23:38 +00:00
|
|
|
}
|
2020-04-19 11:25:32 +00:00
|
|
|
return cell_f;
|
2018-04-05 09:03:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
equal2_p (struct scm *a, struct scm *b)
|
2018-04-05 18:01:04 +00:00
|
|
|
{
|
2019-05-18 11:27:42 +00:00
|
|
|
equal2:
|
2018-04-24 04:59:18 +00:00
|
|
|
if (a == b)
|
2018-04-05 18:01:04 +00:00
|
|
|
return cell_t;
|
2020-10-18 09:34:32 +00:00
|
|
|
if (a->type == TPAIR && b->type == TPAIR)
|
2018-04-24 04:59:18 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
if (equal2_p (a->car, b->car) == cell_t)
|
2018-04-24 04:59:18 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
a = a->cdr;
|
|
|
|
b = b->cdr;
|
2018-04-24 04:59:18 +00:00
|
|
|
goto equal2;
|
|
|
|
}
|
|
|
|
return cell_f;
|
|
|
|
}
|
2020-10-18 09:34:32 +00:00
|
|
|
if (a->type == TSTRING && b->type == TSTRING)
|
2018-11-11 15:25:36 +00:00
|
|
|
return string_equal_p (a, b);
|
2020-10-18 09:34:32 +00:00
|
|
|
if (a->type == TVECTOR && b->type == TVECTOR)
|
2018-04-05 18:01:04 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
if (a->length != b->length)
|
2018-04-05 18:01:04 +00:00
|
|
|
return cell_f;
|
2020-04-19 11:25:32 +00:00
|
|
|
long i;
|
2020-10-18 09:34:32 +00:00
|
|
|
for (i = 0; i < a->length; i = i + 1)
|
2018-04-05 18:01:04 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *ai = cell_ref (a->vector, i);
|
|
|
|
struct scm *bi = cell_ref (b->vector, i);
|
|
|
|
if (ai->type == TREF)
|
|
|
|
ai = ai->ref;
|
|
|
|
if (bi->type == TREF)
|
|
|
|
bi = bi->ref;
|
2018-04-05 18:01:04 +00:00
|
|
|
if (equal2_p (ai, bi) == cell_f)
|
|
|
|
return cell_f;
|
|
|
|
}
|
|
|
|
return cell_t;
|
|
|
|
}
|
|
|
|
return eq_p (a, b);
|
|
|
|
}
|
2018-04-24 05:00:35 +00:00
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
last_pair (struct scm *x)
|
2018-04-24 05:00:35 +00:00
|
|
|
{
|
2020-07-16 19:25:06 +00:00
|
|
|
while (x != cell_nil)
|
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
if (x->cdr == cell_nil)
|
2020-07-16 19:25:06 +00:00
|
|
|
return x;
|
2020-10-18 09:34:32 +00:00
|
|
|
x = x->cdr;
|
2020-07-16 19:25:06 +00:00
|
|
|
}
|
2018-04-24 05:00:35 +00:00
|
|
|
return x;
|
|
|
|
}
|
2018-10-18 18:06:10 +00:00
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
pair_p (struct scm *x)
|
2018-10-18 18:06:10 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
if (x->type == TPAIR)
|
2020-04-19 11:25:32 +00:00
|
|
|
return cell_t;
|
|
|
|
return cell_f;
|
2018-10-18 18:06:10 +00:00
|
|
|
}
|
2020-05-17 22:40:50 +00:00
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
char_to_integer (struct scm *x)
|
2020-05-17 22:40:50 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
return make_number (x->value);
|
2020-05-17 22:40:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:34:32 +00:00
|
|
|
struct scm *
|
|
|
|
integer_to_char (struct scm *x)
|
2020-05-17 22:40:50 +00:00
|
|
|
{
|
2020-10-18 09:34:32 +00:00
|
|
|
return make_char (x->value);
|
2020-05-17 22:40:50 +00:00
|
|
|
}
|