util.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Mike Sperber
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/util/util.scm
  8. (define-module (ps-compiler util util)
  9. #:use-module (prescheme scheme48)
  10. #:export (bug
  11. user-error
  12. user-warning
  13. true false
  14. or-map
  15. remove-similar-elts
  16. select-from-table
  17. table->list table->entry-list
  18. table-push table-pop
  19. merge-lists
  20. vector-every?
  21. make-ignorable
  22. sub-vector->list
  23. flag-assq
  24. enforce
  25. writec
  26. mem?
  27. walk-vector
  28. vector-replace
  29. copy-list
  30. copy-vector
  31. string-hash
  32. char->ascii
  33. object-hash
  34. union intersection set-difference))
  35. ;; Messages
  36. ;;==========================================================================
  37. (define (bug format-string . args)
  38. (apply error (list "~?~% (compiler error)" format-string args)))
  39. (define (user-error format-string . args)
  40. (apply error (list "~?~% (source error)" format-string args)))
  41. (define (user-warning format-string . args)
  42. (apply error (list "~?~% (source error)" format-string args)))
  43. ;; Little utilities.
  44. ;;========================================================================
  45. (define (true x) #t)
  46. (define (false x) #f)
  47. (define (or-map proc list)
  48. (do ((list list (cdr list))
  49. (r '#f (or (proc (car list)) r)))
  50. ((null? list) r)))
  51. (define (remove-similar-elts pred list)
  52. (do ((list list (cdr list))
  53. (res '() (if (mem? pred (car list) res)
  54. res
  55. (cons (car list) res))))
  56. ((null-list? list)
  57. res)))
  58. (define (select-from-table pred table)
  59. (let ((res '()))
  60. (table-walk (lambda (key entry)
  61. (if (pred key entry)
  62. (set! res `((,key . ,entry) . ,res))))
  63. table)
  64. res))
  65. (define (table->list table)
  66. (select-from-table (lambda (x y) #t) table))
  67. ;; this is exported from `tables', but that's a fairly recent development
  68. (define (table->entry-list table)
  69. (let ((res '()))
  70. (table-walk (lambda (key entry)
  71. (set! res (cons entry res)))
  72. table)
  73. res))
  74. (define (table-push table key val)
  75. (let ((old (table-ref table key)))
  76. (table-set! table key (if old
  77. (cons val old)
  78. (list val)))
  79. val))
  80. (define (table-pop table key)
  81. (let ((old (table-ref table key)))
  82. (table-set! table key (cdr old))
  83. (car old)))
  84. (define (merge-lists x y)
  85. (cond ((null? y) x)
  86. (else (do ((z x (cdr z))
  87. (u y (let ((w (car z)))
  88. (if (memq? w u) u (cons w u)))))
  89. ((null? z) u)))))
  90. (define (vector-every? pred vec)
  91. (let loop ((i (- (vector-length vec) '1)))
  92. (cond ((< i '0)
  93. '#t)
  94. ((not (pred (vector-ref vec i)))
  95. '#f)
  96. (else
  97. (loop (- i '1))))))
  98. (define (make-ignorable vars)
  99. (let loop ((vars vars) (res '()))
  100. (cond ((null? vars)
  101. `(ignorable . ,res))
  102. ((atom? vars)
  103. `(ignorable ,vars . ,res))
  104. (else
  105. (loop (cdr vars) (cons (car vars) res))))))
  106. (define (sub-vector->list vec start)
  107. (do ((i (- (vector-length vec) '1) (- i '1))
  108. (r '() (cons (vector-ref vec i) r)))
  109. ((< i start)
  110. r)))
  111. ;; A version of ASSQ that works on lists containing atoms.
  112. (define (flag-assq obj list)
  113. (let loop ((l list))
  114. (if (null-list? l)
  115. #f
  116. (let ((z (car l)))
  117. (if (and (pair? z)
  118. (eq? obj (car z)))
  119. z
  120. (loop (cdr l)))))))
  121. ;; T system stuff
  122. (define (enforce predicate value)
  123. (if (predicate value)
  124. value
  125. (error "enforce ~A failed on ~A" predicate value)))
  126. (define (writec port char)
  127. (write-char char port))
  128. (define (mem? predicate object list)
  129. (let loop ((list list))
  130. (cond ((null? list)
  131. #f)
  132. ((predicate object (car list))
  133. #t)
  134. (else
  135. (loop (cdr list))))))
  136. (define (walk-vector procedure vector)
  137. (do ((i 0 (+ i 1)))
  138. ((>= i (vector-length vector))
  139. (values))
  140. (procedure (vector-ref vector i))))
  141. (define (vector-replace to from count)
  142. (do ((i 0 (+ i 1)))
  143. ((>= i count))
  144. (vector-set! to i (vector-ref from i)))
  145. (values))
  146. (define (copy-vector vector)
  147. (let ((new (make-vector (vector-length vector))))
  148. (do ((i 0 (+ i 1)))
  149. ((= i (vector-length vector)))
  150. (vector-set! new i (vector-ref vector i)))
  151. new))
  152. (define (copy-list list)
  153. (if (null-list? list)
  154. list
  155. (let ((copy (cons (car list) '())))
  156. (let loop ((list (cdr list)) (result copy))
  157. (cond ((not (null-list? list))
  158. (let ((next (cons (car list) '())))
  159. (set-cdr! result next)
  160. (loop (cdr list) next)))))
  161. copy)))
  162. (define (object-hash x)
  163. 0) ; wah!
  164. (define (union l1 l2)
  165. (if (null? l2)
  166. l1
  167. (do ((l1 l1 (cdr l1))
  168. (res l2 (if (memq? (car l1) res) res (cons (car l1) res))))
  169. ((null? l1)
  170. res))))
  171. (define (intersection l1 l2)
  172. (if (null? l2)
  173. '()
  174. (do ((l1 l1 (cdr l1))
  175. (res '() (if (memq? (car l1) l2) (cons (car l1) res) res)))
  176. ((null? l1)
  177. res))))
  178. (define (set-difference l1 l2)
  179. (if (null? l2)
  180. l1
  181. (do ((l1 l1 (cdr l1))
  182. (res '() (if (memq? (car l1) l2) res (cons (car l1) res))))
  183. ((null? l1)
  184. res))))