prescheme.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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
  6. ;;;
  7. ;;; scheme48-1.9.2/scheme/prescheme/prescheme.scm
  8. ;;;
  9. ;;; Stuff in Pre-Scheme that is not in Scheme.
  10. ;;;
  11. (define-module (prescheme prescheme)
  12. #:use-module (prescheme scheme48)
  13. #:use-module (prescheme platform)
  14. #:use-module (prescheme ps-defenum)
  15. #:use-module ((rnrs io simple)
  16. #:select (open-input-file
  17. open-output-file
  18. close-input-port
  19. close-output-port
  20. read-char
  21. peek-char)
  22. #:prefix scheme:)
  23. #:export (shift-left arithmetic-shift-right logical-shift-right
  24. deallocate
  25. null-pointer
  26. null-pointer?
  27. errors
  28. error-string
  29. read-integer write-integer
  30. write-string
  31. goto
  32. external
  33. fl+ fl- fl* fl/ fl= fl< fl> fl<= fl>=
  34. un+ un- un* unquotient unremainder un= un< un> un<= un>=
  35. unsigned->integer integer->unsigned)
  36. #:replace (current-error-port
  37. open-input-file open-output-file
  38. close-output-port close-input-port
  39. read-char peek-char
  40. write-char newline
  41. force-output))
  42. (define shift-left arithmetic-shift)
  43. (define (arithmetic-shift-right i n)
  44. (arithmetic-shift i (- 0 n)))
  45. ;; Hack for the robots
  46. (define small* *) ;; could do a range check
  47. (define int-mask (- (arithmetic-shift 1 pre-scheme-integer-size) 1))
  48. (define (logical-shift-right i n)
  49. (if (>= i 0)
  50. (arithmetic-shift i (- 0 n))
  51. (arithmetic-shift (bitwise-and i int-mask) (- 0 n))))
  52. (define (deallocate x) #f)
  53. (define the-null-pointer (list 'null-pointer))
  54. (define (null-pointer? x) (eq? x the-null-pointer))
  55. (define (null-pointer)
  56. the-null-pointer)
  57. (define-external-enumeration errors
  58. (no-errors
  59. (parse-error "EDOM")
  60. (file-not-found "ENOENT")
  61. (out-of-memory "ENOMEM")
  62. (invalid-port "EBADF")
  63. ))
  64. (define (error-string status)
  65. "an error")
  66. ;; (symbol->string (enumerand->name status errors)))
  67. (define (open-input-file name)
  68. (let ((port (scheme:open-input-file name)))
  69. (values port
  70. (if port
  71. (enum errors no-errors)
  72. (enum errors file-not-found)))))
  73. (define (open-output-file name)
  74. (let ((port (scheme:open-output-file name)))
  75. (values port
  76. (if port
  77. (enum errors no-errors)
  78. (enum errors file-not-found)))))
  79. (define (close-input-port port)
  80. (scheme:close-input-port port)
  81. (enum errors no-errors))
  82. (define (close-output-port port)
  83. (scheme:close-output-port port)
  84. (enum errors no-errors))
  85. (define (read-char port)
  86. (let ((ch (scheme:read-char port)))
  87. (if (eof-object? ch)
  88. (values (ascii->char 0) #t (enum errors no-errors))
  89. (values ch #f (enum errors no-errors)))))
  90. (define (peek-char port)
  91. (let ((ch (scheme:peek-char port)))
  92. (if (eof-object? ch)
  93. (values (ascii->char 0) #t (enum errors no-errors))
  94. (values ch #f (enum errors no-errors)))))
  95. (define (read-integer port)
  96. (eat-whitespace! port)
  97. (let ((neg? (let ((x (scheme:peek-char port)))
  98. (if (eof-object? x)
  99. #f
  100. (case x
  101. ((#\+) (scheme:read-char port) #f)
  102. ((#\-) (scheme:read-char port) #t)
  103. (else #f))))))
  104. (let loop ((n 0) (any? #f))
  105. (let ((x (scheme:peek-char port)))
  106. (cond ((and (char? x)
  107. (char-numeric? x))
  108. (scheme:read-char port)
  109. (loop (+ (* n 10)
  110. (- (char->integer x)
  111. (char->integer #\0)))
  112. #t))
  113. (any?
  114. (values (if neg? (- n) n) #f (enum errors no-errors)))
  115. ((eof-object? x)
  116. (values 0 #t (enum errors no-errors)))
  117. (else
  118. (values 0 #f (enum errors parse-error))))))))
  119. (define (eat-whitespace! port)
  120. (cond ((char-whitespace? (scheme:peek-char port))
  121. (scheme:read-char port)
  122. (eat-whitespace! port))))
  123. (define (write-x string port)
  124. (display string port)
  125. (enum errors no-errors))
  126. (define write-char write-x)
  127. (define write-string write-x)
  128. (define write-integer write-x)
  129. (define (force-output port)
  130. (enum errors no-errors))
  131. (define (newline port)
  132. (write-char #\newline port)
  133. (enum errors no-errors))
  134. (define-syntax goto
  135. (syntax-rules ()
  136. ((_ func args ...)
  137. (func args ...))))
  138. ;; (external <string> <type> . <maybe scheme value>)
  139. (define-syntax external
  140. (syntax-rules ()
  141. ((_ c-name ps-type)
  142. (error "not implemented:" '(_ c-name ps-type)))
  143. ((_ c-name ps-type scm-value)
  144. scm-value)))
  145. (define current-error-port current-output-port)
  146. ;; ps-flonums
  147. (define fl+ +) (define fl- -) (define fl* *) (define fl/ /)
  148. (define fl= =)
  149. (define fl< <) (define fl> >)
  150. (define fl<= <=) (define fl>= >=)
  151. ;; ps-unsigned-integers
  152. (define un+ +) (define un- -) (define un* *)
  153. (define unquotient quotient)
  154. (define unremainder remainder)
  155. (define un= =)
  156. (define un< <) (define un> >)
  157. (define un<= <=) (define un>= >=)
  158. (define (unsigned->integer x) x)
  159. (define (integer->unsigned x) x)