]> git.jsancho.org Git - gacela.git/blob - src/utils.scm
eb20531fdc90680abfcd224e942d81afd510af43
[gacela.git] / src / utils.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
8 ;;;
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ;;; GNU General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18 (define-module (gacela utils)
19   #:export (use-cache-with
20             arguments-calling
21             arguments-apply
22             bound?))
23
24
25 ;;; Cache for procedures
26
27 (define (use-cache-with proc)
28   "Cache for procedures"
29   (let ((cache (make-weak-value-hash-table)))
30     (lambda (. param)
31       (let* ((key param)
32              (res (hash-ref cache key)))
33         (cond (res res)
34               (else
35                (set! res (apply proc param))
36                (hash-set! cache key res)
37                res))))))
38
39
40 ;;; Playing with procedures arguments
41
42 (define undefined)
43 (define (bound? var) (not (eq? var undefined)))
44
45 (define (required-arguments args values)
46   "Return an alist with required arguments and their values"
47   (define (f vars values)
48     (cond ((null? vars) '())
49           ((null? values) (assoc-set! (f (cdr vars) '())
50                                       (car vars)
51                                       undefined))
52           (else (assoc-set! (f (cdr vars) (cdr values))
53                             (car vars)
54                             (car values)))))
55   (f (assoc-ref args 'required) values))
56
57 (define (optional-arguments args values)
58   "Return an alist with optional arguments and their values"
59   (define (f vars values)
60     (cond ((null? vars) '())
61           ((null? values) (assoc-set! (f (cdr vars) '())
62                                       (car vars)
63                                       undefined))
64           (else (assoc-set! (f (cdr vars) (cdr values))
65                             (car vars)
66                             (car values)))))
67   (f (assoc-ref args 'optional)
68      (list-tail values
69                 (min (length (assoc-ref args 'required))
70                      (length values)))))
71
72 (define (keyword-arguments args values)
73   "Return an alist with keyword arguments and their values"
74   (define (f vars values)
75     (cond ((null? vars) '())
76           (else
77            (let ((val (member (car vars) values)))
78              (assoc-set! (f (cdr vars) values)
79                             (keyword->symbol (car vars))
80                             (if val (cadr val) undefined))))))
81   (f (map (lambda (x) (car x)) (assoc-ref args 'keyword)) values))
82
83 (define (rest-arguments args values)
84   "Return an alist with rest arguments"
85   (let ((rest (assoc-ref args 'rest)))
86     (cond (rest (assoc-set! '()
87                             rest
88                             (list-tail values
89                                        (min (+ (length (assoc-ref args 'required))
90                                                (length (assoc-ref args 'optional)))
91                                             (length values)))))
92           (else '()))))
93
94 (define (arguments-calling proc values)
95   "Return an alist with procedure arguments and their values"
96   (let ((args ((@ (ice-9 session) procedure-arguments) proc)))
97     (append (required-arguments args values)
98             (optional-arguments args values)
99             (keyword-arguments args values)
100             (rest-arguments args values))))
101
102 (define (required-arguments-apply args values)
103   "Return a list with required arguments for use with apply"
104   (define (f vars values)
105     (cond ((null? vars) '())
106           (else 
107            (cons (assoc-ref values (car vars))
108                  (f (cdr vars) values)))))
109   (f (assoc-ref args 'required) values))
110
111 (define (optional-arguments-apply args values)
112   "Return a list with optional arguments for use with apply"
113   (define (f vars values)
114     (cond ((null? vars) '())
115           (else (let ((a (f (cdr vars) values))
116                       (val (assoc (car vars) values)))
117                   (cond ((and val (bound? (cdr val)))
118                          (cons (cdr val) a))
119                         (else a))))))
120   (f (assoc-ref args 'optional) values))
121
122 (define (keyword-arguments-apply args values)
123   "Return a list with keyword arguments for use with apply"
124   (define (f vars values)
125     (cond ((null? vars) '())
126           (else (let ((a (f (cdr vars) values))
127                       (val (assoc (keyword->symbol (car vars)) values)))
128                   (cond ((and val (bound? (cdr val)))
129                          (cons (car vars) (cons (cdr val) a)))
130                         (else a))))))
131   (f (map (lambda (x) (car x)) (assoc-ref args 'keyword)) values))
132
133 (define (rest-arguments-apply args values)
134   "Return a list with rest arguments for use with apply"
135   (let ((rest (assoc-ref args 'rest)))
136     (cond (rest (assoc-ref values rest))
137           (else '()))))
138   
139 (define (arguments-apply proc values)
140   "Return a list for use with apply"
141   (let ((args ((@ (ice-9 session) procedure-arguments) proc)))
142     (append (required-arguments-apply args values)
143             (optional-arguments-apply args values)
144             (keyword-arguments-apply args values)
145             (rest-arguments-apply args values))))
146
147
148 ;;; Continuations and coroutines
149
150 (define (make-producer body)
151   (define resume #f)
152   (lambda (real-send)
153     (define send-to real-send)
154     (define (send value-to-send)
155       (set! send-to
156             (call/cc
157              (lambda (k)
158                (set! resume k)
159                (send-to value-to-send)))))
160     (if resume
161         (resume real-send)
162         (body send))))