Merge branch 'master' into editline
This commit is contained in:
commit
cc35671a0a
|
@ -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)
|
|
@ -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<N; mti++) {
|
||||
mt[mti] =
|
||||
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 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<N-M;kk++) {
|
||||
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
||||
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
||||
}
|
||||
for (;kk<N-1;kk++) {
|
||||
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
||||
mt[kk] = mt[kk+(M-N)] ^ (y >> 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 <stdio.h>
|
||||
|
||||
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
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
(import (srfi 27)
|
||||
(scheme base)
|
||||
(picrin test))
|
||||
|
||||
(test #t (procedure? random-real))
|
|
@ -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)
|
||||
|
||||
|
|
79
src/read.c
79
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';
|
||||
|
@ -236,35 +236,48 @@ 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)
|
||||
{
|
||||
char buf[256];
|
||||
size_t i;
|
||||
long n;
|
||||
|
||||
i = read_uinteger(pic, port, c, buf);
|
||||
|
||||
switch ((char)peek(port)) {
|
||||
switch (peek(port)) {
|
||||
case '.':
|
||||
do {
|
||||
buf[i++] = (char)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));
|
||||
|
||||
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));
|
||||
read_suffix(pic, port, buf + i);
|
||||
return pic_int_value((int)atof(buf));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -325,7 +338,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;
|
||||
}
|
||||
|
@ -336,7 +349,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();
|
||||
|
@ -352,7 +365,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;
|
||||
|
@ -404,7 +417,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);
|
||||
}
|
||||
|
@ -431,7 +444,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;
|
||||
|
@ -443,11 +456,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);
|
||||
}
|
||||
|
@ -559,7 +572,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;
|
||||
|
@ -649,7 +662,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 '|':
|
||||
|
@ -683,7 +696,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 ';':
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue