Syntax-and-conditional-compilation.xhtml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Syntax and conditional compilation</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="Macros.xhtml" title="Macros"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Syntax and conditional compilation" epub:type="subchapter" id="Syntax-and-conditional-compilation">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Syntax and conditional compilation</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <h3 id="idm139667879083936">Feature testing</h3>
  23. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667879083120" class="indexterm"/> <code class="function">cond-expand</code> <em class="replaceable"><code><a class="link" href="Syntax-and-conditional-compilation.xhtml#meta-cond-expand-clause"><em class="replaceable"><code>cond-expand-clause</code></em></a></code></em><em class="replaceable"><code><sup>*</sup></code></em> [<em class="replaceable"><code><span class="bold"><strong>(else</strong></span></code></em> <em class="replaceable"><code>command-or-definition*</code></em><em class="replaceable"><code><span class="bold"><strong>)</strong></span></code></em>]</p>
  24. <div class="blockquote">
  25. <blockquote class="blockquote">
  26. <div class="literallayout">
  27. <p><a id="idm139667879077232" class="indexterm"/><span id="meta-cond-expand-clause"/><em class="replaceable"><code>cond-expand-clause</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(</strong></span></code><em class="replaceable"><code>feature-requirement</code></em> <em class="replaceable"><code>command-or-definition</code></em>*<code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  28. <a id="idm139667879072976" class="indexterm"/><a id="idm139667879072192" class="indexterm"/><span id="meta-feature-requirement"/><em class="replaceable"><code>feature-requirement</code></em> <code class="literal">::=</code> <a class="link" href="Syntax-and-conditional-compilation.xhtml#meta-feature-identifier"><em class="replaceable"><code>feature-identifier</code></em></a><br/>
  29.   | <code class="literal"><span class="bold"><strong>(and</strong></span></code> <a class="link" href="Syntax-and-conditional-compilation.xhtml#meta-feature-requirement"><em class="replaceable"><code>feature-requirement</code></em></a><sup>*</sup><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  30.   | <code class="literal"><span class="bold"><strong>(or</strong></span></code> <a class="link" href="Syntax-and-conditional-compilation.xhtml#meta-feature-requirement"><em class="replaceable"><code>feature-requirement</code></em></a><sup>*</sup><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  31.   | <code class="literal"><span class="bold"><strong>(not</strong></span></code> <a class="link" href="Syntax-and-conditional-compilation.xhtml#meta-feature-requirement"><em class="replaceable"><code>feature-requirement</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  32.   | <code class="literal"><span class="bold"><strong>(library</strong></span></code> <a class="link" href="Module-classes.xhtml#meta-library-name"><em class="replaceable"><code>library-name</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  33. <a id="idm139667879059520" class="indexterm"/><span id="meta-feature-identifier"/><em class="replaceable"><code>feature-identifier</code></em> <code class="literal">::=</code> a symbol which is the name or alias of a SRFI<br/>
  34. </p>
  35. </div>
  36. <p>The <code class="literal">cond-expand</code> form tests for the existence of features at
  37. macro-expansion time. It either expands into the body of one of its
  38. clauses or signals an error during syntactic
  39. processing. <code class="literal">cond-expand</code> expands into the body of the first clause
  40. whose feature requirement is currently satisfied; the <code class="literal">else</code>
  41. clause, if present, is selected if none of the previous clauses is
  42. selected.
  43. </p>
  44. <p>The implementation has a set of
  45. feature identifiers which are “present”, as well as a set
  46. of libraries which can be imported.
  47. The value of a
  48. <em class="replaceable"><code>feature-requirement</code></em> is determined by replacing each
  49. <em class="replaceable"><code>feature-identifier</code></em> by <code class="literal">#t</code> if it is present
  50. (and <code class="literal">#f</code> otherwise);
  51. replacing <code class="literal">(library <em class="replaceable"><code>library-name</code></em>)</code>
  52. by <code class="literal">#t</code> if <em class="replaceable"><code>library-name</code></em> is importable (and <code class="literal">#f</code> otherwise);
  53. and then evaluating the resulting expression as a Scheme boolean expression
  54. under the normal interpretation of <code class="literal">and</code>, <code class="literal">or</code>, and <code class="literal">not</code>.
  55. </p>
  56. <p>Examples:
  57. </p>
  58. <pre class="screen">(cond-expand
  59. ((and srfi-1 srfi-10)
  60. (write 1))
  61. ((or srfi-1 srfi-10)
  62. (write 2))
  63. (else))
  64. </pre>
  65. <pre class="screen">(cond-expand
  66. (command-line
  67. (define (program-name) (car (argv)))))
  68. </pre>
  69. <p>The second example assumes that <code class="literal">command-line</code> is an alias for some
  70. feature which gives access to command line arguments. Note that an error
  71. will be signaled at macro-expansion time if this feature is not present.
  72. </p>
  73. <p>You can use <code class="literal">java-6</code>, <code class="literal">java-7</code>, <code class="literal">java-8</code>,
  74. or <code class="literal">java-9</code> to check if the underlying Java
  75. is a specific version or newer.
  76. For example the name <code class="literal">java-7</code> matches for
  77. either Java 7, Java 8, or newer, as
  78. reported by <code class="literal">System</code> property <code class="literal">"java.version"</code>.
  79. </p>
  80. <p>You can use <code class="literal">class-exists:<em class="replaceable"><code>ClassName</code></em></code> to check
  81. if <code class="literal"><em class="replaceable"><code>ClassName</code></em></code> exists at compile-time.
  82. The identifier <code class="literal">class-exists:org.example.MyClass</code>
  83. is roughly equivalent to the test <code class="literal">(library (org example MyClass))</code>.
  84. (The latter has some special handling for <code class="literal">(srfi ...)</code> as well
  85. as builtin Kawa classes.)
  86. </p>
  87. </blockquote>
  88. </div>
  89. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667879040080" class="indexterm"/> <code class="function">features</code></p>
  90. <div class="blockquote">
  91. <blockquote class="blockquote">
  92. <p>Returns a list of feature identifiers which <code class="literal">cond-expand</code>
  93. treats as true.
  94. This not a complete list - for example <code class="literal">class-exists:<em class="replaceable"><code>ClassName</code></em></code>
  95. feature identifiers are not included.
  96. It is an error to modify this list.
  97. Here is an example of what <code class="literal">features</code> might return:
  98. </p>
  99. <pre class="screen">(features) ⇒
  100. (complex exact-complex full-unicode java-7 java-6 kawa
  101. ratios srfi-0 srfi-4 srfi-6 srfi-8 srfi-9 srfi-11
  102. srfi-16 srfi-17 srfi-23 srfi-25 srfi-26 srfi-28 srfi-30
  103. srfi-39 string-normalize-unicode threads)
  104. </pre>
  105. </blockquote>
  106. </div>
  107. <h3 id="idm139667879034704">File inclusion</h3>
  108. <span id="include"/>
  109. <span id="include-relative"/>
  110. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667879033088" class="indexterm"/> <code class="function">include</code> <em class="replaceable"><code>path</code></em><em class="replaceable"><code><sup>+</sup></code></em></p>
  111. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667879029872" class="indexterm"/> <code class="function">include-relative</code> <em class="replaceable"><code>path</code></em><em class="replaceable"><code><sup>+</sup></code></em></p>
  112. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667879026656" class="indexterm"/> <code class="function">include-ci</code> <em class="replaceable"><code>path</code></em><em class="replaceable"><code><sup>+</sup></code></em></p>
  113. <div class="blockquote">
  114. <blockquote class="blockquote">
  115. <p>These take one or more path names expressed as string literals,
  116. find corresponding files, read the contents of the files in the specified order
  117. as if by repeated applications of <code class="literal">read</code>, and effectively
  118. replace the <code class="literal">include</code> with a <code class="literal">begin</code> form
  119. containing what was read from the files.
  120. </p>
  121. <p>You can control the search path used for <code class="literal">include</code>
  122. by setting the <code class="literal">kawa.include.path</code> property. For example:
  123. </p>
  124. <pre class="screen">$ kawa -Dkawa.include.path="|:/opt/kawa-includes"
  125. </pre>
  126. <p>The special <code class="literal">"|"</code> path element means to search
  127. relative to the directory containing the including source file.
  128. The default search path is <code class="literal">"|:."</code> which means to first
  129. search the directory containing the including source file,
  130. and then search the directory specified by <code class="literal">(current-path)</code>.
  131. </p>
  132. <p>The search path for <code class="literal">include-relative</code> prepends <code class="literal">"|"</code>
  133. before the search path used by <code class="literal">include</code>, so it always
  134. searches first the directory containing the including source file.
  135. Note that if the default search path is used then <code class="literal">include</code>
  136. and <code class="literal">include-relative</code> are equivalent; there is only a difference
  137. if the <code class="literal">kawa.include.path</code> property changes the default.
  138. </p>
  139. <p>Using <code class="literal">include-ci</code> is like <code class="literal">include</code>, except that it reads each
  140. file as if it began with the <code class="literal">#!fold-case</code> directive.
  141. </p>
  142. </blockquote>
  143. </div>
  144. </section>
  145. <footer>
  146. <div class="navfooter">
  147. <p>
  148. Up: <a accesskey="u" href="Syntax.xhtml">Syntax</a></p>
  149. <p>
  150. Previous: <a accesskey="p" href="Bodies.xhtml">Programs and Bodies</a></p>
  151. <p>
  152. Next: <a accesskey="n" href="Macros.xhtml">Macros</a></p>
  153. </div>
  154. </footer>
  155. </body>
  156. </html>