mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-21 22:59:16 +02:00
Patches from Ben Mather.
Allow #f to leave (srfi 99) record constructors and predicates undefined. Distinguish default immutable fields (raw identifier), from default mutable fields (list of one identifier) in the syntactic layer by not creating setters for the immutable fields. The record introspection can still mutate such fields. Fixes issue #190.
This commit is contained in:
parent
19e5398b92
commit
92ccc0144a
2 changed files with 98 additions and 56 deletions
|
@ -37,14 +37,15 @@
|
||||||
(,(rename 'make-type-predicate)
|
(,(rename 'make-type-predicate)
|
||||||
,(id->string pred-name)
|
,(id->string pred-name)
|
||||||
,name)))
|
,name)))
|
||||||
#f)
|
'())
|
||||||
;; accessors
|
;; accessors
|
||||||
,@(map (lambda (f)
|
,@(map (lambda (f)
|
||||||
(let ((g (if (and (pair? f) (pair? (cdr f)))
|
(let ((g (if (and (pair? f) (pair? (cdr f)))
|
||||||
(cadr f)
|
(cadr f)
|
||||||
(and (identifier? f)
|
|
||||||
(string->symbol
|
(string->symbol
|
||||||
(string-append name-str "-" (id->string f)))))))
|
(string-append name-str
|
||||||
|
"-"
|
||||||
|
(id->string (if (pair? f) (car f) f)))))))
|
||||||
(and g
|
(and g
|
||||||
`(,_define ,g
|
`(,_define ,g
|
||||||
(,(rename 'make-getter)
|
(,(rename 'make-getter)
|
||||||
|
@ -53,11 +54,14 @@
|
||||||
(,_type_slot_offset ,name ',(if (pair? f) (car f) f)))))))
|
(,_type_slot_offset ,name ',(if (pair? f) (car f) f)))))))
|
||||||
fields)
|
fields)
|
||||||
,@(map (lambda (f)
|
,@(map (lambda (f)
|
||||||
(let ((s (if (and (pair? f) (pair? (cdr f)) (pair? (cddr f)))
|
(let ((s (and (pair? f)
|
||||||
|
(if (and (pair? (cdr f)) (pair? (cddr f)))
|
||||||
(car (cddr f))
|
(car (cddr f))
|
||||||
(and (identifier? f)
|
|
||||||
(string->symbol
|
(string->symbol
|
||||||
(string-append name-str "-" (id->string f) "-set!"))))))
|
(string-append name-str
|
||||||
|
"-"
|
||||||
|
(id->string (car f))
|
||||||
|
"-set!"))))))
|
||||||
(and s
|
(and s
|
||||||
`(,_define ,s
|
`(,_define ,s
|
||||||
(,(rename 'make-setter)
|
(,(rename 'make-setter)
|
||||||
|
@ -66,9 +70,10 @@
|
||||||
(,_type_slot_offset ,name ',(if (pair? f) (car f) f)))))))
|
(,_type_slot_offset ,name ',(if (pair? f) (car f) f)))))))
|
||||||
fields)
|
fields)
|
||||||
;; constructor
|
;; constructor
|
||||||
,(if make-fields
|
,@(if make-name
|
||||||
|
(if make-fields
|
||||||
(let ((fields (map (lambda (f) (cons (rename f) f)) make-fields)))
|
(let ((fields (map (lambda (f) (cons (rename f) f)) make-fields)))
|
||||||
`(,_define ,make-name
|
`((,_define ,make-name
|
||||||
,(let lp ((ls fields) (sets '()))
|
,(let lp ((ls fields) (sets '()))
|
||||||
(cond
|
(cond
|
||||||
((null? ls)
|
((null? ls)
|
||||||
|
@ -87,8 +92,8 @@
|
||||||
(cons (list (car (cddr field)) 'res (cdar ls)) sets)))
|
(cons (list (car (cddr field)) 'res (cdar ls)) sets)))
|
||||||
(else
|
(else
|
||||||
(lp (cdr ls)
|
(lp (cdr ls)
|
||||||
(cons `(,_slot-set! ,name res (,_type_slot_offset ,name ',(cdar ls)) ,(caar ls)) sets))))))))))
|
(cons `(,_slot-set! ,name res (,_type_slot_offset ,name ',(cdar ls)) ,(caar ls)) sets)))))))))))
|
||||||
`(,_define ,make-name
|
`((,_define ,make-name
|
||||||
(,_let ((,_make (,(rename 'make-constructor)
|
(,_let ((,_make (,(rename 'make-constructor)
|
||||||
,(id->string make-name)
|
,(id->string make-name)
|
||||||
,name)))
|
,name)))
|
||||||
|
@ -107,4 +112,5 @@
|
||||||
a))
|
a))
|
||||||
(else
|
(else
|
||||||
(,_slot-set! ,name res (,_type_slot_offset ,name (car p)) (car a))
|
(,_slot-set! ,name res (,_type_slot_offset ,name (car p)) (car a))
|
||||||
(lp (cdr a) (cdr p)))))))))))))))
|
(lp (cdr a) (cdr p)))))))))))
|
||||||
|
'()))))))
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
(cond-expand
|
(cond-expand
|
||||||
(modules
|
(modules
|
||||||
(import (srfi 99)
|
(import (srfi 99)
|
||||||
|
(only (chibi) env-exports)
|
||||||
(only (chibi test) test-begin test-assert test test-end)))
|
(only (chibi test) test-begin test-assert test test-end)))
|
||||||
(else #f))
|
(else #f))
|
||||||
|
|
||||||
|
@ -112,8 +113,8 @@
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(define-record-type person #t #t name sex age)
|
(define-record-type person #t #t (name) (sex) (age))
|
||||||
(define-record-type (employee person) #t #t department salary)
|
(define-record-type (employee person) #t #t (department) (salary))
|
||||||
|
|
||||||
(define bob (make-employee "Bob" 'male 28 'hr 50000.0))
|
(define bob (make-employee "Bob" 'male 28 'hr 50000.0))
|
||||||
(define alice (make-employee "Alice" 'female 32 'research 100000.0))
|
(define alice (make-employee "Alice" 'female 32 'research 100000.0))
|
||||||
|
@ -148,9 +149,9 @@
|
||||||
(test #f (employee-department bob))
|
(test #f (employee-department bob))
|
||||||
(test 0.0 (employee-salary bob))
|
(test 0.0 (employee-salary bob))
|
||||||
|
|
||||||
;;;; SRFI-99 forbids this, but we currently do it anyway.
|
;; SRFI-99 forbids this, but we currently do it anyway.
|
||||||
;;(test-assert (equal? (make-employee "Chuck" 'male 20 'janitorial 50000.0)
|
(test-assert (equal? (make-employee "Chuck" 'male 20 'janitorial 50000.0)
|
||||||
;; (make-employee "Chuck" 'male 20 'janitorial 50000.0)))
|
(make-employee "Chuck" 'male 20 'janitorial 50000.0)))
|
||||||
|
|
||||||
(test-assert (record? alice))
|
(test-assert (record? alice))
|
||||||
(test 'person (rtd-name person))
|
(test 'person (rtd-name person))
|
||||||
|
@ -169,7 +170,7 @@
|
||||||
;; (make-foo x)
|
;; (make-foo x)
|
||||||
;; foo?
|
;; foo?
|
||||||
;; (x foo-x))
|
;; (x foo-x))
|
||||||
|
;;
|
||||||
;; (test-assert (not (rtd-field-mutable? foo 'x)))
|
;; (test-assert (not (rtd-field-mutable? foo 'x)))
|
||||||
|
|
||||||
(define point (make-rtd "point" #(x y)))
|
(define point (make-rtd "point" #(x y)))
|
||||||
|
@ -183,4 +184,39 @@
|
||||||
(test-assert (example? (make-example 3)))
|
(test-assert (example? (make-example 3)))
|
||||||
(test 3 (example-example (make-example 3)))
|
(test 3 (example-example (make-example 3)))
|
||||||
|
|
||||||
|
;; record types definitions with #f passed as either the constructor or
|
||||||
|
;; predicate argument should not create the corresponding function
|
||||||
|
|
||||||
|
(define-record-type abstract
|
||||||
|
#f #t)
|
||||||
|
|
||||||
|
(test #f (memq 'make-abstract (env-exports (current-environment))))
|
||||||
|
|
||||||
|
(define-record-type (derived abstract)
|
||||||
|
#t #f)
|
||||||
|
|
||||||
|
(define instance (make-derived))
|
||||||
|
(test-assert (abstract? instance))
|
||||||
|
(test #f (memq 'derived? (env-exports (current-environment))))
|
||||||
|
|
||||||
|
(define-record-type container
|
||||||
|
#t #t
|
||||||
|
default-immutable
|
||||||
|
(default-mutable)
|
||||||
|
(named-immutable get-container-immutable)
|
||||||
|
(named-mutable get-container-mutable set-container-mutable!))
|
||||||
|
|
||||||
|
(define container-instance (make-container 1 2 3 4))
|
||||||
|
|
||||||
|
(test 1 (container-default-immutable container-instance))
|
||||||
|
(test 2 (container-default-mutable container-instance))
|
||||||
|
(test 3 (get-container-immutable container-instance))
|
||||||
|
(test 4 (get-container-mutable container-instance))
|
||||||
|
|
||||||
|
(container-default-mutable-set! container-instance #t)
|
||||||
|
(test #t (container-default-mutable container-instance))
|
||||||
|
|
||||||
|
(set-container-mutable! container-instance #t)
|
||||||
|
(test #t (get-container-mutable container-instance))
|
||||||
|
|
||||||
(test-end)
|
(test-end)
|
||||||
|
|
Loading…
Add table
Reference in a new issue