env.texi 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. @node Standard Pre-Scheme environment
  2. @section Standard environment
  3. @stindex prescheme
  4. Pre-Scheme programs usually open the @code{prescheme} structure. There
  5. are several other structures built-in to Pre-Scheme as well, described
  6. in the next section. This section describes the @code{prescheme}
  7. structure.
  8. @menu
  9. * Scheme bindings in Pre-Scheme:: Bindings from R5RS
  10. * Tail call optimization in Pre-Scheme::
  11. * Pre-Scheme bitwise manipulation::
  12. * Compound Pre-Scheme data manipulation::
  13. * Pre-Scheme error handling::
  14. * Input & output in Pre-Scheme::
  15. * Pre-Scheme access to C functions and macros::
  16. @end menu
  17. @node Scheme bindings in Pre-Scheme
  18. @subsection Scheme bindings
  19. Bindings for all the names specified here from R5RS Scheme are
  20. available in Pre-Scheme. The remainder of the sections after this one
  21. detail Pre-Scheme specifics that are not a part of Scheme.
  22. @deffn syntax define name value
  23. @deffnx syntax define (name . argument-list) value
  24. @deffnx syntax if condition consequent [alternate]
  25. @deffnx syntax let ((name expression) @dots{}) body
  26. @deffnx syntax let* ((name expression) @dots{}) body
  27. @deffnx syntax and conjunct @dots{}
  28. @deffnx syntax or disjunct @dots{}
  29. @deffnx syntax cond cond-clause @dots{}
  30. @deffnx syntax do ((name init-exp [step-exp]) @dots{}) (test-exp [return-exp]) body
  31. These special forms & macros are all unchanged from their R5RS
  32. specifications.
  33. @end deffn
  34. @deffn syntax define-syntax name transformer-expression [aux-names]
  35. @deffnx syntax let-syntax ((name transformer-expression) @dots{}) body
  36. @deffnx syntax letrec-syntax ((name transformer-expression) @dots{}) body
  37. Pre-Scheme's macro facility is exactly the same as Scheme48's.
  38. @var{Transformer-expression} may be either a @code{syntax-rules} or an
  39. explicit renaming transformer, just as in Scheme48; in the latter case,
  40. it is evaluated either in a standard Scheme environment or however the
  41. @code{for-syntax} clause specified of the package in whose code the
  42. transformer appeared. For details on the extra @var{aux-names} operand
  43. to @code{define-syntax}, @pxref{Explicit renaming macros}.
  44. @end deffn
  45. @deffn procedure not boolean @returns{} boolean
  46. @deffnx procedure eq? value@suba{a} value@suba{b} @returns{} boolean
  47. @deffnx procedure char=? char@suba{a} char@suba{b} @returns{} boolean
  48. @deffnx procedure char<? char@suba{a} char@suba{b} @returns{} boolean
  49. @deffnx procedure values value @dots{} @returns{} values
  50. @deffnx procedure call-with-values producer consumer @returns{} values
  51. @deffnx procedure current-input-port @returns{} input-port
  52. @deffnx procedure current-output-port @returns{} output-port
  53. These procedures are all unchanged from their R5RS specifications.
  54. @end deffn
  55. @deffn procedure + addend @dots{} @returns{} integer
  56. @deffnx procedure - integer @returns{} integer
  57. @deffnx procedure - minuend subtrahend @returns{} integer
  58. @deffnx procedure * multiplicand @dots{} @returns{} integer
  59. @deffnx procedure = integer@suba{a} integer@suba{b} @returns{} boolean
  60. @deffnx procedure < integer@suba{a} integer@suba{b} @returns{} boolean
  61. @deffnx procedure > integer@suba{a} integer@suba{b} @returns{} boolean
  62. @deffnx procedure <= integer@suba{a} integer@suba{b} @returns{} boolean
  63. @deffnx procedure >= integer@suba{a} integer@suba{b} @returns{} boolean
  64. @deffnx procedure min integer@sub{1} integer@sub{2} @dots{} @returns{} integer
  65. @deffnx procedure max integer@sub{1} integer@sub{2} @dots{} @returns{} integer
  66. @deffnx procedure abs integer @returns{} integer
  67. @deffnx procedure quotient divisor dividend @returns{} integer
  68. @deffnx procedure remainder divisor dividend @returns{} integer
  69. @deffnx procedure expt base exponent @returns{} integer
  70. These numerical operations are all unchanged from their R5RS
  71. counterparts, except that they are applicable only to fixnums, not to
  72. flonums, and they always return fixnums.
  73. @end deffn
  74. @node Tail call optimization in Pre-Scheme
  75. @subsection Tail call optimization
  76. @cindex Pre-Scheme tail call optimization
  77. @cindex tail call optimization in Pre-Scheme
  78. @cindex tail recursion in Pre-Scheme
  79. @deffn syntax goto procedure argument @dots{}
  80. The Pre-Scheme compiler can be forced to optimize tail calls, even
  81. those it would not have otherwise optimized, by use of the @code{goto}
  82. special form, rather than simple procedure calls. In every respect
  83. other than tail call optimization, this is equivalent to calling
  84. @var{procedure} with the given arguments. Note, however, that uses of
  85. @code{goto} may cause code to blow up if the Pre-Scheme compiler had
  86. reason not to optimize the tail call were it not for the @code{goto}:
  87. it may need to merge the tail-called procedure into the caller's code.
  88. @end deffn
  89. @node Pre-Scheme bitwise manipulation
  90. @subsection Bitwise manipulation
  91. Pre-Scheme provides basic bitwise manipulation operators.
  92. @deffn procedure bitwise-and integer@suba{a} integer@suba{b} @returns{} integer
  93. @deffnx procedure bitwise-ior integer@suba{a} integer@suba{b} @returns{} integer
  94. @deffnx procedure bitwise-xor integer@suba{a} integer@suba{b} @returns{} integer
  95. @deffnx procedure bitwise-not integer @returns{} integer
  96. Bitwise boolean logical operations.
  97. @end deffn
  98. @deffn procedure shift-left integer count @returns{} integer
  99. @deffnx procedure arithmetic-shift-right integer count @returns{} integer
  100. @deffnx procedure logical-shift-right integer count @returns{} integer
  101. Three ways to shift bit strings: @code{shift-left} shifts @var{integer}
  102. left by @var{count}, @code{arithmetic-shift-right} shifts @var{integer}
  103. right by @var{count} arithmetically, and @code{logical-shift-right}
  104. shifts @var{integer} right by @var{count} logically.
  105. @end deffn
  106. @node Compound Pre-Scheme data manipulation
  107. @subsection Compound data manipulation
  108. @cindex vectors in Pre-Scheme
  109. @cindex Pre-Scheme vectors
  110. @cindex strings in Pre-Scheme
  111. @cindex Pre-Scheme strings
  112. @cindex Pre-Scheme memory management
  113. @cindex memory management in Pre-Scheme
  114. Pre-Scheme has somewhat lower-level vector & string facilities than
  115. Scheme, with more orientation towards static typing. It also provides
  116. a statically typed record facility, which translates to C structs,
  117. though not described here, as it is not in the @code{prescheme}
  118. structure; @pxref{Pre-Scheme record types}.
  119. @deffn procedure make-vector length init @returns{} vector
  120. @deffnx procedure vector-length vector @returns{} integer
  121. @deffnx procedure vector-ref vector index @returns{} value
  122. @deffnx procedure vector-set! vector index value @returns{} unit
  123. Vectors in Pre-Scheme are almost the same as vectors in regular Scheme,
  124. but with a few differences. @code{Make-vector} initializes what it
  125. returns with null pointers (see below); it uses the @emph{required}
  126. (unlike Scheme) @var{init} argument only to determine the type of the
  127. vector: vectors are statically typed; they can contain only values that
  128. have the same static type as @var{init}. @code{Vector-length} is
  129. available only at the top level, where calls to it can be evaluated at
  130. compile-time; vectors do not at run-time store their lengths. Vectors
  131. must also be explicitly deallocated.
  132. @strong{Warning:} As in C, there is @emph{no} vector bounds checking at
  133. run-time.
  134. @end deffn
  135. @deffn procedure make-string length @returns{} string
  136. @deffnx procedure string-length string @returns{} integer
  137. @deffnx procedure string-ref string index @returns{} char
  138. @deffnx procedure string-set! string index char @returns{} unit
  139. Strings in Pre-Scheme are the nearly same as strings in R5RS Scheme.
  140. The only three differences here are that @code{make-string} accepts
  141. exactly one argument, strings must be explicitly deallocated, and
  142. strings are @code{nul}-terminated: @code{string-length} operates by
  143. scanning for the first ASCII @code{nul} character in a string.
  144. @strong{Warning:} As in C, there is @emph{no} string bounds checking at
  145. run-time.
  146. @end deffn
  147. @deffn procedure deallocate pointer @returns{} unit
  148. Deallocates the memory pointed to by @code{pointer}. This is necessary
  149. at the end of a string, vector, or record's life, as Pre-Scheme data
  150. are not automatically garbage-collected.
  151. @end deffn
  152. @deffn procedure null-pointer @returns{} null-pointer
  153. @deffnx procedure null-pointer? pointer @returns{} boolean
  154. @code{Null-pointer} returns the distinguished null pointer object. It
  155. corresponds with @code{0} in a pointer context or @code{NULL} in C.
  156. @code{Null-pointer?} returns true if @var{pointer} is a null pointer,
  157. or false if not.
  158. @end deffn
  159. @node Pre-Scheme error handling
  160. @subsection Error handling
  161. Pre-Scheme's method of error handling is similar to the most common one
  162. in C: error codes. There is an enumeration @code{errors} of some error
  163. codes commonly and portably encountered in Pre-Scheme.
  164. @defvr {enumeration} errors
  165. @lisp
  166. (define-enumeration errors
  167. (no-errors
  168. parse-error
  169. file-not-found
  170. out-of-memory
  171. invalid-port))@end lisp
  172. Each enumerand has the following meaning:
  173. @table @code
  174. @item (enum errors no-errors)
  175. Absence of error: success.
  176. @item (enum errors parse-error)
  177. Any kind of parsing error. The Scheme48 VM uses this when someone
  178. attempts to resume a malformed suspended heap image.
  179. @item (enum errors file-not-found)
  180. Used when an operation that operates on a file given a string filename
  181. found that the file for that filename was absent.
  182. @item (enum errors out-of-memory)
  183. When there is no more memory to allocate.
  184. @item (enum errors invalid-port)
  185. Unused.
  186. @end table
  187. @end defvr
  188. @deffn procedure error-string error-status @returns{} string
  189. Returns a string describing the meaning of the @code{errors} enumerand
  190. @var{error-status}.
  191. @end deffn
  192. @deffn procedure error message irritant @dots{}
  193. Signals a fatal error with the given message & related irritants and
  194. halts the program. On Unix, the program's exit code is -1.
  195. @end deffn
  196. @node Input & output in Pre-Scheme
  197. @subsection Input & output
  198. Pre-Scheme's I/O facilities are somewhat different from Scheme's, given
  199. the low level and the static type strictness. There is no exception
  200. mechanism in Pre-Scheme; everything is maintained by returning a status
  201. token, as in C. Pre-Scheme's built-in I/O facilities are buffered.
  202. @footnote{Scheme48's VM does not use Pre-Scheme's built-in I/O
  203. facilities to implement @embedref{Channels, channels} --- it builds its
  204. own lower-level facilities that are still OS-independent, but, because
  205. they're written individually for different OSs, they integrate better
  206. as low-level I/O channels with the OS. On Unix, the Scheme48 VM uses
  207. file descriptors; Pre-Scheme's built-in I/O uses @code{stdio}.
  208. Scheme48's VM uses Pre-Scheme's built-in I/O only to read heap images.}
  209. (@pxref{Low-level Pre-Scheme memory manipulation}, for two other I/O
  210. primitives, @code{read-block} & @code{write-block}, for reading &
  211. writing blocks of direct memory.)
  212. @deffn procedure open-input-file filename @returns{} [port status]
  213. @deffnx procedure open-output-file filename @returns{} [port status]
  214. @deffnx procedure close-input-port input-port @returns{} status
  215. @deffnx procedure close-output-port output-port @returns{} status
  216. @code{Open-input-file} & @code{open-output-file} open ports for the
  217. given filenames. They each return two values: the newly open port and
  218. an @code{errors} enumerand status. Users of these procedures should
  219. always check the error status before proceeding to operate with the
  220. port. @code{Close-input-port} & @code{close-output-port} close their
  221. port arguments and return the @code{errors} enumerand status of the
  222. closing.
  223. @end deffn
  224. @deffn procedure read-char input-port @returns{} [char eof? status]
  225. @deffnx procedure peek-char input-port @returns{} [char eof? status]
  226. @deffnx procedure read-integer input-port @returns{} [integer eof? status]
  227. @code{Read-char} reads & consumes a single character from its
  228. @var{input-port} argument. @code{Peek-char} reads, but does not
  229. consume, a single character from @var{input-port}. @code{Read-integer}
  230. parses an integer literal, including sign. All of these also return
  231. two other values: whether or not the file is at the end and any
  232. @code{errors} enumerand status. If any error occurred, the first two
  233. values returned should be ignored. If @var{status} is @code{(enum
  234. errors no-errors)}, users of these three procedures should then check
  235. @var{eof?}; it is true if @var{input-port} was at the end of the file
  236. with nothing more left to read and false otherwise. Finally, if both
  237. @var{status} is @code{(enum errors no-errors)} and @var{eof?} is false,
  238. the first value returned may be safely used.
  239. @end deffn
  240. @deffn procedure write-char char output-port @returns{} status
  241. @deffnx procedure newline output-port @returns{} status
  242. @deffnx procedure write-string string output-port @returns{} status
  243. @deffnx procedure write-integer integer output-port @returns{} status
  244. These all write particular elements to their @var{output-port}
  245. arguments. @code{Write-char} writes individual characters.
  246. @code{Newline} writes newlines (line-feed, or ASCII codepoint 10, on
  247. Unix). @code{Write-string} writes the contents of @var{string}.
  248. @code{Write-integer} writes an ASCII representation of @var{integer} to
  249. port, suitable to be read by @code{read-integer}. These all return an
  250. @code{errors} enumerand status. If it is @code{no-errors}, the write
  251. succeeded.
  252. @end deffn
  253. @deffn procedure force-output output-port @returns{} status
  254. Forces all buffered output in @var{output-port}. @var{Status} tells
  255. whether or not the operation was successful.
  256. @end deffn
  257. @node Pre-Scheme access to C functions and macros
  258. @subsection Access to C functions and macros
  259. @deffn syntax external c-name ps-type @returns{} procedure
  260. Special form for accessing C functions & macros. Calls in Pre-Scheme to
  261. the resulting procedure are compiled to calls in C to the function or
  262. macro named by @var{c-name}, which should be a string. @var{PS-type} is
  263. the @embedref{Pre-Scheme type specifiers, Pre-Scheme type} that the
  264. procedure should have, which is necessary for type inference.
  265. @end deffn