-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcj-source-util--include.scm
153 lines (130 loc) · 4.58 KB
/
cj-source-util--include.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
;;; Copyright 2010-2018 by Christian Jaeger <ch@christianjaeger.ch>
;;; This file is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License (GPL) as published
;;; by the Free Software Foundation, either version 2 of the License, or
;;; (at your option) any later version.
;; XX move to cj-source-lambda.scm?
'(require)
'(export schemedefinition-arity:template->checker
schemedefinition-arity:pattern->template
schemedefinition-arity-checker
improper-length ;;via (include "improper-length--source.scm")
safer-apply)
(include "cj-standarddeclares-1--include.scm")
;; tests see cj-source-util-test.scm
(define (schemedefinition-arity:template->checker t)
(let ((min-count (vector-ref t 1)))
(case (vector-ref t 0)
((at-least)
(lambda (argslen)
(if (>= argslen min-count)
'ok
'not-enough)))
((up-to)
(let ((max-count (vector-ref t 2)))
(lambda (argslen)
(if (>= argslen min-count)
(if (<= argslen max-count)
'ok
'too-many)
'not-enough))))
((exact)
(lambda (argslen)
(cond ((= argslen min-count)
'ok)
((< argslen min-count)
'not-enough)
(else
'too-many))))
(else
(error "bug")))))
(define (schemedefinition-arity-template-qualifier? v)
(case v
;; XX should probably rename 'exact to 'fixed
((at-least up-to exact) #t)
(else #f)))
(define (schemedefinition-arity:pattern->template bindS)
;; -> (vector-of schemedefinition-arity-template-qualifier?
;; fixnum-natural0?)
(define (inc x) ;; copy from cj-env because of phasing issue
(+ x 1))
(let lp ((l (source-code bindS))
(min-count 0))
(define (at-least)
(vector 'at-least min-count))
(define (up-to max-count)
(vector 'up-to min-count max-count))
(define (handle-key l max-count)
;; l is the remainder after #!key. Each key argument
;; requires two args. [and we still don't check for
;; correct keys yet, not even for even number of
;; arguments in key area]
(let lp ((l l)
(max-count max-count))
(cond ((null? l)
(up-to max-count))
((pair? l)
;; *almost* copy from above, hm.
(let ((a (car l)))
(case (source-code a)
((#!rest)
(at-least))
((#!optional)
(error "#!optional after #!key in argument list:"
bindS))
((#!key)
(error "more than one #!key in argument list:" bindS))
(else
(lp (cdr l)
(+ max-count 2))))))
(else
(at-least)))))
(cond ((pair? l)
(let ((a (car l)))
(case (source-code a)
((#!rest)
(at-least))
((#!optional)
(let lp ((l (cdr l))
(max-count min-count))
(cond ((null? l)
(up-to max-count))
((pair? l)
(let ((a (car l)))
(case (source-code a)
((#!rest)
(at-least))
((#!optional)
(error "more than one #!optional in argument list:"
bindS))
((#!key)
(handle-key (cdr l) max-count))
(else
(lp (cdr l)
(inc max-count))))))
(else
(at-least)))))
((#!key)
(handle-key (cdr l) min-count))
(else
(lp (cdr l)
(inc min-count))))))
((null? l)
(vector 'exact min-count))
(else
(at-least)))))
(define (schemedefinition-arity-checker x)
(schemedefinition-arity:template->checker
(schemedefinition-arity:pattern->template
x)))
(include "improper-length--include.scm")
(define (safer-apply template fn args err cont)
(declare (not optimize-dead-local-variables))
(let ((len (improper-length args)))
(if (negative? len)
(error "got improper list:" args)
(case ((schemedefinition-arity:template->checker template) len)
((ok) (cont (apply fn args)))
((not-enough) (err "not enough arguments"))
((too-many) (err "too many arguments"))
(else (error "bug in safer-apply"))))))