3 @chapter Reading and Evaluating Scheme Code
5 This chapter describes Guile functions that are concerned with reading,
6 loading and evaluating Scheme code at run time.
9 * Scheme Syntax:: Standard and extended Scheme syntax.
10 * Scheme Read:: Reading Scheme code.
11 * Fly Evaluation:: Procedures for on the fly evaluation.
12 * Loading:: Loading Scheme code from file.
13 * Delayed Evaluation:: Postponing evaluation until it is needed.
14 * Local Evaluation:: Evaluation in a local environment.
15 * Evaluator Behaviour:: Modifying Guile's evaluator.
20 @section Scheme Syntax: Standard and Guile Extensions
32 @node Expression Syntax
33 @subsection Expression Syntax
39 @c FIXME::martin: Review me!
41 Comments in Scheme source files are written by starting them with a
42 semicolon character (@code{;}). The comment then reaches up to the end
43 of the line. Comments can begin at any column, and the may be inserted
44 on the same line as Scheme code.
49 (define x 1) ; Comment after expression
53 ;;; Comment at left margin.
57 It is common to use a single semicolon for comments following
58 expressions on a line, to use two semicolons for comments which are
59 indented like code, and three semicolons for comments which start at
60 column 0, even if they are inside an indented code block. This
61 convention is used when indenting code in Emacs' Scheme mode.
65 @subsection Block Comments
67 @c FIXME::martin: Review me!
69 @cindex multiline comments
70 In addition to the standard line comments defined by R5RS, Guile has
71 another comment type for multiline comments, called @dfn{block
72 comments}. This type of comment begins with the character sequence
73 @code{#!} and ends with the characters @code{!#}, which must appear on a
74 line of their own. These comments are compatible with the block
75 comments in the Scheme Shell @file{scsh} (@pxref{The Scheme shell
76 (scsh)}). The characters @code{#!} were chosen because they are the
77 magic characters used in shell scripts for indicating that the name of
78 the program for executing the script follows on the same line.
80 Thus a Guile script often starts like this.
83 #! /usr/local/bin/guile -s
87 More details on Guile scripting can be found in the scripting section
88 (@pxref{Guile Scripting}).
91 @node Case Sensitivity
92 @subsection Case Sensitivity
94 @c FIXME::martin: Review me!
96 Scheme as defined in R5RS is not case sensitive when reading symbols.
97 Guile, on the contrary is case sensitive by default, so the identifiers
104 are the same in R5RS Scheme, but are different in Guile.
106 It is possible to turn off case sensitivity in Guile by setting the
107 reader option @code{case-insensitive}. More on reader options can be
108 found at (@pxref{Reader options}).
111 (read-enable 'case-insensitive)
114 Note that this is seldom a problem, because Scheme programmers tend not
115 to use uppercase letters in their identifiers anyway.
119 @subsection Keyword Syntax
122 @node Reader Extensions
123 @subsection Reader Extensions
125 @deffn {Scheme Procedure} read-hash-extend chr proc
126 @deffnx {C Function} scm_read_hash_extend (chr, proc)
127 Install the procedure @var{proc} for reading expressions
128 starting with the character sequence @code{#} and @var{chr}.
129 @var{proc} will be called with two arguments: the character
130 @var{chr} and the port to read further data from. The object
131 returned will be the return value of @code{read}.
136 @section Reading Scheme Code
139 @deffn {Scheme Procedure} read [port]
140 @deffnx {C Function} scm_read (port)
141 Read an s-expression from the input port @var{port}, or from
142 the current input port if @var{port} is not specified.
143 Any whitespace before the next token is discarded.
146 The behaviour of Guile's Scheme reader can be modified by manipulating
147 its read options. For more information about options, @xref{General
148 option interface}. If you want to know which reader options are
149 available, @xref{Reader options}.
151 @c FIXME::martin: This is taken from libguile/options.c. Is there
152 @c actually a difference between 'help and 'full?
154 @deffn {Scheme Procedure} read-options [setting]
155 Display the current settings of the read options. If @var{setting} is
156 omitted, only a short form of the current read options is printed.
157 Otherwise, @var{setting} should be one of the following symbols:
160 Display the complete option settings.
162 Like @code{help}, but also print programmer options.
166 @deffn {Scheme Procedure} read-enable option-name
167 @deffnx {Scheme Procedure} read-disable option-name
168 @deffnx {Scheme Procedure} read-set! option-name value
169 Modify the read options. @code{read-enable} should be used with boolean
170 options and switches them on, @code{read-disable} switches them off.
171 @code{read-set!} can be used to set an option to a specific value.
174 @deffn {Scheme Procedure} read-options-interface [setting]
175 @deffnx {C Function} scm_read_options (setting)
176 Option interface for the read options. Instead of using
177 this procedure directly, use the procedures @code{read-enable},
178 @code{read-disable}, @code{read-set!} and @var{read-options}.
183 @section Procedures for On the Fly Evaluation
186 @c ARGFIXME environment/environment specifier
187 @deffn {Scheme Procedure} eval exp module
188 @deffnx {C Function} scm_eval (exp, module)
189 Evaluate @var{exp}, a list representing a Scheme expression,
190 in the top-level environment specified by @var{module}.
191 While @var{exp} is evaluated (using @code{primitive-eval}),
192 @var{module} is made the current module. The current module
193 is reset to its previous value when @var{eval} returns.
196 @rnindex interaction-environment
197 @deffn {Scheme Procedure} interaction-environment
198 @deffnx {C Function} scm_interaction_environment ()
199 Return a specifier for the environment that contains
200 implementation--defined bindings, typically a superset of those
201 listed in the report. The intent is that this procedure will
202 return the environment in which the implementation would
203 evaluate expressions dynamically typed by the user.
206 @deffn {Scheme Procedure} eval-string string
207 @deffnx {C Function} scm_eval_string (string)
208 Evaluate @var{string} as the text representation of a Scheme
209 form or forms, and return whatever value they produce.
210 Evaluation takes place in the environment returned by the
211 procedure @code{interaction-environment}.
214 @deffn {Scheme Procedure} apply:nconc2last lst
215 @deffnx {C Function} scm_nconc2last (lst)
216 Given a list (@var{arg1} @dots{} @var{args}), this function
217 conses the @var{arg1} @dots{} arguments onto the front of
218 @var{args}, and returns the resulting list. Note that
219 @var{args} is a list; thus, the argument to this function is
220 a list whose last element is a list.
221 Note: Rather than do new consing, @code{apply:nconc2last}
222 destroys its argument, so use with care.
226 @deffn {Scheme Procedure} apply proc arg1 @dots{} args
227 @var{proc} must be a procedure and @var{args} must be a list. Call
228 @var{proc} with the elements of the list @code{(append (list @var{arg1}
229 @dots{}) @var{args})} as the actual arguments.
232 @deffn {Scheme Procedure} primitive-eval exp
233 @deffnx {C Function} scm_primitive_eval (exp)
234 Evaluate @var{exp} in the top-level environment specified by
240 @section Loading Scheme Code from File
243 @deffn {Scheme Procedure} load filename
244 Load @var{filename} and evaluate its contents in the top-level
245 environment. The load paths are not searched. If the variable
246 @code{%load-hook} is defined, it should be bound to a procedure that
247 will be called before any code is loaded. See documentation for
248 @code{%load-hook} later in this section.
251 @deffn {Scheme Procedure} load-from-path filename
252 Similar to @code{load}, but searches for @var{filename} in the load
256 @deffn {Scheme Procedure} primitive-load filename
257 @deffnx {C Function} scm_primitive_load (filename)
258 Load the file named @var{filename} and evaluate its contents in
259 the top-level environment. The load paths are not searched;
260 @var{filename} must either be a full pathname or be a pathname
261 relative to the current directory. If the variable
262 @code{%load-hook} is defined, it should be bound to a procedure
263 that will be called before any code is loaded. See the
264 documentation for @code{%load-hook} later in this section.
267 @deffn {Scheme Procedure} primitive-load-path filename
268 @deffnx {C Function} scm_primitive_load_path (filename)
269 Search @var{%load-path} for the file named @var{filename} and
270 load it into the top-level environment. If @var{filename} is a
271 relative pathname and is not found in the list of search paths,
272 an error is signalled.
275 @deffn {Scheme Procedure} %search-load-path filename
276 @deffnx {C Function} scm_sys_search_load_path (filename)
277 Search @var{%load-path} for the file named @var{filename},
278 which must be readable by the current user. If @var{filename}
279 is found in the list of paths to search or is an absolute
280 pathname, return its full pathname. Otherwise, return
281 @code{#f}. Filenames may have any of the optional extensions
282 in the @code{%load-extensions} list; @code{%search-load-path}
283 will try each extension automatically.
287 A procedure to be run whenever @code{primitive-load} is called. If this
288 procedure is defined, it will be called with the filename argument that
289 was passed to @code{primitive-load}.
292 (define %load-hook (lambda (file)
295 (write-line "...."))) @result{} undefined
296 (load-from-path "foo.scm")
297 @print{} Loading /usr/local/share/guile/site/foo.scm....
302 @deffn {Scheme Procedure} current-load-port
303 @deffnx {C Function} scm_current_load_port ()
304 Return the current-load-port.
305 The load port is used internally by @code{primitive-load}.
308 @defvar %load-extensions
309 A list of default file extensions for files containing Scheme code.
310 @code{%search-load-path} tries each of these extensions when looking for
311 a file to load. By default, @code{%load-extensions} is bound to the
312 list @code{("" ".scm")}.
316 @node Delayed Evaluation
317 @section Delayed Evaluation
321 @deffn {Scheme Procedure} promise? obj
322 @deffnx {C Function} scm_promise_p (obj)
323 Return true if @var{obj} is a promise, i.e. a delayed computation
324 (@pxref{Delayed evaluation,,,r5rs.info,The Revised^5 Report on Scheme}).
328 @deffn {Scheme Procedure} force x
329 @deffnx {C Function} scm_force (x)
330 If the promise @var{x} has not been computed yet, compute and
331 return @var{x}, otherwise just return the previously computed
336 @node Local Evaluation
337 @section Local Evaluation
341 @deffn {Scheme Procedure} local-eval exp [env]
342 @deffnx {C Function} scm_local_eval (exp, env)
343 Evaluate @var{exp} in its environment. If @var{env} is supplied,
344 it is the environment in which to evaluate @var{exp}. Otherwise,
345 @var{exp} must be a memoized code object (in which case, its environment
350 @node Evaluator Behaviour
351 @section Evaluator Behaviour
353 @c FIXME::martin: Maybe this node name is bad, but the old name clashed with
354 @c `Evaluator options' under `Options and Config'.
356 The behaviour of Guile's evaluator can be modified by manipulating the
357 evaluator options. For more information about options, @xref{General
358 option interface}. If you want to know which evaluator options are
359 available, @xref{Evaluator options}.
361 @c FIXME::martin: This is taken from libguile/options.c. Is there
362 @c actually a difference between 'help and 'full?
364 @deffn {Scheme Procedure} eval-options [setting]
365 Display the current settings of the evaluator options. If @var{setting}
366 is omitted, only a short form of the current evaluator options is
367 printed. Otherwise, @var{setting} should be one of the following
371 Display the complete option settings.
373 Like @code{help}, but also print programmer options.
377 @deffn {Scheme Procedure} eval-enable option-name
378 @deffnx {Scheme Procedure} eval-disable option-name
379 @deffnx {Scheme Procedure} eval-set! option-name value
380 Modify the evaluator options. @code{eval-enable} should be used with boolean
381 options and switches them on, @code{eval-disable} switches them off.
382 @code{eval-set!} can be used to set an option to a specific value.
385 @deffn {Scheme Procedure} eval-options-interface [setting]
386 @deffnx {C Function} scm_eval_options_interface (setting)
387 Option interface for the evaluation options. Instead of using
388 this procedure directly, use the procedures @code{eval-enable},
389 @code{eval-disable}, @code{eval-set!} and @var{eval-options}.
392 @c FIXME::martin: Why aren't these procedure named like the other options
395 @deffn {Scheme Procedure} traps [setting]
396 Display the current settings of the evaluator traps options. If
397 @var{setting} is omitted, only a short form of the current evaluator
398 traps options is printed. Otherwise, @var{setting} should be one of the
402 Display the complete option settings.
404 Like @code{help}, but also print programmer options.
408 @deffn {Scheme Procedure} trap-enable option-name
409 @deffnx {Scheme Procedure} trap-disable option-name
410 @deffnx {Scheme Procedure} trap-set! option-name value
411 Modify the evaluator options. @code{trap-enable} should be used with boolean
412 options and switches them on, @code{trap-disable} switches them off.
413 @code{trap-set!} can be used to set an option to a specific value.
416 @deffn {Scheme Procedure} evaluator-traps-interface [setting]
417 @deffnx {C Function} scm_evaluator_traps (setting)
418 Option interface for the evaluator trap options.
423 @c TeX-master: "guile.texi"