From 6533fc9b5bb45d6031bffa91b868c720860217b9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 8 Aug 2014 13:59:15 +0900 Subject: [PATCH 1/5] =?UTF-8?q?[bugfix]=20identifier=3D=3F?= --- piclib/picrin/macro.scm | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/piclib/picrin/macro.scm b/piclib/picrin/macro.scm index 3b12d3ff..6387e15c 100644 --- a/piclib/picrin/macro.scm +++ b/piclib/picrin/macro.scm @@ -35,8 +35,16 @@ val)))) (define (identifier=? env1 sym1 env2 sym2) - (eq? (make-identifier sym1 env1) - (make-identifier sym2 env2))) + + (define (resolve sym env) + (define x (make-identifier sym env)) + (define y (make-identifier sym env)) + (if (eq? x y) + x + sym)) ; resolved to no variable + + (eq? (resolve sym1 env1) + (resolve sym2 env2))) (define (make-syntactic-closure env free form) From d9ffc9be20fc0cac06f556626aceb341a3ef08c3 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 8 Aug 2014 14:59:08 +0900 Subject: [PATCH 2/5] add srfi-27 --- contrib/10.random/CMakeLists.txt | 11 ++ contrib/10.random/src/mt19937ar.c | 194 ++++++++++++++++++++++++++++++ contrib/10.random/src/random.c | 19 +++ contrib/10.random/t/test.scm | 5 + 4 files changed, 229 insertions(+) create mode 100644 contrib/10.random/CMakeLists.txt create mode 100644 contrib/10.random/src/mt19937ar.c create mode 100644 contrib/10.random/src/random.c create mode 100644 contrib/10.random/t/test.scm diff --git a/contrib/10.random/CMakeLists.txt b/contrib/10.random/CMakeLists.txt new file mode 100644 index 00000000..224686b9 --- /dev/null +++ b/contrib/10.random/CMakeLists.txt @@ -0,0 +1,11 @@ +file(GLOB PICRIN_RANDOM_SOURCES ${PROJECT_SOURCE_DIR}/contrib/10.random/src/*.c) + +list(APPEND PICRIN_CONTRIB_INITS random) +list(APPEND PICRIN_CONTRIB_SOURCES ${PICRIN_RANDOM_SOURCES}) +add_custom_target(test-random + for test in ${PROJECT_SOURCE_DIR}/contrib/10.random/t/*.scm \; + do + bin/picrin "$$test" \; + done + DEPENDS repl) +set(CONTRIB_TESTS ${CONTRIB_TESTS} test-random) diff --git a/contrib/10.random/src/mt19937ar.c b/contrib/10.random/src/mt19937ar.c new file mode 100644 index 00000000..fbc2d3ae --- /dev/null +++ b/contrib/10.random/src/mt19937ar.c @@ -0,0 +1,194 @@ +/* + A C-program for MT19937, with initialization improved 2002/1/26. + Coded by Takuji Nishimura and Makoto Matsumoto. + + Before using, initialize the state by using init_genrand(seed) + or init_by_array(init_key, key_length). + + Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + + Any feedback is very welcome. + http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html + email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) +*/ + +/* Period parameters */ +#define N 624 +#define M 397 +#define MATRIX_A 0x9908b0dfUL /* constant vector a */ +#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ +#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ + +static unsigned long mt[N]; /* the array for the state vector */ +static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ + +/* initializes mt[N] with a seed */ +void init_genrand(unsigned long s) +{ + mt[0]= s & 0xffffffffUL; + for (mti=1; mti> 30)) + mti); + /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ + /* In the previous versions, MSBs of the seed affect */ + /* only MSBs of the array mt[]. */ + /* 2002/01/09 modified by Makoto Matsumoto */ + mt[mti] &= 0xffffffffUL; + /* for >32 bit machines */ + } +} + +/* initialize by an array with array-length */ +/* init_key is the array for initializing keys */ +/* key_length is its length */ +/* slight change for C++, 2004/2/26 */ +void init_by_array(unsigned long init_key[], int key_length) +{ + int i, j, k; + init_genrand(19650218UL); + i=1; j=0; + k = (N>key_length ? N : key_length); + for (; k; k--) { + mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) + + init_key[j] + j; /* non linear */ + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ + i++; j++; + if (i>=N) { mt[0] = mt[N-1]; i=1; } + if (j>=key_length) j=0; + } + for (k=N-1; k; k--) { + mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) + - i; /* non linear */ + mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ + i++; + if (i>=N) { mt[0] = mt[N-1]; i=1; } + } + + mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ +} + +/* generates a random number on [0,0xffffffff]-interval */ +unsigned long genrand_int32(void) +{ + unsigned long y; + static unsigned long mag01[2]={0x0UL, MATRIX_A}; + /* mag01[x] = x * MATRIX_A for x=0,1 */ + + if (mti >= N) { /* generate N words at one time */ + int kk; + + if (mti == N+1) /* if init_genrand() has not been called, */ + init_genrand(5489UL); /* a default initial seed is used */ + + for (kk=0;kk> 1) ^ mag01[y & 0x1UL]; + } + for (;kk> 1) ^ mag01[y & 0x1UL]; + } + y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); + mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; + + mti = 0; + } + + y = mt[mti++]; + + /* Tempering */ + y ^= (y >> 11); + y ^= (y << 7) & 0x9d2c5680UL; + y ^= (y << 15) & 0xefc60000UL; + y ^= (y >> 18); + + return y; +} + +/* generates a random number on [0,0x7fffffff]-interval */ +long genrand_int31(void) +{ + return (long)(genrand_int32()>>1); +} + +/* generates a random number on [0,1]-real-interval */ +double genrand_real1(void) +{ + return genrand_int32()*(1.0/4294967295.0); + /* divided by 2^32-1 */ +} + +/* generates a random number on [0,1)-real-interval */ +double genrand_real2(void) +{ + return genrand_int32()*(1.0/4294967296.0); + /* divided by 2^32 */ +} + +/* generates a random number on (0,1)-real-interval */ +double genrand_real3(void) +{ + return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0); + /* divided by 2^32 */ +} + +/* generates a random number on [0,1) with 53-bit resolution*/ +double genrand_res53(void) +{ + unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; + return(a*67108864.0+b)*(1.0/9007199254740992.0); +} +/* These real versions are due to Isaku Wada, 2002/01/09 added */ + +#if 0 + +#include + +int main(void) +{ + int i; + unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4; + init_by_array(init, length); + printf("1000 outputs of genrand_int32()\n"); + for (i=0; i<1000; i++) { + printf("%10lu ", genrand_int32()); + if (i%5==4) printf("\n"); + } + printf("\n1000 outputs of genrand_real2()\n"); + for (i=0; i<1000; i++) { + printf("%10.8f ", genrand_real2()); + if (i%5==4) printf("\n"); + } + return 0; +} + +#endif diff --git a/contrib/10.random/src/random.c b/contrib/10.random/src/random.c new file mode 100644 index 00000000..a750f92b --- /dev/null +++ b/contrib/10.random/src/random.c @@ -0,0 +1,19 @@ +#include "picrin.h" + +double genrand_real3(void); + +static pic_value +pic_random_real(pic_state *pic) +{ + pic_get_args(pic, ""); + + return pic_float_value(genrand_real3()); +} + +void +pic_init_random(pic_state *pic) +{ + pic_deflibrary (pic, "(srfi 27)") { + pic_defun(pic, "random-real", pic_random_real); + } +} diff --git a/contrib/10.random/t/test.scm b/contrib/10.random/t/test.scm new file mode 100644 index 00000000..7b3547ef --- /dev/null +++ b/contrib/10.random/t/test.scm @@ -0,0 +1,5 @@ +(import (srfi 27) + (scheme base) + (picrin test)) + +(test #t (procedure? random-real)) From a023db80540457634dab5875700fb93c58c1fc08 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 8 Aug 2014 15:07:21 +0900 Subject: [PATCH 3/5] drop support for rational literal --- src/read.c | 11 ----------- t/r7rs-tests.scm | 18 +++++++++--------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/read.c b/src/read.c index 489426b5..d1546104 100644 --- a/src/read.c +++ b/src/read.c @@ -241,7 +241,6 @@ read_number(pic_state *pic, struct pic_port *port, int c) { char buf[256]; size_t i; - long n; i = read_uinteger(pic, port, c, buf); @@ -253,16 +252,6 @@ read_number(pic_state *pic, struct pic_port *port, int c) buf[i] = '\0'; return pic_float_value(atof(buf)); - case '/': - n = atoi(buf); - next(port); - read_uinteger(pic, port, next(port), buf); - if (n == n / atoi(buf) * atoi(buf)) { - return pic_int_value(n / atoi(buf)); /* exact */ - } else { - return pic_float_value(n / (double)atoi(buf)); - } - default: return pic_int_value(atoi(buf)); } diff --git a/t/r7rs-tests.scm b/t/r7rs-tests.scm index 19050d71..b96b14eb 100644 --- a/t/r7rs-tests.scm +++ b/t/r7rs-tests.scm @@ -631,11 +631,11 @@ ;; (test #t (real? #e1e10)) (test #t (real? +inf.0)) ;; (test #f (rational? -inf.0)) -(test #t (rational? 6/10)) -(test #t (rational? 6/3)) +;; (test #t (rational? 6/10)) +;; (test #t (rational? 6/3)) ;; (test #t (integer? 3+0i)) (test #t (integer? 3.0)) -(test #t (integer? 8/4)) +;; (test #t (integer? 8/4)) (test #f (exact? 3.0)) ;; (test #t (exact? #e3.0)) @@ -643,7 +643,7 @@ (test #t (exact-integer? 32)) (test #f (exact-integer? 32.0)) -(test #f (exact-integer? 32/5)) +;; (test #f (exact-integer? 32/5)) (test #t (finite? 3)) (test #f (finite? +inf.0)) @@ -757,8 +757,8 @@ (test -1 (- 3 4)) (test -6 (- 3 4 5)) (test -3 (- 3)) -(test 3/20 (/ 3 4 5)) -(test 1/3 (/ 3)) +;; (test 3/20 (/ 3 4 5)) +;; (test 1/3 (/ 3)) (test 7 (abs -7)) (test 7 (abs 7)) @@ -811,7 +811,7 @@ (test 3.0 (truncate 3.5)) (test 4.0 (round 3.5)) -(test 4 (exact (round 7/2))) +;; (test 4 (exact (round 7/2))) (test 7 (round 7)) ;; (test 1/3 (rationalize (exact .3) 1/10)) @@ -2126,9 +2126,9 @@ ;; ;; Exact ratios ;; (test-numeric-syntax "1/2" (/ 1 2)) ;; (test-numeric-syntax "#e1/2" (/ 1 2) "1/2") -(test-numeric-syntax "10/2" 5 "5") +;; (test-numeric-syntax "10/2" 5 "5") ;; (test-numeric-syntax "-1/2" (- (/ 1 2))) -(test-numeric-syntax "0/10" 0 "0") +;; (test-numeric-syntax "0/10" 0 "0") ;; (test-numeric-syntax "#e0/10" 0 "0") ;; (test-numeric-syntax "#i3/2" (/ 3.0 2.0) "1.5") ;; ;; Exact complex From 977345392f2797d48eb9c0c55d752bfba573a998 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 8 Aug 2014 15:19:10 +0900 Subject: [PATCH 4/5] no need char conversion --- src/read.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/read.c b/src/read.c index d1546104..b83d3281 100644 --- a/src/read.c +++ b/src/read.c @@ -66,7 +66,7 @@ expect(struct pic_port *port, const char *str) static bool isdelim(int c) { - return c == EOF || strchr("();,|\" \t\n\r", (char)c) != NULL; /* ignores "#", "'" */ + return c == EOF || strchr("();,|\" \t\n\r", c) != NULL; /* ignores "#", "'" */ } static bool @@ -131,7 +131,7 @@ read_datum_comment(pic_state *pic, struct pic_port *port, int c) static pic_value read_directive(pic_state *pic, struct pic_port *port, int c) { - switch ((char)peek(port)) { + switch (peek(port)) { case 'n': if (expect(port, "no-fold-case")) { pic->rfcase = false; @@ -182,7 +182,7 @@ read_comma(pic_state *pic, struct pic_port *port, int c) { c = next(port); - if ((char)c == '@') { + if (c == '@') { return pic_list2(pic, pic_sym_value(pic->sUNQUOTE_SPLICING), read(pic, port, next(port))); } else { return pic_list2(pic, pic_sym_value(pic->sUNQUOTE), read(pic, port, c)); @@ -208,7 +208,7 @@ read_symbol(pic_state *pic, struct pic_port *port, int c) } len += 1; buf = pic_realloc(pic, buf, len + 1); - buf[len - 1] = (char)c; + buf[len - 1] = c; } while (! isdelim(peek(port))); sym = pic_intern(pic, buf, len); @@ -226,9 +226,9 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c, char buf[]) read_error(pic, "expected one or more digits"); } - buf[i++] = (char)c; + buf[i++] = c; while (isdigit(c = peek(port))) { - buf[i++] = (char)next(port); + buf[i++] = next(port); } buf[i] = '\0'; @@ -244,10 +244,10 @@ read_number(pic_state *pic, struct pic_port *port, int c) i = read_uinteger(pic, port, c, buf); - switch ((char)peek(port)) { + switch (peek(port)) { case '.': do { - buf[i++] = (char)next(port); + buf[i++] = next(port); } while (isdigit(peek(port))); buf[i] = '\0'; return pic_float_value(atof(buf)); @@ -314,7 +314,7 @@ read_boolean(pic_state *pic, struct pic_port *port, int c) UNUSED(port); if (! isdelim(peek(port))) { - if ((char)c == 't') { + if (c == 't') { if (! expect(port, "rue")) { goto fail; } @@ -325,7 +325,7 @@ read_boolean(pic_state *pic, struct pic_port *port, int c) } } - if ((char)c == 't') { + if (c == 't') { return pic_true_value(); } else { return pic_false_value(); @@ -341,7 +341,7 @@ read_char(pic_state *pic, struct pic_port *port, int c) c = next(port); if (! isdelim(peek(port))) { - switch ((char)c) { + switch (c) { default: read_error(pic, "unexpected character after char literal"); case 'a': c = '\a'; if (! expect(port, "lerm")) goto fail; break; case 'b': c = '\b'; if (! expect(port, "ackspace")) goto fail; break; @@ -393,7 +393,7 @@ read_string(pic_state *pic, struct pic_port *port, int c) case 'r': c = '\r'; break; } } - buf[cnt++] = (char)c; + buf[cnt++] = c; if (cnt >= size) { buf = pic_realloc(pic, buf, size *= 2); } @@ -420,7 +420,7 @@ read_pipe(pic_state *pic, struct pic_port *port, char c) cnt = 0; while ((c = next(port)) != '|') { if (c == '\\') { - switch ((char)(c = next(port))) { + switch ((c = next(port))) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 't': c = '\t'; break; @@ -432,11 +432,11 @@ read_pipe(pic_state *pic, struct pic_port *port, char c) if (i >= sizeof HEX_BUF) read_error(pic, "expected ';'"); } - c = (char)strtol(HEX_BUF, NULL, 16); + c = strtol(HEX_BUF, NULL, 16); break; } } - buf[cnt++] = (char)c; + buf[cnt++] = c; if (cnt >= size) { buf = pic_realloc(pic, buf, size *= 2); } @@ -548,7 +548,7 @@ read_label_set(pic_state *pic, struct pic_port *port, int i) pic_value val; int c; - switch ((char)(c = skip(port, ' '))) { + switch ((c = skip(port, ' '))) { case '(': case '[': { pic_value tmp; @@ -638,7 +638,7 @@ read_dispatch(pic_state *pic, struct pic_port *port, int c) { c = next(port); - switch ((char)c) { + switch (c) { case '!': return read_directive(pic, port, c); case '|': @@ -672,7 +672,7 @@ read_nullable(pic_state *pic, struct pic_port *port, int c) read_error(pic, "unexpected EOF"); } - switch ((char)c) { + switch (c) { case ')': read_error(pic, "unmatched parenthesis"); case ';': From bd98dda5d0b7c3aca5f3a4ef7a842dff7b9a8a12 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Fri, 8 Aug 2014 15:38:29 +0900 Subject: [PATCH 5/5] support exponential literal --- src/read.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/read.c b/src/read.c index b83d3281..a7a199ca 100644 --- a/src/read.c +++ b/src/read.c @@ -236,6 +236,30 @@ read_uinteger(pic_state *pic, struct pic_port *port, int c, char buf[]) return i; } +static size_t +read_suffix(pic_state *pic, struct pic_port *port, char buf[]) +{ + size_t i = 0; + char c; + + c = peek(port); + + if (c != 'e' && c != 'E') { + return i; + } + + buf[i++] = next(port); + + switch ((c = next(port))) { + case '-': + case '+': + buf[i++] = c; + c = next(port); + default: + return i + read_uinteger(pic, port, c, buf + i); + } +} + static pic_value read_number(pic_state *pic, struct pic_port *port, int c) { @@ -246,14 +270,14 @@ read_number(pic_state *pic, struct pic_port *port, int c) switch (peek(port)) { case '.': - do { - buf[i++] = next(port); - } while (isdigit(peek(port))); - buf[i] = '\0'; + buf[i++] = next(port); + i += read_uinteger(pic, port, next(port), buf + i); + read_suffix(pic, port, buf + i); return pic_float_value(atof(buf)); default: - return pic_int_value(atoi(buf)); + read_suffix(pic, port, buf + i); + return pic_int_value((int)atof(buf)); } }