core.sls 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!r6rs
  2. ;;; Copyright © 2016 Federico Beffa
  3. ;;;
  4. ;;; This program is free software; you can redistribute it and/or modify it
  5. ;;; under the terms of the GNU General Public License as published by
  6. ;;; the Free Software Foundation; either version 3 of the License, or (at
  7. ;;; your option) any later version.
  8. ;;;
  9. ;;; This program is distributed in the hope that it will be useful, but
  10. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;; GNU General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code
  17. ;; Use of gensym '#:' syntax.
  18. #!chezscheme
  19. (library (mit core)
  20. (export declare usual-integrations integrate-operator integrate
  21. error assert warn ignore-errors bkpt
  22. guarantee-symbol
  23. exact-rational? exact-positive-integer? exact-integer?
  24. exact-nonnegative-integer?
  25. guarantee-exact-integer
  26. guarantee-exact-positive-integer guarantee-exact-nonnegative-integer
  27. load*
  28. start-canonicalizing-symbols!
  29. start-preserving-case!
  30. default-object?
  31. pp print
  32. define-integrable
  33. true false unspecific
  34. runtime
  35. there-exists? for-all?
  36. symbol<? generate-uninterned-symbol string->uninterned-symbol
  37. undefined-value?
  38. string-find-next-char-in-set string-search-forward string-head
  39. string-tail
  40. write-line
  41. symbol-append
  42. symbol
  43. sort
  44. delq delv
  45. run-shell-command user-homedir-pathname system-tmpdir-pathname
  46. ->namestring
  47. graphics-type graphics-type-name)
  48. (import (rename (except (rnrs) error assert) (remq delq) (remv delv))
  49. (prefix (only (rnrs) assert) r6rs:)
  50. ;;(except (chezscheme) error)
  51. (only (chezscheme) load eval eval-when case-sensitive void pretty-print
  52. warning format void
  53. current-time time-second time-nanosecond
  54. gensym 1+ getenv system break)
  55. (prefix (only (chezscheme) error sort) chez:)
  56. (only (srfi :13) string-index string-contains)
  57. (except (mit arity) procedure-arity) ; use version in apply-hook
  58. (mit curry)
  59. (mit arithmetic))
  60. ;; Declare is used at the start of files. According to R6RS a library
  61. ;; must start with definitions. (define (declare args) (if #f #f))
  62. (define-syntax declare
  63. (syntax-rules ()
  64. ((_ args ...) (define #:declare args ...))))
  65. (define (usual-integrations . args) #f)
  66. (define-syntax integrate-operator
  67. (syntax-rules ()
  68. ((_ args ...) unspecific)))
  69. (define-syntax integrate
  70. (syntax-rules ()
  71. ((_ args ...) unspecific)))
  72. (define (load* f env)
  73. (load (string-append f ".scm")
  74. (lambda (x)
  75. (eval x env))))
  76. (eval-when (compile eval load)
  77. (define (start-canonicalizing-symbols!)
  78. (case-sensitive #f))
  79. (define (start-preserving-case!)
  80. (case-sensitive #t)))
  81. (define (default-object? x)
  82. (eq? (void) x))
  83. (define-syntax define-integrable
  84. (syntax-rules ()
  85. ((_ form body ...) (define form body ...))))
  86. (define* (pp object #:optional (port (current-output-port)) (display? #f))
  87. (pretty-print object port))
  88. (define print pp)
  89. (define* (error msg #:optional (irritant 'not-specified) . rest)
  90. (apply chez:error 'not-specified msg irritant rest))
  91. (define* (warn msg #:optional (irritant 'not-specified) . rest)
  92. (apply warning 'warn msg irritant rest))
  93. (define-syntax assert
  94. (syntax-rules ()
  95. ((_ form rest ...) (r6rs:assert form))))
  96. (define* (ignore-errors thunk #:optional map-error)
  97. (call-with-current-continuation
  98. (lambda (k)
  99. (with-exception-handler
  100. (lambda (x)
  101. (cond ((or (default-object? map-error)
  102. (not map-error))
  103. (if (error? x) (k x) x))
  104. ((and (procedure? map-error)
  105. (procedure-arity-valid? map-error 1))
  106. (lambda (condition)
  107. (k (map-error condition))))
  108. (else
  109. (error "wrong-type-argument" map-error
  110. "map-error procedure"
  111. 'IGNORE-ERRORS))))
  112. thunk))))
  113. (define (bkpt datum . arguments)
  114. (break 'bkpt datum arguments))
  115. (define (guarantee-symbol x msg)
  116. (if (not (symbol? x))
  117. (error msg x "not a symbol")))
  118. (define (exact-rational? x)
  119. (and (rational? x)
  120. (exact? x)))
  121. (define (exact-positive-integer? x)
  122. (and (integer? x)
  123. (positive? x)
  124. (exact? x)))
  125. (define (exact-integer? x)
  126. (and (integer? x)
  127. (exact? x)))
  128. (define (exact-nonnegative-integer? x)
  129. (and (integer? x)
  130. (not (negative? x))
  131. (exact? x)))
  132. (define (guarantee-exact-integer x msg)
  133. (if (not (exact-integer? x))
  134. (error msg x "not an exact integer")))
  135. (define (guarantee-exact-positive-integer x msg)
  136. (if (not (exact-positive-integer? x))
  137. (error msg x "not an exact positive integer")))
  138. (define (guarantee-exact-nonnegative-integer x msg)
  139. (unless (or (zero? x) (exact-positive-integer? x))
  140. (error msg x "not an exact positive integer")))
  141. (define true #t)
  142. (define false #f)
  143. (define unspecific (void))
  144. (define (runtime)
  145. (let ((t (current-time 'time-process)))
  146. (+ (time-second t)
  147. (/ (time-nanosecond t) 1e9))))
  148. (define (there-exists? items predicate)
  149. (let loop ((items* items))
  150. (if (pair? items*)
  151. (if (predicate (car items*))
  152. #t
  153. (loop (cdr items*)))
  154. (begin
  155. (if (not (null? items*))
  156. (error ":not-list items" 'THERE-EXISTS?))
  157. #f))))
  158. (define (for-all? items predicate)
  159. (let loop ((items* items))
  160. (if (pair? items*)
  161. (if (predicate (car items*))
  162. (loop (cdr items*))
  163. #f)
  164. (begin
  165. (if (not (null? items*))
  166. (error ":not-list items" 'FOR-ALL?))
  167. #t))))
  168. (define (symbol<? x y)
  169. (string<? (symbol->string x)
  170. (symbol->string x)))
  171. (define* (generate-uninterned-symbol #:optional s)
  172. (if (default-object? s)
  173. (gensym)
  174. (if (symbol? s)
  175. (gensym (symbol->string s))
  176. (gensym s))))
  177. (define string->uninterned-symbol gensym)
  178. (define (undefined-value? object)
  179. (or (eq? object unspecific)
  180. ;;(and (variable? object) (not (variable-bound? object)))
  181. ;;(eq? object (object-new-type (ucode-type constant) 2))
  182. ))
  183. (define string-find-next-char-in-set string-index)
  184. (define (string-search-forward pattern string)
  185. (string-contains string pattern))
  186. (define (string-head string end)
  187. (substring string 0 end))
  188. (define (string-tail string start)
  189. (substring string start (string-length string)))
  190. (define write-line
  191. (case-lambda
  192. ((obj) (write-line obj (current-output-port)))
  193. ((obj port) (display obj port) (newline port))))
  194. (define symbol-append
  195. (lambda args
  196. (string->symbol (apply string-append (map symbol->string args)))))
  197. (define (symbol . args)
  198. (define (ensure-symbol s)
  199. (cond
  200. ((symbol? s) s)
  201. ((number? s) (string->symbol (number->string s)))
  202. ((string? s) (string->symbol s))
  203. (else (error "wrong type" s 'symbol))))
  204. (apply symbol-append (map ensure-symbol args)))
  205. (define (sort elements proc)
  206. (chez:sort proc elements))
  207. (define (run-shell-command cmd . rest)
  208. (system cmd))
  209. (define (user-homedir-pathname)
  210. (string-append (getenv "HOME") "/"))
  211. (define (system-tmpdir-pathname) "/tmp/")
  212. (define (->namestring pathname)
  213. pathname)
  214. ;;; temporary graphics fix
  215. (define (graphics-type arg) #f)
  216. ;; This function is used in a context where symbols are canonicalized.
  217. ;; Therefore we emit a lowercase symbol. (This module is loaded
  218. ;; preserving case.)
  219. (define (graphics-type-name name) 'x)
  220. )