2018-10-15 12:36:19 +01:00
|
|
|
/* -*-comment-start: "//";comment-end:""-*-
|
|
|
|
* GNU Mes --- Maxwell Equations of Software
|
2019-01-13 10:02:28 +00:00
|
|
|
* Copyright © 2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
* Copyright © 2019 Jeremiah Orians <jeremiah@pdp10.guru>
|
2018-10-15 12:36:19 +01:00
|
|
|
*
|
|
|
|
* This file is part of GNU Mes.
|
|
|
|
*
|
|
|
|
* GNU 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.
|
|
|
|
*
|
|
|
|
* GNU 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 GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-08 14:36:22 +01:00
|
|
|
#include "mes/lib.h"
|
|
|
|
#include "mes/mes.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2018-10-15 12:36:19 +01:00
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
SCM
|
|
|
|
hash_cstring (char *s, SCM size)
|
2018-10-15 12:36:19 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
int h = s[0] * 37;
|
|
|
|
|
2018-11-11 15:25:36 +00:00
|
|
|
if (s[0] && s[1])
|
2019-01-13 10:02:28 +00:00
|
|
|
{
|
|
|
|
h = h + s[1] * 43;
|
|
|
|
}
|
2018-10-15 12:36:19 +01:00
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
require (0 != size, "src/hash.c: hash_cstring must not be zero");
|
|
|
|
h = h % size;
|
|
|
|
return h;
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
2019-01-13 10:02:28 +00:00
|
|
|
hashq_ (struct scm *x, SCM size)
|
2018-10-15 12:36:19 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *y = x;
|
|
|
|
if (y->type == TSPECIAL || y->type == TSYMBOL)
|
|
|
|
{
|
|
|
|
char *p = y->cdr->string;
|
|
|
|
return hash_cstring (p, size); /* FIXME: hash x directly */
|
|
|
|
}
|
|
|
|
|
|
|
|
error (cell_symbol_system_error, cons (make_string_ ("hashq_: not a symbol"), x));
|
|
|
|
exit (EXIT_FAILURE);
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SCM
|
2019-01-13 10:02:28 +00:00
|
|
|
hash_ (struct scm *x, SCM size)
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *y = x;
|
|
|
|
if (y->type == TSTRING)
|
|
|
|
{
|
|
|
|
char *p = y->cdr->string;
|
|
|
|
return hash_cstring (p, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
require (FALSE, "src/hash.c: hash_ impossible condition hit");
|
|
|
|
return hashq_ (x, size);
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
hashq (struct scm *x, struct scm *size)
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
require (FALSE, "src/hash.c: hashq impossible condition hit");
|
|
|
|
return make_number (hashq_ (x, size->value));
|
2018-10-15 12:36:19 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
hash (struct scm *x, struct scm *size)
|
2018-10-15 12:36:19 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
require (FALSE, "src/hash.c: hash impossible condition hit");
|
|
|
|
return make_number (hash_ (x, size->value));
|
2018-10-15 12:36:19 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
hashq_get_handle (struct scm *table, struct scm *key, struct scm *dflt)
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *ydflt = dflt;
|
|
|
|
if (ydflt->type == TPAIR)
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
return ydflt->car;
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
}
|
2019-01-13 10:02:28 +00:00
|
|
|
|
|
|
|
struct scm *ybucket = vector_ref_ (struct_ref_ (table, 4), hashq_ (key, struct_ref_ (table, 3)->value));
|
|
|
|
if (ybucket->type == TPAIR)
|
|
|
|
{
|
|
|
|
return assq (key, ybucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell_f;
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
hashq_ref (struct scm *table, struct scm *key, struct scm *dflt)
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *x = hashq_get_handle (table, key, dflt);
|
|
|
|
|
|
|
|
if (x == cell_f)
|
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return x->cdr;
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
hash_ref (struct scm *table, struct scm *key, struct scm *dflt) /* External */
|
2018-10-15 12:36:19 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *bucket = vector_ref_ (struct_ref_ (table, 4), hash_ (key, struct_ref_ (table, 3)->value));
|
|
|
|
if (bucket->type == TPAIR)
|
|
|
|
{
|
|
|
|
struct scm *y = assoc (key, bucket);
|
|
|
|
if (y != cell_f)
|
|
|
|
{
|
|
|
|
return y->cdr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell_f;
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
hashq_set_x (struct scm *table, struct scm *key, struct scm *value)
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
SCM size = struct_ref_ (table, 3)->value;
|
|
|
|
struct scm *buckets = struct_ref_ (table, 4);
|
|
|
|
|
|
|
|
struct scm *ybucket = vector_ref_ (buckets, hashq_ (key, size));
|
|
|
|
if (ybucket->type != TPAIR)
|
|
|
|
{
|
|
|
|
vector_set_x_ (buckets, hashq_ (key, size), acons (key, value, cell_nil));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vector_set_x_ (buckets, hashq_ (key, size),
|
|
|
|
acons (key, value, vector_ref_ (buckets, hashq_ (key, size))));
|
|
|
|
}
|
2018-10-15 12:36:19 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
hash_set_x (struct scm *table, struct scm *key, struct scm *value)
|
2018-10-15 14:45:41 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
SCM size = struct_ref_ (table, 3)->value;
|
|
|
|
unsigned h = hash_ (key, size);
|
|
|
|
struct scm *buckets = struct_ref_ (table, 4);
|
|
|
|
struct scm *bucket = vector_ref_ (buckets, h);
|
|
|
|
|
|
|
|
struct scm *ybucket = bucket;
|
|
|
|
if (ybucket->type != TPAIR)
|
2018-10-15 14:45:41 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
bucket = cell_nil;
|
2018-10-15 14:45:41 +01:00
|
|
|
}
|
2019-01-13 10:02:28 +00:00
|
|
|
|
|
|
|
bucket = acons (key, value, bucket);
|
|
|
|
vector_set_x_ (buckets, h, bucket);
|
|
|
|
return value;
|
2018-10-15 14:45:41 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
make_hashq_type () /* ((internal)) */
|
2018-10-15 14:45:41 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *record_type = cell_symbol_record_type; /* FIXME */
|
|
|
|
struct scm *fields = cell_nil;
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
fields = cons (cell_symbol_buckets, fields);
|
|
|
|
fields = cons (cell_symbol_size, fields);
|
2018-10-15 15:57:00 +01:00
|
|
|
fields = cons (fields, cell_nil);
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
fields = cons (cell_symbol_hashq_table, fields);
|
2018-10-15 19:42:10 +01:00
|
|
|
return make_struct (record_type, fields, cell_unspecified);
|
2018-10-15 14:45:41 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
make_hash_table_ (SCM size)
|
2018-10-15 12:36:19 +01:00
|
|
|
{
|
|
|
|
if (!size)
|
2019-01-13 10:02:28 +00:00
|
|
|
{
|
|
|
|
size = 100;
|
|
|
|
}
|
2018-10-15 19:42:10 +01:00
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *hashq_type = make_hashq_type ();
|
|
|
|
struct scm *buckets = make_vector__ (size);
|
|
|
|
struct scm *values = cell_nil;
|
2018-10-15 14:45:41 +01:00
|
|
|
values = cons (buckets, values);
|
2019-01-13 10:02:28 +00:00
|
|
|
values = cons (make_number (size), values);
|
core: Add hashq_get_handle, hash, hash_ref, hash_set_x.
* src/mes.c (scm_symbol_hashq_table, scm_symbol_record_type,
scm_symbol_module, scm_symbol_buckets, scm_symbol_size): New symbols.
Update users.
* src/hash.c (hash_list_of_char): Rename from hashq_. Respect size,
update callers.
(hashq_, hash_ hash, hashq_get_handle, hash_ref, hash_set_x_,
hash_set_x): New function.
(hashq_ref): Do not return handle. Update callers.
2018-10-18 06:55:28 +01:00
|
|
|
values = cons (cell_symbol_hashq_table, values);
|
2019-01-13 10:02:28 +00:00
|
|
|
/* FIXME: symbol/printer return make_struct (hashq_type, values, cstring_to_symbol ("hash-table-printer"); */
|
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 (hashq_type, values, cell_unspecified);
|
2018-10-15 12:36:19 +01:00
|
|
|
}
|
|
|
|
|
2019-01-13 10:02:28 +00:00
|
|
|
struct scm *
|
|
|
|
make_hash_table (struct scm *x)
|
2018-10-15 12:36:19 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
SCM size = 0;
|
|
|
|
|
|
|
|
struct scm *y = x;
|
|
|
|
if (y->type == TPAIR)
|
2018-10-15 12:36:19 +01:00
|
|
|
{
|
2019-01-13 10:02:28 +00:00
|
|
|
require (TNUMBER == y->type, "y->type must be TNUMBER\nsrc/hash.c: make_hash_table\n");
|
|
|
|
size = y->value;
|
2018-10-15 12:36:19 +01:00
|
|
|
}
|
2019-01-13 10:02:28 +00:00
|
|
|
|
2018-10-15 12:36:19 +01:00
|
|
|
return make_hash_table_ (size);
|
|
|
|
}
|
2019-01-13 10:02:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Externally exposed */
|
|
|
|
struct scm *
|
|
|
|
hashq_set_x_ (struct scm *table, struct scm *key, struct scm *value)
|
|
|
|
{
|
|
|
|
return hashq_set_x (table, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct scm *
|
|
|
|
hash_ref_ (struct scm *table, struct scm *key, struct scm *dflt)
|
|
|
|
{
|
|
|
|
return hash_ref (table, key, dflt);
|
|
|
|
}
|