-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.zp
36 lines (34 loc) · 1.45 KB
/
struct.zp
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
(define-syntax define-struct
(syntax-rules "define a data type <par>id</par> with the fields <par>fields</par>.
The macro also automatically defines field accessor, a type predicate and a constructor.
Example:
<zepto>
(define-struct pair (fst snd))
; this will define the functions:
; (pair:make-pair fst snd) (constructor)
; (pair:pair? obj) (type predicate)
; (pair:get-fst pair) (field getter)
; (pair:set-fst pair) (field setter)
; (pair:get-snd pair) (field getter)
; (pair:set-snd pair) (field setter)
</zepto>
params:
- id: the name of the data type
- fields: a list of field names
complexity: O(n) where n is the length of <par>fields</par>
returns: the last function" ()
((define-struct "intern" id (field))
(list `(field ,(->symbol id ":get-" field) ,(->symbol id ":set-" field))))
((define-struct "intern" id (field fields ...))
(++
(list `(field ,(->symbol id ":get-" field) ,(->symbol id ":set-" field)))
(define-struct "intern" id (fields ...))))
((define-struct id (fields ...))
(let* ((env (current-env))
(make (->symbol id ":make-" id))
(check (->symbol id ":" id "?"))
(template (++ `(id
(,make fields ...)
,check)
(define-struct "intern" id (fields ...)))))
(eval (macro-expand (cons 'define-record-type template)) env)))))