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>
|
2021-04-05 11:16:17 +00:00
|
|
|
* Copyright © 2021 W. J. van der Laan <laanwj@protonmail.com>
|
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
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
2020-04-19 11:25:32 +00:00
|
|
|
exit_ (SCM x) /*:((name . "exit")) */
|
2016-10-22 18:51:32 +00:00
|
|
|
{
|
2020-05-17 14:07:04 +00:00
|
|
|
assert_msg (TYPE (x) == TNUMBER, "TYPE (x) == TNUMBER");
|
2016-11-21 08:30:59 +00:00
|
|
|
exit (VALUE (x));
|
2016-10-22 18:51:32 +00:00
|
|
|
}
|
2016-12-24 00:23:50 +00:00
|
|
|
|
2018-10-20 16:23:20 +00:00
|
|
|
SCM
|
|
|
|
frame_printer (SCM frame)
|
|
|
|
{
|
2019-05-18 11:27:42 +00:00
|
|
|
fdputs ("#<", __stdout);
|
|
|
|
display_ (struct_ref_ (frame, 2));
|
2019-03-02 11:35:18 +00:00
|
|
|
fdputc (' ', __stdout);
|
2019-05-18 11:27:42 +00:00
|
|
|
fdputs ("procedure: ", __stdout);
|
|
|
|
display_ (struct_ref_ (frame, 3));
|
2019-03-02 11:35:18 +00:00
|
|
|
fdputc ('>', __stdout);
|
2018-10-20 16:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
2020-04-19 11:25:32 +00:00
|
|
|
make_frame_type () /*:((internal)) */
|
2018-10-20 16:23:20 +00:00
|
|
|
{
|
2019-05-18 11:27:42 +00:00
|
|
|
SCM record_type = cell_symbol_record_type; // FIXME
|
2018-10-20 16:23:20 +00:00
|
|
|
SCM fields = cell_nil;
|
|
|
|
fields = cons (cell_symbol_procedure, fields);
|
|
|
|
fields = cons (fields, cell_nil);
|
|
|
|
fields = cons (cell_symbol_frame, fields);
|
|
|
|
return make_struct (record_type, fields, cell_unspecified);
|
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
|
|
|
make_frame (SCM stack, long index)
|
|
|
|
{
|
|
|
|
SCM frame_type = make_frame_type ();
|
2020-04-19 11:25:32 +00:00
|
|
|
long array_index = 0;
|
2021-04-05 11:16:17 +00:00
|
|
|
SCM procedure = 0;
|
|
|
|
if (index != 0)
|
|
|
|
{
|
2020-04-19 11:25:32 +00:00
|
|
|
array_index = (STACK_SIZE - (index * FRAME_SIZE));
|
2021-04-05 11:16:17 +00:00
|
|
|
procedure = g_stack_array[array_index + FRAME_PROCEDURE];
|
|
|
|
}
|
2020-04-19 11:25:32 +00:00
|
|
|
if (procedure == 0)
|
2018-10-20 16:23:20 +00:00
|
|
|
procedure = cell_f;
|
|
|
|
SCM values = cell_nil;
|
|
|
|
values = cons (procedure, values);
|
|
|
|
values = cons (cell_symbol_frame, values);
|
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
|
|
|
return make_struct (frame_type, values, cstring_to_symbol ("frame-printer"));
|
2018-10-20 16:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
2020-04-19 11:25:32 +00:00
|
|
|
make_stack_type () /*:((internal)) */
|
2018-10-20 16:23:20 +00:00
|
|
|
{
|
2019-05-18 11:27:42 +00:00
|
|
|
SCM record_type = cell_symbol_record_type; // FIXME
|
2018-10-20 16:23:20 +00:00
|
|
|
SCM fields = cell_nil;
|
|
|
|
fields = cons (cstring_to_symbol ("frames"), fields);
|
|
|
|
fields = cons (fields, cell_nil);
|
|
|
|
fields = cons (cell_symbol_stack, fields);
|
|
|
|
return make_struct (record_type, fields, cell_unspecified);
|
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
2020-04-19 11:25:32 +00:00
|
|
|
make_stack (SCM stack) /*:((arity . n)) */
|
2018-10-20 16:23:20 +00:00
|
|
|
{
|
|
|
|
SCM stack_type = make_stack_type ();
|
2019-05-18 11:27:42 +00:00
|
|
|
long size = (STACK_SIZE - g_stack) / FRAME_SIZE;
|
2018-10-20 16:23:20 +00:00
|
|
|
SCM frames = make_vector__ (size);
|
2020-04-19 11:25:32 +00:00
|
|
|
long i;
|
|
|
|
for (i = 0; i < size; i = i + 1)
|
2018-10-20 16:23:20 +00:00
|
|
|
{
|
|
|
|
SCM frame = make_frame (stack, i);
|
|
|
|
vector_set_x_ (frames, i, frame);
|
|
|
|
}
|
|
|
|
SCM values = cell_nil;
|
|
|
|
values = cons (frames, values);
|
|
|
|
values = cons (cell_symbol_stack, values);
|
|
|
|
return make_struct (stack_type, values, cell_unspecified);
|
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
|
|
|
stack_length (SCM stack)
|
|
|
|
{
|
|
|
|
SCM frames = struct_ref_ (stack, 3);
|
|
|
|
return vector_length (frames);
|
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
|
|
|
stack_ref (SCM stack, SCM index)
|
|
|
|
{
|
|
|
|
SCM frames = struct_ref_ (stack, 3);
|
|
|
|
return vector_ref (frames, index);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
SCM
|
2020-04-19 11:25:32 +00:00
|
|
|
xassq (SCM x, 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
|
|
|
{
|
2018-04-05 19:35:31 +00:00
|
|
|
while (a != cell_nil && x != CDAR (a))
|
|
|
|
a = CDR (a);
|
2020-04-19 11:25:32 +00:00
|
|
|
if (a != cell_nil)
|
|
|
|
return CAR (a);
|
|
|
|
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
|
|
|
|
|
|
|
SCM
|
|
|
|
memq (SCM x, SCM a)
|
|
|
|
{
|
2018-08-12 14:41:07 +00:00
|
|
|
int t = TYPE (x);
|
2019-05-18 11:27:42 +00:00
|
|
|
if (t == TCHAR || t == TNUMBER)
|
|
|
|
{
|
|
|
|
SCM v = VALUE (x);
|
|
|
|
while (a != cell_nil && v != VALUE (CAR (a)))
|
2018-04-05 19:35:31 +00:00
|
|
|
a = CDR (a);
|
2019-05-18 11:27:42 +00:00
|
|
|
}
|
|
|
|
else if (t == TKEYWORD)
|
|
|
|
{
|
|
|
|
while (a != cell_nil && (TYPE (CAR (a)) != TKEYWORD || string_equal_p (x, CAR (a)) == cell_f))
|
|
|
|
a = CDR (a);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
while (a != cell_nil && x != CAR (a))
|
|
|
|
a = CDR (a);
|
2020-04-19 11:25:32 +00:00
|
|
|
if (a != cell_nil)
|
|
|
|
return a;
|
|
|
|
return cell_f;
|
2018-04-05 09:03:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 18:01:04 +00:00
|
|
|
SCM
|
|
|
|
equal2_p (SCM a, SCM b)
|
|
|
|
{
|
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;
|
|
|
|
if (TYPE (a) == TPAIR && TYPE (b) == TPAIR)
|
2018-04-24 04:59:18 +00:00
|
|
|
{
|
|
|
|
if (equal2_p (CAR (a), CAR (b)) == cell_t)
|
|
|
|
{
|
|
|
|
a = CDR (a);
|
|
|
|
b = CDR (b);
|
|
|
|
goto equal2;
|
|
|
|
}
|
|
|
|
return cell_f;
|
|
|
|
}
|
2018-04-05 18:01:04 +00:00
|
|
|
if (TYPE (a) == TSTRING && TYPE (b) == TSTRING)
|
2018-11-11 15:25:36 +00:00
|
|
|
return string_equal_p (a, b);
|
2018-04-05 18:01:04 +00:00
|
|
|
if (TYPE (a) == TVECTOR && TYPE (b) == TVECTOR)
|
|
|
|
{
|
|
|
|
if (LENGTH (a) != LENGTH (b))
|
|
|
|
return cell_f;
|
2020-04-19 11:25:32 +00:00
|
|
|
long i;
|
|
|
|
for (i = 0; i < LENGTH (a); i = i + 1)
|
2018-04-05 18:01:04 +00:00
|
|
|
{
|
|
|
|
SCM ai = VECTOR (a) + i;
|
|
|
|
SCM bi = VECTOR (b) + i;
|
|
|
|
if (TYPE (ai) == TREF)
|
|
|
|
ai = REF (ai);
|
|
|
|
if (TYPE (bi) == TREF)
|
|
|
|
bi = REF (bi);
|
|
|
|
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
|
|
|
|
|
|
|
SCM
|
|
|
|
last_pair (SCM x)
|
|
|
|
{
|
|
|
|
while (x != cell_nil && CDR (x) != cell_nil)
|
|
|
|
x = CDR (x);
|
|
|
|
return x;
|
|
|
|
}
|
2018-10-18 18:06:10 +00:00
|
|
|
|
|
|
|
SCM
|
|
|
|
pair_p (SCM x)
|
|
|
|
{
|
2020-04-19 11:25:32 +00:00
|
|
|
if (TYPE (x) == TPAIR)
|
|
|
|
return cell_t;
|
|
|
|
return cell_f;
|
2018-10-18 18:06:10 +00:00
|
|
|
}
|
2020-05-17 22:40:50 +00:00
|
|
|
|
|
|
|
SCM
|
|
|
|
char_to_integer (SCM x)
|
|
|
|
{
|
core: Remove macros from cell creation: make_char .. make_string.
* src/gc.c (make_char, make_continuation, make_macro, make_number,
make_ref, make_string0, make_string_port): New function.
(make_string): Move from string.c.
* include/mes/macros.h (MAKE_CHAR, MAKE_CONTINUATION, MAKE_MACRO,
MAKE_NUMBER, MAKE_REF, MAKE_STRING0, MAKE_STRING_PORT): Remove macro.
Update users.
* include/mes/m2.h: Likewise.
2020-05-18 20:35:12 +00:00
|
|
|
return make_number (VALUE (x));
|
2020-05-17 22:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
|
|
|
integer_to_char (SCM x)
|
|
|
|
{
|
core: Remove macros from cell creation: make_char .. make_string.
* src/gc.c (make_char, make_continuation, make_macro, make_number,
make_ref, make_string0, make_string_port): New function.
(make_string): Move from string.c.
* include/mes/macros.h (MAKE_CHAR, MAKE_CONTINUATION, MAKE_MACRO,
MAKE_NUMBER, MAKE_REF, MAKE_STRING0, MAKE_STRING_PORT): Remove macro.
Update users.
* include/mes/m2.h: Likewise.
2020-05-18 20:35:12 +00:00
|
|
|
return make_char (VALUE (x));
|
2020-05-17 22:40:50 +00:00
|
|
|
}
|