]> git.jsancho.org Git - gacela.git/blob - src/utils.scm
d63a51148f2a8f590ae1e2a545abc8dc49d5dd16
[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             make-producer))
24
25
26 ;;; Cache for procedures
27
28 (define (use-cache-with proc)
29   "Cache for procedures"
30   (let ((cache (make-weak-value-hash-table)))
31     (lambda (. param)
32       (let* ((key param)
33              (res (hash-ref cache key)))
34         (cond (res res)
35               (else
36                (set! res (apply proc param))
37                (hash-set! cache key res)
38                res))))))
39
40
41 ;;; Playing with procedures arguments
42
43 (define undefined)
44 (define (bound? var) (not (eq? var undefined)))
45
46 (define (required-arguments args values)
47   "Return an alist with required arguments and their values"
48   (define (f vars values)
49     (cond ((null? vars) '())
50           ((null? values) (assoc-set! (f (cdr vars) '())
51                                       (car vars)
52                                       undefined))
53           (else (assoc-set! (f (cdr vars) (cdr values))
54                             (car vars)
55                             (car values)))))
56   (f (assoc-ref args 'required) values))
57
58 (define (optional-arguments args values)
59   "Return an alist with optional arguments and their values"
60   (define (f vars values)
61     (cond ((null? vars) '())
62           ((null? values) (assoc-set! (f (cdr vars) '())
63                                       (car vars)
64                                       undefined))
65           (else (assoc-set! (f (cdr vars) (cdr values))
66                             (car vars)
67                             (car values)))))
68   (f (assoc-ref args 'optional)
69      (list-tail values
70                 (min (length (assoc-ref args 'required))
71                      (length values)))))
72
73 (define (keyword-arguments args values)
74   "Return an alist with keyword arguments and their values"
75   (define (f vars values)
76     (cond ((null? vars) '())
77           (else
78            (let ((val (member (car vars) values)))
79              (assoc-set! (f (cdr vars) values)
80                             (keyword->symbol (car vars))
81                             (if val (cadr val) undefined))))))
82   (f (map (lambda (x) (car x)) (assoc-ref args 'keyword)) values))
83
84 (define (rest-arguments args values)
85   "Return an alist with rest arguments"
86   (let ((rest (assoc-ref args 'rest)))
87     (cond (rest (assoc-set! '()
88                             rest
89                             (list-tail values
90                                        (min (+ (length (assoc-ref args 'required))
91                                                (length (assoc-ref args 'optional)))
92                                             (length values)))))
93           (else '()))))
94
95 (define (arguments-calling proc values)
96   "Return an alist with procedure arguments and their values"
97   (let ((args ((@ (ice-9 session) procedure-arguments) proc)))
98     (append (required-arguments args values)
99             (optional-arguments args values)
100             (keyword-arguments args values)
101             (rest-arguments args values))))
102
103 (define (required-arguments-apply args values)
104   "Return a list with required arguments for use with apply"
105   (define (f vars values)
106     (cond ((null? vars) '())
107           (else 
108            (cons (assoc-ref values (car vars))
109                  (f (cdr vars) values)))))
110   (f (assoc-ref args 'required) values))
111
112 (define (optional-arguments-apply args values)
113   "Return a list with optional arguments for use with apply"
114   (define (f vars values)
115     (cond ((null? vars) '())
116           (else (let ((a (f (cdr vars) values))
117                       (val (assoc (car vars) values)))
118                   (cond ((and val (bound? (cdr val)))
119                          (cons (cdr val) a))
120                         (else a))))))
121   (f (assoc-ref args 'optional) values))
122
123 (define (keyword-arguments-apply args values)
124   "Return a list with keyword arguments for use with apply"
125   (define (f vars values)
126     (cond ((null? vars) '())
127           (else (let ((a (f (cdr vars) values))
128                       (val (assoc (keyword->symbol (car vars)) values)))
129                   (cond ((and val (bound? (cdr val)))
130                          (cons (car vars) (cons (cdr val) a)))
131                         (else a))))))
132   (f (map (lambda (x) (car x)) (assoc-ref args 'keyword)) values))
133
134 (define (rest-arguments-apply args values)
135   "Return a list with rest arguments for use with apply"
136   (let ((rest (assoc-ref args 'rest)))
137     (cond (rest (assoc-ref values rest))
138           (else '()))))
139   
140 (define (arguments-apply proc values)
141   "Return a list for use with apply"
142   (let ((args ((@ (ice-9 session) procedure-arguments) proc)))
143     (append (required-arguments-apply args values)
144             (optional-arguments-apply args values)
145             (keyword-arguments-apply args values)
146             (rest-arguments-apply args values))))
147
148
149 ;;; Continuations and coroutines
150
151 (define (make-producer body)
152   (define resume #f)
153   (lambda (real-send)
154     (define send-to real-send)
155     (define (send value-to-send)
156       (set! send-to
157             (call/cc
158              (lambda (k)
159                (set! resume k)
160                (send-to value-to-send)))))
161     (if resume
162         (resume real-send)
163         (body send))))