1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-10 05:50:26 +02:00

* api-evaluation.texi (Loading): Document custom reader.

* boot-9.scm (load-module): Support an optional custom reader arg,
implemented by passing on to r4rs's load.

* r4rs.scm (load): Support an optional custom reader arg,
implemented by passing on to primitive-load.

* load.c (the_reader, the_reader_fluid_num): New.
(scm_primitive_load): Support custom reader.
(scm_init_load): Init the_reader and the_reader_fluid_num; export
the_reader as `current-reader'.
This commit is contained in:
Neil Jerram 2005-12-14 00:21:11 +00:00
parent 2824f4dca1
commit ec3a8ace63
7 changed files with 78 additions and 16 deletions

View file

@ -1,3 +1,7 @@
2005-12-14 Neil Jerram <neil@ossau.uklinux.net>
* api-evaluation.texi (Loading): Document custom reader.
2005-12-06 Marius Vollmer <mvo@zagadka.de>
* api-init.texi, api-scheduling.texi, libguile-concepts.texi:

View file

@ -409,12 +409,24 @@ the current module.
@subsection Loading Scheme Code from File
@rnindex load
@deffn {Scheme Procedure} load filename
@deffn {Scheme Procedure} load filename [reader]
Load @var{filename} and evaluate its contents in the top-level
environment. The load paths are not searched. If the variable
@code{%load-hook} is defined, it should be bound to a procedure that
will be called before any code is loaded. See documentation for
@code{%load-hook} later in this section.
environment. The load paths are not searched.
@var{reader} if provided should be either @code{#f}, or a procedure with
the signature @code{(lambda (port) @dots{})} which reads the next
expression from @var{port}. If @var{reader} is @code{#f} or absent,
Guile's built-in @code{read} procedure is used (@pxref{Scheme Read}).
The @var{reader} argument takes effect by setting the value of the
@code{current-reader} fluid (see below) before loading the file, and
restoring its previous value when loading is complete. The Scheme code
inside @var{filename} can itself change the current reader procedure on
the fly by setting @code{current-reader} fluid.
If the variable @code{%load-hook} is defined, it should be bound to a
procedure that will be called before any code is loaded. See
documentation for @code{%load-hook} later in this section.
@end deffn
@deffn {Scheme Procedure} load-from-path filename
@ -452,6 +464,15 @@ in the @code{%load-extensions} list; @code{%search-load-path}
will try each extension automatically.
@end deffn
@defvar current-reader
@code{current-reader} holds the read procedure that is currently being
used by the above loading procedures to read expressions (from the file
that they are loading). @code{current-reader} is a fluid, so it has an
independent value in each dynamic root and should be read and set using
@code{fluid-ref} and @code{fluid-set!} (@pxref{Fluids and Dynamic
States}).
@end defvar
@defvar %load-hook
A procedure to be called @code{(%load-hook @var{filename})} whenever a
file is loaded, or @code{#f} for no such call. @code{%load-hook} is

View file

@ -1,3 +1,11 @@
2005-12-14 Neil Jerram <neil@ossau.uklinux.net>
* boot-9.scm (load-module): Support an optional custom reader arg,
implemented by passing on to r4rs's load.
* r4rs.scm (load): Support an optional custom reader arg,
implemented by passing on to primitive-load.
2005-12-06 Marius Vollmer <mvo@zagadka.de>
From Stephen Compall.

View file

@ -1635,17 +1635,19 @@
(define basic-load load)
(define (load-module filename)
(define (load-module filename . reader)
(save-module-excursion
(lambda ()
(let ((oldname (and (current-load-port)
(port-filename (current-load-port)))))
(basic-load (if (and oldname
(> (string-length filename) 0)
(not (char=? (string-ref filename 0) #\/))
(not (string=? (dirname oldname) ".")))
(string-append (dirname oldname) "/" filename)
filename))))))
(apply basic-load
(if (and oldname
(> (string-length filename) 0)
(not (char=? (string-ref filename 0) #\/))
(not (string=? (dirname oldname) ".")))
(string-append (dirname oldname) "/" filename)
filename)
reader)))))

View file

@ -206,6 +206,8 @@ procedures, their behavior is implementation dependent."
(set! %load-hook %load-announce)
(define (load name)
(start-stack 'load-stack
(primitive-load name)))
(define (load name . reader)
(with-fluid* current-reader (and (pair? reader) (car reader))
(lambda ()
(start-stack 'load-stack
(primitive-load name)))))

View file

@ -1,5 +1,10 @@
2005-12-14 Neil Jerram <neil@ossau.uklinux.net>
* load.c (the_reader, the_reader_fluid_num): New.
(scm_primitive_load): Support custom reader.
(scm_init_load): Init the_reader and the_reader_fluid_num; export
the_reader as `current-reader'.
* scmsigs.c (do_read_without_guile): Use the "raw_data" passed in
(rather than an uninitialized pointer on the stack).

View file

@ -42,6 +42,7 @@
#include "libguile/validate.h"
#include "libguile/load.h"
#include "libguile/fluids.h"
#include <sys/types.h>
#include <sys/stat.h>
@ -61,6 +62,10 @@
Applied to the full name of the file. */
static SCM *scm_loc_load_hook;
/* The current reader (a fluid). */
static SCM the_reader = SCM_BOOL_F;
static size_t the_reader_fluid_num = 0;
SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
(SCM filename),
"Load the file named @var{filename} and evaluate its contents in\n"
@ -88,9 +93,19 @@ SCM_DEFINE (scm_primitive_load, "primitive-load", 1, 0, 0,
while (1)
{
SCM form = scm_read (port);
SCM reader, form;
/* Lookup and use the current reader to read the next
expression. */
reader = SCM_FAST_FLUID_REF (the_reader_fluid_num);
if (reader == SCM_BOOL_F)
form = scm_read (port);
else
form = scm_call_1 (reader, port);
if (SCM_EOF_OBJECT_P (form))
break;
scm_primitive_eval_x (form);
}
@ -501,6 +516,11 @@ scm_init_load ()
scm_nullstr)));
scm_loc_load_hook = SCM_VARIABLE_LOC (scm_c_define ("%load-hook", SCM_BOOL_F));
the_reader = scm_make_fluid ();
the_reader_fluid_num = SCM_FLUID_NUM (the_reader);
SCM_FAST_FLUID_SET_X (the_reader_fluid_num, SCM_BOOL_F);
scm_c_define("current-reader", the_reader);
init_build_info ();
#include "libguile/load.x"