# Unit eval

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.

## Loading code

### load

    [procedure] (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 to `eval`). 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 reloading compiled code dynamically is still experimental.

### load-relative

    [procedure] (load-relative FILE [EVALPROC])

Similar to `load`, but loads `FILE` relative to the path of the currently loaded file.

### load-noisily

    [procedure] (load-noisily FILE #!key EVALUATOR TIME PRINTER)

As `load` but the result(s) of each evaluated toplevel-expression is written to standard output. If `EVALUATOR` is given and not `#f`, then each expression is evaluated by calling this argument with the read expression as argument. If `TIME` is given and not false, then the execution time of each expression is shown (as with the `time` macro). If `PRINTER` 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

    [procedure] (load-library UNIT [LIBRARYFILE])

On platforms that support dynamic loading, `load-library` loads the compiled library unit `UNIT` (which should be a symbol). If the string `LIBRARYFILE` is given, then the given shared library will be loaded and the toplevel code of the contained unit will be executed. If no `LIBRARYFILE` 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, the `load-library` does nothing.

### set-dynamic-load-mode!

    [procedure] (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`
  If `local` 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 is `global`, which means all C/C++ symbols are available to code loaded at a later stage.
`now`
  If `now` 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.

## Read-eval-print loop

### repl

    [procedure] (repl)

Start a new read-eval-print loop. Sets the `reset-handler` so that any invocation of `reset` 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.

## Macros

### get-line-number

    [procedure] (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.

### macro?

    [procedure] (macro? SYMBOL)

Returns `#t` if there exists a macro-definition for `SYMBOL`.

### macroexpand

    [procedure] (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

    [procedure] (macroexpand-1 X)

If `X` is a macro-form, expand the macro. Returns the resulting expression.

### undefine-macro!

    [procedure] (undefine-macro! SYMBOL)

Remove the current macro-definition of the macro named `SYMBOL`.

### syntax-error

    [procedure] (syntax-error [LOCATION] MESSAGE ARGUMENT ...)

Signals an exception of the kind `(exn syntax)`. Otherwise identical to `error`.

## Loading extension libraries

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.

### repository-path

[parameter] repository-path

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 variable `CHICKEN_HOME` or the default library path (usually `/usr/local/lib/chicken` on UNIX systems).

### extension-information

    [procedure] (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. Otherwise `extension-information` returns `#f`.

### provide

    [procedure] (provide ID ...)

Registers the extension IDs `ID ...` as loaded. This is mainly intended to provide aliases for certain extension identifiers.

### provided?

    [procedure] (provided? ID ...)

Returns `#t` if the extension with the IDs `ID ...` are currently loaded, or `#f` otherwise.

### require

    [procedure] (require ID ...)
    [procedure] (require-for-syntax ID ...)

If the extension library `ID` is not already loaded into the system, then `require` will lookup the location of the shared extension library and load it. If `ID` names a library-unit of the base system, then it is loaded via `load-library`. If no extension library is available for the given ID, then an attempt is made to load the file `ID.so` or `ID.scm` (in that order) from one of the following locations:

- the current include path, which defaults to the pathnames given in `CHICKEN_INCLUDE_PATH` and `CHICKEN_HOME`.
- the current directory

`ID` should be a string or a symbol. The difference between `require` and `require-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!

    [procedure] (set-extension-specifier! SYMBOL PROC)

Registers the handler-procedure `PROC` as a extension-specifier with the name `SYMBOL`. This facility allows extending the set of valid extension specifiers to be used with `require-extension`. When `register-extension` is called with an extension specifier of the form `(SPEC ...)` and `SPEC` has been registered with `set-extension-specifier!`, then `PROC` 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.

## System information

### chicken-home

    [procedure] (chicken-home)

Returns a string given the installation directory (usually `/usr/local/share/chicken` on UNIX-like systems). If the environment variable `CHICKEN_HOME` is set, then its value will be returned. As a last option, if the environment variable `CHICKEN_PREFIX` is set, then `chicken-home` will return `$CHICKEN_PREFIX/share`.

## Eval

    [procedure] (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)`.

Previous: Unit library

Next: Unit extras