X-Git-Url: https://jxself.org/git/?p=mes.git;a=blobdiff_plain;f=string.c;h=36c27816381fd4d466ae6a78eade536cd9bf651b;hp=0d87ce46c2365bc7059be2894b9104fddda68288;hb=7bf25a7e1799a9abaf7fd1a3f1c26555ac9cdcf1;hpb=16f678a1582943e57415516833506f710eb46600 diff --git a/string.c b/string.c index 0d87ce46..36c27816 100644 --- a/string.c +++ b/string.c @@ -18,78 +18,78 @@ * along with Mes. If not, see . */ -scm * -string (scm *x) ///((args . n)) +SCM +string (SCM x) ///((arity . n)) { return make_string (x); } -scm * -string_append (scm *x) ///((args . n)) +SCM +string_append (SCM x) ///((arity . n)) { - scm *p = &scm_nil; - while (x != &scm_nil) + SCM p = cell_nil; + while (x != cell_nil) { - scm *s = car (x); - assert (s->type == STRING); - p = append2 (p, s->string); + SCM s = car (x); + assert (TYPE (s) == STRING); + p = append2 (p, STRING (s)); x = cdr (x); } return make_string (p); } -scm * -list_to_string (scm *x) +SCM +list_to_string (SCM x) { return make_string (x); } -scm * -string_length (scm *x) +SCM +string_length (SCM x) { - assert (x->type == STRING); - return make_number (length (x->string)->value); + assert (TYPE (x) == STRING); + return make_number (VALUE (length (STRING (x)))); } -scm * -string_ref (scm *x, scm *k) +SCM +string_ref (SCM x, SCM k) { - assert (x->type == STRING); - assert (k->type == NUMBER); - scm n = {NUMBER, .value=k->value}; - return make_char (list_ref (x->string, &n)->value); + assert (TYPE (x) == STRING); + assert (TYPE (k) == NUMBER); + VALUE (tmp_num) = VALUE (k); + return make_char (VALUE (list_ref (STRING (x), tmp_num))); } -scm * -substring (scm *x) ///((args . n)) +SCM +substring (SCM x) ///((arity . n)) { - assert (x->type == PAIR); - assert (x->car->type == STRING); - scm *s = x->car->string; - assert (x->cdr->car->type == NUMBER); - int start = x->cdr->car->value; - int end = length (s)->value; - 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; + assert (TYPE (x) == PAIR); + assert (TYPE (car (x)) == STRING); + SCM s = STRING (car (x)); + assert (TYPE (cadr (x)) == NUMBER); + int start = VALUE (cadr (x)); + int end = VALUE (length (s)); + if (TYPE (cddr (x)) == PAIR) { + assert (TYPE (caddr (x)) == NUMBER); + assert (VALUE (caddr (x)) <= end); + end = VALUE (caddr (x)); } 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; + while (start--) s = cdr (s); + SCM p = cell_nil; + while (n-- && s != cell_nil) { + p = append2 (p, cons (make_char (VALUE (car (s))), cell_nil)); + s = cdr (s); } return make_string (p); } -scm * -number_to_string (scm *x) +SCM +number_to_string (SCM x) { - assert (x->type == NUMBER); - int n = x->value; - scm *p = n < 0 ? cons (make_char ('-'), &scm_nil) : &scm_nil; + assert (TYPE (x) == NUMBER); + int n = VALUE (x); + SCM p = n < 0 ? cons (make_char ('-'), cell_nil) : cell_nil; do { p = cons (make_char (n % 10 + '0'), p); n = n / 10; @@ -97,16 +97,30 @@ number_to_string (scm *x) return make_string (p); } -scm * -string_to_symbol (scm *x) +SCM +string_to_symbol (SCM x) { - assert (x->type == STRING); - return make_symbol (x->string); + assert (TYPE (x) == STRING); + return make_symbol (STRING (x)); } -scm * -symbol_to_string (scm *x) +SCM +symbol_to_string (SCM x) { - assert (x->type == SYMBOL); - return make_string (x->string); + assert (TYPE (x) == SYMBOL); + return make_string (STRING (x)); +} + +SCM +keyword_to_symbol (SCM x) +{ + assert (TYPE (x) == KEYWORD); + return make_symbol (STRING (x)); +} + +SCM +symbol_to_keyword (SCM x) +{ + assert (TYPE (x) == SYMBOL); + return make_keyword (STRING (x)); }