mes.c: add set-cdr, substring, string-ref.

This commit is contained in:
Jan Nieuwenhuizen 2016-07-25 00:06:18 +02:00
parent 87678add4a
commit 479d988e42
3 changed files with 40 additions and 0 deletions

36
mes.c
View file

@ -187,6 +187,14 @@ pair_p (scm *x)
return x->type == PAIR ? &scm_t : &scm_f;
}
scm *
set_car_x (scm *x, scm *e)
{
assert (x->type == PAIR);
x->car = e;
return &scm_unspecified;
}
scm *
set_cdr_x (scm *x, scm *e)
{
@ -633,6 +641,34 @@ string_length (scm *x)
return make_number (strlen (x->name));
}
scm *
string_ref (scm *x, scm *k)
{
assert (x->type == STRING);
assert (k->type == NUMBER);
return make_char (x->name[k->value]);
}
scm *
substring (scm *x/*...*/)
{
assert (x->type == PAIR);
assert (x->car->type == STRING);
char *s = x->car->name;
assert (x->cdr->car->type == NUMBER);
int start = x->cdr->car->value;
int end = strlen (s);
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;
}
char buf[256];
strncpy (buf, s+start, end - start);
buf[end-start] = 0;
return make_string (buf);
}
scm *
length (scm *x)
{

View file

@ -138,6 +138,7 @@
#f)))
(define assv assq)
(define assv-ref assq-ref)
(define (assoc key alist)
(cond ((null? alist) #f)

View file

@ -119,6 +119,9 @@
24))
(pass-if "begin" (seq? (begin 'a 'b (+ 1 2)) 3))
(pass-if "string-append" (sequal? (string-append "a" "b" "c") "abc"))
(pass-if "substring" (sequal? (substring "hello world" 6) "world"))
(pass-if "substring 2" (sequal? (substring "hello world" 4 7) "o w"))
(pass-if "string-ref" (seq? (string-ref "hello world" 4) #\o))
(pass-if "eq?" (not (eq? (string-append "a" "b" "c") "abc")))
(pass-if "string-length" (seq? (string-length (string-append "a" "b" "c")) 3))
(pass-if "char" (seq? (char->integer #\A) 65))