diff options
-rw-r--r-- | libguile/ChangeLog | 12 | ||||
-rw-r--r-- | libguile/Makefile.am | 12 | ||||
-rw-r--r-- | libguile/continuations.c | 5 | ||||
-rw-r--r-- | libguile/init.c | 8 | ||||
-rw-r--r-- | libguile/struct.h | 2 | ||||
-rw-r--r-- | libguile/values.c | 154 | ||||
-rw-r--r-- | libguile/values.h | 60 |
7 files changed, 242 insertions, 11 deletions
diff --git a/libguile/ChangeLog b/libguile/ChangeLog index d7d3d8ca4..f3d90c0f5 100644 --- a/libguile/ChangeLog +++ b/libguile/ChangeLog @@ -1,3 +1,15 @@ +2000-11-26 Gary Houston <ghouston@arglist.com> + + * reimplementation of values, call-with-values as primitives: + + * values.c, values.h: new files. use a struct to contain multiple + values, similar to the previous Scheme-level implementation. + * Makefile.am: add values.c, values.h, values.x. + * continuations.c (continuation_apply): support R5RS multiple value + continuations. + * init.c: call scm_init_values. + * struct.h: define SCM_SET_STRUCT_PRINTER. + 2000-11-25 Gary Houston <ghouston@arglist.com> * use an applicable SMOB to represent continuations, instead of a diff --git a/libguile/Makefile.am b/libguile/Makefile.am index fb523e2aa..938ca1794 100644 --- a/libguile/Makefile.am +++ b/libguile/Makefile.am @@ -49,7 +49,7 @@ libguile_la_SOURCES = \ print.c procprop.c procs.c random.c read.c root.c scmsigs.c \ script.c simpos.c smob.c sort.c srcprop.c stackchk.c stacks.c \ stime.c strings.c strop.c strorder.c strports.c struct.c symbols.c \ - tag.c throw.c variable.c vectors.c version.c vports.c weaks.c \ + tag.c throw.c values.c variable.c vectors.c version.c vports.c weaks.c \ gc_os_dep.c properties.c DOT_X_FILES = \ @@ -64,7 +64,7 @@ DOT_X_FILES = \ procs.x random.x read.x root.x scmsigs.x \ script.x simpos.x smob.x socket.x sort.x srcprop.x stackchk.x \ stacks.x stime.x strings.x strop.x strorder.x strports.x struct.x \ - symbols.x tag.x throw.x variable.x vectors.x \ + symbols.x tag.x throw.x values.x variable.x vectors.x \ version.x vports.x weaks.x properties.x EXTRA_DOT_X_FILES = debug-malloc.x filesys.x net_db.x posix.x ramap.x \ @@ -83,8 +83,9 @@ DOT_DOC_FILES = \ procprop.doc procs.doc random.doc read.doc root.doc scmsigs.doc \ script.doc simpos.doc smob.doc socket.doc sort.doc srcprop.doc \ stackchk.doc stacks.doc stime.doc strings.doc strop.doc strorder.doc \ - strports.doc struct.doc symbols.doc tag.doc throw.doc variable.doc \ - vectors.doc version.doc vports.doc weaks.doc properties.doc + strports.doc struct.doc symbols.doc tag.doc throw.doc values.doc \ + variable.doc vectors.doc version.doc vports.doc weaks.doc \ + properties.doc EXTRA_DOT_DOC_FILES = @EXTRA_DOT_DOC_FILES@ @@ -129,7 +130,8 @@ modinclude_HEADERS = \ ramap.h read.h root.h scmsigs.h validate.h script.h simpos.h \ smob.h socket.h sort.h srcprop.h stackchk.h stacks.h stime.h \ strings.h strop.h strorder.h strports.h struct.h symbols.h tag.h \ - tags.h throw.h unif.h variable.h vectors.h version.h vports.h \ + tags.h throw.h unif.h values.h variable.h vectors.h version.h \ + vports.h \ weaks.h snarf.h threads.h coop-defs.h fluids.h iselect.h \ debug-malloc.h properties.h diff --git a/libguile/continuations.c b/libguile/continuations.c index 108465d03..5ef2219f1 100644 --- a/libguile/continuations.c +++ b/libguile/continuations.c @@ -51,6 +51,7 @@ #include "libguile/smob.h" #include "libguile/ports.h" #include "libguile/dynwind.h" +#include "libguile/values.h" #ifdef DEBUG_EXTENSIONS #include "libguile/debug.h" @@ -216,11 +217,9 @@ scm_dynthrow (SCM cont, SCM val) #define FUNC_NAME "continuation_apply" static SCM continuation_apply (SCM cont, SCM args) { - /* FIXME: support R5RS multiple value continuations. */ scm_contregs *continuation = SCM_CONTREGS (cont); scm_contregs *rootcont = SCM_CONTREGS (scm_rootcont); - SCM_ASSERT (scm_ilength (args) == 1, args, SCM_ARGn, FUNC_NAME); if (continuation->seq != rootcont->seq /* this base comparison isn't needed */ || continuation->base != rootcont->base) @@ -231,7 +230,7 @@ static SCM continuation_apply (SCM cont, SCM args) scm_dowinds (continuation->dynenv, scm_ilength (scm_dynwinds) - continuation->dynenv); - scm_dynthrow (cont, SCM_CAR (args)); + scm_dynthrow (cont, scm_values (args)); return SCM_UNSPECIFIED; /* not reached */ } #undef FUNC_NAME diff --git a/libguile/init.c b/libguile/init.c index 16211518a..b4d7467d8 100644 --- a/libguile/init.c +++ b/libguile/init.c @@ -132,6 +132,7 @@ #include "libguile/tag.h" #include "libguile/throw.h" #include "libguile/unif.h" +#include "libguile/values.h" #include "libguile/variable.h" #include "libguile/vectors.h" #include "libguile/version.h" @@ -491,7 +492,7 @@ scm_init_guile_1 (SCM_STACKITEM *base) scm_init_subr_table (); scm_environments_prehistory (); /* create the root environment */ scm_init_continuations (); - scm_init_root (); + scm_init_root (); /* requires continuations */ #ifdef USE_THREADS scm_init_threads (base); #endif @@ -552,11 +553,12 @@ scm_init_guile_1 (SCM_STACKITEM *base) scm_init_srcprop (); #endif scm_init_stackchk (); - scm_init_struct (); /* Requires struct */ - scm_init_stacks (); + scm_init_struct (); + scm_init_stacks (); /* Requires struct */ scm_init_strports (); scm_init_symbols (); scm_init_tag (); + scm_init_values (); /* Requires struct */ scm_init_load (); scm_init_objects (); /* Requires struct */ scm_init_print (); /* Requires struct */ diff --git a/libguile/struct.h b/libguile/struct.h index b78f9a7f9..7c784eb3b 100644 --- a/libguile/struct.h +++ b/libguile/struct.h @@ -87,6 +87,8 @@ typedef scm_sizet (*scm_struct_free_t) (scm_bits_t * vtable, scm_bits_t * data); #define SCM_STRUCT_VTABLE(X) (SCM_PACK (SCM_STRUCT_VTABLE_DATA (X) [scm_vtable_index_vtable])) #define SCM_STRUCT_PRINTER(X) (SCM_PACK (SCM_STRUCT_VTABLE_DATA (X) [scm_vtable_index_printer])) +#define SCM_SET_STRUCT_PRINTER(x, v)\ + (SCM_STRUCT_VTABLE_DATA (x) [scm_vtable_index_printer] = (v)) #define SCM_SET_VTABLE_DESTRUCTOR(X, D) (SCM_STRUCT_DATA (X) [scm_struct_i_free] = (scm_bits_t) (D)) /* Efficiency is important in the following macro, since it's used in GC */ #define SCM_LAYOUT_TAILP(X) (((X) & 32) == 0) /* R, W or O */ diff --git a/libguile/values.c b/libguile/values.c new file mode 100644 index 000000000..6575a147c --- /dev/null +++ b/libguile/values.c @@ -0,0 +1,154 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA + * + * As a special exception, the Free Software Foundation gives permission + * for additional uses of the text contained in its release of GUILE. + * + * The exception is that, if you link the GUILE library with other files + * to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the GUILE library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the + * Free Software Foundation under the name GUILE. If you copy + * code from other Free Software Foundation releases into a copy of + * GUILE, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for GUILE, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. */ + +#include "libguile/_scm.h" +#include "libguile/eval.h" +#include "libguile/feature.h" +#include "libguile/gc.h" +#include "libguile/numbers.h" +#include "libguile/ports.h" +#include "libguile/root.h" +#include "libguile/strings.h" +#include "libguile/struct.h" +#include "libguile/validate.h" + +#include "libguile/values.h" + +static SCM values_vtable; + +#define SCM_VALUESP(x) (SCM_STRUCTP (x)\ + && SCM_EQ_P (scm_struct_vtable (x), values_vtable)) + +static SCM +print_values (SCM obj, SCM pwps) +{ + SCM values = scm_struct_ref (obj, SCM_INUM0); + SCM port = SCM_PORT_WITH_PS_PORT (pwps); + scm_print_state *ps = SCM_PRINT_STATE (SCM_PORT_WITH_PS_PS (pwps)); + + while (SCM_CONSP (values)) + { + scm_iprin1 (SCM_CAR (values), port, ps); + values = SCM_CDR (values); + if (SCM_CONSP (values)) + scm_newline (port); + } + return SCM_UNSPECIFIED; +} + +SCM_DEFINE (scm_values, "values", 0, 0, 1, + (SCM args), + "Delivers all of its arguments to its continuation. Except for\n" + "continuations created by the `call-with-values' procedure, all\n" + "continuations take exactly one value. The effect of passing no\n" + "value or more than one value to continuations that were not\n" + "created by call-with-values is unspecified.") +#define FUNC_NAME s_scm_values +{ + long n; + SCM result; + + SCM_VALIDATE_LIST_COPYLEN (1, args, n); + if (n == 1) + result = SCM_CAR (args); + else + { + result = scm_make_struct (values_vtable, SCM_INUM0, + scm_cons (args, SCM_EOL)); + } + + return result; +} +#undef FUNC_NAME + +SCM_DEFINE (scm_call_with_values, "call-with-values", 2, 0, 0, + (SCM producer, SCM consumer), + "Calls its @var{producer} argument with no values and a\n" + "continuation that, when passed some values, calls the\n" + "@var{consumer} procedure with those values as arguments. The\n" + "continuation for the call to @var{consumer} is the continuation\n" + "of the call to call-with-values.\n\n" + "@example\n" + "(call-with-values (lambda () (values 4 5))\n" + " (lambda (a b) b))\n" + " ==> 5\n\n" + "@end example\n" + "@example\n" + "(call-with-values * -) ==> -1\n" + "@end example") +#define FUNC_NAME s_scm_call_with_values +{ + SCM product; + + SCM_VALIDATE_PROC (1, producer); + SCM_VALIDATE_PROC (2, consumer); + + product = scm_apply (producer, SCM_EOL, SCM_EOL); + if (SCM_VALUESP (product)) + product = scm_struct_ref (product, SCM_INUM0); + else + product = scm_cons (product, SCM_EOL); + return scm_apply (consumer, product, SCM_EOL); +} +#undef FUNC_NAME + +void +scm_init_values (void) +{ + SCM print = scm_make_subr ("%print-values", scm_tc7_subr_2, print_values); + + values_vtable + = scm_permanent_object (scm_make_vtable_vtable (scm_makfrom0str ("pr"), + SCM_INUM0, SCM_EOL)); + SCM_SET_STRUCT_PRINTER (values_vtable, print); + + scm_add_feature ("values"); + +#ifndef SCM_MAGIC_SNARFER +#include "libguile/values.x" +#endif +} + +/* + Local Variables: + c-file-style: "gnu" + End: +*/ diff --git a/libguile/values.h b/libguile/values.h new file mode 100644 index 000000000..79cdcf6fb --- /dev/null +++ b/libguile/values.h @@ -0,0 +1,60 @@ +/* classes: h_files */ + +#ifndef SCM_VALUES_H +#define SCM_VALUES_H +/* Copyright (C) 2000 Free Software Foundation, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307 USA + * + * As a special exception, the Free Software Foundation gives permission + * for additional uses of the text contained in its release of GUILE. + * + * The exception is that, if you link the GUILE library with other files + * to produce an executable, this does not by itself cause the + * resulting executable to be covered by the GNU General Public License. + * Your use of that executable is in no way restricted on account of + * linking the GUILE library code into it. + * + * This exception does not however invalidate any other reasons why + * the executable file might be covered by the GNU General Public License. + * + * This exception applies only to the code released by the + * Free Software Foundation under the name GUILE. If you copy + * code from other Free Software Foundation releases into a copy of + * GUILE, as the General Public License permits, the exception does + * not apply to the code that you add in this way. To avoid misleading + * anyone as to the status of such modified files, you must delete + * this exception notice from them. + * + * If you write modifications of your own for GUILE, it is your choice + * whether to permit this exception to apply to your modifications. + * If you do not wish that, delete this exception notice. */ + + + +#include "libguile/__scm.h" + +extern SCM scm_values (SCM args); +extern SCM scm_call_with_values (SCM producer, SCM consumer); +extern void scm_init_values (void); + +#endif /* SCM_VALUES_H */ + +/* + Local Variables: + c-file-style: "gnu" + End: +*/ |