From 479d988e420f99d94c9e28e5a178d18f9a8677ba Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Mon, 25 Jul 2016 00:06:18 +0200 Subject: [PATCH] mes.c: add set-cdr, substring, string-ref. --- mes.c | 36 ++++++++++++++++++++++++++++++++++++ scm.mes | 1 + test.mes | 3 +++ 3 files changed, 40 insertions(+) diff --git a/mes.c b/mes.c index 133fb93c..93204ec5 100644 --- a/mes.c +++ b/mes.c @@ -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) { diff --git a/scm.mes b/scm.mes index 0bbd4a39..0ecf363f 100755 --- a/scm.mes +++ b/scm.mes @@ -138,6 +138,7 @@ #f))) (define assv assq) +(define assv-ref assq-ref) (define (assoc key alist) (cond ((null? alist) #f) diff --git a/test.mes b/test.mes index ae525b93..02ff08f8 100644 --- a/test.mes +++ b/test.mes @@ -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))