Evaluating-Scheme-expressions-from-Java.xhtml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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>Evaluating Scheme expressions from Java</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="Loading-Java-functions-into-Scheme.xhtml" title="Loading Java functions into Scheme"/>
  10. <link rel="next" href="XML-tools.xhtml" title="Working with XML and HTML"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Evaluating Scheme expressions from Java" epub:type="subchapter" id="Evaluating-Scheme-expressions-from-Java">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Evaluating Scheme expressions from Java</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>The following methods are recommended if you need to evaluate a
  23. Scheme expression from a Java method.
  24. (Some details (such as the ‘<code class="literal">throws</code>’ lists) may change.)
  25. </p>
  26. <p class="synopsis" kind="Static method"><span class="kind">Static method</span><span class="ignore">: </span><a id="idm139667870741808" class="indexterm"/> <span class="returnvalue">void</span> <code class="function">Scheme.registerEnvironment</code> ()</p>
  27. <div class="blockquote">
  28. <blockquote class="blockquote">
  29. <p>Initializes the Scheme environment. Maybe needed if you
  30. try to load a module compiled from a Scheme source file.
  31. </p>
  32. </blockquote>
  33. </div>
  34. <p class="synopsis" kind="Static method"><span class="kind">Static method</span><span class="ignore">: </span><a id="idm139667870738128" class="indexterm"/> <span class="returnvalue">Object</span> <code class="function">Scheme.eval</code> (<span class="type">InPort</span> <em class="replaceable"><code><em class="replaceable"><code>port</code></em></code></em>, <span class="type">Environment</span> <em class="replaceable"><code><em class="replaceable"><code>env</code></em></code></em>)</p>
  35. <div class="blockquote">
  36. <blockquote class="blockquote">
  37. <p>Read expressions from <em class="replaceable"><code>port</code></em>, and evaluate them in the
  38. <em class="replaceable"><code>env</code></em> environment, until end-of-file is reached.
  39. Return the value of the last expression,
  40. or <code class="literal">Interpreter.voidObject</code> if there is no expression.
  41. </p>
  42. </blockquote>
  43. </div>
  44. <p class="synopsis" kind="Static method"><span class="kind">Static method</span><span class="ignore">: </span><a id="idm139667870731264" class="indexterm"/> <span class="returnvalue">Object</span> <code class="function">Scheme.eval</code> (<span class="type">String</span> <em class="replaceable"><code><em class="replaceable"><code>string</code></em></code></em>, <span class="type">Environment</span> <em class="replaceable"><code><em class="replaceable"><code>env</code></em></code></em>)</p>
  45. <div class="blockquote">
  46. <blockquote class="blockquote">
  47. <p>Read expressions from <em class="replaceable"><code>string</code></em>, and evaluate them in the
  48. <em class="replaceable"><code>env</code></em> environment, until the end of the string is reached.
  49. Return the value of the last expression,
  50. or <code class="literal">Interpreter.voidObject</code> if there is no expression.
  51. </p>
  52. </blockquote>
  53. </div>
  54. <p class="synopsis" kind="Static method"><span class="kind">Static method</span><span class="ignore">: </span><a id="idm139667870724448" class="indexterm"/> <span class="returnvalue">Object</span> <code class="function">Scheme.eval</code> (<span class="type">Object</span> <em class="replaceable"><code><em class="replaceable"><code>sexpr</code></em></code></em>, <span class="type">Environment</span> <em class="replaceable"><code><em class="replaceable"><code>env</code></em></code></em>)</p>
  55. <div class="blockquote">
  56. <blockquote class="blockquote">
  57. <p>The <em class="replaceable"><code>sexpr</code></em> is an S-expression (as may be returned by <code class="literal">read</code>).
  58. Evaluate it in the <em class="replaceable"><code>env</code></em> environment, and return the result.
  59. </p>
  60. </blockquote>
  61. </div>
  62. <p>For the <code class="literal">Environment</code> in most cases you could use
  63. ‘<code class="literal">Environment.current()</code>’. Before you start, you
  64. need to initialize the global environment,
  65. which you can do with
  66. </p>
  67. <pre class="screen">Environment.setCurrent(new Scheme().getEnvironment());
  68. </pre>
  69. <p>Alternatively, rather than setting the global environment,
  70. you can use this style:
  71. </p>
  72. <pre class="screen">Scheme scm = new Scheme();
  73. Object x = scm.eval("(+ 3 2)");
  74. System.out.println(x);
  75. </pre>
  76. <section class="sect2" title="Using javax.script portable Java scripting" epub:type="division" id="idm139667870715120">
  77. <div class="titlepage">
  78. <div>
  79. <div>
  80. <h3 class="title">Using <code class="literal">javax.script</code> portable Java scripting</h3>
  81. </div>
  82. </div>
  83. </div>
  84. <p>Kawa also supports the standard
  85. <a class="ulink" href="http://docs.oracle.com/javase/8/docs/api/javax/script/package-summary.html" target="_top"><code class="literal">javax.script</code></a> API.
  86. The main advantage of this API is if you want your users to be able to choose
  87. between multiple scripting languages. That way you can support Kawa
  88. without Kawa-specific programming.
  89. </p>
  90. <p>For example the standard JDK tool <a class="ulink" href="http://docs.oracle.com/javase/8/docs/technotes/tools/unix/jrunscript.html" target="_top">jrunscript</a> provides a
  91. read-eval-print-loop for any language that implements the <code class="literal">javax.script</code>
  92. API. It knows nothing about Kawa but can still use it:
  93. </p>
  94. <pre class="screen">$ jrunscript -cp kawa.jar -l scheme
  95. scheme&gt; (cadr '(3 4 5))
  96. 4
  97. </pre>
  98. <p>(Of course the <code class="literal">jrunscript</code> REPL isn’t as nice as the one that
  99. Kawa provides. For example the latter can handle multi-line inputs.)
  100. </p>
  101. </section>
  102. </section>
  103. <footer>
  104. <div class="navfooter">
  105. <ul>
  106. <li>
  107. <b class="toc">
  108. <a href="Evaluating-Scheme-expressions-from-Java.xhtml#idm139667870715120">Using <code class="literal">javax.script</code> portable Java scripting</a>
  109. </b>
  110. </li>
  111. </ul>
  112. <p>
  113. Up: <a accesskey="u" href="Objects-Classes-and-Modules.xhtml">Object, Classes and Modules</a></p>
  114. <p>
  115. Previous: <a accesskey="p" href="Loading-Java-functions-into-Scheme.xhtml">Loading Java functions into Scheme</a></p>
  116. </div>
  117. </footer>
  118. </body>
  119. </html>