Top-level programs

A top-level program specifies an entry point for defining and running a Scheme program. A top-level program specifies a set of libraries to import and code to run. Through the imported libraries, whether directly or through the transitive closure of importing, a top-level program defines a complete Scheme program.

Top-level programs accept a list of string command-line arguments that may be used to pass data to the program.

8.1  Top-level program syntax

A top-level program is a delimited piece of text, typically a file, that follows the following syntax:

<top-level program> → <import form> <top-level body>
<import form> → (import <import spec>*)
<top-level body> → <top-level body form>*
<top-level body form> → <definition> | <expression>
The rules for <top-level program> specify syntax at the form level.

The <import form> is identical to the import clause in libraries (see section 7.1), and specifies a set of libraries to import. A <top-level body> is like a <library body> (see section 7.1), except that definitions and expressions may occur in any order. Thus, the syntax specified by <top-level body form> refers to the result of macro expansion.

When base-library begin, let-syntax, or letrec-syntax forms occur in a top-level body prior to the first expression, they are spliced into the body; see section 11.4.7. Some or all of the body, including portions wrapped in begin, let-syntax, or letrec-syntax forms, may be specified by a syntactic abstraction (see section 9.0.2).

8.2  Top-level program semantics

A top-level program is executed by treating the program similarly to a library, and evaluating its definitions and expressions. The semantics of a top-level body may be roughly explained by a simple translation into a library body: Each <expression> that appears before a definition in the top-level body is converted into a dummy definition (define <variable> (begin <expression> <unspecified>)), where <variable> is a fresh identifier and <unspecified> is a side-effect-free expression returning unspecified values. (It is generally impossible to determine which forms are definitions and expressions without concurrently expanding the body, so the actual translation is somewhat more complicated; see chapter 10.)

On platforms that support it, a top-level program may access its command line by calling the command-line procedure (see library section on “Command-line access and exit values”).