interpreter.xml 5.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<page name="manual_interpreter">

<title>Interpreter</title>

<box title="Command-line" link="cmdline">

<p>
For performance reasons, it is advised to build the interpreter using OCaml 
native code compiler. The structure of the command line is:
</p>

<sample>
14
cduce [OPTIONS ...] [FILES ...] [--arg ARGUMENT ...]
15
16
17
</sample>

<p>
18
The options and arguments are:
19
20
21
22
</p>

<ul>

23
<li> All the arguments following <code>--arg</code> are passed to the
24
25
26
CDuce program, in the variable <code>argv</code> (of type <code>[
String* ]</code>, which means sequence of character strings).  </li>

27

28
<li> The option <code>--compile</code> produces a cduce object file
29
30
31
(suffix <code>.cdo</code>) that can be later executed by  <code>cduce --run</code>.  </li>


32
33
<li> The switch <code>--quiet</code> suppresses normal output (typing,
results).  This option is normally used when the CDuce interpreter is used in
34
35
the context of batch processing.  </li>

36
37
38
39
40
<li> The options <code>--save %%filename%%</code> and 
<code>--load %%filename%%</code> allows persistence
between several invocations of the interpreter, by saving and
restoring the current environment (defined types and values) to a
file. Note that only the arguments after
41
<code>--arg</code> on the first invocation in a session are passed to the
42
43
44
CDuce program(s).
</li>

45
<li> The option <code>--dump %%filename%%</code> is equivalent
46
47
48
49
50
to <code>--load %%filename%% --save %%filename%%</code>. </li>

<li> The options <code>-v</code> and <code>--version</code> make the
interpreter print its version number and exit immediately.
</li>
51

52
53
54
<li> The options <code>--pxp</code> and <code>--expat</code> allow
to choose the parser for XML files (available only when CDuce
is built with expat support; default is <code>--expat</code>).</li>
55
56

<li>All the other arguments on the command line are considered CDuce
57
58
scripts, which are executed successively. 
</li>
59
60
61
62
63

</ul>

</box>

64
65
66
67
68
<box title="Scripting" link="scripting">

<p>
CDuce can also be used for writing using scripts. As usual it suffices to start
the script file by <code> #!%%install_dir%%/cduce</code> to call in a batch way
69
the CDuce interpreter. Here is an example of a script file that prints all the
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
titles of the filters of an Evolution mail client.

</p>
<sample><![CDATA[
#!/usr/local/bin/cduce

type Filter = <filteroptions>[<ruleset> [(<rule>[<title>String _*])+]];;

let filter : Filter = 
      match load_xml "/home/beppe/evolution/filters.xml" with
       | x&Filter -> x
       | _ -> raise "Not a filter document"
 in print_xml(<filters>([filter]/<ruleset>_/<rule>_/<title>_)) ;;



]]></sample>

</box>

90
91
92
93
<box title="Phrases" link="phrases">

<p>
CDuce programs are sequences of phrases, which can
94
be juxtaposed or separated by <code>;;</code>. There are several kinds of
95
96
97
98
99
100
phrases:
</p>

<ul>

<li>Types declarations <code>type %%T%% = %%t%%</code>. Adjacent types declarations are mutually
101
recursive, e.g.:
102
103
104
105
106
107
<sample><![CDATA[
type T = <a>[ S* ]
type S = <b>[ T ]
]]></sample>
</li>

108
<li>Function declarations <code>let %%f%% %%...%%</code>. 
109
Adjacent function declarations are mutually recursive, e.g.:
110
<sample><![CDATA[
111
112
let f (x : Int) : Int = g x
let g (x : Int) : Int  = x + 1
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
]]></sample>
</li>

<li>Global bindings <code>let  %%p%% = %%e%%</code>
or <code>let %%p%% : %%t%% = %%e%%</code>.</li>

<li>Evaluation statements (an expression to evaluate).</li>

<li>Debugging statements:
<sample><![CDATA[
debug filter %%t%% %%p%%          {{ inference for pattern matching }}
debug accept %%p%%            {{ type accepted by a pattern }}
debug sample %%t%%            {{ compute a sample from a type }}
debug subtype %%t%%1 %%t2%%       {{ check subtyping }}
debug compile %%t%% %%p%%1 ... %%pn%% {{ compilation of pattern matching }}
]]></sample>
</li>
130
131
132

<li>Textual inclusion <code>include "%%other_cduce_script.cd%%"</code>;
note that cycle of inclusion are detected and automatically broken.
133
134
Filename are relative to the directory of the current file
(or the current directory in the toplevel).
135
136
</li>

137
138
139
140
141
<li>Global namespace binding <code>namespace %%p%% = "%%...%%"</code>
and global namespace default <code>namespace "%%...%%"</code>
(see <local href="namespaces"/>).
</li>

142
143
144
145
</ul>

</box>

146
147
148
149
150
151
152
153
154
<box title="Toplevel" link="toplevel">

<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 <code>;;</code>.
155
Mutually recursive declarations of types or functions
156
157
158
159
must be contained in a single adjacent sequence of phrases
(without <code>;;</code> inbetween).
</p>

160
<p>
161
162
You can quit the toplevel with the toplevel directive
<code>#quit</code> but also with either  <code>Ctrl-C</code> or
163
<code>Ctrl-D</code>. In order to allow persistence 
164
165
(options <code>-save</code>,<code>--load</code> and 
<code>--dump</code>) to operate, you must quit the toplevel with <code>#quit</code>.
166
167
168
</p>

<p>
169
170
171
172
173
The toplevel directive <code>#env</code> prints the current
environment: the set of defined global types and values, and also 
the current sets of prefix-to-<local href="namespaces">namespace</local> bindings used
for parsing (as defined by the user) and
for pretty-printing (as computed by CDuce itself).
174
175
</p>

176
177
178
179
180
181
<p>
The toplevel directive <code>#reinit_ns</code> reinit the
table of prefix-to-namespace bindings used for pretty-printing
values and types with namespaces (see <local href="namespaces"/>).
</p>

182
183
184
185
186
187
188
189
<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>

</box>

190
</page>