From 383026a64ebd1d7b70bf2e1c1073059bdd1879f3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 8 Feb 2014 22:30:37 +0900 Subject: [PATCH 01/20] update xfile --- extlib/xfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/xfile b/extlib/xfile index 54889153..4e2a62eb 160000 --- a/extlib/xfile +++ b/extlib/xfile @@ -1 +1 @@ -Subproject commit 54889153333c7aa5a228fcf624c730e727abc769 +Subproject commit 4e2a62ebe11e6787c69322ef847d39e869a48f26 From 8d067f66ed338ec40bbdd175494ffb9046a69739 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sat, 8 Feb 2014 23:23:53 +0900 Subject: [PATCH 02/20] add open-input-string --- src/port.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/port.c b/src/port.c index a1fc9822..16de0c57 100644 --- a/src/port.c +++ b/src/port.c @@ -196,6 +196,25 @@ pic_port_close_port(pic_state *pic) return pic_none_value(); } +static pic_value +pic_port_open_input_string(pic_state *pic) +{ + struct pic_port *port; + char *str; + size_t len; + + pic_get_args(pic, "s", &str, &len); + + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); + port->file = xmopen(); + port->flags = PIC_PORT_IN | PIC_PORT_TEXT; + port->status = PIC_PORT_OPEN; + + xfputs(str, port->file); + + return pic_obj_value(port); +} + #define assert_port_profile(port, flgs, stat, caller) do { \ if ((port->flags & (flgs)) != (flgs)) { \ switch (flgs) { \ @@ -385,6 +404,9 @@ pic_init_port(pic_state *pic) pic_defun(pic, "close-input-port", pic_port_close_port); pic_defun(pic, "close-output-port", pic_port_close_port); + /* string I/O */ + pic_defun(pic, "open-input-string", pic_port_open_input_string); + /* input */ pic_defun(pic, "read-char", pic_port_read_char); pic_defun(pic, "peek-char", pic_port_peek_char); From 89b2f7bbab5fca303360d242e3135f1c8147e098 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 00:13:24 +0900 Subject: [PATCH 03/20] update xfile --- extlib/xfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extlib/xfile b/extlib/xfile index 4e2a62eb..7c62dd5c 160000 --- a/extlib/xfile +++ b/extlib/xfile @@ -1 +1 @@ -Subproject commit 4e2a62ebe11e6787c69322ef847d39e869a48f26 +Subproject commit 7c62dd5c264ad1a43bd932b4097445ccc982a557 From 0e464b2f75b9387038bacef89569ecbd03da697d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 01:21:44 +0900 Subject: [PATCH 04/20] [bugfix] write-string doesn't detect given port --- src/port.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/port.c b/src/port.c index 16de0c57..a6b500d8 100644 --- a/src/port.c +++ b/src/port.c @@ -316,11 +316,11 @@ pic_port_write_string(pic_state *pic) n = pic_get_args(pic, "s|pii", &str, &len, &port, &start, &end); switch (n) { - case 2: + case 1: port = pic_stdout(pic); - case 3: + case 2: start = 0; - case 4: + case 3: end = len; } @@ -355,11 +355,11 @@ pic_port_write_blob(pic_state *pic) n = pic_get_args(pic, "b|pii", &blob, &port, &start, &end); switch (n) { - case 2: + case 1: port = pic_stdout(pic); - case 3: + case 2: start = 0; - case 4: + case 3: end = blob->len; } From a666951f21aa5053bb5f4f60498ba430076fc542 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 01:49:55 +0900 Subject: [PATCH 05/20] add open-output-string and get-output-string --- src/port.c | 80 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 19 deletions(-) diff --git a/src/port.c b/src/port.c index a6b500d8..7e934dae 100644 --- a/src/port.c +++ b/src/port.c @@ -196,25 +196,6 @@ pic_port_close_port(pic_state *pic) return pic_none_value(); } -static pic_value -pic_port_open_input_string(pic_state *pic) -{ - struct pic_port *port; - char *str; - size_t len; - - pic_get_args(pic, "s", &str, &len); - - port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); - port->file = xmopen(); - port->flags = PIC_PORT_IN | PIC_PORT_TEXT; - port->status = PIC_PORT_OPEN; - - xfputs(str, port->file); - - return pic_obj_value(port); -} - #define assert_port_profile(port, flgs, stat, caller) do { \ if ((port->flags & (flgs)) != (flgs)) { \ switch (flgs) { \ @@ -242,6 +223,65 @@ pic_port_open_input_string(pic_state *pic) } \ } while (0) +static pic_value +pic_port_open_input_string(pic_state *pic) +{ + struct pic_port *port; + char *str; + size_t len; + + pic_get_args(pic, "s", &str, &len); + + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); + port->file = xmopen(); + port->flags = PIC_PORT_IN | PIC_PORT_TEXT; + port->status = PIC_PORT_OPEN; + + xfputs(str, port->file); + xfflush(port->file); + xrewind(port->file); + + return pic_obj_value(port); +} + +static pic_value +pic_port_open_output_string(pic_state *pic) +{ + struct pic_port *port; + + pic_get_args(pic, ""); + + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); + port->file = xmopen(); + port->flags = PIC_PORT_OUT | PIC_PORT_TEXT; + port->status = PIC_PORT_OPEN; + + return pic_obj_value(port); +} + +static pic_value +pic_port_get_output_string(pic_state *pic) +{ + struct pic_port *port; + long endpos; + char *buf; + + pic_get_args(pic, "p", &port); + + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-char"); + + /* get endpos */ + xfflush(port->file); + endpos = xftell(port->file); + xrewind(port->file); + + /* copy to buf */ + buf = (char *)pic_alloc(pic, endpos); + xfread(buf, 1, endpos, port->file); + + return pic_obj_value(pic_str_new(pic, buf, endpos)); +} + static pic_value pic_port_read_char(pic_state *pic) { @@ -406,6 +446,8 @@ pic_init_port(pic_state *pic) /* string I/O */ pic_defun(pic, "open-input-string", pic_port_open_input_string); + pic_defun(pic, "open-output-string", pic_port_open_output_string); + pic_defun(pic, "get-output-string", pic_port_get_output_string); /* input */ pic_defun(pic, "read-char", pic_port_read_char); From 3d0657074fbaa6f6bee6533193728c097e1e313f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 02:41:20 +0900 Subject: [PATCH 06/20] first argument of get-output-string is optinoal --- src/port.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/port.c b/src/port.c index 7e934dae..2ee95807 100644 --- a/src/port.c +++ b/src/port.c @@ -262,11 +262,11 @@ pic_port_open_output_string(pic_state *pic) static pic_value pic_port_get_output_string(pic_state *pic) { - struct pic_port *port; + struct pic_port *port = pic_stdout(pic);; long endpos; char *buf; - pic_get_args(pic, "p", &port); + pic_get_args(pic, "|p", &port); assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-char"); From 7187e4e982e122b7eadb58e90650ed4656321fa0 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 02:41:52 +0900 Subject: [PATCH 07/20] add open-input-bytevector --- src/port.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/port.c b/src/port.c index 2ee95807..93698bef 100644 --- a/src/port.c +++ b/src/port.c @@ -282,6 +282,24 @@ pic_port_get_output_string(pic_state *pic) return pic_obj_value(pic_str_new(pic, buf, endpos)); } +static pic_value +pic_port_open_input_blob(pic_state *pic) +{ + struct pic_port *port; + struct pic_blob *blob; + + pic_get_args(pic, "b", &blob); + + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); + port->file = xmopen(); + port->flags = PIC_PORT_IN | PIC_PORT_BINARY; + port->status = PIC_PORT_OPEN; + + xfwrite(blob->data, 1, blob->len, port->file); + + return pic_obj_value(port); +} + static pic_value pic_port_read_char(pic_state *pic) { @@ -448,6 +466,7 @@ pic_init_port(pic_state *pic) pic_defun(pic, "open-input-string", pic_port_open_input_string); pic_defun(pic, "open-output-string", pic_port_open_output_string); pic_defun(pic, "get-output-string", pic_port_get_output_string); + pic_defun(pic, "open-input-bytevector", pic_port_open_input_blob); /* input */ pic_defun(pic, "read-char", pic_port_read_char); From 39af5ca254fd383dea5f4e88c404f7f79ab97432 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 02:42:50 +0900 Subject: [PATCH 08/20] implement string-append in C --- piclib/built-in.scm | 8 -------- src/string.c | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/piclib/built-in.scm b/piclib/built-in.scm index 55ac8686..2ceec2e7 100644 --- a/piclib/built-in.scm +++ b/piclib/built-in.scm @@ -476,14 +476,6 @@ (string-copy! res 0 v start end) res))) -(define (string-append . vs) - (define (string-append-2-inv w v) - (let ((res (make-string (+ (string-length v) (string-length w))))) - (string-copy! res 0 v) - (string-copy! res (string-length v) w) - res)) - (fold string-append-2-inv #() vs)) - (define (string-fill! v fill . opts) (let ((start (if (pair? opts) (car opts) 0)) (end (if (>= (length opts) 2) diff --git a/src/string.c b/src/string.c index 1e3044fb..4d5ea02f 100644 --- a/src/string.c +++ b/src/string.c @@ -122,6 +122,29 @@ DEFINE_STRING_CMP(gt, >) DEFINE_STRING_CMP(le, <=) DEFINE_STRING_CMP(ge, >=) +static pic_value +pic_str_string_append(pic_state *pic) +{ + size_t argc, len, i; + pic_value *argv; + char *buf; + + pic_get_args(pic, "*", &argc, &argv); + + len = 0; + buf = NULL; + for (i = 0; i < argc; ++i) { + if (! pic_str_p(argv[i])) { + pic_error(pic, "type error"); + } + buf = pic_realloc(pic, buf, len + pic_str_ptr(argv[i])->len); + /* copy! */ + memcpy(buf + len, pic_str_ptr(argv[i])->str, pic_str_ptr(argv[i])->len); + len += pic_str_ptr(argv[i])->len; + } + return pic_obj_value(pic_str_new(pic, buf, len)); +} + void pic_init_str(pic_state *pic) { @@ -135,4 +158,5 @@ pic_init_str(pic_state *pic) pic_defun(pic, "string>?", pic_str_string_gt); pic_defun(pic, "string<=?", pic_str_string_le); pic_defun(pic, "string>=?", pic_str_string_ge); + pic_defun(pic, "string-append", pic_str_string_append); } From ca965d088a45246c3c900c7d8a13b7907582483d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 02:44:19 +0900 Subject: [PATCH 09/20] add a new test --- t/parameterize.scm | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 t/parameterize.scm diff --git a/t/parameterize.scm b/t/parameterize.scm new file mode 100644 index 00000000..4d0a8571 --- /dev/null +++ b/t/parameterize.scm @@ -0,0 +1,12 @@ +(import (scheme base) + (scheme write)) + +; expects "piece by piece by piece.\n" +(write + (parameterize + ((current-output-port (open-output-string))) + (display "piece") + (display " by piece ") + (display "by piece.") + (newline) + (get-output-string))) From 668ace7901c5fa2a226bf31af3581eff944ab950 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 03:10:58 +0900 Subject: [PATCH 10/20] implement some string functions in c --- piclib/built-in.scm | 33 +------------------- src/string.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 32 deletions(-) diff --git a/piclib/built-in.scm b/piclib/built-in.scm index 2ceec2e7..2dcaade4 100644 --- a/piclib/built-in.scm +++ b/piclib/built-in.scm @@ -457,38 +457,7 @@ (define (list->string list) (apply string list)) -(define (string-copy! to at from . opts) - (let ((start (if (pair? opts) (car opts) 0)) - (end (if (>= (length opts) 2) - (cadr opts) - (string-length from)))) - (do ((i at (+ i 1)) - (j start (+ j 1))) - ((= j end)) - (string-set! to i (string-ref from j))))) - -(define (string-copy v . opts) - (let ((start (if (pair? opts) (car opts) 0)) - (end (if (>= (length opts) 2) - (cadr opts) - (string-length v)))) - (let ((res (make-string (string-length v)))) - (string-copy! res 0 v start end) - res))) - -(define (string-fill! v fill . opts) - (let ((start (if (pair? opts) (car opts) 0)) - (end (if (>= (length opts) 2) - (cadr opts) - (string-length v)))) - (do ((i start (+ i 1))) - ((= i end) - #f) - (string-set! v i fill)))) - -(export string string->list list->string - string-copy! string-copy - string-append string-fill!) +(export string string->list list->string) ;;; 6.8. Vector diff --git a/src/string.c b/src/string.c index 4d5ea02f..0d9a639f 100644 --- a/src/string.c +++ b/src/string.c @@ -122,6 +122,52 @@ DEFINE_STRING_CMP(gt, >) DEFINE_STRING_CMP(le, <=) DEFINE_STRING_CMP(ge, >=) +static pic_value +pic_str_string_copy(pic_state *pic) +{ + size_t len, start, end, i; + char *str; + int n; + struct pic_string *copy; + + n = pic_get_args(pic, "s|ii", &str, &len, &start, &end); + + switch (n) { + case 1: + start = 0; + case 2: + end = len; + } + + copy = pic_str_new(pic, NULL, end - start); + for (i = 0; i < end - start; ++i) { + copy->str[i] = str[start + i]; + } + return pic_obj_value(copy); +} + +static pic_value +pic_str_string_copy_ip(pic_state *pic) +{ + size_t to_len, from_len, at, start, end; + char *to_str, *from_str; + int n; + + n = pic_get_args(pic, "sis|ii", &to_str, &to_len, &at, &from_str, &from_len, &start, &end); + + switch (n) { + case 3: + start = 0; + case 4: + end = from_len; + } + + while (start < end) { + to_str[at++] = from_str[start++]; + } + return pic_none_value(); +} + static pic_value pic_str_string_append(pic_state *pic) { @@ -145,6 +191,28 @@ pic_str_string_append(pic_state *pic) return pic_obj_value(pic_str_new(pic, buf, len)); } +static pic_value +pic_str_string_fill_ip(pic_state *pic) +{ + size_t len, start, end; + char *str, c; + int n; + + n = pic_get_args(pic, "sc|ii", &str, &len, &c, &start, &end); + + switch (n) { + case 1: + start = 0; + case 2: + end = len; + } + + while (start < end) { + str[start++] = c; + } + return pic_none_value(); +} + void pic_init_str(pic_state *pic) { @@ -153,10 +221,15 @@ pic_init_str(pic_state *pic) pic_defun(pic, "string-length", pic_str_string_length); pic_defun(pic, "string-ref", pic_str_string_ref); pic_defun(pic, "string-set!", pic_str_string_set); + pic_defun(pic, "string=?", pic_str_string_eq); pic_defun(pic, "string?", pic_str_string_gt); pic_defun(pic, "string<=?", pic_str_string_le); pic_defun(pic, "string>=?", pic_str_string_ge); + + pic_defun(pic, "string-copy", pic_str_string_copy); + pic_defun(pic, "string-copy!", pic_str_string_copy_ip); pic_defun(pic, "string-append", pic_str_string_append); + pic_defun(pic, "string-fill!", pic_str_string_fill_ip); } From 49e5e3085b9519f7b2804fdb38947b246e9999af Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 03:11:08 +0900 Subject: [PATCH 11/20] pic_str_new may take a NULL ptr --- src/string.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/string.c b/src/string.c index 0d9a639f..2c44c415 100644 --- a/src/string.c +++ b/src/string.c @@ -10,10 +10,17 @@ struct pic_string * pic_str_new(pic_state *pic, const char *cstr, size_t len) { struct pic_string *str; + char *copy; + + if (cstr) { + copy = pic_strdup(pic, cstr); + } else { + copy = (char *)pic_alloc(pic, len); + } str = (struct pic_string *)pic_obj_alloc(pic, sizeof(struct pic_string), PIC_TT_STRING); str->len = len; - str->str = pic_strdup(pic, cstr); + str->str = copy; return str; } From a08a19bed1a4da882b3f89de08b7199933ca8f82 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 03:15:50 +0900 Subject: [PATCH 12/20] cosmetic changes --- piclib/built-in.scm | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/piclib/built-in.scm b/piclib/built-in.scm index 2dcaade4..680b0e16 100644 --- a/piclib/built-in.scm +++ b/piclib/built-in.scm @@ -280,14 +280,19 @@ (lambda (form r compare) (let ((bindings (cadr form)) (body (cddr form))) - (let ((vars (map car bindings))) + (let ((vars (map car bindings)) + (gensym (lambda (var) + (string->symbol + (string-append + "parameterize-" + (symbol->string var)))))) `(,(r 'let) (,@(map (lambda (var) - `(,(r var) (,var))) + `(,(r (gensym var)) (,var))) vars)) ,@bindings (,(r 'let) ((,(r 'result) (begin ,@body))) ,@(map (lambda (var) - `(,(r 'parameter-set!) ,var ,(r var))) + `(,(r 'parameter-set!) ,var ,(r (gensym var)))) vars) ,(r 'result)))))))) @@ -680,14 +685,14 @@ (define-library (scheme cxr) (import (scheme base)) - (define (caaar p) (car (caar p))) - (define (caadr p) (car (cadr p))) - (define (cadar p) (car (cdar p))) - (define (caddr p) (car (cddr p))) - (define (cdaar p) (cdr (caar p))) - (define (cdadr p) (cdr (cadr p))) - (define (cddar p) (cdr (cdar p))) - (define (cdddr p) (cdr (cddr p))) + (define (caaar p) (car (caar p))) + (define (caadr p) (car (cadr p))) + (define (cadar p) (car (cdar p))) + (define (caddr p) (car (cddr p))) + (define (cdaar p) (cdr (caar p))) + (define (cdadr p) (cdr (cadr p))) + (define (cddar p) (cdr (cdar p))) + (define (cdddr p) (cdr (cddr p))) (define (caaaar p) (caar (caar p))) (define (caaadr p) (caar (cadr p))) (define (caadar p) (caar (cdar p))) From cf152e2249d3ee6e7632aaef1ee283110cb67db1 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 03:17:51 +0900 Subject: [PATCH 13/20] remove (picrin bootstrap-utils) library --- piclib/built-in.scm | 95 ++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 53 deletions(-) diff --git a/piclib/built-in.scm b/piclib/built-in.scm index 680b0e16..41e33bce 100644 --- a/piclib/built-in.scm +++ b/piclib/built-in.scm @@ -1,3 +1,39 @@ +;;; Appendix A. Standard Libraries CxR +(define-library (scheme cxr) + (import (scheme base)) + + (define (caaar p) (car (caar p))) + (define (caadr p) (car (cadr p))) + (define (cadar p) (car (cdar p))) + (define (caddr p) (car (cddr p))) + (define (cdaar p) (cdr (caar p))) + (define (cdadr p) (cdr (cadr p))) + (define (cddar p) (cdr (cdar p))) + (define (cdddr p) (cdr (cddr p))) + (define (caaaar p) (caar (caar p))) + (define (caaadr p) (caar (cadr p))) + (define (caadar p) (caar (cdar p))) + (define (caaddr p) (caar (cddr p))) + (define (cadaar p) (cadr (caar p))) + (define (cadadr p) (cadr (cadr p))) + (define (caddar p) (cadr (cdar p))) + (define (cadddr p) (cadr (cddr p))) + (define (cdaaar p) (cdar (caar p))) + (define (cdaadr p) (cdar (cadr p))) + (define (cdadar p) (cdar (cdar p))) + (define (cdaddr p) (cdar (cddr p))) + (define (cddaar p) (cddr (caar p))) + (define (cddadr p) (cddr (cadr p))) + (define (cdddar p) (cddr (cdar p))) + (define (cddddr p) (cddr (cddr p))) + + (export caaar caadr cadar caddr + cdaar cdadr cddar cdddr + caaaar caaadr caadar caaddr + cadaar cadadr caddar cadddr + cdaaar cdaadr cdadar cdaddr + cddaar cddadr cdddar cddddr)) + ;;; hygienic macros (define-library (picrin macro) (import (scheme base)) @@ -13,21 +49,11 @@ (export sc-macro-transformer rsc-macro-transformer)) -;;; bootstrap utilities -(define-library (picrin bootstrap-tools) - (import (scheme base)) - - (define (cadar p) (car (cdar p))) - (define (caddr p) (car (cddr p))) - (define (cdddr p) (cdr (cddr p))) - - (export cadar caddr cdddr)) - ;;; core syntaces (define-library (picrin core-syntax) (import (scheme base) - (picrin macro) - (picrin bootstrap-tools)) + (scheme cxr) + (picrin macro)) (define-syntax let (er-macro-transformer @@ -222,9 +248,9 @@ ;;; multiple value (define-library (picrin multiple-value) (import (scheme base) + (scheme cxr) (picrin macro) - (picrin core-syntax) - (picrin bootstrap-tools)) + (picrin core-syntax)) (define-syntax let*-values (er-macro-transformer @@ -268,9 +294,9 @@ ;;; parameter (define-library (picrin parameter) (import (scheme base) + (scheme cxr) (picrin macro) - (picrin core-syntax) - (picrin bootstrap-tools)) + (picrin core-syntax)) ;; reopen (pircin parameter) ;; see src/var.c @@ -679,40 +705,3 @@ (write obj port))))) (export write write-shared display)) - -;;; Appendix A. Standard Libraries -;; CxR -(define-library (scheme cxr) - (import (scheme base)) - - (define (caaar p) (car (caar p))) - (define (caadr p) (car (cadr p))) - (define (cadar p) (car (cdar p))) - (define (caddr p) (car (cddr p))) - (define (cdaar p) (cdr (caar p))) - (define (cdadr p) (cdr (cadr p))) - (define (cddar p) (cdr (cdar p))) - (define (cdddr p) (cdr (cddr p))) - (define (caaaar p) (caar (caar p))) - (define (caaadr p) (caar (cadr p))) - (define (caadar p) (caar (cdar p))) - (define (caaddr p) (caar (cddr p))) - (define (cadaar p) (cadr (caar p))) - (define (cadadr p) (cadr (cadr p))) - (define (caddar p) (cadr (cdar p))) - (define (cadddr p) (cadr (cddr p))) - (define (cdaaar p) (cdar (caar p))) - (define (cdaadr p) (cdar (cadr p))) - (define (cdadar p) (cdar (cdar p))) - (define (cdaddr p) (cdar (cddr p))) - (define (cddaar p) (cddr (caar p))) - (define (cddadr p) (cddr (cadr p))) - (define (cdddar p) (cddr (cdar p))) - (define (cddddr p) (cddr (cddr p))) - - (export caaar caadr cadar caddr - cdaar cdadr cddar cdddr - caaaar caaadr caadar caaddr - cadaar cadadr caddar cadddr - cdaaar cdaadr cdadar cdaddr - cddaar cddadr cdddar cddddr)) From 6538dfa2cfa1e40d9ce57b948c19f91a94f2dbcf Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 03:41:13 +0900 Subject: [PATCH 14/20] cleanup --- piclib/built-in.scm | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/piclib/built-in.scm b/piclib/built-in.scm index 41e33bce..ce3ed630 100644 --- a/piclib/built-in.scm +++ b/piclib/built-in.scm @@ -465,15 +465,6 @@ ;;; 6.7 String -(define (string . objs) - (let ((len (length objs))) - (let ((v (make-string len))) - (do ((i 0 (+ i 1)) - (l objs (cdr l))) - ((= i len) - v) - (string-set! v i (car l)))))) - (define (string->list string . opts) (let ((start (if (pair? opts) (car opts) 0)) (end (if (>= (length opts) 2) @@ -486,7 +477,16 @@ (set! res (cons (string-ref string i) res))))) (define (list->string list) - (apply string list)) + (let ((len (length list))) + (let ((v (make-string len))) + (do ((i 0 (+ i 1)) + (l list (cdr l))) + ((= i len) + v) + (string-set! v i (car l)))))) + +(define (string . objs) + (list->string objs)) (export string string->list list->string) From b8d44614019083fec24458628c56ae12ae968416 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 13:35:55 +0900 Subject: [PATCH 15/20] add a comment --- include/picrin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/picrin.h b/include/picrin.h index 05fb85a7..48de7c19 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -160,7 +160,7 @@ bool pic_interned_p(pic_state *, pic_sym); char *pic_strdup(pic_state *, const char *); char *pic_strndup(pic_state *, const char *, size_t); -struct pic_string *pic_str_new(pic_state *, const char *, size_t); +struct pic_string *pic_str_new(pic_state *, const char *, size_t); /* the 2nd arg may be NULL for empty string */ struct pic_string *pic_str_new_cstr(pic_state *, const char *); struct pic_vector *pic_vec_new(pic_state *, size_t); From c5c86f53e38b20602ae8ee11e38dca05fd038daf Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 14:08:14 +0900 Subject: [PATCH 16/20] update readme. string I/O has been landed --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b9176439..647061af 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Picrin is a lightweight scheme implementation intended to comply with full R7RS | 6.10 Control features | yes | | | 6.11 Exceptions | yes | TODO: native error handling | | 6.12 Environments and evaluation | N/A | | -| 6.13.1 Ports | incomplete | TODO: string I/O, bytevector I/O | +| 6.13.1 Ports | incomplete | TODO: bytevector I/O | | 6.13.2 Input | incomplete | | | 6.13.3 Output | yes | TODO: `write`, `write-shared` and `display` are unsafe against circular objects | | 6.14 System interface | yes | | From d3f366770c4b312a94e96c1e3a1d2e20cb909d14 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 14:08:40 +0900 Subject: [PATCH 17/20] update readme. equal? is incomplete --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 647061af..ab381958 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Picrin is a lightweight scheme implementation intended to comply with full R7RS | 5.6.1 Library Syntax | incomplete | In picrin, libraries can be reopend and can be nested. | | 5.6.2 Library example | N/A | | | 5.7 The REPL | yes | | -| 6.1 Equivalence predicates | yes | | +| 6.1 Equivalence predicates | yes | TODO: equal? must terminate if circular structure is given | | 6.2.1 Numerical types | yes | picrin has only two types of internal representation of numbers: fixnum and double float. It still comforms the R7RS spec. | | 6.2.2 Exactness | yes | | | 6.2.3 Implementation restrictions | yes | | From 241012d2f93f316b1e43cf2a8bbede287645aaf2 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 14:29:28 +0900 Subject: [PATCH 18/20] add output-bytevector --- src/port.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/port.c b/src/port.c index 93698bef..5a92eb1b 100644 --- a/src/port.c +++ b/src/port.c @@ -300,6 +300,44 @@ pic_port_open_input_blob(pic_state *pic) return pic_obj_value(port); } +static pic_value +pic_port_open_output_bytevector(pic_state *pic) +{ + struct pic_port *port; + + pic_get_args(pic, ""); + + port = (struct pic_port *)pic_obj_alloc(pic, sizeof(struct pic_port *), PIC_TT_PORT); + port->file = xmopen(); + port->flags = PIC_PORT_OUT | PIC_PORT_BINARY; + port->status = PIC_PORT_OPEN; + + return pic_obj_value(port); +} + +static pic_value +pic_port_get_output_bytevector(pic_state *pic) +{ + struct pic_port *port = pic_stdout(pic);; + long endpos; + char *buf; + + pic_get_args(pic, "|p", &port); + + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_BINARY, PIC_PORT_OPEN, "get-output-bytevector"); + + /* get endpos */ + xfflush(port->file); + endpos = xftell(port->file); + xrewind(port->file); + + /* copy to buf */ + buf = (char *)pic_alloc(pic, endpos); + xfread(buf, 1, endpos, port->file); + + return pic_obj_value(pic_blob_new(pic, buf, endpos)); +} + static pic_value pic_port_read_char(pic_state *pic) { @@ -467,6 +505,8 @@ pic_init_port(pic_state *pic) pic_defun(pic, "open-output-string", pic_port_open_output_string); pic_defun(pic, "get-output-string", pic_port_get_output_string); pic_defun(pic, "open-input-bytevector", pic_port_open_input_blob); + pic_defun(pic, "open-output-bytevector", pic_port_open_output_bytevector); + pic_defun(pic, "get-output-bytevector", pic_port_get_output_bytevector); /* input */ pic_defun(pic, "read-char", pic_port_read_char); From 687706eaee9b708bf1acb72897f1b2c10508789f Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 14:29:51 +0900 Subject: [PATCH 19/20] fix error message --- src/port.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/port.c b/src/port.c index 5a92eb1b..83e58134 100644 --- a/src/port.c +++ b/src/port.c @@ -268,7 +268,7 @@ pic_port_get_output_string(pic_state *pic) pic_get_args(pic, "|p", &port); - assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "read-char"); + assert_port_profile(port, PIC_PORT_OUT | PIC_PORT_TEXT, PIC_PORT_OPEN, "get-output-string"); /* get endpos */ xfflush(port->file); From 62988a8c19b106ee89faccbae722652530911445 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 9 Feb 2014 14:30:34 +0900 Subject: [PATCH 20/20] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ab381958..983656ea 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Picrin is a lightweight scheme implementation intended to comply with full R7RS | 6.10 Control features | yes | | | 6.11 Exceptions | yes | TODO: native error handling | | 6.12 Environments and evaluation | N/A | | -| 6.13.1 Ports | incomplete | TODO: bytevector I/O | +| 6.13.1 Ports | yes | | | 6.13.2 Input | incomplete | | | 6.13.3 Output | yes | TODO: `write`, `write-shared` and `display` are unsafe against circular objects | | 6.14 System interface | yes | |