prescheme.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
  3. ; Stuff in Pre-Scheme that is not in Scheme.
  4. (define shift-left arithmetic-shift)
  5. (define (arithmetic-shift-right i n)
  6. (arithmetic-shift i (- 0 n)))
  7. ; Hack for the robots
  8. (define small* *) ; could do a range check
  9. (define int-mask (- (arithmetic-shift 1 pre-scheme-integer-size) 1))
  10. (define (logical-shift-right i n)
  11. (if (>= i 0)
  12. (arithmetic-shift i (- 0 n))
  13. (arithmetic-shift (bitwise-and i int-mask) (- 0 n))))
  14. (define (deallocate x) #f)
  15. (define the-null-pointer (list 'null-pointer))
  16. (define (null-pointer? x) (eq? x the-null-pointer))
  17. (define (null-pointer)
  18. the-null-pointer)
  19. (define-external-enumeration errors
  20. (no-errors
  21. (parse-error "EDOM")
  22. (file-not-found "ENOENT")
  23. (out-of-memory "ENOMEM")
  24. (invalid-port "EBADF")
  25. ))
  26. (define (error-string status)
  27. "an error")
  28. ; (symbol->string (enumerand->name status errors)))
  29. (define (open-input-file name)
  30. (let ((port (scheme:open-input-file name)))
  31. (values port
  32. (if port
  33. (enum errors no-errors)
  34. (enum errors file-not-found)))))
  35. (define (open-output-file name)
  36. (let ((port (scheme:open-output-file name)))
  37. (values port
  38. (if port
  39. (enum errors no-errors)
  40. (enum errors file-not-found)))))
  41. (define (close-input-port port)
  42. (scheme:close-input-port port)
  43. (enum errors no-errors))
  44. (define (close-output-port port)
  45. (scheme:close-output-port port)
  46. (enum errors no-errors))
  47. (define (read-char port)
  48. (let ((ch (scheme:read-char port)))
  49. (if (eof-object? ch)
  50. (values (ascii->char 0) #t (enum errors no-errors))
  51. (values ch #f (enum errors no-errors)))))
  52. (define (peek-char port)
  53. (let ((ch (scheme:peek-char port)))
  54. (if (eof-object? ch)
  55. (values (ascii->char 0) #t (enum errors no-errors))
  56. (values ch #f (enum errors no-errors)))))
  57. (define (read-integer port)
  58. (eat-whitespace! port)
  59. (let ((neg? (let ((x (scheme:peek-char port)))
  60. (if (eof-object? x)
  61. #f
  62. (case x
  63. ((#\+) (scheme:read-char port) #f)
  64. ((#\-) (scheme:read-char port) #t)
  65. (else #f))))))
  66. (let loop ((n 0) (any? #f))
  67. (let ((x (scheme:peek-char port)))
  68. (cond ((and (char? x)
  69. (char-numeric? x))
  70. (scheme:read-char port)
  71. (loop (+ (* n 10)
  72. (- (char->integer x)
  73. (char->integer #\0)))
  74. #t))
  75. (any?
  76. (values (if neg? (- n) n) #f (enum errors no-errors)))
  77. ((eof-object? x)
  78. (values 0 #t (enum errors no-errors)))
  79. (else
  80. (values 0 #f (enum errors parse-error))))))))
  81. (define (eat-whitespace! port)
  82. (cond ((char-whitespace? (scheme:peek-char port))
  83. (scheme:read-char port)
  84. (eat-whitespace! port))))
  85. (define (write-x string port)
  86. (display string port)
  87. (enum errors no-errors))
  88. (define write-char write-x)
  89. (define write-string write-x)
  90. (define write-integer write-x)
  91. (define (force-output port)
  92. (enum errors no-errors))
  93. (define (newline port)
  94. (write-char #\newline port)
  95. (enum errors no-errors))
  96. (define-syntax goto
  97. (lambda (exp rename compare)
  98. (cdr exp)))
  99. ; (external <string> <type> . <maybe scheme value>)
  100. (define-syntax external
  101. (lambda (exp rename compare)
  102. (if (null? (cdddr exp))
  103. exp
  104. (cadddr exp))))
  105. (define current-error-port current-output-port)