http://people.csail.mit.edu/jaffer/Docupage/schlep.html

schlep

Current VersionReleasedTerms
1.632007-05-11Free


Schlep is a Scheme to C translator for a subset of Scheme.
(For full R4RS Scheme compilation, see Hobbit.)

Quick Start

Usage


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

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

Documentation


Usage

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

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

Overview

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

Note that schlep 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

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; and in a Scheme file which is not translated by schlep.

Characters in variable names are translated as follows:
`%' => `_Percent'
`@' => `_At'
`:' => `_'
`-' => `_'
`?' => `_P'

Schlep 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 Syntax Translation

Syntax: quote <datum>
<datum>

Syntax: and <test1> ...
&&, if

Syntax: or <test1> ...
||, if

Syntax: if <test> <consequent> <alternate>
? :, if

Syntax: cond <clause1> <clause2> ...
||, if

Syntax: lambda <formals> <body>

Syntax: begin <expression1> <expression2> ...
( , ), { ; }

Syntax: let <bindings> <body>
Syntax: let* <bindings> <body>
Syntax: letrec <bindings> <body>
{ ; }

Syntax: set! <variable> <expression>
=

Syntax: do <bindings> <clause> <body>
while

Syntax: case <key> <clause1> <clause2> ...
Syntax: casev <key> <clause1> <clause2> ...
Syntax: qase <key> <clause1> <clause2> ...
switch, ? :

Syntax: define <variable> <expression>
Emits a variable declaration.
Syntax: define <variable> (lambda <formals> <expression>)
Syntax: define (<variable> <formals>) <body>
Syntax: define (<variable> . <formal>) <body>
Emits a function declaration.

Tail recursion is converted to goto for functions tail calling themselves and for functions defined using internal DEFINE. Mutual tail recursion is not optimized. Internal functions called in non-tail position are not supported.

Syntax: defvar <variable> <expression>
Emits global declaration.

Function: defined? symbol
Function: provided? feature
#ifdef, #ifndef

Function: provide feature
#define

Syntax: defconst <constant> <expression>
#define in file.h

Syntax: defmacro
Invocations of DEFMACROs are expanded before translation to C.

Scheme Procedure Translation

Procedure: defmacro:load filename
Inserts macroexpanded code.

Procedure: load filename
Procedure: require symbol
#include

Function: in-vicinity vicinity filename
Function: library-vicinity
Library includes are <xxx.h>; otherwise "xxx.h".

Function: call-with-current-continuation proc
goto

Function: * z1 ...
*

Function: + z1 ...
+

Function: - z1 ...
-

Function: / z1 ...
Function: quotient n1 n2
/

Function: modulo n1 n2
Function: remainder n1 n2
%

Function: lognot n1
~

Function: logand n1 n2
Function: logtest n1 n2
&

Function: logior n1 n2
|

Function: logxor n1 n2
^

Function: ash n1 n2
Function: arithmetic-shift n1 n2
<<, >>

Function: logbit? n k
(1<< )&

Function: bit-field n1 n2
(1<< )&

Function: char-downcase char
tolower

Function: char-upcase char
toupper

Function: char->integer char
(unsigned)

Function: integer->char k

Function: not obj
!

Function: < x1 x2
Function: char<? char1 char2
<

Function: <= x1 x2
Function: char<=? char1 char2
<=

Function: > x1 x2
Function: char>? char1 char2
>

Function: >= x1 x2
Function: char>=? char1 char2
>=

Function: positive? x
0 <

Function: negative? x
0 >

Function: odd? n
&

Function: even? n
! &

Function: zero? z
!

Function: = z1 z2
Function: eq? obj1 obj2
Function: eqv? obj1 obj2
Function: char=? char1 char2
==

Function: number? obj
Function: char? obj
always true.

Function: error obj1 obj2
#error

Function: current-time
time(0L)

Function: make-string k char
Function: make-string k
Function: make-bytes k integer
Function: make-bytes k
In a define, char []; otherwise (char *)malloc.

Function: string char ...
In a define, char [].

Function: string-length string
strlen

Function: string->number string
atoi

Function: substring string start end
Returns &string[start].

Procedure: substring! string start end
Sets string[end] = 0 and returns &string[start].

Function: make-bytes k1 k2
Function: make-bytes k1
In a define, byte []; otherwise (byte *)malloc.

Function: bytes k ...
In a define, byte [].

Function: bytes-length string
sizeof

Function: make-vector k fill
Function: make-vector k
Function: vector obj ...
Function: create-array prototype bound1 bound2 ...
Function: make-array prototype bound1 bound2 ...
In a define, generates an array of the type as determined by the variable name and inline declarations or `schlep.typ'; otherwise a malloc cast to the type*.

Function: vector-length vector
sizeof

Function: vector-set-length! vector k
realloc.

Function: array-ref vector k
Function: string-ref string k
Function: byte-ref string k
Function: vector-ref vector k
vector[k]

Procedure: array-set! array k obj
Procedure: string-set! string k char
Procedure: byte-set! string k1 k2
Procedure: vector-set! vector k obj
array[k] = obj

C Types and Flags

Variable names are used to determine C types of variables.

Variable: declarations
The C types to which variables are translated are controlled by declarations. A declaration is a list (glob type). glob is a string; type an expression. Globes 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 `schlep.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.

Procedure: 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 http://swiss.csail.mit.edu/~jaffer/CNS/benchmarks.html#PRNG have examples of the use of declare-names!.

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 .h files should contain:

#ifndef SCM_EXPORT
# define SCM_EXPORT extern
#endif

#ifndef P
# ifdef __STDC__
#  ifndef __HIGHC__
#   define USE_ANSI_PROTOTYPES
#  endif
# endif
# ifdef __sgi__
#  define USE_ANSI_PROTOTYPES
# endif

# ifdef USE_ANSI_PROTOTYPES
#  define P(s) s
# else
#  define P(s) ()
# endif
#endif

Variable: number-lines?

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

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:
`scm'
`c'
Produces files filename.c, filename,h, and filename.txi.
`h'
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.

Defmacros transforming Scheme code to Scheme code may be defined in schlep source files.

Variable: *schlep-defmacros*

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


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.
Docupage
agj @ alum.mit.edu
Go Figure!