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