Debian Codegraph @

%3a244/define-values.sls

raw
1 #!r6rs
2
3 ;; Copyright (C) Marc Nieper-Wißkirchen (2022). All Rights Reserved.
4
5 ;; Permission is hereby granted, free of charge, to any person
6 ;; obtaining a copy of this software and associated documentation
7 ;; files (the "Software"), to deal in the Software without
8 ;; restriction, including without limitation the rights to use, copy,
9 ;; modify, merge, publish, distribute, sublicense, and/or sell copies
10 ;; of the Software, and to permit persons to whom the Software is
11 ;; furnished to do so, subject to the following conditions:
12
13 ;; The above copyright notice and this permission notice shall be
14 ;; included in all copies or substantial portions of the Software.
15
16 ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 ;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 ;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 ;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 ;; BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 ;; ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 ;; CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 ;; SOFTWARE.
24
25 (library (srfi :244 define-values)
26 (export define-values)
27 (import (rnrs (6)))
28
29 (define-syntax define-values
30 (lambda (stx)
31 (define who 'define-values)
32 (define parse-formals
33 (lambda (formals)
34 (define output
35 (lambda (id*)
36 (let f ([id* id*] [i 0])
37 (if (null? id*)
38 '()
39 `((,(car id*) ,i)
40 ,@(f (cdr id*) (+ i 1)))))))
41 (syntax-case formals ()
42 [(id ...)
43 (for-all identifier? #'(id ...))
44 (output #'(id ...))]
45 [(id1 ... . id2)
46 (for-all identifier? #'(id1 ... id2))
47 (output #'(id1 ... id2))]
48 [_
49 (syntax-violation who "invalid formals" stx formals)])))
50 (syntax-case stx ()
51 [(_ () expr)
52 #'(define tmp (begin expr #f))]
53 [(_ (id) expr)
54 (identifier? #'id)
55 #'(define id expr)]
56 [(_ id expr)
57 (identifier? #'id)
58 #'(define id (let-values ([tmp expr])
59 tmp))]
60 [(_ formals expr)
61 (with-syntax ([((id i) ...) (parse-formals #'formals)])
62 #'(begin
63 (define tmp (let-values ([formals expr])
64 (vector id ...)))
65 (define id (vector-ref tmp i))
66 ...))]
67 [_
68 (syntax-violation who "invalid syntax" stx)])))
69
70 )
71
72 ;; Local Variables:
73 ;; mode: scheme
74 ;; End: