Commit | Line | Data |
---|---|---|
07d83abe MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
40296bab | 3 | @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006 |
07d83abe MV |
4 | @c Free Software Foundation, Inc. |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
7 | @page | |
00ce5125 | 8 | @node Read/Load/Eval/Compile |
07d83abe MV |
9 | @section Reading and Evaluating Scheme Code |
10 | ||
11 | This chapter describes Guile functions that are concerned with reading, | |
00ce5125 | 12 | loading, evaluating, and compiling Scheme code at run time. |
07d83abe MV |
13 | |
14 | @menu | |
15 | * Scheme Syntax:: Standard and extended Scheme syntax. | |
16 | * Scheme Read:: Reading Scheme code. | |
17 | * Fly Evaluation:: Procedures for on the fly evaluation. | |
00ce5125 | 18 | * Compilation:: How to compile Scheme files and procedures. |
07d83abe MV |
19 | * Loading:: Loading Scheme code from file. |
20 | * Delayed Evaluation:: Postponing evaluation until it is needed. | |
21 | * Local Evaluation:: Evaluation in a local environment. | |
22 | * Evaluator Behaviour:: Modifying Guile's evaluator. | |
00ce5125 | 23 | * VM Behaviour:: Modifying Guile's virtual machine. |
07d83abe MV |
24 | @end menu |
25 | ||
26 | ||
27 | @node Scheme Syntax | |
28 | @subsection Scheme Syntax: Standard and Guile Extensions | |
29 | ||
30 | @menu | |
31 | * Expression Syntax:: | |
32 | * Comments:: | |
33 | * Block Comments:: | |
34 | * Case Sensitivity:: | |
35 | * Keyword Syntax:: | |
36 | * Reader Extensions:: | |
37 | @end menu | |
38 | ||
39 | ||
40 | @node Expression Syntax | |
41 | @subsubsection Expression Syntax | |
42 | ||
43 | An expression to be evaluated takes one of the following forms. | |
44 | ||
45 | @table @nicode | |
46 | ||
47 | @item @var{symbol} | |
48 | A symbol is evaluated by dereferencing. A binding of that symbol is | |
49 | sought and the value there used. For example, | |
50 | ||
51 | @example | |
52 | (define x 123) | |
53 | x @result{} 123 | |
54 | @end example | |
55 | ||
56 | @item (@var{proc} @var{args}@dots{}) | |
57 | A parenthesised expression is a function call. @var{proc} and each | |
58 | argument are evaluated, then the function (which @var{proc} evaluated | |
59 | to) is called with those arguments. | |
60 | ||
61 | The order in which @var{proc} and the arguments are evaluated is | |
62 | unspecified, so be careful when using expressions with side effects. | |
63 | ||
64 | @example | |
65 | (max 1 2 3) @result{} 3 | |
66 | ||
67 | (define (get-some-proc) min) | |
68 | ((get-some-proc) 1 2 3) @result{} 1 | |
69 | @end example | |
70 | ||
71 | The same sort of parenthesised form is used for a macro invocation, | |
72 | but in that case the arguments are not evaluated. See the | |
73 | descriptions of macros for more on this (@pxref{Macros}, and | |
74 | @pxref{Syntax Rules}). | |
75 | ||
76 | @item @var{constant} | |
77 | Number, string, character and boolean constants evaluate ``to | |
78 | themselves'', so can appear as literals. | |
79 | ||
80 | @example | |
81 | 123 @result{} 123 | |
82 | 99.9 @result{} 99.9 | |
83 | "hello" @result{} "hello" | |
84 | #\z @result{} #\z | |
85 | #t @result{} #t | |
86 | @end example | |
87 | ||
88 | Note that an application must not attempt to modify literal strings, | |
89 | since they may be in read-only memory. | |
90 | ||
91 | @item (quote @var{data}) | |
92 | @itemx '@var{data} | |
93 | @findex quote | |
94 | @findex ' | |
95 | Quoting is used to obtain a literal symbol (instead of a variable | |
96 | reference), a literal list (instead of a function call), or a literal | |
97 | vector. @nicode{'} is simply a shorthand for a @code{quote} form. | |
98 | For example, | |
99 | ||
100 | @example | |
101 | 'x @result{} x | |
102 | '(1 2 3) @result{} (1 2 3) | |
103 | '#(1 (2 3) 4) @result{} #(1 (2 3) 4) | |
104 | (quote x) @result{} x | |
105 | (quote (1 2 3)) @result{} (1 2 3) | |
106 | (quote #(1 (2 3) 4)) @result{} #(1 (2 3) 4) | |
107 | @end example | |
108 | ||
109 | Note that an application must not attempt to modify literal lists or | |
110 | vectors obtained from a @code{quote} form, since they may be in | |
111 | read-only memory. | |
112 | ||
113 | @item (quasiquote @var{data}) | |
114 | @itemx `@var{data} | |
115 | @findex quasiquote | |
116 | @findex ` | |
117 | Backquote quasi-quotation is like @code{quote}, but selected | |
118 | sub-expressions are evaluated. This is a convenient way to construct | |
119 | a list or vector structure most of which is constant, but at certain | |
120 | points should have expressions substituted. | |
121 | ||
122 | The same effect can always be had with suitable @code{list}, | |
123 | @code{cons} or @code{vector} calls, but quasi-quoting is often easier. | |
124 | ||
125 | @table @nicode | |
126 | ||
127 | @item (unquote @var{expr}) | |
128 | @itemx ,@var{expr} | |
129 | @findex unquote | |
130 | @findex , | |
131 | Within the quasiquote @var{data}, @code{unquote} or @code{,} indicates | |
132 | an expression to be evaluated and inserted. The comma syntax @code{,} | |
133 | is simply a shorthand for an @code{unquote} form. For example, | |
134 | ||
135 | @example | |
136 | `(1 2 ,(* 9 9) 3 4) @result{} (1 2 81 3 4) | |
137 | `(1 (unquote (+ 1 1)) 3) @result{} (1 2 3) | |
138 | `#(1 ,(/ 12 2)) @result{} #(1 6) | |
139 | @end example | |
140 | ||
141 | @item (unquote-splicing @var{expr}) | |
142 | @itemx ,@@@var{expr} | |
143 | @findex unquote-splicing | |
144 | @findex ,@@ | |
145 | Within the quasiquote @var{data}, @code{unquote-splicing} or | |
146 | @code{,@@} indicates an expression to be evaluated and the elements of | |
147 | the returned list inserted. @var{expr} must evaluate to a list. The | |
148 | ``comma-at'' syntax @code{,@@} is simply a shorthand for an | |
149 | @code{unquote-splicing} form. | |
150 | ||
151 | @example | |
152 | (define x '(2 3)) | |
153 | `(1 ,@@x 4) @result{} (1 2 3 4) | |
154 | `(1 (unquote-splicing (map 1+ x))) @result{} (1 3 4) | |
155 | `#(9 ,@@x 9) @result{} #(9 2 3 9) | |
156 | @end example | |
157 | ||
158 | Notice @code{,@@} differs from plain @code{,} in the way one level of | |
159 | nesting is stripped. For @code{,@@} the elements of a returned list | |
160 | are inserted, whereas with @code{,} it would be the list itself | |
161 | inserted. | |
162 | @end table | |
163 | ||
164 | @c | |
165 | @c FIXME: What can we say about the mutability of a quasiquote | |
166 | @c result? R5RS doesn't seem to specify anything, though where it | |
167 | @c says backquote without commas is the same as plain quote then | |
168 | @c presumably the "fixed" portions of a quasiquote expression must be | |
169 | @c treated as immutable. | |
170 | @c | |
171 | ||
172 | @end table | |
173 | ||
174 | ||
175 | @node Comments | |
176 | @subsubsection Comments | |
177 | ||
178 | @c FIXME::martin: Review me! | |
179 | ||
180 | Comments in Scheme source files are written by starting them with a | |
181 | semicolon character (@code{;}). The comment then reaches up to the end | |
182 | of the line. Comments can begin at any column, and the may be inserted | |
183 | on the same line as Scheme code. | |
184 | ||
185 | @lisp | |
186 | ; Comment | |
187 | ;; Comment too | |
188 | (define x 1) ; Comment after expression | |
189 | (let ((y 1)) | |
190 | ;; Display something. | |
191 | (display y) | |
192 | ;;; Comment at left margin. | |
193 | (display (+ y 1))) | |
194 | @end lisp | |
195 | ||
196 | It is common to use a single semicolon for comments following | |
197 | expressions on a line, to use two semicolons for comments which are | |
198 | indented like code, and three semicolons for comments which start at | |
199 | column 0, even if they are inside an indented code block. This | |
200 | convention is used when indenting code in Emacs' Scheme mode. | |
201 | ||
202 | ||
203 | @node Block Comments | |
204 | @subsubsection Block Comments | |
456f797b KR |
205 | @cindex multiline comments |
206 | @cindex block comments | |
207 | @cindex #! | |
208 | @cindex !# | |
07d83abe MV |
209 | |
210 | @c FIXME::martin: Review me! | |
211 | ||
07d83abe MV |
212 | In addition to the standard line comments defined by R5RS, Guile has |
213 | another comment type for multiline comments, called @dfn{block | |
214 | comments}. This type of comment begins with the character sequence | |
215 | @code{#!} and ends with the characters @code{!#}, which must appear on a | |
216 | line of their own. These comments are compatible with the block | |
217 | comments in the Scheme Shell @file{scsh} (@pxref{The Scheme shell | |
218 | (scsh)}). The characters @code{#!} were chosen because they are the | |
219 | magic characters used in shell scripts for indicating that the name of | |
220 | the program for executing the script follows on the same line. | |
221 | ||
222 | Thus a Guile script often starts like this. | |
223 | ||
224 | @lisp | |
225 | #! /usr/local/bin/guile -s | |
226 | !# | |
227 | @end lisp | |
228 | ||
229 | More details on Guile scripting can be found in the scripting section | |
230 | (@pxref{Guile Scripting}). | |
231 | ||
232 | ||
233 | @node Case Sensitivity | |
234 | @subsubsection Case Sensitivity | |
235 | ||
236 | @c FIXME::martin: Review me! | |
237 | ||
238 | Scheme as defined in R5RS is not case sensitive when reading symbols. | |
239 | Guile, on the contrary is case sensitive by default, so the identifiers | |
240 | ||
241 | @lisp | |
242 | guile-whuzzy | |
243 | Guile-Whuzzy | |
244 | @end lisp | |
245 | ||
246 | are the same in R5RS Scheme, but are different in Guile. | |
247 | ||
248 | It is possible to turn off case sensitivity in Guile by setting the | |
249 | reader option @code{case-insensitive}. More on reader options can be | |
250 | found at (@pxref{Reader options}). | |
251 | ||
252 | @lisp | |
253 | (read-enable 'case-insensitive) | |
254 | @end lisp | |
255 | ||
256 | Note that this is seldom a problem, because Scheme programmers tend not | |
257 | to use uppercase letters in their identifiers anyway. | |
258 | ||
259 | ||
260 | @node Keyword Syntax | |
261 | @subsubsection Keyword Syntax | |
262 | ||
263 | ||
264 | @node Reader Extensions | |
265 | @subsubsection Reader Extensions | |
266 | ||
267 | @deffn {Scheme Procedure} read-hash-extend chr proc | |
268 | @deffnx {C Function} scm_read_hash_extend (chr, proc) | |
269 | Install the procedure @var{proc} for reading expressions | |
270 | starting with the character sequence @code{#} and @var{chr}. | |
271 | @var{proc} will be called with two arguments: the character | |
272 | @var{chr} and the port to read further data from. The object | |
273 | returned will be the return value of @code{read}. | |
274 | @end deffn | |
275 | ||
276 | ||
277 | @node Scheme Read | |
278 | @subsection Reading Scheme Code | |
279 | ||
280 | @rnindex read | |
281 | @deffn {Scheme Procedure} read [port] | |
282 | @deffnx {C Function} scm_read (port) | |
283 | Read an s-expression from the input port @var{port}, or from | |
284 | the current input port if @var{port} is not specified. | |
285 | Any whitespace before the next token is discarded. | |
286 | @end deffn | |
287 | ||
288 | The behaviour of Guile's Scheme reader can be modified by manipulating | |
289 | its read options. For more information about options, @xref{User level | |
290 | options interfaces}. If you want to know which reader options are | |
291 | available, @xref{Reader options}. | |
292 | ||
293 | @c FIXME::martin: This is taken from libguile/options.c. Is there | |
294 | @c actually a difference between 'help and 'full? | |
295 | ||
296 | @deffn {Scheme Procedure} read-options [setting] | |
297 | Display the current settings of the read options. If @var{setting} is | |
298 | omitted, only a short form of the current read options is printed. | |
299 | Otherwise, @var{setting} should be one of the following symbols: | |
300 | @table @code | |
301 | @item help | |
302 | Display the complete option settings. | |
303 | @item full | |
304 | Like @code{help}, but also print programmer options. | |
305 | @end table | |
306 | @end deffn | |
307 | ||
308 | @deffn {Scheme Procedure} read-enable option-name | |
309 | @deffnx {Scheme Procedure} read-disable option-name | |
310 | @deffnx {Scheme Procedure} read-set! option-name value | |
311 | Modify the read options. @code{read-enable} should be used with boolean | |
312 | options and switches them on, @code{read-disable} switches them off. | |
313 | @code{read-set!} can be used to set an option to a specific value. | |
314 | @end deffn | |
315 | ||
316 | @deffn {Scheme Procedure} read-options-interface [setting] | |
317 | @deffnx {C Function} scm_read_options (setting) | |
318 | Option interface for the read options. Instead of using | |
319 | this procedure directly, use the procedures @code{read-enable}, | |
320 | @code{read-disable}, @code{read-set!} and @code{read-options}. | |
321 | @end deffn | |
322 | ||
323 | ||
324 | @node Fly Evaluation | |
325 | @subsection Procedures for On the Fly Evaluation | |
326 | ||
327 | @xref{Environments}. | |
328 | ||
329 | @rnindex eval | |
330 | @c ARGFIXME environment/environment specifier | |
b4fddbbe MV |
331 | @deffn {Scheme Procedure} eval exp module_or_state |
332 | @deffnx {C Function} scm_eval (exp, module_or_state) | |
07d83abe MV |
333 | Evaluate @var{exp}, a list representing a Scheme expression, |
334 | in the top-level environment specified by @var{module}. | |
335 | While @var{exp} is evaluated (using @code{primitive-eval}), | |
336 | @var{module} is made the current module. The current module | |
337 | is reset to its previous value when @var{eval} returns. | |
b4fddbbe MV |
338 | XXX - dynamic states. |
339 | Example: (eval '(+ 1 2) (interaction-environment)) | |
07d83abe MV |
340 | @end deffn |
341 | ||
342 | @rnindex interaction-environment | |
343 | @deffn {Scheme Procedure} interaction-environment | |
344 | @deffnx {C Function} scm_interaction_environment () | |
345 | Return a specifier for the environment that contains | |
346 | implementation--defined bindings, typically a superset of those | |
347 | listed in the report. The intent is that this procedure will | |
348 | return the environment in which the implementation would | |
349 | evaluate expressions dynamically typed by the user. | |
350 | @end deffn | |
351 | ||
352 | @deffn {Scheme Procedure} eval-string string [module] | |
353 | @deffnx {C Function} scm_eval_string (string) | |
354 | @deffnx {C Function} scm_eval_string_in_module (string, module) | |
355 | Evaluate @var{string} as the text representation of a Scheme form or | |
356 | forms, and return whatever value they produce. Evaluation takes place | |
357 | in the given module, or in the current module when no module is given. | |
358 | While the code is evaluated, the given module is made the current one. | |
359 | The current module is restored when this procedure returns. | |
360 | @end deffn | |
361 | ||
40296bab KR |
362 | @deftypefn {C Function} SCM scm_c_eval_string (const char *string) |
363 | @code{scm_eval_string}, but taking a C string instead of an | |
364 | @code{SCM}. | |
365 | @end deftypefn | |
366 | ||
07d83abe MV |
367 | @deffn {Scheme Procedure} apply proc arg1 @dots{} argN arglst |
368 | @deffnx {C Function} scm_apply_0 (proc, arglst) | |
369 | @deffnx {C Function} scm_apply_1 (proc, arg1, arglst) | |
370 | @deffnx {C Function} scm_apply_2 (proc, arg1, arg2, arglst) | |
371 | @deffnx {C Function} scm_apply_3 (proc, arg1, arg2, arg3, arglst) | |
372 | @deffnx {C Function} scm_apply (proc, arg, rest) | |
373 | @rnindex apply | |
374 | Call @var{proc} with arguments @var{arg1} @dots{} @var{argN} plus the | |
375 | elements of the @var{arglst} list. | |
376 | ||
377 | @code{scm_apply} takes parameters corresponding to a Scheme level | |
378 | @code{(lambda (proc arg . rest) ...)}. So @var{arg} and all but the | |
379 | last element of the @var{rest} list make up | |
380 | @var{arg1}@dots{}@var{argN} and the last element of @var{rest} is the | |
381 | @var{arglst} list. Or if @var{rest} is the empty list @code{SCM_EOL} | |
382 | then there's no @var{arg1}@dots{}@var{argN} and @var{arg} is the | |
383 | @var{arglst}. | |
384 | ||
385 | @var{arglst} is not modified, but the @var{rest} list passed to | |
386 | @code{scm_apply} is modified. | |
387 | @end deffn | |
388 | ||
389 | @deffn {C Function} scm_call_0 (proc) | |
390 | @deffnx {C Function} scm_call_1 (proc, arg1) | |
391 | @deffnx {C Function} scm_call_2 (proc, arg1, arg2) | |
392 | @deffnx {C Function} scm_call_3 (proc, arg1, arg2, arg3) | |
8d596b11 | 393 | @deffnx {C Function} scm_call_4 (proc, arg1, arg2, arg3, arg4) |
07d83abe MV |
394 | Call @var{proc} with the given arguments. |
395 | @end deffn | |
396 | ||
397 | @deffn {Scheme Procedure} apply:nconc2last lst | |
398 | @deffnx {C Function} scm_nconc2last (lst) | |
399 | @var{lst} should be a list (@var{arg1} @dots{} @var{argN} | |
400 | @var{arglst}), with @var{arglst} being a list. This function returns | |
401 | a list comprising @var{arg1} to @var{argN} plus the elements of | |
402 | @var{arglst}. @var{lst} is modified to form the return. @var{arglst} | |
403 | is not modified, though the return does share structure with it. | |
404 | ||
405 | This operation collects up the arguments from a list which is | |
406 | @code{apply} style parameters. | |
407 | @end deffn | |
408 | ||
409 | @deffn {Scheme Procedure} primitive-eval exp | |
410 | @deffnx {C Function} scm_primitive_eval (exp) | |
411 | Evaluate @var{exp} in the top-level environment specified by | |
412 | the current module. | |
413 | @end deffn | |
414 | ||
415 | ||
00ce5125 AW |
416 | @node Compilation |
417 | @subsection Compiling Scheme Code | |
418 | ||
419 | The @code{eval} procedure directly interprets the S-expression | |
420 | representation of Scheme. An alternate strategy for evaluation is to | |
421 | determine ahead of time what computations will be necessary to | |
422 | evaluate the expression, and then use that recipe to produce the | |
423 | desired results. This is known as @dfn{compilation}. | |
424 | ||
425 | While it is possible to compile simple Scheme expressions such as | |
426 | @code{(+ 2 2)} or even @code{"Hello world!"}, compilation is most | |
ca445ba5 | 427 | interesting in the context of procedures. Compiling a lambda expression |
00ce5125 AW |
428 | produces a compiled procedure, which is just like a normal procedure |
429 | except typically much faster, because it can bypass the generic | |
430 | interpreter. | |
431 | ||
432 | Functions from system modules in a Guile installation are normally | |
433 | compiled already, so they load and run quickly. | |
434 | ||
435 | Note that well-written Scheme programs will not typically call the | |
436 | procedures in this section, for the same reason that it is often bad | |
437 | taste to use @code{eval}. The normal interface to the compiler is the | |
438 | command-line file compiler, which can be invoked from the shell as | |
439 | @code{guile-tools compile @var{foo.scm}}. This interface needs more | |
440 | documentation. | |
441 | ||
442 | (Why are calls to @code{eval} and @code{compile} usually in bad taste? | |
443 | Because they are limited, in that they can only really make sense for | |
444 | top-level expressions. Also, most needs for ``compile-time'' | |
445 | computation are fulfilled by macros and closures. Of course one good | |
446 | counterexample is the REPL itself, or any code that reads expressions | |
447 | from a port.) | |
448 | ||
ca445ba5 AW |
449 | For more information on the compiler itself, see @ref{Compiling to the |
450 | Virtual Machine}. For information on the virtual machine, see @ref{A | |
00ce5125 AW |
451 | Virtual Machine for Guile}. |
452 | ||
453 | @deffn {Scheme Procedure} compile exp [env=#f] [from=(current-language)] [to=value] [opts=()] | |
454 | Compile the expression @var{exp} in the environment @var{env}. If | |
455 | @var{exp} is a procedure, the result will be a compiled procedure; | |
456 | otherwise @code{compile} is mostly equivalent to @code{eval}. | |
457 | ||
458 | For a discussion of languages and compiler options, @xref{Compiling to | |
459 | the Virtual Machine}. | |
460 | @end deffn | |
461 | ||
462 | @deffn {Scheme Procedure} compile-file file [to=objcode] [opts='()] | |
463 | Compile the file named @var{file}. | |
464 | ||
465 | Output will be written to a file in the current directory whose name | |
466 | is computed as @code{(compiled-file-name @var{file})}. | |
467 | @end deffn | |
468 | ||
469 | @deffn {Scheme Procedure} compiled-file-name file | |
470 | Compute an appropriate name for a compiled version of a Scheme file | |
471 | named @var{file}. | |
472 | ||
473 | Usually, the result will be the original file name with the | |
474 | @code{.scm} suffix replaced with @code{.go}, but the exact behavior | |
475 | depends on the contents of the @code{%load-extensions} and | |
476 | @code{%load-compiled-extensions} lists. | |
477 | @end deffn | |
478 | ||
07d83abe MV |
479 | @node Loading |
480 | @subsection Loading Scheme Code from File | |
481 | ||
482 | @rnindex load | |
ec3a8ace | 483 | @deffn {Scheme Procedure} load filename [reader] |
07d83abe | 484 | Load @var{filename} and evaluate its contents in the top-level |
ec3a8ace NJ |
485 | environment. The load paths are not searched. |
486 | ||
487 | @var{reader} if provided should be either @code{#f}, or a procedure with | |
488 | the signature @code{(lambda (port) @dots{})} which reads the next | |
489 | expression from @var{port}. If @var{reader} is @code{#f} or absent, | |
490 | Guile's built-in @code{read} procedure is used (@pxref{Scheme Read}). | |
491 | ||
492 | The @var{reader} argument takes effect by setting the value of the | |
493 | @code{current-reader} fluid (see below) before loading the file, and | |
494 | restoring its previous value when loading is complete. The Scheme code | |
495 | inside @var{filename} can itself change the current reader procedure on | |
496 | the fly by setting @code{current-reader} fluid. | |
497 | ||
498 | If the variable @code{%load-hook} is defined, it should be bound to a | |
499 | procedure that will be called before any code is loaded. See | |
500 | documentation for @code{%load-hook} later in this section. | |
07d83abe MV |
501 | @end deffn |
502 | ||
00ce5125 AW |
503 | @deffn {Scheme Procedure} load-compiled filename |
504 | Load the compiled file named @var{filename}. The load paths are not | |
505 | searched. | |
506 | ||
507 | Compiling a source file (@pxref{Read/Load/Eval/Compile}) and then | |
508 | calling @code{load-compiled} on the resulting file is equivalent to | |
509 | calling @code{load} on the source file. | |
510 | @end deffn | |
511 | ||
07d83abe MV |
512 | @deffn {Scheme Procedure} load-from-path filename |
513 | Similar to @code{load}, but searches for @var{filename} in the load | |
00ce5125 AW |
514 | paths. Preferentially loads a compiled version of the file, if it is |
515 | available and up-to-date. | |
07d83abe MV |
516 | @end deffn |
517 | ||
518 | @deffn {Scheme Procedure} primitive-load filename | |
519 | @deffnx {C Function} scm_primitive_load (filename) | |
520 | Load the file named @var{filename} and evaluate its contents in | |
521 | the top-level environment. The load paths are not searched; | |
522 | @var{filename} must either be a full pathname or be a pathname | |
523 | relative to the current directory. If the variable | |
524 | @code{%load-hook} is defined, it should be bound to a procedure | |
525 | that will be called before any code is loaded. See the | |
526 | documentation for @code{%load-hook} later in this section. | |
527 | @end deffn | |
528 | ||
40296bab KR |
529 | @deftypefn {C Function} SCM scm_c_primitive_load (const char *filename) |
530 | @code{scm_primitive_load}, but taking a C string instead of an | |
531 | @code{SCM}. | |
532 | @end deftypefn | |
533 | ||
07d83abe MV |
534 | @deffn {Scheme Procedure} primitive-load-path filename |
535 | @deffnx {C Function} scm_primitive_load_path (filename) | |
5c132e68 | 536 | Search @code{%load-path} for the file named @var{filename} and |
07d83abe MV |
537 | load it into the top-level environment. If @var{filename} is a |
538 | relative pathname and is not found in the list of search paths, | |
00ce5125 AW |
539 | an error is signalled. Preferentially loads a compiled version of the |
540 | file, if it is available and up-to-date. | |
07d83abe MV |
541 | @end deffn |
542 | ||
543 | @deffn {Scheme Procedure} %search-load-path filename | |
544 | @deffnx {C Function} scm_sys_search_load_path (filename) | |
5c132e68 | 545 | Search @code{%load-path} for the file named @var{filename}, |
07d83abe MV |
546 | which must be readable by the current user. If @var{filename} |
547 | is found in the list of paths to search or is an absolute | |
548 | pathname, return its full pathname. Otherwise, return | |
549 | @code{#f}. Filenames may have any of the optional extensions | |
550 | in the @code{%load-extensions} list; @code{%search-load-path} | |
551 | will try each extension automatically. | |
552 | @end deffn | |
553 | ||
ec3a8ace NJ |
554 | @defvar current-reader |
555 | @code{current-reader} holds the read procedure that is currently being | |
556 | used by the above loading procedures to read expressions (from the file | |
557 | that they are loading). @code{current-reader} is a fluid, so it has an | |
558 | independent value in each dynamic root and should be read and set using | |
559 | @code{fluid-ref} and @code{fluid-set!} (@pxref{Fluids and Dynamic | |
560 | States}). | |
561 | @end defvar | |
562 | ||
07d83abe | 563 | @defvar %load-hook |
42ad91f7 KR |
564 | A procedure to be called @code{(%load-hook @var{filename})} whenever a |
565 | file is loaded, or @code{#f} for no such call. @code{%load-hook} is | |
566 | used by all of the above loading functions (@code{load}, | |
567 | @code{load-path}, @code{primitive-load} and | |
568 | @code{primitive-load-path}). | |
569 | ||
570 | For example an application can set this to show what's loaded, | |
07d83abe MV |
571 | |
572 | @example | |
42ad91f7 KR |
573 | (set! %load-hook (lambda (filename) |
574 | (format #t "Loading ~a ...\n" filename))) | |
07d83abe | 575 | (load-from-path "foo.scm") |
42ad91f7 | 576 | @print{} Loading /usr/local/share/guile/site/foo.scm ... |
07d83abe | 577 | @end example |
07d83abe MV |
578 | @end defvar |
579 | ||
580 | @deffn {Scheme Procedure} current-load-port | |
581 | @deffnx {C Function} scm_current_load_port () | |
582 | Return the current-load-port. | |
583 | The load port is used internally by @code{primitive-load}. | |
584 | @end deffn | |
585 | ||
586 | @defvar %load-extensions | |
587 | A list of default file extensions for files containing Scheme code. | |
588 | @code{%search-load-path} tries each of these extensions when looking for | |
589 | a file to load. By default, @code{%load-extensions} is bound to the | |
590 | list @code{("" ".scm")}. | |
591 | @end defvar | |
592 | ||
593 | ||
594 | @node Delayed Evaluation | |
595 | @subsection Delayed Evaluation | |
596 | @cindex delayed evaluation | |
597 | @cindex promises | |
598 | ||
599 | Promises are a convenient way to defer a calculation until its result | |
600 | is actually needed, and to run such a calculation only once. | |
601 | ||
602 | @deffn syntax delay expr | |
603 | @rnindex delay | |
604 | Return a promise object which holds the given @var{expr} expression, | |
605 | ready to be evaluated by a later @code{force}. | |
606 | @end deffn | |
607 | ||
608 | @deffn {Scheme Procedure} promise? obj | |
609 | @deffnx {C Function} scm_promise_p (obj) | |
610 | Return true if @var{obj} is a promise. | |
611 | @end deffn | |
612 | ||
613 | @rnindex force | |
614 | @deffn {Scheme Procedure} force p | |
615 | @deffnx {C Function} scm_force (p) | |
616 | Return the value obtained from evaluating the @var{expr} in the given | |
617 | promise @var{p}. If @var{p} has previously been forced then its | |
618 | @var{expr} is not evaluated again, instead the value obtained at that | |
619 | time is simply returned. | |
620 | ||
621 | During a @code{force}, an @var{expr} can call @code{force} again on | |
622 | its own promise, resulting in a recursive evaluation of that | |
623 | @var{expr}. The first evaluation to return gives the value for the | |
624 | promise. Higher evaluations run to completion in the normal way, but | |
625 | their results are ignored, @code{force} always returns the first | |
626 | value. | |
627 | @end deffn | |
628 | ||
629 | ||
630 | @node Local Evaluation | |
631 | @subsection Local Evaluation | |
632 | ||
633 | [the-environment] | |
634 | ||
635 | @deffn {Scheme Procedure} local-eval exp [env] | |
636 | @deffnx {C Function} scm_local_eval (exp, env) | |
637 | Evaluate @var{exp} in its environment. If @var{env} is supplied, | |
638 | it is the environment in which to evaluate @var{exp}. Otherwise, | |
639 | @var{exp} must be a memoized code object (in which case, its environment | |
640 | is implicit). | |
641 | @end deffn | |
642 | ||
643 | ||
644 | @node Evaluator Behaviour | |
645 | @subsection Evaluator Behaviour | |
646 | ||
647 | @c FIXME::martin: Maybe this node name is bad, but the old name clashed with | |
648 | @c `Evaluator options' under `Options and Config'. | |
649 | ||
650 | The behaviour of Guile's evaluator can be modified by manipulating the | |
651 | evaluator options. For more information about options, @xref{User level | |
652 | options interfaces}. If you want to know which evaluator options are | |
653 | available, @xref{Evaluator options}. | |
654 | ||
655 | @c FIXME::martin: This is taken from libguile/options.c. Is there | |
656 | @c actually a difference between 'help and 'full? | |
657 | ||
658 | @deffn {Scheme Procedure} eval-options [setting] | |
659 | Display the current settings of the evaluator options. If @var{setting} | |
660 | is omitted, only a short form of the current evaluator options is | |
661 | printed. Otherwise, @var{setting} should be one of the following | |
662 | symbols: | |
663 | @table @code | |
664 | @item help | |
665 | Display the complete option settings. | |
666 | @item full | |
667 | Like @code{help}, but also print programmer options. | |
668 | @end table | |
669 | @end deffn | |
670 | ||
671 | @deffn {Scheme Procedure} eval-enable option-name | |
672 | @deffnx {Scheme Procedure} eval-disable option-name | |
673 | @deffnx {Scheme Procedure} eval-set! option-name value | |
674 | Modify the evaluator options. @code{eval-enable} should be used with boolean | |
675 | options and switches them on, @code{eval-disable} switches them off. | |
676 | @code{eval-set!} can be used to set an option to a specific value. | |
677 | @end deffn | |
678 | ||
679 | @deffn {Scheme Procedure} eval-options-interface [setting] | |
680 | @deffnx {C Function} scm_eval_options_interface (setting) | |
681 | Option interface for the evaluation options. Instead of using | |
682 | this procedure directly, use the procedures @code{eval-enable}, | |
683 | @code{eval-disable}, @code{eval-set!} and @code{eval-options}. | |
684 | @end deffn | |
685 | ||
686 | @c FIXME::martin: Why aren't these procedure named like the other options | |
687 | @c procedures? | |
688 | ||
689 | @deffn {Scheme Procedure} traps [setting] | |
690 | Display the current settings of the evaluator traps options. If | |
691 | @var{setting} is omitted, only a short form of the current evaluator | |
692 | traps options is printed. Otherwise, @var{setting} should be one of the | |
693 | following symbols: | |
694 | @table @code | |
695 | @item help | |
696 | Display the complete option settings. | |
697 | @item full | |
698 | Like @code{help}, but also print programmer options. | |
699 | @end table | |
700 | @end deffn | |
701 | ||
702 | @deffn {Scheme Procedure} trap-enable option-name | |
703 | @deffnx {Scheme Procedure} trap-disable option-name | |
704 | @deffnx {Scheme Procedure} trap-set! option-name value | |
705 | Modify the evaluator options. @code{trap-enable} should be used with boolean | |
706 | options and switches them on, @code{trap-disable} switches them off. | |
707 | @code{trap-set!} can be used to set an option to a specific value. | |
19ab431e HWN |
708 | |
709 | See @ref{Evaluator trap options} for more information on the available | |
710 | trap handlers. | |
07d83abe MV |
711 | @end deffn |
712 | ||
713 | @deffn {Scheme Procedure} evaluator-traps-interface [setting] | |
714 | @deffnx {C Function} scm_evaluator_traps (setting) | |
715 | Option interface for the evaluator trap options. | |
716 | @end deffn | |
717 | ||
00ce5125 AW |
718 | @node VM Behaviour |
719 | @subsection VM Behaviour | |
720 | ||
721 | Like the procedures from the previous section that operate on the | |
722 | evaluator, there are also procedures to modify the behavior of a | |
723 | virtual machine. | |
724 | ||
725 | The most useful thing that a user can do is to add to one of the | |
726 | virtual machine's predefined hooks: | |
727 | ||
728 | @deffn {Scheme Procedure} vm-next-hook vm | |
729 | @deffnx {Scheme Procedure} vm-apply-hook vm | |
730 | @deffnx {Scheme Procedure} vm-boot-hook vm | |
731 | @deffnx {Scheme Procedure} vm-return-hook vm | |
732 | @deffnx {Scheme Procedure} vm-break-hook vm | |
733 | @deffnx {Scheme Procedure} vm-exit-hook vm | |
734 | @deffnx {Scheme Procedure} vm-halt-hook vm | |
735 | @deffnx {Scheme Procedure} vm-enter-hook vm | |
736 | Accessors to a virtual machine's hooks. Usually you pass | |
737 | @code{(the-vm)} as the @var{vm}. | |
738 | @end deffn | |
739 | ||
740 | @xref{A Virtual Machine for Guile}, for more information on Guile's | |
741 | virtual machine. | |
07d83abe MV |
742 | |
743 | @c Local Variables: | |
744 | @c TeX-master: "guile.texi" | |
745 | @c End: |