util.scm 5.6 KB

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