From 0904663bd02cd0ac8e11aee50e62f63c24cdd079 Mon Sep 17 00:00:00 2001 From: yamacir-kit Date: Tue, 3 Oct 2023 22:47:21 +0900 Subject: [PATCH] Update procedure `string-fill!` to built-in Signed-off-by: yamacir-kit --- README.md | 6 +-- VERSION | 2 +- basis/r4rs.ss | 35 +---------------- src/kernel/boot.cpp | 94 ++++++++++++++++++++++++++++++++++++--------- 4 files changed, 81 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index b0f7f53a2..11ad3e5a4 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Procedures for each standard are provided by the following R7RS-style libraries: cmake -B build -DCMAKE_BUILD_TYPE=Release cd build make package -sudo apt install build/meevax_0.5.17_amd64.deb +sudo apt install build/meevax_0.5.18_amd64.deb ``` or @@ -131,9 +131,9 @@ sudo rm -rf /usr/local/share/meevax | Target Name | Description |-------------|------------- -| `all` | Build shared-library `libmeevax.0.5.17.so` and executable `meevax` +| `all` | Build shared-library `libmeevax.0.5.18.so` and executable `meevax` | `test` | Test executable `meevax` -| `package` | Generate debian package `meevax_0.5.17_amd64.deb` +| `package` | Generate debian package `meevax_0.5.18_amd64.deb` | `install` | Copy files into `/usr/local` directly ## Usage diff --git a/VERSION b/VERSION index d8aef8135..b7a8bdf37 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.5.17 +0.5.18 diff --git a/basis/r4rs.ss b/basis/r4rs.ss index 9d49e4375..18064403f 100644 --- a/basis/r4rs.ss +++ b/basis/r4rs.ss @@ -46,25 +46,7 @@ (only (meevax inexact) exp log sqrt sin cos tan asin acos atan) (meevax list) (only (meevax macro-transformer) er-macro-transformer identifier?) - (only (meevax number) - number? complex? real? rational? integer? - exact? inexact? - = < > <= >= - zero? positive? negative? odd? even? - max min - + * - / - abs - quotient remainder modulo - gcd lcm - numerator denominator - floor ceiling truncate round - expt - exact inexact - string->number - - ; to be removed - exact-integer? ; r7rs - ) + (only (meevax number) number? complex? real? rational? integer? exact? inexact? = < > <= >= zero? positive? negative? odd? even? max min + * - / abs quotient remainder modulo gcd lcm numerator denominator floor ceiling truncate round expt exact inexact number->string string->number) (meevax pair) (meevax port) (prefix (meevax read) %) @@ -430,21 +412,6 @@ (define substring string-copy) - (define (string-fill! s c . o) ; Chibi-Scheme - (let ((start (if (and (pair? o) - (exact-integer? (car o))) - (car o) - 0)) - (end (if (and (pair? o) - (pair? (cdr o)) - (exact-integer? (cadr o))) - (cadr o) - (string-length s)))) - (let rec ((k (- end 1))) - (if (<= start k) - (begin (string-set! s k c) - (rec (- k 1))))))) - (define (for-each f x . xs) ; Chibi-Scheme (if (null? xs) (letrec ((for-each (lambda (f x) diff --git a/src/kernel/boot.cpp b/src/kernel/boot.cpp index 878c2793e..d10eb9550 100644 --- a/src/kernel/boot.cpp +++ b/src/kernel/boot.cpp @@ -770,9 +770,34 @@ inline namespace kernel return inexact(xs[0]); }); + library.define("number->string", [](let const& xs) + { + switch (length(xs)) + { + case 1: + return number_to_string(xs[0], 10); + + case 2: + return number_to_string(xs[0], xs[1].as()); + + default: + throw error(make("procedure number->string takes one or two arugments, but got"), xs); + } + }); + library.define("string->number", [](let const& xs) { - return make_number(xs[0].as(), 1 < length(xs) ? xs[1].as() : 10); + switch (length(xs)) + { + case 1: + return make_number(xs[0].as(), 10); + + case 2: + return make_number(xs[0].as(), xs[1].as()); + + default: + throw error(make("procedure string->number takes one or two arugments, but got"), xs); + } }); }); @@ -1139,6 +1164,33 @@ inline namespace kernel std::next(std::begin(s1), xs[1].as())); }); + library.define("string-fill!", [](let & xs) + { + switch (length(xs)) + { + case 2: + std::fill(xs[0].as().vector.begin(), + xs[0].as().vector.end(), + xs[1].as()); + break; + + case 3: + std::fill(std::next(xs[0].as().vector.begin(), + xs[2].as()), + xs[0].as().vector.end(), + xs[1].as()); + break; + + case 4: + std::fill(std::next(xs[0].as().vector.begin(), + xs[2].as()), + std::next(xs[0].as().vector.begin(), + xs[3].as()), + xs[1].as()); + break; + } + }); + #define STRING_COMPARE(COMPARE) \ [](let const& xs) \ { \ @@ -1168,11 +1220,6 @@ inline namespace kernel return make_symbol(xs[0].as()); }); - library.define("number->string", [](let const& xs) - { - return number_to_string(xs[0], 1 < length(xs) ? xs[1].as() : 10); - }); - library.define("string->list", [](let const& xs) { /* @@ -1449,18 +1496,29 @@ inline namespace kernel library.define("vector-fill!", [](let & xs) { - /* - (vector-fill! vector fill) procedure - (vector-fill! vector fill start) procedure - (vector-fill! vector fill start end) procedure - - The vector-fill! procedure stores fill in the elements of vector - between start and end. - */ - - std::fill(std::next(std::begin(xs[0].as().vector), 2 < length(xs) ? xs[2].as() : 0), - std::next(std::begin(xs[0].as().vector), 3 < length(xs) ? xs[3].as() : xs[0].as().vector.size()), - 1 < length(xs) ? xs[1] : unspecified); + switch (length(xs)) + { + case 2: + std::fill(xs[0].as().vector.begin(), + xs[0].as().vector.end(), + xs[1]); + break; + + case 3: + std::fill(std::next(xs[0].as().vector.begin(), + xs[2].as()), + xs[0].as().vector.end(), + xs[1]); + break; + + case 4: + std::fill(std::next(xs[0].as().vector.begin(), + xs[2].as()), + std::next(xs[0].as().vector.begin(), + xs[3].as()), + xs[1]); + break; + } }); library.define("vector->list", [](let const& xs)