12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372 |
- /* Copyright (C) 1999,2000,2001 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., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 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/alist.h"
- #include "libguile/eval.h"
- #include "libguile/gh.h"
- #include "libguile/hash.h"
- #include "libguile/ports.h"
- #include "libguile/smob.h"
- #include "libguile/symbols.h"
- #include "libguile/vectors.h"
- #include "libguile/weaks.h"
- #include "libguile/environments.h"
- scm_t_bits scm_tc16_environment;
- scm_t_bits scm_tc16_observer;
- #define DEFAULT_OBARRAY_SIZE 137
- SCM scm_system_environment;
- /* error conditions */
- /*
- * Throw an error if symbol is not bound in environment func
- */
- void
- scm_error_environment_unbound (const char *func, SCM env, SCM symbol)
- {
- /* Dirk:FIXME:: Should throw an environment:unbound type error */
- char error[] = "Symbol `~A' not bound in environment `~A'.";
- SCM arguments = scm_cons2 (symbol, env, SCM_EOL);
- scm_misc_error (func, error, arguments);
- }
- /*
- * Throw an error if func tried to create (define) or remove
- * (undefine) a new binding for symbol in env
- */
- void
- scm_error_environment_immutable_binding (const char *func, SCM env, SCM symbol)
- {
- /* Dirk:FIXME:: Should throw an environment:immutable-binding type error */
- char error[] = "Immutable binding in environment ~A (symbol: `~A').";
- SCM arguments = scm_cons2 (env, symbol, SCM_EOL);
- scm_misc_error (func, error, arguments);
- }
- /*
- * Throw an error if func tried to change an immutable location.
- */
- void
- scm_error_environment_immutable_location (const char *func, SCM env, SCM symbol)
- {
- /* Dirk:FIXME:: Should throw an environment:immutable-location type error */
- char error[] = "Immutable location in environment `~A' (symbol: `~A').";
- SCM arguments = scm_cons2 (env, symbol, SCM_EOL);
- scm_misc_error (func, error, arguments);
- }
- /* generic environments */
- /* Create an environment for the given type. Dereferencing type twice must
- * deliver the initialized set of environment functions. Thus, type will
- * also determine the signature of the underlying environment implementation.
- * Dereferencing type once will typically deliver the data fields used by the
- * underlying environment implementation.
- */
- SCM
- scm_make_environment (void *type)
- {
- SCM env;
- SCM_NEWCELL (env);
- SCM_SET_CELL_WORD_1 (env, type);
- SCM_SET_CELL_TYPE (env, scm_tc16_environment);
- return env;
- }
- SCM_DEFINE (scm_environment_p, "environment?", 1, 0, 0,
- (SCM obj),
- "Return @code{#t} if @var{obj} is an environment, or @code{#f}\n"
- "otherwise.")
- #define FUNC_NAME s_scm_environment_p
- {
- return SCM_BOOL (SCM_ENVIRONMENT_P (obj));
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_environment_bound_p, "environment-bound?", 2, 0, 0,
- (SCM env, SCM sym),
- "Return @code{#t} if @var{sym} is bound in @var{env}, or\n"
- "@code{#f} otherwise.")
- #define FUNC_NAME s_scm_environment_bound_p
- {
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME);
- return SCM_BOOL (SCM_ENVIRONMENT_BOUND_P (env, sym));
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_environment_ref, "environment-ref", 2, 0, 0,
- (SCM env, SCM sym),
- "Return the value of the location bound to @var{sym} in\n"
- "@var{env}. If @var{sym} is unbound in @var{env}, signal an\n"
- "@code{environment:unbound} error.")
- #define FUNC_NAME s_scm_environment_ref
- {
- SCM val;
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME);
- val = SCM_ENVIRONMENT_REF (env, sym);
- if (!SCM_UNBNDP (val))
- return val;
- else
- scm_error_environment_unbound (FUNC_NAME, env, sym);
- }
- #undef FUNC_NAME
- /* This C function is identical to environment-ref, except that if symbol is
- * unbound in env, it returns the value SCM_UNDEFINED, instead of signalling
- * an error.
- */
- SCM
- scm_c_environment_ref (SCM env, SCM sym)
- {
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_ref");
- SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, "scm_c_environment_ref");
- return SCM_ENVIRONMENT_REF (env, sym);
- }
- static SCM
- environment_default_folder (SCM proc, SCM symbol, SCM value, SCM tail)
- {
- return gh_call3 (proc, symbol, value, tail);
- }
- SCM_DEFINE (scm_environment_fold, "environment-fold", 3, 0, 0,
- (SCM env, SCM proc, SCM init),
- "Iterate over all the bindings in @var{env}, accumulating some\n"
- "value.\n"
- "For each binding in @var{env}, apply @var{proc} to the symbol\n"
- "bound, its value, and the result from the previous application\n"
- "of @var{proc}.\n"
- "Use @var{init} as @var{proc}'s third argument the first time\n"
- "@var{proc} is applied.\n"
- "If @var{env} contains no bindings, this function simply returns\n"
- "@var{init}.\n"
- "If @var{env} binds the symbol sym1 to the value val1, sym2 to\n"
- "val2, and so on, then this procedure computes:\n"
- "@lisp\n"
- " (proc sym1 val1\n"
- " (proc sym2 val2\n"
- " ...\n"
- " (proc symn valn\n"
- " init)))\n"
- "@end lisp\n"
- "Each binding in @var{env} will be processed exactly once.\n"
- "@code{environment-fold} makes no guarantees about the order in\n"
- "which the bindings are processed.\n"
- "Here is a function which, given an environment, constructs an\n"
- "association list representing that environment's bindings,\n"
- "using environment-fold:\n"
- "@lisp\n"
- " (define (environment->alist env)\n"
- " (environment-fold env\n"
- " (lambda (sym val tail)\n"
- " (cons (cons sym val) tail))\n"
- " '()))\n"
- "@end lisp")
- #define FUNC_NAME s_scm_environment_fold
- {
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T),
- proc, SCM_ARG2, FUNC_NAME);
- return SCM_ENVIRONMENT_FOLD (env, environment_default_folder, proc, init);
- }
- #undef FUNC_NAME
- /* This is the C-level analog of environment-fold. For each binding in ENV,
- * make the call:
- * (*proc) (data, symbol, value, previous)
- * where previous is the value returned from the last call to *PROC, or INIT
- * for the first call. If ENV contains no bindings, return INIT.
- */
- SCM
- scm_c_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
- {
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_fold");
- return SCM_ENVIRONMENT_FOLD (env, proc, data, init);
- }
- SCM_DEFINE (scm_environment_define, "environment-define", 3, 0, 0,
- (SCM env, SCM sym, SCM val),
- "Bind @var{sym} to a new location containing @var{val} in\n"
- "@var{env}. If @var{sym} is already bound to another location\n"
- "in @var{env} and the binding is mutable, that binding is\n"
- "replaced. The new binding and location are both mutable. The\n"
- "return value is unspecified.\n"
- "If @var{sym} is already bound in @var{env}, and the binding is\n"
- "immutable, signal an @code{environment:immutable-binding} error.")
- #define FUNC_NAME s_scm_environment_define
- {
- SCM status;
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME);
- status = SCM_ENVIRONMENT_DEFINE (env, sym, val);
- if (SCM_EQ_P (status, SCM_ENVIRONMENT_SUCCESS))
- return SCM_UNSPECIFIED;
- else if (SCM_EQ_P (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE))
- scm_error_environment_immutable_binding (FUNC_NAME, env, sym);
- else
- abort();
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_environment_undefine, "environment-undefine", 2, 0, 0,
- (SCM env, SCM sym),
- "Remove any binding for @var{sym} from @var{env}. If @var{sym}\n"
- "is unbound in @var{env}, do nothing. The return value is\n"
- "unspecified.\n"
- "If @var{sym} is already bound in @var{env}, and the binding is\n"
- "immutable, signal an @code{environment:immutable-binding} error.")
- #define FUNC_NAME s_scm_environment_undefine
- {
- SCM status;
- SCM_ASSERT(SCM_ENVIRONMENT_P(env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT(SCM_SYMBOLP(sym), sym, SCM_ARG2, FUNC_NAME);
- status = SCM_ENVIRONMENT_UNDEFINE (env, sym);
- if (SCM_EQ_P (status, SCM_ENVIRONMENT_SUCCESS))
- return SCM_UNSPECIFIED;
- else if (SCM_EQ_P (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE))
- scm_error_environment_immutable_binding (FUNC_NAME, env, sym);
- else
- abort();
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_environment_set_x, "environment-set!", 3, 0, 0,
- (SCM env, SCM sym, SCM val),
- "If @var{env} binds @var{sym} to some location, change that\n"
- "location's value to @var{val}. The return value is\n"
- "unspecified.\n"
- "If @var{sym} is not bound in @var{env}, signal an\n"
- "@code{environment:unbound} error. If @var{env} binds @var{sym}\n"
- "to an immutable location, signal an\n"
- "@code{environment:immutable-location} error.")
- #define FUNC_NAME s_scm_environment_set_x
- {
- SCM status;
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME);
- status = SCM_ENVIRONMENT_SET (env, sym, val);
- if (SCM_EQ_P (status, SCM_ENVIRONMENT_SUCCESS))
- return SCM_UNSPECIFIED;
- else if (SCM_UNBNDP (status))
- scm_error_environment_unbound (FUNC_NAME, env, sym);
- else if (SCM_EQ_P (status, SCM_ENVIRONMENT_LOCATION_IMMUTABLE))
- scm_error_environment_immutable_binding (FUNC_NAME, env, sym);
- else
- abort();
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_environment_cell, "environment-cell", 3, 0, 0,
- (SCM env, SCM sym, SCM for_write),
- "Return the value cell which @var{env} binds to @var{sym}, or\n"
- "@code{#f} if the binding does not live in a value cell.\n"
- "The argument @var{for-write} indicates whether the caller\n"
- "intends to modify the variable's value by mutating the value\n"
- "cell. If the variable is immutable, then\n"
- "@code{environment-cell} signals an\n"
- "@code{environment:immutable-location} error.\n"
- "If @var{sym} is unbound in @var{env}, signal an\n"
- "@code{environment:unbound} error.\n"
- "If you use this function, you should consider using\n"
- "@code{environment-observe}, to be notified when @var{sym} gets\n"
- "re-bound to a new value cell, or becomes undefined.")
- #define FUNC_NAME s_scm_environment_cell
- {
- SCM location;
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, FUNC_NAME);
- SCM_ASSERT (SCM_BOOLP (for_write), for_write, SCM_ARG3, FUNC_NAME);
- location = SCM_ENVIRONMENT_CELL (env, sym, !SCM_FALSEP (for_write));
- if (!SCM_IMP (location))
- return location;
- else if (SCM_UNBNDP (location))
- scm_error_environment_unbound (FUNC_NAME, env, sym);
- else if (SCM_EQ_P (location, SCM_ENVIRONMENT_LOCATION_IMMUTABLE))
- scm_error_environment_immutable_location (FUNC_NAME, env, sym);
- else /* no cell */
- return location;
- }
- #undef FUNC_NAME
- /* This C function is identical to environment-cell, with the following
- * exceptions: If symbol is unbound in env, it returns the value
- * SCM_UNDEFINED, instead of signalling an error. If symbol is bound to an
- * immutable location but the cell is requested for write, the value
- * SCM_ENVIRONMENT_LOCATION_IMMUTABLE is returned.
- */
- SCM
- scm_c_environment_cell(SCM env, SCM sym, int for_write)
- {
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_cell");
- SCM_ASSERT (SCM_SYMBOLP (sym), sym, SCM_ARG2, "scm_c_environment_cell");
- return SCM_ENVIRONMENT_CELL (env, sym, for_write);
- }
- static void
- environment_default_observer (SCM env, SCM proc)
- {
- gh_call1 (proc, env);
- }
- SCM_DEFINE (scm_environment_observe, "environment-observe", 2, 0, 0,
- (SCM env, SCM proc),
- "Whenever @var{env}'s bindings change, apply @var{proc} to\n"
- "@var{env}.\n"
- "This function returns an object, token, which you can pass to\n"
- "@code{environment-unobserve} to remove @var{proc} from the set\n"
- "of procedures observing @var{env}. The type and value of\n"
- "token is unspecified.")
- #define FUNC_NAME s_scm_environment_observe
- {
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- return SCM_ENVIRONMENT_OBSERVE (env, environment_default_observer, proc, 0);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_environment_observe_weak, "environment-observe-weak", 2, 0, 0,
- (SCM env, SCM proc),
- "This function is the same as environment-observe, except that\n"
- "the reference @var{env} retains to @var{proc} is a weak\n"
- "reference. This means that, if there are no other live,\n"
- "non-weak references to @var{proc}, it will be\n"
- "garbage-collected, and dropped from @var{env}'s\n"
- "list of observing procedures.")
- #define FUNC_NAME s_scm_environment_observe_weak
- {
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- return SCM_ENVIRONMENT_OBSERVE (env, environment_default_observer, proc, 1);
- }
- #undef FUNC_NAME
- /* This is the C-level analog of the Scheme functions environment-observe and
- * environment-observe-weak. Whenever env's bindings change, call the
- * function proc, passing it env and data. If weak_p is non-zero, env will
- * retain only a weak reference to data, and if data is garbage collected, the
- * entire observation will be dropped. This function returns a token, with
- * the same meaning as those returned by environment-observe and
- * environment-observe-weak.
- */
- SCM
- scm_c_environment_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p)
- #define FUNC_NAME "scm_c_environment_observe"
- {
- SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- return SCM_ENVIRONMENT_OBSERVE (env, proc, data, weak_p);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_environment_unobserve, "environment-unobserve", 1, 0, 0,
- (SCM token),
- "Cancel the observation request which returned the value\n"
- "@var{token}. The return value is unspecified.\n"
- "If a call @code{(environment-observe env proc)} returns\n"
- "@var{token}, then the call @code{(environment-unobserve token)}\n"
- "will cause @var{proc} to no longer be called when @var{env}'s\n"
- "bindings change.")
- #define FUNC_NAME s_scm_environment_unobserve
- {
- SCM env;
- SCM_ASSERT (SCM_OBSERVER_P (token), token, SCM_ARG1, FUNC_NAME);
- env = SCM_OBSERVER_ENVIRONMENT (token);
- SCM_ENVIRONMENT_UNOBSERVE (env, token);
- return SCM_UNSPECIFIED;
- }
- #undef FUNC_NAME
- static SCM
- environment_mark (SCM env)
- {
- return (*(SCM_ENVIRONMENT_FUNCS (env)->mark)) (env);
- }
- static size_t
- environment_free (SCM env)
- {
- return (*(SCM_ENVIRONMENT_FUNCS (env)->free)) (env);
- }
- static int
- environment_print (SCM env, SCM port, scm_print_state *pstate)
- {
- return (*(SCM_ENVIRONMENT_FUNCS (env)->print)) (env, port, pstate);
- }
- /* observers */
- static SCM
- observer_mark (SCM observer)
- {
- scm_gc_mark (SCM_OBSERVER_ENVIRONMENT (observer));
- scm_gc_mark (SCM_OBSERVER_DATA (observer));
- return SCM_BOOL_F;
- }
- static int
- observer_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED)
- {
- SCM address = scm_ulong2num (SCM_UNPACK (type));
- SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16));
- scm_puts ("#<observer ", port);
- scm_puts (SCM_STRING_CHARS (base16), port);
- scm_puts (">", port);
- return 1;
- }
- /* obarrays
- *
- * Obarrays form the basic lookup tables used to implement most of guile's
- * built-in environment types. An obarray is implemented as a hash table with
- * symbols as keys. The content of the data depends on the environment type.
- */
- /*
- * Enter symbol into obarray. The symbol must not already exist in obarray.
- * The freshly generated (symbol . data) cell is returned.
- */
- static SCM
- obarray_enter (SCM obarray, SCM symbol, SCM data)
- {
- size_t hash = SCM_SYMBOL_HASH (symbol) % SCM_VECTOR_LENGTH (obarray);
- SCM entry = scm_cons (symbol, data);
- SCM slot = scm_cons (entry, SCM_VELTS (obarray)[hash]);
- SCM_VELTS (obarray)[hash] = slot;
- return entry;
- }
- /*
- * Enter symbol into obarray. An existing entry for symbol is replaced. If
- * an entry existed, the old (symbol . data) cell is returned, #f otherwise.
- */
- static SCM
- obarray_replace (SCM obarray, SCM symbol, SCM data)
- {
- size_t hash = SCM_SYMBOL_HASH (symbol) % SCM_VECTOR_LENGTH (obarray);
- SCM new_entry = scm_cons (symbol, data);
- SCM lsym;
- SCM slot;
- for (lsym = SCM_VELTS (obarray)[hash]; !SCM_NULLP (lsym); lsym = SCM_CDR (lsym))
- {
- SCM old_entry = SCM_CAR (lsym);
- if (SCM_EQ_P (SCM_CAR (old_entry), symbol))
- {
- SCM_SETCAR (lsym, new_entry);
- return old_entry;
- }
- }
- slot = scm_cons (new_entry, SCM_VELTS (obarray)[hash]);
- SCM_VELTS (obarray)[hash] = slot;
- return SCM_BOOL_F;
- }
- /*
- * Look up symbol in obarray
- */
- static SCM
- obarray_retrieve (SCM obarray, SCM sym)
- {
- size_t hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (obarray);
- SCM lsym;
- for (lsym = SCM_VELTS (obarray)[hash]; !SCM_NULLP (lsym); lsym = SCM_CDR (lsym))
- {
- SCM entry = SCM_CAR (lsym);
- if (SCM_EQ_P (SCM_CAR (entry), sym))
- return entry;
- }
- return SCM_UNDEFINED;
- }
- /*
- * Remove entry from obarray. If the symbol was found and removed, the old
- * (symbol . data) cell is returned, #f otherwise.
- */
- static SCM
- obarray_remove (SCM obarray, SCM sym)
- {
- size_t hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (obarray);
- SCM lsym;
- SCM *lsymp;
- /* Dirk:FIXME:: gc problem due to use of &SCM_VELTS[hash] */
- for (lsym = *(lsymp = &SCM_VELTS (obarray)[hash]);
- !SCM_NULLP (lsym);
- lsym = *(lsymp = SCM_CDRLOC (lsym)))
- {
- SCM entry = SCM_CAR (lsym);
- if (SCM_EQ_P (SCM_CAR (entry), sym))
- {
- *lsymp = SCM_CDR (lsym);
- return entry;
- }
- }
- return SCM_BOOL_F;
- }
- static void
- obarray_remove_all (SCM obarray)
- {
- size_t size = SCM_VECTOR_LENGTH (obarray);
- size_t i;
- for (i = 0; i < size; i++)
- {
- SCM_VELTS (obarray)[i] = SCM_EOL;
- }
- }
- /* core environments base
- *
- * This struct and the corresponding functions form a base class for guile's
- * built-in environment types.
- */
- struct core_environments_base {
- struct scm_environment_funcs *funcs;
- SCM observers;
- SCM weak_observers;
- };
- #define CORE_ENVIRONMENTS_BASE(env) \
- ((struct core_environments_base *) SCM_CELL_WORD_1 (env))
- #define CORE_ENVIRONMENT_OBSERVERS(env) \
- (CORE_ENVIRONMENTS_BASE (env)->observers)
- #define SCM_SET_CORE_ENVIRONMENT_OBSERVERS(env, v) \
- (CORE_ENVIRONMENT_OBSERVERS (env) = (v))
- #define CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR(env) \
- (CORE_ENVIRONMENTS_BASE (env)->weak_observers)
- #define CORE_ENVIRONMENT_WEAK_OBSERVERS(env) \
- (SCM_VELTS (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env)) [0])
- #define SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS(env, v) \
- (SCM_VELTS (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env)) [0] = (v))
- static SCM
- core_environments_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p)
- {
- SCM observer;
- SCM_NEWCELL2 (observer);
- SCM_SET_CELL_OBJECT_1 (observer, env);
- SCM_SET_CELL_OBJECT_2 (observer, data);
- SCM_SET_CELL_WORD_3 (observer, proc);
- SCM_SET_CELL_TYPE (observer, scm_tc16_observer);
- if (!weak_p)
- {
- SCM observers = CORE_ENVIRONMENT_OBSERVERS (env);
- SCM new_observers = scm_cons (observer, observers);
- SCM_SET_CORE_ENVIRONMENT_OBSERVERS (env, new_observers);
- }
- else
- {
- SCM observers = CORE_ENVIRONMENT_WEAK_OBSERVERS (env);
- SCM new_observers = scm_acons (SCM_BOOL_F, observer, observers);
- SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS (env, new_observers);
- }
- return observer;
- }
- static void
- core_environments_unobserve (SCM env, SCM observer)
- {
- unsigned int handling_weaks;
- for (handling_weaks = 0; handling_weaks <= 1; ++handling_weaks)
- {
- SCM l = handling_weaks
- ? CORE_ENVIRONMENT_WEAK_OBSERVERS (env)
- : CORE_ENVIRONMENT_OBSERVERS (env);
- if (!SCM_NULLP (l))
- {
- SCM rest = SCM_CDR (l);
- SCM first = handling_weaks
- ? SCM_CDAR (l)
- : SCM_CAR (l);
- if (SCM_EQ_P (first, observer))
- {
- /* Remove the first observer */
- handling_weaks
- ? SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS (env, rest)
- : SCM_SET_CORE_ENVIRONMENT_OBSERVERS (env, rest);
- return;
- }
- do {
- SCM rest = SCM_CDR (l);
- if (!SCM_NULLP (rest))
- {
- SCM next = handling_weaks
- ? SCM_CDAR (l)
- : SCM_CAR (l);
- if (SCM_EQ_P (next, observer))
- {
- SCM_SETCDR (l, SCM_CDR (rest));
- return;
- }
- }
- l = rest;
- } while (!SCM_NULLP (l));
- }
- }
- /* Dirk:FIXME:: What to do now, since the observer is not found? */
- }
- static SCM
- core_environments_mark (SCM env)
- {
- scm_gc_mark (CORE_ENVIRONMENT_OBSERVERS (env));
- return CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env);
- }
- static void
- core_environments_finalize (SCM env SCM_UNUSED)
- {
- }
- static void
- core_environments_preinit (struct core_environments_base *body)
- {
- body->funcs = NULL;
- body->observers = SCM_BOOL_F;
- body->weak_observers = SCM_BOOL_F;
- }
- static void
- core_environments_init (struct core_environments_base *body,
- struct scm_environment_funcs *funcs)
- {
- body->funcs = funcs;
- body->observers = SCM_EOL;
- body->weak_observers = scm_make_weak_value_hash_table (SCM_MAKINUM (1));
- }
- /* Tell all observers to clear their caches.
- *
- * Environments have to be informed about changes in the following cases:
- * - The observed env has a new binding. This must be always reported.
- * - The observed env has dropped a binding. This must be always reported.
- * - A binding in the observed environment has changed. This must only be
- * reported, if there is a chance that the binding is being cached outside.
- * However, this potential optimization is not performed currently.
- *
- * Errors that occur while the observers are called are accumulated and
- * signalled as one single error message to the caller.
- */
- struct update_data
- {
- SCM observer;
- SCM environment;
- };
- static SCM
- update_catch_body (void *ptr)
- {
- struct update_data *data = (struct update_data *) ptr;
- SCM observer = data->observer;
- (*SCM_OBSERVER_PROC (observer))
- (data->environment, SCM_OBSERVER_DATA (observer));
- return SCM_UNDEFINED;
- }
- static SCM
- update_catch_handler (void *ptr, SCM tag, SCM args)
- {
- struct update_data *data = (struct update_data *) ptr;
- SCM observer = data->observer;
- SCM message = scm_makfrom0str ("Observer `~A' signals `~A' error: ~S");
- return scm_cons (message, scm_list_3 (observer, tag, args));
- }
- static void
- core_environments_broadcast (SCM env)
- #define FUNC_NAME "core_environments_broadcast"
- {
- unsigned int handling_weaks;
- SCM errors = SCM_EOL;
- for (handling_weaks = 0; handling_weaks <= 1; ++handling_weaks)
- {
- SCM observers = handling_weaks
- ? CORE_ENVIRONMENT_WEAK_OBSERVERS (env)
- : CORE_ENVIRONMENT_OBSERVERS (env);
- for (; !SCM_NULLP (observers); observers = SCM_CDR (observers))
- {
- struct update_data data;
- SCM observer = handling_weaks
- ? SCM_CDAR (observers)
- : SCM_CAR (observers);
- SCM error;
- data.observer = observer;
- data.environment = env;
- error = scm_internal_catch (SCM_BOOL_T,
- update_catch_body, &data,
- update_catch_handler, &data);
- if (!SCM_UNBNDP (error))
- errors = scm_cons (error, errors);
- }
- }
- if (!SCM_NULLP (errors))
- {
- /* Dirk:FIXME:: As soon as scm_misc_error is fixed to handle the name
- * parameter correctly it should not be necessary any more to also pass
- * namestr in order to get the desired information from the error
- * message.
- */
- SCM ordered_errors = scm_reverse (errors);
- scm_misc_error
- (FUNC_NAME,
- "Observers of `~A' have signalled the following errors: ~S",
- scm_cons2 (env, ordered_errors, SCM_EOL));
- }
- }
- #undef FUNC_NAME
- /* leaf environments
- *
- * A leaf environment is simply a mutable set of definitions. A leaf
- * environment supports no operations beyond the common set.
- *
- * Implementation: The obarray of the leaf environment holds (symbol . value)
- * pairs. No further information is necessary, since all bindings and
- * locations in a leaf environment are mutable.
- */
- struct leaf_environment {
- struct core_environments_base base;
- SCM obarray;
- };
- #define LEAF_ENVIRONMENT(env) \
- ((struct leaf_environment *) SCM_CELL_WORD_1 (env))
- static SCM
- leaf_environment_ref (SCM env, SCM sym)
- {
- SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
- SCM binding = obarray_retrieve (obarray, sym);
- return SCM_UNBNDP (binding) ? binding : SCM_CDR (binding);
- }
- static SCM
- leaf_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
- {
- size_t i;
- SCM result = init;
- SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
- for (i = 0; i < SCM_VECTOR_LENGTH (obarray); i++)
- {
- SCM l;
- for (l = SCM_VELTS (obarray)[i]; !SCM_NULLP (l); l = SCM_CDR (l))
- {
- SCM binding = SCM_CAR (l);
- SCM symbol = SCM_CAR (binding);
- SCM value = SCM_CDR (binding);
- result = (*proc) (data, symbol, value, result);
- }
- }
- return result;
- }
- static SCM
- leaf_environment_define (SCM env, SCM sym, SCM val)
- #define FUNC_NAME "leaf_environment_define"
- {
- SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
- obarray_replace (obarray, sym, val);
- core_environments_broadcast (env);
- return SCM_ENVIRONMENT_SUCCESS;
- }
- #undef FUNC_NAME
- static SCM
- leaf_environment_undefine (SCM env, SCM sym)
- #define FUNC_NAME "leaf_environment_undefine"
- {
- SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
- SCM removed = obarray_remove (obarray, sym);
-
- if (!SCM_FALSEP (removed))
- core_environments_broadcast (env);
- return SCM_ENVIRONMENT_SUCCESS;
- }
- #undef FUNC_NAME
- static SCM
- leaf_environment_set_x (SCM env, SCM sym, SCM val)
- #define FUNC_NAME "leaf_environment_set_x"
- {
- SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
- SCM binding = obarray_retrieve (obarray, sym);
- if (!SCM_UNBNDP (binding))
- {
- SCM_SETCDR (binding, val);
- return SCM_ENVIRONMENT_SUCCESS;
- }
- else
- {
- return SCM_UNDEFINED;
- }
- }
- #undef FUNC_NAME
- static SCM
- leaf_environment_cell (SCM env, SCM sym, int for_write SCM_UNUSED)
- {
- SCM obarray = LEAF_ENVIRONMENT (env)->obarray;
- SCM binding = obarray_retrieve (obarray, sym);
- return binding;
- }
- static SCM
- leaf_environment_mark (SCM env)
- {
- scm_gc_mark (LEAF_ENVIRONMENT (env)->obarray);
- return core_environments_mark (env);
- }
- static size_t
- leaf_environment_free (SCM env)
- {
- core_environments_finalize (env);
- free (LEAF_ENVIRONMENT (env));
- return sizeof (struct leaf_environment);
- }
- static int
- leaf_environment_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED)
- {
- SCM address = scm_ulong2num (SCM_UNPACK (type));
- SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16));
- scm_puts ("#<leaf environment ", port);
- scm_puts (SCM_STRING_CHARS (base16), port);
- scm_puts (">", port);
- return 1;
- }
- static struct scm_environment_funcs leaf_environment_funcs = {
- leaf_environment_ref,
- leaf_environment_fold,
- leaf_environment_define,
- leaf_environment_undefine,
- leaf_environment_set_x,
- leaf_environment_cell,
- core_environments_observe,
- core_environments_unobserve,
- leaf_environment_mark,
- leaf_environment_free,
- leaf_environment_print
- };
- void *scm_type_leaf_environment = &leaf_environment_funcs;
- SCM_DEFINE (scm_make_leaf_environment, "make-leaf-environment", 0, 0, 0,
- (),
- "Create a new leaf environment, containing no bindings.\n"
- "All bindings and locations created in the new environment\n"
- "will be mutable.")
- #define FUNC_NAME s_scm_make_leaf_environment
- {
- size_t size = sizeof (struct leaf_environment);
- struct leaf_environment *body = scm_must_malloc (size, FUNC_NAME);
- SCM env;
- core_environments_preinit (&body->base);
- body->obarray = SCM_BOOL_F;
- env = scm_make_environment (body);
- core_environments_init (&body->base, &leaf_environment_funcs);
- body->obarray = scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE);
- return env;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_leaf_environment_p, "leaf-environment?", 1, 0, 0,
- (SCM object),
- "Return @code{#t} if object is a leaf environment, or @code{#f}\n"
- "otherwise.")
- #define FUNC_NAME s_scm_leaf_environment_p
- {
- return SCM_BOOL (SCM_LEAF_ENVIRONMENT_P (object));
- }
- #undef FUNC_NAME
- /* eval environments
- *
- * A module's source code refers to definitions imported from other modules,
- * and definitions made within itself. An eval environment combines two
- * environments -- a local environment and an imported environment -- to
- * produce a new environment in which both sorts of references can be
- * resolved.
- *
- * Implementation: The obarray of the eval environment is used to cache
- * entries from the local and imported environments such that in most of the
- * cases only a single lookup is necessary. Since for neither the local nor
- * the imported environment it is known, what kind of environment they form,
- * the most general case is assumed. Therefore, entries in the obarray take
- * one of the following forms:
- *
- * 1) (<symbol> location mutability . source-env), where mutability indicates
- * one of the following states: IMMUTABLE if the location is known to be
- * immutable, MUTABLE if the location is known to be mutable, UNKNOWN if
- * the location has only been requested for non modifying accesses.
- *
- * 2) (symbol . source-env) if the symbol has a binding in the source-env, but
- * if the source-env can't provide a cell for the binding. Thus, for every
- * access, the source-env has to be contacted directly.
- */
- struct eval_environment {
- struct core_environments_base base;
- SCM obarray;
- SCM imported;
- SCM imported_observer;
- SCM local;
- SCM local_observer;
- };
- #define EVAL_ENVIRONMENT(env) \
- ((struct eval_environment *) SCM_CELL_WORD_1 (env))
- #define IMMUTABLE SCM_MAKINUM (0)
- #define MUTABLE SCM_MAKINUM (1)
- #define UNKNOWN SCM_MAKINUM (2)
- #define CACHED_LOCATION(x) SCM_CAR (x)
- #define CACHED_MUTABILITY(x) SCM_CADR (x)
- #define SET_CACHED_MUTABILITY(x, v) SCM_SETCAR (SCM_CDR (x), (v))
- #define CACHED_SOURCE_ENVIRONMENT(x) SCM_CDDR (x)
- /* eval_environment_lookup will report one of the following distinct results:
- * a) (<object> . value) if a cell could be obtained.
- * b) <environment> if the environment has to be contacted directly.
- * c) IMMUTABLE if an immutable cell was requested for write.
- * d) SCM_UNDEFINED if there is no binding for the symbol.
- */
- static SCM
- eval_environment_lookup (SCM env, SCM sym, int for_write)
- {
- SCM obarray = EVAL_ENVIRONMENT (env)->obarray;
- SCM binding = obarray_retrieve (obarray, sym);
- if (!SCM_UNBNDP (binding))
- {
- /* The obarray holds an entry for the symbol. */
- SCM entry = SCM_CDR (binding);
- if (SCM_CONSP (entry))
- {
- /* The entry in the obarray is a cached location. */
- SCM location = CACHED_LOCATION (entry);
- SCM mutability;
- if (!for_write)
- return location;
- mutability = CACHED_MUTABILITY (entry);
- if (SCM_EQ_P (mutability, MUTABLE))
- return location;
- if (SCM_EQ_P (mutability, UNKNOWN))
- {
- SCM source_env = CACHED_SOURCE_ENVIRONMENT (entry);
- SCM location = SCM_ENVIRONMENT_CELL (source_env, sym, 1);
- if (SCM_CONSP (location))
- {
- SET_CACHED_MUTABILITY (entry, MUTABLE);
- return location;
- }
- else /* IMMUTABLE */
- {
- SET_CACHED_MUTABILITY (entry, IMMUTABLE);
- return IMMUTABLE;
- }
- }
- return IMMUTABLE;
- }
- else
- {
- /* The obarray entry is an environment */
- return entry;
- }
- }
- else
- {
- /* There is no entry for the symbol in the obarray. This can either
- * mean that there has not been a request for the symbol yet, or that
- * the symbol is really undefined. We are looking for the symbol in
- * both the local and the imported environment. If we find a binding, a
- * cached entry is created.
- */
- struct eval_environment *body = EVAL_ENVIRONMENT (env);
- unsigned int handling_import;
- for (handling_import = 0; handling_import <= 1; ++handling_import)
- {
- SCM source_env = handling_import ? body->imported : body->local;
- SCM location = SCM_ENVIRONMENT_CELL (source_env, sym, for_write);
- if (!SCM_UNBNDP (location))
- {
- if (SCM_CONSP (location))
- {
- SCM mutability = for_write ? MUTABLE : UNKNOWN;
- SCM entry = scm_cons2 (location, mutability, source_env);
- obarray_enter (obarray, sym, entry);
- return location;
- }
- else if (SCM_EQ_P (location, SCM_ENVIRONMENT_LOCATION_NO_CELL))
- {
- obarray_enter (obarray, sym, source_env);
- return source_env;
- }
- else
- {
- return IMMUTABLE;
- }
- }
- }
- return SCM_UNDEFINED;
- }
- }
- static SCM
- eval_environment_ref (SCM env, SCM sym)
- #define FUNC_NAME "eval_environment_ref"
- {
- SCM location = eval_environment_lookup (env, sym, 0);
- if (SCM_CONSP (location))
- return SCM_CDR (location);
- else if (!SCM_UNBNDP (location))
- return SCM_ENVIRONMENT_REF (location, sym);
- else
- return SCM_UNDEFINED;
- }
- #undef FUNC_NAME
- static SCM
- eval_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail)
- {
- SCM local = SCM_CAR (extended_data);
- if (!SCM_ENVIRONMENT_BOUND_P (local, symbol))
- {
- SCM proc_as_nr = SCM_CADR (extended_data);
- unsigned long int proc_as_ul = scm_num2ulong (proc_as_nr, 0, NULL);
- scm_environment_folder proc = (scm_environment_folder) proc_as_ul;
- SCM data = SCM_CDDR (extended_data);
- return (*proc) (data, symbol, value, tail);
- }
- else
- {
- return tail;
- }
- }
- static SCM
- eval_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
- {
- SCM local = EVAL_ENVIRONMENT (env)->local;
- SCM imported = EVAL_ENVIRONMENT (env)->imported;
- SCM proc_as_nr = scm_ulong2num ((unsigned long int) proc);
- SCM extended_data = scm_cons2 (local, proc_as_nr, data);
- SCM tmp_result = scm_c_environment_fold (imported, eval_environment_folder, extended_data, init);
- return scm_c_environment_fold (local, proc, data, tmp_result);
- }
- static SCM
- eval_environment_define (SCM env, SCM sym, SCM val)
- #define FUNC_NAME "eval_environment_define"
- {
- SCM local = EVAL_ENVIRONMENT (env)->local;
- return SCM_ENVIRONMENT_DEFINE (local, sym, val);
- }
- #undef FUNC_NAME
- static SCM
- eval_environment_undefine (SCM env, SCM sym)
- #define FUNC_NAME "eval_environment_undefine"
- {
- SCM local = EVAL_ENVIRONMENT (env)->local;
- return SCM_ENVIRONMENT_UNDEFINE (local, sym);
- }
- #undef FUNC_NAME
- static SCM
- eval_environment_set_x (SCM env, SCM sym, SCM val)
- #define FUNC_NAME "eval_environment_set_x"
- {
- SCM location = eval_environment_lookup (env, sym, 1);
- if (SCM_CONSP (location))
- {
- SCM_SETCDR (location, val);
- return SCM_ENVIRONMENT_SUCCESS;
- }
- else if (SCM_ENVIRONMENT_P (location))
- {
- return SCM_ENVIRONMENT_SET (location, sym, val);
- }
- else if (SCM_EQ_P (location, IMMUTABLE))
- {
- return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
- }
- else
- {
- return SCM_UNDEFINED;
- }
- }
- #undef FUNC_NAME
- static SCM
- eval_environment_cell (SCM env, SCM sym, int for_write)
- #define FUNC_NAME "eval_environment_cell"
- {
- SCM location = eval_environment_lookup (env, sym, for_write);
- if (SCM_CONSP (location))
- return location;
- else if (SCM_ENVIRONMENT_P (location))
- return SCM_ENVIRONMENT_LOCATION_NO_CELL;
- else if (SCM_EQ_P (location, IMMUTABLE))
- return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
- else
- return SCM_UNDEFINED;
- }
- #undef FUNC_NAME
- static SCM
- eval_environment_mark (SCM env)
- {
- struct eval_environment *body = EVAL_ENVIRONMENT (env);
- scm_gc_mark (body->obarray);
- scm_gc_mark (body->imported);
- scm_gc_mark (body->imported_observer);
- scm_gc_mark (body->local);
- scm_gc_mark (body->local_observer);
- return core_environments_mark (env);
- }
- static size_t
- eval_environment_free (SCM env)
- {
- core_environments_finalize (env);
- free (EVAL_ENVIRONMENT (env));
- return sizeof (struct eval_environment);
- }
- static int
- eval_environment_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED)
- {
- SCM address = scm_ulong2num (SCM_UNPACK (type));
- SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16));
- scm_puts ("#<eval environment ", port);
- scm_puts (SCM_STRING_CHARS (base16), port);
- scm_puts (">", port);
- return 1;
- }
- static struct scm_environment_funcs eval_environment_funcs = {
- eval_environment_ref,
- eval_environment_fold,
- eval_environment_define,
- eval_environment_undefine,
- eval_environment_set_x,
- eval_environment_cell,
- core_environments_observe,
- core_environments_unobserve,
- eval_environment_mark,
- eval_environment_free,
- eval_environment_print
- };
- void *scm_type_eval_environment = &eval_environment_funcs;
- static void
- eval_environment_observer (SCM caller SCM_UNUSED, SCM eval_env)
- {
- SCM obarray = EVAL_ENVIRONMENT (eval_env)->obarray;
- obarray_remove_all (obarray);
- core_environments_broadcast (eval_env);
- }
- SCM_DEFINE (scm_make_eval_environment, "make-eval-environment", 2, 0, 0,
- (SCM local, SCM imported),
- "Return a new environment object eval whose bindings are the\n"
- "union of the bindings in the environments @var{local} and\n"
- "@var{imported}, with bindings from @var{local} taking\n"
- "precedence. Definitions made in eval are placed in @var{local}.\n"
- "Applying @code{environment-define} or\n"
- "@code{environment-undefine} to eval has the same effect as\n"
- "applying the procedure to @var{local}.\n"
- "Note that eval incorporates @var{local} and @var{imported} by\n"
- "reference:\n"
- "If, after creating eval, the program changes the bindings of\n"
- "@var{local} or @var{imported}, those changes will be visible\n"
- "in eval.\n"
- "Since most Scheme evaluation takes place in eval environments,\n"
- "they transparently cache the bindings received from @var{local}\n"
- "and @var{imported}. Thus, the first time the program looks up\n"
- "a symbol in eval, eval may make calls to @var{local} or\n"
- "@var{imported} to find their bindings, but subsequent\n"
- "references to that symbol will be as fast as references to\n"
- "bindings in finite environments.\n"
- "In typical use, @var{local} will be a finite environment, and\n"
- "@var{imported} will be an import environment")
- #define FUNC_NAME s_scm_make_eval_environment
- {
- SCM env;
- struct eval_environment *body;
- SCM_ASSERT (SCM_ENVIRONMENT_P (local), local, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_ENVIRONMENT_P (imported), imported, SCM_ARG2, FUNC_NAME);
- body = scm_must_malloc (sizeof (struct eval_environment), FUNC_NAME);
- core_environments_preinit (&body->base);
- body->obarray = SCM_BOOL_F;
- body->imported = SCM_BOOL_F;
- body->imported_observer = SCM_BOOL_F;
- body->local = SCM_BOOL_F;
- body->local_observer = SCM_BOOL_F;
- env = scm_make_environment (body);
- core_environments_init (&body->base, &eval_environment_funcs);
- body->obarray = scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE);
- body->imported = imported;
- body->imported_observer
- = SCM_ENVIRONMENT_OBSERVE (imported, eval_environment_observer, env, 1);
- body->local = local;
- body->local_observer
- = SCM_ENVIRONMENT_OBSERVE (local, eval_environment_observer, env, 1);
- return env;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_eval_environment_p, "eval-environment?", 1, 0, 0,
- (SCM object),
- "Return @code{#t} if object is an eval environment, or @code{#f}\n"
- "otherwise.")
- #define FUNC_NAME s_scm_eval_environment_p
- {
- return SCM_BOOL (SCM_EVAL_ENVIRONMENT_P (object));
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_eval_environment_local, "eval-environment-local", 1, 0, 0,
- (SCM env),
- "Return the local environment of eval environment @var{env}.")
- #define FUNC_NAME s_scm_eval_environment_local
- {
- SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- return EVAL_ENVIRONMENT (env)->local;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_eval_environment_set_local_x, "eval-environment-set-local!", 2, 0, 0,
- (SCM env, SCM local),
- "Change @var{env}'s local environment to @var{local}.")
- #define FUNC_NAME s_scm_eval_environment_set_local_x
- {
- struct eval_environment *body;
- SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_ENVIRONMENT_P (local), local, SCM_ARG2, FUNC_NAME);
- body = EVAL_ENVIRONMENT (env);
- obarray_remove_all (body->obarray);
- SCM_ENVIRONMENT_UNOBSERVE (body->local, body->local_observer);
- body->local = local;
- body->local_observer
- = SCM_ENVIRONMENT_OBSERVE (local, eval_environment_observer, env, 1);
- core_environments_broadcast (env);
- return SCM_UNSPECIFIED;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_eval_environment_imported, "eval-environment-imported", 1, 0, 0,
- (SCM env),
- "Return the imported environment of eval environment @var{env}.")
- #define FUNC_NAME s_scm_eval_environment_imported
- {
- SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- return EVAL_ENVIRONMENT (env)->imported;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_eval_environment_set_imported_x, "eval-environment-set-imported!", 2, 0, 0,
- (SCM env, SCM imported),
- "Change @var{env}'s imported environment to @var{imported}.")
- #define FUNC_NAME s_scm_eval_environment_set_imported_x
- {
- struct eval_environment *body;
- SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_ENVIRONMENT_P (imported), imported, SCM_ARG2, FUNC_NAME);
- body = EVAL_ENVIRONMENT (env);
- obarray_remove_all (body->obarray);
- SCM_ENVIRONMENT_UNOBSERVE (body->imported, body->imported_observer);
- body->imported = imported;
- body->imported_observer
- = SCM_ENVIRONMENT_OBSERVE (imported, eval_environment_observer, env, 1);
- core_environments_broadcast (env);
- return SCM_UNSPECIFIED;
- }
- #undef FUNC_NAME
- /* import environments
- *
- * An import environment combines the bindings of a set of argument
- * environments, and checks for naming clashes.
- *
- * Implementation: The import environment does no caching at all. For every
- * access, the list of imported environments is scanned.
- */
- struct import_environment {
- struct core_environments_base base;
- SCM imports;
- SCM import_observers;
- SCM conflict_proc;
- };
- #define IMPORT_ENVIRONMENT(env) \
- ((struct import_environment *) SCM_CELL_WORD_1 (env))
- /* Lookup will report one of the following distinct results:
- * a) <environment> if only environment binds the symbol.
- * b) (env-1 env-2 ...) for conflicting bindings in env-1, ...
- * c) SCM_UNDEFINED if there is no binding for the symbol.
- */
- static SCM
- import_environment_lookup (SCM env, SCM sym)
- {
- SCM imports = IMPORT_ENVIRONMENT (env)->imports;
- SCM result = SCM_UNDEFINED;
- SCM l;
- for (l = imports; !SCM_NULLP (l); l = SCM_CDR (l))
- {
- SCM imported = SCM_CAR (l);
- if (SCM_ENVIRONMENT_BOUND_P (imported, sym))
- {
- if (SCM_UNBNDP (result))
- result = imported;
- else if (SCM_CONSP (result))
- result = scm_cons (imported, result);
- else
- result = scm_cons2 (imported, result, SCM_EOL);
- }
- }
- if (SCM_CONSP (result))
- return scm_reverse (result);
- else
- return result;
- }
- static SCM
- import_environment_conflict (SCM env, SCM sym, SCM imports)
- {
- SCM conflict_proc = IMPORT_ENVIRONMENT (env)->conflict_proc;
- SCM args = scm_cons2 (env, sym, scm_cons (imports, SCM_EOL));
- return scm_apply_0 (conflict_proc, args);
- }
- static SCM
- import_environment_ref (SCM env, SCM sym)
- #define FUNC_NAME "import_environment_ref"
- {
- SCM owner = import_environment_lookup (env, sym);
- if (SCM_UNBNDP (owner))
- {
- return SCM_UNDEFINED;
- }
- else if (SCM_CONSP (owner))
- {
- SCM resolve = import_environment_conflict (env, sym, owner);
- if (SCM_ENVIRONMENT_P (resolve))
- return SCM_ENVIRONMENT_REF (resolve, sym);
- else
- return SCM_UNSPECIFIED;
- }
- else
- {
- return SCM_ENVIRONMENT_REF (owner, sym);
- }
- }
- #undef FUNC_NAME
- static SCM
- import_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail)
- #define FUNC_NAME "import_environment_fold"
- {
- SCM import_env = SCM_CAR (extended_data);
- SCM imported_env = SCM_CADR (extended_data);
- SCM owner = import_environment_lookup (import_env, symbol);
- SCM proc_as_nr = SCM_CADDR (extended_data);
- unsigned long int proc_as_ul = scm_num2ulong (proc_as_nr, 0, NULL);
- scm_environment_folder proc = (scm_environment_folder) proc_as_ul;
- SCM data = SCM_CDDDR (extended_data);
- if (SCM_CONSP (owner) && SCM_EQ_P (SCM_CAR (owner), imported_env))
- owner = import_environment_conflict (import_env, symbol, owner);
- if (SCM_ENVIRONMENT_P (owner))
- return (*proc) (data, symbol, value, tail);
- else /* unresolved conflict */
- return (*proc) (data, symbol, SCM_UNSPECIFIED, tail);
- }
- #undef FUNC_NAME
- static SCM
- import_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
- {
- SCM proc_as_nr = scm_ulong2num ((unsigned long int) proc);
- SCM result = init;
- SCM l;
- for (l = IMPORT_ENVIRONMENT (env)->imports; !SCM_NULLP (l); l = SCM_CDR (l))
- {
- SCM imported_env = SCM_CAR (l);
- SCM extended_data = scm_cons (env, scm_cons2 (imported_env, proc_as_nr, data));
- result = scm_c_environment_fold (imported_env, import_environment_folder, extended_data, result);
- }
- return result;
- }
- static SCM
- import_environment_define (SCM env SCM_UNUSED,
- SCM sym SCM_UNUSED,
- SCM val SCM_UNUSED)
- #define FUNC_NAME "import_environment_define"
- {
- return SCM_ENVIRONMENT_BINDING_IMMUTABLE;
- }
- #undef FUNC_NAME
- static SCM
- import_environment_undefine (SCM env SCM_UNUSED,
- SCM sym SCM_UNUSED)
- #define FUNC_NAME "import_environment_undefine"
- {
- return SCM_ENVIRONMENT_BINDING_IMMUTABLE;
- }
- #undef FUNC_NAME
- static SCM
- import_environment_set_x (SCM env, SCM sym, SCM val)
- #define FUNC_NAME "import_environment_set_x"
- {
- SCM owner = import_environment_lookup (env, sym);
- if (SCM_UNBNDP (owner))
- {
- return SCM_UNDEFINED;
- }
- else if (SCM_CONSP (owner))
- {
- SCM resolve = import_environment_conflict (env, sym, owner);
- if (SCM_ENVIRONMENT_P (resolve))
- return SCM_ENVIRONMENT_SET (resolve, sym, val);
- else
- return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
- }
- else
- {
- return SCM_ENVIRONMENT_SET (owner, sym, val);
- }
- }
- #undef FUNC_NAME
- static SCM
- import_environment_cell (SCM env, SCM sym, int for_write)
- #define FUNC_NAME "import_environment_cell"
- {
- SCM owner = import_environment_lookup (env, sym);
- if (SCM_UNBNDP (owner))
- {
- return SCM_UNDEFINED;
- }
- else if (SCM_CONSP (owner))
- {
- SCM resolve = import_environment_conflict (env, sym, owner);
- if (SCM_ENVIRONMENT_P (resolve))
- return SCM_ENVIRONMENT_CELL (resolve, sym, for_write);
- else
- return SCM_ENVIRONMENT_LOCATION_NO_CELL;
- }
- else
- {
- return SCM_ENVIRONMENT_CELL (owner, sym, for_write);
- }
- }
- #undef FUNC_NAME
- static SCM
- import_environment_mark (SCM env)
- {
- scm_gc_mark (IMPORT_ENVIRONMENT (env)->imports);
- scm_gc_mark (IMPORT_ENVIRONMENT (env)->import_observers);
- scm_gc_mark (IMPORT_ENVIRONMENT (env)->conflict_proc);
- return core_environments_mark (env);
- }
- static size_t
- import_environment_free (SCM env)
- {
- core_environments_finalize (env);
- free (IMPORT_ENVIRONMENT (env));
- return sizeof (struct import_environment);
- }
- static int
- import_environment_print (SCM type, SCM port,
- scm_print_state *pstate SCM_UNUSED)
- {
- SCM address = scm_ulong2num (SCM_UNPACK (type));
- SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16));
- scm_puts ("#<import environment ", port);
- scm_puts (SCM_STRING_CHARS (base16), port);
- scm_puts (">", port);
- return 1;
- }
- static struct scm_environment_funcs import_environment_funcs = {
- import_environment_ref,
- import_environment_fold,
- import_environment_define,
- import_environment_undefine,
- import_environment_set_x,
- import_environment_cell,
- core_environments_observe,
- core_environments_unobserve,
- import_environment_mark,
- import_environment_free,
- import_environment_print
- };
- void *scm_type_import_environment = &import_environment_funcs;
- static void
- import_environment_observer (SCM caller SCM_UNUSED, SCM import_env)
- {
- core_environments_broadcast (import_env);
- }
- SCM_DEFINE (scm_make_import_environment, "make-import-environment", 2, 0, 0,
- (SCM imports, SCM conflict_proc),
- "Return a new environment @var{imp} whose bindings are the union\n"
- "of the bindings from the environments in @var{imports};\n"
- "@var{imports} must be a list of environments. That is,\n"
- "@var{imp} binds a symbol to a location when some element of\n"
- "@var{imports} does.\n"
- "If two different elements of @var{imports} have a binding for\n"
- "the same symbol, the @var{conflict-proc} is called with the\n"
- "following parameters: the import environment, the symbol and\n"
- "the list of the imported environments that bind the symbol.\n"
- "If the @var{conflict-proc} returns an environment @var{env},\n"
- "the conflict is considered as resolved and the binding from\n"
- "@var{env} is used. If the @var{conflict-proc} returns some\n"
- "non-environment object, the conflict is considered unresolved\n"
- "and the symbol is treated as unspecified in the import\n"
- "environment.\n"
- "The checking for conflicts may be performed lazily, i. e. at\n"
- "the moment when a value or binding for a certain symbol is\n"
- "requested instead of the moment when the environment is\n"
- "created or the bindings of the imports change.\n"
- "All bindings in @var{imp} are immutable. If you apply\n"
- "@code{environment-define} or @code{environment-undefine} to\n"
- "@var{imp}, Guile will signal an\n"
- " @code{environment:immutable-binding} error. However,\n"
- "notice that the set of bindings in @var{imp} may still change,\n"
- "if one of its imported environments changes.")
- #define FUNC_NAME s_scm_make_import_environment
- {
- size_t size = sizeof (struct import_environment);
- struct import_environment *body = scm_must_malloc (size, FUNC_NAME);
- SCM env;
- core_environments_preinit (&body->base);
- body->imports = SCM_BOOL_F;
- body->import_observers = SCM_BOOL_F;
- body->conflict_proc = SCM_BOOL_F;
- env = scm_make_environment (body);
- core_environments_init (&body->base, &import_environment_funcs);
- body->imports = SCM_EOL;
- body->import_observers = SCM_EOL;
- body->conflict_proc = conflict_proc;
- scm_import_environment_set_imports_x (env, imports);
- return env;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_import_environment_p, "import-environment?", 1, 0, 0,
- (SCM object),
- "Return @code{#t} if object is an import environment, or\n"
- "@code{#f} otherwise.")
- #define FUNC_NAME s_scm_import_environment_p
- {
- return SCM_BOOL (SCM_IMPORT_ENVIRONMENT_P (object));
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_import_environment_imports, "import-environment-imports", 1, 0, 0,
- (SCM env),
- "Return the list of environments imported by the import\n"
- "environment @var{env}.")
- #define FUNC_NAME s_scm_import_environment_imports
- {
- SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- return IMPORT_ENVIRONMENT (env)->imports;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_import_environment_set_imports_x, "import-environment-set-imports!", 2, 0, 0,
- (SCM env, SCM imports),
- "Change @var{env}'s list of imported environments to\n"
- "@var{imports}, and check for conflicts.")
- #define FUNC_NAME s_scm_import_environment_set_imports_x
- {
- struct import_environment *body = IMPORT_ENVIRONMENT (env);
- SCM import_observers = SCM_EOL;
- SCM l;
- SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- for (l = imports; SCM_CONSP (l); l = SCM_CDR (l))
- {
- SCM obj = SCM_CAR (l);
- SCM_ASSERT (SCM_ENVIRONMENT_P (obj), imports, SCM_ARG2, FUNC_NAME);
- }
- SCM_ASSERT (SCM_NULLP (l), imports, SCM_ARG2, FUNC_NAME);
- for (l = body->import_observers; !SCM_NULLP (l); l = SCM_CDR (l))
- {
- SCM obs = SCM_CAR (l);
- SCM_ENVIRONMENT_UNOBSERVE (env, obs);
- }
- for (l = imports; !SCM_NULLP (l); l = SCM_CDR (l))
- {
- SCM imp = SCM_CAR (l);
- SCM obs = SCM_ENVIRONMENT_OBSERVE (imp, import_environment_observer, env, 1);
- import_observers = scm_cons (obs, import_observers);
- }
- body->imports = imports;
- body->import_observers = import_observers;
- return SCM_UNSPECIFIED;
- }
- #undef FUNC_NAME
- /* export environments
- *
- * An export environment restricts an environment to a specified set of
- * bindings.
- *
- * Implementation: The export environment does no caching at all. For every
- * access, the signature is scanned. The signature that is stored internally
- * is an alist of pairs (symbol . (mutability)).
- */
- struct export_environment {
- struct core_environments_base base;
- SCM private;
- SCM private_observer;
- SCM signature;
- };
- #define EXPORT_ENVIRONMENT(env) \
- ((struct export_environment *) SCM_CELL_WORD_1 (env))
- SCM_SYMBOL (symbol_immutable_location, "immutable-location");
- SCM_SYMBOL (symbol_mutable_location, "mutable-location");
- static SCM
- export_environment_ref (SCM env, SCM sym)
- #define FUNC_NAME "export_environment_ref"
- {
- struct export_environment *body = EXPORT_ENVIRONMENT (env);
- SCM entry = scm_assq (sym, body->signature);
- if (SCM_FALSEP (entry))
- return SCM_UNDEFINED;
- else
- return SCM_ENVIRONMENT_REF (body->private, sym);
- }
- #undef FUNC_NAME
- static SCM
- export_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init)
- {
- struct export_environment *body = EXPORT_ENVIRONMENT (env);
- SCM result = init;
- SCM l;
- for (l = body->signature; !SCM_NULLP (l); l = SCM_CDR (l))
- {
- SCM symbol = SCM_CAR (l);
- SCM value = SCM_ENVIRONMENT_REF (body->private, symbol);
- if (!SCM_UNBNDP (value))
- result = (*proc) (data, symbol, value, result);
- }
- return result;
- }
- static SCM
- export_environment_define (SCM env SCM_UNUSED,
- SCM sym SCM_UNUSED,
- SCM val SCM_UNUSED)
- #define FUNC_NAME "export_environment_define"
- {
- return SCM_ENVIRONMENT_BINDING_IMMUTABLE;
- }
- #undef FUNC_NAME
- static SCM
- export_environment_undefine (SCM env SCM_UNUSED, SCM sym SCM_UNUSED)
- #define FUNC_NAME "export_environment_undefine"
- {
- return SCM_ENVIRONMENT_BINDING_IMMUTABLE;
- }
- #undef FUNC_NAME
- static SCM
- export_environment_set_x (SCM env, SCM sym, SCM val)
- #define FUNC_NAME "export_environment_set_x"
- {
- struct export_environment *body = EXPORT_ENVIRONMENT (env);
- SCM entry = scm_assq (sym, body->signature);
- if (SCM_FALSEP (entry))
- {
- return SCM_UNDEFINED;
- }
- else
- {
- if (SCM_EQ_P (SCM_CADR (entry), symbol_mutable_location))
- return SCM_ENVIRONMENT_SET (body->private, sym, val);
- else
- return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
- }
- }
- #undef FUNC_NAME
- static SCM
- export_environment_cell (SCM env, SCM sym, int for_write)
- #define FUNC_NAME "export_environment_cell"
- {
- struct export_environment *body = EXPORT_ENVIRONMENT (env);
- SCM entry = scm_assq (sym, body->signature);
- if (SCM_FALSEP (entry))
- {
- return SCM_UNDEFINED;
- }
- else
- {
- if (!for_write || SCM_EQ_P (SCM_CADR (entry), symbol_mutable_location))
- return SCM_ENVIRONMENT_CELL (body->private, sym, for_write);
- else
- return SCM_ENVIRONMENT_LOCATION_IMMUTABLE;
- }
- }
- #undef FUNC_NAME
- static SCM
- export_environment_mark (SCM env)
- {
- struct export_environment *body = EXPORT_ENVIRONMENT (env);
- scm_gc_mark (body->private);
- scm_gc_mark (body->private_observer);
- scm_gc_mark (body->signature);
- return core_environments_mark (env);
- }
- static size_t
- export_environment_free (SCM env)
- {
- core_environments_finalize (env);
- free (EXPORT_ENVIRONMENT (env));
- return sizeof (struct export_environment);
- }
- static int
- export_environment_print (SCM type, SCM port,
- scm_print_state *pstate SCM_UNUSED)
- {
- SCM address = scm_ulong2num (SCM_UNPACK (type));
- SCM base16 = scm_number_to_string (address, SCM_MAKINUM (16));
- scm_puts ("#<export environment ", port);
- scm_puts (SCM_STRING_CHARS (base16), port);
- scm_puts (">", port);
- return 1;
- }
- static struct scm_environment_funcs export_environment_funcs = {
- export_environment_ref,
- export_environment_fold,
- export_environment_define,
- export_environment_undefine,
- export_environment_set_x,
- export_environment_cell,
- core_environments_observe,
- core_environments_unobserve,
- export_environment_mark,
- export_environment_free,
- export_environment_print
- };
- void *scm_type_export_environment = &export_environment_funcs;
- static void
- export_environment_observer (SCM caller SCM_UNUSED, SCM export_env)
- {
- core_environments_broadcast (export_env);
- }
- SCM_DEFINE (scm_make_export_environment, "make-export-environment", 2, 0, 0,
- (SCM private, SCM signature),
- "Return a new environment @var{exp} containing only those\n"
- "bindings in private whose symbols are present in\n"
- "@var{signature}. The @var{private} argument must be an\n"
- "environment.\n\n"
- "The environment @var{exp} binds symbol to location when\n"
- "@var{env} does, and symbol is exported by @var{signature}.\n\n"
- "@var{signature} is a list specifying which of the bindings in\n"
- "@var{private} should be visible in @var{exp}. Each element of\n"
- "@var{signature} should be a list of the form:\n"
- " (symbol attribute ...)\n"
- "where each attribute is one of the following:\n"
- "@table @asis\n"
- "@item the symbol @code{mutable-location}\n"
- " @var{exp} should treat the\n"
- " location bound to symbol as mutable. That is, @var{exp}\n"
- " will pass calls to @code{environment-set!} or\n"
- " @code{environment-cell} directly through to private.\n"
- "@item the symbol @code{immutable-location}\n"
- " @var{exp} should treat\n"
- " the location bound to symbol as immutable. If the program\n"
- " applies @code{environment-set!} to @var{exp} and symbol, or\n"
- " calls @code{environment-cell} to obtain a writable value\n"
- " cell, @code{environment-set!} will signal an\n"
- " @code{environment:immutable-location} error. Note that, even\n"
- " if an export environment treats a location as immutable, the\n"
- " underlying environment may treat it as mutable, so its\n"
- " value may change.\n"
- "@end table\n"
- "It is an error for an element of signature to specify both\n"
- "@code{mutable-location} and @code{immutable-location}. If\n"
- "neither is specified, @code{immutable-location} is assumed.\n\n"
- "As a special case, if an element of signature is a lone\n"
- "symbol @var{sym}, it is equivalent to an element of the form\n"
- "@code{(sym)}.\n\n"
- "All bindings in @var{exp} are immutable. If you apply\n"
- "@code{environment-define} or @code{environment-undefine} to\n"
- "@var{exp}, Guile will signal an\n"
- "@code{environment:immutable-binding} error. However,\n"
- "notice that the set of bindings in @var{exp} may still change,\n"
- "if the bindings in private change.")
- #define FUNC_NAME s_scm_make_export_environment
- {
- size_t size;
- struct export_environment *body;
- SCM env;
- SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG1, FUNC_NAME);
- size = sizeof (struct export_environment);
- body = scm_must_malloc (size, FUNC_NAME);
- core_environments_preinit (&body->base);
- body->private = SCM_BOOL_F;
- body->private_observer = SCM_BOOL_F;
- body->signature = SCM_BOOL_F;
- env = scm_make_environment (body);
- core_environments_init (&body->base, &export_environment_funcs);
- body->private = private;
- body->private_observer
- = SCM_ENVIRONMENT_OBSERVE (private, export_environment_observer, env, 1);
- body->signature = SCM_EOL;
- scm_export_environment_set_signature_x (env, signature);
- return env;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_export_environment_p, "export-environment?", 1, 0, 0,
- (SCM object),
- "Return @code{#t} if object is an export environment, or\n"
- "@code{#f} otherwise.")
- #define FUNC_NAME s_scm_export_environment_p
- {
- return SCM_BOOL (SCM_EXPORT_ENVIRONMENT_P (object));
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_export_environment_private, "export-environment-private", 1, 0, 0,
- (SCM env),
- "Return the private environment of export environment @var{env}.")
- #define FUNC_NAME s_scm_export_environment_private
- {
- SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- return EXPORT_ENVIRONMENT (env)->private;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_export_environment_set_private_x, "export-environment-set-private!", 2, 0, 0,
- (SCM env, SCM private),
- "Change the private environment of export environment @var{env}.")
- #define FUNC_NAME s_scm_export_environment_set_private_x
- {
- struct export_environment *body;
- SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG2, FUNC_NAME);
- body = EXPORT_ENVIRONMENT (env);
- SCM_ENVIRONMENT_UNOBSERVE (private, body->private_observer);
- body->private = private;
- body->private_observer
- = SCM_ENVIRONMENT_OBSERVE (private, export_environment_observer, env, 1);
- return SCM_UNSPECIFIED;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_export_environment_signature, "export-environment-signature", 1, 0, 0,
- (SCM env),
- "Return the signature of export environment @var{env}.")
- #define FUNC_NAME s_scm_export_environment_signature
- {
- SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- return EXPORT_ENVIRONMENT (env)->signature;
- }
- #undef FUNC_NAME
- static SCM
- export_environment_parse_signature (SCM signature, const char* caller)
- {
- SCM result = SCM_EOL;
- SCM l;
- for (l = signature; SCM_CONSP (l); l = SCM_CDR (l))
- {
- SCM entry = SCM_CAR (l);
- if (SCM_SYMBOLP (entry))
- {
- SCM new_entry = scm_cons2 (entry, symbol_immutable_location, SCM_EOL);
- result = scm_cons (new_entry, result);
- }
- else
- {
- SCM sym;
- SCM new_entry;
- int immutable = 0;
- int mutable = 0;
- SCM mutability;
- SCM l2;
- SCM_ASSERT (SCM_CONSP (entry), entry, SCM_ARGn, caller);
- SCM_ASSERT (SCM_SYMBOLP (SCM_CAR (entry)), entry, SCM_ARGn, caller);
- sym = SCM_CAR (entry);
- for (l2 = SCM_CDR (entry); SCM_CONSP (l2); l2 = SCM_CDR (l2))
- {
- SCM attribute = SCM_CAR (l2);
- if (SCM_EQ_P (attribute, symbol_immutable_location))
- immutable = 1;
- else if (SCM_EQ_P (attribute, symbol_mutable_location))
- mutable = 1;
- else
- SCM_ASSERT (0, entry, SCM_ARGn, caller);
- }
- SCM_ASSERT (SCM_NULLP (l2), entry, SCM_ARGn, caller);
- SCM_ASSERT (!mutable || !immutable, entry, SCM_ARGn, caller);
- if (!mutable && !immutable)
- immutable = 1;
- mutability = mutable ? symbol_mutable_location : symbol_immutable_location;
- new_entry = scm_cons2 (sym, mutability, SCM_EOL);
- result = scm_cons (new_entry, result);
- }
- }
- SCM_ASSERT (SCM_NULLP (l), signature, SCM_ARGn, caller);
- /* Dirk:FIXME:: Now we know that signature is syntactically correct. There
- * are, however, no checks for symbols entered twice with contradicting
- * mutabilities. It would be nice, to implement this test, to be able to
- * call the sort functions conveniently from C.
- */
- return scm_reverse (result);
- }
- SCM_DEFINE (scm_export_environment_set_signature_x, "export-environment-set-signature!", 2, 0, 0,
- (SCM env, SCM signature),
- "Change the signature of export environment @var{env}.")
- #define FUNC_NAME s_scm_export_environment_set_signature_x
- {
- SCM parsed_sig;
- SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME);
- parsed_sig = export_environment_parse_signature (signature, FUNC_NAME);
- EXPORT_ENVIRONMENT (env)->signature = parsed_sig;
- return SCM_UNSPECIFIED;
- }
- #undef FUNC_NAME
- void
- scm_environments_prehistory ()
- {
- /* create environment smob */
- scm_tc16_environment = scm_make_smob_type ("environment", 0);
- scm_set_smob_mark (scm_tc16_environment, environment_mark);
- scm_set_smob_free (scm_tc16_environment, environment_free);
- scm_set_smob_print (scm_tc16_environment, environment_print);
- /* create observer smob */
- scm_tc16_observer = scm_make_smob_type ("observer", 0);
- scm_set_smob_mark (scm_tc16_observer, observer_mark);
- scm_set_smob_print (scm_tc16_observer, observer_print);
- /* create system environment */
- scm_system_environment = scm_make_leaf_environment ();
- scm_permanent_object (scm_system_environment);
- }
- void
- scm_init_environments ()
- {
- #include "libguile/environments.x"
- }
- /*
- Local Variables:
- c-file-style: "gnu"
- End:
- */
|