PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/support.scm

http://github.com/pablomarx/Thomas
Scheme | 241 lines | 163 code | 23 blank | 55 comment | 0 complexity | f9d7b47e22053864d88c8548ed2195dc MD5 | raw file
  1. ;* Copyright 1992 Digital Equipment Corporation
  2. ;* All Rights Reserved
  3. ;*
  4. ;* Permission to use, copy, and modify this software and its documentation is
  5. ;* hereby granted only under the following terms and conditions. Both the
  6. ;* above copyright notice and this permission notice must appear in all copies
  7. ;* of the software, derivative works or modified versions, and any portions
  8. ;* thereof, and both notices must appear in supporting documentation.
  9. ;*
  10. ;* Users of this software agree to the terms and conditions set forth herein,
  11. ;* and hereby grant back to Digital a non-exclusive, unrestricted, royalty-free
  12. ;* right and license under any changes, enhancements or extensions made to the
  13. ;* core functions of the software, including but not limited to those affording
  14. ;* compatibility with other hardware or software environments, but excluding
  15. ;* applications which incorporate this software. Users further agree to use
  16. ;* their best efforts to return to Digital any such changes, enhancements or
  17. ;* extensions that they make and inform Digital of noteworthy uses of this
  18. ;* software. Correspondence should be provided to Digital at:
  19. ;*
  20. ;* Director, Cambridge Research Lab
  21. ;* Digital Equipment Corp
  22. ;* One Kendall Square, Bldg 700
  23. ;* Cambridge MA 02139
  24. ;*
  25. ;* This software may be distributed (but not offered for sale or transferred
  26. ;* for compensation) to third parties, provided such third parties agree to
  27. ;* abide by the terms and conditions of this notice.
  28. ;*
  29. ;* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  30. ;* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  31. ;* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  32. ;* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  33. ;* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  34. ;* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  35. ;* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  36. ;* SOFTWARE.
  37. ; $Id: support.scm,v 1.22 1992/09/20 17:43:11 birkholz Exp $
  38. ;;;; General Supporting Procedures
  39. ;;; This file contains general support routines used at runtime by Dylan
  40. ;;; procedures. See also the files generic.scm (generic operator
  41. ;;; dispatch), class.scm (class heterarchy), and runtime.scm
  42. ;;; (Dylan-callable operations written in Scheme).
  43. (define the-unassigned-value '<<THE-UNASSIGNED-VALUE>>)
  44. (define *the-uninitialized-slot-value* (list '<<UNINITIALIZED>>))
  45. (define next-method:not-generic #T) ; Passed as default
  46. ; next-method parameter when
  47. ; not going through generic
  48. ; dispatch mechanism
  49. ;; Create a new symbol from two strings (prefix and postfix) and an
  50. ;; existing symbol or string.
  51. (define (new-name prefix name postfix)
  52. (string->symbol
  53. (canonicalize-string-for-symbol
  54. (list->string
  55. (append (string->list prefix)
  56. (string->list ((if (string? name)
  57. canonicalize-string-for-symbol
  58. symbol->string)
  59. name))
  60. (string->list postfix))))))
  61. ;;;; Slot descriptors
  62. (define slot-type
  63. (make-record-type
  64. 'dylan-slot
  65. '(debug-name
  66. getter
  67. setter
  68. type
  69. init-value
  70. has-init-value?
  71. init-function
  72. init-keyword
  73. required-init-keyword
  74. allocation
  75. inherited?
  76. data-location))) ; Depends on allocation type:
  77. ; Instance = offset in instance data
  78. ; Class = (class-ptr . offset)
  79. ; Each-Subclass = offset in class-data
  80. ; Virtual = #F
  81. ; Constant = the value of the slot
  82. (define slot? (record-predicate slot-type))
  83. (define make-slot (record-constructor slot-type))
  84. (define slot.debug-name
  85. (record-accessor slot-type 'debug-name))
  86. (define slot.getter
  87. (record-accessor slot-type 'getter))
  88. (define slot.setter
  89. (record-accessor slot-type 'setter))
  90. (define slot.type
  91. (record-accessor slot-type 'type))
  92. (define slot.init-value
  93. (record-accessor slot-type 'init-value))
  94. (define slot.has-initial-value?
  95. (record-accessor slot-type 'has-init-value?))
  96. (define slot.init-function
  97. (record-accessor slot-type 'init-function))
  98. (define slot.init-keyword
  99. (record-accessor slot-type 'init-keyword))
  100. (define slot.required-init-keyword
  101. (record-accessor slot-type 'required-init-keyword))
  102. (define slot.allocation
  103. (record-accessor slot-type 'allocation))
  104. (define slot.inherited?
  105. (record-accessor slot-type 'inherited?))
  106. (define slot.data-location
  107. (record-accessor slot-type 'data-location))
  108. (define set-slot.debug-name!
  109. (record-updater slot-type 'debug-name))
  110. (define set-slot.getter!
  111. (record-updater slot-type 'getter))
  112. (define set-slot.setter!
  113. (record-updater slot-type 'setter))
  114. (define set-slot.type!
  115. (record-updater slot-type 'type))
  116. (define set-slot.init-value!
  117. (record-updater slot-type 'init-value))
  118. (define set-slot.has-initial-value?!
  119. (record-updater slot-type 'has-init-value?))
  120. (define set-slot.init-function!
  121. (record-updater slot-type 'init-function))
  122. (define set-slot.init-keyword!
  123. (record-updater slot-type 'init-keyword))
  124. (define set-slot.required-init-keyword!
  125. (record-updater slot-type 'required-init-keyword))
  126. (define set-slot.allocation!
  127. (record-updater slot-type 'allocation))
  128. (define set-slot.inherited?!
  129. (record-updater slot-type 'inherited?))
  130. (define set-slot.data-location!
  131. (record-updater slot-type 'data-location))
  132. ;;;; Keywords and names
  133. (define (keyword? obj)
  134. (and (symbol? obj)
  135. (let ((string (symbol->string obj)))
  136. (char=? #\: (string-ref string
  137. (- (string-length string) 1))))))
  138. (define (dylan::find-keyword keyword-list keyword default-fn)
  139. (let loop ((rest keyword-list))
  140. (cond ((null? rest) (default-fn))
  141. ((eq? keyword (car rest)) (cadr rest))
  142. (else (loop (cddr rest))))))
  143. (define (validate-keywords arglist allowed error)
  144. (let loop ((args arglist))
  145. (cond ((null? args) #T)
  146. ((or (not (pair? args))
  147. (not (pair? (cdr args)))
  148. (not (keyword? (car args))))
  149. (error "incorrect keyword format" arglist allowed))
  150. ((not (or (eq? allowed #T)
  151. (memq (car args) allowed)))
  152. (error "illegal keyword argument" (car args)
  153. (if (eq? allowed #T) 'ANY allowed)))
  154. (else (loop (cddr args))))))
  155. ;;;; General utilities
  156. (define (last l)
  157. (if (null? l)
  158. '()
  159. (let loop ((l l))
  160. (if (null? (cdr l))
  161. (car l)
  162. (loop (cdr l))))))
  163. (define (split-last l continue)
  164. (if (null? l)
  165. (continue '() '())
  166. (let loop ((previous '())
  167. (left l))
  168. (if (null? (cdr left))
  169. (continue (reverse previous) left)
  170. (loop (cons (car left) previous) (cdr left))))))
  171. (define (subset? smaller larger)
  172. (let loop
  173. ((smaller smaller))
  174. (if (pair? smaller)
  175. (if (memq (car smaller) larger)
  176. (loop (cdr smaller))
  177. #F)
  178. #T)))
  179. (define (unique? objects predicate)
  180. (let loop ((objects objects))
  181. (or (null? objects)
  182. (and (not (predicate (car objects) (cdr objects)))
  183. (loop (cdr objects))))))
  184. (define (set-difference main subtract predicate)
  185. (let loop ((result '())
  186. (to-do main))
  187. (cond ((null? to-do) (reverse result))
  188. ((predicate (car to-do) subtract)
  189. (loop result (cdr to-do)))
  190. (else (loop (cons (car to-do) result) (cdr to-do))))))
  191. (define (union set1 set2 predicate)
  192. (let loop ((result set1)
  193. (remaining set2))
  194. (if (null? remaining)
  195. result
  196. (loop (if (predicate (car remaining) result)
  197. result
  198. (cons (car remaining) result))
  199. (cdr remaining)))))
  200. (define (adjoin elem set predicate)
  201. (if (predicate elem set)
  202. set
  203. (cons elem set)))
  204. (define (any? fn l)
  205. (and (not (null? l))
  206. (or (fn (car l))
  207. (any? fn (cdr l)))))
  208. (define (all? fn l)
  209. (or (null? l)
  210. (and (fn (car l))
  211. (all? fn (cdr l)))))
  212. (define (but-first count list)
  213. (if (= count 0)
  214. list
  215. (but-first (- count 1) (cdr list))))
  216. (define (population->list population)
  217. (map-over-population population (lambda (x) x)))