GCC logohttp://people.csail.mit.edu/jaffer/Schlep/scm2c.html


Current VersionReleasedTerms
1.17Dec 26 2014Free

Table of Contents



Usage: scm2c [-p stdc | nil | scm] FILE1.scm FILE2.scm ... [DIR/]
Usage: scm2c [-p stdc | nil | scm] FILE1.c FILE2.c ... [DIR/]
  Translates Scheme files FILE1.scm, FILE2.scm, ... to
  DIR/FILEn.c, DIR/FILEn.h, and DIR/FILEn.txi.
Usage: scm2c [-p stdc | nil | scm] FILE1.h FILE2.h ... [DIR/]
  Translates Scheme files FILE1.scm, FILE2.scm, ... to
  DIR/FILEn.h and DIR/FILEn.txi.

 -p stdc        FILE*.h will have ANSI prototypes
 -p nil         FILE*.h will have () prototypes
 -p scm         FILE*.h will have SCM conditional prototypes


Source Language

Scm2c is a Scheme to C translator for a subset of Scheme. Using Scheme files as source, scm2c produces texinfo documentation and formatted C code preserving comments; and type, function, and variable names as much as possible. The output from scm2c is human-readable and can form the base for further development in C; abandoning the original Scheme source.

Note that scm2c is a translator -- the C code it produces will be nearly as readable as the original Scheme source. An unrelated project, Hobbit compiles full R4RS Scheme to C functions for use with the SCM Scheme Implementation.

Scope of the Scheme Subset

The Scheme subset supported by scm2c is listed at
http://people.csail.mit.edu/jaffer/Schlep/#Schlep Subset.

Scheme integers and real numbers, booleans, chars, and strings are translated to the equivalent C types. SLIB byte vectors map to the type byte* in C code. Vectors of user defined (C) types are supported.

The user incorporates custom C data types by coding their definitions and accessors in a C include file. For the purposes of running in Scheme, put analogous definitions in a file which is not translated by scm2c.

Characters in variable names are translated as follows:
`%' => `_Percent'
`@' => `_At'
`->' => `to_'
`=' => `_equal'
`>' => `_more'
`<' => `_less'
`?' => `_P'
`:' => `_'
`.' => `_'
`-' => `_'

Scm2c does not include support for a Scheme runtime. Generated C programs should not assume garbage collection or general Scheme values unless linked with a library providing these features.

Scheme identifier names are used to determine the C types of the corresponding variables and functions. This is described in the Declarations section.


Defmacros transforming Scheme code to Scheme code may be defined in Scheme source files. If there are defmacros used in more than one Scheme source file, they can be removed to a separate file, and that file imported by a call to defmacro:load in each file (before a defmacro is invoked).

Function: defmacro:load filename

Imports defmacros from filename.

Variable: *schlep-defmacros*

Is an alist associating defmacro names with their transformers. It is fluid-let during each file translatation, so that scm2c macro definitions do not take effect in the Scheme top level.


Variable: declarations
The C types to which variables are translated are controlled by declarations. A declaration is a list (glob type). glob is a match string; type an expression. Globs are matched to variable names with trailing digits removed. Declarations made later override those made earlier.

Declarations can be made by a call to declare-names. If no declarations have been made before schlep is called, schlep will read a list of declarations from a file named `scm2c.typ' if it exists in the same directory as the Scheme file being translated.

If the type is a symbol or string, variables which match the glob string will be declared as types of that name. If the type is a list, then it will be interpreted as follows.

(ptr type)
produces * type.
(array type)
produces type[].

Types can be nested. Procedure names ending with `!' are typed to return void. Procedure names ending with `?' are translated to names with a trailing `_P'.

These procedures can be mixed with code to be translated.

Function: declare-names globs
globs must be a list of lists (declarations). Glob declarations may have local scope. Glob declarations at top level persist from one file to the next.

The Scheme files in the table at
have examples of the use of declare-names.

Target Language

Function: pragma.h string ...
In Scheme source, pragma.h has no effect, but the strings are written to filename.h during translation.

Function: pragma.c string ...
In Scheme source, pragma.c has no effect, but the strings are written to filename.c during translation.

Function: prototype-style style
Sets the type of prototypes written to the .h files. Make style the symbol STDC for ANSI function prototypes; SCM for SCM conditional prototypes; NIL for K&R C. The default value is STDC.

To work with the conditional prototypes, an include file loaded before the Schlepped .h files should contain: #include "schleprt.h"

#include "schleprt.h" or its content may also be needed if your code uses min, max, non-stack allocations, or the diagnostic output routines dprintf, wdprintf, or edprintf. If you use the diagnostic output routines, you must also define diagout. The file "schleprt.c" does this; its entire content is:

 #include <stdio.h>
 FILE *diagout;

Variable: number-lines?

Define number-lines? to #t for C code to be prefixed with commmented line numbers. The default value is #f.

Schlep API

The following procedures are called to translate a file. They should not be called from the file being translated.

Function: schlep filename ...
Translates scheme files to C. Each argument must be a string. If the suffix is:
Produces files filename.c, filename,h, and filename.txi.
Produces files filename.h and filename.txi.

Otherwise, the file named by the argument filename is translated to filename.c, filename,h, and filename.txi.

If filename.txi is empty after translation, then it is deleted.

Procedure: declarations-report! cumulative? port
Prints an association-list (suitable passing to declare-name!) along with the counts of references to each name pattern type declared.

I am a guest and not a member of the MIT Computer Science and Artificial Intelligence Laboratory. My actions and comments do not reflect in any way on MIT.
agj @ alum.mit.edu
Go Figure!