scheme48.scm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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, Jonathan Rees, Mike Sperber, Robert Ransom
  6. ;;;
  7. ;;; scheme48-1.9.2/scheme/big/big-util.scm
  8. ;;; scheme48-1.9.2/scheme/big/more-port.scm
  9. ;;; scheme48-1.9.2/scheme/rts/current-port.scm
  10. ;;; scheme48-1.9.2/scheme/rts/exception.scm
  11. ;;; scheme48-1.9.2/scheme/rts/util.scm
  12. (define-module (prescheme scheme48)
  13. #:use-module (ice-9 format)
  14. #:use-module (ice-9 textual-ports)
  15. #:use-module (ice-9 q)
  16. #:use-module (srfi srfi-8)
  17. #:use-module (srfi srfi-60)
  18. #:use-module (srfi srfi-111)
  19. #:use-module (rnrs bytevectors)
  20. #:use-module (rnrs io ports)
  21. #:use-module (prescheme s48-defenum)
  22. #:export (arithmetic-shift
  23. ascii->char
  24. char->ascii
  25. ascii-limit
  26. unspecific
  27. unspecific?
  28. make-code-vector
  29. code-vector?
  30. code-vector-ref
  31. code-vector-set!
  32. code-vector-length
  33. make-table
  34. make-integer-table
  35. make-symbol-table
  36. table-ref
  37. table-set!
  38. table-walk
  39. make-queue
  40. enqueue!
  41. dequeue!
  42. queue-empty?
  43. immutable?
  44. make-immutable!
  45. make-table-immutable!
  46. make-cell
  47. cell-ref
  48. cell-set!
  49. fluid-cell-ref
  50. fluid-cell-set!
  51. byte-ready?
  52. peek-byte
  53. read-byte
  54. write-byte
  55. current-column
  56. current-line
  57. make-tracking-input-port
  58. make-tracking-output-port
  59. call-with-string-output-port
  60. current-noise-port
  61. write-one-line
  62. assertion-violation
  63. warning
  64. concatenate-symbol
  65. breakpoint
  66. atom?
  67. neq?
  68. n=
  69. memq?
  70. first
  71. any
  72. no-op
  73. null-list?
  74. any?
  75. every?
  76. filter-map
  77. partition-list
  78. reduce
  79. fold
  80. fold->3
  81. every
  82. last)
  83. #:re-export (define-enumeration
  84. enum
  85. name->enumerand
  86. enumerand->name
  87. bitwise-and
  88. bitwise-ior
  89. bitwise-xor
  90. bitwise-not
  91. receive))
  92. (define arithmetic-shift ash)
  93. (define ascii->char integer->char)
  94. (define char->ascii char->integer)
  95. (define ascii-limit 128)
  96. (define unspecific (if #f #f))
  97. (define (unspecific? x) (eq? x unspecific))
  98. (define make-code-vector make-bytevector)
  99. (define code-vector? bytevector?)
  100. (define code-vector-ref bytevector-u8-ref)
  101. (define code-vector-set! bytevector-u8-set!)
  102. (define code-vector-length bytevector-length)
  103. (define make-table make-hash-table)
  104. (define make-integer-table make-hash-table)
  105. (define make-symbol-table make-hash-table)
  106. (define table-ref hash-ref)
  107. (define table-set! hash-set!)
  108. (define table-walk hash-for-each)
  109. (define make-queue make-q)
  110. (define enqueue! enq!)
  111. (define dequeue! deq!)
  112. (define queue-empty? q-empty?)
  113. (define (immutable? x) #f)
  114. (define (make-immutable! x) x)
  115. (define (make-table-immutable! x) x)
  116. (define make-cell box)
  117. (define cell-ref unbox)
  118. (define cell-set! set-box!)
  119. (define (fluid-cell-ref x)
  120. (cell-ref (fluid-ref x)))
  121. (define (fluid-cell-set! x v)
  122. (cell-set! (fluid-ref x) v))
  123. (define byte-ready? char-ready?)
  124. (define peek-byte lookahead-u8)
  125. (define read-byte get-u8)
  126. (define write-byte put-u8)
  127. (define current-column port-column)
  128. (define current-line port-line)
  129. (define make-tracking-input-port identity)
  130. (define make-tracking-output-port identity)
  131. (define call-with-string-output-port call-with-output-string)
  132. (define current-noise-port current-error-port)
  133. (define (write-one-line port count proc)
  134. ;; FIXME port write-one-line from scheme/big/more-port.scm
  135. (proc port))
  136. (define (assertion-violation who message . irritants)
  137. (apply error message irritants))
  138. (define (warning who message . irritants)
  139. ;; FIXME review exception handling
  140. (apply error message irritants))
  141. (define (concatenate-symbol . stuff)
  142. (string->symbol
  143. (apply string-append
  144. (map (lambda (x)
  145. (cond ((string? x) x)
  146. ((symbol? x) (symbol->string x))
  147. ((number? x) (number->string x))
  148. (else
  149. (assertion-violation 'concatenate-symbol "cannot coerce to a string"
  150. x))))
  151. stuff))))
  152. (define (breakpoint format-string . args)
  153. (error (apply format (cons #f (cons format-string args)))))
  154. (define (atom? x)
  155. (not (pair? x)))
  156. (define (neq? a b)
  157. (not (eq? a b)))
  158. (define (n= x y)
  159. (not (= x y)))
  160. (define (memq? x l)
  161. (let loop ((l l))
  162. (cond ((null? l) #f)
  163. ((eq? x (car l)) #t)
  164. (else (loop (cdr l))))))
  165. (define (first pred list)
  166. (let loop ((list list))
  167. (cond ((null? list)
  168. #f)
  169. ((pred (car list))
  170. (car list))
  171. (else
  172. (loop (cdr list))))))
  173. (define any first) ;; ANY need not search in order, but it does anyway
  174. (define (no-op x) x)
  175. (define (null-list? x)
  176. (cond ((null? x) #t)
  177. ((pair? x) #f)
  178. (else
  179. (assertion-violation 'null-list? "non-list" x))))
  180. (define (any? proc list)
  181. (let loop ((list list))
  182. (cond ((null? list)
  183. #f)
  184. ((proc (car list))
  185. #t)
  186. (else
  187. (loop (cdr list))))))
  188. (define (every? pred list)
  189. (let loop ((list list))
  190. (cond ((null? list)
  191. #t)
  192. ((pred (car list))
  193. (loop (cdr list)))
  194. (else
  195. #f))))
  196. (define (filter-map f l)
  197. (let loop ((l l) (r '()))
  198. (cond ((null? l)
  199. (reverse r))
  200. ((f (car l))
  201. => (lambda (x)
  202. (loop (cdr l) (cons x r))))
  203. (else
  204. (loop (cdr l) r)))))
  205. (define (partition-list pred l)
  206. (let loop ((l l) (yes '()) (no '()))
  207. (cond ((null? l)
  208. (values (reverse yes) (reverse no)))
  209. ((pred (car l))
  210. (loop (cdr l) (cons (car l) yes) no))
  211. (else
  212. (loop (cdr l) yes (cons (car l) no))))))
  213. (define (reduce cons nil list)
  214. (if (null? list)
  215. nil
  216. (cons (car list) (reduce cons nil (cdr list)))))
  217. (define (fold folder list accumulator)
  218. (do ((list list (cdr list))
  219. (accum accumulator (folder (car list) accum)))
  220. ((null? list)
  221. accum)))
  222. (define (fold->3 folder list acc0 acc1 acc2)
  223. (let loop ((list list) (acc0 acc0) (acc1 acc1) (acc2 acc2))
  224. (if (null? list)
  225. (values acc0 acc1 acc2)
  226. (call-with-values
  227. (lambda ()
  228. (folder (car list) acc0 acc1 acc2))
  229. (lambda (acc0 acc1 acc2)
  230. (loop (cdr list) acc0 acc1 acc2))))))
  231. (define (every pred l)
  232. (if (null? l)
  233. #t
  234. (and (pred (car l)) (every pred (cdr l)))))
  235. (define (last x)
  236. (if (null? (cdr x))
  237. (car x)
  238. (last (cdr x))))