CHICKEN provides an interpreter named csi for evaluating Scheme programs and expressions interactively.
csi {FILENAME|OPTION}
where FILENAME specifies a file with Scheme source-code. If the extension of the source file is .scm, it may be omitted. The runtime options described in Compiler command line format are also available for the interpreter. If the environment variable CSI_OPTIONS is set to a list of options, then these options are additionally passed to every direct or indirect invocation of csi. Please note that runtime options (like -:...) can not be passed using this method. The options recognized by the interpreter are:
Since UNIX shells use the #! notation for starting scripts, anything following the characters #! is ignored, with the exception of the special symbols #!optional, #!key, #!rest and #!eof.
The easiest way is to use the -script option like this:
% cat foo #! /usr/local/bin/csi -script (print (eval (with-input-from-string (car (command-line-arguments)) read)))
% chmod +x foo % foo "(+ 3 4)" 7
The parameter command-line-arguments is set to a list of the parameters that were passed to the Scheme script. Scripts can be compiled to standalone executables (don't forget to declare used library units).
CHICKEN supports writing shell scripts in Scheme for these platforms as well, using a slightly different approach. The first example would look like this on Windows:
C:>type foo.bat @;csibatch %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 (print (eval (with-input-from-string (car (command-line-arguments)) read)))
C:>foo "(+ 3 4)" 7
Like UNIX scripts, batch files can be compiled. Windows batch scripts do not accept more than 8 arguments.
Since it is sometimes useful to run a script into the interpreter without actually running it (for example to test specific parts of it), the option -ss can be used as an alternative to -script. -ss PATHNAME is equivalent to -script PATHNAME but invokes (main (command-line-arguments)) after loading all top-level forms of the script file. The result of main is returned as the exit status to the shell. Any non-numeric result exits with status zero:
% cat hi.scm (define (main args) (print "Hi, " (car args)) 0) % csi -ss hi.scm you Hi, you % csi -q #;1> ,l hi.scm #;2> (main (list "ye all")) Hi, ye all 0 #;3>
The toplevel loop understands a number of special commands:
#;1> (fac 10) ==> 3628800 #;2> ,tr fac #;3> (fac 3) |(fac 3) | (fac 2) | (fac 1) | (fac 0) | fac -> 1 | fac -> 1 | fac -> 2 |fac -> 6 ==> 6 #;4> ,utr fac #;5> (fac 3) ==> 6
You can define your own toplevel commands using the toplevel-command procedure:
[procedure] (toplevel-command SYMBOL PROC [HELPSTRING])
Defines or redefines a toplevel interpreter command which can be invoked by entering ,SYMBOL. PROC will be invoked when the command is entered and may read any required argument via read (or read-line). If the optional argument HELPSTRING is given, it will be listed by the ,? command.
The interpreter toplevel accepts the special object #[INDEX] which returns the result of entry number INDEX in the history list. If the expression for that entry resulted in multiple values, the first result (or an unspecified value for no values) is returned. If no INDEX is given (and if a whitespace or closing paranthesis character follows the #, then the result of the last expression is returned. Note that the value returned is implicitly quoted.
[procedure] (set-describer! TAG PROC)
Sets a custom description handler that invokes PROC when the ,d command is invoked with a record-type object that has the type TAG (a symbol). PROC is called with two arguments: the object to be described and an output-port. It should write a possibly useful textual description of the object to the passed output-port. For example:
#;1> (define-record point x y) #;2> (set-describer! 'point (lambda (pt o) (print "a point with x=" (point-x pt) "and y=" (point-y pt)))) #;3> ,d (make-point 1 2) a point with x=1 and y=2
On platforms that support it, it is possible to get auto-completion of symbols, history (over different csi sessions) and a more feature-full editor for the expressions you type using the http://galinha.ucpel.tche.br/readline egg by Tony Garnock Jones. It is very useful for interactive use of csi.
To enable it install the egg and put this in your ~/.csirc file:
(use readline regex) (current-input-port (make-gnu-readline-port)) (gnu-history-install-file-manager (string-append (or (getenv "HOME") ".") "/.csi.history"))
More details are available in the egg's documentation.
You can access the manual directly from csi using the http://galinha.ucpel.tche.br/man extension by http://galinha.ucpel.tche.br/Mario Domenech Goulart.
To enable it install the egg and put this in your ~/.csirc file:
(use man) (man:load)
Then, in csi, you can search for definitions using man:search as in:
(man:search "case")
Note that the search uses regular expressions. To view the documentation for one entry from the manual, use man:help as in:
(man:help "case-lambda")
Previous: Using the compiler
Next: Supported language