c-io.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, Marcus Crestani
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/prescheme/primop/c-io.scm
  8. (define-module (ps-compiler prescheme primop c-io)
  9. #:use-module (ice-9 format)
  10. #:use-module (ps-compiler node node)
  11. #:use-module (ps-compiler node variable)
  12. #:use-module (ps-compiler prescheme c-call)
  13. #:use-module (ps-compiler prescheme primop c-primop)
  14. #:use-module (ps-compiler util util))
  15. (define (write-c-io-call call port name . args)
  16. (format port name)
  17. (writec port #\()
  18. (for-each (lambda (arg)
  19. (cond ((string? arg)
  20. (format port arg))
  21. ((variable? arg)
  22. (c-variable arg port))
  23. (else
  24. (c-value (call-arg call arg) port))))
  25. args)
  26. (writec port #\)))
  27. ;; stdin, stdout, and stderr cannot be variables because they may be macros in C.
  28. (define-c-generator stdin #t
  29. (lambda (call port indent)
  30. (format port "stdin")))
  31. (define-c-generator stdout #t
  32. (lambda (call port indent)
  33. (format port "stdout")))
  34. (define-c-generator stderr #t
  35. (lambda (call port indent)
  36. (format port "stderr")))
  37. ;; char eof? status
  38. (define-c-generator read-char #f
  39. (lambda (call port indent)
  40. (indent-to port indent)
  41. (let ((vars (lambda-variables (call-arg call 0))))
  42. (write-c-io-call call port "PS_READ_CHAR" 1 ", "
  43. (car vars) ", " (cadr vars) ", " (caddr vars)))))
  44. (define-c-generator peek-char #f
  45. (lambda (call port indent)
  46. (indent-to port indent)
  47. (let ((vars (lambda-variables (call-arg call 0))))
  48. (write-c-io-call call port "PS_PEEK_CHAR" 1 ", "
  49. (car vars) ", " (cadr vars) ", " (caddr vars)))))
  50. (define-c-generator read-integer #f
  51. (lambda (call port indent)
  52. (indent-to port indent)
  53. (let ((vars (lambda-variables (call-arg call 0))))
  54. (write-c-io-call call port "PS_READ_INTEGER" 1 ", "
  55. (car vars) ", " (cadr vars) ", " (caddr vars)))))
  56. (define-c-generator write-char #f
  57. (lambda (call port indent)
  58. (indent-to port indent)
  59. (let ((vars (lambda-variables (call-arg call 0))))
  60. (if (used? (car vars))
  61. (write-c-io-call call port "PS_WRITE_CHAR" 1 ", " 2 ", " (car vars))
  62. (begin
  63. (display "{ long ignoreXX;" port)
  64. (indent-to port indent)
  65. (write-c-io-call call port "PS_WRITE_CHAR" 1 ", " 2 ", ignoreXX")
  66. (display " }" port))))))
  67. (define-c-generator write-string #t
  68. (lambda (call port indent)
  69. (write-c-io-call call port "ps_write_string" 0 ", " 1)))
  70. (define-c-generator write-integer #t
  71. (lambda (call port indent)
  72. (write-c-io-call call port "ps_write_integer" 0 ", " 1)))
  73. (define-c-generator force-output #t
  74. (lambda (call port indent)
  75. (write-c-io-call call port "ps_flush" 0)))
  76. (define-c-generator read-block #f
  77. (lambda (call port indent)
  78. (let ((vars (lambda-variables (call-arg call 0))))
  79. (c-assign-to-variable (car vars) port indent)
  80. (write-c-io-call call port "ps_read_block" 1 ", ((char *) " 2 "), " 3
  81. ", &" (cadr vars) ", &" (caddr vars))
  82. (write-char #\; port))))
  83. (define-c-generator write-block #t
  84. (lambda (call port indent)
  85. (write-c-io-call call port "ps_write_block" 0 ", ((char *) " 1 ")"
  86. ", " 2)))
  87. ;; (read-block (lambda (okay? eof? got) ...) port buffer count)
  88. ;;
  89. ;;(define-c-generator read-block #f
  90. ;; (lambda (call port indent)
  91. ;; (let* ((cont (call-arg call 0))
  92. ;; (vars (lambda-variables cont)))
  93. ;; ;; got = ps_read(port, buffer, count, &okay?, &eof?);
  94. ;; (c-assign-to-variable (caddr vars) port indent)
  95. ;; (write-c-io-call call port
  96. ;; "ps_read" 1 ", (void *)" 2 ", " 3 ", &" (car vars)
  97. ;; ", &" (cadr vars))
  98. ;; (write-char #\; port))))
  99. ;;
  100. ;;; (write-block (lambda (okay? sent) ...) port buffer count)
  101. ;;
  102. ;;(define-c-generator write-block #f
  103. ;; (lambda (call port indent)
  104. ;; (let* ((cont (call-arg call 0))
  105. ;; (vars (lambda-variables cont)))
  106. ;; ;; sent = ps_write(port, buffer, count, &okay?);
  107. ;; (c-assign-to-variable (cadr vars) port indent)
  108. ;; (write-c-io-call call port
  109. ;; "ps_write" 1 ", (void *)" 2 ", " 3 ", &" (car vars))
  110. ;; (write-char #\; port))))
  111. (define-c-generator open-input-file #f
  112. (lambda (call port indent)
  113. (let ((vars (lambda-variables (call-arg call 0))))
  114. (c-assign-to-variable (car vars) port indent)
  115. (write-c-io-call call port "ps_open_input_file" 1 ", &" (cadr vars))
  116. (write-char #\; port))))
  117. (define-c-generator open-output-file #f
  118. (lambda (call port indent)
  119. (let ((vars (lambda-variables (call-arg call 0))))
  120. (c-assign-to-variable (car vars) port indent)
  121. (write-c-io-call call port "ps_open_output_file" 1 ", &" (cadr vars))
  122. (write-char #\; port))))
  123. (define-c-generator close-input-port #t
  124. (lambda (call port indent)
  125. (write-c-io-call call port "ps_close" 0)))
  126. (define-c-generator close-output-port #t
  127. (lambda (call port indent)
  128. (write-c-io-call call port "ps_close" 0)))
  129. (define-c-generator abort #t
  130. (lambda (call port indent)
  131. (format port "(exit -1)")))
  132. (define-c-generator error #f
  133. (lambda (call port indent)
  134. (indent-to port indent)
  135. (format port "ps_error(")
  136. (c-value (call-arg call 1) port)
  137. (format port ", ~D" (- (call-arg-count call) 2))
  138. (do ((i 2 (+ i 1)))
  139. ((= i (call-arg-count call)))
  140. (format port ", ")
  141. (c-value (call-arg call i) port))
  142. (format port ");")))
  143. (define-c-generator error-string #t
  144. (lambda (call port indent)
  145. (write-c-io-call call port "ps_error_string" 0)))
  146. ;; (c-e-v <proc> <nargs> <pointer-to-args>)
  147. (define-c-generator call-external-value #t
  148. (lambda (call port indent)
  149. (format port "((long(*)())")
  150. (c-value (call-arg call 0) port)
  151. (format port ")(")
  152. (c-value (call-arg call 1) port)
  153. (format port ", ")
  154. (c-value (call-arg call 2) port)
  155. (writec port #\))))
  156. (define-c-generator call-external-value-2 #t
  157. (lambda (call port indent)
  158. (format port "((long(*)())")
  159. (c-value (call-arg call 0) port)
  160. (format port ")(")
  161. (c-value (call-arg call 1) port)
  162. (format port ", ")
  163. (c-value (call-arg call 2) port)
  164. (writec port #\))))