2016-10-22 18:18:03 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
scm *
|
|
|
|
string (scm *x) ///((args . n))
|
|
|
|
{
|
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
|
|
|
return make_string (x);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
string_append (scm *x) ///((args . n))
|
|
|
|
{
|
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
|
|
|
scm *p = &scm_nil;
|
2016-10-22 18:18:03 +00:00
|
|
|
while (x != &scm_nil)
|
|
|
|
{
|
|
|
|
scm *s = car (x);
|
|
|
|
assert (s->type == STRING);
|
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
|
|
|
p = append2 (p, s->string);
|
2016-10-22 18:18:03 +00:00
|
|
|
x = cdr (x);
|
|
|
|
}
|
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
|
|
|
return make_string (p);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
list_to_string (scm *x)
|
|
|
|
{
|
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
|
|
|
return make_string (x);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
string_length (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == STRING);
|
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
|
|
|
return make_number (length (x->string)->value);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
string_ref (scm *x, scm *k)
|
|
|
|
{
|
|
|
|
assert (x->type == STRING);
|
|
|
|
assert (k->type == NUMBER);
|
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
|
|
|
scm n = {NUMBER, .value=k->value};
|
|
|
|
return make_char (list_ref (x->string, &n)->value);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
substring (scm *x) ///((args . n))
|
|
|
|
{
|
|
|
|
assert (x->type == PAIR);
|
|
|
|
assert (x->car->type == STRING);
|
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
|
|
|
scm *s = x->car->string;
|
2016-10-22 18:18:03 +00:00
|
|
|
assert (x->cdr->car->type == NUMBER);
|
|
|
|
int start = x->cdr->car->value;
|
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
|
|
|
int end = length (s)->value;
|
2016-10-22 18:18:03 +00:00
|
|
|
if (x->cdr->cdr->type == PAIR) {
|
|
|
|
assert (x->cdr->cdr->car->type == NUMBER);
|
|
|
|
assert (x->cdr->cdr->car->value <= end);
|
|
|
|
end = x->cdr->cdr->car->value;
|
|
|
|
}
|
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
|
|
|
int n = end - start;
|
|
|
|
while (start--) s = s->cdr;
|
|
|
|
scm *p = &scm_nil;
|
|
|
|
while (n-- && s != &scm_nil) {
|
|
|
|
p = append2 (p, cons (make_char (s->car->value), &scm_nil));
|
|
|
|
s = s->cdr;
|
|
|
|
}
|
|
|
|
return make_string (p);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
number_to_string (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == NUMBER);
|
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
|
|
|
int n = x->value;
|
|
|
|
scm *p = n < 0 ? cons (make_char ('-'), &scm_nil) : &scm_nil;
|
|
|
|
do {
|
|
|
|
p = cons (make_char (n % 10 + '0'), p);
|
|
|
|
n = n / 10;
|
|
|
|
} while (n);
|
|
|
|
return make_string (p);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
string_to_symbol (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == STRING);
|
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
|
|
|
return make_symbol (x->string);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scm *
|
|
|
|
symbol_to_string (scm *x)
|
|
|
|
{
|
|
|
|
assert (x->type == SYMBOL);
|
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
|
|
|
return make_string (x->string);
|
2016-10-22 18:18:03 +00:00
|
|
|
}
|