<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>CDuce: Compiler/interpreter/toplevel</title><meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/><link type="text/css" rel="stylesheet" href="cduce.css"/></head><body style="margin: 0; padding : 0;"><table border="0" width="100%" cellspacing="10" cellpadding="0"><tr><td valign="top" align="left" style="width:20%;"><div class="leftbar" id="leftbar"><div class="smallbox"><ul><li><a href="#cmdline">Command-line</a></li><li><a href="#scripting">Scripting</a></li><li><a href="#phrases">Phrases</a></li><li><a href="#toplevl">Toplevel</a></li><li><a href="#lex">Lexical entities</a></li></ul></div></div></td><td><h1>Compiler/interpreter/toplevel</h1><div class="mainpanel"><div class="smallbox"><p><a href="index.html">CDuce: documentation</a>: <a href="manual.html">User's manual</a>: Compiler/interpreter/toplevel</p><p><a href="manual.html"><img class="icon" width="16" alt="Previous page:" height="16" src="img/left.gif"/> User's manual</a> <a href="manual_types_patterns.html"><img class="icon" width="16" alt="Next page:" height="16" src="img/right.gif"/> Types and patterns</a></p></div><div><h2><a name="cmdline">Command-line</a></h2><p> According to the command line arguments, the <b><tt>cduce</tt></b> command behaves either as an interactive toplevel, an interpreter, a compiler, or a loader. </p><ul><li><div class="code"><pre> cduce [OPTIONS ...] [--arg ARGUMENT ...] </pre></div><p> The command operates as an interactive toplevel. See the <a href="#toplevl">Toplevel</a> section below. </p></li><li><div class="code"><pre> cduce [OPTIONS ...] [ <strong class="highlight">script</strong>.cd | --stdin ] [--arg ARGUMENT ...] </pre></div><div class="code"><pre> cduce [OPTIONS ...] --script <strong class="highlight">script</strong>.cd [ ARGUMENT ...] </pre></div><p> The command runs the script <b><tt><strong class="highlight">script</strong>.cd</tt></b>. </p></li><li><div class="code"><pre> cduce [OPTIONS ...] --compile <strong class="highlight">script</strong>.cd </pre></div><p> The command compiles the script <b><tt><strong class="highlight">script</strong>.cd</tt></b> and produces <b><tt><strong class="highlight">script</strong>.cdo</tt></b>. If the OCaml/CDuce interface is available and enabled, the compilers looks for a corresponding OCaml interface <b><tt><strong class="highlight">script</strong>.cmi</tt></b>. See the <a href="manual_interfacewithocaml.html">Interfacing CDuce with OCaml</a> page for more information. </p></li><li><div class="code"><pre> cduce [OPTIONS ...] --run [ <strong class="highlight">script</strong>.cdo ... ] [--arg ARGUMENT ...] </pre></div><p> The command runs one or several pre-compiled scripts. </p></li></ul><p> The arguments that follow the <b><tt>--arg</tt></b> option are the scripts' command line. They can be accessed within CDuce using the <b><tt>argv</tt></b> operator (of type <b><tt>[] -> [ String* ]</tt></b>). </p><p> The options and arguments are: </p><ul><li><b><tt>--verbose</tt></b> (for <b><tt>--compile</tt></b> mode only). Display the type of values in the compiled unit.</li><li><b><tt>--obj-dir <i>directory</i></tt></b> (for <b><tt>--compile</tt></b> mode only). Specify where to put the <b><tt>.cdo</tt></b> file (default: same directory as the source file).</li><li><b><tt>--I <i>directory</i></tt></b> Add a directory to the search path for <b><tt>.cdo</tt></b>, <b><tt>.cmi</tt></b> and include files. </li><li><b><tt>--stdin</tt></b>. Read CDuce script from standard input. </li><li><b><tt>--no <i>feature</i></tt></b>. Disable one of the built-in optional features. The list of feature and their symbolic name can be obtained with the <b><tt>-v</tt></b> option. Can be used for instance to turn the Expat parser off, in order to use PXP, if both have been included at compile time. </li><li><b><tt>-v</tt></b>, <b><tt>--version</tt></b>. Show version information and built-in optional features, and exit. </li><li><b><tt>--mlstub</tt></b>. See <a href="manual_interfacewithocaml.html">Interfacing CDuce with OCaml</a>. </li><li><b><tt>--help</tt></b>. Show usage information about the command line. </li></ul></div><div><h2><a name="scripting">Scripting</a></h2><p> CDuce can be used for writing scripts. As usual it suffices to start the script file by <b><tt> #!<i>install_dir</i>/cduce</tt></b> to call in a batch way the CDuce interpreter. The <b><tt>--script</tt></b> option can be used to avoid <b><tt>--arg</tt></b> when calling the script. Here is an example of a script file that prints all the titles of the filters of an Evolution mail client. </p><div class="code"><pre> #!/bin/env cduce --script type Filter = <filteroptions>[<ruleset> [(<rule>[<title>String _*])+]];; let src : Latin1 = match argv [] with | [ f ] -> f | _ -> raise "Invalid command line" in let filter : Filter = match load_xml src with | x&Filter -> x | _ -> raise "Not a filter document" in print_xml(<filters>([filter]/<ruleset>_/<rule>_/<title>_)) ;; </pre></div></div><div><h2><a name="phrases">Phrases</a></h2><p> CDuce programs are sequences of phrases, which can be juxtaposed or separated by <b><tt>;;</tt></b>. There are several kinds of phrases: </p><ul><li>Types declarations <b><tt>type <i>T</i> = <i>t</i></tt></b>. Adjacent types declarations are mutually recursive, e.g.: <div class="code"><pre> type T = <a>[ S* ] type S = <b>[ T ] </pre></div></li><li>Function declarations <b><tt>let <i>f</i> <i>...</i></tt></b>. Adjacent function declarations are mutually recursive, e.g.: <div class="code"><pre> let f (x : Int) : Int = g x let g (x : Int) : Int = x + 1 </pre></div></li><li>Global bindings <b><tt>let <i>p</i> = <i>e</i></tt></b> (bind the result of the expression <b><tt><i>e</i></tt></b> using the pattern <b><tt><i>p</i></tt></b>), <b><tt>let <i>p</i> : <i>t</i> = <i>e</i></tt></b> (gives a less precise type to the expression), <b><tt>let <i>p</i> :? <i>t</i> = %%e$$</tt></b> (dynamically checks that the expression has some type).</li><li>Evaluation statements: an expression to evaluate.</li><li>Textual inclusion <b><tt>include "<i>other_cduce_script.cd</i>"</tt></b>; note that cycle of inclusion are detected and automatically broken. Filename are relative to the directory of the current file (or the current directory in the toplevel). </li><li>Global namespace binding: see <a href="namespaces.html">XML Namespaces</a>.</li><li>Schema declaration: see <a href="manual_schema.html">XML Schema</a>.</li><li>Alias for an external unit <b><tt>using <i>alias</i> = "<i>unit</i>"</tt></b> or <b><tt>using <i>alias</i> = <i>unit</i></tt></b>: gives an alternative name for a pre-compiled unit. Values, types, namespace prefixes, schema from <b><tt><i>unit</i>.cdo</tt></b> can be referred to either as <b><tt><i>alias</i>.<i>ident</i></tt></b> or as <b><tt><i>unit</i>.<i>ident</i></tt></b>. </li><li>Open an external unit <b><tt>open <i>u</i></tt></b>: the effect of this statement is to import all the idenfiers exported by the compilation unit <b><tt><i>u</i></tt></b> into the current scope. These identifiers are also re-exported by the current unit.</li></ul></div><div><h2><a name="toplevl">Toplevel</a></h2><p> If no CDuce file is given on the command line, the interpreter behaves as an interactive toplevel. </p><p> Toplevel phrases are processed after each <b><tt>;;</tt></b>. Mutually recursive declarations of types or functions must be contained in a single adjacent sequence of phrases (without <b><tt>;;</tt></b> inbetween). </p><p> You can quit the toplevel with the toplevel directive <b><tt>#quit</tt></b> but also with either <b><tt>Ctrl-C</tt></b> or <b><tt>Ctrl-D</tt></b>. Another option is to use the built-in <b><tt>exit</tt></b>. </p><p> The toplevel directive <b><tt>#help</tt></b> prints an help message about the available toplevel directives. </p><p> The toplevel directive <b><tt>#env</tt></b> prints the current environment: the set of defined global types and values, and also the current sets of prefix-to-<a href="namespaces.html">namespace</a> bindings used for parsing (as defined by the user) and for pretty-printing (as computed by CDuce itself). </p><p> The two toplevel directives <b><tt>#silent</tt></b> and <b><tt>#verbose</tt></b> can be used to turn down and up toplevel outputs (results of typing and evaluation). </p><p> The toplevel directive <b><tt>#reinit_ns</tt></b> reinit the table of prefix-to-namespace bindings used for pretty-printing values and types with namespaces (see <a href="namespaces.html">XML Namespaces</a>). </p><p> The toplevel directive <b><tt>#print_type</tt></b> shows a representationo of a CDuce type (including types imported from <a href="manual_schema.html">XML Schema</a> documents). </p><p> The toplevel directive <b><tt>#builtins</tt></b> prints the name of embedded OCaml values (see <a href="manual_interfacewithocaml.html">Interfacing CDuce with OCaml</a>). </p><p> The toplevel has no line editing facilities. You can use an external wrapper such as <a href="http://pauillac.inria.fr/~ddr/">ledit</a>. </p></div><div><h2><a name="lex">Lexical entities</a></h2><p> The <b>identifiers</b> (for variables, types, recursive patterns, ...) are qualified names, in the sense of <a href="http://www.w3.org/TR/REC-xml-names/">XML Namespaces</a>. The chapter <a href="namespaces.html">XML Namespaces</a> explains how to declare namespace prefixes in CDuce. Identifiers are resolved as XML attributes (which means that the default namespace does not apply). All the identifiers are in the same scope. For instance, there cannot be simultaneously a type and variable (or a schema, a namespace prefix, an alias for an external unit) with the same name. </p><p> The dot must be protected by a backslash in identifiers, to avoid ambiguity with the dot notation. </p><p> The dot notation serves several purposes: </p><ul><li> to refer to values and types declared in a separate CDuce compilation unit; </li><li> to refer to values from OCaml compilation unit (see <a href="manual_interfacewithocaml.html">Interfacing CDuce with OCaml</a>); </li><li> to refer to schema components (see <a href="manual_schema.html">XML Schema</a>); </li><li> to select a field from a record expression. </li></ul><p> CDuce supports two style of <b>comments</b>: <b><tt>(* ... *)</tt></b> and <b><tt>/* ... */</tt></b>. The first style allows the programmer to put a piece a code apart. Nesting is allowed, and strings within simple or double quotes are not searched for the end-marker <b><tt>*)</tt></b>. In particular, simple quotes (and apostrophes) have to be balanced inside a <b><tt>(* ... *)</tt></b> comment. The other style <b><tt>/* ... */</tt></b> is more adapted to textual comments. They cannot be nested and quotes are not treated specially inside the comment. </p></div><div class="meta"><p><a href="sitemap.html">Site map</a></p></div><div class="smallbox"><p><a href="index.html">CDuce: documentation</a>: <a href="manual.html">User's manual</a>: Compiler/interpreter/toplevel</p><p><a href="manual.html"><img class="icon" width="16" alt="Previous page:" height="16" src="img/left.gif"/> User's manual</a> <a href="manual_types_patterns.html"><img class="icon" width="16" alt="Next page:" height="16" src="img/right.gif"/> Types and patterns</a></p></div></div></td></tr></table></body></html>