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

* Added Jost Boekemeier's implementation of environments to guile.

* Added a test suite for (up to now only) leaf environments.
This commit is contained in:
Dirk Herrmann 2000-08-25 17:01:32 +00:00
parent 167d89cff9
commit 5d3e2388f3
7 changed files with 2903 additions and 4 deletions

View file

@ -1,3 +1,14 @@
2000-08-25 Dirk Herrmann <D.Herrmann@tu-bs.de>
* Makefile.am: Added all necessary environments.* files.
* init.c: Include environments.h.
(scm_boot_guile_1): Initialize the environments.
* environments.[ch]: Added. Most of the credit for these files
goes to Jost Boekemeier.
2000-08-25 Mikael Djurfeldt <mdj@linnaeus.mit.edu>
* procprop.c: #include "libguile/smob.h"; handle applicable smobs.

View file

@ -40,7 +40,8 @@ guile_LDFLAGS = @DLPREOPEN@
libguile_la_SOURCES = \
alist.c arbiters.c async.c backtrace.c boolean.c chars.c \
continuations.c debug.c dynl.c dynwind.c eq.c error.c eval.c \
continuations.c debug.c dynl.c dynwind.c environments.c eq.c \
error.c eval.c \
evalext.c feature.c fluids.c fports.c gc.c gdbint.c gh_data.c \
gh_eval.c gh_funcs.c gh_init.c gh_io.c gh_list.c gh_predicates.c \
gsubr.c guardians.c hash.c hashtab.c hooks.c init.c ioext.c \
@ -54,7 +55,8 @@ libguile_la_SOURCES = \
DOT_X_FILES = \
alist.x arbiters.x \
async.x backtrace.x boolean.x chars.x continuations.x debug.x \
dynl.x dynwind.x eq.x error.x eval.x evalext.x feature.x \
dynl.x dynwind.x environments.x eq.x error.x eval.x evalext.x \
feature.x \
fluids.x fports.x gc.x gsubr.x guardians.x hash.x hashtab.x \
hooks.x init.x ioext.x iselect.x keywords.x lang.x list.x load.x \
macros.x mallocs.x modules.x net_db.x numbers.x objects.x \
@ -70,7 +72,8 @@ EXTRA_DOT_X_FILES = debug-malloc.x filesys.x net_db.x posix.x ramap.x \
DOT_DOC_FILES = \
alist.doc arbiters.doc async.doc backtrace.doc boolean.doc \
chars.doc continuations.doc debug.doc dynl.doc dynwind.doc eq.doc \
chars.doc continuations.doc debug.doc dynl.doc dynwind.doc \
environments.doc eq.doc \
error.doc eval.doc evalext.doc feature.doc fluids.doc fports.doc \
gc.doc gsubr.doc guardians.doc hash.doc hashtab.doc hooks.doc init.doc \
ioext.doc iselect.doc keywords.doc lang.doc list.doc load.doc \
@ -115,7 +118,8 @@ pkginclude_HEADERS = gh.h
modincludedir = $(includedir)/libguile
modinclude_HEADERS = \
__scm.h alist.h arbiters.h async.h backtrace.h boolean.h chars.h \
continuations.h debug.h dynl.h dynwind.h eq.h error.h eval.h \
continuations.h debug.h dynl.h dynwind.h environments.h eq.h \
error.h eval.h \
evalext.h feature.h filesys.h fports.h gc.h gdb_interface.h \
gdbint.h gsubr.h guardians.h hash.h hashtab.h hooks.h init.h \
ioext.h keywords.h lang.h list.h load.h macros.h mallocs.h \

2305
libguile/environments.c Normal file

File diff suppressed because it is too large Load diff

208
libguile/environments.h Normal file
View file

@ -0,0 +1,208 @@
/* classes: h_files */
#ifndef ENVIRONMENTS_H
#define ENVIRONMENTS_H
/* Copyright (C) 1999, 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"
/* The type for folding functions written in C. A function meant to be passed
* to scm_c_environment_fold should have the type scm_environment_folder.
*/
typedef SCM (*scm_environment_folder) (SCM data, SCM sym, SCM val, SCM tail);
/* The type for observer functions written in C. A function meant to be
* passed to scm_c_environment_observe should have the type
* scm_environment_observer.
*/
typedef void (*scm_environment_observer) (SCM env, SCM data);
struct scm_environment_funcs {
SCM (*ref) (SCM self, SCM symbol);
SCM (*fold) (SCM self, scm_environment_folder proc, SCM data, SCM init);
SCM (*define) (SCM self, SCM symbol, SCM value);
SCM (*undefine) (SCM self, SCM symbol);
SCM (*set) (SCM self, SCM symbol, SCM value);
SCM (*cell) (SCM self, SCM symbol, int for_write);
SCM (*observe) (SCM self, scm_environment_observer proc, SCM data, int weak_p);
void (*unobserve) (SCM self, SCM token);
SCM (*mark) (SCM self);
scm_sizet (*free) (SCM self);
int (*print) (SCM self, SCM port, scm_print_state *pstate);
};
#define SCM_ENVIRONMENT_SUCCESS SCM_BOOL_T
#define SCM_ENVIRONMENT_BINDING_IMMUTABLE SCM_MAKINUM (0)
#define SCM_ENVIRONMENT_LOCATION_IMMUTABLE SCM_MAKINUM (1)
#define SCM_ENVIRONMENT_LOCATION_NO_CELL SCM_BOOL_F
extern long scm_tc16_environment;
#define SCM_ENVIRONMENT_P(x) \
(!SCM_IMP (x) && SCM_CELL_TYPE (x) == scm_tc16_environment)
#define SCM_ENVIRONMENT_FUNCS(env) \
(*((struct scm_environment_funcs **) SCM_CELL_WORD_1 (env)))
#define SCM_ENVIRONMENT_BOUND_P(env, symbol) \
(!SCM_UNBNDP (SCM_ENVIRONMENT_REF (env, symbol)))
#define SCM_ENVIRONMENT_REF(env, symbol) \
((*(SCM_ENVIRONMENT_FUNCS (env)->ref)) (env, symbol))
#define SCM_ENVIRONMENT_FOLD(env, proc, data, init) \
((*(SCM_ENVIRONMENT_FUNCS (env)->fold)) (env, proc, data, init))
#define SCM_ENVIRONMENT_DEFINE(env, symbol, value) \
((*(SCM_ENVIRONMENT_FUNCS (env)->define)) (env, symbol, value))
#define SCM_ENVIRONMENT_UNDEFINE(env, symbol) \
((*(SCM_ENVIRONMENT_FUNCS (env)->undefine)) (env, symbol))
#define SCM_ENVIRONMENT_SET(env, symbol, value) \
((*(SCM_ENVIRONMENT_FUNCS (env)->set)) (env, symbol, value))
#define SCM_ENVIRONMENT_CELL(env, symbol, for_write) \
((*(SCM_ENVIRONMENT_FUNCS (env)->cell)) (env, symbol, for_write))
#define SCM_ENVIRONMENT_OBSERVE(env, proc, data, weak_p) \
((*(SCM_ENVIRONMENT_FUNCS (env)->observe)) (env, proc, data, weak_p))
#define SCM_ENVIRONMENT_UNOBSERVE(env, token) \
((*(SCM_ENVIRONMENT_FUNCS (env)->unobserve)) (env, token))
extern long scm_tc16_observer;
#define SCM_OBSERVER_P(x) \
(!SCM_IMP (x) && (SCM_CELL_TYPE (x) == scm_tc16_observer))
#define SCM_OBSERVER_ENVIRONMENT(x) \
(SCM_CELL_OBJECT_1 (x))
#define SCM_OBSERVER_DATA(x) \
(SCM_CELL_OBJECT_2 (x))
#define SCM_OBSERVER_PROC(x) \
((scm_environment_observer) SCM_CELL_WORD_3 (x))
extern void scm_error_environment_unbound (const char *, SCM, SCM) SCM_NORETURN;
extern void scm_error_environment_immutable_binding (const char *, SCM, SCM) SCM_NORETURN;
extern void scm_error_environment_immutable_location (const char *, SCM, SCM) SCM_NORETURN;
extern SCM scm_make_environment (void *type);
extern SCM scm_environment_p (SCM env);
extern SCM scm_environment_bound_p (SCM env, SCM sym);
extern SCM scm_environment_ref (SCM env, SCM sym);
extern SCM scm_c_environment_ref (SCM env, SCM sym);
extern SCM scm_environment_fold (SCM env, SCM proc, SCM init);
extern SCM scm_c_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init);
extern SCM scm_environment_define (SCM env, SCM sym, SCM val);
extern SCM scm_environment_undefine (SCM env, SCM sym);
extern SCM scm_environment_set_x (SCM env, SCM sym, SCM val);
extern SCM scm_environment_cell (SCM env, SCM sym, SCM for_write);
extern SCM scm_c_environment_cell (SCM env, SCM sym, int for_write);
extern SCM scm_environment_observe (SCM env, SCM proc);
extern SCM scm_environment_observe_weak (SCM env, SCM proc);
extern SCM scm_c_environment_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p);
extern SCM scm_environment_unobserve (SCM token);
extern void scm_environments_prehistory (void);
extern void scm_init_environments (void);
extern void *scm_type_leaf_environment;
#define SCM_LEAF_ENVIRONMENT_P(env) \
(SCM_ENVIRONMENT_P (env) \
&& SCM_ENVIRONMENT_FUNCS (env) == scm_type_leaf_environment)
extern SCM scm_make_leaf_environment (void);
extern SCM scm_leaf_environment_p (SCM env);
extern void *scm_type_eval_environment;
#define SCM_EVAL_ENVIRONMENT_P(env) \
(SCM_ENVIRONMENT_P (env) \
&& SCM_ENVIRONMENT_FUNCS (env) == scm_type_eval_environment)
extern SCM scm_make_eval_environment (SCM local, SCM imported);
extern SCM scm_eval_environment_p (SCM env);
extern SCM scm_eval_environment_local (SCM env);
extern SCM scm_eval_environment_set_local_x (SCM env, SCM local);
extern SCM scm_eval_environment_imported (SCM env);
extern SCM scm_eval_environment_set_imported_x (SCM env, SCM imported);
extern void *scm_type_import_environment;
#define SCM_IMPORT_ENVIRONMENT_P(env) \
(SCM_ENVIRONMENT_FUNCS (env) == scm_type_import_environment)
extern SCM scm_make_import_environment (SCM imports, SCM conflict_proc);
extern SCM scm_import_environment_p (SCM env);
extern SCM scm_import_environment_imports (SCM env);
extern SCM scm_import_environment_set_imports_x (SCM env, SCM imports);
extern void *scm_type_export_environment;
#define SCM_EXPORT_ENVIRONMENT_P(env) \
(SCM_ENVIRONMENT_FUNCS (env) == scm_type_export_environment)
extern SCM scm_make_export_environment (SCM private, SCM signature);
extern SCM scm_export_environment_p (SCM env);
extern SCM scm_export_environment_private (SCM env);
extern SCM scm_export_environment_set_private_x (SCM env, SCM private);
extern SCM scm_export_environment_signature (SCM env);
extern SCM scm_export_environment_set_signature_x (SCM env, SCM signature);
#endif
/*
Local Variables:
c-file-style: "gnu"
End:
*/

View file

@ -68,6 +68,7 @@
#endif
#include "libguile/dynl.h"
#include "libguile/dynwind.h"
#include "libguile/environments.h"
#include "libguile/eq.h"
#include "libguile/error.h"
#include "libguile/eval.h"
@ -490,12 +491,14 @@ scm_boot_guile_1 (SCM_STACKITEM *base, struct main_func_closure *closure)
scm_struct_prehistory (); /* Must come after scm_init_storage */
scm_weaks_prehistory (); /* Must come after scm_init_storage */
scm_init_subr_table ();
scm_environments_prehistory (); /* create the root environment */
scm_init_root ();
#ifdef USE_THREADS
scm_init_threads (base);
#endif
start_stack (base);
scm_init_gsubr ();
scm_init_environments ();
scm_init_feature ();
scm_init_alist ();
scm_init_arbiters ();

View file

@ -1,3 +1,7 @@
2000-08-25 Dirk Herrmann <D.Herrmann@tu-bs.de>
* tests/environments.test: Added.
2000-08-21 Dirk Herrmann <D.Herrmann@tu-bs.de>
* lib.scm (pass-if, expect-fail): Generalized to allow a sequence

View file

@ -0,0 +1,364 @@
;;;; environments.test -*- scheme -*-
;;;; 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.
(use-modules (ice-9 documentation))
;;;
;;; miscellaneous
;;;
(define (documented? object)
(object-documentation object))
(define (make-adder)
(let* ((counter 0))
(lambda increment
(if (not (null? increment))
(set! counter (+ counter (car increment))))
counter)))
(define (folder sym val res)
(cons (cons sym val) res))
;;;
;;; leaf-environments
;;;
(with-test-prefix "leaf-environments"
(with-test-prefix "leaf-environment?"
(pass-if "documented?"
(documented? leaf-environment?))
(pass-if "non-environment-object"
(not (leaf-environment? #f))))
(with-test-prefix "make-leaf-environment"
(pass-if "documented?"
(documented? make-leaf-environment))
(pass-if "produces an environment"
(environment? (make-leaf-environment)))
(pass-if "produces a leaf-environment"
(leaf-environment? (make-leaf-environment)))
(pass-if "produces always a new environment"
(not (eq? (make-leaf-environment) (make-leaf-environment)))))
(with-test-prefix "bound, define, ref, set!, cell"
(let* ((env (make-leaf-environment))
(ctr (make-adder)))
(pass-if "unbound by default"
(and (not (environment-bound? env 'a))
(not (environment-bound? env 'b))
(not (environment-bound? env 'c))))
(pass-if "bound after define"
(environment-define env 'a (ctr 1))
(environment-bound? env 'a))
(pass-if "ref defined"
(and (begin
(environment-define env 'a (ctr 1))
(eq? (environment-ref env 'a) (ctr)))
(begin
(environment-define env 'a (ctr 1))
(eq? (environment-ref env 'a) (ctr)))))
(pass-if "set! defined"
(and (begin
(environment-set! env 'a (ctr 1))
(eq? (environment-ref env 'a) (ctr)))
(begin
(environment-set! env 'a (ctr 1))
(eq? (environment-ref env 'a) (ctr)))))
(pass-if "read-only cell"
(let* ((cell (environment-cell env 'a #f)))
(and (begin
(environment-set! env 'a (ctr 1))
(eq? (cdr cell) (ctr))))))
(pass-if "read-only cell rebound after define"
(let* ((cell (environment-cell env 'a #f)))
(environment-define env 'a (ctr 1))
(not (eq? (environment-cell env 'a #f) cell))))
(pass-if "writable cell"
(let* ((readable (environment-cell env 'a #f))
(writable (environment-cell env 'a #t)))
(and (eq? readable writable)
(begin
(environment-set! env 'a (ctr 1))
(eq? (cdr writable) (ctr)))
(begin
(set-cdr! writable (ctr 1))
(eq? (environment-ref env 'a) (ctr)))
(begin
(set-cdr! (environment-cell env 'a #t) (ctr 1))
(eq? (cdr writable) (ctr))))))
(pass-if "writable cell rebound after define"
(let* ((cell (environment-cell env 'a #t)))
(environment-define env 'a (ctr 1))
(not (eq? (environment-cell env 'a #t) cell))))
(pass-if "referencing undefined"
(catch #t
(lambda ()
(environment-ref env 'b)
#f)
(lambda args
#t)))
(pass-if "set!ing undefined"
(catch #t
(lambda ()
(environment-set! env 'b)
#f)
(lambda args
#t)))
(pass-if "readable cell from undefined"
(catch #t
(lambda ()
(environment-cell env 'b #f)
#f)
(lambda args
#t)))
(pass-if "writable cell from undefined"
(catch #t
(lambda ()
(environment-cell env 'b #t)
#f)
(lambda args
#t)))))
(with-test-prefix "undefine"
(let* ((env (make-leaf-environment)))
(pass-if "undefine defined"
(environment-define env 'a 1)
(and (environment-bound? env 'a)
(begin
(environment-undefine env 'a)
(not (environment-bound? env 'a)))))
(pass-if "undefine undefined"
(and (not (environment-bound? env 'a))
(begin
(environment-undefine env 'a)
(not (environment-bound? env 'a)))))))
(with-test-prefix "fold"
(let* ((env (make-leaf-environment))
(ctr (make-adder)))
(pass-if "fold empty"
(eq? 'success (environment-fold env folder 'success)))
(pass-if "after define"
(environment-define env 'a (ctr 1))
(equal? `((a . ,(ctr))) (environment-fold env folder '())))
(pass-if "after undefine"
(environment-undefine env 'a)
(eq? 'success (environment-fold env folder 'success)))
(pass-if "after two defines"
(let* ((i (ctr 1))
(j (+ i 1)))
(environment-define env 'a i)
(environment-define env 'b j)
(let ((folded (environment-fold env folder '())))
(or (equal? folded `((a . ,i) (b . ,j)))
(equal? folded `((b . ,j) (a . ,i)))))))
(pass-if "after set!"
(let* ((i (environment-ref env 'a)))
(environment-set! env 'b i)
(let ((folded (environment-fold env folder '())))
(or (equal? folded `((a . ,i) (b . ,i)))
(equal? folded `((b . ,i) (a . ,i)))))))))
(with-test-prefix "observe"
(let* ((env (make-leaf-environment))
(tag #f)
(func (lambda (env) (set! tag (not tag))))
(observer #f))
(pass-if "observe unobserved"
(set! observer (environment-observe env func))
#t)
(pass-if "define undefined"
(set! tag #f)
(environment-define env 'a 1)
tag)
(pass-if "define defined"
(set! tag #f)
(environment-define env 'a 1)
tag)
(pass-if "set! defined"
(set! tag #t)
(environment-set! env 'a 1)
tag)
(pass-if "undefine defined"
(set! tag #f)
(environment-undefine env 'a)
tag)
(pass-if "undefine undefined"
(set! tag #t)
(environment-undefine env 'a)
tag)
(pass-if "unobserve observed"
(set! tag #t)
(environment-unobserve observer)
(environment-define env 'a 1)
tag)
(pass-if "unobserve unobserved"
(environment-unobserve observer)
#t)))
(with-test-prefix "observe-weak"
(let* ((env (make-leaf-environment))
(tag #f)
(func (lambda (env) (set! tag (not tag))))
(observer #f))
(pass-if "weak-observe unobserved"
(set! observer (environment-observe-weak env func))
#t)
(pass-if "define undefined"
(set! tag #f)
(environment-define env 'a 1)
tag)
(pass-if "define defined"
(set! tag #f)
(environment-define env 'a 1)
tag)
(pass-if "set! defined"
(set! tag #t)
(environment-set! env 'a 1)
tag)
(pass-if "undefine defined"
(set! tag #f)
(environment-undefine env 'a)
tag)
(pass-if "undefine undefined"
(set! tag #t)
(environment-undefine env 'a)
tag)
(pass-if "unobserve observed"
(set! tag #t)
(environment-unobserve observer)
(environment-define env 'a 1)
tag)
(pass-if "unobserve unobserved"
(environment-unobserve observer)
#t)
(pass-if "weak observer gets collected"
(gc)
(environment-observe-weak env func)
(set! tag #f)
(environment-define env 'a 1)
(and tag
(begin
(gc)
(environment-define env 'a 1)
tag)))))
(with-test-prefix "observer-errors"
(let* ((env (make-leaf-environment))
(tag-1 #f)
(tag-2 #f)
(func-1 (lambda (env)
(set! tag-1 (not tag-1))
(error)))
(func-2 (lambda (env)
(set! tag-2 (not tag-2))
(error))))
(pass-if "update continues after error"
(environment-observe env func-1)
(environment-observe env func-2)
(catch #t
(lambda ()
(environment-define env 'a 1)
#f)
(lambda args
(and tag-1 tag-2)))))))