source: project/chicken/trunk/manual/Using the interpreter @ 5945

Last change on this file since 5945 was 5945, checked in by felix winkelmann, 12 years ago

renamed some tools in misc/; apply hack fix on x86-64 (again); re-added manual

File size: 10.4 KB
Line 
1[[tags: manual]]
2[[toc:]]
3
4== Using the interpreter
5
6CHICKEN provides an interpreter named {{csi}} for evaluating Scheme programs
7and expressions interactively.
8
9=== Interpreter command line format
10
11{{csi {FILENAME|OPTION}}}
12
13where {{FILENAME}} specifies a file with Scheme source-code.  If the
14extension of the source file is {{.scm}}, it may be omitted. The
15runtime options described in [[http://galinha.ucpel.tche.br/Using%20the%20compiler#Compiler%20command%20line%20format|Compiler command line format]] are also available
16for the interpreter.  If the environment variable {{CSI_OPTIONS}}
17is set to a list of options, then these options are additionally passed
18to every direct or indirect invocation of {{csi}}. Please note that
19runtime options (like {{-:...}}) can not be passed using this method.
20The options recognized by the interpreter are:
21
22; -- : Ignore everything on the command-line following this marker. Runtime options ({{-:...}}) are still recognized.
23
24; -i  -case-insensitive : Enables the reader to read symbols case insensitive. The default is to read case sensitive (in violation of R5RS).  This option registers the {{case-insensitive}} feature identifier.
25
26; -b  -batch : Quit the interpreter after processing all command line options.
27
28; -e  -eval EXPRESSIONS : Evaluate {{EXPRESSIONS}}. This option implies {{-batch}} and {{-quiet}}, so no startup message will be printed and the interpreter exits after processing all {{-eval}} options and/or loading files given on the command-line.
29
30; -D  -feature SYMBOL : Registers {{SYMBOL}} to be a valid feature identifier for {{cond-expand}}.
31
32; -h  -help : Write a summary of the available command line options to standard output and exit.
33
34; -I  -include-path PATHNAME : Specifies an alternative search-path for files included via the {{include}} special form. This option may be given multiple times. If the environment variable {{CHICKEN_INCLUDE_PATH}} is set, it should contain a list of alternative include pathnames separated by {{;}}. The environment variable {{CHICKEN_HOME}} is also considered as a search path.
35
36; -k  -keyword-style STYLE : Enables alternative keyword syntax, where {{STYLE}} may be either {{prefix}} (as in Common Lisp) or {{suffix}} (as in DSSSL). Any other value is ignored.
37
38; -n  -no-init : Do not load initialization-file. If this option is not given and the file {{./.csirc}} or {{$HOME/.csirc}} exists, then it is loaded before the read-eval-print loop commences.
39
40; -w  -no-warnings : Disables any warnings that might be issued by the reader or evaluated code.
41
42; -q  -quiet : Do not print a startup message.
43
44; -s  -script PATHNAME : This is equivalent to {{-batch -quiet -no-init PATHNAME}}. Arguments following {{PATHNAME}} are available by using  {{command-line-arguments}} and are not processed as interpreter options. Extra options in the environment variable {{CSI_OPTIONS}} are ignored.
45
46; -ss PATHNAME : The same as {{-s PATHNAME}} but invokes the procedure {{main}} with the value of {{(command-line-arguments)}} as its single argument. If the main procedure returns an integer result, then the interpreter is terminated, returning the integer as the status code back to the invoking process. Any other result terminates the interpreter with a zero exit status.
47
48; -R  -require-extension NAME : Equivalent to evaluating {{(require-extension NAME)}}.
49
50; -v  -version : Write the banner with version information to standard output and exit.
51
52
53=== Writing Scheme scripts
54
55Since UNIX shells use the {{#!}} notation for starting scripts,
56anything following the characters {{#!}} is ignored, with the exception of the special
57symbols {{#!optional, #!key, #!rest}} and {{#!eof}}.
58
59The easiest way is to use the {{-script}} option like this:
60
61 % cat foo
62 #! /usr/local/bin/csi -script
63 (print (eval (with-input-from-string
64                 (car (command-line-arguments))
65                  read)))
66
67 % chmod +x foo
68 % foo "(+ 3 4)"
69 7
70
71The parameter {{command-line-arguments}} is set to a list of the
72parameters that were passed to the Scheme script.  Scripts can be compiled
73to standalone executables (don't forget to declare used library units).
74
75CHICKEN supports writing shell scripts in Scheme for these platforms as well,
76using a slightly different approach. The first example would look like
77this on Windows:
78
79 C:>type foo.bat
80 @;csibatch %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
81 (print (eval (with-input-from-string
82                 (car (command-line-arguments))
83                 read)))
84
85 C:>foo "(+ 3 4)"
86 7
87
88Like UNIX scripts, batch files can be compiled. Windows batch scripts do not
89accept more than 8 arguments.
90
91Since it is sometimes useful to run a script into the interpreter without actually running it
92(for example to test specific parts of it), the option {{-ss}} can be used as an alternative to {{-script}}.
93{{-ss PATHNAME}} is equivalent to {{-script PATHNAME}} but invokes {{(main (command-line-arguments))}}
94after loading all top-level forms of the script file. The result of {{main}} is returned as the exit status
95to the shell. Any non-numeric result exits with status zero:
96
97 % cat hi.scm
98 (define (main args)
99   (print "Hi, " (car args))
100   0)
101 % csi -ss hi.scm you
102 Hi, you
103 % csi -q
104 #;1> ,l hi.scm
105 #;2> (main (list "ye all"))
106 Hi, ye all
107 0
108 #;3>
109
110=== Toplevel commands
111
112The toplevel loop understands a number of special commands:
113
114; ,? : Show summary of available toplevel commands.
115
116; ,l FILENAME ... : Load files with given {{FILENAME}}s
117
118; ,ln FILENAME ... : Load files and print result(s) of each top-level expression.
119
120; ,p EXP : Pretty-print evaluated expression {{EXP}}.
121
122; ,d EXP : Describe result of evaluated expression {{EXP}}.
123
124; ,du EXP : Dump contents of the result of evaluated expression {{EXP}}.
125
126; ,dur EXP N : Dump {{N}} bytes of the result of evaluated expression {{EXP}}.
127
128; ,exn : Describes the last exception that occurred and adds it to the result history (it can be accessed using the {{#}} notation).
129
130; ,q : Quit the interpreter.
131
132; ,r : Show system information.
133
134; ,s TEXT ... : Execute shell-command.
135
136; ,t EXP : Evaluate form and print elapsed time.
137
138; ,x EXP : Pretty-print macroexpanded expression {{EXP}} (the expression is not evaluated).
139
140; ,tr SYMBOL ... : Enables tracing of the toplevel procedures with the given names.
141
142<enscript highlight=scheme>
143#;1> (fac 10)                       ==> 3628800
144#;2> ,tr fac
145#;3> (fac 3)
146|(fac 3)
147| (fac 2)
148|  (fac 1)
149|   (fac 0)
150|   fac -> 1
151|  fac -> 1
152| fac -> 2
153|fac -> 6                          ==> 6
154#;4> ,utr fac
155#;5> (fac 3)                        ==> 6
156</enscript>
157
158; ,utr SYMBOL ... : Disables tracing of the given toplevel procedures.
159
160; ,br SYMBOL ... : Sets a breakpoint at the procedures named {{SYMBOL ...}}. Breakpoint can also be trigged using the {{breakpoint}} procedure.
161
162; ,ubr SYMBOL ... : Removes breakpoints.
163
164; ,c : Continues execution from the last invoked breakpoint.
165
166; ,breakall : Enable breakpoints for all threads (this is the default).
167
168; ,breakonly THREAD : Enable breakpoints only for the thread returned by the expression {{THREAD}}.
169
170; ,info : Lists traced procedures and breakpoints.
171
172; ,step EXPR : Evaluates {{EXPR}} in single-stepping mode. On each procedure call you will be presented with a menu that allows stepping to the next call, leaving single-stepping mode or triggering a breakpoint. Note that you will see some internal calls, and unsafe or heavily optimized compiled code might not be stepped at all. Single-stepping mode is also possible by invoking the {{singlestep}} procedure.
173
174You can define your own toplevel commands using the {{toplevel-command}}
175procedure:
176
177=== toplevel-command
178
179 [procedure] (toplevel-command SYMBOL PROC [HELPSTRING])
180
181Defines or redefines a toplevel interpreter command which can be invoked by entering
182{{,SYMBOL}}. {{PROC}} will be invoked when the command is entered and may
183read any required argument via {{read}} (or {{read-line}}). If the optional
184argument {{HELPSTRING}} is given, it will be listed by the {{,?}} command.
185
186=== History access
187
188The interpreter toplevel accepts the special object {{#[INDEX]}} which
189returns the result of entry number {{INDEX}} in the history list. If the expression
190for that entry resulted in multiple values, the first result (or an unspecified value for no values)
191is returned. If no {{INDEX}} is given (and if a whitespace or closing paranthesis character follows
192the {{#}}, then the result of the last expression is returned.
193Note that the value returned is implicitly quoted.
194
195=== set-describer!
196
197 [procedure] (set-describer! TAG PROC)
198
199Sets a custom description handler that invokes {{PROC}} when the {{,d}} command is invoked
200with a record-type object that has the type {{TAG}} (a symbol). {{PROC}} is called with
201two arguments: the object to be described and an output-port. It should write a possibly useful
202textual description of the object to the passed output-port. For example:
203
204 #;1> (define-record point x y)
205 #;2> (set-describer! 'point (lambda (pt o) (print "a point with x=" (point-x pt) " and y=" (point-y pt))))
206 #;3> ,d (make-point 1 2)
207 a point with x=1 and y=2
208
209=== Auto-completion and edition
210
211On platforms that support it, it is possible to get auto-completion of symbols,
212history (over different {{csi}} sessions) and a more feature-full
213editor for the expressions you type
214using the [[http://www.call-with-current-continuation.org/eggs/readline.html]] egg by
215Tony Garnock Jones.
216It is very useful for interactive use of csi.
217
218To enable it install the egg and put this in your {{~/.csirc}} file:
219
220 (use readline regex)
221 (current-input-port (make-gnu-readline-port))
222 (gnu-history-install-file-manager (string-append (or (getenv "HOME") ".") "/.csi.history"))
223
224More details are available in [[http://www.call-with-current-continuation.org/eggs/readline.html|the egg's documentation]].
225
226=== Accessing documentation
227
228You can access the manual directly from {{csi}} using the [[http://www.call-with-current-continuation.org/eggs/man.html|man]] extension by Mario Domenech Goulart.
229
230To enable it install the egg and put this in your {{~/.csirc}} file:
231
232 (use man)
233 (man:load)
234
235Then, in {{csi}}, you can search for definitions using {{man:search}} as in:
236
237 (man:search "case")
238
239Note that the search uses regular expressions.
240To view the documentation for one entry from the manual, use {{man:help}} as in:
241
242 (man:help "case-lambda")
243
244Note: Currently the documentation provided by the {{man}} extension corresponds to Chicken's 2.429, one of the last releases whose documentation was in the texinfo format (the format the {{man}} extension parses).
245
246----
247
248Previous: [[Using the compiler]]
249
250Next: [[Supported language]]
Note: See TracBrowser for help on using the repository browser.