Primitive-expression-syntax.xhtml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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>Primitive expression syntax</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="Hash-prefixed-forms.xhtml" title="Hash-prefixed forms"/>
  10. <link rel="next" href="Colon-notation.xhtml" title="Property access using colon notation"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Primitive expression syntax" epub:type="subchapter" id="Primitive-expression-syntax">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Primitive expression syntax</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <div class="literallayout">
  23. <p><a id="idm139667879292592" class="indexterm"/><span id="meta-expression"/><em class="replaceable"><code>expression</code></em> <code class="literal">::=</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-literal-expression"><em class="replaceable"><code>literal-expression</code></em></a> | <a class="link" href="Primitive-expression-syntax.xhtml#meta-variable-reference"><em class="replaceable"><code>variable-reference</code></em></a><br/>
  24.   | <a class="link" href="Primitive-expression-syntax.xhtml#meta-procedure-call"><em class="replaceable"><code>procedure-call</code></em></a> | TODO<br/>
  25. </p>
  26. </div>
  27. <span id="literal-expression"/>
  28. <section class="sect2" title="Literal expressions" epub:type="division" id="idm139667879287568">
  29. <div class="titlepage">
  30. <div>
  31. <div>
  32. <h3 class="title">Literal expressions</h3>
  33. </div>
  34. </div>
  35. </div>
  36. <div class="literallayout">
  37. <p><a id="idm139667879286496" class="indexterm"/><span id="meta-literal-expression"/><em class="replaceable"><code>literal-expression</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(quote</strong></span></code> <a class="link" href="Datum-syntax.xhtml#meta-datum"><em class="replaceable"><code>datum</code></em></a><code class="literal"><span class="bold"><strong>)</strong></span></code><br/>
  38.   | <code class="literal"><span class="bold"><strong>’</strong></span></code> <a class="link" href="Datum-syntax.xhtml#meta-datum"><em class="replaceable"><code>datum</code></em></a><br/>
  39.   | <em class="replaceable"><code>constant</code></em> <br/>
  40. <a id="idm139667879280112" class="indexterm"/><span id="meta-constant"/><em class="replaceable"><code>constant</code></em> <code class="literal">::=</code> <em class="replaceable"><code>number</code></em> | <em class="replaceable"><code>boolean</code></em> | <em class="replaceable"><code>character</code></em> | <em class="replaceable"><code>string</code></em><br/>
  41. </p>
  42. </div>
  43. <p><code class="literal">(quote <em class="replaceable"><code>datum</code></em>)</code> evaluates to <em class="replaceable"><code>datum</code></em>,
  44. which may be any external representation of a Scheme object.
  45. This notation is used to include literal constants in Scheme code.
  46. </p>
  47. <pre class="screen">(quote a) ⇒ a
  48. (quote #(a b c)) ⇒ #(a b c)
  49. (quote (+ 1 2)) ⇒ (+ 1 2)
  50. </pre>
  51. <p><code class="literal">(quote <em class="replaceable"><code>datum</code></em>)</code> may be abbreviated as <code class="literal">'<em class="replaceable"><code>datum</code></em></code>.
  52. The two notations are equivalent in all respects.
  53. </p>
  54. <pre class="screen">’a ⇒ a
  55. ’#(a b c) ⇒ #(a b c)
  56. ’() ⇒ ()
  57. ’(+ 1 2) ⇒ (+ 1 2)
  58. ’(quote a) ⇒ (quote a)
  59. ’’a ⇒ (quote a)
  60. </pre>
  61. <p>Numerical constants, string constants, character constants,
  62. bytevector constants, and boolean constants evaluate to
  63. themselves; they need not be quoted.
  64. </p>
  65. <pre class="screen">145932 ⇒ 145932
  66. #t ⇒ #t
  67. "abc" ⇒ "abc"
  68. </pre>
  69. <p>Note that <a class="link" href="Keywords.xhtml" title="Keywords">keywords</a> need to be quoted,
  70. unlike some other Lisp/Scheme dialect, including Common Lisp,
  71. and earlier versions of Kawa. (Kawa currently evaluates a non-quoted
  72. keyword as itself, but that will change.)
  73. </p>
  74. <span id="variable-reference"/>
  75. </section>
  76. <section class="sect2" title="Variable references" epub:type="division" id="idm139667879268592">
  77. <div class="titlepage">
  78. <div>
  79. <div>
  80. <h3 class="title">Variable references</h3>
  81. </div>
  82. </div>
  83. </div>
  84. <div class="literallayout">
  85. <p><a id="idm139667879267520" class="indexterm"/><span id="meta-variable-reference"/><em class="replaceable"><code>variable-reference</code></em> <code class="literal">::=</code> <a class="link" href="Lexical-syntax.xhtml#meta-identifier"><em class="replaceable"><code>identifier</code></em></a><br/>
  86. </p>
  87. </div>
  88. <p>An expression consisting of a variable is a variable reference if it is
  89. not a macro use (see below). The value of the variable reference is the
  90. value stored in the location to which the variable is bound. It is a
  91. syntax violation to reference an unbound variable.
  92. </p>
  93. <p>The following example assumes the base library has been
  94. imported:
  95. </p>
  96. <pre class="screen">(define x 28)
  97. x ⇒ 28
  98. </pre>
  99. </section>
  100. <section class="sect2" title="Procedure calls" epub:type="division" id="idm139667879263008">
  101. <div class="titlepage">
  102. <div>
  103. <div>
  104. <h3 class="title">Procedure calls</h3>
  105. </div>
  106. </div>
  107. </div>
  108. <div class="literallayout">
  109. <p><a id="idm139667879261936" class="indexterm"/><span id="meta-procedure-call"/><em class="replaceable"><code>procedure-call</code></em> <code class="literal">::=</code> <code class="literal"><span class="bold"><strong>(</strong></span></code><a class="link" href="Primitive-expression-syntax.xhtml#meta-operator"><em class="replaceable"><code>operator</code></em></a> <a class="link" href="Primitive-expression-syntax.xhtml#meta-operand"><em class="replaceable"><code>operand</code></em></a> …)<br/>
  110. <a id="idm139667879257632" class="indexterm"/><span id="meta-operator"/><em class="replaceable"><code>operator</code></em> <code class="literal">::=</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  111. <a id="idm139667879254832" class="indexterm"/><span id="meta-operand"/><em class="replaceable"><code>operand</code></em> <code class="literal">::=</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  112.   | <a class="link" href="Keywords.xhtml#meta-keyword"><em class="replaceable"><code>keyword</code></em></a> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  113.   | <code class="literal">@</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  114.   | <code class="literal">@:</code> <a class="link" href="Primitive-expression-syntax.xhtml#meta-expression"><em class="replaceable"><code>expression</code></em></a><br/>
  115. </p>
  116. </div>
  117. <p>A procedure call consists of expressions for the procedure to be called
  118. and the arguments to be passed to it, with enclosing parentheses. A
  119. form in an expression context is a procedure call if <em class="replaceable"><code>operator</code></em> is
  120. not an identifier bound as a syntactic keyword.
  121. </p>
  122. <p>When a procedure call is evaluated, the operator and operand expressions
  123. are evaluated (in an unspecified order) and the resulting procedure is
  124. passed the resulting arguments.
  125. </p>
  126. <pre class="screen">(+ 3 4) ⇒ 7
  127. ((if #f + *) 3 4) ⇒ 12
  128. </pre>
  129. <p>The syntax <a class="link" href="Keywords.xhtml#meta-keyword"><em class="replaceable"><code>keyword</code></em></a> <em class="replaceable"><code>expression</code></em> is a <em class="firstterm">keyword argument</em>.
  130. This is a mechanism for specifying arguments using a name rather than position,
  131. and is especially useful for procedures with many optional paramaters.
  132. Note that <a class="link" href="Keywords.xhtml#meta-keyword"><em class="replaceable"><code>keyword</code></em></a> must be literal, and cannot be the
  133. result from evaluating a non-literal expression.
  134. (This is a change from previous versions of Kawa,
  135. and is different from Common Lisp and some other Scheme dialects.)
  136. </p>
  137. <p>An expression prefixed by <code class="literal">@</code> or <code class="literal">@:</code> is
  138. a splice argument. The following expression must evaluate to an
  139. “argument list” (see <a class="link" href="Application-and-Arguments-Lists.xhtml" title="Application and Arguments Lists">Application and Arguments Lists</a> for details);
  140. each element in the argument
  141. becomes a separate argument when call the <em class="replaceable"><code>operator</code></em>.
  142. (This is very similar to the “spread” operator is EcmaScript 6.)
  143. </p>
  144. </section>
  145. </section>
  146. <footer>
  147. <div class="navfooter">
  148. <ul>
  149. <li>
  150. <b class="toc">
  151. <a href="Primitive-expression-syntax.xhtml#idm139667879287568">Literal expressions</a>
  152. </b>
  153. </li>
  154. <li>
  155. <b class="toc">
  156. <a href="Primitive-expression-syntax.xhtml#idm139667879268592">Variable references</a>
  157. </b>
  158. </li>
  159. <li>
  160. <b class="toc">
  161. <a href="Primitive-expression-syntax.xhtml#idm139667879263008">Procedure calls</a>
  162. </b>
  163. </li>
  164. </ul>
  165. <p>
  166. Up: <a accesskey="u" href="Syntax.xhtml">Syntax</a></p>
  167. <p>
  168. Previous: <a accesskey="p" href="Hash-prefixed-forms.xhtml">Hash-prefixed forms</a></p>
  169. <p>
  170. Next: <a accesskey="n" href="Colon-notation.xhtml">Property access using colon notation</a></p>
  171. </div>
  172. </footer>
  173. </body>
  174. </html>