Copyright © 2000-2006, Felix L. Winkelmann All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
CHICKEN - A practical and portable Scheme system User's manual (Version 2, Build 41)
(c) 2000-2006, Felix L. Winkelmann All rights reserved. Translated to LaTeX by Peter Keller. Translated to texinfo by Linh Dang.
--- The Detailed Node Listing ---
Using the compiler
Using the interpreter
Supported language
Non-standard macros and special forms
Unit library
Unit eval
Unit extras
Unit posix
Unit utils
Unit lolevel
Unit tinyclos
Interface to external functions and variables
The Easy Foreign Function Interface
chicken-setup
Additional files
CHICKEN is a compiler that translates Scheme source files into C, which in turn can be fed to a C-compiler to generate a standalone executable. This principle, which is used by several existing compilers, achieves high portability because C is implemented on nearly all available platforms.
This package is distributed under the BSD license and as such is free to use and modify. An interpreter is also available and can be used as a scripting environment or for testing programs before compilation.
The method of compilation and the design of the runtime-system follow
closely Henry Baker's CONS Should Not CONS Its Arguments, Part II:
Cheney on the M.T.A. paper and expose a number of interesting
properties: consing (creation of data on the heap) is relatively
inexpensive, because a generational garbage collection scheme is used,
in which short-lived data structures are reclaimed extremely quickly.
Moreover, call-with-current-continuation
is practically for free
and CHICKEN does not suffer under any performance penalties if
first-class continuations are used in complex ways. The generated C
code is fully tail-recursive.
Some of the features supported by CHICKEN:
match
package
This manual is merely a reference for the CHICKEN system and assumes a working knowledge of Scheme.
The compiler translates Scheme source code into fairly portable C that can be compiled and linked with most available C compilers. CHICKEN supports the generation of executables and libraries, linked either statically or dynamically. Compiled Scheme code can be loaded dynamically, or can be embedded in applications written in other languages. Separate compilation of modules is fully supported.
The most portable way of creating separately linkable entities is supported by so-called units. A unit is a single compiled object module that contains a number of toplevel expressions that are executed either when the unit is the main unit or if the unit is used. To use a unit, the unit has to be declareed as used, like this:
(declare (uses UNITNAME))
The toplevel expressions of used units are executed in the order in which the units appear in the uses declaration. Units may be used multiple times and uses declarations may be circular (the unit is initialized at most once). To compile a file as a unit, add a unit declaration:
(declare (unit UNITNAME))
When compiling different object modules, make sure to have one main unit. This unit is called initially and initializes all used units before executing its toplevel expressions. The main-unit has no unit declaration.
Another method of using definitions in separate source files is to include them. This simply inserts the code in a given file into the current file:
(include "FILENAME")
Macro definitions are only available when processed
by include
. Macro definitions in separate units are not available, since
they are defined at compile time, i.e the time when that other unit
was compiled (macros can optionally be
available at runtime, see define-macro in Substitution forms and macros).
On platforms that support dynamic loading of compiled code (like Windows and most ELF based
systems like Linux or BSD) code can be compiled into a shared object (.so
) and loaded
dynamically into a running application.
The interface to chicken
is intentionally simple. System
dependent makefiles, shell-scripts or batch-files should perform
any necessary steps before and after invocation of chicken
.
A program named csc
provides a much simpler
interface to the Scheme- and C-compilers and linker. Enter
csc -help
on the command line for more information.
chicken FILENAME {OPTION}
FILENAME
is the complete pathname of the source file that is to
be translated into C. A filename argument of “-
” specifies that
the source text should be read from standard input. Note that the filename
has to be the first argument to chicken
.
Possible options are:
-analyze-only
-benchmark-mode
-no-trace -no-lambda-info -optimize-level 3
-fixnum-arithmetic -disable-interrupts -block -lambda-lift
.
-block
eval
and unused toplevel bindings are removed.
-case-insensitive
case-insensitive
feature identifier.
-check-imports
(declare (uses ...))
, the compiler will search a file named UNITNAME.exports
in the current include path and load its contents into the “import-table” (if found).
Also, export-information for extensions (accessed through (require-extension ...)
)
will be searched and stored in the import-table. If a required extensions does not
provide explicit exports-innformation and .exports
file is searched
(as with used units). After the analysis phase of
the compiler, referenced toplevel variables for which no assignment was found will
generate a warning. Also, re-assignments of imported variables will trigger a warning.
-check-syntax
-compress-literals THRESHOLD
THRESHOLD
as strings
and parse the strings at run-time. This reduces the size of the code and
speeds up compile-times of the host C compiler, but has a small run-time
performance penalty. The size of a literal is computed by counting recursively the objects
in the literal, so a vector counts as 1 plus the count of the elements,
a pair counts as the counts of the car and the cdr, respectively.
All other objects count 1.
-debug MODES
MODES
is a string
of characters that select debugging information about the compiler that
will be printed to standard output.
-debug-level LEVEL
LEVEL
should be an integer.
-debug-level 0
is equivalent to -no-trace -no-lambda-info
-debug-level 1
is equivalent to -no-trace
-debug-level 2
does nothing (the default)
-disable-c-syntax-checks
-disable-interrupts
(disable-interrupts)
declaration. No
interrupt-checks are generated for compiled programs.
-disable-stack-overflow-checks
-:o
runtime option.
-disable-warning CLASS
usage
warnings related to command-line arguments
type
warnings related to type-conversion
ext
warnings related to extension libraries
var
warnings related to variable- and syntax-definitions and use
const
warnings related to constant-definitions
syntax
syntax-related warnings
redef
warnings about redefinitions of standard- or extended-bindings
call
warnings related to known procedure calls
ffi
warnings related to the foreign function interface
-dynamic
-epilogue FILENAME
FILENAME
at the end of the compiled source file. The include-path
is not searched. This option may be given multiple times.
-emit-exports FILENAME
FILENAME
.
-emit-external-prototypes-first
define-external
before any
other foreign declarations. This is sometimes useful, when C/C++ code embedded into
the a Scheme program has to access the callbacks. By default the prototypes are emitted
after foreign declarations.
-explicit-use
library, eval
and extras
.
Use this option if compiling a library unit instead of an application
unit.
-extend FILENAME
-extension
-prelude '(define-extension <NAME>)'
, where
<NAME>
is the basename of the currently compiled file.
Note that if you want to compile a file
as a normal (dynamically loadable) extension library, you should also pass the
-shared
option.
-feature SYMBOL
SYMBOL
to be a valid feature identifier for
cond-expand
.
-ffi
#>! ... <#
.
-ffi-parse
#>? ... <#
.
-ffi-define SYMBOL
foreign-parse
declarations.
-ffi-include-path PATH
-ffi-no-include
-fixnum-arithmetic
(fixnum-arithmetic)
declaration. Assume all
mathematical operations use small integer arguments.
-heap-size NUMBER
NUMBER
bytes. The parameter may be followed by a
M
(m
) or K
(k
)
suffix which stand for mega- and kilobytes, respectively. The default
heap size is 5 kilobytes. Note that only half of it is in use at every
given time.
-heap-initial-size NUMBER
-heap-growth PERCENTAGE
-:hg
).
-heap-shrinkage PERCENTAGE
-:hs
).
-help
-import FILENAME
-check-imports
. This is equivalent to declaring
(declare (import FILENAME))
Implies -check-imports
.
-include-path PATHNAME
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.
-inline
-inline-limit
option).
-inline-limit THRESHOLD
-inline
option.
The default threshold is 10
.
-keyword-style STYLE
STYLE
may be either
prefix
(as in Common Lisp), suffix
(as in DSSSL) or none
.
Any other value is ignored. The default is suffix
.
-lambda-lift
-no-lambda-info
lambda
expression (currently the argument-list,
after alpha-conversion/renaming).
-no-trace
-no-trace
is specified. With this option the generated
code is slightly faster.
-no-warnings
-nursery NUMBER
-stack-size NUMBER
NUMBER
bytes. The parameter may be followed by a M
(m
) or K
(k
) suffix. The default stack-size
depends on the target platform.
-optimize-leaf-routines
-optimize-level LEVEL
LEVEL
should be
an integer.
-optimize-level 0
does nothing.
-optimize-level 1
is equivalent to
-optimize-leaf-routines
-optimize-level 2
is currently the same as -optimize-level 1
-optimize-level 3
is equivalent to
-optimize-leaf-routines -unsafe
-output-file FILENAME
FILENAME.c
.
-postlude EXPRESSIONS
EXPRESSIONS
after all other toplevel expressions in the
compiled file. This option may be given multiple times. Processing of
this option takes place after processing of -epilogue
.
-prelude EXPRESSIONS
EXPRESSIONS
before all other toplevel expressions in the
compiled file. This option may be given multiple times. Processing of
this option takes place before processing of -prologue
.
-profile
-accumulate-profile
exit
or implicitly), profiling statistics are written to a file named
PROFILE
. Each line of the generated file contains a list with
the procedure name, the number of calls and the time spent executing
it. Use the chicken-profile
program to display the profiling
information in a more user-friendly form. Enter chicken-profile
with no arguments at the command line to get a list of available options.
The -accumulate-profile
option is similar to -profile
, but
the resulting profile information will be appended to any existing
PROFILE
file. chicken-profile
will merge and sum up the accumulated
timing information, if several entries for the same procedure calls exist.
-profile-name FILENAME
PROFILE
. Implies -profile
.
-prologue FILENAME
FILENAME
at the start of the compiled
source file. The include-path is not searched. This option may be given
multiple times.
-quiet
-raw
runtime.c
and chicken.h
).
-require-extension NAME
NAME
before the compilation process
commences. This is identical to adding (require-extension NAME)
at the start of
the compiled program.
-run-time-macros
-to-stdout
.c
file.
-unit NAME
-prelude "(declare (unit NAME))"
-unsafe
-unsafe-libraries
-uses NAME
NAME
. This is equivalent to
-prelude "(declare (uses NAME))"
-no-usual-integrations
(not usual-integrations)
.
-version
-verbose
The environment variable CHICKEN_OPTIONS
can be set to a string
with default command-line options for the compiler.
After successful compilation a C source file is generated and can be compiled with a C compiler. Executables generated with CHICKEN (and the compiler itself) accept a small set of runtime options:
-:?
-:b
-:c
csi
) to force output of the #;N> prompt even if stdin
is not a terminal (for example if running in an emacs
buffer
under Windows).
-:d
-:D
-:hNUMBER
-:hiNUMBER
-:hgPERCENTAGE
PERCENTAGE
.
The default is 200.
-:hmNUMBER
-:hsPERCENTAGE
PERCENTAGE
of the heap is used, then it will shrink to
PERCENTAGE
. The default is 50. Note: If you want to make sure
that the heap never shrinks, specify a value of 0
. (this can
be useful in situations where an optimal heap-size is known in advance).
-:o
-:sNUMBER
-:tNUMBER
-:fNUMBER
-:aNUMBER
-:w
-:r
-no-trace
options.
-:x
-:x
is not given, a warning will be shown, though.
-:B
The argument values may be given in bytes, in kilobytes (suffixed with
K
or k
), in megabytes (suffixed with M
or m
), or in gigabytes (suffixed with G
or g
). Runtime options may be combined, like -:dc
,
but everything following a NUMBER
argument is ignored. So
-:wh64m
is OK, but -:h64mw
will not enable GC of
unused symbols.
To compile a Scheme program (assuming a UNIX-like environment) perform the following steps:
foo.scm
;;; foo.scm (define (fac n) (if (zero? n) 1 (* n (fac (- n 1))) ) ) (write (fac 10)) (newline)
foo.scm
% csc foo.scm
% foo
3628800
If multiple bodies of Scheme code are to be combined into a single executable, then we have to compile each file and link the resulting object files together with the runtime system:
foo.scm
and bar.scm
;;; foo.scm (declare (uses bar)) (write (fac 10)) (newline) ;;; bar.scm (declare (unit bar)) (define (fac n) (if (zero? n) 1 (* n (fac (- n 1))) ) )
foo.scm
and bar.scm
% csc -c bar.scm % csc foo.scm bar.o -o foo
% foo 3628800
The declarations specify which of the compiled files is the main
module, and which is the library module. An executable can only have
one main module, since a program has only a single entry-point. In this
case foo.scm
is the main module, because it doesn't have a
unit
declaration.
The compiler supplies a couple of hooks to add user-level passes to the
compilation process. Before compilation commences any Scheme source files
or compiled code specified using the -extend
option are loaded
and evaluated. The parameters user-options-pass, user-read-pass,
user-preprocessor-pass, user-pass, user-pass-2
and user-post-analysis-pass
can be set
to procedures that are called to perform certain compilation passes
instead of the usual processing (for more information about parameters
see: Parameters.
Holds a procedure that will be called with a list of command-line arguments and should return two values: the source filename and the actual list of options, where compiler switches have their leading
-
(hyphen) removed and are converted to symbols. Note that this parameter is invoked before processing of the-extend
option, and so can only be changed in compiled user passes.
Holds a procedure of three arguments. The first argument is a list of strings with the code passed to the compiler via
-prelude
options. The second argument is a list of source files including any files specified by-prologue
and-epilogue
. The third argument is a list of strings specified using-postlude
options. The procedure should return a list of toplevel Scheme expressions.
Holds a procedure of one argument. This procedure is applied to each toplevel expression in the source file before macro-expansion. The result is macro-expanded and compiled in place of the original expression.
Holds a procedure of one argument. This procedure is applied to each toplevel expression after macro-expansion. The result of the procedure is then compiled in place of the original expression.
Holds a procedure of three arguments, which is called with the canonicalized node-graph as its sole argument. The result is ignored, so this pass has to mutate the node-structure to cause any effect.
Holds a procedure that will be called after the last performed program analysis. The procedure (when defined) will be called with three arguments: the program database, a getter and a setter-procedure which can be used to access and manipulate the program database, which holds various information about the compiled program. The getter procedure should be called with two arguments: a symbol representing the binding for which information should be retrieved, and a symbol that specifies the database-entry. The current value of the database entry will be returned or
#f
, if no such entry is available. The setter procedure is called with three arguments: the symbol and key and the new value.For information about the contents of the program database contact the author.
Loaded code (via the -extend
option) has access to the library
units extras, srfi-1, srfi-4, utils, regex
and the pattern matching macros.
Multithreading is not available.
Note that the macroexpansion/canonicalization phase of the compiler adds
certain forms to the source program. These extra expressions are not
seen by user-preprocessor-pass
but by user-pass
.
It is relatively easy to create distributions of Scheme projects that
have been compiled to C. The runtime system of CHICKEN consists of only
two handcoded C files (runtime.c
and chicken.h
), plus
the file chicken-config.h
, which is generated by the build process. All
other modules of the runtime system and the extension libraries are just
compiled Scheme code. The following example shows a minimal application, which
should run without changes on the most frequent operating systems, like Windows,
Linux or FreeBSD:
Let's take a simple “Hello, world!”:
; hello.scm (print "Hello, world!")
Compiled to C, we get hello.c
. We need the files chicken.h
and
runtime.c
, which contain the basic runtime system, plus the three basic library
files library.c
, eval.c
and extras.c
which contain the same functionality as the
library linked into a plain CHICKEN-compiled application, or which is available by default in the
interpreter, csi
:
% csc hello.scm -O2 -d1
A simple makefile is needed as well:
# Makefile for UNIX systems hello: hello.o runtime.o library.o eval.o extras.o $(CC) -o hello hello.o runtime.o library.o eval.o extras.o -lm hello.o: chicken.h runtime.o: chicken.h library.o: chicken.h eval.o: chicken.h extras.o: chicken.h
Now we have all files together, and can create an tarball containing all the files:
% tar cf hello.tar Makefile hello.c runtime.c library.c eval.c extras.c chicken.h % gzip hello.tar
This is of naturally rather simplistic. Things like enabling dynamic loading, estimating the optimal stack-size and selecting supported features of the host system would need more configuration- and build-time support. All this can be addressed using more elaborate build-scripts, makefiles or by using autoconf/automake/libtool.
Note also that the size of the application can still be reduced by removing extras
and
eval
and compiling hello.scm
with the -explicit-use
option.
For more information, study the CHICKEN source code and/or get in contact with the author.
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:
--
-:...
”) are still recognized.
-i -case-insensitive
case-insensitive
feature identifier.
-b -batch
-e -eval EXPRESSIONS
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.
-D -feature SYMBOL
SYMBOL
to be a valid feature identifier for
cond-expand
.
-h -help
-I -include-path PATHNAME
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.
-k -keyword-style STYLE
STYLE
may be either
prefix
(as in Common Lisp) or suffix
(as in DSSSL).
Any other value is ignored.
-n -no-init
-w -no-warnings
-q -quiet
-s -script PATHNAME
-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.
-R -require-extension NAME
(require-extension NAME)
.
-v -version
#!
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.
The toplevel loop understands a number of special commands:
,?
,l FILENAME ...
FILENAME
s
,ln FILENAME ...
,p EXP
EXP
.
,d EXP
EXP
.
,du EXP
EXP
.
,dur EXP N
N
bytes of the result of evaluated expression EXP
.
,exn
#
notation).
,q
,r
,s TEXT ...
,t EXP
,x EXP
EXP
(the expression is
not evaluated).
,tr SYMBOL ...
#;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
,utr SYMBOL ...
,br SYMBOL ...
SYMBOL ...
.
Breakpoint can also be trigged using the breakpoint
procedure.
,ubr SYMBOL ...
,c
,breakall
,breakonly THREAD
THREAD
.
,info
,step EXPR
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.
You can define your own toplevel commands using the toplevel-command
procedure:
(toplevel-command SYMBOL PROC [HELPSTRING])Defines or defines a toplevel interpreter command the can be invoked by entering
,SYMBOL
.PROC
will be invoked when the command is entered and may read any required argument viaread
(orread-line
). If the optional argumentHELPSTRING
is given, it will be listed by the,?
command.
Additional macros and procedures available in the interpreter are:
# #INDEXReturns 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 noINDEX
is given (and if a whitespace or closing paranthesis character follows the#
, then the result of the last expression is returned. Note that this facility is a reader macro and is implicitly quoted.
(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 typeTAG
(a symbol).PROC
is called with two arguments: the object to be described an an output-port and 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
[2] Identifiers are by default case-sensitive.
[4.1.3] The maximal number of arguments that may be passed to a compiled procedure or macro is 120. A macro-definition that has a single rest-parameter can have any number of arguments.
If the libffi
library is available on this platform, and if it
is installed, then CHICKEN can take advantage
of this. See the README
file for more details.
[4.3] syntax-rules
macros are not provided but available separately.
[6.2.4] The runtime system uses the numerical string-conversion routines of the underlying C library and so does only understand standard (C-library) syntax for floating-point constants.
[6.2.5] There is no built-in support for rationals, complex numbers or
extended-precision integers (bignums).
The routines complex?
, real?
and rational?
are identical to the standard procedure
number?
. The procedures numerator
, denominator
,
rationalize
, make-rectangular
and make-polar
are not implemented.
Support for extended numbers is available as a separate package, provided the GNU multiprecision library is installed.
[6.2.6] The procedure string->number
does not obey read/write
invariance on inexact numbers.
[6.4] The maximum number of values that can be passed to continuations
captured using call-with-current-continuation
is 120.
[6.5] Code evaluated in scheme-report-environment
or
null-environment
still sees non-standard syntax.
[6.6.2] The procedure char-ready?
always returns
#t
for terminal ports. The procedure read
does not obey read/write
invariance on inexact numbers.
[6.6.3] The procedures write
and display
do not obey
read/write invariance to inexact numbers.
[6.6.4] The transcript-on
and transcript-off
procedures are
not implemented.
[2.1] Identifiers may contain special characters if delimited with
| ... |
.
[2.3] The brackets [ ... ]
are provided as an alternative syntax
for ( ... )
. A number of reader extensions is provided. See
Non standard read syntax.
[4] Numerous non-standard macros are provided. See Non-standard macros and special forms for more information.
[4.1.4] Extended DSSSL style lambda lists are supported. DSSSL formal argument lists are defined by the following grammar:
<formal-argument-list> ==> <required-formal-argument>* [(#!optional <optional-formal-argument>*)] [(#!rest <rest-formal-argument>)] [(#!key <key-formal-argument>*)] <required-formal-argument> ==> <ident> <optional-formal-argument> ==> <ident> | (<ident> <initializer>) <rest-formal-argument> ==> <ident> <key-formal-argument> ==> <ident> | (<ident> <initializer>) <initializer> ==> <expr>
When a procedure is applied to a list of actual arguments, the formal and actual arguments are processed from left to right as follows:
Variables in required-formal-arguments are bound to successive actual arguments starting with the first actual argument. It shall be an error if there are fewer actual arguments than required-formal-arguments.
Next, variables in optional-formal-arguments are bound to any remaining actual arguments.
If there are fewer remaining actual arguments than optional-formal-arguments, then variables are
bound to the result of the evaluation of initializer, if one was specified or otherwise to #f
. The initializer is
evaluated in an environment in which all previous formal arguments have been bound.
If there is a rest-formal-argument, then it is bound to a list of all remaining actual arguments. The remaining actual arguments are also eligible to be bound to keyword-formal-arguments. If there is no rest-formal-argument and there are no keywordas, then it shall be an error if there are any remaining actual arguments.
If #!key
was specified in the formal-argument-list, there shall be an even number of remaining actual arguments.
These are interpreted as a series of pairs, where the first member of each pair is a keyword specifying the argument
name, and thorresplue. It shall be an error if the first member of a pair is not a keyword.
It shall be an error if the argument name is not the same as a variable in a keyword-formal-argument, unless
there is a rest-formal-argument. If the same argument name occurs more than once in the list of actual arguments,
then the first value is used. If there is no actual argument for a particular keyword-formal-argument, then the variable
is bound to the result of evaluating initializer if one was specified or #f
. The initializer is evaluated in an
environment in which all previous formal arguments have been bound.
It shall be an error for an <ident>
to appear more than once in a formal-argument-list.
Example:
((lambda x x) 3 4 5 6) => (3 4 5 6) ((lambda (x y #!rest z) z) 3 4 5 6) => (5 6) ((lambda (x y #!optional z #!rest r #!key i (j 1)) (list x y z i: i j: j)) 3 4 5 i: 6 i: 7) => (3 4 5 i: 6 j: 1)
[4.1.6] set!
for unbound toplevel variables is allowed. set! (PROCEDURE ...) ...)
is supported, as CHICKEN implements SRFI-17.
[4.2.1] The cond
form supports SRFI-61.
[4.2.2] It is allowed for initialization values of bindings in a letrec
construct to refer to previous variables in the same set of bindings, so
(letrec ([foo 123] [bar foo] ) bar)
is allowed and returns 123
.
[4.2.3] (begin)
is allowed in non-toplevel contexts and evaluates
to an unspecified value.
[4.2.5] Delayed expressions may return multiple values.
[5.2.2] CHICKEN extends standard semantics by allowing internal definitions
everywhere, and not only at the beginning of a body. A set of internal definitions
is equivalent to a letrec
form enclosing all following expressions
in the body:
(let ([foo 123]) (bar) (define foo 456) (baz foo) )
expands into
(let ([foo 123]) (bar) (letrec ([foo 456]) (baz foo) ) )
[5.2] define
with a single argument is allowed and initializes the toplevel or local binding
to an unspecified value. CHICKEN supports “curried” definitions, where the the variable name
may also be a list specifying a name and a nested lambda list. So
(define ((make-adder x) y) (+ x y))
is equivalent to
(define (make-adder x) (lambda (y) (+ x y)))
[6] CHICKEN provides numerous non-standard procedures. See the manual sections on library units for more information.
[6.2.4] The special IEEE floating-point numbers “+nan”, “+inf” and “-inf” are supported, as is negative zero.
[6.3.4] User defined character names are supported. See
char-name
in User-defined named characters. Characters can be given
in hexadecimal notation using the “#\xXX” syntax where “XX” specifies the
character code. Character codes above 255 are supported and can be read (and are
written) using the “#\uXXXX” and “#\UXXXXXXXX” notations.
Non-standard characters names supported are #\tab
, #\linefeed
, #\return
, #\alarm
,
#\vtab
, #\nul
, #\page
, #\esc
, #\delete
and #\backspace
.
[6.3.5] CHICKEN supports special characters preceded with
a backslash “\” in quoted string
constants. “\n” denotes the newline-character,
“\r” carriage return, “\b”
backspace, “\t” TAB, “\v” vertical TAB, “\a” alarm, “\f” formfeed,
“\xXX” a character with the code XX
in hex and
“\uXXXX” (and “\UXXXXXXXX”) a unicode character with the code XXXX
.
The latter is encoded in UTF-8 format.
The third argument to substring
is optional and defaults to the length
of the string.
[6.4] force
called with an argument that is not a promise returns
that object unchanged. Captured continuations can be safely invoked
inside before- and after-thunks of a dynamic-wind
form and
execute in the outer dynamic context of the dynamic-wind
form.
Implicit non-multival continuations accept multiple values by discarding all
but the first result. Zero values result in the continuation receiving an
unspecified value. Note that this slight relaxation of the behaviour of
returning mulitple values to non-multival continuations does not apply to
explicit continuations (created with call-with-current-continuation
).
[6.5] The second argument to eval
is optional and
defaults to the value of (interaction-environment)
.
scheme-report-environment
and null-environment
accept
an optional 2nd parameter: if not #f
(which is the default),
toplevel bindings to standard procedures are mutable and new toplevel
bindings may be introduced.
[6.6] The “tilde” character (~
) is automatically expanded in pathnames.
Additionally, if a pathname starts with $VARIABLE...
, then the prefix is replaced
by the value of the given environment variable.
[6.6.1] if the procedures current-input-port
and
current-output-port
are called with an argument (which should
be a port), then that argument is selected as the new current input- and
output-port, respectively. The procedures open-input-file
,
open-output-file
, with-input-from-file
,
with-output-to-file
, call-with-input-file
and
call-with-output-file
accept an optional second (or third)
argument which should be one or more keywords, if supplied. These
arguments specify the mode in which the file is opened. Possible
values are the keywords #:text
, #:binary
or
#:append
.
Allows user-defined extension of external representations. (For more information see the documentation for SRFI-10)
Syntax for keywords. Keywords are symbols that evaluate to themselves, and as such don't have to be quoted.
Specifies a multiline string constant. Anything up to a line equal to
TAG
(or end of file) will be returned as a single string:(define msg #<<END "Hello, world!", she said. END )is equivalent to
(define msg "\"Hello, world!\", she said.")
Similar to
#<<
, but allows substitution of embedded Scheme expressions prefixed with#
and optionally enclosed in{ ... }
. Two consecutive#
s are translated to a single#
:(define three 3) (display #<#EOF This is a simple string with an embedded `##' character and substituted expressions: (+ three 99) ==> #(+ three 99) (three is "#{three}") EOF )prints
This is a simple string with an embedded `#' character and substituted expressions: (+ three 99) ==> 102 (three is "3")
Treated as a commment and ignores everything up the end of the current line. The keywords
#!optional, #!rest
and#!key
are handled separately and returned as normal symbols. The special (self-evaluating) symbol#!eof
is read as the end-of-file object. Note that if this constant appears at top-level in a loaded file, it is indistiguishable from normal end-of-file.
Read the next expression in case-sensitive mode (regardless of the current global setting).
Read the next expression in case-insensitive mode (regardless of the current global setting).
(require-extension ID ...) (use ID ...)This form does all necessary steps to make the libraries or extensions given in
ID ...
available. It loads syntactic extension, if needed and generates code for loading/linking with core library modules or separately installed extensions.use
is just a shorter alias forrequire-extension
. This implementation ofrequire-extension
is compliant to SRFI-55 (see SRFI-55 for more information).During interpretation/evaluation
require-extension
performs one of the following:
- If
ID
names a built in featureschicken srfi-23 srfi-30 srfi-39 srfi-8 srfi-6 srfi-2 srfi-0 srfi-10 srfi-9 srfi-17 srfi-55
, then nothing is done.- If
ID
names one of syntactic extensionschicken-more-macros chicken-ffi-macros
, then this extension will be loaded.- If
ID
names one of the core library units shipped with CHICKEN, then a(load-library 'ID)
will be performed.- If
ID
names an installed extension with thesyntax
orrequire-at-runtime
attribute, then the equivalent of(require-for-syntax 'ID)
is performed.- Otherwise
(require-extension ID)
is equivalent to(require 'ID)
.During compilation one of the following happens instead:
- If
ID
names a built in featureschicken srfi-23 srfi-30 srfi-39 srfi-8 srfi-6 srfi-2 srfi-0 srfi-10 srfi-9 srfi-17 srfi-55
, then nothing is done.- If
ID
names one of syntactic extensionschicken-more-macros chicken-ffi-macros
, then this extension will be loaded at compile-time, making the syntactic extensions available in compiled code.- If
ID
names one of the core library units shipped with CHICKEN, then a(declare (uses ID))
is generated.- If
ID
names an installed extension with thesyntax
orrequire-at-runtime
attribute, then the equivalent of(require-for-syntax 'ID)
is performed.- Otherwise
(require-extension ID)
is equivalent to(require 'ID)
.To make long matters short - just use
require-extension
and it will normally figure everything out for dynamically loadable extensions and core library units.
ID
should be a pure extension name and should not contain any path prefixes (for exampledir/lib...
) is illegal).
ID
may also be a list that designates an extension-specifier. Currently the following extension specifiers are defined:
(srfi NUMBER ...)
is required for SRFI-55 compatibility and is fully implemented(version ID NUMBER)
is equivalent toID
, but checks at compile-time whether the extension namedID
is installed and whether its version is equal or higher thanNUMBER
.NUMBER
may be a string or a number, the comparison is done lexicographically (usingstring>=?
).See also:
set-extension-specifier!
(define-extension NAME CLAUSE ...)This macro simplifies the task of writing extensions that can be linked both statically and dynamically. If encountered in interpreted code or code that is compiled into a shared object (specifically if compiled with the feature
chicken-compile-shared
, done automatically bycsc
when compiling with the-shared
or-dynamic
option) then the code given by clauses if the form(dynamic EXPRESSION ...)is inserted into the output as a
begin
form.If compiled statically (specifically if the feature
chicken-compiled-shared
has not been given), then this form expands into the following:(declare (unit NAME)) (provide 'NAME)and all clauses of the form
(static EXPRESSION ...)all additionally inserted into the expansion.
As a convenience, the clause
(export IDENTIFIER ...)is also allowed and is identical to
(declare (export IDENTIFIER ...))
(unless thedefine-extension
form occurs in interpreted code, in with it is simply ignored).Note that the compiler option
-extension NAME
is equivalent to prefixing the compiled file with(define-extension NAME)
(:optional ARGS DEFAULT)Use this form for procedures that take a single optional argument. If
ARGS
is the empty listDEFAULT
is evaluated and returned, otherwise the first element of the listARGS
. It is an error ifARGS
contains more than one value.(define (incr x . i) (+ x (:optional i 1))) (incr 10) ==> 11 (incr 12 5) ==> 17
(case-lambda (LAMBDA-LIST1 EXP1 ...) ...)SRFI-16. Expands into a lambda that invokes the body following the first matching lambda-list.
(define plus (case-lambda (() 0) ((x) x) ((x y) (+ x y)) ((x y z) (+ (+ x y) z)) (args (apply + args)))) (plus) ==> 9 (plus 1) ==> 1 (plus 1 2 3) ==> 6For more information see the documentation for SRFI-16
(let-optionals ARGS ((VAR1 DEFAULT1) ...) BODY ...) (let-optionals* ARGS ((VAR1 DEFAULT1) ... [RESTVAR]) BODY ...)Binding constructs for optional procedure arguments.
ARGS
should be a rest-parameter taken from a lambda-list.let-optionals
bindsVAR1 ...
to available arguments in parallel, or toDEFAULT1 ...
if not enough arguments were provided.let-optionals*
bindsVAR1 ...
sequentially, so every variable sees the previous ones. If a single variableRESTVAR
is given, then it is bound to any remaining arguments, otherwise it is an error if any excess arguments are provided.(let-optionals '(one two) ((a 1) (b 2) (c 3)) (list a b c) ) ==> (one two 3) (let-optionals* '(one two) ((a 1) (b 2) (c a)) (list a b c) ) ==> (one two one)
(and-let* (BINDING ...) EXP1 EXP2 ...)SRFI-2. Bind sequentially and execute body.
BINDING
can be a list of a variable and an expression, a list with a single expression, or a single variable. If the value of an expression bound to a variable is#f
, theand-let*
form evaluates to#f
(and the subsequent bindings and the body are not executed). Otherwise the next binding is performed. If all bindings/expressions evaluate to a true result, the body is executed normally and the result of the last expression is the result of theand-let*
form. See also the documentation for SRFI-2.
(rec NAME EXPRESSION) (rec (NAME VARIABLE ...) BODY ...)Allows simple definition of recursive definitions.
(rec NAME EXPRESSION)
is equivalent to(letrec ((NAME EXPRESSION)) NAME)
and(rec (NAME VARIABLE ...) BODY ...)
is the same as(letrec ((NAME (lambda (VARIABLE ...) BODY ...))) NAME)
.
(define-values (NAME ...) EXP)Defines several variables at once, with the result values of expression
EXP
.
(fluid-let ((VAR1 X1) ...) BODY ...)Binds the variables
VAR1 ...
dynamically to the valuesX1 ...
during execution ofBODY ...
.
(let-values (((NAME ...) EXP) ...) BODY ...)Binds multiple variables to the result values of
EXP ...
. All variables are bound simultaneously.
(let*-values (((NAME ...) EXP) ...) BODY ...)Binds multiple variables to the result values of
EXP ...
. The variables are bound sequentially.(let*-values (((a b) (values 2 3)) ((p) (+ a b)) ) p) ==> 5
(letrec-values (((NAME ...) EXP) ...) BODY ...)Binds the result values of
EXP ...
to multiple variables at once. All variables are mutually recursive.(letrec-values (((odd even) (values (lambda (n) (if (zero? n) #f (even (sub1 n)))) (lambda (n) (if (zero? n) #t (odd (sub1 n)))) ) ) ) (odd 17) ) ==> #t
(parameterize ((PARAMETER1 X1) ...) BODY ...)Binds the parameters
PARAMETER1 ...
dynamically to the valuesX1 ...
during execution ofBODY ...
. (see also:make-parameter
in Parameters). Note thatPARAMETER
may be any expression that evaluates to a parameter procedure.
(receive (NAME1 ... [. NAMEn]) VALUEEXP BODY ...) (receive VALUEEXP)SRFI-8. Syntactic sugar for
call-with-values
. Binds variables to the result values ofVALUEEXP
and evaluatesBODY ...
.The syntax
(receive VALUEEXP)is equivalent to
(receive _ VALUEEXP _)
(set!-values (NAME ...) EXP)Assigns the result values of expression
EXP
to multiple variables.
(define-constant NAME CONST)Define a variable with a constant value, evaluated at compile-time. Any reference to such a constant should appear textually after its definition. This construct is equivalent to
define
when evaluated or interpreted. Constant definitions should only appear at toplevel. Note that constants are local to the current compilation unit and are not available outside of the source file in which they are defined. Names of constants still exist in the Scheme namespace and can be lexically shadowed. If the value is mutable, then the compiler is careful to preserve its identity.CONST
may be any constant expression, and may also refer to constants defined viadefine-constant
previously. This for should only be used at top-level.
(define-inline (NAME VAR ... [. VAR]) BODY ...) (define-inline NAME EXP)Defines an inline procedure. Any occurrence of
NAME
will be replaced byEXP
or(lambda (VAR ... [. VAR]) BODY ...)
. This is similar to a macro, but variable-names and -scope will be correctly handled. Inline substitutions take place after macro-expansion.EXP
should be a lambda-expression. Any reference toNAME
should appear textually after its definition. Note that inline procedures are local to the current compilation unit and are not available outside of the source file in which they are defined. Names of inline procedures still exist in the Scheme namespace and can be lexically shadowed. This construct is equivalent todefine
when evaluated or interpreted. Inline definitions should only appear at toplevel.
(define-macro (NAME VAR ... [. VAR]) EXP1 ...) (define-macro NAME (lambda (VAR ... [. VAR]) EXP1 ...)) (define-macro NAME1 NAME2)Define a globally visible macro special form. The macro is available as soon as it is defined, i.e. it is registered at compile-time. If the file containing this definition invokes
eval
and the declarationrun-time-macros
(or the command line option-run-time-macros
) has been used, then the macro is visible in evaluated expressions during runtime. The second possible syntax fordefine-macro
is allowed for portability purposes only. In this case the second argument must be a lambda-expression or a macro name. Only global macros can be defined using this form.(define-macro NAME1 NAME2)
simply copies the macro definition fromNAME2
toNAME1
, creating an alias.Extended lambda list syntax (
#!optional
, etc.) can be used but note that arguments are source expressions and thus default values for optional or keyword arguments should take this into consideration.
(define-for-syntax (NAME VAR ... [. VAR]) EXP1 ...) (define-for-syntax NAME [VALUE])Defines the toplevel variable
NAME
at macro-expansion time. This can be helpful when you want to define support procedures for use in macro-transformers, for example.
(switch EXP (KEY EXP1 ...) ... [(else EXPn ...)])This is similar to
case
, but a) only a single key is allowed, and b) the key is evaluated.
(define-record NAME SLOTNAME ...)Defines a record type. Call
make-NAME
to create an instance of the structure (with one initialization-argument for each slot).(NAME? STRUCT)
tests any object for being an instance of this structure. Slots are accessed via(NAME-SLOTNAME STRUCT)
and updated using(NAME-SLOTNAME-set!
STRUCT
VALUE)
.(define-record point x y) (define p1 (make-point 123 456)) (point? p1) ==> #t (point-x p1) ==> 123 (point-y-set! p1 99) (point-y p1) ==> 99
(define-record-printer (NAME RECORDVAR PORTVAR) BODY ...) (define-record-printer NAME PROCEDURE)Defines a printing method for record of the type
NAME
by associating a procedure with the record type. When a record of this type is written usingdisplay, write
or(define-record foo x y z) (define f (make-foo 1 2 3)) (define-record-printer (foo x out) (fprintf out "#,(foo ~S ~S ~S)" (foo-x x) (foo-y x) (foo-z x)) ) (define-reader-ctor 'foo make-foo) (define s (with-output-to-string (lambda () (write f)))) s ==> "#,(foo 1 2 3)" (equal? f (with-input-from-string s read))) ==> #t
define-record-printer
works also with SRFI-9 record types.
(define-record-type NAME (CONSTRUCTOR TAG ...) PREDICATE (FIELD ACCESSOR [MODIFIER]) ...)SRFI-9 record types. For more information see the documentation for SRFI-9.
(assert EXP [STRING ARG ...])Signals an error if
EXP
evaluates to false. An optional messageSTRING
and argumentsARG ...
may be supplied to give a more informative error-message. If compiled in unsafe mode (either by specifying the-unsafe
compiler option or by declaring(unsafe)
), then this expression expands to an unspecified value. The result is the value ofEXP
.
(cond-expand FEATURE-CLAUSE ...)SRFI-0. Expands by selecting feature clauses. Predefined feature-identifiers are
srfi-0
,srfi-2
,srfi-6
,srfi-8
,srfi-9
,srfi-10
, andchicken
. If the source file containing this form is currently compiled, the featurecompiling
is defined. For further information, see the documentation for SRFI-0 This form is allowed to appear in non-toplevel expressions.
(ensure PREDICATE EXP [ARGUMENTS ...])Evaluates the expression
EXP
and applies the one-argument procedurePREDICATE
to the result. If the predicate returns#f
an error is signaled, otherwise the result ofEXP
is returned. If compiled in unsafe mode (either by specifying the-unsafe
compiler option or by declaring(unsafe)
), then this expression expands to an unspecified value. If specified, the optionalARGUMENTS
are used as arguments to the invocation of the error-signalling code, as in(error ARGUMENTS ...)
. If noARGUMENTS
are given, a generic error message is displayed with the offending value andPREDICATE
expression.
(eval-when (SITUATION ...) EXP ...)Controls evaluation/compilation of subforms.
SITUATION
should be one of the symbolseval
,compile
orload
. When encountered in the evaluator, and the situation specifiereval
is not given, then this form is not evaluated and an unspecified value is returned. When encountered while compiling code, and the situation specifiercompile
is given, then this form is evaluated at compile-time. When encountered while compiling code, and the situation specifierload
is not given, then this form is ignored and an expression resulting into an unspecified value is compiled instead.The following table should make this clearer:
in compiled code In interpreted code eval
ignore evaluate compile
evaluate at compile time ignore load
compile as normal ignore The situation specifiers
compile-time
andrun-time
are also defined and have the same meaning ascompile
andload
, respectively.
(include STRING)Include toplevel-expressions from the given source file in the currently compiled/interpreted program. If the included file has the extension
.scm
, then it may be omitted. The file is searched in the current directory and, if not found, in all directories specified in the-include-path
option.
(nth-value N EXP)Returns the
N
th value (counting from zero) of the values returned by expressionEXP
.
(time EXP1 ...)Evaluates
EXP1 ...
and prints elapsed time and some values about GC use, like time spent in major GCs, number of minor and major GCs.
(This description has been taken mostly from Andrew Wright's postscript document)
Pattern matching allows complicated control decisions based on data
structure to be expressed in a concise manner. Pattern matching is
found in several modern languages, notably Standard ML, Haskell and
Miranda. These syntactic extensions internally use the match
library unit.
The basic form of pattern matching expression is:
(match exp [pat body] ...)
where exp
is an expression, pat
is a pattern, and
body
is one or more expressions
(like the body of a lambda-expression).
The match
form matches its first subexpression against a sequence
of patterns, and branches to the body
corresponding to the first pattern successfully matched.
For example, the following code defines the usual map
function:
(define map (lambda (f l) (match l [() ()] [(x . y) (cons (f x) (map f y))])))
The first pattern ()
matches the empty list. The second pattern
(x . y)
matches a pair, binding x
to the first component of
the pair and y
to the second component of the pair.
The complete syntax of the pattern matching expressions follows:
exp ::= (match exp clause ...) | (match-lambda clause ...) | (match-lambda* clause ...) | (match-let ([pat exp] ...) body) | (match-let* ([pat exp] ...) body) | (match-letrec ([pat exp] ...) body) | (match-let var ([pat exp] ...) body) | (match-define pat exp) clause ::= [pat body] | [pat (=> identifier) body] pat ::= identifier matches anything, and binds identifier as a variable | _ anything | () itself (the empty list) | #t itself | #f itself | string an `equal?' string | number an `equal?' number | character an `equal?' character | 's-expression an `equal?' s-expression | (pat-1 ... pat-n) a proper list of n elements | (pat-1 ... pat-n . pat-n+1) a list of n or more elements | (pat-1 ... pat-n pat-n+1 ..k) a proper list of n+k or more elements [1] | #(pat-1 ... pat-n) a vector of n elements | #(pat-1 ... pat-n pat-n+1 ..k) a vector of n+k or more elements | ($ struct pat-1 ... pat-n) a structure | (= field pat) a field of a structure | (and pat-1 ... pat-n) if all of pat-1 through pat-n match | (or pat-1 ... pat-n) if any of pat-1 through pat-n match | (not pat-1 ... pat-n) if none of pat-1 through pat-n match | (? predicate pat-1 ... pat-n) if predicate true and pat-1 through pat-n all match | (set! identifier) anything, and binds identifier as a setter | (get! identifier) anything, and binds identifier as a getter | `qp a quasipattern qp ::= () itself (the empty list) | #t itself | #f itself | string an `equal?' string | number an `equal?' number | character an `equal?' character | symbol an `equal?' symbol | (qp-1 ... qp-n) a proper list of n elements | (qp-1 ... qp-n . qp-n+1) a list of n or more elements | (qp-1 ... qp-n qp-n+1 ..k) a proper list of n+k or more elements | #(qp-1 ... qp-n) a vector of n elements | #(qp-1 ... qp-n qp-n+1 ..k) a vector of n+k or more elements | ,pat a pattern | ,@pat a pattern, spliced
The notation ..k
denotes a keyword consisting of
three consecutive dots (ie., “...
”),
or two dots and an non-negative integer (eg., “..1
”, “..2
”),
or three consecutive underscores (ie., “___
”),
or two underscores and a non-negative integer.
The keywords “..k
” and “__ k
” are equivalent.
The keywords “...
”, “___
”, “..0
”, and “__0
”
are equivalent.
The next subsection describes the various patterns.
The match-lambda
and match-lambda*
forms are convenient
combinations of match
and lambda
, and can be explained
as follows:
(match-lambda [pat body] ...) = (lambda (x) (match x [pat body] ...)) (match-lambda* [pat body] ...) = (lambda x (match x [pat body] ...))
where x
is a unique variable.
The match-lambda
form is convenient when defining a single argument
function that immediately destructures its argument.
The match-lambda*
form constructs a function that accepts any number
of arguments; the patterns of match-lambda*
should be lists.
The match-let
, match-let*
, match-letrec
,
and match-define
forms generalize
Scheme's let
, let*
, letrec
, and define
expressions to allow
patterns in the binding position rather than just variables.
For example, the following expression:
(match-let ([(x y z) (list 1 2 3)]) body ...)
binds x
to 1, y
to 2, and z
to 3 in body ...
.
These forms are convenient for destructuring the result
of a function that returns multiple values as a list or vector.
As usual for letrec
and define
,
pattern variables bound by match-letrec
and match-define
should not be used in computing the bound value.
The match
, match-lambda
, and match-lambda*
forms
allow the optional syntax (=> identifier)
between the pattern
and the body of a clause. When the pattern match for such a clause
succeeds, the identifier
is bound to a `failure
procedure' of zero arguments within the body
. If this
procedure is invoked, it jumps back to the pattern matching
expression, and resumes the matching process as if the pattern had
failed to match. The body
must not mutate the object being
matched, otherwise unpredictable behavior may result.
identifier
: (excluding the reserved names
?
, ,
, =
, _
, and
, or
, not
, set!
, get!
, ...
, and
..k
for non-negative integers k
)
matches anything, and binds a variable of this name to
the matching value in the body
.
_
:
matches anything, without binding any variables.
()
, #t
, #f
, string
, number
,
character
, 's-expression
:
These constant patterns match themselves, i.e.,
the corresponding value must be equal?
to the pattern.
(pat-1 ... pat-n)
:
matches a proper list of n
elements
that match pat-1
through pat-n
.
(pat-1 ... pat-n . pat-n+1)
:
matches a (possibly improper) list of at least n
elements that ends in
something matching pat-n+1
.
(pat-1 ... pat-n pat-n+1 ...)
:
matches a proper list of n
or more elements, where
each element of the tail matches pat-n+1
. Each pattern variable in
pat-n+1
is bound to a list of the matching values. For example,
the expression:
(match '(let ([x 1][y 2]) z) [('let ((binding values) ...) exp) body])
binds binding
to the list '(x y)
,
values
to the list \'(1 2)
,
and exp
to 'z
in the body of the match
-expression.
For the special case where pat-n+1
is a pattern variable, the list
bound to that variable may share with the matched value.
(pat-1 ... pat-n pat-n+1 ___)
:
This pattern means the same thing as the previous pattern.
(pat-1 ... pat-n pat-n+1 ..k)
:
This pattern is similar to the previous pattern, but the tail must be
at least k
elements long.
The pattern keywords ..0
and ...
are equivalent.
(pat-1 ... pat-n ~ pat-n+1 __k)
:
This pattern means the same thing as the previous pattern.
#(pat-1 ... pat-n)
:
matches a vector of length n
, whose elements match
pat-1
through pat-n
.
#(pat-1 ... pat-n pat-n+1 ...)
:
matches a vector of length n
or more, where each element
beyond n
matches pat-n+1
.
#(pat-1 ... pat-n pat-n+1 ..k)
:
matches a vector of length n+k
or more, where each element
beyond n
matches pat-n+1
.
($ struct pat-1 ... pat-n)
:
matches a structure
declared with define-record
or define-record-type
.
(= field pat)
:
is intended for selecting a field from a structure. “field” may be
any expression; it is applied to the value being matched, and the
result of this application is matched against pat
.
(and pat-1 ... pat-n)
:
matches if all of the subpatterns match.
At least one subpattern must be present.
This pattern is often used as (and x pat)
to bind x
to
to the entire value that matches pat
(cf. “as-patterns” in ML or Haskell).
(or pat-1 ... pat-n)
:
matches if any of the subpatterns match.
At least one subpattern must be present.
All subpatterns must bind the same set of pattern variables.
(not pat-1 ... pat-n)
:
matches if none of the subpatterns match.
At least one subpattern must be present.
The subpatterns may not bind any pattern variables.
(? predicate pat-1 ... pat-n)
:
In this pattern,
predicate
must be an expression evaluating to a single argument
function.
This pattern matches if predicate
applied to the corresponding value
is true, and the subpatterns pat-1 ... pat-n
all match.
The predicate
should not have side effects, as
the code generated by the pattern matcher may invoke predicates repeatedly
in any order.
The predicate
expression is bound in the same scope as the
match expression, i.e.,
free variables in predicate
are not bound by pattern variables.
(set! identifier)
:
matches anything, and binds identifier
to a procedure of one argument that mutates the corresponding field of
the matching value.
This pattern must be nested within a pair, vector, box, or structure
pattern. For example, the expression:
(define x (list 1 (list 2 3))) (match x [(_ (_ (set! setit))) (setit 4)])
mutates the cadadr
of x
to 4, so that x
is
'(1 (2 4))
.
(get! identifier)
:
matches anything, and binds identifier
to a procedure of zero arguments that accesses the corresponding field of
the matching value. This pattern is the complement to set!
.
As with set!
,
this pattern must be nested within a pair, vector, box, or structure
pattern.
Quasipatterns:
Quasiquote introduces a quasipattern, in which identifiers are considered
to be symbolic constants. Like Scheme's quasiquote for data,
unquote
(,) and unquote-splicing
(,@) escape back to
normal patterns.
If no clause matches the value, the default action
is to invoke the procedure ##sys#match-error
with the value that did not match. The default definition of
##sys#match-error
calls error
with an appropriate message:
#;1> (match 1 (2 2)) Failed match: Error: no matching clause for : 1
For most situations, this behavior is adequate, but it can be changed
by altering the value of the parameter match-error-control
:
(match-error-control [MODE])Selects a mode that specifies how
match...
macro forms are to be expanded. With no argument this procedure returns the current mode. A single argument specifies the new mode that decides what should happen if no match-clause applies. The following modes are supported:
#:error
- Signal an error. This is the default.
#:match
- Signal an error and output the offending form.
#:fail
- Omits
pair?
tests when the consequence is to fail incar
orcdr
rather than to signal an error.#:unspecified
- Non-matching expressions will either fail in
car
orcdr
or return an unspecified value. This mode applies to files compiled with theunsafe
option or declaration.When an error is signalled, the raised exception will be of kind
(exn match)
.
Note: the $
pattern handles native record structures and SRFI-9 records transparently.
Currently it is required that SRFI-9 record predicates are named exactly like the record type
name, followed by a ?
(question mark) character.
Pattern matching macros are compiled into if
-expressions
that decompose the value being matched with standard
Scheme procedures, and test the components with standard predicates.
Rebinding or lexically shadowing the names of any of these procedures
will change the
semantics of the match
macros. The names that should not be
rebound or shadowed are:
null? pair? number? string? symbol? boolean? char? procedure? vector? list? equal? car cdr cadr cdddr ... vector-length vector-ref reverse length call/cc
Additionally, the code generated to match a structure pattern like
($ Foo pat-1 ... pat-n)
refers to the name Foo?
.
This name also should not be shadowed.
(declare DECLSPEC ...)Process declaration specifiers. Declarations always override any command-line settings. Declarations are valid for the whole compilation-unit (source file), the position of the declaration in the source file can be arbitrary. Declarations are ignored in the interpreter but not in code evaluated at compile-time (by
eval-when
or in syntax extensions loaded viarequire-extension
orrequire-for-syntax
.DECLSPEC
may be any of the following:— declaration specifier: always-bound
(always-bound SYMBOL ...)Declares that the given variables are always bound and accesses to those have not to be checked.
— declaration specifier: block
(block)Assume global variables are never redefined. This is the same as specifying the
-block
option.— declaration specifier: block-global
— declaration specifier: hide
(block-global SYMBOL ...) (hide SYMBOL ...)Declares that the toplevel bindings for
SYMBOL ...
should not be accessible from code in other compilation units or byeval
. Access to toplevel bindings declared as block global is also more efficient.— declaration specifier: bound-to-procedure
(bound-to-procedure SYMBOL ...)Declares that the given identifiers are always bound to procedure values.
— declaration specifier: c-options
(c-options STRING ...)Declares additional C/C++ compiler options that are to be passed to the subsequent compilation pass that translates C to machine code. This declaration will only work if the source file is compiled with the
csc
compiler driver.— declaration specifier: check-c-syntax
(check-c-syntax) (not check-c-syntax)Enables or disables syntax-checking of embedded C/C++ code fragments. Checking C syntax is the default.
— declaration specifier: compress-literals
(compress-literals [THRESHOLD [INITIALIZER]])The same as the
-compress-literals
compiler option. The threshold argument defaults to 50. If the optional argumentINITIALIZER
is given, then the literals will not be created at module startup, but when the procedure with this name will be called.— declaration specifier: constant
(constant SYMBOL ...)Declares the procedures with the names
SYMMBOL ...
as constant, that is, as not having any side effects. This can help the compiler to remove non-side-effecting expressions.— declaration specifier: export
(export SYMBOL ...)The opposite of
hide
. All given identifiers will be exported and all toplevel variables not listed will be hidden and not be accessible outside of this compilation unit.— declaration-specifier: emit-exports
(emit-exports STRING)Write exported toplevel variables to file with name
STRING
.— declaration specifier: emit-external-prototypes-first
(emit-external-prototypes-first)Emit prototypes for callbacks defined with
define-external
before any other foreign declarations. Equivalent to giving the-emit-external-prototypes-first
option to the compiler.— declaration specifier: disable-interrupts
(disable-interrupts) (not interrupts-enabled)Disable timer-interrupts checks in the compiled program. Threads can not be preempted in main- or library-units that contain this declaration.
— declaration specifier: disable-warning
(disable-warning CLASS ...)Disable warnings of type
CLASS ...
(equivalent to the-disable-warning CLASS
compiler option).— declaration specifier: import
(import SYMBOL-OR-STRING ...)Adds new imports to the list of externally available toplevel variables. Arguments to this declaration may be either strings (designating
.exports
files, without the file-extension) or symbols which directly designate imported variables.— declaration specifier: inline
(inline) (not inline) (inline IDENTIFIER ...) (not inline IDENTIFIER ...)If given without an identifier-list, inlining of known procedures is enabled (this is equivalent to the
-inline
compiler option). When an identifier-list is given, then inlining is enabled only for the specified global procedures. The negated forms(not inline)
and(not inline IDENTIFIER)
disable global inlining, or inlining for the given global procedures only, respectively.— declaration specifier: inline-limit
(inline-limit THRESHOLD)Sets the maximum size of procedures which may potentially be inlined. The default threshold is
10
.— declaration specifier: interrupts-enabled
(interrupts-enabled)Enable timer-interrupts checks in the compiled program (the default).
— declaration specifier: keep-shadowed-macros
(keep-shadowed-macros)Normally, when a toplevel variable is assigned or defined that has the same name as a macro, the macro-definition will be removed (in addition to showing a warning). This declaration will disable the removal of the macro.
— declaration specifier: lambda-lift
(lambda-lift)Enables lambda-lifting (equivalent to the
-lambda-lift
option).— declaration specifier: link-options
(link-options STRING ...)Declares additional linker compiler options that are to be passed to the subsequent compilation pass that links the generated code into an executable or library. This declaration will only work if the source file is compiled with the
csc
compiler driver.— declaration specifier: no-bound-checks
(no-bound-checks)Disables the bound-checking of toplevel bindings.
— declaration specifier: no-procedure-checks
(no-procedure-checks)Disables checking of values in operator position for being of procedure type.
— declaration specifier: post-process
(post-process STRING ...)Arranges for the shell commands
STRING ...
to be invoked after the current file has been translated to C. Any occurrences of the substring$@
in the strings given for this declaration will be replaced by the pathname of the currently compiled file, without the file-extension. This declaration will only work if the source file is compiled with thecsc
compiler driver.— declaration specifier: TYPE
— declaration specifier: fixnum-arithmetic
([number-type] TYPE) (fixnum-arithmetic)Declares that only numbers of the given type are used.
TYPE
may befixnum
orgeneric
(which is the default).— declaration specifier: run-time-macros
(run-time-macros)Equivalent to the compiler option of the same name - macros defined in the compiled code are also made available at runtime.
— declaration specifier: standard-bindings
(standard-bindings SYMBOL ...) (not standard-bindings SYMBOL ...)Declares that all given standard procedures (or all if no symbols are specified) are never globally redefined. If
not
is specified, then all but the given standard bindings are assumed to be never redefined.— declaration specifier: extended-bindings
(extended-bindings SYMBOL ...) (not extended-bindings SYMBOL ...)Declares that all given non-standard and CHICKEN-specific procedures (or all if no symbols are specified) are never globally redefined. If
not
is specified, then all but the given extended bindings are assumed to be never redefined.— declaration specifier: usual-integrations
(usual-integrations SYMBOL ...) (not usual-integrations SYMBOL ...)Declares that all given standard and extended bindings (or all if no symbols are specified) are never globally redefined. If
not
is specified, then all but the given standard and extended bindings are assumed to be never redefined. Note that this is the default behaviour, unless the-no-usual-integrations
option has been given.— declaration specifier: unsafe
(unsafe) (not safe)Do not generate safety-checks. This is the same as specifying the
-unsafe
option. Also implies(declare (no-bound-checks) (no-procedure-checks) (no-argc-checks))— declaration specifier: uses
(uses SYMBOL ...)Gives a list of used library-units. Before the toplevel-expressions of the main-module are executed, all used units evaluate their toplevel-expressions in the order in which they appear in this declaration. If a library unit A uses another unit B, then B's toplevel expressions are evaluated before A's. Furthermore, the used symbols are registered as features during compile-time, so
cond-expand
knows about them.
Certain behavior of the interpreter and compiled programs can be customized via 'parameters', where a parameter is a procedure of zero or one arguments. To retrieve the value of a parameter call the parameter-procedure with zero arguments. To change the setting of the parameter, call the parameter-procedure with the new value as argument:
(define foo (make-parameter 123)) (foo) ==> 123 (foo 99) (foo) ==> 99
Parameters are fully thread-local, each thread of execution owns a local copy of a parameters' value.
CHICKEN implements SRFI-39
(make-parameter VALUE [GUARD])Returns a procedure that accepts zero or one argument. Invoking the procedure with zero arguments returns
VALUE
. Invoking the procedure with one argument changes its value to the value of that argument (subsequent invocations with zero parameters return the new value).GUARD
should be a procedure of a single argument. Any new values of the parameter (even the initial value) are passed to this procedure. The guard procedure should check the value and/or convert it to an appropriate form.
If true, then
read
reads symbols and identifiers in case-sensitive mode and uppercase characters in symbols are printed escaped. Defaults to#t
.
A list of strings containing shared libraries that should be checked for explicitly loaded library units (this facility is not available on all platforms). See
load-library
.
Contains the list of arguments passed to this program, with the name of the program and any runtime options (all options starting with
-:
) removed.
A read-table object that holds read-procedures for special non-standard read-syntax (see
set-read-syntax!
for more information).
A procedure of a single optional argument. When
exit
is called, then this procedure will be invoked with the exit-code as argument. The default behavior is to terminate the program.
A procedure of one or two arguments. When
eval
is invoked, it calls the value of this parameter with the same arguments. The default behavior is to evaluate the argument expression and to ignore the second parameter.
If true, force and execute all pending finalizers before exiting the program (either explicitly by
exit
or implicitly when the last toplevel expression has been executed). Default is#t
.
A procedure of no arguments. When the last toplevel expression of the program has executed, then the value of this parameter is called. The default behaviour is to invoke all pending finalizers.
Enables alternative keyword syntax, where
STYLE
may be either#:prefix
(as in Common Lisp) or#:suffix
(as in DSSSL). Any other value disables the alternative syntaxes.
A boolean indicating whether loading of source files, compiled code (if available) and compiled libraries should display a message.
A procedure that should evaluate to a string that will be printed before reading interactive input from the user in a read-eval-print loop. Defaults to
(lambda () "#;N> ")
.
A procedure of zero arguments that is called via
reset
. The default behavior in compiled code is to invoke the value of(exit-handler)
. The default behavior in the interpreter is to abort the current computation and to restart the read-eval-print loop.
This unit contains basic Scheme definitions. This unit is used by default, unless the program
is compiled with the -explicit-use
option.
(bitwise-and N1 ...) (bitwise-ior N1 ...) (bitwise-xor N1 ...) (bitwise-not N) (arithmetic-shift N1 N2)Binary integer operations.
arithmetic-shift
shifts the argumentN1
byN2
bits to the left. IfN2
is negative, thanN1
is shifted to the right. These operations only accept exact integers or inexact integers in word range (32 bit signed on 32-bit platforms, or 64 bit signed on 64-bit platforms).
(fx+ N1 N2) (fx- N1 N2) (fx* N1 N2) (fx/ N1 N2) (fxmod N1 N2) (fxneg N) (fxmin N1 N2) (fxmax N1 N2) (fx= N1 N2) (fx> N1 N2) (fx< N1 N2) (fx>= N1 N2) (fx<= N1 N2) (fxand N1 N2) (fxior N1 N2) (fxxor N1 N2) (fxnot N) (fxshl N1 N2) (fxshr N1 N2)Arithmetic fixnum operations. These procedures do not check their arguments, so non-fixnum parameters will result in incorrect results.
fxneg
negates its argument.On division by zero,
fx/
andfxmod
signal a condition of kind(exn arithmetic)
.
fxshl
andfxshr
perform arithmetic shift left and right, respectively.
(fp+ N1 N2) (fp- N1 N2) (fp* N1 N2) (fp/ N1 N2) (fpneg N) (fpmin N1 N2) (fpmax N1 N2) (fp= N1 N2) (fp> N1 N2) (fp< N1 N2) (fp>= N1 N2) (fp<= N1 N2)Arithmetic floating-point operations. These procedures do not check their arguments, so non-flonum parameters will result in incorrect results. On division by zero,
fp/
signals a condition of kind(exn arithmetic)
.
(signum N)Returns
1
ifN
is positive,-1
ifN
is negative or0
ifN
is zero.signum
is exactness preserving.
(current-error-port [PORT])Returns default error output port. If
PORT
is given, then that port is selected as the new current error output port.
(flush-output [PORT])Write buffered output to the given output-port.
PORT
defaults to the value of(current-output-port)
.
(port-name PORT)Fetch filename from
PORT
. This returns the filename that was used to open this file. Returns a special tag string, enclosed into parentheses for non-file ports.
(port-position PORT)Returns the current position of
PORT
as two values: row and column number. If the port does not support such an operation an error is signaled. This procedure is currently only available for input ports.
(delete-file STRING)Deletes the file with the pathname
STRING
. If the file does not exist, an error is signaled.
(file-exists? STRING)Returns
STRING
if a file with the given pathname exists, or#f
otherwise.
(rename-file OLD NEW)Renames the file or directory with the pathname
OLD
toNEW
. If the operation does not succeed, an error is signaled.
(get-output-string PORT)Returns accumulated output of a port created with
(open-output-string)
.
(open-output-string)Returns a port for accumulating output in a string.
CHICKEN maintains a global list of “features” naming functionality available
int the current system. Additionally the cond-expand
form accesses this
feature list to infer what features are provided. Predefined features are
chicken
, and the SRFIs (Scheme Request For Implementation) provided by the
base system: srfi-23, srfi-30, srfi-39
. If the eval
unit
is used (the default), the features srfi-0, srfi-2, srfi-6, srfi-8, srfi-9
and srfi-10
are defined. When compiling code (during compile-time) the
feature compiling
is registered. When evaluating code in the interpreter
(csi), the feature csi
is registered.
(features)Returns a list of all registered features that will be accepted as valid feature-identifiers by
cond-expand
.
(feature? ID ...)Returns
#t
if all features with the given feature-identifiersID ...
are registered.
(register-feature! FEATURE ...)Register one or more features that will be accepted as valid feature-identifiers by
cond-expand
.FEATURE ...
may be a keyword, string or symbol.
(unregister-feature! FEATURE ...)Unregisters the specified feature-identifiers.
FEATURE ...
may be a keyword, string or symbol.
Keywords are special symbols prefixed with #:
that evaluate
to themselves. Procedures can use keywords to accept optional named
parameters in addition to normal required parameters. Assignment to
and bindings of keyword symbols is not allowed.
The parameter keyword-style
and the compiler/interpreter option
-keyword-style
can be used to allow an additional keyword
syntax, either compatible to Common LISP, or to DSSSL.
(get-keyword KEYWORD ARGLIST [THUNK])Returns the argument from
ARGLIST
specified under the keywordKEYWORD
. If the keyword is not found, then the zero-argument procedureTHUNK
is invoked and the result value is returned. IfTHUNK
is not given,#f
is returned.(define (increase x . args) (+ x (get-keyword #:amount args (lambda () 1))) ) (increase 123) ==> 124 (increase 123 #:amount 10) ==> 133Note: the
KEYWORD
may actually be any kind of object.
CHICKEN implements the (currently withdrawn) SRFI-12 exception system. For more information, see the SRFI-12 document
(condition-case EXPRESSION CLAUSE ...)Evaluates
EXPRESSION
and handles any exceptions that are covered byCLAUSE ...
, whereCLAUSE
should be of the following form:CLAUSE = ([VARIABLE] (KIND ...) BODY ...)If provided,
VARIABLE
will be bound to the signalled exception object.BODY ...
is executed when the exception is a property- or composite condition with the kinds givenKIND ...
(unevaluated). If no clause applies, the exception is re-signalled in the same dynamic context as thecondition-case
form.(define (check thunk) (condition-case (thunk) [(exn file) (print "file error")] [(exn) (print "other error")] [var () (print "something else")] ) ) (check (lambda () (open-input-file ""))) ; -> "file error" (check (lambda () some-unbound-variable)) ; -> "othererror" (check (lambda () (signal 99))) ; -> "something else" (condition-case some-unbound-variable [(exn file) (print "ignored)] ) ; -> signals error
(breakpoint [NAME])Programmatically triggers a breakpoint (similar to the
,br
top-level csi command).
All error-conditions signalled by the system are of kind exn
.
The following composite conditions are additionally defined:
(exn arity)
(exn type)
(exn arithmetic)
(exn i/o)
(exn i/o file)
(exn i/o net)
(exn bounds)
(exn runtime)
(exn runtime limit)
(exn match)
match
).
(exn syntax)
(exn breakpoint)
Notes:
exn
) are non-continuable.
exn
kind have additional
arguments
and location
properties that contain the
arguments passed to the exception-handler and the name of the procedure
where the error occurred (if available).
posix
unit is available and used, then a
user-interrupt (signal/int
) signals an exception of the kind
user-interrupt
.
condition-property-accessor
accepts an optional
third argument. If the condition does not have a value for the desired property
and if the optional argument is given, no error is signalled and
the accessor returns the third argument.
(argv)Return a list of all supplied command-line arguments. The first item in the list is a string containing the name of the executing program. The other items are the arguments passed to the application. This list is freshly created on every invocation of
(argv)
. It depends on the host-shell whether arguments are expanded ('globbed') or not.
(exit [CODE])Exit the running process and return exit-code, which defaults to 0 (Invokes
exit-handler
).
(build-platform)Returns a symbol specifying the toolset which has been used for building the executing system, which is one of the following:
cygwin msvc mingw32 gnu metrowerks intel watcom unknown
(chicken-version [FULL])Returns a string containing the version number of the CHICKEN runtime system. If the optional argument
FULL
is given and true, then a full version string is returned.
(getenv STRING)Returns the value of the environment variable
STRING
or#f
if that variable is not defined.
(machine-byte-order)Returns the symbol
little-endian
orbig-endian
, depending on the machine's byte-order.
(machine-type)Returns a symbol specifying the processor on which this process is currently running, which is one of the following:
alpha mips hppa ultrasparc sparc ppc ia64 x86 x86-64 unknown
(software-type)Returns a symbol specifying the operating system on which this process is currently running, which is one of the following:
windows unix macos ecos unknown
(software-version)Returns a symbol specifying the operating system version on which this process is currently running, which is one of the following:
linux freebsd netbsd openbsd macosx hpux solaris sunos unknown
(c-runtime)Returns a symbol that designates what kind of C runtime library has been linked with this version of the Chicken libraries. Possible return values are
static
,dynamic
orunknown
. On systems not compiled with the Microsoft C compiler,c-runtime
always returnsunknown
.
(chicken-home)Returns a string given the installation directory (usually
/usr/local/share/chicken
on UNIX-like systems). If the environment variableCHICKEN_HOME
is set, then its value will be returned. As a last option, if the environment variableCHICKEN_PREFIX
is set, thenchicken-home
will return$CHICKEN_PREFIX/share
.
(system STRING)Execute shell command. The functionality offered by this procedure depends on the capabilities of the host shell.
(cpu-time)Returns the used CPU time of the current process in milliseconds as two values: the time spent in user code, and the time spent in system code. On platforms where user and system time can not be differentiated, system time will be always be 0.
(current-milliseconds)Returns the number of milliseconds since process- or machine startup.
(current-seconds)Returns the number of seconds since midnight, Jan. 1, 1970.
(current-gc-milliseconds)Returns the number of milliseconds spent in major garbage collections since the last call of
current-gc-milliseconds
and returns an exact integer.
(enable-warnings [BOOL])Enables or disables warnings, depending on wether
BOOL
is true or false. If called with no arguments, this procedure returns#t
if warnings are currently enabled, or#f
otherwise. Note that this is not a parameter. The current state (wether warnings are enabled or disabled) is global and not thread-local.
(error [LOCATION] STRING EXP ...)Prints error message, writes all extra arguments to the value of
(current-error-port)
and invokes the current exception-handler. This conforms to SRFI-23. IfLOCATION
is given and a symbol, it specifies the “location” (the name of the procedure) where the error occurred.
(get-call-chain [START [THREAD]])Returns a list with the call history. Backtrace information is only generated in code compiled without
-no-trace
and evaluated code. If the optional argumentSTART
is given, the backtrace starts at this offset, i.e. whenSTART
is 1, the next to last trace-entry is printed, and so on. If the optional argumentTHREAD
is given, then the call-chain will only be constructed for calls performed by this thread.
(print-call-chain [PORT [START [THREAD]]])Prints a backtrace of the procedure call history to
PORT
, which defaults to(current-output-port)
.
(print-error-message EXN [PORT [STRING]])Prints an appropriate error message to
PORT
(which defaults to the value of(current-output-port)
for the objectEXN
.EXN
may be a condition, a string or any other object. If the optional argumentSTRING
is given, it is printed before the error-message.STRING
defaults to"Error:"
.
(procedure-information PROC)Returns an s-expression with debug information for the procedure
PROC
, or#f
, ifPROC
has no associated debug information.
(warning STRING EXP ...)Displays a warning message (if warnings are enabled with
enable-warnings
) and continues execution.
(singlestep THUNK)Executes the code in the zero-procedure
THUNK
in single-stepping mode.
(gc [FLAG])Invokes a garbage-collection and returns the number of free bytes in the heap. The flag specifies whether a minor (
#f
) or major (#t
) GC is to be triggered. If no argument is given,#t
is assumed. When the argument is#t
, all pending finalizers are executed.
(memory-statistics)Performs a major garbage collection and returns a three element vector containing the total heap size in bytes, the number of bytes currently used and the size of the nursery (the first heap generation). Note that the actual heap is actually twice the size given in the heap size, because CHICKEN uses a copying semi-space collector.
(set-finalizer! X PROC)Registers a procedure of one argument
PROC
, that will be called as soon as the non-immediate data objectX
is about to be garbage-collected (with that object as its argument). Note that the finalizer will not be called when interrupts are disabled. This procedure returnsX
.
(set-gc-report! FLAG)Print statistics after every GC, depending on
FLAG
. A value of#t
shows statistics after every major GC. A true value different from#t
shows statistics after every minor GC.#f
switches statistics off.
(andmap PROC LIST1 ...)Repeatedly calls
PROC
with arguments taken fromLIST1 ...
. If any invocation should return#f
, the result ofandmap
is#f
. If all invocations return a true result, then the result ofandmap
is#t
.
(ormap PROC LIST1 ...)Repeatedly calls
PROC
with arguments taken fromLIST1 ...
. If any invocation should return a value different from#f
, then this value is returned as the result oformap
. If all invocations return #f, then the result oformap
is#f
.
(reverse-list->string LIST)Returns a string with the characters in
LIST
in reverse order. This is equivalent to(list->string (reverse LIST))
, but much more efficient.
(gensym [STRING-OR-SYMBOL])Returns a newly created uninterned symbol. If an argument is provided, the new symbol is prefixed with that argument.
(string->uninterned-symbol STRING)Returns a newly created, unique symbol with the name
STRING
.
(print EXP1 EXP2 ...)Outputs the arguments
EXP1 EXP2 ...
usingdisplay
and writes a newline character to the port that is the value of(current-output-port)
. Returns its first argument.
(print* EXP1 ...)Similar to
flush-outout
after writing its arguments.
(char-name SYMBOL-OR-CHAR [CHAR])This procedure can be used to inquire about character names or to define new ones. With a single argument the behavior is as follows: If
SYMBOL-OR-CHAR
is a symbol, thenchar-name
returns the character with this name, or#f
if no character is defined under this name. IfSYMBOL-OR-CHAR
is a character, then the name of the character is returned as a symbol, or#f
if the character has no associated name.If the optional argument
CHAR
is provided, thenSYMBOL-OR-CHAR
should be a symbol that will be the new name of the given character. If multiple names designate the same character, then thewrite
will use the character name that was defined last.(char-name 'space) ==> #\space (char-name #\space) ==> space (char-name 'bell) ==> #f (char-name (integer->char 7)) ==> #f (char-name 'bell (integer->char 7)) (char-name 'bell) ==> #\bell (char->integer (char-name 'bell)) ==> 7
(vector-copy! VECTOR1 VECTOR2 [COUNT])Copies contents of
VECTOR1
intoVECTOR2
. If the argumentCOUNT
is given, it specifies the maximal number of elements to be copied. If not given, the minimum of the lengths of the argument vectors is copied.Exceptions:
(exn bounds)
(vector-resize VECTOR N [INIT])Creates and returns a new vector with the contents of
VECTOR
and lengthN
. IfN
is greater than the original length ofVECTOR
, then all additional items are initialized toINIT
. IfINIT
is not specified, the contents are initialized to some unspecified value.
(continuation-capture PROCEDURE)Creates a continuation object representing the current continuation and tail-calls
PROCEDURE
with this continuation as the single argument.More information about this continuation API can be found in the paper http://repository.readscheme.org/ftp/papers/sw2001/feeley.pdf “A Better API for first class Continuations” by Marc Feeley.
(continuation? X)Returns
#t
ifX
is a continuation object, or#f
otherwise.
(continuation-graft CONT THUNK)Calls the procedure
THUNK
with no arguments and the implicit continuationCONT
.
(continuation-return CONT VALUE ...)Returns the value(s) to the continuation
CONT
.continuation-result
could be implemented like this:(define (continuation-return k . vals) (continuation-graft k (lambda () (apply values vals)) ) )
SRFI-17 is fully implemented. For more information see: SRFI-17
(setter PROCEDURE)Returns the setter-procedure of
PROCEDURE
, or signals an error ifPROCEDURE
has no associated setter-procedure.Note that
(set! (setter PROC) ...)
for a procedure that has no associated setter procedure yet is a very slow operation (the old procedure is replaced by a modified copy, which involves a garbage collection).
(getter-with-setter GETTER SETTER)Returns a copy of the procedure
GETTER
with the associated setter procedureSETTER
. Contrary to the SRFI specification, the setter of the returned procedure may be changed.
This unit has support for evaluation and macro-handling. This unit is used
by default, unless the program is compiled with the -explicit-use
option.
(load FILE [EVALPROC])Loads and evaluates expressions from the given source file, which may be either a string or an input port. Each expression read is passed to
EVALPROC
(which defaults toeval
). On platforms that support it (currently native Windows, Linux ELF and Solaris),load
can be used to load compiled programs:% cat x.scm (define (hello) (print "Hello!")) % csc -s x.scm % csi -q #;1> (load "x.so") ; loading x.so ... #;2> (hello) Hello! #;3>The second argument to
load
is ignored when loading compiled code. If source code is loaded from a port, then that port is closed after all expressions have been read.Compiled code can be re-loaded, but care has to be taken, if code from the replaced dynamically loaded module is still executing (i.e. if an active continuation refers to compiled code in the old module).
Support for realoding compiled code dynamically is still experimental.
(load-relative FILE [EVALPROC])Similar to
load
, but loadsFILE
relative to the path of the currently loaded file.
(load-noisily FILE #!key EVALUATOR TIME PRINTER)As
load
but the result(s) of each evaluated toplevel-expression is written to standard output. IfEVALUATOR
is given and not#f
, then each expression is evaluated by calling this argument with the read expression as argument. IfTIME
is given and not false, then the execution time of each expression is shown (as with thetime
macro). IfPRINTER
is given and not false, then each expression is printed before evaluation by applying the expression to the value of this argument, which should be a one-argument procedure.
(load-library UNIT [LIBRARYFILE])On platforms that support dynamic loading,
load-library
loads the compiled library unitUNIT
(which should be a symbol). If the stringLIBRARYFILE
is given, then the given shared library will be loaded and the toplevel code of the contained unit will be executed. If noLIBRARYFILE
argument is given, then the following libraries are checked for the required unit:
- a file named “
<UNIT>.so
”- the files given in the parameter
dynamic-load-libraries
If the unit is not found, an error is signaled. When the library unit can be successfully loaded, a feature-identifier named
UNIT
is registered. If the feature is already registered before loading, theload-library
does nothing.
(set-dynamic-load-mode! MODELIST)On systems that support dynamic loading of compiled code via the
dlopen(3)
interface (for example Linux and Solaris), some options can be specified to fine-tune the behaviour of the dynamic linker.MODE
should be a list of symbols (or a single symbol) taken from the following set:
local
Iflocal
is given, then any C/C++ symbols defined in the dynamically loaded file are not available for subsequently loaded files and libraries. Use this if you have linked foreign code into your dynamically loadable file and if you don't want to export them (for example because you want to load another file that defines the same symbols).global
The default isglobal
, which means all C/C++ symbols are available to code loaded at a later stage.now
Ifnow
is specified, all symbols are resolved immediately.lazy
Unresolved symbols are resolved as code from the file is executed. This is the default.Note that this procedure does not control the way Scheme variables are handled - this facility is mainly of interest when accessing foreign code.
(repl)Start a new read-eval-print loop. Sets the
reset-handler
so that any invocation ofreset
restarts the read-eval-print loop. Also changes the current exception-handler to display a message, write any arguments to the value of(current-error-port)
and reset.
(get-line-number EXPR)If
EXPR
is a pair with the car being a symbol, and line-number information is available for this expression, then this procedure returns the associated line number. If line-number information is not available, then#f
is returned. Note that line-number information for expressions is only available in the compiler.
(macroexpand X)If
X
is a macro-form, expand the macro (and repeat expansion until expression is a non-macro form). Returns the resulting expression.
(macroexpand-1 X)If
X
is a macro-form, expand the macro. Returns the resulting expression.
(undefine-macro! SYMBOL)Remove the current macro-definition of the macro named
SYMBOL
.
(syntax-error [LOCATION] MESSAGE ARGUMENT ...)Signals an exception of the kind
(exn syntax)
. Otherwise identical toerror
.
This functionality is only available on platforms that support dynamic loading of compiled code. Currently Linux, BSD, Solaris, Windows (with Cygwin) and HP/UX are supported.
Contains a string naming the path to the extension repository, which defaults to either the value of the environment variable
CHICKEN_REPOSITORY
, the value of the environment variableCHICKEN_HOME
or the default library path (usually/usr/local/lib/chicken
on UNIX systems).
(extension-information ID)If an extension with the name
ID
is installed and if it has a setup-information list registered in the extension repository, then the info-list is returned. Otherwiseextension-information
returns#f
.
(provide ID ...)Registers the extension IDs
ID ...
as loaded. This is mainly intended to provide aliases for certain extension identifiers.
(provided? ID ...)Returns
#t
if the extension with the IDsID ...
are currently loaded, or#f
otherwise. Works also for feature-ids.
(require ID ...) (require-for-syntax ID ...)If the extension library
ID
is not already loaded into the system, thenrequire
will lookup the location of the shared extension library and load it. IfID
names a library-unit of the base system, then it is loaded viaload-library
. If no extension library is available for the given ID, then an attempt is made to load the fileID.so
orID.scm
(in that order) from one of the following locations:
- the current include path, which defaults to the pathnames given in
CHICKEN_INCLUDE_PATH
andCHICKEN_HOME
.- the current directory
ID
should be a string or a symbol. The difference betweenrequire
andrequire-for-syntax
is the the latter loads the extension library at compile-time (the argument is still evaluated), while the former loads it at run-time.
(set-extension-specifier! SYMBOL PROC)Registers the handler-procedure
PROC
as a extension-specifier with the nameSYMBOL
. This facility allows extending the set of valid extension specifiers to be used withrequire-extension
. Whenregister-extension
is called with an extension specifier of the form(SPEC ...)
andSPEC
has been registered withset-extension-specifier!
, thenPROC
will be called with two arguments: the specifier and the previously installed handler (or#f
if no such handler was defined). The handler should return a new specifier that will be processed recursively. If the handler returns a vector, then each element of the vector will be processed recursively. Alternatively the handler may return a string which specifies a file to be loaded:(eval-when (compile eval) (set-extension-specifier! 'my-package (lambda (spec old) (make-pathname my-package-directory (->string (cadr spec))) ) ) ) (require-extension (my-package stuff)) ; --> expands into '(load "my-package-dir/stuff")Note that the handler has to be registered at compile time, if it is to be visible in compiled code.
(define-reader-ctor SYMBOL PROC)Define new read-time constructor for
#,
read syntax. For further information, see the documentation for SRFI-10.
(set-read-syntax! CHAR PROC)When the reader is encounting the non-whitespace character
CHAR
while reading an expression from a given port, then the procedurePROC
will be called with that port as its argument. The procedure should return a value that will be returned to the reader:; A simple RGB color syntax: (set-read-syntax! #\% (lambda (port) (apply vector (map (cut string->number <> 16) (string-chop (read-string 6 port) 2) ) ) ) ) (with-input-from-string "(1 2 %f0f0f0 3)" read) ; ==> (1 2 #(240 240 240) 3)You can undo special handling of read-syntax by passing
#f
as the second argument (if the syntax was previously defined viaset-read-syntax!
).Note that all of CHICKEN's special non-standard read-syntax is handled directly by the reader to disable built-in read-syntax, define a handler that triggers an error (for example).
(set-sharp-read-syntax! CHAR PROC)Similar to
set-read-syntax!
, but allows defining new#<CHAR> ...
reader syntax.
(copy-read-table READ-TABLE)Returns a copy of the given read-table. You can access the currently active read-table with
(current-read-table)
.
(eval EXP [ENVIRONMENT])Evaluates
EXP
and returns the result of the evaluation. The second argument is optional and defaults to the value of(interaction-environment)
.
This unit contains a collection of useful utility definitions.
This unit is used by default, unless the program
is compiled with the -explicit-use
option.
(alist-ref KEY ALIST [TEST [DEFAULT]])Looks up
KEY
inALIST
usingTEST
as the comparison function (oreqv?
if no test was given) and returns the cdr of the found pair, orDEFAULT
(which defaults to#f
).
(alist-update! KEY VALUE ALIST [TEST])If the list
ALIST
contains a pair of the form(KEY . X)
, then this procedure replacesX
withVALUE
and returnsALIST
. IfALIST
contains no such item, thenalist-update!
returns((KEY . VALUE) . ALIST)
. The optional argumentTEST
specifies the comparison procedure to search a matching pair inALIST
and defaults toeqv?
.
(rassoc KEY LIST [TEST])Similar to
assoc
, but comparesKEY
with thecdr
of each pair inLIST
usingTEST
as the comparison procedures (which defaults toeqv?
.
(chop LIST N)Returns a new list of sublists, where each sublist contains
N
elements ofLIST
. IfLIST
has a length that is not a multiple ofN
, then the last sublist contains the remaining elements.(chop '(1 2 3 4 5 6) 2) ==> ((1 2) (3 4) (5 6)) (chop '(a b c d) 3) ==> ((a b c) (d))
(compress BLIST LIST)Returns a new list with elements taken from
LIST
with corresponding true values in the listBLIST
.(define nums '(99 100 110 401 1234)) (compress (map odd? nums) nums) ==> (99 401)
(flatten LIST1 ...)Returns
LIST1 ...
concatenated together, with nested lists removed (flattened).
(intersperse LIST X)Returns a new list with
X
placed between each element.
(join LISTOFLISTS [LIST])Concatenates the lists in
LISTOFLISTS
withLIST
placed between each sublist.LIST
defaults to the empty list.(join '((a b) (c d) (e)) '(x y)) ==> (a b x y c d x y e) (join '((p q) () (r (s) t)) '(-)) ==> (p q - - r (s) t)
join
could be implemented as follows:(define (join lstoflsts #!optional (lst '())) (apply append (intersperse lstoflists lst)) )
(call-with-input-string STRING PROC)Calls the procedure
PROC
with a single argument that is a string-input-port with the contents ofSTRING
.
(call-with-output-string PROC)Calls the procedure
PROC
with a single argument that is a string-output-port. Returns the accumulated output-string.
(with-input-from-string STRING THUNK)Call procedure
THUNK
with the current input-port temporarily bound to an input-string-port with the contents ofSTRING
.
(with-output-to-string THUNK)Call procedure
THUNK
with the current output-port temporarily bound to a string-output-port and return the accumulated output string.
(fprintf PORT FORMATSTRING ARG ...) (printf FORMATSTRING ARG) (sprintf FORMATSTRING ARG ...) (format FORMATSTRING ARG ...)Simple formatted output to a given port (
fprintf
), the value of(current-output-port)
(printf
) or a string (sprintf
/format
). TheFORMATSTRING
can contain any sequence of characters. The character `~' prefixes special formatting directives:
~%
- write newline character
~N
- the same as
~%
~S
- write the next argument
~A
- display the next argument
~\n
- skip all whitespace in the format-string until the next non-whitespace character
~B
- write the next argument as a binary number
~O
- write the next argument as an octal number
~X
- write the next argument as a hexadecimal number
~C
- write the next argument as a character
~~
- display `~'
~!
- flush all pending output
~?
- invoke formatted output routine recursively with the next two arguments as format-string and list of parameters
CHICKEN implements SRFI-69. For more information, see SRFI-69.
A setter for hash-table-ref
is defined, so
(set! (hash-table-ref HT KEY) VAL)
is equivalent to
(hash-table-set! HT KEY VAL)
(list->queue LIST)Returns
LIST
converted into a queue, where the first element of the list is the same as the first element of the queue. The resulting queue may share memory with the list and the list should not be modified after this operation.
(queue->list QUEUE)Returns
QUEUE
converted into a list, where the first element of the list is the same as the first element of the queue. The resulting list may share memory with the queue object and should not be modified.
(queue-first QUEUE)Returns the first element of
QUEUE
. IfQUEUE
is empty an error is signaled
(queue-last QUEUE)Returns the last element of
QUEUE
. IfQUEUE
is empty an error is signaled
(queue-remove! QUEUE)Removes and returns the first element of
QUEUE
. IfQUEUE
is empty an error is signaled
(queue-push-back! QUEUE ITEM)Pushes an item into the first position of a queue, i.e. the next
queue-remove!
will returnITEM
.
(queue-push-back-list! QUEUE LIST)Pushes the items in item-list back onto the queue, so that
(car LIST)
becomes the next removable item.
(merge LIST1 LIST2 LESS?) (merge! LIST1 LIST2 LESS?)Joins two lists in sorted order.
merge!
is the destructive version of merge.LESS?
should be a procedure of two arguments, that returns true if the first argument is to be ordered before the second argument.
(sort SEQUENCE LESS?) (sort! SEQUENCE LESS?)Sort
SEQUENCE
, which should be a list or a vector.sort!
is the destructive version of sort.
(sorted? SEQUENCE LESS?)Returns true if the list or vector
SEQUENCE
is already sorted.
(randomize [X])Set random-number seed. If
X
is not supplied, the current time is used. On startup (when theextras
unit is initialized), the random number generator is initialized with the current time.
(make-input-port READ READY? CLOSE [PEEK])Returns a custom input port. Common operations on this port are handled by the given parameters, which should be procedures of no arguments.
READ
is called when the next character is to be read and should return a character or#!eof
.READY?
is called whenchar-ready?
is called on this port and should return#t
or#f
.CLOSE
is called when the port is closed.PEEK
is called whenpeek-char
is called on this port and should return a character or#!eof
. if the argumentPEEK
is not given, thenREAD
is used instead and the created port object handles peeking automatically (by callingREAD
and buffering the character).
(make-output-port WRITE CLOSE [FLUSH])Returns a custom output port. Common operations on this port are handled by the given parameters, which should be procedures.
WRITE
is called when output is sent to the port and receives a single argument, a string.CLOSE
is called when the port is closed and should be a procedure of no arguments.FLUSH
(if provided) is called for flushing the output port.
(pretty-print EXP [PORT]) (pp EXP [PORT])Print expression nicely formatted.
PORT
defaults to the value of(current-output-port)
.
Specifies the maximal line-width for pretty printing, after which line wrap will occur.
(read-file [FILE-OR-PORT [READER [MAXCOUNT]]])Returns a list containing all toplevel expressions read from the file or port
FILE-OR-PORT
. If no argument is given, input is read from the port that is the current value of(current-input-port)
. After all expressions are read, and if the argument is a port, then the port will not be closed. TheREADER
argument specifies the procedure used to read expressions from the given file or port and defaults toread
. The reader procedure will be called with a single argument (an input port). IfMAXCOUNT
is given then only up toMAXCOUNT
expressions will be read in.
(read-line [PORT [LIMIT]]) (write-line STRING [PORT])Line-input and -output.
PORT
defaults to the value of(current-input-port)
and(current-output-port)
, respectively. if the optional argumentLIMIT
is given and not#f
, thenread-line
reads at mostLIMIT
characters per line.
(read-lines [PORT [MAX]])Read
MAX
or fewer lines fromPORT
.PORT
defaults to the value of(current-input-port)
.PORT
may optionally be a string naming a file.
(read-string [NUM [PORT]]) (write-string STRING [NUM [PORT]]Read or write
NUM
characters from/toPORT
, which defaults to the value of(current-input-port)
or(current-output-port)
, respectively. IfNUM
is#f
or not given, then all data up to the end-of-file is read, or, in the case ofwrite-string
the whole string is written. If no more input is available,read-string
returns the empty string.
(read-token PREDICATE [PORT])Reads characters from
PORT
(which defaults to the value of(current-input-port)
) and calls the procedurePREDICATE
with each character untilPREDICATE
returns false. Returns a string with the accumulated characters.
(with-error-output-to-port PORT THUNK)Call procedure
THUNK
with the current error output-port temporarily bound toPORT
.
(with-input-from-port PORT THUNK)Call procedure
THUNK
with the current input-port temporarily bound toPORT
.
(with-output-to-port PORT THUNK)Call procedure
THUNK
with the current output-port temporarily bound toPORT
.
(conc X ...)Returns a string with the string-represenation of all arguments concatenated together.
conc
could be implemented as(define (conc . args) (apply string-append (map ->string args)) )
(string-chop STRING LENGTH)Returns a list of substrings taken by “chopping”
STRING
everyLENGTH
characters:(string-chop "one two three" 4) ==> ("one " "two " "thre" "e")
(string-chomp STRING [SUFFIX])If
STRING
ends withSUFFIX
, then this procedure returns a copy of its first argument with the suffix removed, otherwise returnsSTRING
unchanged.SUFFIX
defaults to"\n"
.
(string-compare3-ci STRING1 STRING2)Perform a three-way comparison between the
STRING1
andSTRING2
, returning either-1
ifSTRING1
is lexicographically less thanSTRING2
,0
if it is equal, or1
if it s greater.string-compare3-ci
performs a case-insensitive comparison.
(string-intersperse LIST [STRING])Returns a string that contains all strings in
LIST
concatenated together.STRING
is placed between each concatenated string and defaults to" "
.(string-intersperse '("one" "two") "three")is equivalent to
(apply string-append (intersperse '("one" "two") "three"))
(string-split STRING [DELIMITER-STRING [KEEPEMPTY]])Split string into substrings separated by the given delimiters. If no delimiters are specified, a string comprising the tab, newline and space characters is assumed. If the parameter
KEEPEMPTY
is given and not#f
, then empty substrings are retained:(string-split "one two three") ==> ("one" "two" "three") (string-split "foo:bar::baz:" ":" #t) ==> ("foo" "bar" "" "baz" "")
(string-translate STRING FROM [TO])Returns a fresh copy of
STRING
with characters matchingFROM
translated toTO
. IfTO
is omitted, then matching characters are removed.FROM
andTO
may be a character, a string or a list. If bothFROM
andTO
are strings, then the character at the same position inTO
as the matching character inFROM
is substituted.
(string-translate* STRING SMAP)Substitutes elements of
STRING
according toSMAP
.SMAP
should be an association-list where each element of the list is a pair of the form(MATCH \. REPLACEMENT)
. Every occurrence of the stringMATCH
inSTRING
will be replaced by the stringREPLACEMENT
:(string-translate* "<h1>this is a \"string\"</h1>" '(("<" . "<:") (">" . ">") ("\"" . """)) ) ==> "<h1>this is a "string"</ht>"
(substring=? STRING1 STRING2 [START1 [START2 [LENGTH]]]) (substring-ci=? STRING1 STRING2 [START1 [START2 [LENGTH]]])Returns
#t
if the stringsSTRING1
andSTRING2
are equal, or#f
otherwise. The comparison starts at the positionsSTART1
andSTART2
(which default to 0), comparingLENGTH
characters (which defaults to the minimum of the remaining length of both strings).
(substring-index WHICH WHERE [START]) (substring-index-ci WHICH WHERE [START])Searches for first index in string
WHERE
where stringWHICH
occurs. If the optional argumentSTART
is given, then the search starts at that index.substring-index-ci
is a case-insensitive version ofsubstring-index
.
(constantly X ...)Returns a procedure that always returns the values
X ...
regardless of the number and value of its arguments.(constantly X) <=> (lambda args X)
(complement PROC)Returns a procedure that returns the boolean inverse of
PROC
.(complement PROC) <=> (lambda (x) (not (PROC x)))
(compose PROC1 PROC2 ...)Returns a procedure that represents the composition of the argument-procedures
PROC1 PROC2 ...
.(compose F G) <=> (lambda args (call-with-values (lambda () (apply G args)) F))
(conjoin PRED ...)Returns a procedure that returns
#t
if its argument satisfies the predicatesPRED ...
.((conjoin odd? positive?) 33) ==> #t ((conjoin odd? positive?) -33) ==> #f
(disjoin PRED ...)Returns a procedure that returns
#t
if its argument satisfies any predicatePRED ...
.((disjoin odd? positive?) 32) ==> #t ((disjoin odd? positive?) -32) ==> #f
(each PROC ...)Returns a procedure that applies
PROC ...
to its arguments, and returns the result(s) of the last procedure application. For example(each pp eval)is equivalent to
(lambda args (apply pp args) (apply eval args) )
(each PROC)
is equivalent toPROC
and(each)
is equivalent tonoop
.
(flip PROC)Returns a two-argument procedure that calls
PROC
with its arguments swapped:(flip PROC) <=> (lambda (x y) (PROC y x))
(project N)Returns a procedure that returns its
N
th argument (starting from 0).
(list-of PRED)Returns a procedure of one argument that returns
#t
when applied to a list of elements that all satisfy the predicate procedurePRED
, or#f
otherwise.((list-of even?) '(1 2 3)) ==> #f ((list-of number?) '(1 2 3)) ==> #t
(noop X ...)Ignores it's arguments, does nothing and returns an unspecified value.
(binary-search SEQUENCE PROC)Performs a binary search in
SEQUENCE
, which should be a sorted list or vector.PROC
is called to compare items in the sequence, should accept a single argument and return an exact integer: zero if the searched value is equal to the current item, negative if the searched value is “less” than the current item, and positive otherwise.
List library, see the documentation for SRFI-1
Homogeneous numeric vectors, see the documentation for SRFI-4.
64-bit integer vectors (u64vector
and s64vector
are not supported.
The basic constructor procedures for number vectors are extended to allow allocating the storage in non garbage collected memory:
(make-XXXvector SIZE [INIT NONGC FINALIZE])Creates a SRFI-4 homogenous number vector of length
SIZE
. IfINIT
is given, it specifies the initial value for each slot in the vector. The optional argumentsNONGC
andFINALIZE
define whether the vector should be allocated in a memory area not subject to garbage collection and whether the associated storage should be automatically freed (using finalization) when there are no references from Scheme variables and data.NONGC
defaults to#f
(the vector will be located in normal garbage collected memory) andFINALIZE
defaults to#t
. Note that theFINALIZE
argument is only used whenNONGC
is true.
Additionally, the following procedures are provided:
(u8vector->byte-vector U8VECTOR) (s8vector->byte-vector S8VECTOR) (u16vector->byte-vector U16VECTOR) (s16vector->byte-vector S16VECTOR) (u32vector->byte-vector U32VECTOR) (s32vector->byte-vector S32VECTOR) (f32vector->byte-vector F32VECTOR) (f64vector->byte-vector F64VECTOR)Each of these procedures return the contents of the given vector as a 'packed' byte-vector. The byte order in that vector is platform-dependent (for example little-endian on an Intel processor). The returned byte-vector shares memory with the contents of the vector.
(byte-vector->u8vector BYTE-VECTOR) (byte-vector->s8vector BYTE-VECTOR) (byte-vector->u16vector BYTE-VECTOR) (byte-vector->s16vector BYTE-VECTOR) (byte-vector->u32vector BYTE-VECTOR) (byte-vector->s32vector BYTE-VECTOR) (byte-vector->f32vector BYTE-VECTOR) (byte-vector->f64vector BYTE-VECTOR)Each of these procedures return a vector where the argument
BYTE-VECTOR
is taken as a 'packed' representation of the contents of the vector. The argument-byte-vector shares memory with the contents of the vector.
(subu8vector U8VECTOR FROM TO) (subu16vector U16VECTOR FROM TO) (subu32vector U32VECTOR FROM TO) (subs8vector S8VECTOR FROM TO) (subs16vector S16VECTOR FROM TO) (subs32vector S32VECTOR FROM TO) (subf32vector F32VECTOR FROM TO) (subf64vector F64VECTOR FROM TO)Creates a number vector of the same type as the argument vector with the elements at the positions
FROM
up to but not includingTO
.SRFI-17 Setters for
XXXvector-ref
are defined.
String library, see the documentation for SRFI-13
On systems that support dynamic loading, the srfi-13
unit can
be made available in the interpreter (csi
) by entering
(require-extension srfi-13)
Character set library, see the documentation for SRFI-14
On systems that support dynamic loading, the srfi-14
unit can
be made available in the interpreter (csi
) by entering
(require-extension srfi-14)
The runtime-support code for the Pattern Matching extensions. Note that to use the macros in normal compiled code it is not required to declare this unit as used. Only if forms containing these macros are to be expanded at runtime, this is needed.
This library unit provides support for regular expressions. The flavor depends on the particular installation platform:
pregexp
library is used.
(grep REGEX LIST)Returns all items of
LIST
that match the regular expressionREGEX
. This procedure could be defined as follows:(define (grep regex lst) (filter (lambda (x) (string-search regex x)) lst) )
(glob->regexp PATTERN)Converts the file-pattern
PATTERN
into a regular expression.(glob->regexp "foo.*") ==> "foo\..*"
(regexp STRING [IGNORECASE [IGNORESPACE [UTF8]]])Returns a precompiled regular expression object for
string
. The optional argumentsIGNORECASE
,IGNORESPACE
andUTF8
specify whether the regular expression should be matched with case- or whitespace-differences ignored, or whether the string should be treated as containing UTF-8 encoded characters, respectively.Notes:
- regex doesn't allow (?: ) cloisters (non-capturing groups) Currently this means if you use utf8 matching, individual "." matching will return extra submatches.
- pregexp doesn't allow a # comment w/o a trailing newline.
(regexp? X)Returns
#t
ifX
is a precompiled regular expression, or#f
otherwise.
(string-match REGEXP STRING [START]) (string-match-positions REGEXP STRING [START])Matches the regular expression in
REGEXP
(a string or a precompiled regular expression) withSTRING
and returns either#f
if the match failed, or a list of matching groups, where the first element is the complete match. If the optional argumentSTART
is supplied, it specifies the starting position inSTRING
. For each matching group the result-list contains either:#f
for a non-matching but optional group; a list of start- and end-position of the match inSTRING
(in the case ofstring-match-positions
); or the matching substring (in the case ofstring-match
). Note that the exact string is matched. For searching a pattern inside a string, see below. Note also thatstring-match
is implemented by callingstring-search
with the regular expression wrapped in^ ... $
.
(string-search REGEXP STRING [START [RANGE]]) (string-search-positions REGEXP STRING [START [RANGE]])Searches for the first match of the regular expression in
REGEXP
withSTRING
. The search can be limited toRANGE
characters.
(string-split-fields REGEXP STRING [MODE [START]])Splits
STRING
into a list of fields according toMODE
, whereMODE
can be the keyword#:infix
(REGEXP
matches field separator), the keyword#:suffix
(REGEXP
matches field terminator) or#t
(REGEXP
matches field), which is the default.(define s "this is a string 1, 2, 3,") (string-split-fields "[^ ]+" s) => ("this" "is" "a" "string" "1," "2," "3,") (string-split-fields " " s #:infix) => ("this" "is" "a" "string" "1," "2," "3,") (string-split-fields "," s #:suffix)) => ("this is a string 1" " 2" " 3")
(string-substitute REGEXP SUBST STRING [MODE])Searches substrings in
STRING
that matchREGEXP
and substitutes them with the stringSUBST
. The substitution can contain references to subexpressions inREGEXP
with the\NUM
notation, whereNUM
refers to the NUMth parenthesized expression. The optional argumentMODE
defaults to 1 and specifies the number of the match to be substituted. Any non-numeric index specifies that all matches are to be substituted.(string-substitute "([0-9]+) (eggs|chicks)" "\\2 (\\1)" "99 eggs or 99 chicks" 2) ==> "99 eggs or chicks (99)"
(string-substitute* STRING SMAP [MODE])Substitutes elements of
STRING
withstring-substitute
according toSMAP
.SMAP
should be an association-list where each element of the list is a pair of the form(MATCH . REPLACEMENT)
. Every occurrence of the regular expressionMATCH
inSTRING
will be replaced by the stringREPLACEMENT
(string-substitute* "<h1>Hello, world!</h1>" '(("<[/A-Za-z0-9]+>" . "")))) ==> "Hello, world!"
(regexp-escape STRING)Escapes all special characters in
STRING
with\
, so that the string can be embedded into a regular expression.(regexp-escape "^[0-9]+:.*$") ==> "\\^\\[0-9\\]\\+:.\n.\\*\\$"
Platform-specific notes:
pregexp
library, character classes enclosed in [ ... ]
may not begin with a hyphen (-
). A workaround is either to precede the hyphen with a backslash
or use the range ---
.
A simple multithreading package. This threading package follows largely the specification of SRFI-18. For more information see the documentation for SRFI-18
Notes:
thread-start!
accepts a thunk (a zero argument procedure) as argument, which is
equivalent to (thread-start! (make-thread THUNK))
.
enable-warnings
, then a warning
message is written to the port that is the value of (current-error-port)
.
tcp
unit). An exception is the read-eval-print loop on
UNIX platforms: waiting for input will not block other threads, provided the current input
port reads input from a console.
dynamic-wind
is involved.
make-parameter
)
dynamic-wind
thunks.
The following procedures are provided, in addition to the procedures defined in SRFI-18:
(thread-signal! THREAD X)This will cause
THREAD
to signal the conditionX
once it is scheduled for execution. After signalling the condition, the thread continues with its normal execution.
(thread-quantum THREAD)Returns the quantum of
THREAD
, which is an exact integer specifying the approximate time-slice of the thread.
(thread-quantum-set! THREAD QUANTUM)Sets the quantum of
THREAD
toQUANTUM
.
This unit provides services as used on many UNIX-like systems. Note that the following definitions are not all available on non-UNIX systems like Windows. See below for Windows specific notes.
This unit uses the regex
, scheduler
, extras
and utils
units.
All errors related to failing file-operations will signal a condition
of kind (exn i/o file)
.
(change-directory NAME)Changes the current working directory to
NAME
.
(current-directory [DIR])Returns the name of the current working directory. If the optional argument
DIR
is given, then(current-directory DIR)
is equivalent to(change-directory DIR)
.
(delete-directory NAME)Deletes the directory with the pathname
NAME
. The directory has to be empty.
(directory [PATHNAME [SHOW-DOTFILES?]])Returns a list with all files that are contained in the directory with the name
PATHNAME
(which defaults to the value of(current-directory)
). IfSHOW-DOTFILES?
is given and not#f
, then files beginning with “.” are not included in the directory listing.
(directory? NAME)Returns
#t
if there exists a file with the nameNAME
and if that file is a directory, or#f
otherwise.
(glob PATTERN1 ...)Returns a list of the pathnames of all existing files matching
PATTERN1 ...
, which should be strings containing the usual file-patterns (with*
matching zero or more characters and?
matching zero or one character).
(set-root-directory! STRING)Sets the root directory for the current process to the path given in
STRING
(using thechroot
function). If the current process has no root permissions, the operation will fail.
(call-with-input-pipe CMDLINE PROC [MODE]) (call-with-output-pipe CMDLINE PROC [MODE])Call
PROC
with a single argument: a input- or output port for a pipe connected to the subprocess named inCMDLINE
. IfPROC
returns normally, the pipe is closed and any result values are returned.
(close-input-pipe PORT) (close-output-pipe PORT)Closes the pipe given in
PORT
and waits until the connected subprocess finishes. The exit-status code of the invoked process is returned.
(create-pipe)The fundamental pipe-creation operator. Calls the C function
pipe()
and returns 2 values: the file-descriptors of the input- and output-ends of the pipe.
(open-input-pipe CMDLINE [MODE])Spawns a subprocess with the command-line string
CMDLINE
and returns a port, from which the output of the process can be read. IfMODE
is specified, it should be the keyword#:text
(the default) or#:binary
.
(open-output-pipe CMDLINE [MODE])Spawns a subprocess with the command-line string
CMDLINE
and returns a port. Anything written to that port is treated as the input for the process. IfMODE
is specified, it should be the keyword#:text
(the default) or#:binary
.
This variable contains the maximal number of bytes that can be written atomically into a pipe or FIFO.
(with-input-from-pipe CMDLINE THUNK [MODE]) (with-output-to-pipe CMDLINE THUNK [MODE])Temporarily set the value of
current-input-port/current-output-port
to a port for a pipe connected to the subprocess named inCMDLINE
and call the procedureTHUNK
with no arguments. AfterTHUNK
returns normally the pipe is closed and the standard input-/output port is restored to its previous value and any result values are returned.(with-output-to-pipe "gs -dNOPAUSE -sDEVICE=jpeg -dBATCH -sOutputFile=signballs.jpg -g600x600 -q -" (lambda () (print #<<EOF %!IOPSC-1993 %%Creator: HAYAKAWA Takashi<xxxxxxxx@xx.xxxxxx.xx.xx> /C/neg/d/mul/R/rlineto/E/exp/H{{cvx def}repeat}def/T/dup/g/gt/r/roll/J/ifelse 8 H/A/copy(z&v4QX&93r9AxYQOZomQalxS2w!!O&vMYa43d6r93rMYvx2dca!D&cjSnjSnjjS3o!v&6A X&55SAxM1CD7AjYxTTd62rmxCnTdSST0g&12wECST!&!J0g&D1!&xM0!J0g!l&544dC2Ac96ra!m&3A F&&vGoGSnCT0g&wDmlvGoS8wpn6wpS2wTCpS1Sd7ov7Uk7o4Qkdw!&Mvlx1S7oZES3w!J!J!Q&7185d Z&lx1CS9d9nE4!k&X&MY7!&1!J!x&jdnjdS3odS!N&mmx1C2wEc!G&150Nx4!n&2o!j&43r!U&0777d ]&2AY2A776ddT4oS3oSnMVC00VV0RRR45E42063rNz&v7UX&UOzF!F!J![&44ETCnVn!a&1CDN!Y&0M V1c&j2AYdjmMdjjd!o&1r!M){( )T 0 4 3 r put T(/)g{T(9)g{cvn}{cvi}J}{($)g[]J}J cvx}forall/moveto/p/floor/w/div/S/add 29 H[{[{]setgray fill}for Y}for showpage EOF ) ) )
(create-fifo FILENAME [MODE])Creates a FIFO with the name
FILENAME
and the permission bitsMODE
, which defaults to(+ perm/irwxu perm/irwxg perm/irwxo)
(duplicate-fileno OLD [NEW])If
NEW
is given, then the file-descriptorNEW
is opened to access the file with the file-descriptorOLD
. Otherwise a fresh file-descriptor accessing the same file asOLD
is returned.
(file-close FILENO)Closes the input/output file with the file-descriptor
FILENO
.
(file-open FILENAME FLAGS [MODE])Opens the file specified with the string
FILENAME
and open-flagsFLAGS
using the C functionopen()
. On success a file-descriptor for the opened file is returned.FLAGS
should be a bitmask containing one or more of theopen/...
values ored together usingbitwise-ior
(or simply added together). The optionalMODE
should be a bitmask composed of one or more permission values likeperm/irusr
and is only relevant when a new file is created. The default mode isperm/irwxu | perm/irgrp | perm/iroth
.
(file-mkstemp TEMPLATE-FILENAME)Create a file based on the given
TEMPLATE-FILENAME
, in which the six last characters must be “XXXXXX”. These will be replaced with a string that makes the filename unique. The file descriptor of the created file and the generated filename is returned. See themkstemp(3)
manual page for details on how this function works. The template string given is not modified.Example usage:
(let-values (((fd temp-path) (file-mkstemp "/tmp/mytemporary.XXXXXX"))) (let ((temp-port (open-output-file* fd))) (format temp-port "This file is ~A.~%" temp-path) (close-output-port temp-port)))
(file-read FILENO SIZE [BUFFER])Reads
SIZE
bytes from the file with the file-descriptorFILENO
. If a string or bytevector is passed in the optional argumentBUFFER
, then this string will be destructively modified to contain the read data. This procedure returns a list with two values: the buffer containing the data and the number of bytes read.
(file-select READFDLIST WRITEFDLIST [TIMEOUT])Waits until any of the file-descriptors given in the lists
READFDLIST
andWRITEFDLIST
is ready for input or output, respectively. If the optional argumentTIMEOUT
is given and not false, then it should specify the number of seconds after which the wait is to be aborted. This procedure returns two values: the lists of file-descriptors ready for input and output, respectively.READFDLIST
and WRITEFDLIST may also by file-descriptors instead of lists. In this case the returned values are booleans indicating whether input/output is ready by#t
or#f
otherwise. You can also pass#f
asREADFDLIST
orWRITEFDLIST
argument, which is equivalent to()
.
(file-write FILENO BUFFER [SIZE])Writes the contents of the string or bytevector
BUFFER
into the file with the file-descriptorFILENO
. If the optional argumentSIZE
is given, then only the specified number of bytes are written.
These variables contain file-descriptors for the standard I/O files.
Flags for use with
file-open
.
(open-input-file* FILENO [OPENMODE]) (open-output-file* FILENO [OPENMODE])Opens file for the file-descriptor
FILENO
for input or output and returns a port.FILENO
should be a positive exact integer.OPENMODE
specifies an additional mode for opening the file (currently only the keyword#:append
is supported, which opens an output-file for appending).
(port->fileno PORT)If
PORT
is a file- or tcp-port, then a file-descriptor is returned for this port. Otherwise an error is signaled.
(file-access-time FILE) (file-change-time FILE) (file-modification-time FILE)Returns time (in seconds) of the last acces, modification or change of
FILE
.FILE
may be a filename or a file-descriptor. If the file does not exist, an error is signaled.
(file-stat FILE [LINK])Returns a 9-element vector with the following contents: inode-number, mode (as with
file-permissions
), number of hard links, uid of owner (as withfile-owner
), gid of owner, size (as withfile-size
) and access-, change- and modification-time (as withfile-access-time
,file-change-time
andfile-modification-time
). If the optional argumentLINK
is given and not#f
, then the file-statistics vector will be resolved for symbolic links (otherwise symbolic links are resolved).
(file-position FILE)Returns the current file position of
FILE
, which should be a port or a file-descriptor.
(file-size FILENAME)Returns the size of the file designated by
FILE
.FILE
may be a filename or a file-descriptor. If the file does not exist, an error is signaled.
(regular-file? FILENAME)Returns true, if
FILENAME
names a regular file (not a directory or symbolic link).
(file-truncate FILE OFFSET)Truncates the file
FILE
to the lengthOFFSET
, which should be an integer. If the file-size is smaller or equal toOFFSET
then nothing is done.FILE
should be a filename or a file-descriptor.
(set-file-position! FILE POSITION [WHENCE])Sets the current read/write position of
FILE
toPOSITION
, which should be an exact integer.FILE
should be a port or a file-descriptor.WHENCE
specifies how the position is to interpreted and should be one of the valuesseek/set, seek/cur
andseek/end
. It defaults toseek/set
.Exceptions:
(exn bounds)
,(exn i/o file)
(parent-process-id)Returns the process ID of the parent of the current process.
(process-execute PATHNAME [ARGUMENT-LIST [ENVIRONMENT-LIST]])Creates a new child process and replaces the running process with it using the C library function
execvp(3)
. If the optional argumentARGUMENT-LIST
is given, then it should contain a list of strings which are passed as arguments to the subprocess. If the optional argumentENVIRONMENT-LIST
is supplied, then the library functionexecve(2)
is used, and the environment passed inENVIRONMENT-LIST
(which should be of the form("<NAME>=<VALUE>" ...)
is given to the invoked process. Note thatexecvp(3)
respects the current setting of thePATH
environment variable whileexecve(3)
does not.On native Windows,
process-execute
ignores theENVIRONMENT-LIST
arguments.
(process-fork [THUNK])Creates a new child process with the UNIX system call
fork()
. Returns either the PID of the child process or 0. IfTHUNK
is given, then the child process calls it as a procedure with no arguments and terminates.
(process-run PATHNAME [LIST])Creates a new child process using the UNIX system call
fork()
that executes the program given by the stringPATHNAME
using the UNIX system callexecv()
. The PID of the new process is returned. IfLIST
is not specified, thenPATHNAME
is passed to a program named by the environment variableSHELL
(or/bin/sh
, if the variable is not defined), so usual argument expansion can take place.
(process-signal PID [SIGNAL])Sends
SIGNAL
to the process with the idPID
using the UNIX system callkill()
.SIGNAL
defaults to the value of the variablesignal/term
.
(process-wait [PID [NOHANG]])Suspends the current process until the child process with the id
PID
has terminated using the UNIX system callwaitpid()
. IfPID
is not given, then this procedure waits for any child process. IfNOHANG
is given and not#f
then the current process is not suspended. This procedure returns three values:
PID
or 0, if NOHANG
is true and the child process
has not terminated yet;
#t
if the process exited normally or #f
otherwise;
(process COMMANDLINE [ARGUMENTLIST [ENVIRONMENT]])Passes the string
COMMANDLINE
to the host-system's shell that is invoked as a subprocess and returns three values: an input port from which data written by the sub-process can be read, an output port from which any data written to will be received as input in the sub-process and the process-id of the started sub-process. Blocking reads and writes to or from the ports returned byprocess
only block the current thread, not other threads executing concurrently.If
ARGUMENTLIST
is given, then the invocation of the subprocess is not done via the shell, but directly. The arguments are directly passed toprocess-execute
(as isENVIRONMENT
). Not using the shell may be preferrable for security reasons.On native Windows the
ARGUMENTLIST
andENVIRONMENT
arguments are ignored.
(sleep SECONDS)Puts the process to sleep for
SECONDS
. Returns either 0 if the time has completely elapsed, or the number of remaining seconds, if a signal occurred.
(symbolic-link? FILENAME)Returns true, if
FILENAME
names a symbolic link.
(create-symbolic-link OLDNAME NEWNAME)Creates a symbolic link with the filename
NEWNAME
that points to the file namedOLDNAME
.
(read-symbolic-link FILENAME)Returns the filename to which the symbolic link
FILENAME
points.
(file-link OLDNAME NEWNAME)Creates a hard link from
OLDNAME
toNEWNAME
(both strings).
(file-owner FILE)Returns the user-id of
FILE
.FILE
may be a filename or a file-descriptor.
(file-permissions FILE)Returns the permission bits for
FILE
. You can test this value by performing bitwise operations on the result and theperm/...
values.FILE
may be a filename or a file-descriptor.
(file-read-access? FILENAME) (file-write-access? FILENAME) (file-execute-access? FILENAME)These procedures return
#t
if the current user has read, write or execute permissions on the file namedFILENAME
.
(change-file-mode FILENAME MODE)Changes the current file mode of the file named
FILENAME
toMODE
using thechmod()
system call. Theperm/...
variables contain the various permission bits and can be combinded with thebitwise-ior
procedure.
(change-file-owner FILENAME UID GID)Changes the owner information of the file named
FILENAME
to the user- and group-idsUID
andGID
(which should be exact integers) using thechown()
system call.
(current-user-id) (current-group-id) (current-effective-user-id) (current-effective-group-id)Return the user- and group-ids of the current process.
(group-information GROUP)If
GROUP
specifies a valid group-name or group-id, then this procedure returns a list of four values: the group-name, the encrypted group password, the group ID and a list of the names of all group members. If no group with the given name or ID exists, then#f
is returned.
(get-groups)Returns a list with the supplementary group IDs of the current user.
(set-groups! GIDLIST)Sets the supplementrary group IDs of the current user to the IDs given in the list
GIDLIST
.Only the superuser may invoke this procedure.
(initialize-groups USERNAME BASEGID)Sets the supplementrary group IDs of the current user to the IDs from the user with name
USERNAME
(a string), includingBASEGID
.Only the superuser may invoke this procedure.
These variables contain permission bits as used in
change-file-mode
.
(set-user-id! UID)Sets the effective user id of the current process to
UID
, which should be a positive integer.
(set-group-id! GID)Sets the effective group id of the current process to
GID
, which should be a positive integer.
(set-user-id! PID PGID)Sets the process group ID of the process specifed by
PID
toPGID
.
(user-information USER)If
USER
specifes a valid username (as a string) or user ID, then the user database is consulted and a list of 7 values are returned: the user-name, the encrypted password, the user ID, the group ID, a user-specific string, the home directory and the default shell. If no user with this name or ID can be found, then#f
is returned.
(create-session)Creates a new session if the calling process is not a process group leader and returns the session ID.
(file-lock PORT [START [LEN]])Locks the file associated with
PORT
for reading or writing (according to whetherPORT
is an input- or output-port).START
specifies the starting position in the file to be locked and defaults to 0.LEN
specifies the length of the portion to be locked and defaults to#t
, which means the complete file.file-lock
returns a “lock”-object.
(file-lock/blocking PORT [START [LEN]])Similar to
file-lock
, but if a lock is held on the file, the current process blocks (including all threads) until the lock is released.
(file-test-lock PORT [START [LEN]])Tests whether the file associated with
PORT
is locked for reading or writing (according to whetherPORT
is an input- or output-port) and returns either#f
or the process-id of the locking process.
(file-unlock LOCK)Unlocks the previously locked portion of a file given in
LOCK
.
(set-alarm! SECONDS)Sets an internal timer to raise the
signal/alrm
afterSECONDS
are elapsed. You can use theset-signal-handler!
procedure to write a handler for this signal.
(set-signal-handler! SIGNUM PROC)Establishes the procedure of one argument
PROC
as the handler for the signal with the codeSIGNAL
.PROC
is called with the signal number as its sole argument. If the argumentPROC
is#f
then this signal will be ignored. Note that is is unspecified in which thread of execution the signal handler will be invoked.
(set-signal-mask! SIGLIST)Sets the signal mask of the current process to block all signals given in the list
SIGLIST
. Signals masked in that way will not be delivered to the current process.
These variables contain signal codes for use with
process-signal
orset-signal-handler!
.
(current-environment)Returns a association list of the environment variables and their current values.
(setenv VARIABLE VALUE)Sets the environment variable named
VARIABLE
toVALUE
. Both arguments should be strings. If the variable is not defined in the environment, a new definition is created.
(unsetenv VARIABLE)Removes the definition of the environment variable
VARIABLE
from the environment of the current process. If the variable is not defined, nothing happens.
(memory-mapped-file? X)Returns
#t
, ifX
is an object representing a memory mapped file, or#f
otherwise.
(map-file-to-memory ADDRESS LEN PROTECTION FLAG FILENO [OFFSET])Maps a section of a file to memory using the C function
mmap()
.ADDRESS
should be a foreign pointer object or#f
;LEN
specifies the size of the section to be mapped;PROTECTION
should be one or more of the flagsprot/read, prot/write, prot/exec
orprot/none
bitwise-iored together;FLAG
should be one or more of the flagsmap/fixed, map/shared, map/private, map/anonymous
ormap/file
;FILENO
should be the file-descriptor of the mapped file. The optional argumentOFFSET
gives the offset of the section of the file to be mapped and defaults to 0. This procedure returns an object representing the mapped file section. The proceduremove-memory!
can be used to access the mapped memory.
(memory-mapped-file-pointer MMAP)Returns a machine pointer to the start of the memory region to which the file is mapped.
(unmap-file-from-memory MMAP [LEN])Unmaps the section of a file mapped to memory using the C function
munmap()
.MMAP
should be a mapped file as returned by the proceduremap-file-to-memory
. The optional argumentLEN
specifies the length of the section to be unmapped and defaults to the complete length given when the file was mapped.
(seconds->local-time SECONDS)Breaks down the time value represented in
SECONDS
into a 10 element vector of the form#(seconds minutes hours mday month year wday yday dstflag timezone)
, in the following format:
- seconds: the number of seconds after the minute (0 - 59)
- minutes: the number of minutes after the hour (0 - 59)
- hours: the number of hours past midnight (0 - 23)
- mday: the day of the month (1 - 31)
- month: the number of months since january (0 - 11)
- year: the number of years since 1900
- wday: the number of days since Sunday (0 - 6)
- yday: the number of days since January 1 (0 - 365)
- dstflag: a flag that is true if Daylight Saving Time is in effect at the time described.
- timezone: the difference between UTC and the latest local standard time, in seconds west of UTC.
(local-time->seconds VECTOR)Converts the ten-element vector
VECTOR
representing the time value relative to the current timezone into the number of seconds since the first of January, 1970 UTC.
(local-timezone-abbrevtiation)Returns the abbreviation for the local timezone as a string.
(seconds->string SECONDS)Converts the local time represented in
SECONDS
into a string of the form"Tue May 21 13:46:22 1991\n"
.
(seconds->utc-time SECONDS)Similar to
seconds->local-time
, but interpretesSECONDS
as UTC time.
(utc-time->seconds VECTOR)Converts the ten-element vector
VECTOR
representing the UTC time value into the number of seconds since the first of January, 1970 UTC.
(time->string VECTOR)Converts the broken down time represented in the 10 element vector
VECTOR
into a string of the form"Tue May 21 13:46:22 1991\n"
.
(_exit [CODE])Exits the current process without flushing any buffered output (using the C function
_exit
). Note that theexit-handler
is not called when this procedure is invoked. The optional return-codeCODE
defaults to0
.
These variables contain error codes as returned by
errno
.
(find-files DIRECTORY PREDICATE [ACTION [IDENTITY [LIMIT]]])Recursively traverses the contents of
DIRECTORY
(which should be a string) and invokes the procedureACTION
for all files for which the procedurePREDICATE
is true.PREDICATE
may me a procedure of one argument or a regular-expression string.ACTION
should be a procedure of two arguments: the currently encountered file and the result of the previous invocation ofACTION
, or, if this is the first invocation, the value ofIDENTITY
.ACTION
defaults tocons
,IDENTITY
defaults to()
.LIMIT
should a procedure of one argument that is called for each nested directory and which should return true, if that directory is to be traversed recursively.LIMIT
may also be an exact integer that gives the maximum recursion depth. A depth of0
means the files in the specified directory are traversed but not any nested directories.LIMIT
may also be#f
(the default), which is equivalent to(constantly #t)
.Note that
ACTION
is called with the full pathname of each file, including the directory prefix.
(get-host-name)Returns the hostname of the machine that this process is running on.
(system-information)Invokes the UNIX system call
uname()
and returns a list of 5 values: system-name, node-name, OS release, OS version and machine.
(set-buffering-mode! PORT MODE [BUFSIZE])Sets the buffering-mode for the file associated with
PORT
toMODE
, which should be one of the keywords#:full, #:line
or#:none
. IfBUFSIZE
is specified it determines the size of the buffer to be used (if any).
(terminal-name PORT)Returns the name of the terminal that is connected to
PORT
.
(terminal-port? PORT)Returns
#t
ifPORT
is connected to a terminal and#f
otherwise.
change-directory
change-file-mode
change-file-owner
create-directory
create-fifo
create-pipe
create-session
create-symbolic-link
current-directory
current-effective-groupd-id
current-effective-user-id
current-group-id
current-parent-id
current-process-id
current-user-id
delete-directory
duplicate-fileno
_exit
file-close
file-access-time
file-change-time
file-modification-time
file-execute-access?
file-open
file-lock
file-position
file-read
file-read-access?
file-select
file-stat
file-test-lock
file-truncate
file-unlock
file-write
file-write-access?
get-groups
get-host-name
initialize-groups
local-time->seconds
local-timezone-abbreviation
map-file-to-memory
open-input-file*
open-output-file*
open-input-pipe
open-output-pipe
port->fileno
process-execute
process-fork
process-group-id
process-signal
process-wait
close-input-pipe
close-output-pipe
read-symbolic-link
seconds->local-time
seconds->string
seconds->utc-time
set-alarm!
set-buffering-mode!
set-file-position!
set-groups!
set-signal-mask!
set-group-id!
set-process-group-id!
set-user-id!
set-root-directory!
setenv
sleep
system-information
terminal-name
terminal-port?
time->string
unsetenv
unmap-file-from-memory
user-information
utc-time->seconds
The following definitions are not supported for native Windows builds (compiled with the Microsoft tools or with MingW):
open/noctty open/nonblock open/fsync open/sync perm/isvtx perm/isuid perm/isgid file-select signal/... set-signal-handler! set-signal-mask! user-information group-information get-groups set-groups! initialize-groups errno/wouldblock change-file-owner current-user-id current-group-id current-effective-user-id current-effective-groupd-id set-user-id! set-group-id! create-session process-group-id set-process-group-id! create-symbolic-link read-symbolic-link file-truncate file-lock file-lock/blocking file-unlock file-test-lock create-fifo fifo? prot/... map/... map-file-to-memory unmap-file-from-memory memory-mapped-file-pointer memory-mapped-file? set-alarm! terminal-port? terminal-name process-fork process-signal parent-process-id set-root-directory! utc-time->seconds local-timezone-abbreviation
Additionally, the following definitions are only available for Windows:
These variables contains special flags that specify the exact semantics of
process-spawn
:spawn/overlay
replaces the current process with the new one.spawn/wait
suspends execution of the current process until the spawned process returns.spawn/nowait
does the opposite (spawn/nowaito
is identical, according to the Microsoft documentation) and runs the process asynchronously.spawn/detach
runs the new process in the background, without being attached to a console.
(process-spawn MODE FILENAME ARGUMENT ...)Creates and runs a new process with the given filename and command-line arguments.
MODE
specifies how exactly the process should be executed and must be one or more of thespawn/...
flags defined above.
This unit contains some utility procedures for Shell scripting and for some file operations.
This unit uses the extras
and regex
units.
(absolute-pathname? PATHNAME)Returns
#t
if the stringPATHNAME
names an absolute pathname, and returns#f
otherwise.
(decompose-pathname PATHNAME)Returns three values: the directory-, filename- and extension-components of the file named by the string
PATHNAME
. For any component that is not contained inPATHNAME
,#f
is returned.
(make-pathname DIRECTORY FILENAME [EXTENSION]) (make-absolute-pathname DIRECTORY FILENAME [EXTENSION])Returns a string that names the file with the components
DIRECTORY, FILENAME
and (optionally)EXTENSION
.DIRECTORY
can be#f
(meaning no directory component), a string or a list of strings.FILENAME
andEXTENSION
should be strings or#f
.make-absolute-pathname
returns always an absolute pathname.
(pathname-extension PATHNAME)Accessors for the components of
PATHNAME
. If the pathname does not contain the accessed component, then#f
is returned.
(pathname-replace-extension PATHNAME EXTENSION)Return a new pathname with the specified component of
PATHNAME
replaced by a new value.
(pathname-strip-extension PATHNAME)Return a new pathname with the specified component of
PATHNAME
stripped.
(create-temporary-file [EXTENSION])Creates an empty temporary file and returns its pathname. If
EXTENSION
is not given, then.tmp
is used. If the environment variableTMPDIR, TEMP
orTMP
is set, then the pathname names a file in that directory.
(delete-file* FILENAME)If the file
FILENAME
exists, it is deleted and#t
is returned. If the file does not exist, nothing happens and#f
is returned.
(for-each-line PROCEDURE [PORT])Calls
PROCEDURE
for each line read fromPORT
(which defaults to the value of(current-input-port)
. The argument passed toPORCEDURE
is a string with the contents of the line, excluding any line-terminators. When all input has been read from the port,for-each-line
returns some unspecified value.
(for-each-argv-line PROCEDURE)Opens each file listed on the command line in order, passing one line at a time into
PROCEDURE
. The filename-
is interpreted as(current-input-port)
. If no arguments are given on the command line it again uses the value of(current-input-port)
. During execution ofPROCEDURE
, the current input port will be correctly bound to the current input source.This code will act as a simple Unix cat(1) command:
(for-each-argv-line print)
(port-for-each FN THUNK) (port-map FN THUNK)Apply
FN
to successive results of calling the zero argument procedureTHUNK
until it returns#!eof
.port-for-each
discards the results, whileport-map
returns a list of the collected results.
(system* FORMATSTRING ARGUMENT1 ...)Similar to
(system (sprintf FORMATSTRING ARGUMENT1 ...))
, but signals an error if the invoked program should return a nonzero exit status.
(read-all [FILE-OR-PORT])If
FILE-OR-PORT
is a string, then this procedure returns the contents of the file as a string. IfFILE-OR-PORT
is a port, all remaining input is read and returned as a string. The port is not closed. If no argument is provided, input will be read from the port that is the current value of(current-input-port)
.
(shift! LIST [DEFAULT])Returns the car of
LIST
(orDEFAULT
ifLIST
is empty) and replaces the car ofLIST
with it's cadr and the cdr with the cddr. IfDEFAULT
is not given, and the list is empty,#f
is returned. An example might be clearer, here:(define lst '(1 2 3)) (shift! lst) ==> 1, lst is now (2 3)The list must at least contain 2 elements.
(unshift! X PAIR)Sets the car of
PAIR
toX
and the cdr to its cddr. ReturnsPAIR
:(define lst '(2)) (unshift! 99 lst) ; lst is now (99 2)
This unit provides basic facilities for communicating over TCP sockets. The socket interface should be mostly compatible to the one found in PLT Scheme.
This unit uses the extras
unit.
All errors related to failing network operations will raise a condition
of kind (exn i/o network)
.
(tcp-listen TCPPORT [BACKLOG [HOST]])Creates and returns a TCP listener object that listens for connections on
TCPPORT
, which should be an exact integer.BACKLOG
specifies the number of maximally pending connections (and defaults to 4). If the optional argumentHOST
is given and not#f
, then only incoming connections for the given host (or IP) are accepted.
(tcp-listener? X)Returns
#t
ifX
is a TCP listener object, or#f
otherwise.
(tcp-accept LISTENER)Waits until a connection is established on the port on which
LISTENER
is listening and returns two values: an input- and output-port that can be used to communicate with the remote process.Note: this operation and any I/O on the ports returned will not block other running threads.
(tcp-accept-ready? LISTENER)Returns
#t
if there are any connections pending onLISTENER
, or#f
otherwise.
(tcp-listener-port LISTENER)Returns the port number assigned to
LISTENER
(If you pass0
totcp-listen
, then the system will choose a port-number for you).
(tcp-listener-port LISTENER)Returns the file-descriptor associated with
LISTENER
.
(tcp-connect HOSTNAME [TCPPORT])Establishes a client-side TCP connection to the machine with the name
HOSTNAME
(a string) atTCPPORT
(an exact integer) and returns two values: an input- and output-port for communicating with the remote process.Note: any I/O on the ports returned will not block other running threads.
(tcp-addresses PORT)Returns two values for the input- or output-port
PORT
(which should be a port returned by eithertcp-accept
ortcp-connect
): the IP address of the local and the remote machine that are connected over the socket associated withPORT
. The returned addresses are strings inXXX.XXX.XXX.XXX
notation.
(tcp-port-numbers PORT)Returns two values for the input- or output-port
PORT
(which should be a port returned by eithertcp-accept
ortcp-connect
): the TCP port numbers of the local and the remote machine that are connected over the socket associated withPORT
.
(tcp-abandon-port PORT)Marks the socket port
PORT
as abandoned. This is mainly useful to close down a port without breaking the connection.
Sets the size of the output buffer. By default no output-buffering for TCP output is done, but to improve performance by minimizing the number of TCP packets, buffering may be turned on by setting this parameter to an exact integer greater zero. A buffer size of zero or
#f
turns buffering off. The setting of this parameter takes effect at the time when the I/O ports for a particular socket are created, i.e. whentcp-connect
ortcp-accept
is called.Note that since output is not immediately written to the associated socket, you may need to call
flush-output
, once you want the output to be transmitted. Closing the output port will flush automatically.
A very simple example follows. Say we have the two files client.scm
and server.scm
:
; client.scm (define-values (i o) (tcp-connect "localhost" 4242)) (write-line "Good Bye!" o) (print (read-line i))
; server.scm (define l (tcp-listen 4242)) (define-values (i o) (tcp-accept l)) (write-line "Hello!" o) (print (read-line i)) (close-input-port i) (close-output-port o) % csi -script server.scm & [1] 1409 % csi -script client.scm Good Bye! Hello!
This unit provides a number of handy low-level operations. Use at your own risk.
This unit uses the srfi-4
and extras
units.
(address->pointer ADDRESS)Creates a new foreign pointer object initialized to point to the address given in the integer
ADDRESS
.
(allocate BYTES)Returns a pointer to a freshly allocated region of static memory. This procedure could be defined as follows:
(define allocate (foreign-lambda c-pointer "malloc" integer))
(free POINTER)Frees the memory pointed to by
POINTER
. This procedure could be defined as follows:(define free (foreign-lambda c-pointer "free" integer))
(null-pointer? PTR)Returns
#t
ifPTR
contains aNULL
pointer, or#f
otherwise.
(object->pointer X)Returns a pointer pointing to the Scheme object X, which should be a non-immediate object. Note that data in the garbage collected heap moves during garbage collection.
(pointer->address PTR)Returns the address, to which the pointer
PTR
points.
(pointer->object PTR)Returns the Scheme object pointed to by the pointer
PTR
.
(pointer-offset PTR N)Returns a new pointer representing the pointer
PTR
increased byN
.
(pointer-u8-ref PTR)Returns the unsigned byte at the address designated by
PTR
.
(pointer-s8-ref PTR)Returns the signed byte at the address designated by
PTR
.
(pointer-u16-ref PTR)Returns the unsigned 16-bit integer at the address designated by
PTR
.
(pointer-s16-ref PTR)Returns the signed 16-bit integer at the address designated by
PTR
.
(pointer-u32-ref PTR)Returns the unsigned 32-bit integer at the address designated by
PTR
.
(pointer-s32-ref PTR)Returns the signed 32-bit integer at the address designated by
PTR
.
(pointer-f32-ref PTR)Returns the 32-bit float at the address designated by
PTR
.
(pointer-f64-ref PTR)Returns the 64-bit double at the address designated by
PTR
.
(pointer-u8-set! PTR N) (set! (pointer-u8-ref PTR) N)Stores the unsigned byte
N
at the address designated byPTR
.
(pointer-s8-set! PTR N) (set! (pointer-s8-ref PTR) N)Stores the signed byte
N
at the address designated byPTR
.
(pointer-u16-set! PTR N) (set! (pointer-u16-ref PTR) N)Stores the unsigned 16-bit integer
N
at the address designated byPTR
.
(pointer-s16-set! PTR N) (set! (pointer-s16-ref PTR) N)Stores the signed 16-bit integer
N
at the address designated byPTR
.
(pointer-u32-set! PTR N) (set! (pointer-u32-ref PTR) N)Stores the unsigned 32-bit integer
N
at the address designated byPTR
.
(pointer-s32-set! PTR N) (set! (pointer-s32-ref PTR) N)Stores the 32-bit integer
N
at the address designated byPTR
.
(pointer-f32-set! PTR N) (set! (pointer-f32-ref PTR) N)Stores the 32-bit floating-point number
N
at the address designated byPTR
.
(pointer-f64-set! PTR N) (set! (pointer-f64-ref PTR) N)Stores the 64-bit floating-point number
N
at the address designated byPTR
.
(align-to-word PTR-OR-INT)Accepts either a machine pointer or an integer as argument and returns a new pointer or integer aligned to the native word size of the host platform.
“Tagged” pointers are foreign pointer objects with an extra tag object.
(tag-pointer PTR TAG)Creates a new tagged pointer object from the foreign pointer
PTR
with the tagTAG
, which may an arbitrary Scheme object.
(tagged-pointer? X TAG)Returns
#t
, ifX
is a tagged pointer object with the tagTAG
(using aneq?
comparison), or#f
otherwise.
(pointer-tag PTR)If
PTR
is a tagged pointer object, its tag is returned. IfPTR
is a normal, untagged foreign pointer object#f
is returned. Otherwise an error is signalled.
(extend-procedure PROCEDURE X)Returns a copy of the procedure
PROCEDURE
which contains an additional data slot initialized toX
. IfPROCEDURE
is already an extended procedure, then its data slot is changed to containX
and the same procedure is returned.
(extended-procedure? PROCEDURE)Returns
#t
ifPROCEDURE
is an extended procedure, or#f
otherwise.
(procedure-data PROCEDURE)Returns the data object contained in the extended procedure
PROCEDURE
, or#f
if it is not an extended procedure.
(set-procedure-data! PROCEDURE X)Changes the data object contained in the extended procedure
PROCEDURE
toX
.(define foo (letrec ((f (lambda () (procedure-data x))) (x #f) ) (set! x (extend-procedure f 123)) x) ) (foo) ==> 123 (set-procedure-data! foo 'hello) (foo) ==> hello
(byte-vector FIXNUM ...)Returns a freshly allocated byte-vector with
FIXNUM ...
as its initial contents.
(byte-vector? X)Returns
#t
ifX
is a byte-vector object, or#f
otherwise.
(byte-vector-fill! BYTE-VECTOR N)Sets each element of
BYTE-VECTOR
toN
, which should be an exact integer.
(byte-vector->list BYTE-VECTOR)Returns a list with elements taken from
BYTE-VECTOR
.
(byte-vector->string BYTE-VECTOR)Returns a string with the contents of
BYTE-VECTOR
.
(byte-vector-length BYTE-VECTOR)Returns the number of elements in
BYTE-VECTOR
.
(byte-vector-ref BYTE-VECTOR INDEX)Returns the byte at the
INDEX
th position ofBYTE-VECTOR
.
(byte-vector-set! BYTE-VECTOR INDEX N) (set! (byte-vector-ref BYTE-VECTOR INDEX) N)Sets the byte at the
INDEX
th position ofBYTE-VECTOR
to the value of the exact integern
.
(list->byte-vector LIST)Returns a byte-vector with elements taken from
LIST
, where the elements ofLIST
should be exact integers.
(make-byte-vector SIZE [INIT])Creates a new byte-vector of size
SIZE
. IfINIT
is given, then it should be an exact integer with which every element of the byte-vector is initialized.
(make-static-byte-vector SIZE [INIT])As
make-byte-vector
, but allocates the byte-vector in storage that is not subject to garbage collection. To free the allocated memory, one has to callobject-release
explicitly.Exceptions:
(exn bounds)
,(exn runtime)
(static-byte-vector->pointer PBYTE-VECTOR)Returns a pointer object pointing to the data in the statically allocated byte-vector
PBYTE-VECTOR
.
(string->byte-vector STRING)Returns a byte-vector with the contents of
STRING
.
(object-evict X [ALLOCATOR])Copies the object
X
recursively into the memory pointed to by the foreign pointer object returned byALLOCATOR
, which should be a procedure of a single argument (the number of bytes to allocate). The freshly copied object is returned. This facility allows moving arbitrary objects into static memory, but care should be taken when mutating evicted data: setting slots in evicted vector-like objects to non-evicted data is not allowed. It is possible to set characters/bytes in evicted strings or byte-vectors, though. It is advisable not to evict ports, because they might be mutated by certain file-operations.object-evict
is able to handle circular and shared structures, but evicted symbols are no longer unique: a fresh copy of the symbol is created, so(define x 'foo) (define y (object-evict 'foo)) y ==> foo (eq? x y) ==> #f (define z (object-evict '(bar bar))) (eq? (car z) (cadr z)) ==> #tThe
ALLOCATOR
defaults toallocate
.
(object-evict-to-location X PTR [LIMIT])As
object-evict
but moves the object at the address pointed to by the machine pointerPTR
. If the number of copied bytes exceeds the optionalLIMIT
then an error is signalled (specifically a composite condition of typesexn
andevict
. The latter provides alimit
property which holds the exceeded limit. Two values are returned: the evicted object and a new pointer pointing to the first free address after the evicted object.
(object-evicted? X)Returns
#t
ifX
is a non-immediate evicted data object, or#f
otherwise.
(object-size X)Returns the number of bytes that would be needed to evict the data object
X
.
(object-release X [RELEASER])Frees memory occupied by the evicted object
X
recursively.RELEASER
should be a procedure of a single argument (a foreign pointer object to the static memory to be freed) and defaults tofree
.
(object-unevict X [FULL])Copies the object
X
and nested objects back into the normal Scheme heap. Symbols are re-interned into the symbol table. Strings and byte-vectors are not copied, unlessFULL
is given and not#f
.
A locative is an object that points to an element of a containing object,
much like a “pointer” in low-level, imperative programming languages like “C”. The element can
be accessed and changed indirectly, by performing access or change operations
on the locative. The container object can be computed by calling the
location->object
procedure.
Locatives may be passed to foreign procedures that expect pointer arguments.
The effect of creating locatives for evicted data (see object-evict
) is undefined.
(make-locative EXP [INDEX])Creates a locative that refers to the element of the non-immediate object
EXP
at positionINDEX
.EXP
may be a vector, pair, string, byte-vector, SRFI-4 number-vector, or record.INDEX
should be a fixnum.INDEX
defaults to 0.
(make-weak-locative EXP [INDEX])Creates a “weak” locative. Even though the locative refers to an element of a container object, the container object will still be reclaimed by garbage collection if no other references to it exist.
(locative-ref LOC)Returns the element to which the locative
LOC
refers. If the containing object has been reclaimed by garbage collection, an error is signalled.
(locative-set! LOC X) (set! (locative-ref LOC) X)Changes the element to which the locative
LOC
refers toX
. If the containing object has been reclaimed by garbage collection, an error is signalled.
(locative->object LOC)Returns the object that contains the element referred to by
LOC
or#f
if the container has been reclaimed by garbage collection.
(global-bound? SYMBOL)Returns
#t
, if the global (“toplevel”) variable with the nameSYMBOL
is bound to a value, or#f
otherwise.
(global-ref SYMBOL)Returns the value of the global variable
SYMBOL
. If no variable under that name is bound, an error is signalled.Note that it is not possible to access a toplevel binding with
global-ref
orglobal-set!
if it has been hidden in compiled code via(declare (hide ...))
, or if the code has been compiled inblock
mode.
(global-set! SYMBOL X) (set! (global-ref SYMBOL) X)Sets the global variable named
SYMBOL
to the valueX
.
(block-ref BLOCK INDEX)Returns the contents of the
INDEX
th slot of the objectBLOCK
.BLOCK
may be a vector, record structure, pair or symbol.
(block-set! BLOCK INDEX X) (set! (block-ref BLOCK INDEX) X)Sets the contents of the
INDEX
th slot of the objectBLOCK
to the value ofX
.BLOCK
may be a vector, record structure, pair or symbol.
(object-copy X)Copies
X
recursively and returns the fresh copy. Objects allocated in static memory are copied back into garbage collected storage.
(make-record-instance SYMBOL ARG1 ...)Returns a new instance of the record type
SYMBOL
, with its slots initialized toARG1 ...
. To illustrate:(define-record point x y)expands into something quite similar to:
(begin (define (make-point x y) (make-record-instance 'point x y) ) (define (point? x) (and (record-instance? x) (eq? 'point (block-ref x 0)) ) ) (define (point-x p) (block-ref p 1)) (define (point-x-set! p x) (block-set! p 1 x)) (define (point-y p) (block-ref p 2)) (define (point-y-set! p y) (block-set! p 1 y)) )
(move-memory! FROM TO [BYTES])Copies
BYTES
bytes of memory fromFROM
toTO
.FROM
andTO
may be strings, primitive byte-vectors, SRFI-4 byte-vectors (see: Unit srfi-4), memory mapped files, foreign pointers (as obtained from a call toforeign-lambda
, for example) or locatives. ifBYTES
is not given and the size of the source or destination operand is known then the maximal number of bytes will be copied. Moving memory to the storage returned by locatives will cause havoc, if the locative refers to containers of non-immediate data, like vectors or pairs.
(number-of-bytes BLOCK)Returns the number of bytes that the object
BLOCK
contains.BLOCK
may be any non-immediate value.
(number-of-slots BLOCK)Returns the number of slots that the object
BLOCK
contains.BLOCK
may be a vector, record structure, pair or symbol.
(record-instance? X)Returns
#t
ifX
is an instance of a record type. See also:make-record-instance
.
(record->vector BLOCK)Returns a new vector with the type and the elements of the record
BLOCK
.
(set-invalid-procedure-call-handler! PROC)Sets an internal hook that is invoked when a call to an object other than a procedure is executed at runtime. The procedure
PROC
will in that case be called with two arguments: the object being called and a list of the passed arguments.;;; Access sequence-elements as in ARC: (set-invalid-procedure-call-handler! (lambda (proc args) (cond [(string? proc) (apply string-ref proc args)] [(vector? proc) (apply vector-ref proc args)] [else (error "call of non-procedure" proc)] ) ) ) ("hello" 4) ==> #\oThis facility does not work in code compiled with the “unsafe” setting.
(unbound-variable-value [X])Defines the value that is returned for unbound variables. Normally an error is signalled, use this procedure to override the check and return
X
instead. To set the default behavior (of signalling an error), callunbound-variable-value
with no arguments.This facility does not work in code compiled with the “unsafe” setting.
(object-become! ALIST)Changes the identity of the value of the car of each pair in
ALIST
to the value of the cdr. Both values may not be immediate (i.e. exact integers, characters, booleans or the empty list).(define x "i used to be a string") (define y '#(and now i am a vector)) (object-become! (list (cons x y))) x ==> #(and now i am a vector) y ==> #(and now i am a vector) (eq? x y) ==> #tNote: this operation invokes a major garbage collection.
The effect of using
object-become!
on evicted data (seeobject-evict
) is undefined.
This unit is a port of Gregor Kiczales TinyCLOS with numerous modifications.
This unit uses the extras
unit.
(define-class NAME (SUPERCLASS1 ...) (SLOTNAME1 ...) [METACLASS])Sets the variable
NAME
to a new class (a new instance of the class<class>
).SUPERCLASS1 ...
is a list of superclasses of the newly created class. If no superclasses are given, then<object>
is assumed.SLOTNAME1 ...
are the names of the direct slots of the class. ifMETACLASS
is provided, then the new class-instance is an instance ofMETACLASS
instead of<class>
.(define-class NAME (SUPER) (SLOT1 SLOT2) META)is equivalent to
(define NAME (make META 'name 'NAME 'direct-supers (list SUPER) 'direct-slots (list 'SLOT1 'SLOT2)) )Note that slots-names are not required to be symbols, so the following is perfectly valid:
(define hidden-slot (list 'hidden)) (define <myclass> (make <class> 'direct-supers (list <object>) 'direct-slots (list hidden-slot) ) ) (define x1 (make <myclass>) (slot-set! x1 hidden-slot 99)
(define-generic NAME [CLASS])Sets the variable
NAME
to contain a fresh generic function object without associated methods. If the optional argumentCLASS
is given, then the generic function will be an instance of that class.
(define-method (NAME (VARIABLE1 CLASS1) ... PARAMETERS ...) BODY ...)Adds a new method with the code
BODY ...
to the generic function that was assigned to the variablename
.CLASS1 ...
is a list if classes that specialize this particular method. The method can have additional parametersPARAMETERS
, which do not specialize the method any further. Extended lambda-lists are allowed (#!optional, #!key
or#!rest
argument lists), but can not be specialized. Inside the body of the method the identifiercall-next-method
names a procedure of zero arguments that can be invoked to call the next applicable method with the same arguments. If no generic function is defined under this name, then a fresh generic function object is created and assigned toNAME
.Note that only
define-generic
expands into a valid definition, so for internal lexically scoped definitions usedefine-generic
.Currently methods defined with
define-method
should not be hidden (via(declare (hide ...))
, nor should such files be compiled inblock
mode, unless the methods are exported.
(add-method GENERIC METHOD)Adds the method object
METHOD
to the list of applicable methods for the generic functionGENERIC
.
(make CLASS INITARG ...)Creates a new instance of
CLASS
and passesINITARG ...
to theinitialize
method of this class.
(make-class SUPERCLASSES SLOTNAMES)Creates a new class object, where
SUPERCLASSES
should be the list of direct superclass objects andSLOTNAMES
should be a list of symbols naming the slots of this class.
(make-generic [NAME])Creates a new generic function object. If
NAME
is specified, then it should be a string.
(make-method SPECIALIZERS PROC)Creates a new method object specialized to the list of classes in
SPECIALIZERS
.(define-method (foo (x <bar>)) 123) <=> (add-method foo (make-method (list <bar>) (lambda (call-next-method x) 123)))
(slot-ref INSTANCE SLOTNAME)Returns the value of the slot
SLOTNAME
of the objectINSTANCE
.
(slot-set! INSTANCE SLOTNAME VALUE) (set! (slot-ref INSTANCE SLOTNAME) VALUE)Sets the value of the slot
SLOTNAME
of the objectINSTANCE
toVALUE
.
(class-cpl CLASS)Returns the class-precedence-list of
CLASS
as a list of classes.
(class-direct-slots CLASS)Returns the list of direct slots of
CLASS
as a list of lists, where each sublist contains the name of the slot.
(class-direct-supers CLASS)Returns the list of direct superclasses of
CLASS
.
(class-slots CLASS)Returns the list of all slots of
CLASS
and its superclasses as a list of lists, where each sublist contains the name of the slot.
(generic-methods GENERIC)Returns the list of all methods associated with the generic function
GENERIC
.
(method-specializers METHOD)Returns the list of classes that specialize
METHOD
.
(method-procedure METHOD)Returns the procedure that contains the body of
METHOD
.
(subclass? CLASS1 CLASS2)Returns
#t
isCLASS1
is a subclass ofCLASS2
, or#f
otherwise. Note that the following holds:(subclass? X X) ==> #t
(instance-of? X CLASS)Returns
#t
ifX
is an instance ofCLASS
(or one of its subclasses).
These definitions allow interfacing to the Meta Object Protocol
of TinyCLOS. For serious use, it is recommended to consult
the source code (tinyclos.scm
).
(allocate-instance CLASS)Allocates storage for an instance of
CLASS
and returns the instance.
(compute-apply-generic GENERIC)Returns a procedure that will be called to apply the generic function methods to the arguments.
(compute-apply-methods GENERIC)Returns a procedure of two arguments, a list of applicable methods and a list of arguments and applies the methods.
(compute-methods GENERIC)Returns a procedure of one argument. The procedure is called with the list of actual arguments passed to the generic function and should return a list of applicable methods, sorted by precedence.
(compute-getter-and-setter CLASS SLOT ALLOCATOR)Returns two values, the procedures that get and set the contents of the slot
SLOT
.ALLOCATOR
is a procedure of one argument that gets an initalizer function and returns the getter and setter procedures for the allocated slot.
(compute-method-more-specific? GENERIC)Returns a procedure of three arguments (two methods and a list of arguments) that returns
#t
if the first method is more specific than the second one with respect to the list of arguments. Otherwise the returned predicate returns#f
.
(initialize INSTANCE INITARGS)Initializes the object
INSTANCE
.INITARGS
is the list of initialization arguments that were passed to themake
procedure.
(describe-object INSTANCE [PORT])Writes a description of
INSTANCE
toPORT
. Execution of the interpreter command,d
will invoke this generic function. IfPORT
is not given it defaults to the value of(current-output-port)
.
(print-object INSTANCE [PORT])Writes a textual representation of
INSTANCE
toPORT
. Any output of an instance withdisplay, write
andPORT
is not given it defaults to the value of(current-output-port)
.
(initialize-slots INSTANCE INITARGS)This procedure takes a sequence of alternating slot-names and initialization values in
INITARGS
and initializes the corresponding slots inINSTANCE
.(define-class <pos> () (x y)) (define-method (initialize (pos <pos>) initargs) (call-next-method) (initialize-slots pos initargs)) (define p1 (make <pos> 'x 1 'y 2)) (define p2 (make <pos> 'x 3 'y 5))
The class hierarchy of builtin classes looks like this:
<top> <object> <class> <procedure-class> <procedure> <entity-class> <generic> <primitive-class> <c++-object> <primitive> <void> <boolean> <symbol> <char> <vector> <pair> <number> <integer> <exact> <inexact> <string> <port> <input-port> <output-port> <pointer> <tagged-pointer> <swig-pointer> <locative> <byte-vector> <u8vector> <s8vector> <u16vector> <s16vector> <u32vector> <s32vector> <f32vector> <f64vector> <structure> <char-set> <condition> <condition-variable> <environment> <hash-table> <lock> <mmap> <mutex> <promise> <queue> <read-table> <regexp> <tcp-listener> <thread> <time> <end-of-file>
The classes of primitive Scheme objects.
The classes of extended data types provided by the various library units.
The parent class of objects that can be invoked as a procedure and have slots.
The parent class of objects that can be invoked as a procedure.
The classes of data objects provided by the various supported SRFIs.
Classes of objects used in the
posix
library unit.
A machine pointer (untagged, tagged or pointing to SWIG-wrapped data).
The class of generated wrappers for C++ classes parsed by the “easy” Foreign Function interface.
The CHICKEN distribution provides several examples in the file
tests/tinyclos-examples.scm
.
(foreign-code STRING)Executes the embedded C/C++ code
STRING
, which should be a sequence of C statements, which are executed and return an unspecified result.(foreign-code "doSomeInitStuff();") => #<unspecified>Code wrapped inside
foreign-code
may not invoke callbacks into Scheme.
(foreign-value STRING TYPE)Evaluates the embedded C/C++ expression
STRING
, returning a value of type given in the foreign-type specifierTYPE
.(print (foreign-value "my_version_string" c-string))
(foreign-declare STRING ...)Include given strings verbatim into header of generated file.
(foreign-parse STRING ...)Parse given strings and generate foreign-interface bindings. See The Easy Foreign Function Interface for more information.
(foreign-parse/declare STRING ...)Parse and include strings into the generated code.
(define-foreign-type NAME TYPE [ARGCONVERT [RETCONVERT]])Defines an alias for
TYPE
with the nameNAME
(a symbol).TYPE
may be a type-specifier or a string naming a C type. The namespace of foreign type specifiers is separate from the normal Scheme namespace. The optional argumentsARGCONVERT
andRETCONVERT
should evaluate to procedures that map argument- and result-values to a value that can be transformed toTYPE
:(define-foreign-type char-vector nonnull-c-string (compose list->string vector->list) (compose list->vector string->list) ) (define strlen (foreign-lambda int "strlen" char-vector) ) (strlen '#(#\a #\b #\c)) ==> 3 (define memset (foreign-lambda char-vector "memset" char-vector char int) ) (memset '#(#_ #_ #_) #\X 3) ==> #(#\X #\X #\X)Foreign type-definitions are only visible in the compilation-unit in which they are defined, so use
include
to use the same definitions in multiple files.
(define-foreign-variable NAME TYPE [STRING])Defines a foreign variable of name
NAME
(a symbol).STRING
should be the real name of a foreign variable or parameterless macro. IfSTRING
is not given, then the variable nameNAME
will be converted to a string and used instead. All references and assignments (viaset!
) are modified to correctly convert values between Scheme and C representation. This foreign variable can only be accessed in the current compilation unit, but the name can be lexically shadowed. Note thatSTRING
can name an arbitrary C expression. If no assignments are performed, thenSTRING
doesn't even have to specify an lvalue.#> enum { abc=3, def, ghi }; <# (define-macro (define-simple-foreign-enum . items) `(begin ,@(map (match-lambda [(name realname) `(define-foreign-variable ,name int ,realname)] [name `(define-foreign-variable ,name int)] ) items) ) ) (define-simple-foreign-enum abc def ghi) ghi ==> 5
(define-foreign-record NAME [DECL ...] SLOT ...)Defines accessor procedures for a C structure definition.
NAME
should either be a symbol or a list of the form(TYPENAME FOREIGNNAME)
. IfNAME
is a symbol, then a C declaration will be generated that defines a C struct namedstruct NAME
. IfNAME
is a list, then no struct declaration will be generated. A foreign-type specifier namedNAME
(orTYPENAME
) will be defined as a pointer to the given C structure. ASLOT
definition should be a list of one of the following forms:(TYPE SLOTNAME)or
(TYPE SLOTNAME SIZE)The latter form defines an array of SIZE elements of the type TYPE embedded in the structure. For every slot, the following accessor procedures will be generated:
— procedure: TYPENAME-SLOTNAME
(TYPENAME-SLOTNAME FOREIGN-RECORD-POINTER [INDEX])A procedure of one argument (a pointer to a C structure), that returns the slot value of the slot
SLOTNAME
. If aSIZE
has been given in the slot definition, then an additional argumentINDEX
is required that specifies the index of an array-element.— procedure: TYPENAME-SLOTNAME-set!
(TYPENAME-SLOTNAME-set! FOREIGN-RECORD-POINTER [INXDEX] VALUE)A procedure of two arguments (a pointer to a C structure) and a value, that sets the slot value of the slot
SLOTNAME
in the structure. If aSIZE
has been given in the slot definition, then an additional argumentINDEX
is required for the array index.If a slot type is of the form
(const ...)
, then no setter procedure will be generated. Slots of the types(struct ...)
or(union ...)
are accessed as pointers to the embedded struct (or union) and no setter will be generated.Additionally, special record-declarations (
DECL ...
) may be given, where each declaration consists of a list of the form(KEYWORD ARGUMENT ...)
. The available declarations are:— record declaration: constructor:
(constructor: NAME)Generate a constructor-procedure with no arguments that has the name
NAME
(a symbol) that returns a pointer to a structure of this type. The storage will be allocated withmalloc(3)
.— record declaration: destructor:
(destructor: NAME)Generate a destructor function with the name
NAME
that takes a pointer to a structure of this type as its single argument and releases the storage withfree(3)
. If the argument is#f
, the destructor procedure does nothing.— record declaration: rename:
(rename: EXPRESSION)Evaluates
EXPRESSION
at compile-/macro-expansion-time and applies the result, which should be a procedure, to the string-representation of the name of each accessor-procedure generated. Another (or the same) string should be returned, which in turn is taken as the actual name of the accessor.An example:
(require-for-syntax 'srfi-13) (define-foreign-record Some_Struct (rename: (compose string-downcase (cut string-translate <> "_" "-"))) (constructor: make-some-struct) (destructor: free-some-struct) (int xCoord) (int yCoord) )will generate the following procedures:
(make-some-struct) --> C-POINTER (free-some-struct C-POINTER) (some-struct-xcoord C-POINTER) --> NUMBER (some-struct-ycoord C-POINTER) --> NUMBER (some-struct-xcoord-set! C-POINTER NUMBER) (some-struct-ycoord-set! C-POINTER NUMBER)
(define-foreign-enum TYPENAME ITEM ...)Defines a foreign type (as with
define-foreign-type
) that maps the elements of a C/C++ enum (or a enum-like list of constants) to and from a set of symbols.TYPENAME
names a foreign type that converts a symbol argument from the setITEM ...
into the appropriate enum value when passed as an argument to a foreign function. A list of symbols passed as an argument will be combined usingbitwise-ior
. An empty list will be passed as 0 (zero). Results of the enum type are automatically converted into a symbol (note that combinations are not supported in this case).TYPENAME
may alternatively be a list of the form(SCHEMENAME REALTYPE)
- in this caseREALTYPE
designates the native type used. The default native type is"TYPENAME"
. Additionally two procedures are generated namedSCHEMENAME->number
andnumber->SCHEMENAME
which take one argument and convert a symbol (or a list of symbols) into its numeric value and vice versa.Here a heavily contrived example:
#> enum foo { a_foo = 4, b_foo, c_foo }; enum foo bar(enum foo x) { printf("%d\n", x); return b_foo; } <# (define-foreign-enum (foo (enum "foo")) a_foo b_foo (c c_foo)) (define bar (foreign-lambda foo bar foo)) (pp (bar '())) (pp (bar 'a_foo)) (pp (bar '(b_foo c)))
(foreign-lambda RETURNTYPE NAME ARGTYPE ...)Represents a binding to an external routine. This form can be used in the position of an ordinary
lambda
expression.NAME
specifies the name of the external procedure and should be a string or a symbol.
(foreign-lambda* RETURNTYPE ((ARGTYPE VARIABLE) ...) STRING ...)Similar to
foreign-lambda
, but instead of generating code to call an external function, the body of the C procedure is directly given inSTRING ...
:(define my-strlen (foreign-lambda* int ((c-string str)) "int n = 0; while(*(str++)) ++n; C_return(n);") ) (my-strlen "one two three") ==> 13For obscure technical reasons you should use the
C_return
macro instead of the normalreturn
statement to return a result from the foreign lambda body as some cleanup code has to be run before execution commences in the calling code.
(foreign-safe-lambda RETURNTYPE NAME ARGTYPE ...)This is similar to
foreign-lambda
, but also allows the called function to call Scheme functions and allocate Scheme data-objects. See Callbacks.
(foreign-safe-lambda* RETURNTYPE ((ARGTYPE VARIABLE)...) STRING ...)This is similar to
foreign-lambda*
, but also allows the called function to call Scheme functions and allocate Scheme data-objects. See Callbacks.
(foreign-primitive [RETURNTYPE] ((ARGTYPE VARIABLE) ...) STRING ...)This is also similar to
foreign-lambda*
but the code will be executed in a “primitive” CPS context, which means it will not actually return, but call it's continuation on exit. This means that code inside this form may allocate Scheme data on the C stack (the “nursery”) withC_alloc
(see below). If theRETURNTYPE
is omitted it defaults tovoid
. You can return multiple values inside the body of theforeign-primitive
form by calling this C function:C_values(N + 2, C_SCHEME_UNDEFINED, C_k, X1, ...)where
N
is the number of values to be returned, andX1, ...
are the results, which should be Scheme data objects. When returning multiple values, the return-type should be omitted.
($ [RETURNTYPE] NAME (TYPE ARGUMENT) ...)Invokes a C/C++ function by evaluating the arguments, performing the neccessary type-conversions for the foreign type-specifiers and calling
NAME
(which must be a symbol). If a return-type is given, then the result will be converted properly and returned. Callbacks into Scheme are not allowed inside the invoked foreign code. The type/argument lists may also be literal Scheme data or expressions of the form(quote LITERAL)
or(location ...)
- in this case the arguments are converted according to the type of the literal or are treated as a c-pointer, respectively. Booleans, characters, numbers, strings, SRFI-4 number vectors and symbols are converted, all other data is passed as being of typescheme-object
.($ printf "%d times Hello, %s!\n" 1000 "world") (define f 99.2) (let-location ((double n)) (let ((f ($ double modf (double f) (location n)))) (cons n f) ) )If no return type is given,
void
is assumed. This macro expands into one of theforeign-lambda*
,foreign-code
orforeign-value
forms.
Here is a list of valid foreign type specifiers:
scheme-object
bool
#f
is false, anything else is true).
As result: anything different from 0 and the NULL
-pointer
is #t
.
byte unsigned-byte
char unsigned-char
short unsigned-short
int unsigned-int
int32 unsigned-int32
integer unsigned-integer
integer32 unsigned-integer32
integer64
long unsigned-long
float double
number
double
, but when used as a result
type, then either an exact integer or a floating-point number is returned, depending
on whether the result fits into an exact integer or not.
symbol
scheme-pointer
#f
is also allowed
and is passed as a NULL
pointer.
Don't confuse this type with (pointer ...)
which means something
different (a machine-pointer object).
nonnull-scheme-pointer
pointer
, but guaranteed not to be #f
.
Don't confuse this type with (nonnull-pointer ...)
which means something
different (a machine-pointer object).
c-pointer
#f
is also
allowed and is passed as a NULL
pointer. If uses as the type of
a return value, a NULL
pointer will be returned as #f
.
nonnull-c-pointer
c-pointer
, but guaranteed not to be #f/NULL
.
scheme-or-c-pointer
scheme-pointer
.
Not allowed as a result type.
[nonnull-] byte-vector
byte-vector
may optionally be #f
, which is
passed as a NULL pointer. This is not allowed as a return type.
[nonnull-] u8vector
[nonnull-] u16vector
[nonnull-] u32vector
[nonnull-] s8vector
[nonnull-] s16vector
[nonnull-] s32vector
[nonnull-] f32vector
[nonnull-] f64vector
byte-vector
may optionally be
#f
, which is passed as a NULL pointer. These are not allowed
as return types.
c-string
#f
is also allowed and is passed as a NULL
pointer. If uses as
the type of a return value, a NULL
pointer will be returned as
#f
. Note that the string is copied (with a zero-byte appended)
when passed as an argument to a foreign function. Also a return value
of this type is copied into garbage collected memory.
nonnull-c-string
c-string
, but guaranteed not to be #f/NULL
.
[nonnull-] c-string*
[nonnull-]c-string
, but if used as a result-type,
the pointer returned by the foreign code will be freed (using the
C-libraries free()
)
after copying. This type specifier is not valid as a result type
for callbacks defined with define-external
.
void
(const TYPE)
TYPE
with an additional const
specifier.
(enum NAME)
integer
.
(pointer TYPE)
(c-pointer TYPE)
TYPE
.
(nonnull-pointer TYPE)
(nonnull-c-pointer TYPE)
(pointer TYPE)
, but guaranteed not to be #f/NULL
.
(ref TYPE)
(struct NAME)
NAME
, which should be a string. Structs
can not be directly passed as arguments to foreign function, neither
can they be result values. Pointers to structs are allowed, though.
(template TYPE ARGTYPE ...)
vector<int>
would be specified
as (template "vector" int)
. Template types can not be directly passed
as arguments or returned as results.
(union NAME)
NAME
, which should be a string. Unions can
not be directly passed as arguments to foreign function, neither can
they be result values. Pointers to unions are allowed, though.
(instance CNAME SCHEMECLASS)
CNAME
should designate
the name of the C++ class, and SCHEMECLASS
should be the class
that wraps the instance pointer. Normally SCHEMECLASS
should be a subclass of <c++-object>
.
(instance-ref CNAME SCHEMECLASS)
(function RESULTTYPE (ARGUMENTTYPE1 ... [...]) [CALLCONV])
CALLCONV
specifies an optional calling
convention and should be a string. The meaning of this string is entirely
platform dependent. The value #f
is also allowed and is passed
as a NULL
pointer.
Foreign types are mapped to C types in the following manner:
bool
[unsigned-]char
[unsigned-]short
[unsigned-]int
[unsigned-]integer
[unsigned-]long
float
double
number
[nonnull-]pointer
[nonnull-]c-pointer
[nonnull-]byte-vector
[nonnull-]u8vector
[nonnull-]s8vector
[nonnull-]u16vector
[nonnull-]s16vector
[nonnull-]u32vector
[nonnull-]s32vector
[nonnull-]f32vector
[nonnull-]f64vector
[nonnull-]c-string
symbol
void
([nonnull-]pointer TYPE)
(enum NAME)
(struct NAME)
(ref TYPE)
(template T1 T2 ...)
(union NAME)
(function RTYPE (ATYPE ...) [CALLCONV])
(instance CNAME SNAME)
(instance-ref CNAME SNAME)
Compiled Scheme files can be linked with C code, provided the Scheme code was compiled
in “embedded” mode by passing -DC_EMBEDDED
to the C compiler (this will
disable generation of a main()
function). csc
will do this, when given
the -embedded
option. Alternatively pass -embedded
to csc
.
The following C API is available:
Parse the programs command-line contained in
argc
andargv
and return the heap-, stack- and symbol table limits given by runtime options of the form-:...
, or choose default limits. The library procedureargv
can access the command-line only if this function has been called by the containing application.
Initializes the Scheme execution context and memory.
heap
holds the number of bytes that are to be allocated for the secondary heap.stack
holds the number of bytes for the primary heap.symbols
contains the size of the symbol table. Passing0
to one or more of these parameters will select a default size.toplevel
should be a pointer to the toplevel entry point procedure. You should passC_toplevel
here. In any subsequent call toCHICKEN_run
you can simply passNULL
. Calling this function more than once has no effect. If enough memory is available and initialization was successful, then1
is returned, otherwise this function returns0
.
Starts the Scheme program. Call this function once to execute all toplevel expressions in your compiled Scheme program. If the runtime system was not initialized before, then
CHICKEN_initialize
is called with default sizes.toplevel
is the toplevel entry-point procedure, you usually passC_toplevel
here. The result value is the continuation that can be used to re-invoke the Scheme code from the point after it calledreturn-to-host
(see below).If you just need a Scheme interpreter, you can also pass
CHICKEN_default_toplevel
as the toplevel procedure, which just uses the default library units.
Once CHICKEN_run
has been called, Scheme code is executing until all toplevel
expressions have been evaluated or until return-to-host
is called inside the
Scheme program.
(return-to-host)Exits the Scheme code and returns to the invoking context that called
CHICKEN_run
orCHICKEN_continue
.
After return-to-host
has been executed and once CHICKEN_run
returns,
you can invoke callbacks which have been defined with define-external
.
The eval
library unit also provides “boilerplate” callbacks, that simplify invoking Scheme
code embedded in a C or C++ application a lot.
Evaluates the Scheme object passed in
exp
, writing the result value toresult
. The return value is 1 if the operation succeeded, or 0 if an error occurred. CallCHICKEN_get_error_message
to obtain a description of the error.
Evaluates the Scheme expression passed in the string
str
, writing the result value toresult
.
Evaluates the Scheme expression passed in
exp
, writing a textual representation of the result intoresult
.size
should specify the maximal size of the result string.
Evaluates the Scheme expression passed in the string
str
, writing a textual representation of the result intoresult
.size
should specify the maximal size of the result string.
Applies the procedure passed in
func
to the list of argumentsargs
, writing the result value toresult
.
Applies the procedure passed in
func
to the list of argumentsargs
, writing a textual representation of the result intoresult
.
Reads a Scheme object from the string
str
, writing the result value toresult
.
Loads the Scheme file
filename
(either in source form or compiled).
Returns a textual description of the most recent error that occurred in executing embedded Scheme code.
If threads have been spawned during earlier invocations of embedded Scheme code, then this function will run the next scheduled thread for one complete time-slice. This is useful, for example, inside an “idle” handler in a GUI application with background Scheme threads. Note that the
srfi-18
library unit has to be linked in for this.
An example:
% cat x.scm ;;; x.scm (define (bar x) (gc) (* x x)) (define-external (baz (int i)) double (sqrt i) % cat y.c /* y.c */ #include "chicken.h" #include <assert.h> extern double baz(int); int main() { char buffer[ 256 ]; int status; C_word val = C_SCHEME_UNDEFINED; C_word *data[ 1 ]; data[ 0 ] = &val; CHICKEN_run(C_toplevel); status = CHICKEN_read("(bar 99)", &val); assert(status); C_gc_protect(data, 1); printf("data: %08x\n", val); status = CHICKEN_eval_string_to_string("(bar)", buffer, 255); assert(!status); CHICKEN_get_error_message(buffer, 255); printf("ouch: %s\n", buffer); status = CHICKEN_eval_string_to_string("(bar 23)", buffer, 255); assert(status); printf("-> %s\n", buffer); printf("data: %08x\n", val); status = CHICKEN_eval_to_string(val, buffer, 255); assert(status); printf("-> %s\n", buffer); printf("->` %g\n", baz(22)); return 0; } % csc x.scm y.c -embedded
It is also possible to re-enter the computation following the call to return-to-host
by calling
CHICKEN_continue
:
Re-enters Scheme execution.
k
is the continuation received from the previous invocation ofCHICKEN_run
orCHICKEN_continue
. Whenreturn-to-host
is called again, this function returns another continuation that can be used to restart again.Note: if you invoke callbacks prior to calling
CHICKEN_continue
, make sure that the continuation is not reclaimed by garbage collection. This can be avoided by usingC_gc_protect
or gc-roots.
Another example:
% cat x.scm (require-extension srfi-18) (define m (make-mutex)) (define (t) (mutex-lock! m) (thread-sleep! 1) (print (thread-name (current-thread))) (mutex-unlock! m) (t) ) (thread-start! (make-thread t 'PING!)) (thread-start! (make-thread t 'PONG!)) (let loop () (return-to-host) (thread-yield!) (loop) ) % cat y.c #include "chicken.h" int main() { C_word k = CHICKEN_run(C_toplevel); for(;;) k = CHICKEN_continue(k); return 0; } % csc x.scm y.c -embedded
A simpler interface For handling GC-safe references to Scheme data are the so called “gc-roots”:
Returns a pointer to a “GC root”, which is an object that holds a reference to a Scheme value that will always be valid, even after a garbage collection. The content of the gc root is initialized to an unspecified value.
Sets the content of the GC root to a new value.
Sometimes it is handy to access global variables from C code:
Returns a GC root that holds the global variable with the name
name
. If no such variable exists,NULL
is returned.
Returns the value of the global variable referenced by the GC root
global
.
Sets the value of the global variable referenced by the GC root
global
tovalue
.
To enable an external C function to call back to Scheme, the form
foreign-safe-lambda
(or foreign-safe-lambda*
)
has to be used. This generates special code to save and restore important
state information during execution of C code. There are two ways of
calling Scheme procedures from C: the first is to invoke the runtime
function C_callback
with the closure to be called and the number
of arguments. The second is to define an externally visible wrapper
function around a Scheme procedure with the define-external
form.
Note: the names of all functions, variables and macros exported by the
CHICKEN runtime system start with “C_
”. It is advisable to
use a different naming scheme for your own code to avoid name clashes.
Callbacks (defined by define-external
)
do not capture the lexical environment.
Non-local exits leaving the scope of the invocation of a callback from Scheme into C will not remove the C call-frame from the stack (and will result in a memory leak).
(define-external [QUALIFIERS] (NAME (ARGUMENTTYPE1 VARIABLE1) ...) RETURNTYPE BODY ...) (define-external NAME TYPE [INIT])The first form defines an externally callable Scheme procedure.
NAME
should be a symbol, which, when converted to a string, represents a legal C identifier.ARGUMENTTYPE1 ...
andRETURNTYPE
are foreign type specifiers for the argument variablesVAR1 ...
and the result, respectively.QUALIFIERS
is an optional qualifier for the foreign procedure definition, like__stdcall
.(define-external (foo (c-string x)) int (string-length x))The second form of
define-external
can be used to define variables that are accessible from foreign code. It declares a global variable named by the symbolNAME
that has the typeTYPE
.INIT
can be an arbitrary expression that is used to initialize the variable.NAME
is accessible from Scheme just like any other foreign variable defined bydefine-foreign-variable
.(define-external foo int 42) ((foreign-lambda* int () "C_return(foo);")) ==> 42Note: don't be tempted to assign strings or bytevectors to external variables. Garbage collection moves those objects around, so it is very bad idea to assign pointers to heap-data. If you have to do so, then copy the data object into statically allocated memory (for example by using
object-evict
).Results of type
scheme-object
returned bydefine-external
are always allocated in the secondary heap, that is, not in the stack.
This function can be used to invoke the Scheme procedure
closure
.argc
should contain the number of arguments that are passed to the procedure on the temporary stack. Values are put onto the temporary stack with theC_save
macro.
The runtime-system uses the stack as a special allocation area and internally holds pointers to estimated limits to distinguish between Scheme data objects inside the stack from objects outside of it. If you invoke callbacks at wildly differing stack-levels, these limits may shift from invocation to invocation. Callbacks defined with
define-external
will perform appropriate adjustments automatically, but if you invokeC_callback
manually, you should perform aC_callback_adjust_stack_limits
to make sure the internal limits are set properly.ptr
should point to some data object on the stack. The call will make sure the limits are adjusted so that the value pointed to byptr
is located in the stack.
It is also possible to define variables containing unboxed C data, so called locations. It should be noted that locations may only contain simple data, that is: everything that fits into a machine word, and double-precision floating point values.
(define-location NAME TYPE [INIT])Identical to
(define-external NAME TYPE [INIT])
, but the variable is not accessible from outside of the current compilation unit (it is declaredstatic
).
(let-location ((NAME TYPE [INIT]) ...) BODY ...)Defines a lexically bound location.
(location NAME) (location X)This form returns a pointer object that contains the address of the variable
NAME
. If the argument tolocation
is not a location defined bydefine-location
,define-external
orlet-location
, then(location X)is essentially equivalent to
(make-locative X)(See the manual chapter or
locatives
for more information about locatives.Note that
(location X)
may be abbreviated as#$X
.(define-external foo int) ((foreign-lambda* void (((pointer int) ip)) "*ip = 123;") (location foo)) foo ==> 123This facility is especially useful in situations, where a C function returns more than one result value:
#> #include <math.h> <# (define modf (foreign-lambda double "modf" double (pointer double)) ) (let-location ([i double]) (let ([f (modf 1.99 (location i))]) (print "i=" i ", f=" f) ) )
location
returns a value of type c-pointer
, when given
the name of a callback-procedure defined with define-external
.
(argc+argv)Returns two values: an integer and a foreign-pointer object representing the
argc
andargv
arguments passed to the current process.
The compiler contains a builtin parser for a restricted subset of C and C++ that
allows the easy generation of foreign variable declarations, procedure bindings and
C++ class wrappers. The parser is invoked via the declaration-specifier
foreign-parse
, which extracts binding information and generates
the necessary code. An example:
(foreign-declare " #include <math.h> #define my_pi 3.14 ") (foreign-parse "extern double sin(double);") (print (sin 3.14))
The parser would generate code that is equivalent to
(foreign-declare " #include <math.h> #define my_pi 3.14 ") (define-foreign-variable my_pi float "my_pi") (define sin (foreign-lambda double "sin" double))
Note that the read syntax #>[SPEC] ... <#
provides a somewhat simpler way of using the parser. The example above could
alternatively be expressed as
#>! #define my_pi 3.14 extern double sin(double); <# (print (sin 3.14))
Another example, here using C++. Consider the following class:
// file: foo.h class Foo { private: int x_; public: Foo(int x); void setX(int x); int getX(); };
To generate a wrapper class that provides generic functions for the
constructor and the setX
and getX
methods, we
can use the following class definition:
; file: test-foo.scm (require-extension tinyclos) #>! #include "Foo.h" <# (define x (make <Foo> 99)) (print (getX x)) ; prints ``99'' (setX x 42) (print (getX x)) ; prints ``42'' (destroy x)
Provided the file foo.o
contains the implementation of the class Foo
, the
given example could be compiled like this (assuming a UNIX like environment):
% csc test-foo.scm foo.o -c++
Here is another example, a minimal “Hello world” application for QT. We can see the three different ways of embedding C/C++ code in Scheme:
; compile like this: ; csc hello.scm -c++ -C -IQTDIR/include -L "-LQTDIR/lib -lqt" (require-extension tinyclos) ; Include into generated code, but don't parse: #> #include <qapplication.h> #include <qpushbutton.h> <# ; Parse but don't embed: we only want wrappers for a few classes: #>? class QWidget { public: void resize(int, int); void show(); }; class QApplication { public: QApplication(int, char **); ~QApplication(); void setMainWidget(QWidget *); void exec(); }; class QPushButton : public QWidget { public: QPushButton(char *, QWidget *); ~QPushButton(); } <# (define a (apply make <QApplication> (receive (argc+argv)))) (define hello (make <QPushButton> "hello world!" #f)) (resize hello 100 30) (setMainWidget a hello) (show hello) (exec a) (destroy hello) (destroy a)
#> ... <#
SyntaxOccurrences of the special read syntax #>[SPEC ...] ...<#
will be handled according to
SPEC
:
If SPEC
is the ?
character, the text following up to the next <#
will be processed as a (declare (foreign-parse "..."))
declaration (the code will be processed
by the FFI parser described in this section).
If SPEC
is the !
character, the text will be embedded as
(foreign-parse(declare "...")
It will be both included verbatim in the declaration section of the generated C/C++ file and processed by the FFI parser.
If SPEC
is the :
character, the text will be so it will be executed at the location where it appears.
If SPEC
is a list of the form (TAG ...)
, then each TAG
(which should be a symbol)
specifies what should be done with the text:
declare
parse
execute
If any other character follows the #>
, then the complete text will be included verbatim in the declaration
part of the generated file (as in a foreign-declare
form).
The parser will generally perform the following functions
1) Translate macro, enum-definitions and constants into define-foreign-variable
or define-constant
forms
2) Translate function prototypes into foreign-lambda
forms
3) Translate variable declarations into accessor procedures
4) Handle basic preprocessor operations
5) Translate simple C++ class definitions into TinyCLOS wrapper classes and methods
Basic token-substitution of macros defined via #define
is performed. The
preprocessor commands #ifdef
, #ifndef
, #else
, #endif
, #undef
and #error
are handled. The preprocessor commands #if
and #elif
are not supported and will signal
an error when encountered by the parser, because C expressions (even if constant) are not parsed.
The preprocessor command #pragma
is allowed but will be ignored.
During processing of foreign-parse
declarations the macro CHICKEN
is defined (similar
to the C compiler option -DCHICKEN
).
Macro- and type-definitions are available in subsequent foreign-parse
declarations.
C variables declared generate a procedure with zero or one argument with the same name
as the variable. When called with no arguments, the procedure returns the current value of the
variable. When called with an argument, then the variable is set to the value of that argument.
C and C++ style comments are supported. Variables declared as const
will generate normal Scheme variables, bound to the initial value of the variable.
Function-, member-function and constructor/destructor definitions may be preceded by the ___safe
qualifier, which marks the function as (possibly) performing a callback into Scheme. If a wrapped function
calls back into Scheme code, and ___safe
has not been given very strange and hard to debug
problems will occur. For backward compatibilty, ___callback
is also allowed in place of ___safe
.
Functions and member functions prefixed with ___discard
and a result type that maps to
a Scheme string (c-string
), will have their result type changed to c-string*
instead.
Constants (as declared by #define
or enum
) are not visible outside of the current
Compilation units unless the export_constants
pseudo declaration has been used.
Only numeric or character constants are directly supported.
When given the option -ffi
, CHICKEN will compile a C/C++ file in “Scheme” mode, that is,
it wraps the C/C++ source inside #>! ... <#
and compiles it while generating Scheme
bindings for exported definitions.
Function-arguments may be preceded by ___in
, ___out
and ___inout
qualifiers to specify values that are passed by reference to a function, or returned by
reference. Only basic types (booleans, numbers and characters) can be passed using this method.
During the call a pointer to a temporary piece of storage containing the initial value (or a random
value, for ___out
parameters) will
be allocated and passed to the wrapped function. This piece of storage is subject to
garbage collection and will move, should a callback into Scheme occur that triggers
a garbage collection. Multiple __out
and ___inout
parameters will
be returned as multiple values, preceded by the normal return value of thhe function
(if not void
). Here is a simple example:
#>! #ifndef CHICKEN #include <math.h> #endif double modf(double x, ___out double *iptr); <# (let-values ([(frac int) (modf 33.44)]) ...)
Function-arguments may be preceded by ___length(ID)
, where ID
designates
the name of another argument that must refer to a number vector or string argument. The value
of the former argument will be computed at run-time and thus can be omitted:
#>! (require-extension srfi-4) double sumarray(double *arr, ___length(arr) int len) { double sum = 0; while(len--) sum += *(arr++); return sum; } <# (print (sumarray (f64vector 33 44 55.66)))
The length variable may be positioned anywhere in the argument list. Length markers may only be
specified for arguments passed as SRFI-4 byte-vectors, byte-vectors (as provided by the
lolevel
library unit) or strings.
Structure and union definitions containing actual field declarations generate getter procedures
(and setter procedures when
declared ___mutable
or the mutable_fields
pseudo declaration has been used)
The names of these procedures are computed by concatenating the struct (or union) name,
a hyphen ("-"
) and the field name, and the string "-set!"
, in the case of
setters. Structure definitions with fields may not be used in positions where a type
specifier is normally expected. The field accessors operate on struct/union pointers only.
Additionally a zero-argument procedure named make-<structname>
will be generated
that allocates enough storage to hold an instance of the structure (or union).
Prefixing the definition with ___abstract
will omit the creation procedure.
#>! struct My_struct { int x; ___mutable float y; }; typedef struct My_struct My_struct; My_struct *make_struct(int x, float y) { My_struct *s = (My_struct *)malloc(sizeof(My_struct)); s->x = x; s->y = y; return s; } <#
will generate the following definitions:
(make-My_struct) -> PTR (My_struct-x PTR) -> INT (My_struct-y PTR) -> FLOAT (My_struct-y-set! PTR FLOAT) (make_struct INT FLOAT) -> PTR
Nested structs or unions are not supported (but pointers to nested structs/unions are).
All specially handled tokens preceded with ___
are defined as C macros in the
headerfile chicken.h
and will usually expand into nothing, so they don't
invalidate the processed source code.
C++ namespace
declarations of the form namespace NAME { ... }
recognized but will
be completely ignored.
Keep in mind that this is not a fully general C/C++ parser. Taking an arbitrary headerfile and feeding it to CHICKEN will in most cases not work or generate riduculuous amounts of code. This FFI facility is for carefully written headerfiles, and for declarations directly embedded into Scheme code.
Using the ___declare(DECL, VALUE)
form, pseudo declarations can be embedded into
processed C/C++ code to provide additional control over the wrapper generation. Pseudo declarations
will be ignored when processed by the system's C/C++ compiler.
<string>
as being abstract, i.e. no constructor will
be defined. Alternatively, a class definition may be prefixed with ___abstract
.
set-finalizer!
so that any unused references to
instances of subsequently defined C++ class wrappers will be destroyed. This should be used
with care: if the embedded C++ object which is represented by the reclaimed TinyCLOS instance
is still in use in foreign code, then unpredictable things will happen.
___mutable
).
destroy
.
#define
or enum
),
making the constant available outside the current compilation unit. Use the values
yes
/1
for switching constant export on, or no
/0
for switching
it off.
catch
forms that perform any actions
that should be taken in case an exception is thrown by the wrapped member function:
#>! ___declare(exception_handler, "catch(...) { return 0; }") class Foo { public: Foo *bar(bool f) { if(f) throw 123; else return this; } }; <# (define f1 (make <Foo>)) (print (bar f1 #f)) (print (bar f1 #t))
will print <Foo>
and #f
, respectively.
char
bool
c-string
unsigned-char
byte
unsigned-byte
[unsigned-]int
[unsigned-]short
[unsigned-]long
[unsigned-]integer
float
double
number
(enum _)char
(const T)char
(function ...)
c-pointer
(pointer _)
(c-pointer _)
u8vector
s8vector
u16vector
s16vector
u32vector
s32vector
f32vector
f64vector
All other foreign types are specialized as <top>
.
Full specialization can be enabled globally, or only for sections of code by enclosing it in
___declare(full_specialization, yes) ... int foo(int x); int foo(char *x); ... ___declare(full_specialization, no)
Alternatively, member function definitions may be prefixed by ___specialize
for specializing
only specific members.
Sets a prefix that should be be added to all generated Scheme identifiers. For example
___declare(prefix, "mylib:") #define SOME_CONST 42
would generate the following code:
(define-constant mylib:SOME_CONST 42)
To switch prefixing off, use the values no
or 0
. Prefixes are not applied to
Class names.
Defines to what a certain C/C++ name should be renamed. The value for this declaration
should have the form "<c-name>;<scheme-name>"
, where <c-name>
specifies
the C/C++ identifier occurring in the parsed text and <scheme-name>
gives
the name used in generated wrapper code.
Embeds the Scheme expression <string>
in the generated Scheme code.
Declares a name-substitution for all generated Scheme identifiers. The value for this
declaration should be a string containing a regular expression and a replacement string
(separated by the ;
character):
___declare(substitute, "^SDL_;sdl:") extern void SDL_Quit();
generates
(define sdl:Quit (foreign-lambda integer "SDL_Quit") )
Defines an arbitrary transformation procedure for names that match a given regular expression.
The value should be a string containing a regular expression and a Scheme expression that
evaluates to a procedure of one argument. If the regex matches, the procedure will be called
at compile time with the match-result (as returned by string-match
) and should return
a string with the desired transformations applied:
(require-for-syntax 'srfi-13) #>! ___declare(transform, "([A-Z]+)_(.*);(lambda (x) (string-append (cadr x) \"-\" (string-downcase (caddr x))))") void FOO_Bar(int x) { return x * 2; } <# (print (FOO-bar 33))
Chooses a standard name-transformation, converting underscores (_
) to hyphens (-
) and
transforming “CamelCase” into “camel-case”. All uppercase characters are also converted to lowercase.
The result is prefixed with the argument string (equivalent to the prefix
pseudo declaration).
Declares a foreign type transformation, similar to define-foreign-type
.
The value should be a list of two to four items, separated by the ;
character:
a C typename, a Scheme foreign type specifier and optional argument- and result-value
conversion procedures.
;;;; foreign type that converts to unicode (assumes 4-byte wchar_t): ; ; - Note: this is rather kludgy and is only meant to demonstrate the `type' ; pseudo-declaration (require-extension srfi-4) (define mbstowcs (foreign-lambda int "mbstowcs" nonnull-u32vector c-string int)) (define (str->ustr str) (let* ([len (string-length str)] [us (make-u32vector (add1 len) 0)] ) (mbstowcs us str len) us) ) #>! ___declare(type, "unicode;nonnull-u32vector;str->ustr") static void foo(unicode ws) { printf("\"%ls\"\n", ws); } <# (foo "this is a test!")
Similar to type
, but provides automatic argument- and result conversions
to wrap a value into a structure:
#>? ___declare(opaque, "myfile;(pointer \"FILE\")") myfile fopen(char *, char *); <# (fopen "somefile" "r") ==> <myfile>
___declare(opaque, "TYPENAME;TYPE")
is basically equivalent to
___declare(type, "TYPENAME;TYPE;TYPE->RECORD;RECORD->TYPE")
where
TYPE->RECORD
and RECORD->TYPE
are compiler-generated conversion
functions that wrap objects of type TYPE
into a record and back.
The parser understand the following grammar:
PROGRAM = PPCOMMAND | DECLARATION ";" PPCOMMAND = "#define" ID [TOKEN ...] | "#ifdef" ID | "#ifndef" ID | "#else" | "#endif" | "#undef" ID | "#error" TOKEN ... | "#include" INCLUDEFILE | "#import" INCLUDEFILE | "#pragma" TOKEN ... DECLARATION = FUNCTION | VARIABLE | ENUM | TYPEDEF | CLASS | CONSTANT | STRUCT | NAMESPACE | "___declare" "(" PSEUDODECL "," <tokens> ")" STRUCT = ("struct" | "union") ID ["{" {["___mutable"] TYPE {"*"} ID {"," {"*"} ID}} "}] NAMESPACE = "namespace" ID "{" DECLARATION ... "}" INCLUDEFILE = "\"" ... "\"" | "<" ... ">" FUNCTION = {"___callback" | "___safe" | "___specialize" | "___discard"} [STORAGE] TYPE ID "(" ARGTYPE "," ... ")" [CODE] | {"___callback" | "___safe" | "___specialize" | "___discard"} [STORAGE] TYPE ID "(" "void" ")" [CODE] ARGTYPE = [IOQUALIFIER] TYPE [ID ["[" ... "]"]] IOQUALIFIER = "___in" | "___out" | "___inout" | LENQUALIFIER LENQUALIFIER = "___length" "(" ID ")" VARIABLE = [STORAGE] ENTITY ["=" INITDATA] ENTITY = TYPE ID ["[" ... "]"] STORAGE = "extern" | "static" | "volatile" | "inline" CONSTANT = "const" TYPE ID "=" INITDATA PSEUDODECL = "export_constants" | "prefix" | "substitute" | "abstract" | "type" | "scheme" | "rename" | "transform" | "full_specialization" | "destructor_name" | "class_finalizers" | "exception_handler" | "mutable_fields" ENUM = "enum" "{" ID ["=" (NUMBER | ID)] "," ... "}" TYPEDEF = "typedef" TYPE ["*" ...] [ID] TYPE = ["const"] BASICTYPE [("*" ... | "&" | "<" TYPE "," ... ">" | "(" "*" [ID] ")" "(" TYPE "," ... ")")] BASICTYPE = ["unsigned" | "signed"] "int" | ["unsigned" | "signed"] "char" | ["unsigned" | "signed"] "short" ["int"] | ["unsigned" | "signed"] "long" ["int"] | ["unsigned" | "signed"] "___byte" | "size_t" | "float" | "double" | "void" | "bool" | "___bool" | "___scheme_value" | "___scheme_pointer" | "___byte_vector" | "___pointer" TYPE "*" | "C_word" | "___fixnum" | "___number" | "___symbol" | "___u32" | "___s32" | "___s64" | "__int64" | "int64_t" | "struct" ID | "union" ID | "enum" ID | ID CLASS = ["___abstract"] "class" ID [":" [QUALIFIER] ID "," ...] "{" MEMBER ... "}" MEMBER = [QUALIFIER ":"] ["virtual"] (MEMBERVARIABLE | CONSTRUCTOR | DESTRUCTOR | MEMBERFUNCTION) MEMBERVARIABLE = TYPE ID ["=" INITDATA] MEMBERFUNCTION = {"___callback" | "static" | "___specialize" | "___discard"} TYPE ID "(" ARGTYPE "," ... ")" ["const"] ["=" "0"] [CODE] | {"___callback" | "static" | "___specialize" | "___discard"} TYPE ID "(" "void" ")" ["const"] ["=" "0"] [CODE] CONSTRUCTOR = ["___callback" | "___safe"] ["explicit"] ID "(" ARGTYPE "," ... ")" [BASECONSTRUCTORS] [CODE] DESTRUCTOR = ["___callback" | "___safe"] "~" ID "(" ["void"] ")" [CODE] QUALIFIER = ("public" | "private" | "protected") NUMBER = <a C integer or floating-point number, in decimal, octal or hexadecimal notation> INITDATA = <everything up to end of chunk> BASECONSTRUCTORS = <everything up to end of chunk> CODE = <everything up to end of chunk>
The following table shows how argument-types are translated:
[unsigned] char
[unsigned] short
[unsigned] int
[unsigned] long
___u32
___s32
___s64
int64_t
__int64
float
double
size_t
bool
___bool
___fixnum
___number
___symbol
___scheme_value
C_word
___scheme_pointer
char *
signed char *
[signed] short *
[signed] int *
[signed] long *
unsigned char *
unsigned short *
unsigned int *
unsigned long *
float *
double *
___byte_vector
CLASS *
CLASS &
TYPE *
TYPE &
TYPE<T1, ...>
TYPE1 (*)(TYPE2, ...)
The following table shows how result-types are translated:
void
[unsigned] char
[unsigned] short
[unsigned] int
[unsigned] long
___u32
___s32
___s64
int64_t
__int64
float
double
size_t
bool
___bool
___fixnum
___number
___symbol
___scheme_value
char *
TYPE *
TYPE &
TYPE<T1, ...>
TYPE1 (*)(TYPE2, ...)
CLASS *
CLASS &
The ___pointer
argument marker disables automatic simplification of pointers to numbers: normally
arguments of type int *
are handled as SRFI-4 s32vector
number vectors. To force treatment
as a pointer argument, precede the argument type with ___pointer
.
Foreign variable definitions for macros are not exported from the current compilation unit, but definitions for C variables and functions are.
foreign-parse
does not embed the text into
the generated C file, use foreign-declare
for that (or even better, use the #>! ... <#
syntax
which does both).
Functions with variable number of arguments are not supported.
Each C++ class defines a TinyCLOS class, which is a subclass of <c++-object>
. Instances of this class
contain a single slot named this
, which holds a pointer to a heap-allocated C++ instance.
The name of the TinyCLOS class is obtained by putting the C++ classname between angled brackets (<...>
).
TinyCLOS classes are not seen by C++ code.
The C++ constructor is invoked by the initialize
generic, which accepts as many arguments
as the constructor. If no constructor is defined, a default-constructor will be provided taking no arguments.
To allow creating class instances from pointers created in foreign code, the initialize
generic will optionally accept an arguments list of the form 'this POINTER
, where POINTER
is a foreign pointer object. This will create a TinyCLOS instance for the given C++ object.
To release the storage allocated for a C++ instance invoke the destroy
generic
(the name can be changed by using the destructor_name
pseudo declaration).
Static member functions are wrapped in a Scheme procedure named <class>::<member>
.
Member variables and non-public member functions are ignored.
Virtual member functions are not seen by C++ code. Overriding a virtual member function with a TinyCLOS method will not work when the member function is called by C++.
Operator functions and default arguments are not supported.
Exceptions must be explicitly handled by user code and may not be thrown beyond an invocation of C++ by Scheme code.
The following functions and macros are available for C code that invokes Scheme or foreign procedures that are called by Scheme:
These macros return immediate Scheme data objects.
These functions allocate memory from
ptr
and initialize a fresh data object. The new data object is returned.ptr
should be the address of an allocation pointer created withC_alloc
.
Allocates memory from the C stack (
C_alloc
) and returns a pointer to it.words
should be the number of words needed for all data objects that are to be created in this function. Note that stack-allocated data objects have to be passed to Scheme callback functions, or they will not be seen by the garbage collector. This is really only usable for callback procedure invocations, make sure not to use it in normal code, because the allocated memory will be re-used after the foreign procedure returns. When invoking Scheme callback procedures a minor garbage collection is performed, so data allocated withC_alloc
will already have moved to a safe place.Note that
C_alloc
is really just a wrapper aroundalloca
, and can also be simulated by declaring a stack-allocated array ofC_word
s:
These are macros that return the size in words needed for a data object of a given type.
These macros and functions can be used to convert Scheme data objects back to C data. Note that
C_c_string()
returns a pointer to the character buffer of the actual Scheme object and is not zero-terminated.
Return the number of elements and the type-bits of the non-immediate Scheme data object
x
.
This macro can be used to access slots of the non-immediate Scheme data object
x
.index
specifies the index of the slot to be fetched, starting at 0. Pairs have 2 slots, one for the car and one for the cdr. Vectors have one slot for each element.
Aliases for
C_block_item(x, 0)
andC_block_item(x, 1)
, respectively.
Returns a pointer to the data-section of a non-immediate Scheme object.
A macro to build a Scheme object header from its bits and size parts.
Assign the Scheme value
val
to the location specified byslot
. If the value points to data inside the nursery (the first heap-generation), then the garbage collector will remember to handle the data appropriately. Assigning nursery-pointers directly will otherwise result in lost data. Note that no copying takes place at the moment whenC_mutate
is called, but later - at the next (minor) garbage collection.
Returns the global value of the variable with the name
symbol
. If the variable is unboundC_SCHEME_UNBOUND
is returned. You can set a variable's value withC_mutate(&C_symbol_value(SYMBOL), VALUE)
.
Registers
n
variables at addressptrs
to be garbage collection roots. The locations should not contain pointers to data allocated in the nursery, only immediate values or pointers to heap-data are valid. Any assignment of potential nursery data into a root-array should be done viaC_mutate()
. The variables have to be initialized to sensible values before the next garbage collection starts (when in doubt, set all locations inptrs
toC_SCHEME_UNDEFINED
)C_gc_protect
may not called before the runtime system has been initialized (either byCHICKEN_initialize
,CHICKEN_run
orCHICKEN_invoke
.For a slightly simpler interface to creating and using GC roots see
CHICKEN_new_gc_root
.
Removes the last
n
registered variables from the set of root variables.
If not
NULL
, the function pointed to by this variable will be called after each garbage collection with a flag indicating what kind of collection was performed (either0
for a minor collection or1
for a major collection). Minor collections happen very frequently, so the hook function should not consume too much time. The hook function may not invoke Scheme callbacks.
An example:
% cat foo.scm #> extern int callout(int, int, int); <# (define callout (foreign-safe-lambda int "callout" int int int)) (define-external (callin (scheme-object xyz)) int (print "This is 'callin': " xyz) 123) (print (callout 1 2 3)) % cat bar.c #include <stdio.h> #include "chicken.h" extern int callout(int, int, int); extern int callin(C_word x); int callout(int x, int y, int z) { C_word *ptr = C_alloc(C_SIZEOF_LIST(3)); C_word lst; printf("This is 'callout': %d, %d, %d\n", x, y, z); lst = C_list(&ptr, 3, C_fix(x), C_fix(y), C_fix(z)); return callin(lst); /* Note: `callin' will have GC'd the data in `ptr' */ } % csc foo.scm bar.c -o foo % foo This is 'callout': 1, 2, 3 This is 'callin': (1 2 3) 123
Notes:
-nursery
option)
or when running the executable (using the -:s
runtime option).
call-with-current-continuation
and passed to C code can be invoked like any other Scheme procedure.
Extension libraries are extensions to the core functionality provided
by the basic CHICKEN system, to be built and installed separately.
The mechanism for loading compiled extensions is based on dynamically
loadable code and as such is only available on systems on which
loading compiled code at runtime is supported. Currently this are
most UNIX-compatible platforms that provide the libdl
functionality
like Linux, Solaris, BSD or Mac OS X.
Windows with the Microsoft tools is partially supported.
Note: Extension may also be normal applications or shell scripts.
To install an extension library, run the chicken-setup
program
with the extension name as argument. If the extension consists of a
single Scheme file, then it is compiled and installed in the extension
repository. If it is an archive containing addition files, then
the files are extracted and the contained setup script is
executed. This setup script is a normal Scheme source file, which
will be interpreted by chicken-setup
. The complete language supported
by csi
is available, and the library units
srfi-1 regex utils posix tcp
are loaded. Additional
libraries can of course be loaded at run-time.
The setup script should perform all necessary steps to build the
new library (or application). After a successful build, the extension
can be installed by invoking one of the procedures
install-extension
, install-program
or install-script
.
These procedures will copy a number of given files into the extension
repository or in the path where the CHICKEN executables are located (in the
case of executable programs or scripts). Additionally the list of
installed files, and user-defined metadata is stored in the repository.
If no extension name is given on the command-line, and if none of the
options -list
, -version
, -repository
(without argument),
-program-path
(without argument), -fetch
or -docindex
is given, then all .setup
scripts in the current directory are
processed.
Extensions can be created by creating an (optionally gzipped) tar
archive named EXTENSION.egg
containing all needed files plus a .setup
script in the root directory.
After chicken-setup
has extracted the files, the setup script will be
invoked. There are no additional constraints on the structure of the archive,
but the setup script has to be in the root path of the archive.
(install-extension ID FILELIST [INFOLIST])Installs the extension library with the name
ID
. All files given in the list of stringsFILELIST
will be copied to the extension repository. It should be noted here that the extension id has to be identical to the name of the file implementing the extension. The extension may load or include other files, or may load other extensions at runtime specified by therequire-at-runtime
property.
FILELIST
may be a filename, a list of filenames, or a list of pairs of the form(SOURCE DEST)
(if you want to copy into a particular sub-directory - the destination directory will be created as needed). IfDEST
is a relative pathname, it will be copied into the extension repository.The optional argument
INFOLIST
should be an association list that maps symbols to values, this list will be stored asID.setup
at the same location as the extension code. Currently the following properties are used:— property: syntax
(syntax)Marks the extension as syntax-only. No code is compiled, the extension is intended as a file containing macros to be loaded at compile/macro-expansion time.
— property: require-at-runtime
(require-at-runtime ID ...)Specifies extensions that should be loaded (via
require
) at runtime. This is mostly useful for syntax extensions that need additional support code at runtime.— property: documentation
(documentation FILENAME)The filename of a HTML document containing extension-specific documentation. This file should be given in the file-list passed to
install-extension
and a link to it will be automatically included in the index page (accessible viachicken-setup -docindex
).— property: examples
(examples FILENAME ...)Copies the given files into the examples directory, which is usually
$prefix/share/chicken/examples
(equivalent to$CHICKEN_HOME/examples
or(make-pathname (chicken-home) "examples")
).— property: exports
(exports EXPORT ...)Add export-information to the generated extension-information.
EXPORT
may be a symbol naming an exported toplevel variable or a string designating a file with exported variables, as generated by the-emit-exports
option or theemit-exports
declaration specifier.All other properties are currently ignored. The
FILELIST
argument may also be a single string.
(install-program ID FILELIST [INFOLIST])Similar to
install-extension
, but installs an executable program in the executable path (usually/usr/local/bin
).
(install-script ID FILELIST [INFOLIST])Similar to
install-program
, but additionally changes the file permissions of all files inFILELIST
to executable (for installing shell-scripts).
(run FORM ...)Runs the shell command
FORM
, which is wrapped in an implicitquasiquote
.(run (csc ...))
is treated specially and passes-v
(if-verbose
has been given tochicken-setup
) and-feature compiling-extension
options to the compiler.
(make ((TARGET (DEPENDENT ...) COMMAND ...) ...) ARGUMENTS)A “make” macro that executes the expressions
COMMAND ...
, when any of the dependentsDEPENDENT ...
have changed, to buildTARGET
. This is the same as themake
extension, which is available separately. For more information, see make.
(patch WHICH REGEX SUBST)Replaces all occurrences of the regular expression
REGEX
with the stringSUBST
, in the file given inWHICH
. IfWHICH
is a string, the file will be patched and overwritten. IfWHICH
is a list of the formOLD NEW
, then a different file namedNEW
will be generated.
(copy-file FROM TO)Copies the file or directory (recursively) given in the string
FROM
to the destination file or directoryTO
.
(move-file FROM TO)Moves the file or directory (recursively) given in the string
FROM
to the destination file or directoryTO
.
(remove-file* PATH)Removes the file or directory given in the string
PATH
.
(create-directory PATH)Creates the directory given in the string
PATH
, with all parent directories as needed.
(find-library NAME PROC)Returns
#t
if the library namedlibNAME.[a|so]
(unix) orNAME.lib
(windows) could be found by compiling and linking a test program.PROC
should be the name of a C function that must be provided by the library. If no such library was found or the function could not be resolved,#f
is returned.
(find-header NAME)Returns
#t
if a C include-file with the given name is available, or#f
otherwise.
(test-compile CODE #!key cflags ldflags compile-only)Returns
#t
if the C code inCODE
compiles and links successfully, or#f
otherwise. The keyword parameterscflags
andldflags
accept additional compilation and linking flags. Ifcompile-only
is true, then no linking step takes place.
Holds the prefix under which CHICKEN executables and libraries have been installed (either the value of the environment variable
CHICKEN_PREFIX
or whatever prefix was specified at the time the system was built.
Holds the path where executables are installed and defaults to either
$CHICKEN_PREFIX/bin
, if the environment variableCHICKEN_PREFIX
is set,$CHICKEN_HOME
or the path where the CHICKEN binaries (chicken
,csi
, etc.) are installed.
Contains the path of the directory where
chicken-setup
was invoked.
Contains the path of the directory where the extension is built. This is not necessarily identical to
setup-root-directory
.
Reflects the setting of the
-verbose
option, i.e. is#t
, if-verbose
was given.
Reflects the setting of the
--no-install
option, i.e. is#f
, if-no-install
was given.
The simplest case is a single file that does not export any syntax. For example
;;;; hello.scm (define (hello name) (print "Hello, " name " !") )
After entering
$ chicken-setup hello
at the shell prompt, the file hello.scm
will be compiled into a dynamically loadable library,
with the default compiler options -optimize-level 2 -no-trace -shared
. If the
compilation succeeds, hello.so
will be stored in the repository, together with a file named
hello.setup
(not to be confused with a setup script - this .setup
file just contains
an a-list with metadata).
Use it like any other CHICKEN extension:
$ csi -q #;1> (require-extension hello) ; loading /usr/local/lib/chicken/hello.so ... #;2> (hello "me") Hello, me! #;3>
For more elaborate build operations, when installing applications or scripts, or when
additional metadata should be stored for an extension, a setup
script is required
and the script and all additional files should be packaged in a gzipped tar
archive.
Here we create a simple application:
;;;; hello2.scm (print "Hello, ") (for-each (lambda (x) (printf "~A " x)) (command-line-arguments)) (print "!")
We also need a setup script:
;;;; hello2.setup (run (csc hello2.scm)) ; compile `hello2' (install-program 'hello2 "hello2") ; name of the extension and files to be installed
To use it, just run chicken-setup
in the same directory:
$ chicken-setup hello2
Now the program hello2
will be installed in the same location as the other CHICKEN
tools (like chicken
, csi
, etc.), which will normally be /usr/local/bin
.
Note that you need write-permissions for those locations.
Uninstallation is just as easy:
$ chicken-setup -uninstall hello2
chicken-setup
provides a make
macro, so building operations can be of
arbitrary complexity. When running chicken-setup
with an argument NAME
,
for which no associated file NAME.setup
, NAME.egg
or NAME.scm
exists will ask you to download the extension via HTTP from the default URL
http://www.call-with-current-continuation.org/eggs
. You can use the
-host
option to specify an alternative source location.
If the given extension name contains a path prefix and the -host
option
is given, then chicken-setup
can also download and install eggs from
an arbitrary HTTP server. Alternatively you can pass a full URL (including the
http://
prefix. Note that no dependency checks are done when downloading
eggs directly with the URL syntax.
Finally a somewhat more complex example: We want to package a syntax extension with additional support code that is to be loaded at run-time of any Scheme code that uses that extension. We create a “glass” lambda, a procedure with free variables that can be manipulated from outside:
;;;; glass.scm (define-macro (glass-lambda llist vars . body) ;; Low-level macros are fun! (let ([lvar (gensym)] [svar (gensym)] [x (gensym)] [y (gensym)] [yn (gensym)] ) `(let ,(map (lambda (v) (list v #f)) vars) (define (,svar ,x . ,y) (let* ([,yn (pair? ,y)] [,y (and ,yn (car ,y))] ) (case ,x ,@(map (lambda (v) `([,v] (if ,yn (set! ,v ,y) ,v) ) ) vars) (else (error "variable not found" ,x)) ) ) ) (define ,lvar (lambda ,llist ,@body)) (extend-procedure ,lvar ,svar) ) ) )
Here some support code that needs to be loaded at runtime:
;;;; glass-support.scm (require-extension lolevel) (define glass-lambda-accessor procedure-data) (define (glass-lambda-ref gl v) ((procedure-data gl) v)) (define (glass-lambda-set! gl v x) ((procedure-data gl) v x))
The setup script looks like this:
(run (csc -s -O2 -d0 glass-support.scm)) (install-extension 'glass '("glass.scm" "glass-support.so") '((syntax) (require-at-runtime glass-support)) )
The invocation of install-extension
provides the files that
are to be copied into the extension repository, and a metadata list that
specifies that the extension glass
is a syntax extension and that,
if it is declared to be used by other code (either with the require-extension
or require-for-syntax
form), then client code should perform an implicit
(require 'glass-support)
at startup.
This can be conveniently packaged as an “egg”:
$ tar cfz glass.egg glass.setup glass.scm glass-support.scm
And now we use it:
$ chicken-setup glass $ csi -quiet #;1> (require-extension glass) ; loading /usr/local/lib/chicken/glass.scm ... ; loading /usr/local/lib/chicken/glass-support.so ... #;2> (define foo (glass-lambda (x) (y) (+ x y))) #;3> (glass-lambda-set! foo 'y 99) #;4> (foo 33) 132
chicken-setup
referenceAvailable options:
-h -help
Show usage information and exit.
-V -version
Display version and exit.
-R -repository [PATHNAME]
When used without an argument, the path of the extension repository is displayed on standard
output. When given an argument, the repository pathname (and the repository-path
parameter)
will be set to PATHNAME
for all subsequent operations. The default repository path is
the installation library directory (usually /usr/local/lib/chicken
), or (if set) the
directory given in the environment variable CHICKEN_REPOSITORY
.
PATHNAME
should be an absolute pathname.
-P -program-path [PATHNAME]
When used without an argument, the path for executables is displayed on standard output.
When given an argument, the program path for installing executables and scripts will be set to
PATHNAME
for all subsequent operations.
PATHNAME
should be an absolute pathname.
-h -host HOSTNAME[:PORT]
Specifies alternative host for downloading extensions, optionally with a TCP port number (which
defaults to 80).
-u -uninstall EXTENSION
Removes all files that were installed for EXTENSION
from the file-system, together
with any metadata that has been stored.
-l -list [NAME ...]
List all installed extensions or show extension information.
-r -run FILENAME
Load and execute given file.
-s -script FILENAME
Executes the given Scheme source file with all remaining arguments and exit. The “she-bang”
shell script header is recognized, so you can write Scheme scripts that use chicken-setup
just as with csi
.
-e -eval EXPRESSION
Evaluates the given expression(s).
-v -verbose
Display additional debug information.
-k -keep
Keep temporary files and directories.
-c -csc-option OPTION
Passes OPTION
as an extra argument to invocations of the compiler-driver (csc
). This
works only if csc
is invoked as (run (csc ...))
.
-d -dont-ask
Do not ask the user before trying to download required extensions.
-n -no-install
Do not install generated binaries and/or support files. Any invocations of install-program
,
install-extension
or install-script
will be be no-ops.
-i -docindex
Displays the path to the index-page of any installed extension-documentation. If the index page
does not exist, it is created.
-check
Downloads the repository-index and lists locally installed extensions for which a newer
release is available for download.
--
Ignore all following arguments.
Note that the options are processed exactly in the order in which they appear in the command-line.
chicken-setup
works on Windows, when compiled with Visual C++, but depends on the tar
and gunzip
tools to extract the contents of an egg. The best way is to download an egg
either manually (or with chicken-setup -fetch
) and extract its contents with a separate program
(like winzip
). the CHICKEN_REPOSITORY
environment variable has to be set (in addition
to CHICKEN_HOME
) to a directory where your compiled extensions should be located.
The .setup
scripts will not always work under Windows, and the extensions may require libraries
that are not provided for Windows or work differently. Under these circumstances it is recommended
to perform the required steps to build an extension manually.
In addition to library units the following files are provided. Use them
by including the file in your code with the include
special form.
chicken-more-macros.scm
This file contains the definitions of all non-standard syntax forms. You normally don't use this file directly, unless you have the following situation: you use non-standard macros at run-time (in evaluated code) in a compiled program and you want non-standard syntax to be available. In this case, add
(require-extension chicken-more-macros)
to your code. This will load the definitions for non-standard macros available in code evaluated by the program.
See also the FAQ for a discussion about the different macro systems and their ideosyncrasies.
chicken-ffi-macros.scm
This file contains the definitions of macros for interfacing to foreign code, and the definitions contained in this file are automatically made available in compiled code.
There exist two different kinds of data objects in the CHICKEN system: immediate and non-immediate objects. Immediate objects are represented by a tagged machine word, which is usually of 32 bits length (64 bits on 64-bit architectures). The immediate objects come in four different flavors:
C_CHARACTER_BITS
. The
ASCII code of the character is encoded in bits 9 to 16, counting from
1 and starting at the lowest order position.
C_BOOLEAN_BITS
. Bit 5
(counting from 0 and starting at the lowest order position) is one if
the boolean designates true, or 0 if it is false.
C_SPECIAL_BITS
. Bits 5 to 8 contain an identifying
number for this type of object. The following constants are
defined: C_SCHEME_END_OF_LIST C_SCHEME_UNDEFINED
C_SCHEME_END_OF_FILE
Non-immediate objects are blocks of data represented by a pointer into the heap. The first word of the data block contains a header, which gives information about the type of the object. The header has the size of a machine word, usually 32 bits (64 bits on 64 bit architectures).
C_GC_FORWARDING_BIT
C_BYTEBLOCK_BIT
C_SPECIALBLOCK_BIT
C_8ALIGN_BIT
The actual data follows immediately after the header. Note that block-addresses are always aligned to the native machine-word boundary. Scheme data objects map to blocks in the following manner:
C_PAIR_TYPE
),
where the car and the cdr are contained in the first and second slots,
respectively.
C_VECTOR_TYPE
).
C_STRING_TYPE
).
C_CLOSURE_TYPE
). The first slot contains a pointer to a
compiled C function. Any extra slots contain the free variables (since
a flat closure representation is used).
C_FLONUM_BITS
). Slots one and two (or a single slot on
64 bit architectures) contain a 64-bit floating-point number, in the
representation used by the host systems C compiler.
C_SYMBOL_TYPE
). Slots
one and two contain the toplevel variable value and the print-name
(a string) of the symbol, respectively.
C_PORT_TYPE
). The first slot contains a pointer to a file-
stream, if this is a file-pointer, or NULL if not. The other slots
contain housekeeping data used for this port.
C_STRUCTURE_TYPE
). The first slot contains a symbol that
specifies the kind of structure this record is an instance of. The other
slots contain the actual record items.
C_POINTER_TYPE
). The single slot contains a machine pointer.
C_TAGGED_POINTER_TYPE
), but the object contains an additional
slot with a tag (an arbitrary data object) that identifies the type
of the pointer.
Data objects may be allocated outside of the garbage collected heap, as long as their layout follows the above mentioned scheme. But care has to be taken not to mutate these objects with heap-data (i.e. non-immediate objects), because this will confuse the garbage collector.
For more information see the header file chicken.h
.
port-position
currently works only for input ports.
Since Scheme is a relatively simple language, a large number of implementations exist and each has its specific advantages and disadvantages. Some are fast, some provide a rich programming environment. Some are free, others are tailored to specific domains, and so on. The reasons for the existance of CHICKEN are:
CHICKEN is portable because it generates C code that runs on a large number of platforms.
CHICKEN is extendable, since its code generation scheme and runtime system/garbage collector fits neatly into a C environment.
CHICKEN is free and can be freely distributed, including its source code.
CHICKEN offers better performance than nearly all interpreter based implementations, but still provides full Scheme semantics.
As far as I know, CHICKEN is the first implementation of Scheme that uses Henry Baker's “Cheney on the M.T.A” concept.
Send e-mail to felix@call-with-current-continuation.org
with some hints about the problem, like
version/build of the compiler, platform, system configuration, code that
causes the bug, etc.
define-foreign-variable
or define-constant
or define-inline
not seen outside of the containing source file?
Accesses to foreign variables are translated directly into C constructs that access the variable, so the Scheme name given to that variable does only exist during compile-time. The same goes for constant- and inline-definitions: The name is only there to tell the compiler that this reference is to be replaced with the actual value.
cond-expand
know which features are registered in used units?
Each unit used via (declare (uses ...))
is registered as a feature and
so a symbol with the unit-name can be tested by cond-expand
during macro-expansion-time.
Features registered using the register-feature!
procedure are only
available during run-time of the compiled file. You can use the eval-when
form
to register features at compile time.
If you don't need eval
or the stuff in the extras
library unit,
you can just use the library
unit:
(declare (uses library)) (display "Hello, world!\n")
(Don't forget to compile with the -explicit-use
option)
Compiled with Visual C++ this generates an excutable of around 240 kilobytes.
It is theoretically possible to compile something without the library, but
a program would have to implement quite a lot of support code on its own.
cons
still trigger garbage collections?
Under CHICKENs implementation policy, tail recursion is achieved simply by avoiding to
return from a function call. Since the programs is CPS converted, a continuous
sequence of nested procedure calls is performed. At some stage the stack-space
has to run out and the current procedure and its parameters (including the current continuation) are stored somewhere
in the runtime system. Now a minor garbage collection occurs and rescues all live
data from the stack (the first heap generation) and moves it into the the second heap generation. Than the stack is cleared (using
a longjmp
) and execution can continue from the saved state.
With this method arbitrary recursion (in tail- or non-tail position) can happen,
provided the application doesn't run out of heap-space.
(The difference between a tail- and a non-tail call is that the tail-call has no
live data after it invokes its continuation - and so the amount of heap-space needed stays constant)
There are a number of declaration specifiers that should be used to speed up
compiled files: declaring (standard-bindings)
is mandatory, since this enables
most optimizations. Even if some standard procedures should be redefined, you can
list untouched bindings in the declaration.
Declaring (extended-bindings)
lets the compiler choose faster versions of certain
internal library functions. This might give another speedup. You can also use the
the usual-integrations
declaration, which is identical to declaring
standard-bindings
and extended-bindings
(note that usual-integrations
is set by default).
Declaring (block)
tells the compiler that global procedures are not changed
outside the current compilation unit, this gives the compiler some more
opportunities for optimization.
If no floating point arithmetic is required, then declaring (number-type fixnum)
can give a big performance improvement, because the compiler can now inline
most arithmetic operations.
Declaring (unsafe)
will switch off most safety checks.
If threads are not used, you can declare (disable-interrupts)
.
You should always use maximum optimizations settings for your C compiler.
Good GCC compiler options on Pentium (and compatible) hardware are:
-Os -fomit-frame-pointer -fno-strict-aliasing
Some programs are very sensitive to the setting of the nursery (the first heap-generation). You
should experiment with different nursery settings (either by compiling with the -nursery
option or by using the -:s...
runtime option).
_C_..._toplevel
?
This message indicates that your program uses a library-unit, but that the
object-file or library was not supplied to the linker. If you have the unit
foo
, which is contained in foo.o
than you have to supply it to the
linker like this (assuming a GCC environment):
% csc program.scm foo.o -o program
_C_toplevel
?
This means you have compiled a library unit as an application. When a unit-declaration (as in (declare (unit ...))
)
is given, then this file has a specially named toplevel entry procedure. Just remove the declaration,
or compile this file to an object-module and link it to your application code.
define-constant
not honoured in case
constructs?
case
expands into a cascaded if
expression, where the first item in each arm
is treated as a quoted list. So the case
macro can not infer wether
a symbol is to be treated as a constant-name (defined via define-constant
) or
a literal symbol.
-unsafe
or unsafe declarations, it crashes during execution.
The compiler option -unsafe
or the declaration (declare (unsafe))
disable
certain safety-checks to improve performance, so code that would normally
trigger an error will work unexpectedly or even crash the running application.
It is advisable to develop and debug a program in safe mode (without unsafe
declarations) and use this feature only if the application works properly.
extended-bindings
or usual-integrations
declaration or compiler option is used?
The following extended bindings are handled specially:
bitwise-and
bitwise-ior
bitwise-xor
bitwise-not
add1
sub1
fx+
fx-
fx*
fx/
fxmod
fx=
fx>
fx>=
fixnum?
fxneg
fxmax
fxmin
fxand
fxior
fxxor
fxnot
fxshl
fxshr
fp+
fp-
fp*
fp/
atom?
fp=
fp>
fp>=
fpneg
fpmax
fpmin
arithmetic-shift
signum
flush-output
thread-specific
thread-specific-set!
not-pair?
null-list?
print
print*
u8vector->bytevector
s8vector->bytevector
u16vector->bytevector
s16vector->bytevector
u32vector->bytevector
s32vector->bytevector
f32vector->bytevector
f64vector->bytevector
block-ref
byte-vector-length
u8vector-length
s8vector-length
u16vector-length
s16vector-length
u32vector-length
s32vector-length
f32vector-length
f64vector-length
u8vector-ref
s8vector-ref
u16vector-ref
s16vector-ref
u32vector-ref
s32vector-ref
f32vector-ref
f64vector-ref
u8vector-set!
s8vector-set!
u16vector-set!
s16vector-set!
u32vector-set!
s32vector-set!
hash-table-ref
block-set!
number-of-slots
first
second
third
fourth
null-pointer?
pointer->object
make-record-instance
locative-ref
locative-set!
locative?
locative->object
identity
cpu-time
error
call/cc
define-reader-ctor
not work in my compiled program?
The following piece of code does not work as expected:
(eval-when (compile) (define-reader-ctor 'integer->char integer->char) ) (print #,(integer->char 33))
The problem is that the compiler reads the complete source-file before doing any processing on it, so the sharp-comma form is encountered before the reader-ctor is defined. A possible solution is to include the file containing the sharp-comma form, like this:
(eval-when (compile) (define-reader-ctor 'integer->char integer->char) ) (include "other-file")
;;; other-file.scm: (print #,(integer->char 33))
match
?
Even when the match
unit is not used, the macros from that package are visible in the compiler.
The reason for this is that macros can not be accessed from library units (only when explicitly evaluated in running
code). To speed up macro-expansion time, the compiler and the interpreter both already provide the compiled
match-...
macro definitions. Macros shadowed lexically are no problem, but global definitions
of variables named identically to (global) macros are useless - the macro definition shadows the global
variable.
This problem can be solved in one of three ways:
- Use a different name
- Undefine the macro, like this:
(eval-when (compile eval) (undefine-macro! 'match))
To enable the read
procedure to read symbols and identifiers case sensitive, you can set the
parameter case-sensitivity
to #t
.
There are two reasons why code involving callbacks can crash out of know apparent reason. The first is that it is
important to use foreign-safe-lambda/foreign-safe-lambda*
for the C code that is
to call back into Scheme. If this is not done than sooner or later the available stack space will be exhausted.
The second reason is that if the C code uses a large amount of stack storage, or if Scheme-to-C-to-Scheme calls are
nested deeply, then the available nursery space on the stack will run low. To avoid this it might be advisable
to run the compiled code with a larger nursery setting, i.e. run the code with -:s...
and a larger
value than the default (for example -:s300k
), or use the -nursery
compiler option.
Note that this can decrease runtime performance on some platforms.
match-error-control
during compilation?
Use eval-when
, like this:
(eval-when (compile) (match-error-control #:unspecified) )
The short answer:
% chicken-setup numbers % csi -q #;1> (use numbers)
The long answer:
There are a number of reasons for this:
- For most applications of Scheme fixnums (exact word-sized integers) and flonums (64-bit floating-point numbers) are more than sufficient;
- Interfacing to C is simpler;
- Dispatching of arithmetic operations is more efficient.
There is an extension based on the GNU Multiprecision Package that implements most of the full numeric tower, see http://www.call-with-current-continuation.org/eggs/numbers.html.
Consider the following piece of code:
(define k (call-with-current-continuation (lambda (k) k))) (k k)
When compiled, this will loop endlessly. But when interpreted, (k k)
will return
to the read-eval-print loop! This happens because the continuation captured will eventually read the
next toplevel expression from the standard-input (or an input-file if loading from a file). At the moment
k
was defined, the next expression was (k k)
. But when k
is invoked, the next expression will be whatever follows after (k k)
.
In other words, invoking a captured continuation will not rewind the file-position of the input source.
A solution is to wrap the whole code into a (begin ...)
expression, so all toplevel
expressions will be loaded together.
Specializing a method on <object>
doesn't work on primitive data objects like
numbers, strings, etc. so for example
(define-method (foo (x <my-class>)) ...) (define-method (foo (x <object>)) ...) (foo 123)
will signal an error, because to applicable method can be found. To specialize a method for primitive
objects, use <top>
:
(define-method (foo (x <top>)) ...)
Currently native threads are not supported. The runtime system is not reentrant, and the garbage-collection algorithm would be made much more complicated, since the location of every object (whether it is allocated on the stack or on the heap or completely outside the GC-able data space) has to be checked - this would be rather complex and inefficient in a situation where multiple threads are involved.
Use csc
in combination with the -dll
option:
C:\> csc foo.scm -dll
Invoke csc
with the -windows
option. Or pass the -DC_WINDOWS_GUI
option to the C compiler and link with the GUI version of the runtime system (that's libchicken-gui[-static].lib
.
The GUI runtime displays error messages in a message box and does some rudimentary command-line
parsing.
It seems that the Microsoft C compiler can only handle files up to a certain size, and it doesn't utilize virtual memory as well as the GNU C compiler, for example. Try closing running applications. If that fails, try to break up the Scheme code into several library units.
csi
inside an emacs buffer under Windows, nothing happens.
Invoke csi
with the -:c
runtime option. Under Windows the interpreter thinks it
is not running under control of a terminal and doesn't print the prompt and does not flush the output stream properly.
Code compiled into a DLL to be loaded dynamically must be linked with the same runtime system as the loading
application. That means that all dynamically loaded entities (including extensions built and installed with
chicken-setup
) must be compiled with the -windows
csc
option.
csc.exe
seems to be doing something wrong.
The Windows development tools include a C# compiler with the same name. Either invoke csc.exe
with a full
pathname, or put the directory where you installed CHICKEN in front of the MS development tool path in the PATH
environment variable.
When you invoke the C compiler for your translated Scheme source program, add the C compiler option
-DC_EMBEDDED
, or pass -embedded
to the csc
driver program, so no entry-point function will be generated (main()
).
When your are finished with your startup processing, invoke:
CHICKEN_main(argc, argv, C_toplevel);
where C_toplevel
is the entry-point into the compiled Scheme code. You
should add the following declarations at the head of your code:
#include "chicken.h" extern void C_toplevel(C_word,C_word,C_word) C_noret;
To add a compiled user pass instead of an interpreted one, create a library unit and recompile
the main unit of the compiler (in the file chicken.scm
) with an additional uses
declaration. Then link all compiler modules and your (compiled) extension to create a new version of
the compiler, like this (assuming all sources are in the
current directory):
% cat userpass.scm ;;;; userpass.scm - My very own compiler pass (declare (unit userpass)) ;; Perhaps more user passes/extensions are added: (let ([old (user-pass)]) (user-pass (lambda (x) (let ([x2 (do-something-with x)]) (if old (old x2) x2) ) ) ) ) ... % csc -c -x userpass.scm % csc chicken.scm -c -o chicken-extended.o -uses userpass % gcc chicken-extended.o support.o easyffi.o compiler.o optimizer.o batch-driver.o c-platform.o \ c-backend.o userpass.o `csc -ldflags -libs` -o chicken-extended
On platforms that support it (Linux ELF, Solaris, Windows + VC++), compiled code can be loaded via -extend
just like source files (see load
in the User's Manual).
Macro bodies that are defined and used in a compiled source-file are
evaluated during compilation and so have no access to definitions in the
compiled file. Note also that during compile-time macros are only available in
the same source file in which they are defined. Files included via include
are considered part of the containing file.
Macros are defined during compile time, so when a file has been compiled, the definitions are gone. An exception
to this rule are macros defined with define-macro
, which are also visible at run-time, i.e.
in eval
. To use macros defined in other files, use the include
special
form.
Also many thanks to Nico Amtsberg, William Annis, Marc Baily, Peter Barabas, Jonah Beckford, Arto Bendiken, Peter Bex, Jean-François Bignolles, Dave Bodenstab, Fabian Böhlke, T. Kurt Bond, Ashley Bone, Dominique Boucher, Terence Brannon, Roy Bryant, Adam Buchbinder, Hans Bulfone, Category 5, Taylor Campbell, Franklin Chen, Thomas Chust, Gian Paolo Ciceri, John Cowan, Grzegorz Chrupa/la, James Crippen, Tollef Fog Heen, Alejandro Forero Cuervo, Linh Dang, Brian Denheyer, Chris Double, Jarod Eells, Petter Egesund, Steve Elkins, Daniel B. Faken, Graham Fawcett, Fizzie, Kimura Fuyuki, Tony Garnock-Jones, Martin Gasbichler, Joey Gibson, Johannes Grødem, Damian Gryski, Mario Domenech Goulart, Andreas Gustafsson, Sven Hartrumpf, Jun-ichiro itojun Hagino, Matthias Heiler, Karl M. Hegbloom, William P. Heinemann, Bill Hoffman, Bruce Hoult, Hans Hübner, Markus Hülsmann, Goetz Isenmann, David Janssens, Christian Jaeger, Dale Jordan, Valentin Kamyshenko, Daishi Kato, Peter Keller, Ron Kneusel, Matthias Koeppe, Krysztof Kowa/lczyk, Todd R. Kueny Sr, Goran Krampe, Micky Latowicki, John Lenz, Kirill Lisovsky, Kon Lovett, Dennis Marti, Charles Martin, Bob McIsaac, Alain Mellan, Eric Merrit, Perry Metzger, Scott G. Miller, Mikael, Bruce Mitchener, Chris Moline, Eric E. Moore, Julian Morrison, Dan Muresan, Lars Nilsson, Ian Oversby, o.t., Gene Pavlovsky, Levi Pearson, Nicolas Pelletier, Carlos Pita, Pupeno, Davide Puricelli, Doug Quale, Eric Raible, Joel Reymont, Andreas Rottman, David Rush, Lars Rustemeier, Oskar Schirmer, Burton Samograd, Reed Sheridan, Ronald Schröder, Spencer Schumann, Alex Shinn, Shmul, Jeffrey B. Siegal, Andrey Sidorenko, Michele Simionato, Volker Stolz, Dorai Sitaram, Robert Skeels, Jason Songhurst, Clifford Stein, Sunnan, Zbigniew Szadkowski, Mike Thomas, Christian Tismer, Andre van Tonder, John Tobey, Henrik Tramberend, Vladimir Tsichevsky, Neil van Dyke, Sander Vesik, Panagiotis Vossos, Shawn Wagner, Peter Wang, Ed Watkeys, Thomas Weidner, Matthew Welland, Joerg Wittenberger, Peter Wright, Mark Wutka, Richard Zidlicky and Houman Zolfaghari for bug-fixes, tips and suggestions.
Special thanks to Brandon van Every for contributing the CMake (http://www.cmake.org) support and for helping with Windows build issues.
Also special thanks to Benedikt Rosenau for his constant encouragement.
CHICKEN contains code from several people:
dynamic-wind
.
let-optionals[*]
and
reference implementations of SRFI-1, SRFI-13 and SRFI-14.
http://home.pipeline.com/\~hbaker1/CheneyMTA.html
http://www.schemers.org/Documents/Standards/R5RS
#
: Macros and procedures implemented in the interpreter#!...
: Non standard read syntax#$EXPRESSION
: Non standard read syntax#%...
: Non standard read syntax#'EXPRESSION
: Non standard read syntax#+FEATURE
: Non standard read syntax#,(CONSTRUCTORNAME DATUM ...)
: Non standard read syntax#:SYMBOL
: Non standard read syntax#;EXPRESSION
: Non standard read syntax#<#TAG
: Non standard read syntax#<<TAG
: Non standard read syntax#>
: Non standard read syntax#>!
: Non standard read syntax#>?
: Non standard read syntax#ci...
: Non standard read syntax#cs...
: Non standard read syntax#| ... |#
: Non standard read syntax$
: Accessing external objects(
: C interface->string
: Strings:optional
: Binding forms for optional arguments<array>
: Builtin classes<boolean>
: Builtin classes<byte-vector>
: Builtin classes<c++-class>
: Builtin classes<char-set>
: Builtin classes<char>
: Builtin classes<class>
: Builtin classes<condition>
: Builtin classes<end-of-file>
: Builtin classes<entity-class>
: Builtin classes<environment>
: Builtin classes<exact>
: Builtin classes<f32vector>
: Builtin classes<f64vector>
: Builtin classes<generic>
: Builtin classes<hash-table>
: Builtin classes<inexact>
: Builtin classes<input-port>
: Builtin classes<integer>
: Builtin classes<locative>
: Builtin classes<lock>
: Builtin classes<method>
: Builtin classes<mmap>
: Builtin classes<null>
: Builtin classes<number>
: Builtin classes<object>
: Builtin classes<output-port>
: Builtin classes<pair>
: Builtin classes<pointer>
: Builtin classes<port>
: Builtin classes<primitive>
: Builtin classes<procedure-class>
: Builtin classes<procedure>
: Builtin classes<promise>
: Builtin classes<queue>
: Builtin classes<regexp>
: Builtin classes<s16vector>
: Builtin classes<s32vector>
: Builtin classes<s8vector>
: Builtin classes<string>
: Builtin classes<structure>
: Builtin classes<swig-pointer>
: Builtin classes<symbol>
: Builtin classes<tagged-pointer>
: Builtin classes<tcp-listener>
: Builtin classes<time>
: Builtin classes<u16vector>
: Builtin classes<u32vector>
: Builtin classes<u8vector>
: Builtin classes<vector>
: Builtin classes_exit
: Raw exitabsolute-pathname?
: Pathname operationsadd-method
: Base languageadd1
: Arithmeticaddress->pointer
: Foreign pointersalign-to-word
: Foreign pointersalist-ref
: Listsalist-update!
: Listsallocate
: Foreign pointersallocate-instance
: Intercessory protocolalways-bound
: Declarationsand-let*
: Other binding formsandmap
: Other control structuresargc+argv
: Other support proceduresargv
: Environment information and system interfacearithmetic-shift
: Arithmeticassert
: Other formsatom?
: Listsbinary-search
: Binary searchingbitwise-and
: Arithmeticbitwise-ior
: Arithmeticbitwise-not
: Arithmeticbitwise-xor
: Arithmeticblock
: Declarationsblock-global
: Declarationsblock-ref
: Low-level data accessblock-set!
: Low-level data accessbound-to-procedure
: Declarationsbreakpoint
: Exceptionsbuild-platform
: Environment information and system interfacebutlast
: Listsbyte-vector
: Bytevectorsbyte-vector->f32vector
: Unit srfi-4byte-vector->f64vector
: Unit srfi-4byte-vector->list
: Bytevectorsbyte-vector->s16vector
: Unit srfi-4byte-vector->s32vector
: Unit srfi-4byte-vector->s8vector
: Unit srfi-4byte-vector->string
: Bytevectorsbyte-vector->u16vector
: Unit srfi-4byte-vector->u32vector
: Unit srfi-4byte-vector->u8vector
: Unit srfi-4byte-vector-fill!
: Bytevectorsbyte-vector-length
: Bytevectorsbyte-vector-ref
: Bytevectorsbyte-vector-set!
: Bytevectorsbyte-vector?
: Bytevectorsc-options
: Declarationsc-runtime
: Environment information and system interfaceC_alloc
: C interfaceC_block_item
: C interfaceC_c_string
: C interfaceC_callback
: CallbacksC_callback_adjust_stack_limits
: CallbacksC_character_code
: C interfaceC_data_pointer
: C interfaceC_fix
: C interfaceC_flonum
: C interfaceC_flonum_magnitude
: C interfaceC_gc_protect
: C interfaceC_gc_unprotect
: C interfaceC_header_bits
: C interfaceC_header_size
: C interfaceC_int_to_num
: C interfaceC_intern2
: C interfaceC_intern3
: C interfaceC_list
: C interfaceC_make_character
: C interfaceC_make_header
: C interfaceC_mpointer
: C interfaceC_mutate
: C interfaceC_num_to_int
: C interfaceC_pair
: C interfaceC_pointer_address
: C interfaceC_restore
: C interfaceC_save
: C interfaceC_SCHEME_END_OF_FILE
: C interfaceC_SCHEME_END_OF_LIST
: C interfaceC_SCHEME_FALSE
: C interfaceC_SCHEME_TRUE
: C interfaceC_SIZEOF_FLONUM
: C interfaceC_SIZEOF_INTERNED_SYMBOL
: C interfaceC_SIZEOF_LIST
: C interfaceC_SIZEOF_LOCATIVE
: C interfaceC_SIZEOF_PAIR
: C interfaceC_SIZEOF_POINTER
: C interfaceC_SIZEOF_STRING
: C interfaceC_SIZEOF_TAGGED_POINTER
: C interfaceC_SIZEOF_VECTOR
: C interfaceC_string
: C interfaceC_string2
: C interfaceC_symbol_value
: C interfaceC_u_i_car
: C interfaceC_unfix
: C interfaceC_vector
: C interfacecall-with-input-pipe
: Pipescall-with-input-string
: String-port extensionscall-with-output-pipe
: Pipescall-with-output-string
: String-port extensionscall/cc
: Continuationscase-lambda
: Binding forms for optional argumentscase-sensitive
: Parameterschange-directory
: Directorieschange-file-mode
: Permissionschange-file-owner
: Permissionschar-name
: User-defined named characterscheck-c-syntax
: Declarationschicken-home
: Environment information and system interfacechicken-version
: Environment information and system interfaceCHICKEN_apply
: EmbeddingCHICKEN_apply_to_string
: EmbeddingCHICKEN_continue
: EmbeddingCHICKEN_delete_gc_root
: EmbeddingCHICKEN_eval
: EmbeddingCHICKEN_eval_string
: EmbeddingCHICKEN_eval_string_to_string
: EmbeddingCHICKEN_eval_to_string
: EmbeddingCHICKEN_gc_root_ref
: EmbeddingCHICKEN_gc_root_set
: EmbeddingCHICKEN_get_error_message
: EmbeddingCHICKEN_global_lookup
: EmbeddingCHICKEN_global_ref
: EmbeddingCHICKEN_global_set
: EmbeddingCHICKEN_initialize
: EmbeddingCHICKEN_load
: EmbeddingCHICKEN_new_gc_root
: EmbeddingCHICKEN_parse_command_line
: EmbeddingCHICKEN_read
: EmbeddingCHICKEN_run
: EmbeddingCHICKEN_yield
: Embeddingchop
: Listsclass-cpl
: Introspectionclass-direct-slots
: Introspectionclass-direct-supers
: Introspectionclass-name
: Introspectionclass-of
: Introspectionclass-slots
: Introspectionclose-input-pipe
: Pipesclose-output-pipe
: Pipescommand-line-arguments
: Parameterscompile
: Procedures and macros available in setup scriptscomplement
: Combinatorscompose
: Combinatorscompress
: Listscompress-literals
: Declarationscompute-apply-generic
: Intercessory protocolcompute-apply-methods
: Intercessory protocolcompute-cpl
: Intercessory protocolcompute-getter-and-setter
: Intercessory protocolcompute-method-more-specific?
: Intercessory protocolcompute-methods
: Intercessory protocolcompute-slots
: Intercessory protocolconc
: Stringscond-expand
: Other formscondition-case
: Exceptionsconjoin
: Combinatorsconstant
: Declarationsconstantly
: Combinatorsconstructor:
: Accessing external objectscontinuation-capture
: Continuationscontinuation-graft
: Continuationscontinuation-return
: Continuationscontinuation?
: Continuationscopy-file
: Procedures and macros available in setup scriptscopy-read-table
: Reader extensionscpu-time
: Execution timecreate-directory
: Procedures and macros available in setup scriptscreate-directory
: Directoriescreate-fifo
: Fifoscreate-pipe
: Pipescreate-session
: Permissionscreate-symbolic-link
: Hard and symbolic linkscreate-temporary-file
: Temporary filescurrent-directory
: Directoriescurrent-effective-group-id
: Permissionscurrent-effective-user-id
: Permissionscurrent-environment
: Environment accesscurrent-error-port
: File Input/Outputcurrent-gc-milliseconds
: Execution timecurrent-group-id
: Permissionscurrent-milliseconds
: Execution timecurrent-process-id
: Processescurrent-read-table
: Parameterscurrent-seconds
: Execution timecurrent-user-id
: Permissionscut
: Other binding formscute
: Other binding formsdeclare
: Declarationsdecompose-pathname
: Pathname operationsdefine-class
: Defining formsdefine-constant
: Substitution forms and macrosdefine-extension
: Making extra libraries and extensions availabledefine-external
: Callbacksdefine-for-syntax
: Substitution forms and macrosdefine-foreign-enum
: Accessing external objectsdefine-foreign-record
: Accessing external objectsdefine-foreign-type
: Accessing external objectsdefine-foreign-variable
: Accessing external objectsdefine-generic
: Defining formsdefine-inline
: Substitution forms and macrosdefine-location
: Locationsdefine-macro
: Substitution forms and macrosdefine-method
: Defining formsdefine-reader-ctor
: Reader extensionsdefine-record
: Record structuresdefine-record-printer
: Record structuresdefine-record-type
: Record structuresdefine-values
: Other binding formsdelete-directory
: Directoriesdelete-file
: Filesdelete-file*
: Deleting a file without signalling an errordescribe-object
: Additional protocoldestructor:
: Accessing external objectsdirectory
: Directoriesdirectory?
: Directoriesdisable-interrupts
: Declarationsdisable-warning
: Declarationsdisjoin
: Combinatorsdocumentation
: Procedures and macros available in setup scriptsduplicate-fileno
: File descriptors and low-level I/Odynamic-load-libraries
: Parameterseach
: Combinatorsemit-exports
: Declarationsemit-external-prototypes-first
: Declarationsenable-warnings
: Interrupts and error-handlingensure
: Other formserrno
: Environment information and system interfaceerrno/acces
: ERRNO valueserrno/again
: ERRNO valueserrno/badf
: ERRNO valueserrno/busy
: ERRNO valueserrno/child
: ERRNO valueserrno/exist
: ERRNO valueserrno/fault
: ERRNO valueserrno/intr
: ERRNO valueserrno/inval
: ERRNO valueserrno/io
: ERRNO valueserrno/isdir
: ERRNO valueserrno/mfile
: ERRNO valueserrno/noent
: ERRNO valueserrno/noexec
: ERRNO valueserrno/nomem
: ERRNO valueserrno/nospc
: ERRNO valueserrno/notdir
: ERRNO valueserrno/perm
: ERRNO valueserrno/pipe
: ERRNO valueserrno/rofs
: ERRNO valueserrno/spipe
: ERRNO valueserrno/srch
: ERRNO valueserrno/wouldblock
: ERRNO valueserror
: Interrupts and error-handlingeval
: Evaleval-handler
: Parameterseval-when
: Other formsexamples
: Procedures and macros available in setup scriptsexit
: Environment information and system interfaceexit-handler
: Parametersexport
: Declarationsexports
: Procedures and macros available in setup scriptsextend-procedure
: Extending procedures with dataextended-bindings
: Declarationsextended-procedure?
: Extending procedures with dataextension-information
: Loading extension librariesf32vector->byte-vector
: Unit srfi-4f64vector->byte-vector
: Unit srfi-4feature?
: Feature identifiersfeatures
: Feature identifiersfifo?
: Fifosfile-access-time
: Retrieving file attributesfile-change-time
: Retrieving file attributesfile-close
: File descriptors and low-level I/Ofile-execute-access?
: Permissionsfile-exists?
: Filesfile-link
: Hard and symbolic linksfile-lock
: Record lockingfile-lock/blocking
: Record lockingfile-mkstemp
: File descriptors and low-level I/Ofile-modification-time
: Retrieving file attributesfile-open
: File descriptors and low-level I/Ofile-owner
: Permissionsfile-permissions
: Permissionsfile-position
: Retrieving file attributesfile-read
: File descriptors and low-level I/Ofile-read-access?
: Permissionsfile-select
: File descriptors and low-level I/Ofile-size
: Retrieving file attributesfile-stat
: Retrieving file attributesfile-test-lock
: Record lockingfile-truncate
: Changing file attributesfile-unlock
: Record lockingfile-write
: File descriptors and low-level I/Ofile-write-access?
: Permissionsfileno/stderr
: File descriptors and low-level I/Ofileno/stdin
: File descriptors and low-level I/Ofileno/stdout
: File descriptors and low-level I/Ofind-files
: Finding filesfind-header
: Procedures and macros available in setup scriptsfind-library
: Procedures and macros available in setup scriptsfixnum-arithmetic
: Declarationsfixnum?
: Arithmeticflatten
: Listsflip
: Combinatorsfluid-let
: Other binding formsflush-output
: File Input/Outputfor-each-argv-line
: Iterating over input lines and filesfor-each-line
: Iterating over input lines and filesforce-finalizers
: Parametersforeign-code
: Accessing external objectsforeign-declare
: Accessing external objectsforeign-lambda
: Accessing external objectsforeign-lambda*
: Accessing external objectsforeign-parse
: Accessing external objectsforeign-parse/declare
: Accessing external objectsforeign-primitive
: Accessing external objectsforeign-safe-lambda
: Accessing external objectsforeign-safe-lambda*
: Accessing external objectsforeign-value
: Accessing external objectsformat
: Formatted outputfp*
: Arithmeticfp+
: Arithmeticfp-
: Arithmeticfp/
: Arithmeticfp<
: Arithmeticfp<=
: Arithmeticfp=
: Arithmeticfp>
: Arithmeticfp>=
: Arithmeticfpmax
: Arithmeticfpmin
: Arithmeticfpneg
: Arithmeticfprintf
: Formatted outputfree
: Foreign pointersfx*
: Arithmeticfx+
: Arithmeticfx-
: Arithmeticfx/
: Arithmeticfx<
: Arithmeticfx<=
: Arithmeticfx=
: Arithmeticfx>
: Arithmeticfx>=
: Arithmeticfxand
: Arithmeticfxior
: Arithmeticfxmax
: Arithmeticfxmin
: Arithmeticfxmod
: Arithmeticfxneg
: Arithmeticfxnot
: Arithmeticfxshl
: Arithmeticfxshr
: Arithmeticfxxor
: Arithmeticgc
: Garbage collectiongeneric-methods
: Introspectiongensym
: Generating uninterned symbolsget-call-chain
: Interrupts and error-handlingget-groups
: Permissionsget-host-name
: Getting the hostname and system informationget-keyword
: Keywordsget-line-number
: Macrosget-output-string
: String portsgetenv
: Environment information and system interfacegetter-with-setter
: Settersglob
: Directoriesglob->regexp
: Unit regexglobal-bound?
: Accessing toplevel variablesglobal-ref
: Accessing toplevel variablesglobal-set!
: Accessing toplevel variablesgrep
: Unit regexgroup-information
: Permissionshide
: Declarationsidentity
: Combinatorsimplicit-exit-handler
: Parametersimport
: Declarationsinclude
: Other formsinitialize
: Intercessory protocolinitialize-groups
: Permissionsinitialize-slots
: Utility proceduresinline
: Declarationsinline-limit
: Declarationsinstall-extension
: Procedures and macros available in setup scriptsinstall-program
: Procedures and macros available in setup scriptsinstall-script
: Procedures and macros available in setup scriptsinstallation-prefix
: Procedures and macros available in setup scriptsinstance-of?
: Introspectioninstance?
: Base languageinterrupts-enabled
: Declarationsintersperse
: Listsjoin
: Listskeep-shadowed-macros
: Declarationskeyword->string
: Keywordskeyword-style
: Parameterskeyword?
: Keywordslambda-lift
: Declarationslet*-values
: Other binding formslet-location
: Locationslet-optionals
: Binding forms for optional argumentslet-optionals*
: Binding forms for optional argumentslet-values
: Other binding formsletrec-values
: Other binding formslink-options
: Declarationslist->byte-vector
: Bytevectorslist->queue
: Queueslist-of
: Combinatorsload
: Loading codeload-library
: Loading codeload-noisily
: Loading codeload-relative
: Loading codeload-verbose
: Parameterslocal-time->seconds
: Date and time routineslocal-timezone-abbreviation
: Date and time routineslocation
: Locationslocative->object
: Locativeslocative-ref
: Locativeslocative-set!
: Locativeslocative?
: Locativesmachine-byte-order
: Environment information and system interfacemachine-type
: Environment information and system interfacemacro?
: Macrosmacroexpand
: Macrosmacroexpand-1
: Macrosmake
: Procedures and macros available in setup scriptsmake
: Base languagemake-absolute-pathname
: Pathname operationsmake-byte-vector
: Bytevectorsmake-class
: Base languagemake-generic
: Base languagemake-input-port
: Input/Output extensionsmake-locative
: Locativesmake-method
: Base languagemake-output-port
: Input/Output extensionsmake-parameter
: Parametersmake-pathname
: Pathname operationsmake-queue
: Queuesmake-record-instance
: Low-level data accessmake-static-byte-vector
: Bytevectorsmake-weak-locative
: Locativesmake-XXXvector
: Unit srfi-4map-file-to-memory
: Memory mapped I/Omatch-error-control
: Pattern Matchingmemory-mapped-file-pointer
: Memory mapped I/Omemory-mapped-file?
: Memory mapped I/Omemory-statistics
: Garbage collectionmerge
: Sortingmerge!
: Sortingmethod-procedure
: Introspectionmethod-specializers
: Introspectionmove-file
: Procedures and macros available in setup scriptsmove-memory!
: Low-level data accessno-argc-checks
: Declarationsno-bound-checks
: Declarationsno-procedure-checks
: Declarationsnoop
: Combinatorsnth-value
: Other formsnull-pointer
: Foreign pointersnull-pointer?
: Foreign pointersnumber-of-bytes
: Low-level data accessnumber-of-slots
: Low-level data accessobject->pointer
: Foreign pointersobject-become!
: Magicobject-copy
: Low-level data accessobject-evict
: Data in unmanaged memoryobject-evict-to-location
: Data in unmanaged memoryobject-evicted?
: Data in unmanaged memoryobject-release
: Data in unmanaged memoryobject-size
: Data in unmanaged memoryobject-unevict
: Data in unmanaged memoryopen-input-file*
: File descriptors and low-level I/Oopen-input-pipe
: Pipesopen-input-string
: String portsopen-output-file*
: File descriptors and low-level I/Oopen-output-pipe
: Pipesopen-output-string
: String portsopen/append
: File descriptors and low-level I/Oopen/binary
: File descriptors and low-level I/Oopen/creat
: File descriptors and low-level I/Oopen/excl
: File descriptors and low-level I/Oopen/fsync
: File descriptors and low-level I/Oopen/noctty
: File descriptors and low-level I/Oopen/nonblock
: File descriptors and low-level I/Oopen/rdonly
: File descriptors and low-level I/Oopen/rdwr
: File descriptors and low-level I/Oopen/read
: File descriptors and low-level I/Oopen/sync
: File descriptors and low-level I/Oopen/text
: File descriptors and low-level I/Oopen/trunc
: File descriptors and low-level I/Oopen/write
: File descriptors and low-level I/Oopen/wronly
: File descriptors and low-level I/Oormap
: Other control structuresparameterize
: Other binding formsparent-process-id
: Processespatch
: Procedures and macros available in setup scriptspathname-directory
: Pathname operationspathname-extension
: Pathname operationspathname-file
: Pathname operationspathname-replace-directory
: Pathname operationspathname-replace-extension
: Pathname operationspathname-replace-file
: Pathname operationspathname-strip-directory
: Pathname operationspathname-strip-extension
: Pathname operationsperm/irgrp
: Permissionsperm/iroth
: Permissionsperm/irusr
: Permissionsperm/irwxg
: Permissionsperm/irwxo
: Permissionsperm/irwxu
: Permissionsperm/isgid
: Permissionsperm/isuid
: Permissionsperm/isvtx
: Permissionsperm/iwgrp
: Permissionsperm/iwoth
: Permissionsperm/iwusr
: Permissionsperm/ixgrp
: Permissionsperm/ixoth
: Permissionsperm/ixusr
: Permissionspipe/buf
: Pipespointer->address
: Foreign pointerspointer->object
: Foreign pointerspointer-f32-ref
: Foreign pointerspointer-f32-set!
: Foreign pointerspointer-f64-ref
: Foreign pointerspointer-f64-set!
: Foreign pointerspointer-offset
: Foreign pointerspointer-s16-ref
: Foreign pointerspointer-s16-set!
: Foreign pointerspointer-s32-ref
: Foreign pointerspointer-s32-set!
: Foreign pointerspointer-s8-ref
: Foreign pointerspointer-s8-set!
: Foreign pointerspointer-tag
: Tagged pointerspointer-u16-ref
: Foreign pointerspointer-u16-set!
: Foreign pointerspointer-u32-ref
: Foreign pointerspointer-u32-set!
: Foreign pointerspointer-u8-ref
: Foreign pointerspointer-u8-set!
: Foreign pointerspointer=?
: Foreign pointerspointer?
: Foreign pointersport->fileno
: File descriptors and low-level I/Oport-for-each
: Iterating over input lines and filesport-map
: Iterating over input lines and filesport-name
: File Input/Outputport-position
: File Input/Outputport?
: Standard Input/Outputpost-process
: Declarationspp
: Input/Output extensionspretty-print
: Input/Output extensionspretty-print-width
: Input/Output extensionsprint
: Standard Input/Outputprint*
: Standard Input/Outputprint-call-chain
: Interrupts and error-handlingprint-error-message
: Interrupts and error-handlingprint-object
: Additional protocolprintf
: Formatted outputprocedure-data
: Extending procedures with dataprocedure-information
: Interrupts and error-handlingprocess
: Processesprocess-execute
: Processesprocess-fork
: Processesprocess-group-id
: Permissionsprocess-run
: Processesprocess-signal
: Processesprocess-spawn
: Windows specific notesprocess-wait
: Processesprogram-path
: Procedures and macros available in setup scriptsproject
: Combinatorspromise?
: Other control structuresprovide
: Loading extension librariesprovided?
: Loading extension librariesqueue->list
: Queuesqueue-add!
: Queuesqueue-empty?
: Queuesqueue-first
: Queuesqueue-last
: Queuesqueue-push-back!
: Queuesqueue-push-back-list!
: Queuesqueue-remove!
: Queuesqueue?
: Queuesrandom
: Random numbersrandomize
: Random numbersrassoc
: Listsread-all
: Reading a file's contentsread-file
: Input/Output extensionsread-line
: Input/Output extensionsread-lines
: Input/Output extensionsread-string
: Input/Output extensionsread-symbolic-link
: Hard and symbolic linksread-token
: Input/Output extensionsrec
: Other binding formsreceive
: Other binding formsrecord->vector
: Low-level data accessrecord-instance?
: Low-level data accessregexp
: Unit regexregexp-escape
: Unit regexregexp?
: Unit regexregister-feature!
: Feature identifiersregular-file?
: Retrieving file attributesremove-file*
: Procedures and macros available in setup scriptsrename-file
: Filesrename:
: Accessing external objectsrepl
: Read-eval-print looprepl-prompt
: Parametersrepository-path
: Loading extension librariesrequire
: Loading extension librariesrequire-at-runtime
: Procedures and macros available in setup scriptsrequire-extension
: Making extra libraries and extensions availablerequire-for-syntax
: Loading extension librariesreset
: Interrupts and error-handlingreset-handler
: Parametersreturn-to-host
: Embeddingreverse-list->string
: String utilitiesrun
: Procedures and macros available in setup scriptsrun-time-macros
: Declarationss16vector->byte-vector
: Unit srfi-4s32vector->byte-vector
: Unit srfi-4s8vector->byte-vector
: Unit srfi-4seconds->local-time
: Date and time routinesseconds->string
: Date and time routinesseconds->utc-time
: Date and time routinesset!-values
: Other binding formsset-alarm!
: Signal handlingset-buffering-mode!
: Setting the file buffering modeset-describer!
: Macros and procedures implemented in the interpreterset-dynamic-load-mode!
: Loading codeset-extension-specifier!
: Loading extension librariesset-file-position!
: Changing file attributesset-finalizer!
: Garbage collectionset-gc-report!
: Garbage collectionset-group-id!
: Permissionsset-groups!
: Permissionsset-invalid-procedure-call-handler!
: Procedure-call- and variable reference hooksset-port-name!
: File Input/Outputset-procedure-data!
: Extending procedures with dataset-process-group-id!
: Permissionsset-read-syntax!
: Reader extensionsset-root-directory!
: Directoriesset-sharp-read-syntax!
: Reader extensionsset-signal-handler!
: Signal handlingset-signal-mask!
: Signal handlingset-user-id!
: Permissionssetenv
: Environment accesssetter
: Setterssetup-build-directory
: Procedures and macros available in setup scriptssetup-install-flag
: Procedures and macros available in setup scriptssetup-root-directory
: Procedures and macros available in setup scriptssetup-verbose-flag
: Procedures and macros available in setup scriptsshift!
: Miscellaneous handy thingsshuffle
: Listssignal/abrt
: Signal handlingsignal/alrm
: Signal handlingsignal/chld
: Signal handlingsignal/cont
: Signal handlingsignal/fpe
: Signal handlingsignal/hup
: Signal handlingsignal/ill
: Signal handlingsignal/int
: Signal handlingsignal/io
: Signal handlingsignal/kill
: Signal handlingsignal/pipe
: Signal handlingsignal/prof
: Signal handlingsignal/quit
: Signal handlingsignal/segv
: Signal handlingsignal/stop
: Signal handlingsignal/term
: Signal handlingsignal/trap
: Signal handlingsignal/tstp
: Signal handlingsignal/urg
: Signal handlingsignal/usr1
: Signal handlingsignal/usr2
: Signal handlingsignal/vtalrm
: Signal handlingsignal/winch
: Signal handlingsignal/xcpu
: Signal handlingsignal/xfsz
: Signal handlingsignum
: Arithmeticsinglestep
: Interrupts and error-handlingsleep
: Processesslot-ref
: Base languageslot-set!
: Base languagesoftware-type
: Environment information and system interfacesoftware-version
: Environment information and system interfacesort
: Sortingsort!
: Sortingsorted?
: Sortingspawn/detach
: Windows specific notesspawn/nowait
: Windows specific notesspawn/nowaito
: Windows specific notesspawn/overlay
: Windows specific notesspawn/wait
: Windows specific notessprintf
: Formatted outputstandard-bindings
: Declarationsstatic-byte-vector->pointer
: Bytevectorsstring->byte-vector
: Bytevectorsstring->keyword
: Keywordsstring->uninterned-symbol
: Generating uninterned symbolsstring-chomp
: Stringsstring-chop
: Stringsstring-compare3
: Stringsstring-compare3-ci
: Stringsstring-intersperse
: Stringsstring-match
: Unit regexstring-match-positions
: Unit regexstring-search
: Unit regexstring-search-positions
: Unit regexstring-split
: Stringsstring-split-fields
: Unit regexstring-substitute
: Unit regexstring-substitute*
: Unit regexstring-translate
: Stringsstring-translate*
: Stringssub1
: Arithmeticsubclass?
: Introspectionsubf32vector
: Unit srfi-4subf64vector
: Unit srfi-4subs16vector
: Unit srfi-4subs32vector
: Unit srfi-4subs8vector
: Unit srfi-4substring-ci=?
: Stringssubstring-index
: Stringssubstring-index-ci
: Stringssubstring=?
: Stringssubu16vector
: Unit srfi-4subu32vector
: Unit srfi-4subu8vector
: Unit srfi-4switch
: Conditional formssymbolic-link?
: Hard and symbolic linkssyntax
: Procedures and macros available in setup scriptssyntax-error
: Macrossystem
: Environment information and system interfacesystem*
: Executing shell commands with formatstring and error checkingsystem-information
: Getting the hostname and system informationtag-pointer
: Tagged pointerstagged-pointer?
: Tagged pointerstail?
: Liststcp-abandon-port
: Unit tcptcp-accept
: Unit tcptcp-accept-ready?
: Unit tcptcp-addresses
: Unit tcptcp-buffer-size
: Unit tcptcp-close
: Unit tcptcp-connect
: Unit tcptcp-listen
: Unit tcptcp-listener-fileno
: Unit tcptcp-listener-port
: Unit tcptcp-listener?
: Unit tcptcp-port-numbers
: Unit tcpterminal-name
: Terminal portsterminal-port?
: Terminal portstest-compile
: Procedures and macros available in setup scriptsthread-quantum
: Unit srfi-18thread-quantum-set!
: Unit srfi-18thread-signal!
: Unit srfi-18time
: Other formstime->string
: Date and time routinestoplevel-command
: Toplevel commandsTYPE
: DeclarationsTYPENAME-SLOTNAME
: Accessing external objectsTYPENAME-SLOTNAME-set!
: Accessing external objectsu16vector->byte-vector
: Unit srfi-4u32vector->byte-vector
: Unit srfi-4u8vector->byte-vector
: Unit srfi-4unbound-variable-value
: Procedure-call- and variable reference hooksundefine-macro!
: Macrosunit
: Declarationsunless
: Conditional formsunmap-file-from-memory
: Memory mapped I/Ounregister-feature!
: Feature identifiersunsafe
: Declarationsunsetenv
: Environment accessunshift!
: Miscellaneous handy thingsuse
: Making extra libraries and extensions availableuser-information
: Permissionsuser-options-pass
: Extending the compileruser-pass
: Extending the compileruser-pass-2
: Extending the compileruser-post-analysis-pass
: Extending the compileruser-preprocessor-pass
: Extending the compileruser-read-pass
: Extending the compileruses
: Declarationsusual-integrations
: Declarationsutc-time->seconds
: Date and time routinesvector-copy!
: Vectorsvector-resize
: Vectorsversion
: Procedures and macros available in setup scriptsvoid
: The unspecified valuewarning
: Interrupts and error-handlingwhen
: Conditional formswith-error-output-to-port
: Input/Output extensionswith-input-from-pipe
: Pipeswith-input-from-port
: Input/Output extensionswith-input-from-string
: String-port extensionswith-output-to-pipe
: Pipeswith-output-to-port
: Input/Output extensionswith-output-to-string
: String-port extensionswrite-line
: Input/Output extensionswrite-string
: Input/Output extensions