2014-08-03 02:47:25 -04:00
|
|
|
(define-library (scheme base)
|
2014-08-05 12:16:37 -04:00
|
|
|
(import (picrin base)
|
2014-08-05 13:07:02 -04:00
|
|
|
(picrin list)
|
2014-08-05 13:14:43 -04:00
|
|
|
(picrin symbol)
|
2014-08-05 12:14:43 -04:00
|
|
|
(picrin macro))
|
|
|
|
|
|
|
|
(export define set! lambda quote
|
|
|
|
if begin define-syntax)
|
2014-08-03 02:47:25 -04:00
|
|
|
|
|
|
|
;; core syntax
|
2013-12-10 12:14:08 -05:00
|
|
|
|
2014-07-19 00:22:24 -04:00
|
|
|
(define-syntax syntax-error
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr rename compare)
|
|
|
|
(apply error (cdr expr)))))
|
|
|
|
|
2014-07-16 22:32:52 -04:00
|
|
|
(define-syntax define-auxiliary-syntax
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr r c)
|
|
|
|
(list (r 'define-syntax) (cadr expr)
|
|
|
|
(list (r 'lambda) '_
|
|
|
|
(list (r 'error) "invalid use of auxiliary syntax"))))))
|
|
|
|
|
|
|
|
(define-auxiliary-syntax else)
|
|
|
|
(define-auxiliary-syntax =>)
|
|
|
|
(define-auxiliary-syntax _)
|
|
|
|
(define-auxiliary-syntax ...)
|
|
|
|
(define-auxiliary-syntax unquote)
|
|
|
|
(define-auxiliary-syntax unquote-splicing)
|
|
|
|
|
2013-12-10 06:09:27 -05:00
|
|
|
(define-syntax let
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr r compare)
|
2014-01-10 22:56:51 -05:00
|
|
|
(if (symbol? (cadr expr))
|
2013-12-10 06:09:27 -05:00
|
|
|
(begin
|
2014-07-19 01:25:22 -04:00
|
|
|
(define name (car (cdr expr)))
|
|
|
|
(define bindings (car (cdr (cdr expr))))
|
|
|
|
(define body (cdr (cdr (cdr expr))))
|
2013-12-10 06:09:27 -05:00
|
|
|
(list (r 'let) '()
|
|
|
|
(list (r 'define) name
|
|
|
|
(cons (r 'lambda) (cons (map car bindings) body)))
|
|
|
|
(cons name (map cadr bindings))))
|
|
|
|
(begin
|
2014-02-07 13:18:13 -05:00
|
|
|
(set! bindings (cadr expr))
|
|
|
|
(set! body (cddr expr))
|
2013-12-10 06:09:27 -05:00
|
|
|
(cons (cons (r 'lambda) (cons (map car bindings) body))
|
|
|
|
(map cadr bindings)))))))
|
|
|
|
|
|
|
|
(define-syntax cond
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr r compare)
|
|
|
|
(let ((clauses (cdr expr)))
|
|
|
|
(if (null? clauses)
|
|
|
|
#f
|
2014-07-19 01:25:22 -04:00
|
|
|
(begin
|
|
|
|
(define clause (car clauses))
|
|
|
|
(if (compare (r 'else) (car clause))
|
|
|
|
(cons (r 'begin) (cdr clause))
|
|
|
|
(if (if (>= (length clause) 2)
|
|
|
|
(compare (r '=>) (list-ref clause 1))
|
|
|
|
#f)
|
|
|
|
(list (r 'let) (list (list (r 'x) (car clause)))
|
|
|
|
(list (r 'if) (r 'x)
|
|
|
|
(list (list-ref clause 2) (r 'x))
|
|
|
|
(cons (r 'cond) (cdr clauses))))
|
|
|
|
(list (r 'if) (car clause)
|
|
|
|
(cons (r 'begin) (cdr clause))
|
|
|
|
(cons (r 'cond) (cdr clauses)))))))))))
|
2013-12-10 06:09:27 -05:00
|
|
|
|
|
|
|
(define-syntax and
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr r compare)
|
|
|
|
(let ((exprs (cdr expr)))
|
2014-02-11 07:35:56 -05:00
|
|
|
(cond
|
|
|
|
((null? exprs)
|
|
|
|
#t)
|
2014-07-19 00:22:24 -04:00
|
|
|
((= (length exprs) 1)
|
2014-02-11 07:35:56 -05:00
|
|
|
(car exprs))
|
|
|
|
(else
|
|
|
|
(list (r 'let) (list (list (r 'it) (car exprs)))
|
|
|
|
(list (r 'if) (r 'it)
|
|
|
|
(cons (r 'and) (cdr exprs))
|
|
|
|
(r 'it)))))))))
|
2013-12-10 06:09:27 -05:00
|
|
|
|
|
|
|
(define-syntax or
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr r compare)
|
|
|
|
(let ((exprs (cdr expr)))
|
2014-02-11 07:35:56 -05:00
|
|
|
(cond
|
|
|
|
((null? exprs)
|
|
|
|
#t)
|
2014-07-19 00:22:24 -04:00
|
|
|
((= (length exprs) 1)
|
2014-02-11 07:35:56 -05:00
|
|
|
(car exprs))
|
|
|
|
(else
|
|
|
|
(list (r 'let) (list (list (r 'it) (car exprs)))
|
|
|
|
(list (r 'if) (r 'it)
|
|
|
|
(r 'it)
|
|
|
|
(cons (r 'or) (cdr exprs))))))))))
|
2013-12-10 02:00:47 -05:00
|
|
|
|
2013-12-10 06:09:27 -05:00
|
|
|
(define-syntax quasiquote
|
2014-02-12 08:31:17 -05:00
|
|
|
(ir-macro-transformer
|
|
|
|
(lambda (form inject compare)
|
|
|
|
|
2014-07-19 00:22:24 -04:00
|
|
|
(define (quasiquote? form)
|
|
|
|
(and (pair? form) (compare (car form) 'quasiquote)))
|
|
|
|
|
|
|
|
(define (unquote? form)
|
|
|
|
(and (pair? form) (compare (car form) 'unquote)))
|
|
|
|
|
|
|
|
(define (unquote-splicing? form)
|
|
|
|
(and (pair? form) (pair? (car form))
|
|
|
|
(compare (car (car form)) 'unquote-splicing)))
|
|
|
|
|
2014-02-12 08:31:17 -05:00
|
|
|
(define (qq depth expr)
|
2013-12-10 06:09:27 -05:00
|
|
|
(cond
|
2014-02-12 08:31:17 -05:00
|
|
|
;; unquote
|
2014-07-19 00:22:24 -04:00
|
|
|
((unquote? expr)
|
2014-02-12 08:31:17 -05:00
|
|
|
(if (= depth 1)
|
|
|
|
(car (cdr expr))
|
|
|
|
(list 'list
|
|
|
|
(list 'quote (inject 'unquote))
|
|
|
|
(qq (- depth 1) (car (cdr expr))))))
|
|
|
|
;; unquote-splicing
|
2014-07-19 00:22:24 -04:00
|
|
|
((unquote-splicing? expr)
|
2014-02-12 08:31:17 -05:00
|
|
|
(if (= depth 1)
|
|
|
|
(list 'append
|
|
|
|
(car (cdr (car expr)))
|
|
|
|
(qq depth (cdr expr)))
|
|
|
|
(list 'cons
|
|
|
|
(list 'list
|
|
|
|
(list 'quote (inject 'unquote-splicing))
|
|
|
|
(qq (- depth 1) (car (cdr (car expr)))))
|
|
|
|
(qq depth (cdr expr)))))
|
|
|
|
;; quasiquote
|
2014-07-19 00:22:24 -04:00
|
|
|
((quasiquote? expr)
|
2014-02-12 08:31:17 -05:00
|
|
|
(list 'list
|
|
|
|
(list 'quote (inject 'quasiquote))
|
|
|
|
(qq (+ depth 1) (car (cdr expr)))))
|
|
|
|
;; list
|
|
|
|
((pair? expr)
|
|
|
|
(list 'cons
|
|
|
|
(qq depth (car expr))
|
|
|
|
(qq depth (cdr expr))))
|
2014-06-28 06:43:49 -04:00
|
|
|
;; vector
|
|
|
|
((vector? expr)
|
|
|
|
(list 'list->vector (qq depth (vector->list expr))))
|
2014-02-12 08:31:17 -05:00
|
|
|
;; simple datum
|
|
|
|
(else
|
|
|
|
(list 'quote expr))))
|
|
|
|
|
|
|
|
(let ((x (cadr form)))
|
|
|
|
(qq 1 x)))))
|
2013-12-10 06:09:27 -05:00
|
|
|
|
|
|
|
(define-syntax let*
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form r compare)
|
|
|
|
(let ((bindings (cadr form))
|
|
|
|
(body (cddr form)))
|
|
|
|
(if (null? bindings)
|
|
|
|
`(,(r 'let) () ,@body)
|
|
|
|
`(,(r 'let) ((,(caar bindings)
|
|
|
|
,@(cdar bindings)))
|
|
|
|
(,(r 'let*) (,@(cdr bindings))
|
|
|
|
,@body)))))))
|
|
|
|
|
2013-12-10 11:37:33 -05:00
|
|
|
(define-syntax letrec*
|
2013-12-10 06:09:27 -05:00
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form r compare)
|
|
|
|
(let ((bindings (cadr form))
|
|
|
|
(body (cddr form)))
|
|
|
|
(let ((vars (map (lambda (v) `(,v #f)) (map car bindings)))
|
|
|
|
(initials (map (lambda (v) `(,(r 'set!) ,@v)) bindings)))
|
|
|
|
`(,(r 'let) (,@vars)
|
|
|
|
,@initials
|
|
|
|
,@body))))))
|
|
|
|
|
2013-12-10 11:37:33 -05:00
|
|
|
(define-syntax letrec
|
2013-12-10 06:09:27 -05:00
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form rename compare)
|
2013-12-10 11:37:33 -05:00
|
|
|
`(,(rename 'letrec*) ,@(cdr form)))))
|
2013-12-10 06:09:27 -05:00
|
|
|
|
|
|
|
(define-syntax do
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form r compare)
|
2014-07-19 01:25:22 -04:00
|
|
|
(let ((bindings (car (cdr form)))
|
|
|
|
(finish (car (cdr (cdr form))))
|
|
|
|
(body (cdr (cdr (cdr form)))))
|
2013-12-10 06:09:27 -05:00
|
|
|
`(,(r 'let) ,(r 'loop) ,(map (lambda (x)
|
|
|
|
(list (car x) (cadr x)))
|
|
|
|
bindings)
|
|
|
|
(,(r 'if) ,(car finish)
|
2014-01-08 06:18:44 -05:00
|
|
|
(,(r 'begin) ,@(cdr finish))
|
2013-12-10 06:09:27 -05:00
|
|
|
(,(r 'begin) ,@body
|
|
|
|
(,(r 'loop) ,@(map (lambda (x)
|
|
|
|
(if (null? (cddr x))
|
|
|
|
(car x)
|
|
|
|
(car (cddr x))))
|
2014-01-08 06:18:44 -05:00
|
|
|
bindings)))))))))
|
2013-12-10 06:09:27 -05:00
|
|
|
|
|
|
|
(define-syntax when
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr rename compare)
|
|
|
|
(let ((test (cadr expr))
|
|
|
|
(body (cddr expr)))
|
|
|
|
`(,(rename 'if) ,test
|
|
|
|
(,(rename 'begin) ,@body)
|
|
|
|
#f)))))
|
|
|
|
|
|
|
|
(define-syntax unless
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr rename compare)
|
|
|
|
(let ((test (cadr expr))
|
|
|
|
(body (cddr expr)))
|
|
|
|
`(,(rename 'if) ,test
|
|
|
|
#f
|
|
|
|
(,(rename 'begin) ,@body))))))
|
2013-12-10 02:00:47 -05:00
|
|
|
|
2013-12-10 04:48:53 -05:00
|
|
|
(define-syntax case
|
2013-12-10 06:09:27 -05:00
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (expr r compare)
|
2013-12-10 04:48:53 -05:00
|
|
|
(let ((key (cadr expr))
|
|
|
|
(clauses (cddr expr)))
|
2013-12-10 06:09:27 -05:00
|
|
|
`(,(r 'let) ((,(r 'key) ,key))
|
2013-12-10 04:48:53 -05:00
|
|
|
,(let loop ((clauses clauses))
|
|
|
|
(if (null? clauses)
|
|
|
|
#f
|
2014-07-19 01:25:22 -04:00
|
|
|
(begin
|
|
|
|
(define clause (car clauses))
|
|
|
|
`(,(r 'if) ,(if (compare (r 'else) (car clause))
|
|
|
|
'#t
|
|
|
|
`(,(r 'or)
|
|
|
|
,@(map (lambda (x)
|
|
|
|
`(,(r 'eqv?) ,(r 'key) (,(r 'quote) ,x)))
|
|
|
|
(car clause))))
|
|
|
|
,(if (compare (r '=>) (list-ref clause 1))
|
|
|
|
`(,(list-ref clause 2) ,(r 'key))
|
|
|
|
`(,(r 'begin) ,@(cdr clause)))
|
|
|
|
,(loop (cdr clauses)))))))))))
|
2013-12-10 04:48:53 -05:00
|
|
|
|
2014-07-16 01:30:45 -04:00
|
|
|
(define-syntax letrec-syntax
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form r c)
|
|
|
|
(let ((formal (car (cdr form)))
|
|
|
|
(body (cdr (cdr form))))
|
|
|
|
`(let ()
|
|
|
|
,@(map (lambda (x)
|
|
|
|
`(,(r 'define-syntax) ,(car x) ,(cadr x)))
|
|
|
|
formal)
|
|
|
|
,@body)))))
|
|
|
|
|
2014-07-20 01:55:17 -04:00
|
|
|
(define-syntax let-syntax
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form r c)
|
|
|
|
`(,(r 'letrec-syntax) ,@(cdr form)))))
|
|
|
|
|
2014-07-22 01:34:39 -04:00
|
|
|
(import (scheme read) (scheme file))
|
|
|
|
|
|
|
|
(define-syntax include
|
|
|
|
(letrec ((read-file
|
|
|
|
(lambda (filename)
|
|
|
|
(let ((port (open-input-file filename)))
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda () #f)
|
|
|
|
(lambda ()
|
|
|
|
(let loop ((expr (read port)) (exprs '()))
|
|
|
|
(if (eof-object? expr)
|
|
|
|
(reverse exprs)
|
|
|
|
(loop (read port) (cons expr exprs)))))
|
|
|
|
(lambda ()
|
|
|
|
(close-port port)))))))
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form rename compare)
|
|
|
|
(let ((filenames (cdr form)))
|
|
|
|
(let ((exprs (apply append (map read-file filenames))))
|
|
|
|
`(,(rename 'begin) ,@exprs)))))))
|
|
|
|
|
2013-12-10 02:00:47 -05:00
|
|
|
(export let let* letrec letrec*
|
|
|
|
quasiquote unquote unquote-splicing
|
|
|
|
and or
|
2013-12-10 04:48:53 -05:00
|
|
|
cond case else =>
|
2013-12-10 06:21:29 -05:00
|
|
|
do when unless
|
2014-07-20 01:55:17 -04:00
|
|
|
let-syntax letrec-syntax
|
2014-07-22 01:34:39 -04:00
|
|
|
include
|
2014-08-03 02:47:25 -04:00
|
|
|
_ ... syntax-error)
|
2013-12-10 11:11:54 -05:00
|
|
|
|
|
|
|
|
2014-08-03 02:47:25 -04:00
|
|
|
;; utility functions
|
2013-12-10 11:34:57 -05:00
|
|
|
|
2014-06-29 02:07:52 -04:00
|
|
|
(define (walk proc expr)
|
|
|
|
(cond
|
|
|
|
((null? expr)
|
|
|
|
'())
|
|
|
|
((pair? expr)
|
2014-08-03 02:47:25 -04:00
|
|
|
(cons (walk proc (car expr))
|
2014-06-29 02:07:52 -04:00
|
|
|
(walk proc (cdr expr))))
|
|
|
|
((vector? expr)
|
2014-07-22 01:28:30 -04:00
|
|
|
(list->vector (map proc (vector->list expr))))
|
2014-06-29 02:07:52 -04:00
|
|
|
(else
|
|
|
|
(proc expr))))
|
|
|
|
|
|
|
|
(define (flatten expr)
|
|
|
|
(let ((list '()))
|
|
|
|
(walk
|
|
|
|
(lambda (x)
|
|
|
|
(set! list (cons x list)))
|
|
|
|
expr)
|
|
|
|
(reverse list)))
|
|
|
|
|
2014-08-03 02:47:25 -04:00
|
|
|
(define (reverse* l)
|
|
|
|
;; (reverse* '(a b c d . e)) => (e d c b a)
|
|
|
|
(let loop ((a '())
|
|
|
|
(d l))
|
|
|
|
(if (pair? d)
|
|
|
|
(loop (cons (car d) a) (cdr d))
|
|
|
|
(cons d a))))
|
|
|
|
|
|
|
|
(define (every? pred l)
|
|
|
|
(if (null? l)
|
|
|
|
#t
|
|
|
|
(and (pred (car l)) (every? pred (cdr l)))))
|
|
|
|
|
|
|
|
|
|
|
|
;; extra syntax
|
|
|
|
|
|
|
|
(define-syntax let*-values
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form r c)
|
|
|
|
(let ((formals (cadr form)))
|
|
|
|
(if (null? formals)
|
|
|
|
`(,(r 'let) () ,@(cddr form))
|
|
|
|
`(,(r 'call-with-values) (,(r 'lambda) () ,@(cdar formals))
|
|
|
|
(,(r 'lambda) (,@(caar formals))
|
|
|
|
(,(r 'let*-values) (,@(cdr formals))
|
|
|
|
,@(cddr form)))))))))
|
|
|
|
|
|
|
|
(define-syntax let-values
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form r c)
|
|
|
|
`(,(r 'let*-values) ,@(cdr form)))))
|
|
|
|
|
2014-06-29 02:07:52 -04:00
|
|
|
(define uniq
|
|
|
|
(let ((counter 0))
|
|
|
|
(lambda (x)
|
|
|
|
(let ((sym (string->symbol (string-append "var$" (number->string counter)))))
|
|
|
|
(set! counter (+ counter 1))
|
|
|
|
sym))))
|
|
|
|
|
2013-12-10 11:48:26 -05:00
|
|
|
(define-syntax define-values
|
2014-06-29 02:07:52 -04:00
|
|
|
(ir-macro-transformer
|
|
|
|
(lambda (form inject compare)
|
|
|
|
(let* ((formal (cadr form))
|
|
|
|
(formal* (walk uniq formal))
|
|
|
|
(exprs (cddr form)))
|
|
|
|
`(begin
|
2014-07-19 01:30:29 -04:00
|
|
|
,@(map
|
|
|
|
(lambda (var) `(define ,var #f))
|
|
|
|
(flatten formal))
|
2014-06-29 02:07:52 -04:00
|
|
|
(call-with-values (lambda () ,@exprs)
|
|
|
|
(lambda ,formal*
|
2014-07-19 01:30:29 -04:00
|
|
|
,@(map
|
|
|
|
(lambda (var val) `(set! ,var ,val))
|
|
|
|
(flatten formal)
|
|
|
|
(flatten formal*)))))))))
|
2013-12-10 11:48:26 -05:00
|
|
|
|
2014-02-02 00:26:58 -05:00
|
|
|
(export let-values
|
2013-12-10 11:48:26 -05:00
|
|
|
let*-values
|
2014-08-03 02:47:25 -04:00
|
|
|
define-values)
|
2014-02-18 12:01:02 -05:00
|
|
|
|
2014-04-03 02:34:55 -04:00
|
|
|
(define-syntax syntax-rules
|
|
|
|
(er-macro-transformer
|
|
|
|
(lambda (form r compare)
|
|
|
|
(define _define (r 'define))
|
|
|
|
(define _let (r 'let))
|
|
|
|
(define _if (r 'if))
|
|
|
|
(define _begin (r 'begin))
|
|
|
|
(define _lambda (r 'lambda))
|
|
|
|
(define _set! (r 'set!))
|
|
|
|
(define _not (r 'not))
|
|
|
|
(define _and (r 'and))
|
|
|
|
(define _car (r 'car))
|
|
|
|
(define _cdr (r 'cdr))
|
|
|
|
(define _cons (r 'cons))
|
|
|
|
(define _pair? (r 'pair?))
|
|
|
|
(define _null? (r 'null?))
|
|
|
|
(define _symbol? (r 'symbol?))
|
2014-08-06 08:05:20 -04:00
|
|
|
(define _vector? (r 'vector?))
|
2014-04-03 02:34:55 -04:00
|
|
|
(define _eqv? (r 'eqv?))
|
|
|
|
(define _string=? (r 'string=?))
|
|
|
|
(define _map (r 'map))
|
|
|
|
(define _vector->list (r 'vector->list))
|
|
|
|
(define _list->vector (r 'list->vector))
|
|
|
|
(define _quote (r 'quote))
|
|
|
|
(define _quasiquote (r 'quasiquote))
|
|
|
|
(define _unquote (r 'unquote))
|
|
|
|
(define _unquote-splicing (r 'unquote-splicing))
|
|
|
|
(define _syntax-error (r 'syntax-error))
|
|
|
|
(define _call/cc (r 'call/cc))
|
|
|
|
(define _er-macro-transformer (r 'er-macro-transformer))
|
|
|
|
|
2014-08-03 02:47:25 -04:00
|
|
|
(define (var->sym v)
|
|
|
|
(let loop ((cnt 0)
|
|
|
|
(v v))
|
|
|
|
(if (symbol? v)
|
|
|
|
(string->symbol
|
|
|
|
(string-append (symbol->string v) "/" (number->string cnt)))
|
|
|
|
(loop (+ 1 cnt) (car v)))))
|
|
|
|
|
|
|
|
(define push-var list)
|
|
|
|
|
2014-04-03 02:34:55 -04:00
|
|
|
(define (compile-match ellipsis literals pattern)
|
|
|
|
(letrec ((compile-match-base
|
|
|
|
(lambda (pattern)
|
2014-08-06 08:47:21 -04:00
|
|
|
(cond ((member pattern literals compare)
|
2014-04-03 02:34:55 -04:00
|
|
|
(values
|
|
|
|
`(,_if (,_and (,_symbol? expr) (cmp expr (rename ',pattern)))
|
|
|
|
#f
|
|
|
|
(exit #f))
|
|
|
|
'()))
|
2014-08-06 08:47:21 -04:00
|
|
|
((compare pattern (r '_)) (values #f '()))
|
2014-04-03 02:34:55 -04:00
|
|
|
((and ellipsis (compare pattern ellipsis))
|
|
|
|
(values `(,_syntax-error "invalid pattern") '()))
|
|
|
|
((symbol? pattern)
|
|
|
|
(values `(,_set! ,(var->sym pattern) expr) (list pattern)))
|
|
|
|
((pair? pattern)
|
|
|
|
(compile-match-list pattern))
|
|
|
|
((vector? pattern)
|
|
|
|
(compile-match-vector pattern))
|
|
|
|
((string? pattern)
|
|
|
|
(values
|
|
|
|
`(,_if (,_not (,_string=? ',pattern expr))
|
|
|
|
(exit #f))
|
|
|
|
'()))
|
|
|
|
(else
|
|
|
|
(values
|
|
|
|
`(,_if (,_not (,_eqv? ',pattern expr))
|
|
|
|
(exit #f))
|
|
|
|
'())))))
|
|
|
|
|
|
|
|
(compile-match-list
|
|
|
|
(lambda (pattern)
|
|
|
|
(let loop ((pattern pattern)
|
|
|
|
(matches '())
|
|
|
|
(vars '())
|
|
|
|
(accessor 'expr))
|
|
|
|
(cond ;; (hoge)
|
|
|
|
((not (pair? (cdr pattern)))
|
|
|
|
(let*-values (((match1 vars1) (compile-match-base (car pattern)))
|
|
|
|
((match2 vars2) (compile-match-base (cdr pattern))))
|
|
|
|
(values
|
|
|
|
`(,_begin ,@(reverse matches)
|
|
|
|
(,_if (,_pair? ,accessor)
|
|
|
|
(,_begin
|
|
|
|
(,_let ((expr (,_car ,accessor)))
|
|
|
|
,match1)
|
|
|
|
(,_let ((expr (,_cdr ,accessor)))
|
|
|
|
,match2))
|
|
|
|
(exit #f)))
|
|
|
|
(append vars (append vars1 vars2)))))
|
|
|
|
;; (hoge ... rest args)
|
|
|
|
((and ellipsis (compare (cadr pattern) ellipsis))
|
|
|
|
(let-values (((match-r vars-r) (compile-match-list-reverse pattern)))
|
|
|
|
(values
|
|
|
|
`(,_begin ,@(reverse matches)
|
|
|
|
(,_let ((expr (,_let loop ((a ())
|
|
|
|
(d ,accessor))
|
|
|
|
(,_if (,_pair? d)
|
|
|
|
(loop (,_cons (,_car d) a) (,_cdr d))
|
|
|
|
(,_cons d a)))))
|
|
|
|
,match-r))
|
|
|
|
(append vars vars-r))))
|
|
|
|
(else
|
|
|
|
(let-values (((match1 vars1) (compile-match-base (car pattern))))
|
|
|
|
(loop (cdr pattern)
|
|
|
|
(cons `(,_if (,_pair? ,accessor)
|
2014-06-26 18:28:56 -04:00
|
|
|
(,_let ((expr (,_car ,accessor)))
|
2014-04-03 02:34:55 -04:00
|
|
|
,match1)
|
|
|
|
(exit #f))
|
|
|
|
matches)
|
|
|
|
(append vars vars1)
|
|
|
|
`(,_cdr ,accessor))))))))
|
|
|
|
|
|
|
|
(compile-match-list-reverse
|
|
|
|
(lambda (pattern)
|
|
|
|
(let loop ((pattern (reverse* pattern))
|
|
|
|
(matches '())
|
|
|
|
(vars '())
|
|
|
|
(accessor 'expr))
|
|
|
|
(cond ((and ellipsis (compare (car pattern) ellipsis))
|
|
|
|
(let-values (((match1 vars1) (compile-match-ellipsis (cadr pattern))))
|
|
|
|
(values
|
|
|
|
`(,_begin ,@(reverse matches)
|
|
|
|
(,_let ((expr ,accessor))
|
|
|
|
,match1))
|
|
|
|
(append vars vars1))))
|
|
|
|
(else
|
|
|
|
(let-values (((match1 vars1) (compile-match-base (car pattern))))
|
|
|
|
(loop (cdr pattern)
|
|
|
|
(cons `(,_let ((expr (,_car ,accessor))) ,match1) matches)
|
|
|
|
(append vars vars1)
|
|
|
|
`(,_cdr ,accessor))))))))
|
|
|
|
|
|
|
|
(compile-match-ellipsis
|
|
|
|
(lambda (pattern)
|
|
|
|
(let-values (((match vars) (compile-match-base pattern)))
|
|
|
|
(values
|
|
|
|
`(,_let loop ((expr expr))
|
|
|
|
(,_if (,_not (,_null? expr))
|
|
|
|
(,_let ,(map (lambda (var) `(,(var->sym var) '())) vars)
|
|
|
|
(,_let ((expr (,_car expr)))
|
|
|
|
,match)
|
|
|
|
,@(map
|
|
|
|
(lambda (var)
|
|
|
|
`(,_set! ,(var->sym (push-var var))
|
|
|
|
(,_cons ,(var->sym var) ,(var->sym (push-var var)))))
|
|
|
|
vars)
|
|
|
|
(loop (,_cdr expr)))))
|
|
|
|
(map push-var vars)))))
|
|
|
|
|
|
|
|
(compile-match-vector
|
|
|
|
(lambda (pattern)
|
2014-08-06 07:11:59 -04:00
|
|
|
(let-values (((match vars) (compile-match-base (vector->list pattern))))
|
2014-04-03 02:34:55 -04:00
|
|
|
(values
|
2014-08-06 08:05:20 -04:00
|
|
|
`(,_if (,_vector? expr)
|
|
|
|
(,_let ((expr (,_vector->list expr)))
|
|
|
|
,match)
|
|
|
|
(exit #f))
|
2014-04-03 02:34:55 -04:00
|
|
|
vars)))))
|
|
|
|
|
|
|
|
(let-values (((match vars) (compile-match-base (cdr pattern))))
|
|
|
|
(values `(,_let ((expr (,_cdr expr)))
|
|
|
|
,match
|
|
|
|
#t)
|
|
|
|
vars))))
|
|
|
|
|
|
|
|
;;; compile expand
|
|
|
|
(define (compile-expand ellipsis reserved template)
|
|
|
|
(letrec ((compile-expand-base
|
|
|
|
(lambda (template ellipsis-valid)
|
2014-07-17 03:11:33 -04:00
|
|
|
(cond ((member template reserved eq?)
|
2014-04-03 02:34:55 -04:00
|
|
|
(values (var->sym template) (list template)))
|
|
|
|
((symbol? template)
|
|
|
|
(values `(rename ',template) '()))
|
|
|
|
((pair? template)
|
|
|
|
(compile-expand-list template ellipsis-valid))
|
|
|
|
((vector? template)
|
|
|
|
(compile-expand-vector template ellipsis-valid))
|
|
|
|
(else
|
|
|
|
(values `',template '())))))
|
|
|
|
|
|
|
|
(compile-expand-list
|
|
|
|
(lambda (template ellipsis-valid)
|
|
|
|
(let loop ((template template)
|
|
|
|
(expands '())
|
|
|
|
(vars '()))
|
|
|
|
(cond ;; (... hoge)
|
|
|
|
((and ellipsis-valid
|
|
|
|
(pair? template)
|
|
|
|
(compare (car template) ellipsis))
|
|
|
|
(if (and (pair? (cdr template)) (null? (cddr template)))
|
|
|
|
(compile-expand-base (cadr template) #f)
|
|
|
|
(values '(,_syntax-error "invalid template") '())))
|
|
|
|
;; hoge
|
|
|
|
((not (pair? template))
|
|
|
|
(let-values (((expand1 vars1)
|
|
|
|
(compile-expand-base template ellipsis-valid)))
|
|
|
|
(values
|
|
|
|
`(,_quasiquote (,@(reverse expands) . (,_unquote ,expand1)))
|
|
|
|
(append vars vars1))))
|
|
|
|
;; (a ... rest syms)
|
|
|
|
((and ellipsis-valid
|
|
|
|
(pair? (cdr template))
|
|
|
|
(compare (cadr template) ellipsis))
|
|
|
|
(let-values (((expand1 vars1)
|
|
|
|
(compile-expand-base (car template) ellipsis-valid)))
|
|
|
|
(loop (cddr template)
|
|
|
|
(cons
|
|
|
|
`(,_unquote-splicing
|
|
|
|
(,_map (,_lambda ,(map var->sym vars1) ,expand1)
|
|
|
|
,@(map (lambda (v) (var->sym (push-var v))) vars1)))
|
|
|
|
expands)
|
|
|
|
(append vars (map push-var vars1)))))
|
|
|
|
(else
|
|
|
|
(let-values (((expand1 vars1)
|
|
|
|
(compile-expand-base (car template) ellipsis-valid)))
|
|
|
|
(loop (cdr template)
|
|
|
|
(cons
|
|
|
|
`(,_unquote ,expand1)
|
|
|
|
expands)
|
|
|
|
(append vars vars1))))))))
|
|
|
|
|
|
|
|
(compile-expand-vector
|
|
|
|
(lambda (template ellipsis-valid)
|
|
|
|
(let-values (((expand1 vars1)
|
2014-08-06 07:11:59 -04:00
|
|
|
(compile-expand-base (vector->list template) ellipsis-valid)))
|
|
|
|
(values
|
|
|
|
`(,_list->vector ,expand1)
|
|
|
|
vars1)))))
|
2014-04-03 02:34:55 -04:00
|
|
|
|
|
|
|
(compile-expand-base template ellipsis)))
|
|
|
|
|
|
|
|
(define (check-vars vars-pattern vars-template)
|
|
|
|
;;fixme
|
|
|
|
#t)
|
|
|
|
|
|
|
|
(define (compile-rule ellipsis literals rule)
|
|
|
|
(let ((pattern (car rule))
|
|
|
|
(template (cadr rule)))
|
|
|
|
(let*-values (((match vars-match)
|
|
|
|
(compile-match ellipsis literals pattern))
|
|
|
|
((expand vars-expand)
|
|
|
|
(compile-expand ellipsis (flatten vars-match) template)))
|
|
|
|
(if (check-vars vars-match vars-expand)
|
|
|
|
(list vars-match match expand)
|
|
|
|
'mismatch))))
|
|
|
|
|
|
|
|
(define (expand-clauses clauses rename)
|
|
|
|
(cond ((null? clauses)
|
|
|
|
`(,_quote (syntax-error "no matching pattern")))
|
|
|
|
((compare (car clauses) 'mismatch)
|
|
|
|
`(,_syntax-error "invalid rule"))
|
|
|
|
(else
|
2014-07-19 01:25:22 -04:00
|
|
|
(let ((vars (list-ref (car clauses) 0))
|
|
|
|
(match (list-ref (car clauses) 1))
|
|
|
|
(expand (list-ref (car clauses) 2)))
|
2014-04-03 02:34:55 -04:00
|
|
|
`(,_let ,(map (lambda (v) (list (var->sym v) '())) vars)
|
|
|
|
(,_let ((result (,_call/cc (,_lambda (exit) ,match))))
|
|
|
|
(,_if result
|
|
|
|
,expand
|
|
|
|
,(expand-clauses (cdr clauses) rename))))))))
|
|
|
|
|
|
|
|
(define (normalize-form form)
|
|
|
|
(if (and (list? form) (>= (length form) 2))
|
|
|
|
(let ((ellipsis '...)
|
|
|
|
(literals (cadr form))
|
|
|
|
(rules (cddr form)))
|
|
|
|
|
|
|
|
(when (symbol? literals)
|
|
|
|
(set! ellipsis literals)
|
|
|
|
(set! literals (car rules))
|
|
|
|
(set! rules (cdr rules)))
|
|
|
|
|
|
|
|
(if (and (symbol? ellipsis)
|
|
|
|
(list? literals)
|
|
|
|
(every? symbol? literals)
|
|
|
|
(list? rules)
|
|
|
|
(every? (lambda (l) (and (list? l) (= (length l) 2))) rules))
|
|
|
|
(if (member ellipsis literals compare)
|
|
|
|
`(syntax-rules #f ,literals ,@rules)
|
|
|
|
`(syntax-rules ,ellipsis ,literals ,@rules))
|
|
|
|
#f))
|
|
|
|
#f))
|
|
|
|
|
|
|
|
(let ((form (normalize-form form)))
|
|
|
|
(if form
|
2014-07-19 01:25:22 -04:00
|
|
|
(let ((ellipsis (list-ref form 1))
|
|
|
|
(literals (list-ref form 2))
|
|
|
|
(rules (list-tail form 3)))
|
2014-04-03 02:34:55 -04:00
|
|
|
(let ((clauses (map (lambda (rule) (compile-rule ellipsis literals rule))
|
|
|
|
rules)))
|
|
|
|
`(,_er-macro-transformer
|
|
|
|
(,_lambda (expr rename cmp)
|
|
|
|
,(expand-clauses clauses r)))))
|
|
|
|
|
|
|
|
`(,_syntax-error "malformed syntax-rules"))))))
|
|
|
|
|
2014-08-03 02:47:25 -04:00
|
|
|
(export syntax-rules)
|
|
|
|
|
2014-08-04 13:20:05 -04:00
|
|
|
|
|
|
|
;; 4.2.6. Dynamic bindings
|
|
|
|
|
|
|
|
(import (picrin parameter))
|
|
|
|
|
|
|
|
(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)))))))
|
|
|
|
|
|
|
|
(export parameterize make-parameter)
|
|
|
|
|
|
|
|
|
|
|
|
;; 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-08-03 18:38:27 -04:00
|
|
|
;; 5.5 Recored-type definitions
|
2014-08-03 02:47:25 -04:00
|
|
|
|
2014-08-06 09:15:35 -04:00
|
|
|
(import (picrin record)
|
|
|
|
(scheme write))
|
|
|
|
|
|
|
|
(define ((default-record-writer ctor) obj)
|
|
|
|
(let ((port (open-output-string)))
|
|
|
|
(display "#.(" port)
|
|
|
|
(display (car ctor) port)
|
|
|
|
(for-each
|
|
|
|
(lambda (field)
|
|
|
|
(display " " port)
|
|
|
|
(write (record-ref obj field) port))
|
|
|
|
(cdr ctor))
|
|
|
|
(display ")" port)
|
|
|
|
(get-output-string port)))
|
|
|
|
|
|
|
|
(define ((boot-make-record-type <meta-type>) name ctor)
|
|
|
|
(let ((rectype (make-record <meta-type>)))
|
2014-08-03 23:40:41 -04:00
|
|
|
(record-set! rectype 'name name)
|
2014-08-06 09:15:35 -04:00
|
|
|
(record-set! rectype 'writer (default-record-writer ctor))
|
2014-08-03 18:38:27 -04:00
|
|
|
rectype))
|
|
|
|
|
2014-08-06 09:15:35 -04:00
|
|
|
(define <record-type>
|
|
|
|
(let ((<record-type>
|
|
|
|
((boot-make-record-type #t) 'record-type '(record-type name writer))))
|
|
|
|
(record-set! <record-type> '@@type <record-type>)
|
|
|
|
<record-type>))
|
|
|
|
|
|
|
|
(define make-record-type (boot-make-record-type <record-type>))
|
2014-08-06 03:27:12 -04:00
|
|
|
|
2014-08-03 18:38:27 -04:00
|
|
|
(define-syntax define-record-constructor
|
|
|
|
(ir-macro-transformer
|
|
|
|
(lambda (form inject compare?)
|
2014-08-04 13:08:14 -04:00
|
|
|
(let ((rectype (car (cdr form)))
|
|
|
|
(name (car (cdr (cdr form))))
|
|
|
|
(fields (cdr (cdr (cdr form)))))
|
2014-08-03 18:38:27 -04:00
|
|
|
`(define (,name ,@fields)
|
|
|
|
(let ((record (make-record ,rectype)))
|
|
|
|
,@(map (lambda (field)
|
2014-08-03 23:40:41 -04:00
|
|
|
`(record-set! record ',field ,field))
|
2014-08-03 18:38:27 -04:00
|
|
|
fields)
|
|
|
|
record))))))
|
|
|
|
|
|
|
|
(define-syntax define-record-predicate
|
|
|
|
(ir-macro-transformer
|
|
|
|
(lambda (form inject compare?)
|
2014-08-04 13:08:14 -04:00
|
|
|
(let ((rectype (car (cdr form)))
|
|
|
|
(name (car (cdr (cdr form)))))
|
2014-08-03 18:38:27 -04:00
|
|
|
`(define (,name obj)
|
2014-08-04 10:01:47 -04:00
|
|
|
(and (record? obj)
|
2014-08-05 22:30:14 -04:00
|
|
|
(eq? (record-type obj)
|
|
|
|
,rectype)))))))
|
2014-08-03 18:38:27 -04:00
|
|
|
|
|
|
|
(define-syntax define-record-field
|
|
|
|
(ir-macro-transformer
|
|
|
|
(lambda (form inject compare?)
|
2014-08-04 13:08:14 -04:00
|
|
|
(let ((pred (car (cdr form)))
|
|
|
|
(field-name (car (cdr (cdr form))))
|
|
|
|
(accessor (car (cdr (cdr (cdr form)))))
|
|
|
|
(modifier? (cdr (cdr (cdr (cdr form))))))
|
2014-08-03 18:38:27 -04:00
|
|
|
(if (null? modifier?)
|
|
|
|
`(define (,accessor record)
|
2014-08-03 23:40:41 -04:00
|
|
|
(if (,pred record)
|
|
|
|
(record-ref record ',field-name)
|
|
|
|
(error "wrong record type")))
|
2014-08-03 18:38:27 -04:00
|
|
|
`(begin
|
|
|
|
(define (,accessor record)
|
2014-08-03 23:40:41 -04:00
|
|
|
(if (,pred record)
|
|
|
|
(record-ref record ',field-name)
|
|
|
|
(error "wrong record type")))
|
2014-08-03 18:38:27 -04:00
|
|
|
(define (,(car modifier?) record val)
|
2014-08-03 23:40:41 -04:00
|
|
|
(if (,pred record)
|
|
|
|
(record-set! record ',field-name val)
|
|
|
|
(error "wrong record type")))))))))
|
2014-08-03 18:38:27 -04:00
|
|
|
|
|
|
|
(define-syntax define-record-type
|
|
|
|
(ir-macro-transformer
|
|
|
|
(lambda (form inject compare?)
|
2014-08-04 13:08:14 -04:00
|
|
|
(let ((name (car (cdr form)))
|
|
|
|
(ctor (car (cdr (cdr form))))
|
|
|
|
(pred (car (cdr (cdr (cdr form)))))
|
|
|
|
(fields (cdr (cdr (cdr (cdr form))))))
|
2014-08-03 18:38:27 -04:00
|
|
|
`(begin
|
2014-08-06 03:54:18 -04:00
|
|
|
(define ,name (make-record-type ',name ',ctor))
|
2014-08-04 13:08:14 -04:00
|
|
|
(define-record-constructor ,name ,@ctor)
|
2014-08-03 18:38:27 -04:00
|
|
|
(define-record-predicate ,name ,pred)
|
2014-08-03 23:40:41 -04:00
|
|
|
,@(map (lambda (field) `(define-record-field ,pred ,@field))
|
2014-08-03 18:38:27 -04:00
|
|
|
fields))))))
|
|
|
|
|
2014-08-03 02:47:25 -04:00
|
|
|
(export define-record-type)
|
|
|
|
|
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-08-05 13:14:43 -04:00
|
|
|
;; 6.5 Symbols
|
|
|
|
|
|
|
|
(export symbol?
|
|
|
|
symbol=?
|
|
|
|
symbol->string
|
|
|
|
string->symbol)
|
|
|
|
|
2014-08-03 02:47:25 -04:00
|
|
|
;; 6.6 Characters
|
|
|
|
|
|
|
|
(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>=? >=)
|
|
|
|
|
|
|
|
(export char=?
|
|
|
|
char<?
|
|
|
|
char>?
|
|
|
|
char<=?
|
|
|
|
char>=?)
|
|
|
|
|
|
|
|
;; 6.7 String
|
|
|
|
|
|
|
|
(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)))))
|
2014-07-27 01:29:08 -04:00
|
|
|
|
2014-08-03 02:47:25 -04:00
|
|
|
(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)
|
|
|
|
|
|
|
|
;; 6.8. Vector
|
|
|
|
|
|
|
|
(define (vector . objs)
|
|
|
|
(list->vector objs))
|
|
|
|
|
|
|
|
(define (vector->string . args)
|
|
|
|
(list->string (apply vector->list args)))
|
|
|
|
|
|
|
|
(define (string->vector . args)
|
|
|
|
(list->vector (apply string->list args)))
|
|
|
|
|
|
|
|
(export vector vector->string string->vector)
|
|
|
|
|
|
|
|
;; 6.9 bytevector
|
|
|
|
|
|
|
|
(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))
|
|
|
|
|
|
|
|
(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)))))
|
|
|
|
|
|
|
|
(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)))))
|
|
|
|
|
|
|
|
(export bytevector
|
|
|
|
bytevector->list
|
|
|
|
list->bytevector
|
|
|
|
utf8->string
|
|
|
|
string->utf8)
|
|
|
|
|
|
|
|
;; 6.10 control features
|
|
|
|
|
|
|
|
(define (string-map f . strings)
|
|
|
|
(list->string (apply map f (map string->list strings))))
|
|
|
|
|
|
|
|
(define (string-for-each f . strings)
|
|
|
|
(apply for-each f (map string->list strings)))
|
|
|
|
|
|
|
|
(define (vector-map f . vectors)
|
|
|
|
(list->vector (apply map f (map vector->list vectors))))
|
|
|
|
|
|
|
|
(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)
|
|
|
|
|
|
|
|
;; 6.13. Input and output
|
|
|
|
|
|
|
|
(define (call-with-port port proc)
|
|
|
|
(dynamic-wind
|
|
|
|
(lambda () #f)
|
|
|
|
(lambda () (proc port))
|
|
|
|
(lambda () (close-port port))))
|
|
|
|
|
|
|
|
(export call-with-port))
|