29. Bigloo A ``practical Scheme compiler'' User manual for version 3.2a January 2009 -- SRFIs

main page

top: | Bigloo A ``practical Scheme compiler'' User manual for version 3.2a January 2009

SRFIs

Chapters

Bigloo supports various SRFIs (Scheme Request For Implementation). Some of them are integrated in the Bigloo core libraries. Some others are implemented by the means of Bigloo libraries (see Bigloo Libraries). Only the first ones are described in the manual. Check the Bigloo web page (http://www.inria.fr/mimosa/fp/Bigloo). The current Bigloo core library support the following SRFIs:

- `srfi-0` (Conditional execution).
- `srfi-2` (AND-LET*: an AND with local bindings, a guarded LET* special form).
- `srfi-6` (Basic String Ports).
- `srfi-8` (Binding to multiple values).
- `srfi-9` (Records specification).
- `srfi-18` (Multithreading support).
- `srfi-22` (Script interpreter invocation).
- `srfi-28` (Basic Format Strings).
- `srfi-30` (Multi-line comments).
- `srfi-34` (Exception Handling for Programs).

### 29.1 SRFI 0

cond-expand [clause] | bigloo syntax

The `cond-expand` form tests for the existence of features at macro-expansion time. It either expands into the body of one of its clauses or signals and error during syntactic processing. `cond-expand` expands into the body of the first clause whose feature requirement is currently satisfied (the `else` clause, if present, is selected if none of the previous clauses is selected).
A feature requirement has an obvious interpretation as a logical formula, where the variables have meaning true is the feature corresponding to the feature identifier, as specified in the SRFI registry, is in effect at the location of the `cond-expand` form, and false otherwise. A feature requirement is satisfied it its formula is true under this interpretation. The formula may make use of identifier, `and`, `or` and `not` operators.
Since Bigloo version 3.1b, `cond-expand` formula may use the new `library` operator that checks if a library exists and is available. Its syntax is: `(library <libname>)`.
Examples:

    (write (cond-expand
              (srfi-0 (* 1 2))
              ((or (library fthread) (library pthread)) (- 4 1))
              (else (+ 3 4))))
       -| 2

    (cond-expand
       (bigloo (define (command-line-arguments) (command-line)))
       (else (define (command-line-arguments) '())))

The second example assumes that `bigloo` is an alias for the SRFI associated with the specification of Bigloo (i.e. the documentation for that Scheme system).
When writing portable code, the case used for the feature identifier should match the one in the SRFI registry. This is to ensure that the feature identifier will be correctly recognized whether or not the Scheme system is case-sensitive. To support case-insensitive Scheme systems, the feature identifiers in the SRFI registry are guaranteed to be unique even when ignoring the case.
In order to distinguish Bigloo versions, the following symbols are recognized in `cond-expand` forms.

- `bigloo`
- `bigloo<branch-release>`
- `bigloo<major-release>`
- `bigloo<major-release><minor-release>`

When finalizers have been configured, the two following symbols are recognized by `cond-expand`:

- `bigloo-finalizer`
- `bigloo-weakptr`

Bigloo implements differents SRFI for the compiler and the interpreter. Thus, their are two Bigloo SRFI registers. One for the compiler and one for the interpreter. Bigloo compiler SRFI register contains at least the following symbols:

- `srfi-0`
- `srfi-1`
- `srfi-2`
- `srfi-6`
- `srfi-8`
- `srfi-9`
- `srfi-22`
- `srfi-28`
- `srfi-30`

With respect to the currently used Bigloo back-end, one of these symbols is registered:

- `bigloo-c`
- `bigloo-jvm`

Bigloo compiler implements the following SRFI:

- `bigloo`
- `bigloo-compile`
- `bigloo<major-release>`
- `bigloo<major-release><minor-release>`

Then the `-g` flag is used, the Bigloo compiler additionally implements the SRFI:

- `bigloo-debug`

Bigloo interpreter implements the following SRFI:

- `bigloo`
- `bigloo-eval`
- `bigloo<major-release>`
- `bigloo<major-release><minor-release>`

When a library is used, the name of the library is added to the compiler SRFI register. That is:

    (module foo
       (library srfi1))

    (print (cond-expand (srfi1 'with-srfi1) (else 'nothing)))
       -| 'with-srfi1
    (print (eval '(cond-expand (srfi1 'with-srfi1) (else 'nothing))))
       -| 'with-srfi1

register-eval-srfi! srfi-name | bigloo procedure

This argument `srfi-name` is a symbol. It registers `srfi-name` in the Bigloo interpreter SRFI register. This function must only be used when implementing a library. The code of that library must contain one unique call to `register-eval-srfi!`. Let's suppose, for instance, a `format` library. The implementation for that library must contain an expression like:

    (register-eval-srfi! 'format)

Calling `(register-eval-srfi! name)` makes `name` supported by interpreted `cond-expand` forms.
Note: There is no `register-compiler-srfi!` because the compiler automatically registers SRFI when the `-library` flags are used. However, it exists several ways to tell the compiler that it actually supports some srfis when compiling some modules.

- The first way is to insert calls to `register-eval-srfi!` in the `.bigloorc` file (see Compiler Description).
- The second, is to use `option` (see Module Declaration) module clause, such as:

      (module example
         ...
         (option (register-srfi! 'srfi-foobar)))

      ...

- The last way is to use the command line option `-srfi` (see Compiler Description).

### 29.2 SRFI 1

The SRFI 1 is implemented as a Bigloo library. Hence, in order to use the functions it provides, a module must import it.

    (module ex
       (library srfi1))

    (print (find-tail even? '(3 1 37 -8 -5 0 0)))
     => '(-8 -5 0 0))

### 29.3 SRFI 22

The SRFI 22 describes basic prerequisites for running Scheme programs as Unix scripts in a uniform way. A file (henceforth a scipt) conforming SRFI 22 has the following syntax:

    <script>         ==> <script prelude>? <program>
    <script prelude> ==> #! <space> <all but linebreak>* <linebreak>

A Scheme script interpreter loads the `<script>`. It ignores the script prelude and interprets the rest of the file according to the language dialect specified by the name of the interpreter.
The Scheme script interpreter may also load a different file after making a reasonable check that loading it is semantically equivalent to loading `<script>`. For example, the script interpreter may assume that a file with a related name (say, with an additional extension) is a compiled version of `<script>`.

### 29.3.1 An example of SRFI-22 script

Let us consider the following Bigloo script located in a file `foo.scm':

    #! /usr/bin/env ./execute
    (module foo
       (main main))

    (define (main argv)
       (print "foo: " argv))

Let us consider the following `execute' shell script:

    $ cat > execute
    #!/bin/sh
    bigloo -i $*

Provided that `foo.scm' as the execute flag switched on, it is possible to execute it:

    $ chmod u+x foo.scm
    $ ./foo.scm
      -| foo: (./foo.scm)

The same Bigloo module can be compiled and executed such as:

    $ bigloo foo.scm
    $ ./a.out
      -| foo: (a.out)

### 29.3.2 Lazy compilation with SRFI-22

SRFI-22 can be used to implement lazy compilation. For instance, let us consider the following shell script:

    $ cat > bgl
    #!/bin/sh
    SOURCEFILE=$1
    case $SOURCEFILE in
        *.scm)
            OUTFILE=${SOURCEFILE%.scm}
            if ( bigloo -s -o $OUTFILE $SOURCEFILE ); then
                /bin/rm $OUTFILE.o
                shift
                ./$OUTFILE $@
            fi
            ;;
        *)
            echo Error: need a \*.scm file!
            ;;
    esac

And the following Bigloo script:

    #! /usr/bin/env ./bgl
    (module foo
       (main main))

    (define (main argv)
       (print "foo: " argv))

When executed in the following way:

    $ chmod u+x foo.scm
    $ ./foo.scm
      -| foo: (./foo.scm)

The Bigloo module `foo.scm` will first be compiled and then executed. Of course, one may consider more complex compiler drivers where it is first checked that the module is not already compiled.

This Html page has been produced by Skribe.
Last update Tue Jan 6 18:43:51 2009.