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