2016-10-22 18:51:32 +00:00
|
|
|
/* -*-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/>.
|
|
|
|
*/
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM caar (SCM x) {return car (car (x));}
|
|
|
|
SCM cadr (SCM x) {return car (cdr (x));}
|
|
|
|
SCM cdar (SCM x) {return cdr (car (x));}
|
|
|
|
SCM cddr (SCM x) {return cdr (cdr (x));}
|
|
|
|
SCM caaar (SCM x) {return car (car (car (x)));}
|
|
|
|
SCM caadr (SCM x) {return car (car (cdr (x)));}
|
|
|
|
SCM caddr (SCM x) {return car (cdr (cdr (x)));}
|
|
|
|
SCM cdadr (SCM x) {return cdr (car (cdr (x)));}
|
|
|
|
SCM cadar (SCM x) {return car (cdr (car (x)));}
|
|
|
|
SCM cddar (SCM x) {return cdr (cdr (car (x)));}
|
|
|
|
SCM cdddr (SCM x) {return cdr (cdr (cdr (x)));}
|
|
|
|
SCM cadddr (SCM x) {return car (cdr (cdr (cdr (x))));}
|
2016-10-22 18:51:32 +00:00
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
length (SCM x)
|
2016-10-22 18:51:32 +00:00
|
|
|
{
|
|
|
|
int n = 0;
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil)
|
2016-10-22 18:51:32 +00:00
|
|
|
{
|
|
|
|
n++;
|
|
|
|
x = cdr (x);
|
|
|
|
}
|
2016-12-23 15:22:19 +00:00
|
|
|
return MAKE_NUMBER (n);
|
2016-10-22 18:51:32 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
last_pair (SCM x)
|
2016-10-22 18:51:32 +00:00
|
|
|
{
|
2016-11-21 08:28:34 +00:00
|
|
|
while (x != cell_nil && cdr (x) != cell_nil)
|
2016-10-22 18:51:32 +00:00
|
|
|
x = cdr (x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
list (SCM x) ///((arity . n))
|
2016-10-22 18:51:32 +00:00
|
|
|
{
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
list_ref (SCM x, SCM k)
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (x) == PAIR);
|
|
|
|
assert (TYPE (k) == NUMBER);
|
|
|
|
int n = VALUE (k);
|
|
|
|
while (n-- && CDR (x) != cell_nil) x = CDR (x);
|
2016-11-21 08:28:34 +00:00
|
|
|
return x != cell_nil ? car (x) : cell_undefined;
|
Implement strings and symbols as list of characters [WAS: c-string].
* mes.c (scm_t): Add string field.
(make_string, internal_lookup_symbol, internal_make_symbol,
make_symbol, lookup, readword): Take scm*. Update callers.
(display_helper): Support string field.
(append_char): New function.
(readstring): Use it. Produce scm*.
(cstring_to_list): New function.
(add_environment, internal_make_symbol): Use it.
(list_of_char_equal_p): New function.
(internal_lookup_symbol): Use it.
* lib.c (list_ref): New function.
* string.c (string_ref): Use it.
(string, string_append, string_length, substring, number_to_string,
string_to_symbol, symbol_to_string): Update to list-of-characters
implementation.
2016-10-25 14:50:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
vector_to_list (SCM v)
|
2016-10-22 18:51:32 +00:00
|
|
|
{
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM x = cell_nil;
|
|
|
|
for (int i = 0; i < LENGTH (v); i++) {
|
|
|
|
SCM e = VECTOR (v)+i;
|
2016-11-21 08:30:59 +00:00
|
|
|
if (TYPE (e) == REF) e = g_cells[e].ref;
|
2016-11-21 08:28:34 +00:00
|
|
|
x = append2 (x, cons (e, cell_nil));
|
Introduce reference type, use vectors of SCM.
* mes.c (type): Add REF.
(scm_t): Add ref, change vector to *scm_t. Update users.
(alloc): New function.
(cons, make_char, make_macro, make_number, make_string,
internal_make_symbol, make_vector): Use it.
(make_ref): New function.
(vector_entry): New function.
(make_vector, list_to_vector, vector_set_x): Use it.
(vector_ref): Dereference REF entry.
(display_helper): Handle REF.
* lib.c (vector_to_list): Handle REF.
* type.c (ref_p): New function.
* tests/vector.test (vector list): New test.
Bugfix vector-ref.
* mes.c (vector-ref): Make copies of simple values. Fixes lalr.
* tests/vector.test (vector-set! 3): New test.
2016-10-24 22:21:28 +00:00
|
|
|
}
|
2016-10-22 18:51:32 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2016-11-21 08:28:34 +00:00
|
|
|
SCM
|
|
|
|
builtin_exit (SCM x)
|
2016-10-22 18:51:32 +00:00
|
|
|
{
|
2016-11-21 08:30:59 +00:00
|
|
|
assert (TYPE (x) == NUMBER);
|
|
|
|
exit (VALUE (x));
|
2016-10-22 18:51:32 +00:00
|
|
|
}
|