2014-08-03 02:47:25 -04:00
|
|
|
(define-library (scheme base)
|
2014-08-05 12:16:37 -04:00
|
|
|
(import (picrin base)
|
2014-09-08 07:20:08 -04:00
|
|
|
(picrin macro)
|
|
|
|
(picrin record)
|
|
|
|
(picrin syntax-rules))
|
2014-04-03 02:34:55 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export else => _ ...)
|
|
|
|
|
|
|
|
;; 4.1.2. Literal expressions
|
|
|
|
|
|
|
|
(export quote)
|
|
|
|
|
|
|
|
;; 4.1.4. Procedures
|
|
|
|
|
|
|
|
(export lambda)
|
|
|
|
|
|
|
|
;; 4.1.5. Conditionals
|
|
|
|
|
|
|
|
(export if)
|
|
|
|
|
|
|
|
;; 4.1.6. Assignments
|
|
|
|
|
|
|
|
(export set!)
|
|
|
|
|
|
|
|
;; 4.1.7. Inclusion
|
|
|
|
|
|
|
|
(export include)
|
|
|
|
|
|
|
|
;; 4.2.1. Conditionals
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export cond
|
|
|
|
case
|
|
|
|
and
|
|
|
|
or
|
|
|
|
when
|
|
|
|
unless)
|
|
|
|
|
|
|
|
;; 4.2.2. Binding constructs
|
|
|
|
|
|
|
|
(export let
|
|
|
|
let*
|
|
|
|
letrec
|
|
|
|
letrec*
|
|
|
|
let-values
|
|
|
|
let*-values)
|
|
|
|
|
|
|
|
;; 4.2.3. Sequencing
|
|
|
|
|
|
|
|
(export begin)
|
|
|
|
|
|
|
|
;; 4.2.4. Iteration
|
|
|
|
|
|
|
|
(export do)
|
2014-08-04 13:20:05 -04:00
|
|
|
|
|
|
|
;; 4.2.6. Dynamic bindings
|
|
|
|
|
|
|
|
(define-syntax parameterize
|
|
|
|
(ir-macro-transformer
|
|
|
|
(lambda (form inject compare)
|
|
|
|
(let ((formal (car (cdr form)))
|
|
|
|
(body (cdr (cdr form))))
|
|
|
|
(let ((vars (map car formal))
|
|
|
|
(vals (map cadr formal)))
|
|
|
|
`(begin
|
|
|
|
,@(map (lambda (var val) `(parameter-push! ,var ,val)) vars vals)
|
|
|
|
(let ((result (begin ,@body)))
|
|
|
|
,@(map (lambda (var) `(parameter-pop! ,var)) vars)
|
|
|
|
result)))))))
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export make-parameter
|
|
|
|
parameterize)
|
2014-08-04 13:20:05 -04:00
|
|
|
|
|
|
|
;; 4.2.7. Exception handling
|
|
|
|
|
2014-08-03 02:47:25 -04:00
|
|
|
(define-syntax guard-aux
|
|
|
|
(syntax-rules (else =>)
|
|
|
|
((guard-aux reraise (else result1 result2 ...))
|
|
|
|
(begin result1 result2 ...))
|
|
|
|
((guard-aux reraise (test => result))
|
|
|
|
(let ((temp test))
|
|
|
|
(if temp
|
|
|
|
(result temp)
|
|
|
|
reraise)))
|
|
|
|
((guard-aux reraise (test => result)
|
|
|
|
clause1 clause2 ...)
|
|
|
|
(let ((temp test))
|
|
|
|
(if temp
|
|
|
|
(result temp)
|
|
|
|
(guard-aux reraise clause1 clause2 ...))))
|
|
|
|
((guard-aux reraise (test))
|
|
|
|
(or test reraise))
|
|
|
|
((guard-aux reraise (test) clause1 clause2 ...)
|
|
|
|
(let ((temp test))
|
|
|
|
(if temp
|
|
|
|
temp
|
|
|
|
(guard-aux reraise clause1 clause2 ...))))
|
|
|
|
((guard-aux reraise (test result1 result2 ...))
|
|
|
|
(if test
|
|
|
|
(begin result1 result2 ...)
|
|
|
|
reraise))
|
|
|
|
((guard-aux reraise
|
|
|
|
(test result1 result2 ...)
|
|
|
|
clause1 clause2 ...)
|
|
|
|
(if test
|
|
|
|
(begin result1 result2 ...)
|
|
|
|
(guard-aux reraise clause1 clause2 ...)))))
|
|
|
|
|
|
|
|
(define-syntax guard
|
|
|
|
(syntax-rules ()
|
|
|
|
((guard (var clause ...) e1 e2 ...)
|
|
|
|
((call/cc
|
|
|
|
(lambda (guard-k)
|
|
|
|
(with-exception-handler
|
|
|
|
(lambda (condition)
|
|
|
|
((call/cc
|
|
|
|
(lambda (handler-k)
|
|
|
|
(guard-k
|
|
|
|
(lambda ()
|
|
|
|
(let ((var condition))
|
|
|
|
(guard-aux
|
|
|
|
(handler-k
|
|
|
|
(lambda ()
|
|
|
|
(raise-continuable condition)))
|
|
|
|
clause ...))))))))
|
|
|
|
(lambda ()
|
|
|
|
(call-with-values
|
|
|
|
(lambda () e1 e2 ...)
|
|
|
|
(lambda args
|
|
|
|
(guard-k
|
|
|
|
(lambda ()
|
|
|
|
(apply values args)))))))))))))
|
|
|
|
|
|
|
|
(export guard)
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 4.2.8. Quasiquotation
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export quasiquote
|
|
|
|
unquote
|
|
|
|
unquote-splicing)
|
2014-08-03 18:38:27 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 4.3.1. Binding constructs for syntactic keywords
|
|
|
|
|
|
|
|
(export let-syntax
|
|
|
|
letrec-syntax)
|
|
|
|
|
|
|
|
;; 4.3.2 Pattern language
|
|
|
|
|
|
|
|
(export syntax-rules)
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 4.3.3. Signaling errors in macro transformers
|
2014-08-31 20:53:19 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export syntax-error)
|
|
|
|
|
|
|
|
;; 5.3. Variable definitions
|
|
|
|
|
|
|
|
(export define)
|
|
|
|
|
|
|
|
;; 5.3.3. Multiple-value definitions
|
|
|
|
|
|
|
|
(export define-values)
|
|
|
|
|
|
|
|
;; 5.4. Syntax definitions
|
|
|
|
|
|
|
|
(export define-syntax)
|
|
|
|
|
|
|
|
;; 5.5 Recored-type definitions
|
|
|
|
|
|
|
|
(export define-record-type)
|
|
|
|
|
|
|
|
;; 6.1. Equivalence predicates
|
2014-09-08 04:08:38 -04:00
|
|
|
|
|
|
|
(export eq?
|
|
|
|
eqv?
|
|
|
|
equal?)
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 6.2. Numbers
|
2014-09-08 04:08:38 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(define (exact-integer? x)
|
|
|
|
(and (exact? x)
|
|
|
|
(integer? x)))
|
|
|
|
|
|
|
|
(define (zero? x)
|
|
|
|
(= x 0))
|
|
|
|
|
|
|
|
(define (positive? x)
|
|
|
|
(> x 0))
|
|
|
|
|
|
|
|
(define (negative? x)
|
|
|
|
(< x 0))
|
|
|
|
|
|
|
|
(define (min . args)
|
|
|
|
(let loop ((args args) (min +inf.0))
|
|
|
|
(if (null? args)
|
|
|
|
min
|
|
|
|
(loop (cdr args) (if (< (car args) min)
|
|
|
|
(car args)
|
|
|
|
min)))))
|
|
|
|
|
|
|
|
(define (max . args)
|
|
|
|
(let loop ((args args) (max -inf.0))
|
|
|
|
(if (null? args)
|
|
|
|
max
|
|
|
|
(loop (cdr args) (if (> (car args) max)
|
|
|
|
(car args)
|
|
|
|
max)))))
|
|
|
|
|
|
|
|
(define (square x)
|
|
|
|
(* x x))
|
2014-09-08 04:08:38 -04:00
|
|
|
|
|
|
|
(export number?
|
|
|
|
complex?
|
|
|
|
real?
|
|
|
|
rational?
|
|
|
|
integer?
|
|
|
|
exact?
|
|
|
|
inexact?
|
|
|
|
exact-integer?
|
|
|
|
=
|
|
|
|
<
|
|
|
|
>
|
|
|
|
<=
|
|
|
|
>=
|
|
|
|
zero?
|
|
|
|
positive?
|
|
|
|
negative?
|
2014-09-08 07:20:08 -04:00
|
|
|
;; odd?
|
|
|
|
;; even?
|
2014-09-08 04:08:38 -04:00
|
|
|
min
|
|
|
|
max
|
|
|
|
+
|
|
|
|
-
|
|
|
|
*
|
|
|
|
/
|
|
|
|
abs
|
2014-09-08 07:20:08 -04:00
|
|
|
;; floor-quotient
|
|
|
|
;; floor-remainder
|
2014-09-08 04:08:38 -04:00
|
|
|
floor/
|
2014-09-08 07:20:08 -04:00
|
|
|
;; truncate-quotient
|
|
|
|
;; truncate-remainder
|
2014-09-08 04:08:38 -04:00
|
|
|
truncate/
|
2014-09-08 07:20:08 -04:00
|
|
|
;; gcd
|
|
|
|
;; lcm
|
2014-09-08 04:08:38 -04:00
|
|
|
floor
|
|
|
|
ceiling
|
|
|
|
truncate
|
|
|
|
round
|
2014-09-08 07:20:08 -04:00
|
|
|
;; exact-integer-sqrt
|
2014-09-08 04:08:38 -04:00
|
|
|
square
|
|
|
|
expt
|
|
|
|
number->string
|
2014-09-08 07:20:08 -04:00
|
|
|
string->number)
|
2014-09-08 04:08:38 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 6.3. Booleans
|
2014-09-08 04:08:38 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export boolean?
|
|
|
|
boolean=?
|
|
|
|
not)
|
2014-09-08 04:08:38 -04:00
|
|
|
|
2014-08-05 13:07:02 -04:00
|
|
|
;; 6.4 Pairs and lists
|
|
|
|
|
|
|
|
(export pair?
|
|
|
|
cons
|
|
|
|
car
|
|
|
|
cdr
|
|
|
|
set-car!
|
|
|
|
set-cdr!
|
|
|
|
null?
|
|
|
|
caar
|
|
|
|
cadr
|
|
|
|
cdar
|
|
|
|
cddr
|
|
|
|
list?
|
|
|
|
make-list
|
|
|
|
list
|
|
|
|
length
|
|
|
|
append
|
|
|
|
reverse
|
|
|
|
list-tail
|
|
|
|
list-ref
|
|
|
|
list-set!
|
|
|
|
list-copy
|
|
|
|
memq
|
|
|
|
memv
|
|
|
|
member
|
|
|
|
assq
|
|
|
|
assv
|
|
|
|
assoc)
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 6.5. Symbols
|
2014-08-05 13:14:43 -04:00
|
|
|
|
|
|
|
(export symbol?
|
|
|
|
symbol=?
|
|
|
|
symbol->string
|
|
|
|
string->symbol)
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 6.6. Characters
|
2014-08-03 02:47:25 -04:00
|
|
|
|
|
|
|
(define-macro (define-char-transitive-predicate name op)
|
|
|
|
`(define (,name . cs)
|
|
|
|
(apply ,op (map char->integer cs))))
|
|
|
|
|
|
|
|
(define-char-transitive-predicate char=? =)
|
|
|
|
(define-char-transitive-predicate char<? <)
|
|
|
|
(define-char-transitive-predicate char>? >)
|
|
|
|
(define-char-transitive-predicate char<=? <=)
|
|
|
|
(define-char-transitive-predicate char>=? >=)
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export char?
|
|
|
|
char->integer
|
|
|
|
integer->char
|
|
|
|
char=?
|
2014-08-03 02:47:25 -04:00
|
|
|
char<?
|
|
|
|
char>?
|
|
|
|
char<=?
|
|
|
|
char>=?)
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 6.7. Strings
|
|
|
|
|
|
|
|
;; (define (string->list string . opts)
|
|
|
|
;; (let ((start (if (pair? opts) (car opts) 0))
|
|
|
|
;; (end (if (>= (length opts) 2)
|
|
|
|
;; (cadr opts)
|
|
|
|
;; (string-length string))))
|
|
|
|
;; (do ((i start (+ i 1))
|
|
|
|
;; (res '()))
|
|
|
|
;; ((= i end)
|
|
|
|
;; (reverse res))
|
|
|
|
;; (set! res (cons (string-ref string i) res)))))
|
|
|
|
|
|
|
|
;; (define (list->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
|
|
|
|
;; (rename string-copy substring))
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export string?
|
|
|
|
string-length
|
|
|
|
string-ref
|
|
|
|
string-copy
|
|
|
|
string-append
|
|
|
|
string=?
|
|
|
|
string<?
|
|
|
|
string>?
|
|
|
|
string<=?
|
|
|
|
string>=?)
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 6.8. Vectors
|
2014-08-03 02:47:25 -04:00
|
|
|
|
|
|
|
(define (vector . objs)
|
|
|
|
(list->vector objs))
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (define (vector->string . args)
|
|
|
|
;; (list->string (apply vector->list args)))
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (define (string->vector . args)
|
|
|
|
;; (list->vector (apply string->list args)))
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (export vector vector->string string->vector)
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export vector?
|
|
|
|
make-vector
|
|
|
|
vector-length
|
|
|
|
vector-ref
|
|
|
|
vector-set!
|
|
|
|
vector-copy!
|
|
|
|
vector-copy
|
|
|
|
vector-append
|
|
|
|
vector-fill!
|
|
|
|
list->vector
|
|
|
|
vector->list)
|
|
|
|
|
|
|
|
;; 6.9. bytevector
|
2014-08-03 02:47:25 -04:00
|
|
|
|
|
|
|
(define (bytevector->list v start end)
|
|
|
|
(do ((i start (+ i 1))
|
|
|
|
(res '()))
|
|
|
|
((= i end)
|
|
|
|
(reverse res))
|
|
|
|
(set! res (cons (bytevector-u8-ref v i) res))))
|
|
|
|
|
|
|
|
(define (list->bytevector list)
|
|
|
|
(let ((len (length list)))
|
|
|
|
(let ((v (make-bytevector len)))
|
|
|
|
(do ((i 0 (+ i 1))
|
|
|
|
(l list (cdr l)))
|
|
|
|
((= i len)
|
|
|
|
v)
|
|
|
|
(bytevector-u8-set! v i (car l))))))
|
|
|
|
|
|
|
|
(define (bytevector . objs)
|
|
|
|
(list->bytevector objs))
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (define (utf8->string v . opts)
|
|
|
|
;; (let ((start (if (pair? opts) (car opts) 0))
|
|
|
|
;; (end (if (>= (length opts) 2)
|
|
|
|
;; (cadr opts)
|
|
|
|
;; (bytevector-length v))))
|
|
|
|
;; (list->string (map integer->char (bytevector->list v start end)))))
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (define (string->utf8 s . opts)
|
|
|
|
;; (let ((start (if (pair? opts) (car opts) 0))
|
|
|
|
;; (end (if (>= (length opts) 2)
|
|
|
|
;; (cadr opts)
|
|
|
|
;; (string-length s))))
|
|
|
|
;; (list->bytevector (map char->integer (string->list s start end)))))
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (export bytevector
|
|
|
|
;; bytevector->list
|
|
|
|
;; list->bytevector
|
|
|
|
;; utf8->string
|
|
|
|
;; string->utf8)
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export bytevector?
|
|
|
|
make-bytevector
|
|
|
|
bytevector-length
|
|
|
|
bytevector-u8-ref
|
|
|
|
bytevector-u8-set!
|
|
|
|
bytevector-copy!
|
|
|
|
bytevector-append)
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; 6.10. Control features
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (define (string-map f . strings)
|
|
|
|
;; (list->string (apply map f (map string->list strings))))
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (define (string-for-each f . strings)
|
|
|
|
;; (apply for-each f (map string->list strings)))
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (define (vector-map f . vectors)
|
|
|
|
;; (list->vector (apply map f (map vector->list vectors))))
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
;; (define (vector-for-each f . vectors)
|
|
|
|
;; (apply for-each f (map vector->list vectors)))
|
|
|
|
|
|
|
|
;; (export string-map string-for-each
|
|
|
|
;; vector-map vector-for-each)
|
|
|
|
|
|
|
|
(export procedure?
|
|
|
|
apply
|
|
|
|
map
|
|
|
|
for-each
|
|
|
|
call-with-current-continuation
|
|
|
|
call/cc
|
|
|
|
dynamic-wind
|
|
|
|
values
|
|
|
|
call-with-values)
|
|
|
|
|
|
|
|
;; 6.11. Exceptions
|
|
|
|
|
|
|
|
(export with-exception-handler
|
|
|
|
raise
|
|
|
|
raise-continuable
|
|
|
|
error
|
|
|
|
error-object?
|
|
|
|
error-object-message
|
|
|
|
error-object-irritants
|
|
|
|
read-error?
|
|
|
|
file-error?)
|
2014-08-03 02:47:25 -04:00
|
|
|
|
|
|
|
;; 6.13. Input and output
|
|
|
|
|
|
|
|
(define (call-with-port port proc)
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda () #f)
|
|
|
|
(lambda () (proc port))
|
|
|
|
(lambda () (close-port port))))
|
|
|
|
|
2014-09-08 07:20:08 -04:00
|
|
|
(export current-input-port
|
|
|
|
current-output-port
|
|
|
|
current-error-port
|
|
|
|
|
|
|
|
call-with-port
|
|
|
|
|
|
|
|
port?
|
|
|
|
input-port?
|
|
|
|
output-port?
|
|
|
|
textual-port?
|
|
|
|
binary-port?
|
|
|
|
|
|
|
|
close-port
|
|
|
|
(rename close-port close-input-port)
|
|
|
|
(rename close-port close-output-port)
|
|
|
|
|
|
|
|
open-input-string
|
|
|
|
open-output-string
|
|
|
|
get-output-string
|
|
|
|
open-input-bytevector
|
|
|
|
open-output-bytevector
|
|
|
|
get-output-bytevector
|
|
|
|
|
|
|
|
eof-object?
|
|
|
|
eof-object
|
|
|
|
|
|
|
|
read-char
|
|
|
|
peek-char
|
|
|
|
char-ready?
|
|
|
|
read-line
|
|
|
|
read-string
|
|
|
|
|
|
|
|
read-u8
|
|
|
|
peek-u8
|
|
|
|
u8-ready?
|
|
|
|
read-bytevector
|
|
|
|
read-bytevector!
|
|
|
|
|
|
|
|
newline
|
|
|
|
write-char
|
|
|
|
write-string
|
|
|
|
write-u8
|
|
|
|
write-bytevector
|
|
|
|
flush-output-port))
|