UNPKG

@ciao-lang/ts-ciao-interface

Version:

Simple Ciao interface for node.

26 lines (23 loc) 6.69 kB
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><meta name="theme-color" content="#273f79"/><link rel="stylesheet" href="lpdoc.css" type="text/css"/><script type="text/javascript" src="lpdoc.js"></script><title>The script interpreter &mdash; The Ciao System v1.22</title></head><body><div class="lpdoc-page fixleftbar"><a href="#" id="sidebar-toggle-button" class="lpdoc-navbutton"><span id="sidebar-button-arrow">&#9776;</span></a><div id="sidebar" class="lpdoc-sidebar"><div style="height: 40px; margin-left: auto; margin-right: auto"><img src="ciao-logo_autofig.png" width=auto height=100%></div><div class="lpdoc-nav"><span class="lpdoc-on-right"><a class="lpdoc-navbutton" href="DevEnv.html">&#x2191;</a><a class="lpdoc-navbutton" href="ciaoc.html">&#x2190;</a><a class="lpdoc-navbutton" href="ciao-utilities.html">&#x2192;</a><a class="lpdoc-navbutton" href="ciaosearch.html">&#x1F50D;</a></span><span><a href="ciaofulltoc.html">TOC</a></span></div><hr></hr><ul class="lpdoc-itemize-sectpath"><li><a href="ciao.html">The Ciao System</a> &raquo;<br/> </li><li><a href="DevEnv.html">PART I - The program development environment</a> &raquo;<br/> </li><li><a href=""><strong>The script interpreter</strong></a></li></ul><hr></hr><em>ON THIS PAGE</em><ul><li><a href="#How it works">How it works</a></li><li><a href="#Command line arguments in scripts">Command line arguments in scripts</a></li></ul></div><div class="lpdoc-main"><div id=""><h1>The script interpreter</h1> <strong>Author(s):</strong> <a class="lpdoc-idx-anchor" id="Daniel Cabeza" href="ciaosearch.html#Daniel Cabeza">Daniel Cabeza</a>, <a class="lpdoc-idx-anchor" id="Manuel Hermenegildo" href="ciaosearch.html#Manuel Hermenegildo">Manuel Hermenegildo</a>, <a class="lpdoc-idx-anchor" id="Jose F. Morales" href="ciaosearch.html#Jose F. Morales">Jose F. Morales (minor)</a>.<p> <a class="lpdoc-idx-anchor" id="0" href="ciaosearch.html#ciao-shell"><tt>ciao-shell</tt></a> is the Ciao script interpreter. It can be used to write <a class="lpdoc-idx-anchor" id="1" href="ciaosearch.html#Prolog shell scripts"><em>Prolog shell scripts</em></a> (see [<a class="lpdoc-idx-anchor" id="2" href="ciaorefs.html#sicstus-scripts-complangprolog-www">Her96</a>]), that is, executable files containing source code, which are compiled on demand.<p>Writing Prolog scripts can sometimes be advantageous with respect to creating binary executables for small- to medium-sized programs that are modified often and perform relatively simple tasks. The advantage is that no explicit compilation is necessary, and thus changes and updates to the program imply only editing the source file. The disadvantage is that startup of the script (the first time after it is modified) is slower than for an application that has been compiled previously.<p><div id="How it works"><h2>How it works</h2> <p>Essentially, <tt>ciao-shell</tt> is a smaller version of the Ciao top-level, which starts by loading the file given to it as the first argument and then starts execution at <a class="lpdoc-idx-anchor" id="3" href="ciaosearch.html#main/1"><tt>main/1</tt></a> (the argument is instantiated to a list containing the command line options, in the usual way). Note that the Prolog script cannot have a <tt>module</tt> declaration for this to work. While loading the file, <tt>ciao-shell</tt> changes the <a class="lpdoc-idx-anchor" id="4" href="ciaosearch.html#prolog flag">prolog flag</a> <tt>quiet</tt> so that no informational or warning messages are printed (error messages will be reported to <tt>user_error</tt>, however). The operation of <tt>ciao-shell</tt> in Unix-like systems is based in a special compiler feature: when the first character of a file is &apos;<tt>#</tt>&apos;, the compiler skips the first lines until an empty line is found. In Windows, its use is as easy as naming the file with a <tt>.pls</tt> extension, which will launch <tt>ciao-shell</tt> appropriately.<p>For example, in a Linux/Unix system, assume a file called <a class="lpdoc-idx-anchor" id="5" href="ciaosearch.html#hello"><tt>hello</tt></a> contains the following program:<p><pre class="lpdoc-codeblock">#!/usr/bin/env ciao-shell % -*- mode: ciao; -*- main(_) :- write(&apos;Hello world&apos;), nl. </pre> <p>Then, the file <a class="lpdoc-idx-anchor" id="6" href="ciaosearch.html#hello"><tt>hello</tt></a> can be <em>run</em> by simply making it executable and invoking it from the command line:<p><pre class="lpdoc-codeblock">$ chmod +x hello $ ./hello Hello world </pre> <p>The lines: <pre class="lpdoc-codeblock">#!/usr/bin/env ciao-shell % -*- mode: ciao; -*- </pre> <p>invokes <a class="lpdoc-idx-anchor" id="7" href="ciaosearch.html#ciao-shell"><tt>ciao-shell</tt></a> through <tt>/usr/bin/env</tt> (POSIX.2 compliant), instructing it to read this same file, and passing it the rest of the arguments to <tt>hello</tt> as arguments to the Prolog program. The second line <tt>% -*- mode: ciao; -*-</tt> is simply a comment which is seen by <a class="lpdoc-idx-anchor" id="8" href="emacs.html"><tt>emacs</tt></a> and instructs it to edit this file in Ciao mode (this is needed because these script files typically do not have a <tt>.pl</tt> ending). When <a class="lpdoc-idx-anchor" id="9" href="ciaosearch.html#ciao-shell"><tt>ciao-shell</tt></a> starts, if it is the first time, it compiles the program (skipping the first lines, as explained above), or else at successive runs loads the <tt>.po</tt> object file, and then calls <a class="lpdoc-idx-anchor" id="10" href="ciaosearch.html#main/1"><tt>main/1</tt></a>.<p>Note that the process of creating Prolog scripts is made very simple by the Ciao <a class="lpdoc-idx-anchor" id="11" href="ciaosearch.html#emacs mode">emacs mode</a>, which automatically inserts the header and makes the file executable (See <a href="CiaoMode.html">Using Ciao inside GNU emacs</a>).<p></div><div id="Command line arguments in scripts"><h2>Command line arguments in scripts</h2> <p>The following example illustrates the use of command-line arguments in scripts. Assume that a file called <tt>say</tt> contains the following lines:<p><pre class="lpdoc-codeblock">#!/usr/bin/env ciao-shell % -*- mode: ciao; -*- main(Argv) :- write_list(Argv), nl. write_list([]). write_list([Arg|Args]) :- write(Arg), write(&apos; &apos;), write_list(Args). </pre> <p>An example of use is:<p><pre class="lpdoc-codeblock">$ say hello dolly hello dolly </pre> </div><br/></div><div class="lpdoc-footer">Generated with LPdoc using Ciao</div></div><div class="lpdoc-clearer"></div></div></body></html>