util.scm 4.6 KB

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