boot-9.scm 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011
  3. ;;;; Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;;
  19. ;;; Commentary:
  20. ;;; This file is the first thing loaded into Guile. It adds many mundane
  21. ;;; definitions and a few that are interesting.
  22. ;;;
  23. ;;; The module system (hence the hierarchical namespace) are defined in this
  24. ;;; file.
  25. ;;;
  26. ;;; Code:
  27. ;; Before compiling, make sure any symbols are resolved in the (guile)
  28. ;; module, the primary location of those symbols, rather than in
  29. ;; (guile-user), the default module that we compile in.
  30. (eval-when (compile)
  31. (set-current-module (resolve-module '(guile))))
  32. ;;; {Error handling}
  33. ;;;
  34. ;; Define delimited continuation operators, and implement catch and throw in
  35. ;; terms of them.
  36. (define make-prompt-tag
  37. (lambda* (#:optional (stem "prompt"))
  38. (gensym stem)))
  39. (define default-prompt-tag
  40. ;; not sure if we should expose this to the user as a fluid
  41. (let ((%default-prompt-tag (make-prompt-tag)))
  42. (lambda ()
  43. %default-prompt-tag)))
  44. (define (call-with-prompt tag thunk handler)
  45. (@prompt tag (thunk) handler))
  46. (define (abort-to-prompt tag . args)
  47. (@abort tag args))
  48. ;; Define catch and with-throw-handler, using some common helper routines and a
  49. ;; shared fluid. Hide the helpers in a lexical contour.
  50. (define with-throw-handler #f)
  51. (let ()
  52. ;; Ideally we'd like to be able to give these default values for all threads,
  53. ;; even threads not created by Guile; but alack, that does not currently seem
  54. ;; possible. So wrap the getters in thunks.
  55. (define %running-exception-handlers (make-fluid))
  56. (define %exception-handler (make-fluid))
  57. (define (running-exception-handlers)
  58. (or (fluid-ref %running-exception-handlers)
  59. (begin
  60. (fluid-set! %running-exception-handlers '())
  61. '())))
  62. (define (exception-handler)
  63. (or (fluid-ref %exception-handler)
  64. (begin
  65. (fluid-set! %exception-handler default-exception-handler)
  66. default-exception-handler)))
  67. (define (default-exception-handler k . args)
  68. (cond
  69. ((eq? k 'quit)
  70. (primitive-exit (cond
  71. ((not (pair? args)) 0)
  72. ((integer? (car args)) (car args))
  73. ((not (car args)) 1)
  74. (else 0))))
  75. (else
  76. (format (current-error-port) "guile: uncaught throw to ~a: ~a\n" k args)
  77. (primitive-exit 1))))
  78. (define (default-throw-handler prompt-tag catch-k)
  79. (let ((prev (exception-handler)))
  80. (lambda (thrown-k . args)
  81. (if (or (eq? thrown-k catch-k) (eqv? catch-k #t))
  82. (apply abort-to-prompt prompt-tag thrown-k args)
  83. (apply prev thrown-k args)))))
  84. (define (custom-throw-handler prompt-tag catch-k pre)
  85. (let ((prev (exception-handler)))
  86. (lambda (thrown-k . args)
  87. (if (or (eq? thrown-k catch-k) (eqv? catch-k #t))
  88. (let ((running (running-exception-handlers)))
  89. (with-fluids ((%running-exception-handlers (cons pre running)))
  90. (if (not (memq pre running))
  91. (apply pre thrown-k args))
  92. ;; fall through
  93. (if prompt-tag
  94. (apply abort-to-prompt prompt-tag thrown-k args)
  95. (apply prev thrown-k args))))
  96. (apply prev thrown-k args)))))
  97. (set! catch
  98. (lambda* (k thunk handler #:optional pre-unwind-handler)
  99. "Invoke @var{thunk} in the dynamic context of @var{handler} for
  100. exceptions matching @var{key}. If thunk throws to the symbol
  101. @var{key}, then @var{handler} is invoked this way:
  102. @lisp
  103. (handler key args ...)
  104. @end lisp
  105. @var{key} is a symbol or @code{#t}.
  106. @var{thunk} takes no arguments. If @var{thunk} returns
  107. normally, that is the return value of @code{catch}.
  108. Handler is invoked outside the scope of its own @code{catch}.
  109. If @var{handler} again throws to the same key, a new handler
  110. from further up the call chain is invoked.
  111. If the key is @code{#t}, then a throw to @emph{any} symbol will
  112. match this call to @code{catch}.
  113. If a @var{pre-unwind-handler} is given and @var{thunk} throws
  114. an exception that matches @var{key}, Guile calls the
  115. @var{pre-unwind-handler} before unwinding the dynamic state and
  116. invoking the main @var{handler}. @var{pre-unwind-handler} should
  117. be a procedure with the same signature as @var{handler}, that
  118. is @code{(lambda (key . args))}. It is typically used to save
  119. the stack at the point where the exception occurred, but can also
  120. query other parts of the dynamic state at that point, such as
  121. fluid values.
  122. A @var{pre-unwind-handler} can exit either normally or non-locally.
  123. If it exits normally, Guile unwinds the stack and dynamic context
  124. and then calls the normal (third argument) handler. If it exits
  125. non-locally, that exit determines the continuation."
  126. (if (not (or (symbol? k) (eqv? k #t)))
  127. (scm-error "catch" 'wrong-type-arg
  128. "Wrong type argument in position ~a: ~a"
  129. (list 1 k) (list k)))
  130. (let ((tag (make-prompt-tag "catch")))
  131. (call-with-prompt
  132. tag
  133. (lambda ()
  134. (with-fluids
  135. ((%exception-handler
  136. (if pre-unwind-handler
  137. (custom-throw-handler tag k pre-unwind-handler)
  138. (default-throw-handler tag k))))
  139. (thunk)))
  140. (lambda (cont k . args)
  141. (apply handler k args))))))
  142. (set! with-throw-handler
  143. (lambda (k thunk pre-unwind-handler)
  144. "Add @var{handler} to the dynamic context as a throw handler
  145. for key @var{key}, then invoke @var{thunk}."
  146. (if (not (or (symbol? k) (eqv? k #t)))
  147. (scm-error "with-throw-handler" 'wrong-type-arg
  148. "Wrong type argument in position ~a: ~a"
  149. (list 1 k) (list k)))
  150. (with-fluids ((%exception-handler
  151. (custom-throw-handler #f k pre-unwind-handler)))
  152. (thunk))))
  153. (set! throw
  154. (lambda (key . args)
  155. "Invoke the catch form matching @var{key}, passing @var{args} to the
  156. @var{handler}.
  157. @var{key} is a symbol. It will match catches of the same symbol or of @code{#t}.
  158. If there is no handler at all, Guile prints an error and then exits."
  159. (if (not (symbol? key))
  160. ((exception-handler) 'wrong-type-arg "throw"
  161. "Wrong type argument in position ~a: ~a" (list 1 key) (list key))
  162. (apply (exception-handler) key args)))))
  163. ;;; {R4RS compliance}
  164. ;;;
  165. (primitive-load-path "ice-9/r4rs")
  166. ;;; {Simple Debugging Tools}
  167. ;;;
  168. ;; peek takes any number of arguments, writes them to the
  169. ;; current ouput port, and returns the last argument.
  170. ;; It is handy to wrap around an expression to look at
  171. ;; a value each time is evaluated, e.g.:
  172. ;;
  173. ;; (+ 10 (troublesome-fn))
  174. ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
  175. ;;
  176. (define (peek . stuff)
  177. (newline)
  178. (display ";;; ")
  179. (write stuff)
  180. (newline)
  181. (car (last-pair stuff)))
  182. (define pk peek)
  183. (define (warn . stuff)
  184. (with-output-to-port (current-error-port)
  185. (lambda ()
  186. (newline)
  187. (display ";;; WARNING ")
  188. (display stuff)
  189. (newline)
  190. (car (last-pair stuff)))))
  191. ;;; {Features}
  192. ;;;
  193. (define (provide sym)
  194. (if (not (memq sym *features*))
  195. (set! *features* (cons sym *features*))))
  196. ;; Return #t iff FEATURE is available to this Guile interpreter. In SLIB,
  197. ;; provided? also checks to see if the module is available. We should do that
  198. ;; too, but don't.
  199. (define (provided? feature)
  200. (and (memq feature *features*) #t))
  201. ;;; {Structs}
  202. ;;;
  203. (define (make-struct/no-tail vtable . args)
  204. (apply make-struct vtable 0 args))
  205. ;;; Boot versions of `map' and `for-each', enough to get the expander
  206. ;;; running.
  207. ;;;
  208. (define map
  209. (case-lambda
  210. ((f l)
  211. (let map1 ((l l))
  212. (if (null? l)
  213. '()
  214. (cons (f (car l)) (map1 (cdr l))))))
  215. ((f l1 l2)
  216. (let map2 ((l1 l1) (l2 l2))
  217. (if (null? l1)
  218. '()
  219. (cons (f (car l1) (car l2))
  220. (map2 (cdr l1) (cdr l2))))))
  221. ((f l1 . rest)
  222. (let lp ((l1 l1) (rest rest))
  223. (if (null? l1)
  224. '()
  225. (cons (apply f (car l1) (map car rest))
  226. (lp (cdr l1) (map cdr rest))))))))
  227. (define for-each
  228. (case-lambda
  229. ((f l)
  230. (let for-each1 ((l l))
  231. (if (pair? l)
  232. (begin
  233. (f (car l))
  234. (for-each1 (cdr l))))))
  235. ((f l1 l2)
  236. (let for-each2 ((l1 l1) (l2 l2))
  237. (if (pair? l1)
  238. (begin
  239. (f (car l1) (car l2))
  240. (for-each2 (cdr l1) (cdr l2))))))
  241. ((f l1 . rest)
  242. (let lp ((l1 l1) (rest rest))
  243. (if (pair? l1)
  244. (begin
  245. (apply f (car l1) (map car rest))
  246. (lp (cdr l1) (map cdr rest))))))))
  247. ;;; {and-map and or-map}
  248. ;;;
  249. ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
  250. ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
  251. ;;;
  252. ;; and-map f l
  253. ;;
  254. ;; Apply f to successive elements of l until exhaustion or f returns #f.
  255. ;; If returning early, return #f. Otherwise, return the last value returned
  256. ;; by f. If f has never been called because l is empty, return #t.
  257. ;;
  258. (define (and-map f lst)
  259. (let loop ((result #t)
  260. (l lst))
  261. (and result
  262. (or (and (null? l)
  263. result)
  264. (loop (f (car l)) (cdr l))))))
  265. ;; or-map f l
  266. ;;
  267. ;; Apply f to successive elements of l until exhaustion or while f returns #f.
  268. ;; If returning early, return the return value of f.
  269. ;;
  270. (define (or-map f lst)
  271. (let loop ((result #f)
  272. (l lst))
  273. (or result
  274. (and (not (null? l))
  275. (loop (f (car l)) (cdr l))))))
  276. ;; let format alias simple-format until the more complete version is loaded
  277. (define format simple-format)
  278. ;; this is scheme wrapping the C code so the final pred call is a tail call,
  279. ;; per SRFI-13 spec
  280. (define string-any
  281. (lambda* (char_pred s #:optional (start 0) (end (string-length s)))
  282. (if (and (procedure? char_pred)
  283. (> end start)
  284. (<= end (string-length s))) ;; let c-code handle range error
  285. (or (string-any-c-code char_pred s start (1- end))
  286. (char_pred (string-ref s (1- end))))
  287. (string-any-c-code char_pred s start end))))
  288. ;; this is scheme wrapping the C code so the final pred call is a tail call,
  289. ;; per SRFI-13 spec
  290. (define string-every
  291. (lambda* (char_pred s #:optional (start 0) (end (string-length s)))
  292. (if (and (procedure? char_pred)
  293. (> end start)
  294. (<= end (string-length s))) ;; let c-code handle range error
  295. (and (string-every-c-code char_pred s start (1- end))
  296. (char_pred (string-ref s (1- end))))
  297. (string-every-c-code char_pred s start end))))
  298. ;; A variant of string-fill! that we keep for compatability
  299. ;;
  300. (define (substring-fill! str start end fill)
  301. (string-fill! str fill start end))
  302. ;; Define a minimal stub of the module API for psyntax, before modules
  303. ;; have booted.
  304. (define (module-name x)
  305. '(guile))
  306. (define (module-add! module sym var)
  307. (hashq-set! (%get-pre-modules-obarray) sym var))
  308. (define (module-define! module sym val)
  309. (let ((v (hashq-ref (%get-pre-modules-obarray) sym)))
  310. (if v
  311. (variable-set! v val)
  312. (module-add! (current-module) sym (make-variable val)))))
  313. (define (module-ref module sym)
  314. (let ((v (module-variable module sym)))
  315. (if v (variable-ref v) (error "badness!" (pk module) (pk sym)))))
  316. (define (resolve-module . args)
  317. #f)
  318. ;; API provided by psyntax
  319. (define syntax-violation #f)
  320. (define datum->syntax #f)
  321. (define syntax->datum #f)
  322. (define syntax-source #f)
  323. (define identifier? #f)
  324. (define generate-temporaries #f)
  325. (define bound-identifier=? #f)
  326. (define free-identifier=? #f)
  327. ;; $sc-dispatch is an implementation detail of psyntax. It is used by
  328. ;; expanded macros, to dispatch an input against a set of patterns.
  329. (define $sc-dispatch #f)
  330. ;; Load it up!
  331. (primitive-load-path "ice-9/psyntax-pp")
  332. ;; The binding for `macroexpand' has now been overridden, making psyntax the
  333. ;; expander now.
  334. (define-syntax and
  335. (syntax-rules ()
  336. ((_) #t)
  337. ((_ x) x)
  338. ((_ x y ...) (if x (and y ...) #f))))
  339. (define-syntax or
  340. (syntax-rules ()
  341. ((_) #f)
  342. ((_ x) x)
  343. ((_ x y ...) (let ((t x)) (if t t (or y ...))))))
  344. ;; The "maybe-more" bits are something of a hack, so that we can support
  345. ;; SRFI-61. Rewrites into a standalone syntax-case macro would be
  346. ;; appreciated.
  347. (define-syntax cond
  348. (syntax-rules (=> else)
  349. ((_ "maybe-more" test consequent)
  350. (if test consequent))
  351. ((_ "maybe-more" test consequent clause ...)
  352. (if test consequent (cond clause ...)))
  353. ((_ (else else1 else2 ...))
  354. (begin else1 else2 ...))
  355. ((_ (test => receiver) more-clause ...)
  356. (let ((t test))
  357. (cond "maybe-more" t (receiver t) more-clause ...)))
  358. ((_ (generator guard => receiver) more-clause ...)
  359. (call-with-values (lambda () generator)
  360. (lambda t
  361. (cond "maybe-more"
  362. (apply guard t) (apply receiver t) more-clause ...))))
  363. ((_ (test => receiver ...) more-clause ...)
  364. (syntax-violation 'cond "wrong number of receiver expressions"
  365. '(test => receiver ...)))
  366. ((_ (generator guard => receiver ...) more-clause ...)
  367. (syntax-violation 'cond "wrong number of receiver expressions"
  368. '(generator guard => receiver ...)))
  369. ((_ (test) more-clause ...)
  370. (let ((t test))
  371. (cond "maybe-more" t t more-clause ...)))
  372. ((_ (test body1 body2 ...) more-clause ...)
  373. (cond "maybe-more"
  374. test (begin body1 body2 ...) more-clause ...))))
  375. (define-syntax case
  376. (syntax-rules (else)
  377. ((case (key ...)
  378. clauses ...)
  379. (let ((atom-key (key ...)))
  380. (case atom-key clauses ...)))
  381. ((case key
  382. (else result1 result2 ...))
  383. (begin result1 result2 ...))
  384. ((case key
  385. ((atoms ...) result1 result2 ...))
  386. (if (memv key '(atoms ...))
  387. (begin result1 result2 ...)))
  388. ((case key
  389. ((atoms ...) result1 result2 ...)
  390. clause clauses ...)
  391. (if (memv key '(atoms ...))
  392. (begin result1 result2 ...)
  393. (case key clause clauses ...)))))
  394. (define-syntax do
  395. (syntax-rules ()
  396. ((do ((var init step ...) ...)
  397. (test expr ...)
  398. command ...)
  399. (letrec
  400. ((loop
  401. (lambda (var ...)
  402. (if test
  403. (begin
  404. (if #f #f)
  405. expr ...)
  406. (begin
  407. command
  408. ...
  409. (loop (do "step" var step ...)
  410. ...))))))
  411. (loop init ...)))
  412. ((do "step" x)
  413. x)
  414. ((do "step" x y)
  415. y)))
  416. (define-syntax-rule (delay exp)
  417. (make-promise (lambda () exp)))
  418. (include-from-path "ice-9/quasisyntax")
  419. (define-syntax current-source-location
  420. (lambda (x)
  421. (syntax-case x ()
  422. ((_)
  423. (with-syntax ((s (datum->syntax x (syntax-source x))))
  424. #''s)))))
  425. (define-syntax-rule (define-once sym val)
  426. (define sym
  427. (if (module-locally-bound? (current-module) 'sym) sym val)))
  428. ;;; The real versions of `map' and `for-each', with cycle detection, and
  429. ;;; that use reverse! instead of recursion in the case of `map'.
  430. ;;;
  431. (define map
  432. (case-lambda
  433. ((f l)
  434. (let map1 ((hare l) (tortoise l) (move? #f) (out '()))
  435. (if (pair? hare)
  436. (if move?
  437. (if (eq? tortoise hare)
  438. (scm-error 'wrong-type-arg "map" "Circular list: ~S"
  439. (list l) #f)
  440. (map1 (cdr hare) (cdr tortoise) #f
  441. (cons (f (car hare)) out)))
  442. (map1 (cdr hare) tortoise #t
  443. (cons (f (car hare)) out)))
  444. (if (null? hare)
  445. (reverse! out)
  446. (scm-error 'wrong-type-arg "map" "Not a list: ~S"
  447. (list l) #f)))))
  448. ((f l1 l2)
  449. (let map2 ((h1 l1) (h2 l2) (t1 l1) (t2 l2) (move? #f) (out '()))
  450. (cond
  451. ((pair? h1)
  452. (cond
  453. ((not (pair? h2))
  454. (scm-error 'wrong-type-arg "map"
  455. (if (list? h2)
  456. "List of wrong length: ~S"
  457. "Not a list: ~S")
  458. (list l2) #f))
  459. ((not move?)
  460. (map2 (cdr h1) (cdr h2) t1 t2 #t
  461. (cons (f (car h1) (car h2)) out)))
  462. ((eq? t1 h1)
  463. (scm-error 'wrong-type-arg "map" "Circular list: ~S"
  464. (list l1) #f))
  465. ((eq? t2 h2)
  466. (scm-error 'wrong-type-arg "map" "Circular list: ~S"
  467. (list l2) #f))
  468. (else
  469. (map2 (cdr h1) (cdr h2) (cdr t1) (cdr t2) #f
  470. (cons (f (car h1) (car h2)) out)))))
  471. ((and (null? h1) (null? h2))
  472. (reverse! out))
  473. ((null? h1)
  474. (scm-error 'wrong-type-arg "map"
  475. (if (list? h2)
  476. "List of wrong length: ~S"
  477. "Not a list: ~S")
  478. (list l2) #f))
  479. (else
  480. (scm-error 'wrong-type-arg "map"
  481. "Not a list: ~S"
  482. (list l1) #f)))))
  483. ((f l1 . rest)
  484. (let ((len (length l1)))
  485. (let mapn ((rest rest))
  486. (or (null? rest)
  487. (if (= (length (car rest)) len)
  488. (mapn (cdr rest))
  489. (scm-error 'wrong-type-arg "map" "List of wrong length: ~S"
  490. (list (car rest)) #f)))))
  491. (let mapn ((l1 l1) (rest rest) (out '()))
  492. (if (null? l1)
  493. (reverse! out)
  494. (mapn (cdr l1) (map cdr rest)
  495. (cons (apply f (car l1) (map car rest)) out)))))))
  496. (define map-in-order map)
  497. (define for-each
  498. (case-lambda
  499. ((f l)
  500. (let for-each1 ((hare l) (tortoise l) (move? #f))
  501. (if (pair? hare)
  502. (if move?
  503. (if (eq? tortoise hare)
  504. (scm-error 'wrong-type-arg "for-each" "Circular list: ~S"
  505. (list l) #f)
  506. (begin
  507. (f (car hare))
  508. (for-each1 (cdr hare) (cdr tortoise) #f)))
  509. (begin
  510. (f (car hare))
  511. (for-each1 (cdr hare) tortoise #t)))
  512. (if (not (null? hare))
  513. (scm-error 'wrong-type-arg "for-each" "Not a list: ~S"
  514. (list l) #f)))))
  515. ((f l1 l2)
  516. (let for-each2 ((h1 l1) (h2 l2) (t1 l1) (t2 l2) (move? #f))
  517. (cond
  518. ((and (pair? h1) (pair? h2))
  519. (cond
  520. ((not move?)
  521. (f (car h1) (car h2))
  522. (for-each2 (cdr h1) (cdr h2) t1 t2 #t))
  523. ((eq? t1 h1)
  524. (scm-error 'wrong-type-arg "for-each" "Circular list: ~S"
  525. (list l1) #f))
  526. ((eq? t2 h2)
  527. (scm-error 'wrong-type-arg "for-each" "Circular list: ~S"
  528. (list l2) #f))
  529. (else
  530. (f (car h1) (car h2))
  531. (for-each2 (cdr h1) (cdr h2) (cdr t1) (cdr t2) #f))))
  532. ((if (null? h1)
  533. (or (null? h2) (pair? h2))
  534. (and (pair? h1) (null? h2)))
  535. (if #f #f))
  536. ((list? h1)
  537. (scm-error 'wrong-type-arg "for-each" "Unexpected tail: ~S"
  538. (list h2) #f))
  539. (else
  540. (scm-error 'wrong-type-arg "for-each" "Unexpected tail: ~S"
  541. (list h1) #f)))))
  542. ((f l1 . rest)
  543. (let ((len (length l1)))
  544. (let for-eachn ((rest rest))
  545. (or (null? rest)
  546. (if (= (length (car rest)) len)
  547. (for-eachn (cdr rest))
  548. (scm-error 'wrong-type-arg "for-each" "List of wrong length: ~S"
  549. (list (car rest)) #f)))))
  550. (let for-eachn ((l1 l1) (rest rest))
  551. (if (pair? l1)
  552. (begin
  553. (apply f (car l1) (map car rest))
  554. (for-eachn (cdr l1) (map cdr rest))))))))
  555. ;;;
  556. ;;; Extensible exception printing.
  557. ;;;
  558. (define set-exception-printer! #f)
  559. ;; There is already a definition of print-exception from backtrace.c
  560. ;; that we will override.
  561. (let ((exception-printers '()))
  562. (define (print-location frame port)
  563. (let ((source (and=> frame frame-source)))
  564. ;; source := (addr . (filename . (line . column)))
  565. (if source
  566. (let ((filename (or (cadr source) "<unnamed port>"))
  567. (line (caddr source))
  568. (col (cdddr source)))
  569. (format port "~a:~a:~a: " filename (1+ line) col))
  570. (format port "ERROR: "))))
  571. (set! set-exception-printer!
  572. (lambda (key proc)
  573. (set! exception-printers (acons key proc exception-printers))))
  574. (set! print-exception
  575. (lambda (port frame key args)
  576. (define (default-printer)
  577. (format port "Throw to key `~a' with args `~s'." key args))
  578. (if frame
  579. (let ((proc (frame-procedure frame)))
  580. (print-location frame port)
  581. (format port "In procedure ~a:\n"
  582. (or (procedure-name proc) proc))))
  583. (print-location frame port)
  584. (catch #t
  585. (lambda ()
  586. (let ((printer (assq-ref exception-printers key)))
  587. (if printer
  588. (printer port key args default-printer)
  589. (default-printer))))
  590. (lambda (k . args)
  591. (format port "Error while printing exception.")))
  592. (newline port)
  593. (force-output port))))
  594. ;;;
  595. ;;; Printers for those keys thrown by Guile.
  596. ;;;
  597. (let ()
  598. (define (scm-error-printer port key args default-printer)
  599. ;; Abuse case-lambda as a pattern matcher, given that we don't have
  600. ;; ice-9 match at this point.
  601. (apply (case-lambda
  602. ((subr msg args . rest)
  603. (if subr
  604. (format port "In procedure ~a: " subr))
  605. (apply format port msg (or args '())))
  606. (_ (default-printer)))
  607. args))
  608. (define (syntax-error-printer port key args default-printer)
  609. (apply (case-lambda
  610. ((who what where form subform . extra)
  611. (format port "Syntax error:\n")
  612. (if where
  613. (let ((file (or (assq-ref where 'filename) "unknown file"))
  614. (line (and=> (assq-ref where 'line) 1+))
  615. (col (assq-ref where 'column)))
  616. (format port "~a:~a:~a: " file line col))
  617. (format port "unknown location: "))
  618. (if who
  619. (format port "~a: " who))
  620. (format port "~a" what)
  621. (if subform
  622. (format port " in subform ~s of ~s" subform form)
  623. (if form
  624. (format port " in form ~s" form))))
  625. (_ (default-printer)))
  626. args))
  627. (set-exception-printer! 'goops-error scm-error-printer)
  628. (set-exception-printer! 'host-not-found scm-error-printer)
  629. (set-exception-printer! 'keyword-argument-error scm-error-printer)
  630. (set-exception-printer! 'misc-error scm-error-printer)
  631. (set-exception-printer! 'no-data scm-error-printer)
  632. (set-exception-printer! 'no-recovery scm-error-printer)
  633. (set-exception-printer! 'null-pointer-error scm-error-printer)
  634. (set-exception-printer! 'out-of-range scm-error-printer)
  635. (set-exception-printer! 'program-error scm-error-printer)
  636. (set-exception-printer! 'read-error scm-error-printer)
  637. (set-exception-printer! 'regular-expression-syntax scm-error-printer)
  638. (set-exception-printer! 'signal scm-error-printer)
  639. (set-exception-printer! 'stack-overflow scm-error-printer)
  640. (set-exception-printer! 'system-error scm-error-printer)
  641. (set-exception-printer! 'try-again scm-error-printer)
  642. (set-exception-printer! 'unbound-variable scm-error-printer)
  643. (set-exception-printer! 'wrong-number-of-args scm-error-printer)
  644. (set-exception-printer! 'wrong-type-arg scm-error-printer)
  645. (set-exception-printer! 'syntax-error syntax-error-printer))
  646. ;;; {Defmacros}
  647. ;;;
  648. (define-syntax define-macro
  649. (lambda (x)
  650. "Define a defmacro."
  651. (syntax-case x ()
  652. ((_ (macro . args) doc body1 body ...)
  653. (string? (syntax->datum #'doc))
  654. #'(define-macro macro doc (lambda args body1 body ...)))
  655. ((_ (macro . args) body ...)
  656. #'(define-macro macro #f (lambda args body ...)))
  657. ((_ macro doc transformer)
  658. (or (string? (syntax->datum #'doc))
  659. (not (syntax->datum #'doc)))
  660. #'(define-syntax macro
  661. (lambda (y)
  662. doc
  663. #((macro-type . defmacro)
  664. (defmacro-args args))
  665. (syntax-case y ()
  666. ((_ . args)
  667. (let ((v (syntax->datum #'args)))
  668. (datum->syntax y (apply transformer v)))))))))))
  669. (define-syntax defmacro
  670. (lambda (x)
  671. "Define a defmacro, with the old lispy defun syntax."
  672. (syntax-case x ()
  673. ((_ macro args doc body1 body ...)
  674. (string? (syntax->datum #'doc))
  675. #'(define-macro macro doc (lambda args body1 body ...)))
  676. ((_ macro args body ...)
  677. #'(define-macro macro #f (lambda args body ...))))))
  678. (provide 'defmacro)
  679. ;;; {Deprecation}
  680. ;;;
  681. (define-syntax begin-deprecated
  682. (lambda (x)
  683. (syntax-case x ()
  684. ((_ form form* ...)
  685. (if (include-deprecated-features)
  686. #'(begin form form* ...)
  687. #'(begin))))))
  688. ;;; {Trivial Functions}
  689. ;;;
  690. (define (identity x) x)
  691. (define (compose proc . rest)
  692. "Compose PROC with the procedures in REST, such that the last one in
  693. REST is applied first and PROC last, and return the resulting procedure.
  694. The given procedures must have compatible arity."
  695. (if (null? rest)
  696. proc
  697. (let ((g (apply compose rest)))
  698. (lambda args
  699. (call-with-values (lambda () (apply g args)) proc)))))
  700. (define (negate proc)
  701. "Return a procedure with the same arity as PROC that returns the `not'
  702. of PROC's result."
  703. (lambda args
  704. (not (apply proc args))))
  705. (define (const value)
  706. "Return a procedure that accepts any number of arguments and returns
  707. VALUE."
  708. (lambda _
  709. value))
  710. (define (and=> value procedure) (and value (procedure value)))
  711. (define call/cc call-with-current-continuation)
  712. (define-syntax-rule (false-if-exception expr)
  713. (catch #t
  714. (lambda () expr)
  715. (lambda (k . args) #f)))
  716. ;;; {General Properties}
  717. ;;;
  718. ;; Properties are a lispy way to associate random info with random objects.
  719. ;; Traditionally properties are implemented as an alist or a plist actually
  720. ;; pertaining to the object in question.
  721. ;;
  722. ;; These "object properties" have the advantage that they can be associated with
  723. ;; any object, even if the object has no plist. Object properties are good when
  724. ;; you are extending pre-existing objects in unexpected ways. They also present
  725. ;; a pleasing, uniform procedure-with-setter interface. But if you have a data
  726. ;; type that always has properties, it's often still best to store those
  727. ;; properties within the object itself.
  728. (define (make-object-property)
  729. ;; Weak tables are thread-safe.
  730. (let ((prop (make-weak-key-hash-table)))
  731. (make-procedure-with-setter
  732. (lambda (obj) (hashq-ref prop obj))
  733. (lambda (obj val) (hashq-set! prop obj val)))))
  734. ;;; {Symbol Properties}
  735. ;;;
  736. ;;; Symbol properties are something you see in old Lisp code. In most current
  737. ;;; Guile code, symbols are not used as a data structure -- they are used as
  738. ;;; keys into other data structures.
  739. (define (symbol-property sym prop)
  740. (let ((pair (assoc prop (symbol-pref sym))))
  741. (and pair (cdr pair))))
  742. (define (set-symbol-property! sym prop val)
  743. (let ((pair (assoc prop (symbol-pref sym))))
  744. (if pair
  745. (set-cdr! pair val)
  746. (symbol-pset! sym (acons prop val (symbol-pref sym))))))
  747. (define (symbol-property-remove! sym prop)
  748. (let ((pair (assoc prop (symbol-pref sym))))
  749. (if pair
  750. (symbol-pset! sym (delq! pair (symbol-pref sym))))))
  751. ;;; {Arrays}
  752. ;;;
  753. (define (array-shape a)
  754. (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
  755. (array-dimensions a)))
  756. ;;; {Keywords}
  757. ;;;
  758. ;;; It's much better if you can use lambda* / define*, of course.
  759. (define (kw-arg-ref args kw)
  760. (let ((rem (member kw args)))
  761. (and rem (pair? (cdr rem)) (cadr rem))))
  762. ;;; {Structs}
  763. ;;;
  764. (define (struct-layout s)
  765. (struct-ref (struct-vtable s) vtable-index-layout))
  766. ;;; {Records}
  767. ;;;
  768. ;; Printing records: by default, records are printed as
  769. ;;
  770. ;; #<type-name field1: val1 field2: val2 ...>
  771. ;;
  772. ;; You can change that by giving a custom printing function to
  773. ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
  774. ;; will be called like
  775. ;;
  776. ;; (<printer> object port)
  777. ;;
  778. ;; It should print OBJECT to PORT.
  779. (define (inherit-print-state old-port new-port)
  780. (if (get-print-state old-port)
  781. (port-with-print-state new-port (get-print-state old-port))
  782. new-port))
  783. ;; 0: type-name, 1: fields, 2: constructor
  784. (define record-type-vtable
  785. ;; FIXME: This should just call make-vtable, not make-vtable-vtable; but for
  786. ;; that we need to expose the bare vtable-vtable to Scheme.
  787. (make-vtable-vtable "prprpw" 0
  788. (lambda (s p)
  789. (cond ((eq? s record-type-vtable)
  790. (display "#<record-type-vtable>" p))
  791. (else
  792. (display "#<record-type " p)
  793. (display (record-type-name s) p)
  794. (display ">" p))))))
  795. (define (record-type? obj)
  796. (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
  797. (define* (make-record-type type-name fields #:optional printer)
  798. ;; Pre-generate constructors for nfields < 20.
  799. (define-syntax make-constructor
  800. (lambda (x)
  801. (define *max-static-argument-count* 20)
  802. (define (make-formals n)
  803. (let lp ((i 0))
  804. (if (< i n)
  805. (cons (datum->syntax
  806. x
  807. (string->symbol
  808. (string (integer->char (+ (char->integer #\a) i)))))
  809. (lp (1+ i)))
  810. '())))
  811. (syntax-case x ()
  812. ((_ rtd exp) (not (identifier? #'exp))
  813. #'(let ((n exp))
  814. (make-constructor rtd n)))
  815. ((_ rtd nfields)
  816. #`(case nfields
  817. #,@(let lp ((n 0))
  818. (if (< n *max-static-argument-count*)
  819. (cons (with-syntax (((formal ...) (make-formals n))
  820. (n n))
  821. #'((n)
  822. (lambda (formal ...)
  823. (make-struct rtd 0 formal ...))))
  824. (lp (1+ n)))
  825. '()))
  826. (else
  827. (lambda args
  828. (if (= (length args) nfields)
  829. (apply make-struct rtd 0 args)
  830. (scm-error 'wrong-number-of-args
  831. (format #f "make-~a" type-name)
  832. "Wrong number of arguments" '() #f)))))))))
  833. (define (default-record-printer s p)
  834. (display "#<" p)
  835. (display (record-type-name (record-type-descriptor s)) p)
  836. (let loop ((fields (record-type-fields (record-type-descriptor s)))
  837. (off 0))
  838. (cond
  839. ((not (null? fields))
  840. (display " " p)
  841. (display (car fields) p)
  842. (display ": " p)
  843. (display (struct-ref s off) p)
  844. (loop (cdr fields) (+ 1 off)))))
  845. (display ">" p))
  846. (let ((rtd (make-struct record-type-vtable 0
  847. (make-struct-layout
  848. (apply string-append
  849. (map (lambda (f) "pw") fields)))
  850. (or printer default-record-printer)
  851. type-name
  852. (copy-tree fields))))
  853. (struct-set! rtd (+ vtable-offset-user 2)
  854. (make-constructor rtd (length fields)))
  855. ;; Temporary solution: Associate a name to the record type descriptor
  856. ;; so that the object system can create a wrapper class for it.
  857. (set-struct-vtable-name! rtd (if (symbol? type-name)
  858. type-name
  859. (string->symbol type-name)))
  860. rtd))
  861. (define (record-type-name obj)
  862. (if (record-type? obj)
  863. (struct-ref obj vtable-offset-user)
  864. (error 'not-a-record-type obj)))
  865. (define (record-type-fields obj)
  866. (if (record-type? obj)
  867. (struct-ref obj (+ 1 vtable-offset-user))
  868. (error 'not-a-record-type obj)))
  869. (define* (record-constructor rtd #:optional field-names)
  870. (if (not field-names)
  871. (struct-ref rtd (+ 2 vtable-offset-user))
  872. (primitive-eval
  873. `(lambda ,field-names
  874. (make-struct ',rtd 0 ,@(map (lambda (f)
  875. (if (memq f field-names)
  876. f
  877. #f))
  878. (record-type-fields rtd)))))))
  879. (define (record-predicate rtd)
  880. (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
  881. (define (%record-type-error rtd obj) ;; private helper
  882. (or (eq? rtd (record-type-descriptor obj))
  883. (scm-error 'wrong-type-arg "%record-type-check"
  884. "Wrong type record (want `~S'): ~S"
  885. (list (record-type-name rtd) obj)
  886. #f)))
  887. (define (record-accessor rtd field-name)
  888. (let ((pos (list-index (record-type-fields rtd) field-name)))
  889. (if (not pos)
  890. (error 'no-such-field field-name))
  891. (lambda (obj)
  892. (if (eq? (struct-vtable obj) rtd)
  893. (struct-ref obj pos)
  894. (%record-type-error rtd obj)))))
  895. (define (record-modifier rtd field-name)
  896. (let ((pos (list-index (record-type-fields rtd) field-name)))
  897. (if (not pos)
  898. (error 'no-such-field field-name))
  899. (lambda (obj val)
  900. (if (eq? (struct-vtable obj) rtd)
  901. (struct-set! obj pos val)
  902. (%record-type-error rtd obj)))))
  903. (define (record? obj)
  904. (and (struct? obj) (record-type? (struct-vtable obj))))
  905. (define (record-type-descriptor obj)
  906. (if (struct? obj)
  907. (struct-vtable obj)
  908. (error 'not-a-record obj)))
  909. (provide 'record)
  910. ;;; {Booleans}
  911. ;;;
  912. (define (->bool x) (not (not x)))
  913. ;;; {Symbols}
  914. ;;;
  915. (define (symbol-append . args)
  916. (string->symbol (apply string-append (map symbol->string args))))
  917. (define (list->symbol . args)
  918. (string->symbol (apply list->string args)))
  919. (define (symbol . args)
  920. (string->symbol (apply string args)))
  921. ;;; {Lists}
  922. ;;;
  923. (define (list-index l k)
  924. (let loop ((n 0)
  925. (l l))
  926. (and (not (null? l))
  927. (if (eq? (car l) k)
  928. n
  929. (loop (+ n 1) (cdr l))))))
  930. ;; Load `posix.scm' even when not (provided? 'posix) so that we get the
  931. ;; `stat' accessors.
  932. (primitive-load-path "ice-9/posix")
  933. (if (provided? 'socket)
  934. (primitive-load-path "ice-9/networking"))
  935. ;; For reference, Emacs file-exists-p uses stat in this same way.
  936. (define file-exists?
  937. (if (provided? 'posix)
  938. (lambda (str)
  939. (->bool (stat str #f)))
  940. (lambda (str)
  941. (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
  942. (lambda args #f))))
  943. (if port (begin (close-port port) #t)
  944. #f)))))
  945. (define file-is-directory?
  946. (if (provided? 'posix)
  947. (lambda (str)
  948. (eq? (stat:type (stat str)) 'directory))
  949. (lambda (str)
  950. (let ((port (catch 'system-error
  951. (lambda () (open-file (string-append str "/.")
  952. OPEN_READ))
  953. (lambda args #f))))
  954. (if port (begin (close-port port) #t)
  955. #f)))))
  956. (define (system-error-errno args)
  957. (if (eq? (car args) 'system-error)
  958. (car (list-ref args 4))
  959. #f))
  960. ;;; {Error Handling}
  961. ;;;
  962. (define error
  963. (case-lambda
  964. (()
  965. (scm-error 'misc-error #f "?" #f #f))
  966. ((message . args)
  967. (let ((msg (string-join (cons "~A" (make-list (length args) "~S")))))
  968. (scm-error 'misc-error #f msg (cons message args) #f)))))
  969. ;;; {Time Structures}
  970. ;;;
  971. (define (tm:sec obj) (vector-ref obj 0))
  972. (define (tm:min obj) (vector-ref obj 1))
  973. (define (tm:hour obj) (vector-ref obj 2))
  974. (define (tm:mday obj) (vector-ref obj 3))
  975. (define (tm:mon obj) (vector-ref obj 4))
  976. (define (tm:year obj) (vector-ref obj 5))
  977. (define (tm:wday obj) (vector-ref obj 6))
  978. (define (tm:yday obj) (vector-ref obj 7))
  979. (define (tm:isdst obj) (vector-ref obj 8))
  980. (define (tm:gmtoff obj) (vector-ref obj 9))
  981. (define (tm:zone obj) (vector-ref obj 10))
  982. (define (set-tm:sec obj val) (vector-set! obj 0 val))
  983. (define (set-tm:min obj val) (vector-set! obj 1 val))
  984. (define (set-tm:hour obj val) (vector-set! obj 2 val))
  985. (define (set-tm:mday obj val) (vector-set! obj 3 val))
  986. (define (set-tm:mon obj val) (vector-set! obj 4 val))
  987. (define (set-tm:year obj val) (vector-set! obj 5 val))
  988. (define (set-tm:wday obj val) (vector-set! obj 6 val))
  989. (define (set-tm:yday obj val) (vector-set! obj 7 val))
  990. (define (set-tm:isdst obj val) (vector-set! obj 8 val))
  991. (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
  992. (define (set-tm:zone obj val) (vector-set! obj 10 val))
  993. (define (tms:clock obj) (vector-ref obj 0))
  994. (define (tms:utime obj) (vector-ref obj 1))
  995. (define (tms:stime obj) (vector-ref obj 2))
  996. (define (tms:cutime obj) (vector-ref obj 3))
  997. (define (tms:cstime obj) (vector-ref obj 4))
  998. ;;; {File Descriptors and Ports}
  999. ;;;
  1000. (define file-position ftell)
  1001. (define* (file-set-position port offset #:optional (whence SEEK_SET))
  1002. (seek port offset whence))
  1003. (define (move->fdes fd/port fd)
  1004. (cond ((integer? fd/port)
  1005. (dup->fdes fd/port fd)
  1006. (close fd/port)
  1007. fd)
  1008. (else
  1009. (primitive-move->fdes fd/port fd)
  1010. (set-port-revealed! fd/port 1)
  1011. fd/port)))
  1012. (define (release-port-handle port)
  1013. (let ((revealed (port-revealed port)))
  1014. (if (> revealed 0)
  1015. (set-port-revealed! port (- revealed 1)))))
  1016. (define dup->port
  1017. (case-lambda
  1018. ((port/fd mode)
  1019. (fdopen (dup->fdes port/fd) mode))
  1020. ((port/fd mode new-fd)
  1021. (let ((port (fdopen (dup->fdes port/fd new-fd) mode)))
  1022. (set-port-revealed! port 1)
  1023. port))))
  1024. (define dup->inport
  1025. (case-lambda
  1026. ((port/fd)
  1027. (dup->port port/fd "r"))
  1028. ((port/fd new-fd)
  1029. (dup->port port/fd "r" new-fd))))
  1030. (define dup->outport
  1031. (case-lambda
  1032. ((port/fd)
  1033. (dup->port port/fd "w"))
  1034. ((port/fd new-fd)
  1035. (dup->port port/fd "w" new-fd))))
  1036. (define dup
  1037. (case-lambda
  1038. ((port/fd)
  1039. (if (integer? port/fd)
  1040. (dup->fdes port/fd)
  1041. (dup->port port/fd (port-mode port/fd))))
  1042. ((port/fd new-fd)
  1043. (if (integer? port/fd)
  1044. (dup->fdes port/fd new-fd)
  1045. (dup->port port/fd (port-mode port/fd) new-fd)))))
  1046. (define (duplicate-port port modes)
  1047. (dup->port port modes))
  1048. (define (fdes->inport fdes)
  1049. (let loop ((rest-ports (fdes->ports fdes)))
  1050. (cond ((null? rest-ports)
  1051. (let ((result (fdopen fdes "r")))
  1052. (set-port-revealed! result 1)
  1053. result))
  1054. ((input-port? (car rest-ports))
  1055. (set-port-revealed! (car rest-ports)
  1056. (+ (port-revealed (car rest-ports)) 1))
  1057. (car rest-ports))
  1058. (else
  1059. (loop (cdr rest-ports))))))
  1060. (define (fdes->outport fdes)
  1061. (let loop ((rest-ports (fdes->ports fdes)))
  1062. (cond ((null? rest-ports)
  1063. (let ((result (fdopen fdes "w")))
  1064. (set-port-revealed! result 1)
  1065. result))
  1066. ((output-port? (car rest-ports))
  1067. (set-port-revealed! (car rest-ports)
  1068. (+ (port-revealed (car rest-ports)) 1))
  1069. (car rest-ports))
  1070. (else
  1071. (loop (cdr rest-ports))))))
  1072. (define (port->fdes port)
  1073. (set-port-revealed! port (+ (port-revealed port) 1))
  1074. (fileno port))
  1075. (define (setenv name value)
  1076. (if value
  1077. (putenv (string-append name "=" value))
  1078. (putenv name)))
  1079. (define (unsetenv name)
  1080. "Remove the entry for NAME from the environment."
  1081. (putenv name))
  1082. ;;; {Load Paths}
  1083. ;;;
  1084. (define (in-vicinity vicinity file)
  1085. (let ((tail (let ((len (string-length vicinity)))
  1086. (if (zero? len)
  1087. #f
  1088. (string-ref vicinity (- len 1))))))
  1089. (string-append vicinity
  1090. (if (or (not tail)
  1091. (eq? tail #\/))
  1092. ""
  1093. "/")
  1094. file)))
  1095. ;;; {Help for scm_shell}
  1096. ;;;
  1097. ;;; The argument-processing code used by Guile-based shells generates
  1098. ;;; Scheme code based on the argument list. This page contains help
  1099. ;;; functions for the code it generates.
  1100. ;;;
  1101. (define (command-line) (program-arguments))
  1102. ;; This is mostly for the internal use of the code generated by
  1103. ;; scm_compile_shell_switches.
  1104. (define (load-user-init)
  1105. (let* ((home (or (getenv "HOME")
  1106. (false-if-exception (passwd:dir (getpwuid (getuid))))
  1107. "/")) ;; fallback for cygwin etc.
  1108. (init-file (in-vicinity home ".guile")))
  1109. (if (file-exists? init-file)
  1110. (primitive-load init-file))))
  1111. ;;; {The interpreter stack}
  1112. ;;;
  1113. ;; %stacks defined in stacks.c
  1114. (define (%start-stack tag thunk)
  1115. (let ((prompt-tag (make-prompt-tag "start-stack")))
  1116. (call-with-prompt
  1117. prompt-tag
  1118. (lambda ()
  1119. (with-fluids ((%stacks (acons tag prompt-tag
  1120. (or (fluid-ref %stacks) '()))))
  1121. (thunk)))
  1122. (lambda (k . args)
  1123. (%start-stack tag (lambda () (apply k args)))))))
  1124. (define-syntax-rule (start-stack tag exp)
  1125. (%start-stack tag (lambda () exp)))
  1126. ;;; {Loading by paths}
  1127. ;;;
  1128. ;;; Load a Scheme source file named NAME, searching for it in the
  1129. ;;; directories listed in %load-path, and applying each of the file
  1130. ;;; name extensions listed in %load-extensions.
  1131. (define (load-from-path name)
  1132. (start-stack 'load-stack
  1133. (primitive-load-path name)))
  1134. (define %load-verbosely #f)
  1135. (define (assert-load-verbosity v) (set! %load-verbosely v))
  1136. (define (%load-announce file)
  1137. (if %load-verbosely
  1138. (with-output-to-port (current-error-port)
  1139. (lambda ()
  1140. (display ";;; ")
  1141. (display "loading ")
  1142. (display file)
  1143. (newline)
  1144. (force-output)))))
  1145. (set! %load-hook %load-announce)
  1146. ;;; {Reader Extensions}
  1147. ;;;
  1148. ;;; Reader code for various "#c" forms.
  1149. ;;;
  1150. (define read-eval? (make-fluid))
  1151. (fluid-set! read-eval? #f)
  1152. (read-hash-extend #\.
  1153. (lambda (c port)
  1154. (if (fluid-ref read-eval?)
  1155. (eval (read port) (interaction-environment))
  1156. (error
  1157. "#. read expansion found and read-eval? is #f."))))
  1158. ;;; {Low Level Modules}
  1159. ;;;
  1160. ;;; These are the low level data structures for modules.
  1161. ;;;
  1162. ;;; Every module object is of the type 'module-type', which is a record
  1163. ;;; consisting of the following members:
  1164. ;;;
  1165. ;;; - eval-closure: the function that defines for its module the strategy that
  1166. ;;; shall be followed when looking up symbols in the module.
  1167. ;;;
  1168. ;;; An eval-closure is a function taking two arguments: the symbol to be
  1169. ;;; looked up and a boolean value telling whether a binding for the symbol
  1170. ;;; should be created if it does not exist yet. If the symbol lookup
  1171. ;;; succeeded (either because an existing binding was found or because a new
  1172. ;;; binding was created), a variable object representing the binding is
  1173. ;;; returned. Otherwise, the value #f is returned. Note that the eval
  1174. ;;; closure does not take the module to be searched as an argument: During
  1175. ;;; construction of the eval-closure, the eval-closure has to store the
  1176. ;;; module it belongs to in its environment. This means, that any
  1177. ;;; eval-closure can belong to only one module.
  1178. ;;;
  1179. ;;; The eval-closure of a module can be defined arbitrarily. However, three
  1180. ;;; special cases of eval-closures are to be distinguished: During startup
  1181. ;;; the module system is not yet activated. In this phase, no modules are
  1182. ;;; defined and all bindings are automatically stored by the system in the
  1183. ;;; pre-modules-obarray. Since no eval-closures exist at this time, the
  1184. ;;; functions which require an eval-closure as their argument need to be
  1185. ;;; passed the value #f.
  1186. ;;;
  1187. ;;; The other two special cases of eval-closures are the
  1188. ;;; standard-eval-closure and the standard-interface-eval-closure. Both
  1189. ;;; behave equally for the case that no new binding is to be created. The
  1190. ;;; difference between the two comes in, when the boolean argument to the
  1191. ;;; eval-closure indicates that a new binding shall be created if it is not
  1192. ;;; found.
  1193. ;;;
  1194. ;;; Given that no new binding shall be created, both standard eval-closures
  1195. ;;; define the following standard strategy of searching bindings in the
  1196. ;;; module: First, the module's obarray is searched for the symbol. Second,
  1197. ;;; if no binding for the symbol was found in the module's obarray, the
  1198. ;;; module's binder procedure is exececuted. If this procedure did not
  1199. ;;; return a binding for the symbol, the modules referenced in the module's
  1200. ;;; uses list are recursively searched for a binding of the symbol. If the
  1201. ;;; binding can not be found in these modules also, the symbol lookup has
  1202. ;;; failed.
  1203. ;;;
  1204. ;;; If a new binding shall be created, the standard-interface-eval-closure
  1205. ;;; immediately returns indicating failure. That is, it does not even try
  1206. ;;; to look up the symbol. In contrast, the standard-eval-closure would
  1207. ;;; first search the obarray, and if no binding was found there, would
  1208. ;;; create a new binding in the obarray, therefore not calling the binder
  1209. ;;; procedure or searching the modules in the uses list.
  1210. ;;;
  1211. ;;; The explanation of the following members obarray, binder and uses
  1212. ;;; assumes that the symbol lookup follows the strategy that is defined in
  1213. ;;; the standard-eval-closure and the standard-interface-eval-closure.
  1214. ;;;
  1215. ;;; - obarray: a hash table that maps symbols to variable objects. In this
  1216. ;;; hash table, the definitions are found that are local to the module (that
  1217. ;;; is, not imported from other modules). When looking up bindings in the
  1218. ;;; module, this hash table is searched first.
  1219. ;;;
  1220. ;;; - binder: either #f or a function taking a module and a symbol argument.
  1221. ;;; If it is a function it is called after the obarray has been
  1222. ;;; unsuccessfully searched for a binding. It then can provide bindings
  1223. ;;; that would otherwise not be found locally in the module.
  1224. ;;;
  1225. ;;; - uses: a list of modules from which non-local bindings can be inherited.
  1226. ;;; These modules are the third place queried for bindings after the obarray
  1227. ;;; has been unsuccessfully searched and the binder function did not deliver
  1228. ;;; a result either.
  1229. ;;;
  1230. ;;; - transformer: either #f or a function taking a scheme expression as
  1231. ;;; delivered by read. If it is a function, it will be called to perform
  1232. ;;; syntax transformations (e. g. makro expansion) on the given scheme
  1233. ;;; expression. The output of the transformer function will then be passed
  1234. ;;; to Guile's internal memoizer. This means that the output must be valid
  1235. ;;; scheme code. The only exception is, that the output may make use of the
  1236. ;;; syntax extensions provided to identify the modules that a binding
  1237. ;;; belongs to.
  1238. ;;;
  1239. ;;; - name: the name of the module. This is used for all kinds of printing
  1240. ;;; outputs. In certain places the module name also serves as a way of
  1241. ;;; identification. When adding a module to the uses list of another
  1242. ;;; module, it is made sure that the new uses list will not contain two
  1243. ;;; modules of the same name.
  1244. ;;;
  1245. ;;; - kind: classification of the kind of module. The value is (currently?)
  1246. ;;; only used for printing. It has no influence on how a module is treated.
  1247. ;;; Currently the following values are used when setting the module kind:
  1248. ;;; 'module, 'directory, 'interface, 'custom-interface. If no explicit kind
  1249. ;;; is set, it defaults to 'module.
  1250. ;;;
  1251. ;;; - duplicates-handlers: a list of procedures that get called to make a
  1252. ;;; choice between two duplicate bindings when name clashes occur. See the
  1253. ;;; `duplicate-handlers' global variable below.
  1254. ;;;
  1255. ;;; - observers: a list of procedures that get called when the module is
  1256. ;;; modified.
  1257. ;;;
  1258. ;;; - weak-observers: a weak-key hash table of procedures that get called
  1259. ;;; when the module is modified. See `module-observe-weak' for details.
  1260. ;;;
  1261. ;;; In addition, the module may (must?) contain a binding for
  1262. ;;; `%module-public-interface'. This variable should be bound to a module
  1263. ;;; representing the exported interface of a module. See the
  1264. ;;; `module-public-interface' and `module-export!' procedures.
  1265. ;;;
  1266. ;;; !!! warning: The interface to lazy binder procedures is going
  1267. ;;; to be changed in an incompatible way to permit all the basic
  1268. ;;; module ops to be virtualized.
  1269. ;;;
  1270. ;;; (make-module size use-list lazy-binding-proc) => module
  1271. ;;; module-{obarray,uses,binder}[|-set!]
  1272. ;;; (module? obj) => [#t|#f]
  1273. ;;; (module-locally-bound? module symbol) => [#t|#f]
  1274. ;;; (module-bound? module symbol) => [#t|#f]
  1275. ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
  1276. ;;; (module-symbol-interned? module symbol) => [#t|#f]
  1277. ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
  1278. ;;; (module-variable module symbol) => [#<variable ...> | #f]
  1279. ;;; (module-symbol-binding module symbol opt-value)
  1280. ;;; => [ <obj> | opt-value | an error occurs ]
  1281. ;;; (module-make-local-var! module symbol) => #<variable...>
  1282. ;;; (module-add! module symbol var) => unspecified
  1283. ;;; (module-remove! module symbol) => unspecified
  1284. ;;; (module-for-each proc module) => unspecified
  1285. ;;; (make-scm-module) => module ; a lazy copy of the symhash module
  1286. ;;; (set-current-module module) => unspecified
  1287. ;;; (current-module) => #<module...>
  1288. ;;;
  1289. ;;;
  1290. ;;; {Printing Modules}
  1291. ;;;
  1292. ;; This is how modules are printed. You can re-define it.
  1293. (define (%print-module mod port)
  1294. (display "#<" port)
  1295. (display (or (module-kind mod) "module") port)
  1296. (display " " port)
  1297. (display (module-name mod) port)
  1298. (display " " port)
  1299. (display (number->string (object-address mod) 16) port)
  1300. (display ">" port))
  1301. (letrec-syntax
  1302. ;; Locally extend the syntax to allow record accessors to be defined at
  1303. ;; compile-time. Cache the rtd locally to the constructor, the getters and
  1304. ;; the setters, in order to allow for redefinition of the record type; not
  1305. ;; relevant in the case of modules, but perhaps if we make this public, it
  1306. ;; could matter.
  1307. ((define-record-type
  1308. (lambda (x)
  1309. (define (make-id scope . fragments)
  1310. (datum->syntax scope
  1311. (apply symbol-append
  1312. (map (lambda (x)
  1313. (if (symbol? x) x (syntax->datum x)))
  1314. fragments))))
  1315. (define (getter rtd type-name field slot)
  1316. #`(define #,(make-id rtd type-name '- field)
  1317. (let ((rtd #,rtd))
  1318. (lambda (#,type-name)
  1319. (if (eq? (struct-vtable #,type-name) rtd)
  1320. (struct-ref #,type-name #,slot)
  1321. (%record-type-error rtd #,type-name))))))
  1322. (define (setter rtd type-name field slot)
  1323. #`(define #,(make-id rtd 'set- type-name '- field '!)
  1324. (let ((rtd #,rtd))
  1325. (lambda (#,type-name val)
  1326. (if (eq? (struct-vtable #,type-name) rtd)
  1327. (struct-set! #,type-name #,slot val)
  1328. (%record-type-error rtd #,type-name))))))
  1329. (define (accessors rtd type-name fields n exp)
  1330. (syntax-case fields ()
  1331. (() exp)
  1332. (((field #:no-accessors) field* ...) (identifier? #'field)
  1333. (accessors rtd type-name #'(field* ...) (1+ n)
  1334. exp))
  1335. (((field #:no-setter) field* ...) (identifier? #'field)
  1336. (accessors rtd type-name #'(field* ...) (1+ n)
  1337. #`(begin #,exp
  1338. #,(getter rtd type-name #'field n))))
  1339. (((field #:no-getter) field* ...) (identifier? #'field)
  1340. (accessors rtd type-name #'(field* ...) (1+ n)
  1341. #`(begin #,exp
  1342. #,(setter rtd type-name #'field n))))
  1343. ((field field* ...) (identifier? #'field)
  1344. (accessors rtd type-name #'(field* ...) (1+ n)
  1345. #`(begin #,exp
  1346. #,(getter rtd type-name #'field n)
  1347. #,(setter rtd type-name #'field n))))))
  1348. (define (predicate rtd type-name fields exp)
  1349. (accessors
  1350. rtd type-name fields 0
  1351. #`(begin
  1352. #,exp
  1353. (define (#,(make-id rtd type-name '?) obj)
  1354. (and (struct? obj) (eq? (struct-vtable obj) #,rtd))))))
  1355. (define (field-list fields)
  1356. (syntax-case fields ()
  1357. (() '())
  1358. (((f . opts) . rest) (identifier? #'f)
  1359. (cons #'f (field-list #'rest)))
  1360. ((f . rest) (identifier? #'f)
  1361. (cons #'f (field-list #'rest)))))
  1362. (define (constructor rtd type-name fields exp)
  1363. (let ((ctor (make-id rtd type-name '-constructor))
  1364. (args (field-list fields)))
  1365. (predicate rtd type-name fields
  1366. #`(begin #,exp
  1367. (define #,ctor
  1368. (let ((rtd #,rtd))
  1369. (lambda #,args
  1370. (make-struct rtd 0 #,@args))))
  1371. (struct-set! #,rtd (+ vtable-offset-user 2)
  1372. #,ctor)))))
  1373. (define (type type-name printer fields)
  1374. (define (make-layout)
  1375. (let lp ((fields fields) (slots '()))
  1376. (syntax-case fields ()
  1377. (() (datum->syntax #'here
  1378. (make-struct-layout
  1379. (apply string-append slots))))
  1380. ((_ . rest) (lp #'rest (cons "pw" slots))))))
  1381. (let ((rtd (make-id type-name type-name '-type)))
  1382. (constructor rtd type-name fields
  1383. #`(begin
  1384. (define #,rtd
  1385. (make-struct record-type-vtable 0
  1386. '#,(make-layout)
  1387. #,printer
  1388. '#,type-name
  1389. '#,(field-list fields)))
  1390. (set-struct-vtable-name! #,rtd '#,type-name)))))
  1391. (syntax-case x ()
  1392. ((_ type-name printer (field ...))
  1393. (type #'type-name #'printer #'(field ...)))))))
  1394. ;; module-type
  1395. ;;
  1396. ;; A module is characterized by an obarray in which local symbols
  1397. ;; are interned, a list of modules, "uses", from which non-local
  1398. ;; bindings can be inherited, and an optional lazy-binder which
  1399. ;; is a (CLOSURE module symbol) which, as a last resort, can provide
  1400. ;; bindings that would otherwise not be found locally in the module.
  1401. ;;
  1402. ;; NOTE: If you change the set of fields or their order, you also need to
  1403. ;; change the constants in libguile/modules.h.
  1404. ;;
  1405. ;; NOTE: The getter `module-eval-closure' is used in libguile/modules.c.
  1406. ;; NOTE: The getter `module-transfomer' is defined libguile/modules.c.
  1407. ;; NOTE: The getter `module-name' is defined later, due to boot reasons.
  1408. ;; NOTE: The getter `module-public-interface' is used in libguile/modules.c.
  1409. ;;
  1410. (define-record-type module
  1411. (lambda (obj port) (%print-module obj port))
  1412. (obarray
  1413. uses
  1414. binder
  1415. eval-closure
  1416. (transformer #:no-getter)
  1417. (name #:no-getter)
  1418. kind
  1419. duplicates-handlers
  1420. (import-obarray #:no-setter)
  1421. observers
  1422. (weak-observers #:no-setter)
  1423. version
  1424. submodules
  1425. submodule-binder
  1426. public-interface
  1427. filename)))
  1428. ;; make-module &opt size uses binder
  1429. ;;
  1430. ;; Create a new module, perhaps with a particular size of obarray,
  1431. ;; initial uses list, or binding procedure.
  1432. ;;
  1433. (define* (make-module #:optional (size 31) (uses '()) (binder #f))
  1434. (define %default-import-size
  1435. ;; Typical number of imported bindings actually used by a module.
  1436. 600)
  1437. (if (not (integer? size))
  1438. (error "Illegal size to make-module." size))
  1439. (if (not (and (list? uses)
  1440. (and-map module? uses)))
  1441. (error "Incorrect use list." uses))
  1442. (if (and binder (not (procedure? binder)))
  1443. (error
  1444. "Lazy-binder expected to be a procedure or #f." binder))
  1445. (let ((module (module-constructor (make-hash-table size)
  1446. uses binder #f macroexpand
  1447. #f #f #f
  1448. (make-hash-table %default-import-size)
  1449. '()
  1450. (make-weak-key-hash-table 31) #f
  1451. (make-hash-table 7) #f #f #f)))
  1452. ;; We can't pass this as an argument to module-constructor,
  1453. ;; because we need it to close over a pointer to the module
  1454. ;; itself.
  1455. (set-module-eval-closure! module (standard-eval-closure module))
  1456. module))
  1457. ;;; {Observer protocol}
  1458. ;;;
  1459. (define (module-observe module proc)
  1460. (set-module-observers! module (cons proc (module-observers module)))
  1461. (cons module proc))
  1462. (define* (module-observe-weak module observer-id #:optional (proc observer-id))
  1463. ;; Register PROC as an observer of MODULE under name OBSERVER-ID (which can
  1464. ;; be any Scheme object). PROC is invoked and passed MODULE any time
  1465. ;; MODULE is modified. PROC gets unregistered when OBSERVER-ID gets GC'd
  1466. ;; (thus, it is never unregistered if OBSERVER-ID is an immediate value,
  1467. ;; for instance).
  1468. ;; The two-argument version is kept for backward compatibility: when called
  1469. ;; with two arguments, the observer gets unregistered when closure PROC
  1470. ;; gets GC'd (making it impossible to use an anonymous lambda for PROC).
  1471. (hashq-set! (module-weak-observers module) observer-id proc))
  1472. (define (module-unobserve token)
  1473. (let ((module (car token))
  1474. (id (cdr token)))
  1475. (if (integer? id)
  1476. (hash-remove! (module-weak-observers module) id)
  1477. (set-module-observers! module (delq1! id (module-observers module)))))
  1478. *unspecified*)
  1479. (define module-defer-observers #f)
  1480. (define module-defer-observers-mutex (make-mutex 'recursive))
  1481. (define module-defer-observers-table (make-hash-table))
  1482. (define (module-modified m)
  1483. (if module-defer-observers
  1484. (hash-set! module-defer-observers-table m #t)
  1485. (module-call-observers m)))
  1486. ;;; This function can be used to delay calls to observers so that they
  1487. ;;; can be called once only in the face of massive updating of modules.
  1488. ;;;
  1489. (define (call-with-deferred-observers thunk)
  1490. (dynamic-wind
  1491. (lambda ()
  1492. (lock-mutex module-defer-observers-mutex)
  1493. (set! module-defer-observers #t))
  1494. thunk
  1495. (lambda ()
  1496. (set! module-defer-observers #f)
  1497. (hash-for-each (lambda (m dummy)
  1498. (module-call-observers m))
  1499. module-defer-observers-table)
  1500. (hash-clear! module-defer-observers-table)
  1501. (unlock-mutex module-defer-observers-mutex))))
  1502. (define (module-call-observers m)
  1503. (for-each (lambda (proc) (proc m)) (module-observers m))
  1504. ;; We assume that weak observers don't (un)register themselves as they are
  1505. ;; called since this would preclude proper iteration over the hash table
  1506. ;; elements.
  1507. (hash-for-each (lambda (id proc) (proc m)) (module-weak-observers m)))
  1508. ;;; {Module Searching in General}
  1509. ;;;
  1510. ;;; We sometimes want to look for properties of a symbol
  1511. ;;; just within the obarray of one module. If the property
  1512. ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
  1513. ;;; DISPLAY is locally rebound in the module `safe-guile'.''
  1514. ;;;
  1515. ;;;
  1516. ;;; Other times, we want to test for a symbol property in the obarray
  1517. ;;; of M and, if it is not found there, try each of the modules in the
  1518. ;;; uses list of M. This is the normal way of testing for some
  1519. ;;; property, so we state these properties without qualification as
  1520. ;;; in: ``The symbol 'fnord is interned in module M because it is
  1521. ;;; interned locally in module M2 which is a member of the uses list
  1522. ;;; of M.''
  1523. ;;;
  1524. ;; module-search fn m
  1525. ;;
  1526. ;; return the first non-#f result of FN applied to M and then to
  1527. ;; the modules in the uses of m, and so on recursively. If all applications
  1528. ;; return #f, then so does this function.
  1529. ;;
  1530. (define (module-search fn m v)
  1531. (define (loop pos)
  1532. (and (pair? pos)
  1533. (or (module-search fn (car pos) v)
  1534. (loop (cdr pos)))))
  1535. (or (fn m v)
  1536. (loop (module-uses m))))
  1537. ;;; {Is a symbol bound in a module?}
  1538. ;;;
  1539. ;;; Symbol S in Module M is bound if S is interned in M and if the binding
  1540. ;;; of S in M has been set to some well-defined value.
  1541. ;;;
  1542. ;; module-locally-bound? module symbol
  1543. ;;
  1544. ;; Is a symbol bound (interned and defined) locally in a given module?
  1545. ;;
  1546. (define (module-locally-bound? m v)
  1547. (let ((var (module-local-variable m v)))
  1548. (and var
  1549. (variable-bound? var))))
  1550. ;; module-bound? module symbol
  1551. ;;
  1552. ;; Is a symbol bound (interned and defined) anywhere in a given module
  1553. ;; or its uses?
  1554. ;;
  1555. (define (module-bound? m v)
  1556. (let ((var (module-variable m v)))
  1557. (and var
  1558. (variable-bound? var))))
  1559. ;;; {Is a symbol interned in a module?}
  1560. ;;;
  1561. ;;; Symbol S in Module M is interned if S occurs in
  1562. ;;; of S in M has been set to some well-defined value.
  1563. ;;;
  1564. ;;; It is possible to intern a symbol in a module without providing
  1565. ;;; an initial binding for the corresponding variable. This is done
  1566. ;;; with:
  1567. ;;; (module-add! module symbol (make-undefined-variable))
  1568. ;;;
  1569. ;;; In that case, the symbol is interned in the module, but not
  1570. ;;; bound there. The unbound symbol shadows any binding for that
  1571. ;;; symbol that might otherwise be inherited from a member of the uses list.
  1572. ;;;
  1573. (define (module-obarray-get-handle ob key)
  1574. ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
  1575. (define (module-obarray-ref ob key)
  1576. ((if (symbol? key) hashq-ref hash-ref) ob key))
  1577. (define (module-obarray-set! ob key val)
  1578. ((if (symbol? key) hashq-set! hash-set!) ob key val))
  1579. (define (module-obarray-remove! ob key)
  1580. ((if (symbol? key) hashq-remove! hash-remove!) ob key))
  1581. ;; module-symbol-locally-interned? module symbol
  1582. ;;
  1583. ;; is a symbol interned (not neccessarily defined) locally in a given module
  1584. ;; or its uses? Interned symbols shadow inherited bindings even if
  1585. ;; they are not themselves bound to a defined value.
  1586. ;;
  1587. (define (module-symbol-locally-interned? m v)
  1588. (not (not (module-obarray-get-handle (module-obarray m) v))))
  1589. ;; module-symbol-interned? module symbol
  1590. ;;
  1591. ;; is a symbol interned (not neccessarily defined) anywhere in a given module
  1592. ;; or its uses? Interned symbols shadow inherited bindings even if
  1593. ;; they are not themselves bound to a defined value.
  1594. ;;
  1595. (define (module-symbol-interned? m v)
  1596. (module-search module-symbol-locally-interned? m v))
  1597. ;;; {Mapping modules x symbols --> variables}
  1598. ;;;
  1599. ;; module-local-variable module symbol
  1600. ;; return the local variable associated with a MODULE and SYMBOL.
  1601. ;;
  1602. ;;; This function is very important. It is the only function that can
  1603. ;;; return a variable from a module other than the mutators that store
  1604. ;;; new variables in modules. Therefore, this function is the location
  1605. ;;; of the "lazy binder" hack.
  1606. ;;;
  1607. ;;; If symbol is defined in MODULE, and if the definition binds symbol
  1608. ;;; to a variable, return that variable object.
  1609. ;;;
  1610. ;;; If the symbols is not found at first, but the module has a lazy binder,
  1611. ;;; then try the binder.
  1612. ;;;
  1613. ;;; If the symbol is not found at all, return #f.
  1614. ;;;
  1615. ;;; (This is now written in C, see `modules.c'.)
  1616. ;;;
  1617. ;;; {Mapping modules x symbols --> bindings}
  1618. ;;;
  1619. ;;; These are similar to the mapping to variables, except that the
  1620. ;;; variable is dereferenced.
  1621. ;;;
  1622. ;; module-symbol-binding module symbol opt-value
  1623. ;;
  1624. ;; return the binding of a variable specified by name within
  1625. ;; a given module, signalling an error if the variable is unbound.
  1626. ;; If the OPT-VALUE is passed, then instead of signalling an error,
  1627. ;; return OPT-VALUE.
  1628. ;;
  1629. (define (module-symbol-local-binding m v . opt-val)
  1630. (let ((var (module-local-variable m v)))
  1631. (if (and var (variable-bound? var))
  1632. (variable-ref var)
  1633. (if (not (null? opt-val))
  1634. (car opt-val)
  1635. (error "Locally unbound variable." v)))))
  1636. ;; module-symbol-binding module symbol opt-value
  1637. ;;
  1638. ;; return the binding of a variable specified by name within
  1639. ;; a given module, signalling an error if the variable is unbound.
  1640. ;; If the OPT-VALUE is passed, then instead of signalling an error,
  1641. ;; return OPT-VALUE.
  1642. ;;
  1643. (define (module-symbol-binding m v . opt-val)
  1644. (let ((var (module-variable m v)))
  1645. (if (and var (variable-bound? var))
  1646. (variable-ref var)
  1647. (if (not (null? opt-val))
  1648. (car opt-val)
  1649. (error "Unbound variable." v)))))
  1650. ;;; {Adding Variables to Modules}
  1651. ;;;
  1652. ;; module-make-local-var! module symbol
  1653. ;;
  1654. ;; ensure a variable for V in the local namespace of M.
  1655. ;; If no variable was already there, then create a new and uninitialzied
  1656. ;; variable.
  1657. ;;
  1658. ;; This function is used in modules.c.
  1659. ;;
  1660. (define (module-make-local-var! m v)
  1661. (or (let ((b (module-obarray-ref (module-obarray m) v)))
  1662. (and (variable? b)
  1663. (begin
  1664. ;; Mark as modified since this function is called when
  1665. ;; the standard eval closure defines a binding
  1666. (module-modified m)
  1667. b)))
  1668. ;; Create a new local variable.
  1669. (let ((local-var (make-undefined-variable)))
  1670. (module-add! m v local-var)
  1671. local-var)))
  1672. ;; module-ensure-local-variable! module symbol
  1673. ;;
  1674. ;; Ensure that there is a local variable in MODULE for SYMBOL. If
  1675. ;; there is no binding for SYMBOL, create a new uninitialized
  1676. ;; variable. Return the local variable.
  1677. ;;
  1678. (define (module-ensure-local-variable! module symbol)
  1679. (or (module-local-variable module symbol)
  1680. (let ((var (make-undefined-variable)))
  1681. (module-add! module symbol var)
  1682. var)))
  1683. ;; module-add! module symbol var
  1684. ;;
  1685. ;; ensure a particular variable for V in the local namespace of M.
  1686. ;;
  1687. (define (module-add! m v var)
  1688. (if (not (variable? var))
  1689. (error "Bad variable to module-add!" var))
  1690. (module-obarray-set! (module-obarray m) v var)
  1691. (module-modified m))
  1692. ;; module-remove!
  1693. ;;
  1694. ;; make sure that a symbol is undefined in the local namespace of M.
  1695. ;;
  1696. (define (module-remove! m v)
  1697. (module-obarray-remove! (module-obarray m) v)
  1698. (module-modified m))
  1699. (define (module-clear! m)
  1700. (hash-clear! (module-obarray m))
  1701. (module-modified m))
  1702. ;; MODULE-FOR-EACH -- exported
  1703. ;;
  1704. ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
  1705. ;;
  1706. (define (module-for-each proc module)
  1707. (hash-for-each proc (module-obarray module)))
  1708. (define (module-map proc module)
  1709. (hash-map->list proc (module-obarray module)))
  1710. ;; Submodules
  1711. ;;
  1712. ;; Modules exist in a separate namespace from values, because you generally do
  1713. ;; not want the name of a submodule, which you might not even use, to collide
  1714. ;; with local variables that happen to be named the same as the submodule.
  1715. ;;
  1716. (define (module-ref-submodule module name)
  1717. (or (hashq-ref (module-submodules module) name)
  1718. (and (module-submodule-binder module)
  1719. ((module-submodule-binder module) module name))))
  1720. (define (module-define-submodule! module name submodule)
  1721. (hashq-set! (module-submodules module) name submodule))
  1722. ;;; {Module-based Loading}
  1723. ;;;
  1724. (define (save-module-excursion thunk)
  1725. (let ((inner-module (current-module))
  1726. (outer-module #f))
  1727. (dynamic-wind (lambda ()
  1728. (set! outer-module (current-module))
  1729. (set-current-module inner-module)
  1730. (set! inner-module #f))
  1731. thunk
  1732. (lambda ()
  1733. (set! inner-module (current-module))
  1734. (set-current-module outer-module)
  1735. (set! outer-module #f)))))
  1736. ;;; {MODULE-REF -- exported}
  1737. ;;;
  1738. ;; Returns the value of a variable called NAME in MODULE or any of its
  1739. ;; used modules. If there is no such variable, then if the optional third
  1740. ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
  1741. ;;
  1742. (define (module-ref module name . rest)
  1743. (let ((variable (module-variable module name)))
  1744. (if (and variable (variable-bound? variable))
  1745. (variable-ref variable)
  1746. (if (null? rest)
  1747. (error "No variable named" name 'in module)
  1748. (car rest) ; default value
  1749. ))))
  1750. ;; MODULE-SET! -- exported
  1751. ;;
  1752. ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
  1753. ;; to VALUE; if there is no such variable, an error is signaled.
  1754. ;;
  1755. (define (module-set! module name value)
  1756. (let ((variable (module-variable module name)))
  1757. (if variable
  1758. (variable-set! variable value)
  1759. (error "No variable named" name 'in module))))
  1760. ;; MODULE-DEFINE! -- exported
  1761. ;;
  1762. ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
  1763. ;; variable, it is added first.
  1764. ;;
  1765. (define (module-define! module name value)
  1766. (let ((variable (module-local-variable module name)))
  1767. (if variable
  1768. (begin
  1769. (variable-set! variable value)
  1770. (module-modified module))
  1771. (let ((variable (make-variable value)))
  1772. (module-add! module name variable)))))
  1773. ;; MODULE-DEFINED? -- exported
  1774. ;;
  1775. ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
  1776. ;; uses)
  1777. ;;
  1778. (define (module-defined? module name)
  1779. (let ((variable (module-variable module name)))
  1780. (and variable (variable-bound? variable))))
  1781. ;; MODULE-USE! module interface
  1782. ;;
  1783. ;; Add INTERFACE to the list of interfaces used by MODULE.
  1784. ;;
  1785. (define (module-use! module interface)
  1786. (if (not (or (eq? module interface)
  1787. (memq interface (module-uses module))))
  1788. (begin
  1789. ;; Newly used modules must be appended rather than consed, so that
  1790. ;; `module-variable' traverses the use list starting from the first
  1791. ;; used module.
  1792. (set-module-uses! module (append (module-uses module)
  1793. (list interface)))
  1794. (hash-clear! (module-import-obarray module))
  1795. (module-modified module))))
  1796. ;; MODULE-USE-INTERFACES! module interfaces
  1797. ;;
  1798. ;; Same as MODULE-USE!, but only notifies module observers after all
  1799. ;; interfaces are added to the inports list.
  1800. ;;
  1801. (define (module-use-interfaces! module interfaces)
  1802. (let* ((cur (module-uses module))
  1803. (new (let lp ((in interfaces) (out '()))
  1804. (if (null? in)
  1805. (reverse out)
  1806. (lp (cdr in)
  1807. (let ((iface (car in)))
  1808. (if (or (memq iface cur) (memq iface out))
  1809. out
  1810. (cons iface out))))))))
  1811. (set-module-uses! module (append cur new))
  1812. (hash-clear! (module-import-obarray module))
  1813. (module-modified module)))
  1814. ;;; {Recursive Namespaces}
  1815. ;;;
  1816. ;;; A hierarchical namespace emerges if we consider some module to be
  1817. ;;; root, and submodules of that module to be nested namespaces.
  1818. ;;;
  1819. ;;; The routines here manage variable names in hierarchical namespace.
  1820. ;;; Each variable name is a list of elements, looked up in successively nested
  1821. ;;; modules.
  1822. ;;;
  1823. ;;; (nested-ref some-root-module '(foo bar baz))
  1824. ;;; => <value of a variable named baz in the submodule bar of
  1825. ;;; the submodule foo of some-root-module>
  1826. ;;;
  1827. ;;;
  1828. ;;; There are:
  1829. ;;;
  1830. ;;; ;; a-root is a module
  1831. ;;; ;; name is a list of symbols
  1832. ;;;
  1833. ;;; nested-ref a-root name
  1834. ;;; nested-set! a-root name val
  1835. ;;; nested-define! a-root name val
  1836. ;;; nested-remove! a-root name
  1837. ;;;
  1838. ;;; These functions manipulate values in namespaces. For referencing the
  1839. ;;; namespaces themselves, use the following:
  1840. ;;;
  1841. ;;; nested-ref-module a-root name
  1842. ;;; nested-define-module! a-root name mod
  1843. ;;;
  1844. ;;; (current-module) is a natural choice for a root so for convenience there are
  1845. ;;; also:
  1846. ;;;
  1847. ;;; local-ref name == nested-ref (current-module) name
  1848. ;;; local-set! name val == nested-set! (current-module) name val
  1849. ;;; local-define name val == nested-define! (current-module) name val
  1850. ;;; local-remove name == nested-remove! (current-module) name
  1851. ;;; local-ref-module name == nested-ref-module (current-module) name
  1852. ;;; local-define-module! name m == nested-define-module! (current-module) name m
  1853. ;;;
  1854. (define (nested-ref root names)
  1855. (if (null? names)
  1856. root
  1857. (let loop ((cur root)
  1858. (head (car names))
  1859. (tail (cdr names)))
  1860. (if (null? tail)
  1861. (module-ref cur head #f)
  1862. (let ((cur (module-ref-submodule cur head)))
  1863. (and cur
  1864. (loop cur (car tail) (cdr tail))))))))
  1865. (define (nested-set! root names val)
  1866. (let loop ((cur root)
  1867. (head (car names))
  1868. (tail (cdr names)))
  1869. (if (null? tail)
  1870. (module-set! cur head val)
  1871. (let ((cur (module-ref-submodule cur head)))
  1872. (if (not cur)
  1873. (error "failed to resolve module" names)
  1874. (loop cur (car tail) (cdr tail)))))))
  1875. (define (nested-define! root names val)
  1876. (let loop ((cur root)
  1877. (head (car names))
  1878. (tail (cdr names)))
  1879. (if (null? tail)
  1880. (module-define! cur head val)
  1881. (let ((cur (module-ref-submodule cur head)))
  1882. (if (not cur)
  1883. (error "failed to resolve module" names)
  1884. (loop cur (car tail) (cdr tail)))))))
  1885. (define (nested-remove! root names)
  1886. (let loop ((cur root)
  1887. (head (car names))
  1888. (tail (cdr names)))
  1889. (if (null? tail)
  1890. (module-remove! cur head)
  1891. (let ((cur (module-ref-submodule cur head)))
  1892. (if (not cur)
  1893. (error "failed to resolve module" names)
  1894. (loop cur (car tail) (cdr tail)))))))
  1895. (define (nested-ref-module root names)
  1896. (let loop ((cur root)
  1897. (names names))
  1898. (if (null? names)
  1899. cur
  1900. (let ((cur (module-ref-submodule cur (car names))))
  1901. (and cur
  1902. (loop cur (cdr names)))))))
  1903. (define (nested-define-module! root names module)
  1904. (if (null? names)
  1905. (error "can't redefine root module" root module)
  1906. (let loop ((cur root)
  1907. (head (car names))
  1908. (tail (cdr names)))
  1909. (if (null? tail)
  1910. (module-define-submodule! cur head module)
  1911. (let ((cur (or (module-ref-submodule cur head)
  1912. (let ((m (make-module 31)))
  1913. (set-module-kind! m 'directory)
  1914. (set-module-name! m (append (module-name cur)
  1915. (list head)))
  1916. (module-define-submodule! cur head m)
  1917. m))))
  1918. (loop cur (car tail) (cdr tail)))))))
  1919. (define (local-ref names)
  1920. (nested-ref (current-module) names))
  1921. (define (local-set! names val)
  1922. (nested-set! (current-module) names val))
  1923. (define (local-define names val)
  1924. (nested-define! (current-module) names val))
  1925. (define (local-remove names)
  1926. (nested-remove! (current-module) names))
  1927. (define (local-ref-module names)
  1928. (nested-ref-module (current-module) names))
  1929. (define (local-define-module names mod)
  1930. (nested-define-module! (current-module) names mod))
  1931. ;;; {The (guile) module}
  1932. ;;;
  1933. ;;; The standard module, which has the core Guile bindings. Also called the
  1934. ;;; "root module", as it is imported by many other modules, but it is not
  1935. ;;; necessarily the root of anything; and indeed, the module named '() might be
  1936. ;;; better thought of as a root.
  1937. ;;;
  1938. (define (set-system-module! m s)
  1939. (set-procedure-property! (module-eval-closure m) 'system-module s))
  1940. ;; The root module uses the pre-modules-obarray as its obarray. This
  1941. ;; special obarray accumulates all bindings that have been established
  1942. ;; before the module system is fully booted.
  1943. ;;
  1944. ;; (The obarray continues to be used by code that has been closed over
  1945. ;; before the module system has been booted.)
  1946. ;;
  1947. (define the-root-module
  1948. (let ((m (make-module 0)))
  1949. (set-module-obarray! m (%get-pre-modules-obarray))
  1950. (set-module-name! m '(guile))
  1951. (set-system-module! m #t)
  1952. m))
  1953. ;; The root interface is a module that uses the same obarray as the
  1954. ;; root module. It does not allow new definitions, tho.
  1955. ;;
  1956. (define the-scm-module
  1957. (let ((m (make-module 0)))
  1958. (set-module-obarray! m (%get-pre-modules-obarray))
  1959. (set-module-eval-closure! m (standard-interface-eval-closure m))
  1960. (set-module-name! m '(guile))
  1961. (set-module-kind! m 'interface)
  1962. (set-system-module! m #t)
  1963. ;; In Guile 1.8 and earlier M was its own public interface.
  1964. (set-module-public-interface! m m)
  1965. m))
  1966. (set-module-public-interface! the-root-module the-scm-module)
  1967. ;; Now that we have a root module, even though modules aren't fully booted,
  1968. ;; expand the definition of resolve-module.
  1969. ;;
  1970. (define (resolve-module name . args)
  1971. (if (equal? name '(guile))
  1972. the-root-module
  1973. (error "unexpected module to resolve during module boot" name)))
  1974. ;; Cheat. These bindings are needed by modules.c, but we don't want
  1975. ;; to move their real definition here because that would be unnatural.
  1976. ;;
  1977. (define define-module* #f)
  1978. (define process-use-modules #f)
  1979. (define module-export! #f)
  1980. (define default-duplicate-binding-procedures #f)
  1981. ;; This boots the module system. All bindings needed by modules.c
  1982. ;; must have been defined by now.
  1983. ;;
  1984. (set-current-module the-root-module)
  1985. ;; Now that modules are booted, give module-name its final definition.
  1986. ;;
  1987. (define module-name
  1988. (let ((accessor (record-accessor module-type 'name)))
  1989. (lambda (mod)
  1990. (or (accessor mod)
  1991. (let ((name (list (gensym))))
  1992. ;; Name MOD and bind it in the module root so that it's visible to
  1993. ;; `resolve-module'. This is important as `psyntax' stores module
  1994. ;; names and relies on being able to `resolve-module' them.
  1995. (set-module-name! mod name)
  1996. (nested-define-module! (resolve-module '() #f) name mod)
  1997. (accessor mod))))))
  1998. (define (make-modules-in module name)
  1999. (or (nested-ref-module module name)
  2000. (let ((m (make-module 31)))
  2001. (set-module-kind! m 'directory)
  2002. (set-module-name! m (append (module-name module) name))
  2003. (nested-define-module! module name m)
  2004. m)))
  2005. (define (beautify-user-module! module)
  2006. (let ((interface (module-public-interface module)))
  2007. (if (or (not interface)
  2008. (eq? interface module))
  2009. (let ((interface (make-module 31)))
  2010. (set-module-name! interface (module-name module))
  2011. (set-module-version! interface (module-version module))
  2012. (set-module-kind! interface 'interface)
  2013. (set-module-public-interface! module interface))))
  2014. (if (and (not (memq the-scm-module (module-uses module)))
  2015. (not (eq? module the-root-module)))
  2016. ;; Import the default set of bindings (from the SCM module) in MODULE.
  2017. (module-use! module the-scm-module)))
  2018. (define (version-matches? version-ref target)
  2019. (define (sub-versions-match? v-refs t)
  2020. (define (sub-version-matches? v-ref t)
  2021. (let ((matches? (lambda (v) (sub-version-matches? v t))))
  2022. (cond
  2023. ((number? v-ref) (eqv? v-ref t))
  2024. ((list? v-ref)
  2025. (case (car v-ref)
  2026. ((>=) (>= t (cadr v-ref)))
  2027. ((<=) (<= t (cadr v-ref)))
  2028. ((and) (and-map matches? (cdr v-ref)))
  2029. ((or) (or-map matches? (cdr v-ref)))
  2030. ((not) (not (matches? (cadr v-ref))))
  2031. (else (error "Invalid sub-version reference" v-ref))))
  2032. (else (error "Invalid sub-version reference" v-ref)))))
  2033. (or (null? v-refs)
  2034. (and (not (null? t))
  2035. (sub-version-matches? (car v-refs) (car t))
  2036. (sub-versions-match? (cdr v-refs) (cdr t)))))
  2037. (let ((matches? (lambda (v) (version-matches? v target))))
  2038. (or (null? version-ref)
  2039. (case (car version-ref)
  2040. ((and) (and-map matches? (cdr version-ref)))
  2041. ((or) (or-map matches? (cdr version-ref)))
  2042. ((not) (not (matches? (cadr version-ref))))
  2043. (else (sub-versions-match? version-ref target))))))
  2044. (define (make-fresh-user-module)
  2045. (let ((m (make-module)))
  2046. (beautify-user-module! m)
  2047. m))
  2048. ;; NOTE: This binding is used in libguile/modules.c.
  2049. ;;
  2050. (define resolve-module
  2051. (let ((root (make-module)))
  2052. (set-module-name! root '())
  2053. ;; Define the-root-module as '(guile).
  2054. (module-define-submodule! root 'guile the-root-module)
  2055. (lambda* (name #:optional (autoload #t) (version #f) #:key (ensure #t))
  2056. (let ((already (nested-ref-module root name)))
  2057. (cond
  2058. ((and already
  2059. (or (not autoload) (module-public-interface already)))
  2060. ;; A hit, a palpable hit.
  2061. (if (and version
  2062. (not (version-matches? version (module-version already))))
  2063. (error "incompatible module version already loaded" name))
  2064. already)
  2065. (autoload
  2066. ;; Try to autoload the module, and recurse.
  2067. (try-load-module name version)
  2068. (resolve-module name #f #:ensure ensure))
  2069. (else
  2070. ;; No module found (or if one was, it had no public interface), and
  2071. ;; we're not autoloading. Make an empty module if #:ensure is true.
  2072. (or already
  2073. (and ensure
  2074. (make-modules-in root name)))))))))
  2075. (define (try-load-module name version)
  2076. (try-module-autoload name version))
  2077. (define (reload-module m)
  2078. "Revisit the source file corresponding to the module @var{m}."
  2079. (let ((f (module-filename m)))
  2080. (if f
  2081. (save-module-excursion
  2082. (lambda ()
  2083. ;; Re-set the initial environment, as in try-module-autoload.
  2084. (set-current-module (make-fresh-user-module))
  2085. (primitive-load-path f)
  2086. m))
  2087. ;; Though we could guess, we *should* know it.
  2088. (error "unknown file name for module" m))))
  2089. (define (purify-module! module)
  2090. "Removes bindings in MODULE which are inherited from the (guile) module."
  2091. (let ((use-list (module-uses module)))
  2092. (if (and (pair? use-list)
  2093. (eq? (car (last-pair use-list)) the-scm-module))
  2094. (set-module-uses! module (reverse (cdr (reverse use-list)))))))
  2095. ;; Return a module that is an interface to the module designated by
  2096. ;; NAME.
  2097. ;;
  2098. ;; `resolve-interface' takes four keyword arguments:
  2099. ;;
  2100. ;; #:select SELECTION
  2101. ;;
  2102. ;; SELECTION is a list of binding-specs to be imported; A binding-spec
  2103. ;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG
  2104. ;; is the name in the used module and SEEN is the name in the using
  2105. ;; module. Note that SEEN is also passed through RENAMER, below. The
  2106. ;; default is to select all bindings. If you specify no selection but
  2107. ;; a renamer, only the bindings that already exist in the used module
  2108. ;; are made available in the interface. Bindings that are added later
  2109. ;; are not picked up.
  2110. ;;
  2111. ;; #:hide BINDINGS
  2112. ;;
  2113. ;; BINDINGS is a list of bindings which should not be imported.
  2114. ;;
  2115. ;; #:prefix PREFIX
  2116. ;;
  2117. ;; PREFIX is a symbol that will be appended to each exported name.
  2118. ;; The default is to not perform any renaming.
  2119. ;;
  2120. ;; #:renamer RENAMER
  2121. ;;
  2122. ;; RENAMER is a procedure that takes a symbol and returns its new
  2123. ;; name. The default is not perform any renaming.
  2124. ;;
  2125. ;; Signal "no code for module" error if module name is not resolvable
  2126. ;; or its public interface is not available. Signal "no binding"
  2127. ;; error if selected binding does not exist in the used module.
  2128. ;;
  2129. (define* (resolve-interface name #:key
  2130. (select #f)
  2131. (hide '())
  2132. (prefix #f)
  2133. (renamer (if prefix
  2134. (symbol-prefix-proc prefix)
  2135. identity))
  2136. version)
  2137. (let* ((module (resolve-module name #t version #:ensure #f))
  2138. (public-i (and module (module-public-interface module))))
  2139. (and (or (not module) (not public-i))
  2140. (error "no code for module" name))
  2141. (if (and (not select) (null? hide) (eq? renamer identity))
  2142. public-i
  2143. (let ((selection (or select (module-map (lambda (sym var) sym)
  2144. public-i)))
  2145. (custom-i (make-module 31)))
  2146. (set-module-kind! custom-i 'custom-interface)
  2147. (set-module-name! custom-i name)
  2148. ;; XXX - should use a lazy binder so that changes to the
  2149. ;; used module are picked up automatically.
  2150. (for-each (lambda (bspec)
  2151. (let* ((direct? (symbol? bspec))
  2152. (orig (if direct? bspec (car bspec)))
  2153. (seen (if direct? bspec (cdr bspec)))
  2154. (var (or (module-local-variable public-i orig)
  2155. (module-local-variable module orig)
  2156. (error
  2157. ;; fixme: format manually for now
  2158. (simple-format
  2159. #f "no binding `~A' in module ~A"
  2160. orig name)))))
  2161. (if (memq orig hide)
  2162. (set! hide (delq! orig hide))
  2163. (module-add! custom-i
  2164. (renamer seen)
  2165. var))))
  2166. selection)
  2167. ;; Check that we are not hiding bindings which don't exist
  2168. (for-each (lambda (binding)
  2169. (if (not (module-local-variable public-i binding))
  2170. (error
  2171. (simple-format
  2172. #f "no binding `~A' to hide in module ~A"
  2173. binding name))))
  2174. hide)
  2175. custom-i))))
  2176. (define (symbol-prefix-proc prefix)
  2177. (lambda (symbol)
  2178. (symbol-append prefix symbol)))
  2179. ;; This function is called from "modules.c". If you change it, be
  2180. ;; sure to update "modules.c" as well.
  2181. (define* (define-module* name
  2182. #:key filename pure version (duplicates '())
  2183. (imports '()) (exports '()) (replacements '())
  2184. (re-exports '()) (autoloads '()) transformer)
  2185. (define (list-of pred l)
  2186. (or (null? l)
  2187. (and (pair? l) (pred (car l)) (list-of pred (cdr l)))))
  2188. (define (valid-export? x)
  2189. (or (symbol? x) (and (pair? x) (symbol? (car x)) (symbol? (cdr x)))))
  2190. (define (valid-autoload? x)
  2191. (and (pair? x) (list-of symbol? (car x)) (list-of symbol? (cdr x))))
  2192. (define (resolve-imports imports)
  2193. (define (resolve-import import-spec)
  2194. (if (list? import-spec)
  2195. (apply resolve-interface import-spec)
  2196. (error "unexpected use-module specification" import-spec)))
  2197. (let lp ((imports imports) (out '()))
  2198. (cond
  2199. ((null? imports) (reverse! out))
  2200. ((pair? imports)
  2201. (lp (cdr imports)
  2202. (cons (resolve-import (car imports)) out)))
  2203. (else (error "unexpected tail of imports list" imports)))))
  2204. ;; We could add a #:no-check arg, set by the define-module macro, if
  2205. ;; these checks are taking too much time.
  2206. ;;
  2207. (let ((module (resolve-module name #f)))
  2208. (beautify-user-module! module)
  2209. (if filename
  2210. (set-module-filename! module filename))
  2211. (if pure
  2212. (purify-module! module))
  2213. (if version
  2214. (begin
  2215. (if (not (list-of integer? version))
  2216. (error "expected list of integers for version"))
  2217. (set-module-version! module version)
  2218. (set-module-version! (module-public-interface module) version)))
  2219. (let ((imports (resolve-imports imports)))
  2220. (call-with-deferred-observers
  2221. (lambda ()
  2222. (if (pair? imports)
  2223. (module-use-interfaces! module imports))
  2224. (if (list-of valid-export? exports)
  2225. (if (pair? exports)
  2226. (module-export! module exports))
  2227. (error "expected exports to be a list of symbols or symbol pairs"))
  2228. (if (list-of valid-export? replacements)
  2229. (if (pair? replacements)
  2230. (module-replace! module replacements))
  2231. (error "expected replacements to be a list of symbols or symbol pairs"))
  2232. (if (list-of valid-export? re-exports)
  2233. (if (pair? re-exports)
  2234. (module-re-export! module re-exports))
  2235. (error "expected re-exports to be a list of symbols or symbol pairs"))
  2236. ;; FIXME
  2237. (if (not (null? autoloads))
  2238. (apply module-autoload! module autoloads))
  2239. ;; Wait until modules have been loaded to resolve duplicates
  2240. ;; handlers.
  2241. (if (pair? duplicates)
  2242. (let ((handlers (lookup-duplicates-handlers duplicates)))
  2243. (set-module-duplicates-handlers! module handlers))))))
  2244. (if transformer
  2245. (if (and (pair? transformer) (list-of symbol? transformer))
  2246. (let ((iface (resolve-interface transformer))
  2247. (sym (car (last-pair transformer))))
  2248. (set-module-transformer! module (module-ref iface sym)))
  2249. (error "expected transformer to be a module name" transformer)))
  2250. (run-hook module-defined-hook module)
  2251. module))
  2252. ;; `module-defined-hook' is a hook that is run whenever a new module
  2253. ;; is defined. Its members are called with one argument, the new
  2254. ;; module.
  2255. (define module-defined-hook (make-hook 1))
  2256. ;;; {Autoload}
  2257. ;;;
  2258. (define (make-autoload-interface module name bindings)
  2259. (let ((b (lambda (a sym definep)
  2260. (and (memq sym bindings)
  2261. (let ((i (module-public-interface (resolve-module name))))
  2262. (if (not i)
  2263. (error "missing interface for module" name))
  2264. (let ((autoload (memq a (module-uses module))))
  2265. ;; Replace autoload-interface with actual interface if
  2266. ;; that has not happened yet.
  2267. (if (pair? autoload)
  2268. (set-car! autoload i)))
  2269. (module-local-variable i sym))))))
  2270. (module-constructor (make-hash-table 0) '() b #f #f name 'autoload #f
  2271. (make-hash-table 0) '() (make-weak-value-hash-table 31) #f
  2272. (make-hash-table 0) #f #f #f)))
  2273. (define (module-autoload! module . args)
  2274. "Have @var{module} automatically load the module named @var{name} when one
  2275. of the symbols listed in @var{bindings} is looked up. @var{args} should be a
  2276. list of module-name/binding-list pairs, e.g., as in @code{(module-autoload!
  2277. module '(ice-9 q) '(make-q q-length))}."
  2278. (let loop ((args args))
  2279. (cond ((null? args)
  2280. #t)
  2281. ((null? (cdr args))
  2282. (error "invalid name+binding autoload list" args))
  2283. (else
  2284. (let ((name (car args))
  2285. (bindings (cadr args)))
  2286. (module-use! module (make-autoload-interface module
  2287. name bindings))
  2288. (loop (cddr args)))))))
  2289. ;;; {Autoloading modules}
  2290. ;;;
  2291. (define autoloads-in-progress '())
  2292. ;; This function is called from "modules.c". If you change it, be
  2293. ;; sure to update "modules.c" as well.
  2294. (define* (try-module-autoload module-name #:optional version)
  2295. (let* ((reverse-name (reverse module-name))
  2296. (name (symbol->string (car reverse-name)))
  2297. (dir-hint-module-name (reverse (cdr reverse-name)))
  2298. (dir-hint (apply string-append
  2299. (map (lambda (elt)
  2300. (string-append (symbol->string elt) "/"))
  2301. dir-hint-module-name))))
  2302. (resolve-module dir-hint-module-name #f)
  2303. (and (not (autoload-done-or-in-progress? dir-hint name))
  2304. (let ((didit #f))
  2305. (dynamic-wind
  2306. (lambda () (autoload-in-progress! dir-hint name))
  2307. (lambda ()
  2308. (with-fluids ((current-reader #f))
  2309. (save-module-excursion
  2310. (lambda ()
  2311. ;; The initial environment when loading a module is a fresh
  2312. ;; user module.
  2313. (set-current-module (make-fresh-user-module))
  2314. ;; Here we could allow some other search strategy (other than
  2315. ;; primitive-load-path), for example using versions encoded
  2316. ;; into the file system -- but then we would have to figure
  2317. ;; out how to locate the compiled file, do auto-compilation,
  2318. ;; etc. Punt for now, and don't use versions when locating
  2319. ;; the file.
  2320. (primitive-load-path (in-vicinity dir-hint name) #f)
  2321. (set! didit #t)))))
  2322. (lambda () (set-autoloaded! dir-hint name didit)))
  2323. didit))))
  2324. ;;; {Dynamic linking of modules}
  2325. ;;;
  2326. (define autoloads-done '((guile . guile)))
  2327. (define (autoload-done-or-in-progress? p m)
  2328. (let ((n (cons p m)))
  2329. (->bool (or (member n autoloads-done)
  2330. (member n autoloads-in-progress)))))
  2331. (define (autoload-done! p m)
  2332. (let ((n (cons p m)))
  2333. (set! autoloads-in-progress
  2334. (delete! n autoloads-in-progress))
  2335. (or (member n autoloads-done)
  2336. (set! autoloads-done (cons n autoloads-done)))))
  2337. (define (autoload-in-progress! p m)
  2338. (let ((n (cons p m)))
  2339. (set! autoloads-done
  2340. (delete! n autoloads-done))
  2341. (set! autoloads-in-progress (cons n autoloads-in-progress))))
  2342. (define (set-autoloaded! p m done?)
  2343. (if done?
  2344. (autoload-done! p m)
  2345. (let ((n (cons p m)))
  2346. (set! autoloads-done (delete! n autoloads-done))
  2347. (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
  2348. ;;; {Run-time options}
  2349. ;;;
  2350. (define-syntax define-option-interface
  2351. (syntax-rules ()
  2352. ((_ (interface (options enable disable) (option-set!)))
  2353. (begin
  2354. (define options
  2355. (case-lambda
  2356. (() (interface))
  2357. ((arg)
  2358. (if (list? arg)
  2359. (begin (interface arg) (interface))
  2360. (for-each
  2361. (lambda (option)
  2362. (apply (lambda (name value documentation)
  2363. (display name)
  2364. (if (< (string-length (symbol->string name)) 8)
  2365. (display #\tab))
  2366. (display #\tab)
  2367. (display value)
  2368. (display #\tab)
  2369. (display documentation)
  2370. (newline))
  2371. option))
  2372. (interface #t))))))
  2373. (define (enable . flags)
  2374. (interface (append flags (interface)))
  2375. (interface))
  2376. (define (disable . flags)
  2377. (let ((options (interface)))
  2378. (for-each (lambda (flag) (set! options (delq! flag options)))
  2379. flags)
  2380. (interface options)
  2381. (interface)))
  2382. (define-syntax-rule (option-set! opt val)
  2383. (eval-when (eval load compile expand)
  2384. (options (append (options) (list 'opt val)))))))))
  2385. (define-option-interface
  2386. (debug-options-interface
  2387. (debug-options debug-enable debug-disable)
  2388. (debug-set!)))
  2389. (define-option-interface
  2390. (read-options-interface
  2391. (read-options read-enable read-disable)
  2392. (read-set!)))
  2393. (define-option-interface
  2394. (print-options-interface
  2395. (print-options print-enable print-disable)
  2396. (print-set!)))
  2397. ;;; {The Unspecified Value}
  2398. ;;;
  2399. ;;; Currently Guile represents unspecified values via one particular value,
  2400. ;;; which may be obtained by evaluating (if #f #f). It would be nice in the
  2401. ;;; future if we could replace this with a return of 0 values, though.
  2402. ;;;
  2403. (define-syntax *unspecified*
  2404. (identifier-syntax (if #f #f)))
  2405. (define (unspecified? v) (eq? v *unspecified*))
  2406. ;;; {Running Repls}
  2407. ;;;
  2408. (define *repl-stack* (make-fluid))
  2409. ;; Programs can call `batch-mode?' to see if they are running as part of a
  2410. ;; script or if they are running interactively. REPL implementations ensure that
  2411. ;; `batch-mode?' returns #f during their extent.
  2412. ;;
  2413. (define (batch-mode?)
  2414. (null? (or (fluid-ref *repl-stack*) '())))
  2415. ;; Programs can re-enter batch mode, for example after a fork, by calling
  2416. ;; `ensure-batch-mode!'. It's not a great interface, though; it would be better
  2417. ;; to abort to the outermost prompt, and call a thunk there.
  2418. ;;
  2419. (define (ensure-batch-mode!)
  2420. (set! batch-mode? (lambda () #t)))
  2421. (define (quit . args)
  2422. (apply throw 'quit args))
  2423. (define exit quit)
  2424. (define (gc-run-time)
  2425. (cdr (assq 'gc-time-taken (gc-stats))))
  2426. (define abort-hook (make-hook))
  2427. (define before-error-hook (make-hook))
  2428. (define after-error-hook (make-hook))
  2429. (define before-backtrace-hook (make-hook))
  2430. (define after-backtrace-hook (make-hook))
  2431. (define before-read-hook (make-hook))
  2432. (define after-read-hook (make-hook))
  2433. (define before-eval-hook (make-hook 1))
  2434. (define after-eval-hook (make-hook 1))
  2435. (define before-print-hook (make-hook 1))
  2436. (define after-print-hook (make-hook 1))
  2437. ;;; This hook is run at the very end of an interactive session.
  2438. ;;;
  2439. (define exit-hook (make-hook))
  2440. ;;; The default repl-reader function. We may override this if we've
  2441. ;;; the readline library.
  2442. (define repl-reader
  2443. (lambda* (prompt #:optional (reader (fluid-ref current-reader)))
  2444. (if (not (char-ready?))
  2445. (display (if (string? prompt) prompt (prompt))))
  2446. (force-output)
  2447. (run-hook before-read-hook)
  2448. ((or reader read) (current-input-port))))
  2449. ;;; {IOTA functions: generating lists of numbers}
  2450. ;;;
  2451. (define (iota n)
  2452. (let loop ((count (1- n)) (result '()))
  2453. (if (< count 0) result
  2454. (loop (1- count) (cons count result)))))
  2455. ;;; {While}
  2456. ;;;
  2457. ;;; with `continue' and `break'.
  2458. ;;;
  2459. ;; The inliner will remove the prompts at compile-time if it finds that
  2460. ;; `continue' or `break' are not used.
  2461. ;;
  2462. (define-syntax while
  2463. (lambda (x)
  2464. (syntax-case x ()
  2465. ((while cond body ...)
  2466. #`(let ((break-tag (make-prompt-tag "break"))
  2467. (continue-tag (make-prompt-tag "continue")))
  2468. (call-with-prompt
  2469. break-tag
  2470. (lambda ()
  2471. (define-syntax #,(datum->syntax #'while 'break)
  2472. (lambda (x)
  2473. (syntax-case x ()
  2474. ((_ arg (... ...))
  2475. #'(abort-to-prompt break-tag arg (... ...)))
  2476. (_
  2477. #'(lambda args
  2478. (apply abort-to-prompt break-tag args))))))
  2479. (let lp ()
  2480. (call-with-prompt
  2481. continue-tag
  2482. (lambda ()
  2483. (define-syntax #,(datum->syntax #'while 'continue)
  2484. (lambda (x)
  2485. (syntax-case x ()
  2486. ((_)
  2487. #'(abort-to-prompt continue-tag))
  2488. ((_ . args)
  2489. (syntax-violation 'continue "too many arguments" x))
  2490. (_
  2491. #'(lambda ()
  2492. (abort-to-prompt continue-tag))))))
  2493. (do () ((not cond) #f) body ...))
  2494. (lambda (k) (lp)))))
  2495. (lambda (k . args)
  2496. (if (null? args)
  2497. #t
  2498. (apply values args)))))))))
  2499. ;;; {Module System Macros}
  2500. ;;;
  2501. ;; Return a list of expressions that evaluate to the appropriate
  2502. ;; arguments for resolve-interface according to SPEC.
  2503. (eval-when (compile)
  2504. (if (memq 'prefix (read-options))
  2505. (error "boot-9 must be compiled with #:kw, not :kw")))
  2506. (define (keyword-like-symbol->keyword sym)
  2507. (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
  2508. (define-syntax define-module
  2509. (lambda (x)
  2510. (define (keyword-like? stx)
  2511. (let ((dat (syntax->datum stx)))
  2512. (and (symbol? dat)
  2513. (eqv? (string-ref (symbol->string dat) 0) #\:))))
  2514. (define (->keyword sym)
  2515. (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
  2516. (define (parse-iface args)
  2517. (let loop ((in args) (out '()))
  2518. (syntax-case in ()
  2519. (() (reverse! out))
  2520. ;; The user wanted #:foo, but wrote :foo. Fix it.
  2521. ((sym . in) (keyword-like? #'sym)
  2522. (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out))
  2523. ((kw . in) (not (keyword? (syntax->datum #'kw)))
  2524. (syntax-violation 'define-module "expected keyword arg" x #'kw))
  2525. ((#:renamer renamer . in)
  2526. (loop #'in (cons* #',renamer #:renamer out)))
  2527. ((kw val . in)
  2528. (loop #'in (cons* #'val #'kw out))))))
  2529. (define (parse args imp exp rex rep aut)
  2530. ;; Just quote everything except #:use-module and #:use-syntax. We
  2531. ;; need to know about all arguments regardless since we want to turn
  2532. ;; symbols that look like keywords into real keywords, and the
  2533. ;; keyword args in a define-module form are not regular
  2534. ;; (i.e. no-backtrace doesn't take a value).
  2535. (syntax-case args ()
  2536. (()
  2537. (let ((imp (if (null? imp) '() #`(#:imports `#,imp)))
  2538. (exp (if (null? exp) '() #`(#:exports '#,exp)))
  2539. (rex (if (null? rex) '() #`(#:re-exports '#,rex)))
  2540. (rep (if (null? rep) '() #`(#:replacements '#,rep)))
  2541. (aut (if (null? aut) '() #`(#:autoloads '#,aut))))
  2542. #`(#,@imp #,@exp #,@rex #,@rep #,@aut)))
  2543. ;; The user wanted #:foo, but wrote :foo. Fix it.
  2544. ((sym . args) (keyword-like? #'sym)
  2545. (parse #`(#,(->keyword (syntax->datum #'sym)) . args)
  2546. imp exp rex rep aut))
  2547. ((kw . args) (not (keyword? (syntax->datum #'kw)))
  2548. (syntax-violation 'define-module "expected keyword arg" x #'kw))
  2549. ((#:no-backtrace . args)
  2550. ;; Ignore this one.
  2551. (parse #'args imp exp rex rep aut))
  2552. ((#:pure . args)
  2553. #`(#:pure #t . #,(parse #'args imp exp rex rep aut)))
  2554. ((kw)
  2555. (syntax-violation 'define-module "keyword arg without value" x #'kw))
  2556. ((#:version (v ...) . args)
  2557. #`(#:version '(v ...) . #,(parse #'args imp exp rex rep aut)))
  2558. ((#:duplicates (d ...) . args)
  2559. #`(#:duplicates '(d ...) . #,(parse #'args imp exp rex rep aut)))
  2560. ((#:filename f . args)
  2561. #`(#:filename 'f . #,(parse #'args imp exp rex rep aut)))
  2562. ((#:use-module (name name* ...) . args)
  2563. (and (and-map symbol? (syntax->datum #'(name name* ...))))
  2564. (parse #'args #`(#,@imp ((name name* ...))) exp rex rep aut))
  2565. ((#:use-syntax (name name* ...) . args)
  2566. (and (and-map symbol? (syntax->datum #'(name name* ...))))
  2567. #`(#:transformer '(name name* ...)
  2568. . #,(parse #'args #`(#,@imp ((name name* ...))) exp rex rep aut)))
  2569. ((#:use-module ((name name* ...) arg ...) . args)
  2570. (and (and-map symbol? (syntax->datum #'(name name* ...))))
  2571. (parse #'args
  2572. #`(#,@imp ((name name* ...) #,@(parse-iface #'(arg ...))))
  2573. exp rex rep aut))
  2574. ((#:export (ex ...) . args)
  2575. (parse #'args imp #`(#,@exp ex ...) rex rep aut))
  2576. ((#:export-syntax (ex ...) . args)
  2577. (parse #'args imp #`(#,@exp ex ...) rex rep aut))
  2578. ((#:re-export (re ...) . args)
  2579. (parse #'args imp exp #`(#,@rex re ...) rep aut))
  2580. ((#:re-export-syntax (re ...) . args)
  2581. (parse #'args imp exp #`(#,@rex re ...) rep aut))
  2582. ((#:replace (r ...) . args)
  2583. (parse #'args imp exp rex #`(#,@rep r ...) aut))
  2584. ((#:replace-syntax (r ...) . args)
  2585. (parse #'args imp exp rex #`(#,@rep r ...) aut))
  2586. ((#:autoload name bindings . args)
  2587. (parse #'args imp exp rex rep #`(#,@aut name bindings)))
  2588. ((kw val . args)
  2589. (syntax-violation 'define-module "unknown keyword or bad argument"
  2590. #'kw #'val))))
  2591. (syntax-case x ()
  2592. ((_ (name name* ...) arg ...)
  2593. (and-map symbol? (syntax->datum #'(name name* ...)))
  2594. (with-syntax (((quoted-arg ...)
  2595. (parse #'(arg ...) '() '() '() '() '()))
  2596. ;; Ideally the filename is either a string or #f;
  2597. ;; this hack is to work around a case in which
  2598. ;; port-filename returns a symbol (`socket') for
  2599. ;; sockets.
  2600. (filename (let ((f (assq-ref (or (syntax-source x) '())
  2601. 'filename)))
  2602. (and (string? f) f))))
  2603. #'(eval-when (eval load compile expand)
  2604. (let ((m (define-module* '(name name* ...)
  2605. #:filename filename quoted-arg ...)))
  2606. (set-current-module m)
  2607. m)))))))
  2608. ;; The guts of the use-modules macro. Add the interfaces of the named
  2609. ;; modules to the use-list of the current module, in order.
  2610. ;; This function is called by "modules.c". If you change it, be sure
  2611. ;; to change scm_c_use_module as well.
  2612. (define (process-use-modules module-interface-args)
  2613. (let ((interfaces (map (lambda (mif-args)
  2614. (or (apply resolve-interface mif-args)
  2615. (error "no such module" mif-args)))
  2616. module-interface-args)))
  2617. (call-with-deferred-observers
  2618. (lambda ()
  2619. (module-use-interfaces! (current-module) interfaces)))))
  2620. (define-syntax use-modules
  2621. (lambda (x)
  2622. (define (keyword-like? stx)
  2623. (let ((dat (syntax->datum stx)))
  2624. (and (symbol? dat)
  2625. (eqv? (string-ref (symbol->string dat) 0) #\:))))
  2626. (define (->keyword sym)
  2627. (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
  2628. (define (quotify-iface args)
  2629. (let loop ((in args) (out '()))
  2630. (syntax-case in ()
  2631. (() (reverse! out))
  2632. ;; The user wanted #:foo, but wrote :foo. Fix it.
  2633. ((sym . in) (keyword-like? #'sym)
  2634. (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out))
  2635. ((kw . in) (not (keyword? (syntax->datum #'kw)))
  2636. (syntax-violation 'define-module "expected keyword arg" x #'kw))
  2637. ((#:renamer renamer . in)
  2638. (loop #'in (cons* #'renamer #:renamer out)))
  2639. ((kw val . in)
  2640. (loop #'in (cons* #''val #'kw out))))))
  2641. (define (quotify specs)
  2642. (let lp ((in specs) (out '()))
  2643. (syntax-case in ()
  2644. (() (reverse out))
  2645. (((name name* ...) . in)
  2646. (and-map symbol? (syntax->datum #'(name name* ...)))
  2647. (lp #'in (cons #''((name name* ...)) out)))
  2648. ((((name name* ...) arg ...) . in)
  2649. (and-map symbol? (syntax->datum #'(name name* ...)))
  2650. (with-syntax (((quoted-arg ...) (quotify-iface #'(arg ...))))
  2651. (lp #'in (cons #`(list '(name name* ...) quoted-arg ...)
  2652. out)))))))
  2653. (syntax-case x ()
  2654. ((_ spec ...)
  2655. (with-syntax (((quoted-args ...) (quotify #'(spec ...))))
  2656. #'(eval-when (eval load compile expand)
  2657. (process-use-modules (list quoted-args ...))
  2658. *unspecified*))))))
  2659. (include-from-path "ice-9/r6rs-libraries")
  2660. (define-syntax-rule (define-private foo bar)
  2661. (define foo bar))
  2662. (define-syntax define-public
  2663. (syntax-rules ()
  2664. ((_ (name . args) . body)
  2665. (define-public name (lambda args . body)))
  2666. ((_ name val)
  2667. (begin
  2668. (define name val)
  2669. (export name)))))
  2670. (define-syntax-rule (defmacro-public name args body ...)
  2671. (begin
  2672. (defmacro name args body ...)
  2673. (export-syntax name)))
  2674. ;; And now for the most important macro.
  2675. (define-syntax-rule (λ formals body ...)
  2676. (lambda formals body ...))
  2677. ;; Export a local variable
  2678. ;; This function is called from "modules.c". If you change it, be
  2679. ;; sure to update "modules.c" as well.
  2680. (define (module-export! m names)
  2681. (let ((public-i (module-public-interface m)))
  2682. (for-each (lambda (name)
  2683. (let* ((internal-name (if (pair? name) (car name) name))
  2684. (external-name (if (pair? name) (cdr name) name))
  2685. (var (module-ensure-local-variable! m internal-name)))
  2686. (module-add! public-i external-name var)))
  2687. names)))
  2688. (define (module-replace! m names)
  2689. (let ((public-i (module-public-interface m)))
  2690. (for-each (lambda (name)
  2691. (let* ((internal-name (if (pair? name) (car name) name))
  2692. (external-name (if (pair? name) (cdr name) name))
  2693. (var (module-ensure-local-variable! m internal-name)))
  2694. ;; FIXME: use a bit on variables instead of object
  2695. ;; properties.
  2696. (set-object-property! var 'replace #t)
  2697. (module-add! public-i external-name var)))
  2698. names)))
  2699. ;; Export all local variables from a module
  2700. ;;
  2701. (define (module-export-all! mod)
  2702. (define (fresh-interface!)
  2703. (let ((iface (make-module)))
  2704. (set-module-name! iface (module-name mod))
  2705. (set-module-version! iface (module-version mod))
  2706. (set-module-kind! iface 'interface)
  2707. (set-module-public-interface! mod iface)
  2708. iface))
  2709. (let ((iface (or (module-public-interface mod)
  2710. (fresh-interface!))))
  2711. (set-module-obarray! iface (module-obarray mod))))
  2712. ;; Re-export a imported variable
  2713. ;;
  2714. (define (module-re-export! m names)
  2715. (let ((public-i (module-public-interface m)))
  2716. (for-each (lambda (name)
  2717. (let* ((internal-name (if (pair? name) (car name) name))
  2718. (external-name (if (pair? name) (cdr name) name))
  2719. (var (module-variable m internal-name)))
  2720. (cond ((not var)
  2721. (error "Undefined variable:" internal-name))
  2722. ((eq? var (module-local-variable m internal-name))
  2723. (error "re-exporting local variable:" internal-name))
  2724. (else
  2725. (module-add! public-i external-name var)))))
  2726. names)))
  2727. (define-syntax-rule (export name ...)
  2728. (eval-when (eval load compile expand)
  2729. (call-with-deferred-observers
  2730. (lambda ()
  2731. (module-export! (current-module) '(name ...))))))
  2732. (define-syntax-rule (re-export name ...)
  2733. (eval-when (eval load compile expand)
  2734. (call-with-deferred-observers
  2735. (lambda ()
  2736. (module-re-export! (current-module) '(name ...))))))
  2737. (define-syntax-rule (export! name ...)
  2738. (eval-when (eval load compile expand)
  2739. (call-with-deferred-observers
  2740. (lambda ()
  2741. (module-replace! (current-module) '(name ...))))))
  2742. (define-syntax-rule (export-syntax name ...)
  2743. (export name ...))
  2744. (define-syntax-rule (re-export-syntax name ...)
  2745. (re-export name ...))
  2746. ;;; {Parameters}
  2747. ;;;
  2748. (define* (make-mutable-parameter init #:optional (converter identity))
  2749. (let ((fluid (make-fluid)))
  2750. (fluid-set! fluid (converter init))
  2751. (case-lambda
  2752. (() (fluid-ref fluid))
  2753. ((val) (fluid-set! fluid (converter val))))))
  2754. ;;; {Handling of duplicate imported bindings}
  2755. ;;;
  2756. ;; Duplicate handlers take the following arguments:
  2757. ;;
  2758. ;; module importing module
  2759. ;; name conflicting name
  2760. ;; int1 old interface where name occurs
  2761. ;; val1 value of binding in old interface
  2762. ;; int2 new interface where name occurs
  2763. ;; val2 value of binding in new interface
  2764. ;; var previous resolution or #f
  2765. ;; val value of previous resolution
  2766. ;;
  2767. ;; A duplicate handler can take three alternative actions:
  2768. ;;
  2769. ;; 1. return #f => leave responsibility to next handler
  2770. ;; 2. exit with an error
  2771. ;; 3. return a variable resolving the conflict
  2772. ;;
  2773. (define duplicate-handlers
  2774. (let ((m (make-module 7)))
  2775. (define (check module name int1 val1 int2 val2 var val)
  2776. (scm-error 'misc-error
  2777. #f
  2778. "~A: `~A' imported from both ~A and ~A"
  2779. (list (module-name module)
  2780. name
  2781. (module-name int1)
  2782. (module-name int2))
  2783. #f))
  2784. (define (warn module name int1 val1 int2 val2 var val)
  2785. (format (current-error-port)
  2786. "WARNING: ~A: `~A' imported from both ~A and ~A\n"
  2787. (module-name module)
  2788. name
  2789. (module-name int1)
  2790. (module-name int2))
  2791. #f)
  2792. (define (replace module name int1 val1 int2 val2 var val)
  2793. (let ((old (or (and var (object-property var 'replace) var)
  2794. (module-variable int1 name)))
  2795. (new (module-variable int2 name)))
  2796. (if (object-property old 'replace)
  2797. (and (or (eq? old new)
  2798. (not (object-property new 'replace)))
  2799. old)
  2800. (and (object-property new 'replace)
  2801. new))))
  2802. (define (warn-override-core module name int1 val1 int2 val2 var val)
  2803. (and (eq? int1 the-scm-module)
  2804. (begin
  2805. (format (current-error-port)
  2806. "WARNING: ~A: imported module ~A overrides core binding `~A'\n"
  2807. (module-name module)
  2808. (module-name int2)
  2809. name)
  2810. (module-local-variable int2 name))))
  2811. (define (first module name int1 val1 int2 val2 var val)
  2812. (or var (module-local-variable int1 name)))
  2813. (define (last module name int1 val1 int2 val2 var val)
  2814. (module-local-variable int2 name))
  2815. (define (noop module name int1 val1 int2 val2 var val)
  2816. #f)
  2817. (set-module-name! m 'duplicate-handlers)
  2818. (set-module-kind! m 'interface)
  2819. (module-define! m 'check check)
  2820. (module-define! m 'warn warn)
  2821. (module-define! m 'replace replace)
  2822. (module-define! m 'warn-override-core warn-override-core)
  2823. (module-define! m 'first first)
  2824. (module-define! m 'last last)
  2825. (module-define! m 'merge-generics noop)
  2826. (module-define! m 'merge-accessors noop)
  2827. m))
  2828. (define (lookup-duplicates-handlers handler-names)
  2829. (and handler-names
  2830. (map (lambda (handler-name)
  2831. (or (module-symbol-local-binding
  2832. duplicate-handlers handler-name #f)
  2833. (error "invalid duplicate handler name:"
  2834. handler-name)))
  2835. (if (list? handler-names)
  2836. handler-names
  2837. (list handler-names)))))
  2838. (define default-duplicate-binding-procedures
  2839. (make-mutable-parameter #f))
  2840. (define default-duplicate-binding-handler
  2841. (make-mutable-parameter '(replace warn-override-core warn last)
  2842. (lambda (handler-names)
  2843. (default-duplicate-binding-procedures
  2844. (lookup-duplicates-handlers handler-names))
  2845. handler-names)))
  2846. ;;; {`load'.}
  2847. ;;;
  2848. ;;; Load is tricky when combined with relative paths, compilation, and
  2849. ;;; the filesystem. If a path is relative, what is it relative to? The
  2850. ;;; path of the source file at the time it was compiled? The path of
  2851. ;;; the compiled file? What if both or either were installed? And how
  2852. ;;; do you get that information? Tricky, I say.
  2853. ;;;
  2854. ;;; To get around all of this, we're going to do something nasty, and
  2855. ;;; turn `load' into a macro. That way it can know the path of the
  2856. ;;; source file with respect to which it was invoked, so it can resolve
  2857. ;;; relative paths with respect to the original source path.
  2858. ;;;
  2859. ;;; There is an exception, and that is that if the source file was in
  2860. ;;; the load path when it was compiled, instead of looking up against
  2861. ;;; the absolute source location, we load-from-path against the relative
  2862. ;;; source location.
  2863. ;;;
  2864. (define %auto-compilation-options
  2865. ;; Default `compile-file' option when auto-compiling.
  2866. '(#:warnings (unbound-variable arity-mismatch format)))
  2867. (define* (load-in-vicinity dir path #:optional reader)
  2868. (define (canonical->suffix canon)
  2869. (cond
  2870. ((string-prefix? "/" canon) canon)
  2871. ((and (> (string-length canon) 2)
  2872. (eqv? (string-ref canon 1) #\:))
  2873. ;; Paths like C:... transform to /C...
  2874. (string-append "/" (substring canon 0 1) (substring canon 2)))
  2875. (else canon)))
  2876. ;; Returns the .go file corresponding to `name'. Does not search load
  2877. ;; paths, only the fallback path. If the .go file is missing or out of
  2878. ;; date, and auto-compilation is enabled, will try auto-compilation, just
  2879. ;; as primitive-load-path does internally. primitive-load is
  2880. ;; unaffected. Returns #f if auto-compilation failed or was disabled.
  2881. ;;
  2882. ;; NB: Unless we need to compile the file, this function should not cause
  2883. ;; (system base compile) to be loaded up. For that reason compiled-file-name
  2884. ;; partially duplicates functionality from (system base compile).
  2885. ;;
  2886. (define (compiled-file-name canon-path)
  2887. ;; FIXME: would probably be better just to append SHA1(canon-path)
  2888. ;; to the %compile-fallback-path, to avoid deep directory stats.
  2889. (and %compile-fallback-path
  2890. (string-append
  2891. %compile-fallback-path
  2892. (canonical->suffix canon-path)
  2893. (cond ((or (null? %load-compiled-extensions)
  2894. (string-null? (car %load-compiled-extensions)))
  2895. (warn "invalid %load-compiled-extensions"
  2896. %load-compiled-extensions)
  2897. ".go")
  2898. (else (car %load-compiled-extensions))))))
  2899. (define (fresh-compiled-file-name name go-path)
  2900. (catch #t
  2901. (lambda ()
  2902. (let* ((scmstat (stat name))
  2903. (gostat (and (not %fresh-auto-compile)
  2904. (stat go-path #f))))
  2905. (if (and gostat
  2906. (or (> (stat:mtime gostat) (stat:mtime scmstat))
  2907. (and (= (stat:mtime gostat) (stat:mtime scmstat))
  2908. (>= (stat:mtimensec gostat)
  2909. (stat:mtimensec scmstat)))))
  2910. go-path
  2911. (begin
  2912. (if gostat
  2913. (format (current-error-port)
  2914. ";;; note: source file ~a\n;;; newer than compiled ~a\n"
  2915. name go-path))
  2916. (cond
  2917. (%load-should-auto-compile
  2918. (%warn-auto-compilation-enabled)
  2919. (format (current-error-port) ";;; compiling ~a\n" name)
  2920. (let ((cfn
  2921. ((module-ref
  2922. (resolve-interface '(system base compile))
  2923. 'compile-file)
  2924. name
  2925. #:opts %auto-compilation-options
  2926. #:env (current-module))))
  2927. (format (current-error-port) ";;; compiled ~a\n" cfn)
  2928. cfn))
  2929. (else #f))))))
  2930. (lambda (k . args)
  2931. (format (current-error-port)
  2932. ";;; WARNING: compilation of ~a failed:\n" name)
  2933. (for-each (lambda (s)
  2934. (if (not (string-null? s))
  2935. (format (current-error-port) ";;; ~a\n" s)))
  2936. (string-split
  2937. (call-with-output-string
  2938. (lambda (port) (print-exception port #f k args)))
  2939. #\newline))
  2940. #f)))
  2941. (define (absolute-path? path)
  2942. (string-prefix? "/" path))
  2943. (define (load-absolute abs-path)
  2944. (let ((cfn (let ((canon (false-if-exception (canonicalize-path abs-path))))
  2945. (and canon
  2946. (let ((go-path (compiled-file-name canon)))
  2947. (and go-path
  2948. (fresh-compiled-file-name abs-path go-path)))))))
  2949. (if cfn
  2950. (begin
  2951. (if %load-hook
  2952. (%load-hook abs-path))
  2953. (load-compiled cfn))
  2954. (start-stack 'load-stack
  2955. (primitive-load abs-path)))))
  2956. (save-module-excursion
  2957. (lambda ()
  2958. (with-fluids ((current-reader reader)
  2959. (%file-port-name-canonicalization 'relative))
  2960. (cond
  2961. ((or (absolute-path? path))
  2962. (load-absolute path))
  2963. ((absolute-path? dir)
  2964. (load-absolute (in-vicinity dir path)))
  2965. (else
  2966. (load-from-path (in-vicinity dir path))))))))
  2967. (define-syntax load
  2968. (make-variable-transformer
  2969. (lambda (x)
  2970. (let* ((src (syntax-source x))
  2971. (file (and src (assq-ref src 'filename)))
  2972. (dir (and (string? file) (dirname file))))
  2973. (syntax-case x ()
  2974. ((_ arg ...)
  2975. #`(load-in-vicinity #,(or dir #'(getcwd)) arg ...))
  2976. (id
  2977. (identifier? #'id)
  2978. #`(lambda args
  2979. (apply load-in-vicinity #,(or dir #'(getcwd)) args))))))))
  2980. ;;; {`cond-expand' for SRFI-0 support.}
  2981. ;;;
  2982. ;;; This syntactic form expands into different commands or
  2983. ;;; definitions, depending on the features provided by the Scheme
  2984. ;;; implementation.
  2985. ;;;
  2986. ;;; Syntax:
  2987. ;;;
  2988. ;;; <cond-expand>
  2989. ;;; --> (cond-expand <cond-expand-clause>+)
  2990. ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
  2991. ;;; <cond-expand-clause>
  2992. ;;; --> (<feature-requirement> <command-or-definition>*)
  2993. ;;; <feature-requirement>
  2994. ;;; --> <feature-identifier>
  2995. ;;; | (and <feature-requirement>*)
  2996. ;;; | (or <feature-requirement>*)
  2997. ;;; | (not <feature-requirement>)
  2998. ;;; <feature-identifier>
  2999. ;;; --> <a symbol which is the name or alias of a SRFI>
  3000. ;;;
  3001. ;;; Additionally, this implementation provides the
  3002. ;;; <feature-identifier>s `guile' and `r5rs', so that programs can
  3003. ;;; determine the implementation type and the supported standard.
  3004. ;;;
  3005. ;;; Currently, the following feature identifiers are supported:
  3006. ;;;
  3007. ;;; guile r5rs srfi-0 srfi-4 srfi-6 srfi-13 srfi-14 srfi-55 srfi-61
  3008. ;;;
  3009. ;;; Remember to update the features list when adding more SRFIs.
  3010. ;;;
  3011. (define %cond-expand-features
  3012. ;; Adjust the above comment when changing this.
  3013. '(guile
  3014. guile-2
  3015. r5rs
  3016. srfi-0 ;; cond-expand itself
  3017. srfi-4 ;; homogenous numeric vectors
  3018. srfi-6 ;; open-input-string etc, in the guile core
  3019. srfi-13 ;; string library
  3020. srfi-23 ;; `error` procedure
  3021. srfi-14 ;; character sets
  3022. srfi-55 ;; require-extension
  3023. srfi-61 ;; general cond clause
  3024. ))
  3025. ;; This table maps module public interfaces to the list of features.
  3026. ;;
  3027. (define %cond-expand-table (make-hash-table 31))
  3028. ;; Add one or more features to the `cond-expand' feature list of the
  3029. ;; module `module'.
  3030. ;;
  3031. (define (cond-expand-provide module features)
  3032. (let ((mod (module-public-interface module)))
  3033. (and mod
  3034. (hashq-set! %cond-expand-table mod
  3035. (append (hashq-ref %cond-expand-table mod '())
  3036. features)))))
  3037. (define-syntax cond-expand
  3038. (lambda (x)
  3039. (define (module-has-feature? mod sym)
  3040. (or-map (lambda (mod)
  3041. (memq sym (hashq-ref %cond-expand-table mod '())))
  3042. (module-uses mod)))
  3043. (define (condition-matches? condition)
  3044. (syntax-case condition (and or not)
  3045. ((and c ...)
  3046. (and-map condition-matches? #'(c ...)))
  3047. ((or c ...)
  3048. (or-map condition-matches? #'(c ...)))
  3049. ((not c)
  3050. (if (condition-matches? #'c) #f #t))
  3051. (c
  3052. (identifier? #'c)
  3053. (let ((sym (syntax->datum #'c)))
  3054. (if (memq sym %cond-expand-features)
  3055. #t
  3056. (module-has-feature? (current-module) sym))))))
  3057. (define (match clauses alternate)
  3058. (syntax-case clauses ()
  3059. (((condition form ...) . rest)
  3060. (if (condition-matches? #'condition)
  3061. #'(begin form ...)
  3062. (match #'rest alternate)))
  3063. (() (alternate))))
  3064. (syntax-case x (else)
  3065. ((_ clause ... (else form ...))
  3066. (match #'(clause ...)
  3067. (lambda ()
  3068. #'(begin form ...))))
  3069. ((_ clause ...)
  3070. (match #'(clause ...)
  3071. (lambda ()
  3072. (syntax-violation 'cond-expand "unfulfilled cond-expand" x)))))))
  3073. ;; This procedure gets called from the startup code with a list of
  3074. ;; numbers, which are the numbers of the SRFIs to be loaded on startup.
  3075. ;;
  3076. (define (use-srfis srfis)
  3077. (process-use-modules
  3078. (map (lambda (num)
  3079. (list (list 'srfi (string->symbol
  3080. (string-append "srfi-" (number->string num))))))
  3081. srfis)))
  3082. ;;; srfi-55: require-extension
  3083. ;;;
  3084. (define-syntax require-extension
  3085. (lambda (x)
  3086. (syntax-case x (srfi)
  3087. ((_ (srfi n ...))
  3088. (and-map integer? (syntax->datum #'(n ...)))
  3089. (with-syntax
  3090. (((srfi-n ...)
  3091. (map (lambda (n)
  3092. (datum->syntax x (symbol-append 'srfi- n)))
  3093. (map string->symbol
  3094. (map number->string (syntax->datum #'(n ...)))))))
  3095. #'(use-modules (srfi srfi-n) ...)))
  3096. ((_ (type arg ...))
  3097. (identifier? #'type)
  3098. (syntax-violation 'require-extension "Not a recognized extension type"
  3099. x)))))
  3100. ;;; Defining transparently inlinable procedures
  3101. ;;;
  3102. (define-syntax define-inlinable
  3103. ;; Define a macro and a procedure such that direct calls are inlined, via
  3104. ;; the macro expansion, whereas references in non-call contexts refer to
  3105. ;; the procedure. Inspired by the `define-integrable' macro by Dybvig et al.
  3106. (lambda (x)
  3107. ;; Use a space in the prefix to avoid potential -Wunused-toplevel
  3108. ;; warning
  3109. (define prefix (string->symbol "% "))
  3110. (define (make-procedure-name name)
  3111. (datum->syntax name
  3112. (symbol-append prefix (syntax->datum name)
  3113. '-procedure)))
  3114. (syntax-case x ()
  3115. ((_ (name formals ...) body ...)
  3116. (identifier? #'name)
  3117. (with-syntax ((proc-name (make-procedure-name #'name))
  3118. ((args ...) (generate-temporaries #'(formals ...))))
  3119. #`(begin
  3120. (define (proc-name formals ...)
  3121. (syntax-parameterize ((name (identifier-syntax proc-name)))
  3122. body ...))
  3123. (define-syntax-parameter name
  3124. (lambda (x)
  3125. (syntax-case x ()
  3126. ((_ args ...)
  3127. #'((syntax-parameterize ((name (identifier-syntax proc-name)))
  3128. (lambda (formals ...)
  3129. body ...))
  3130. args ...))
  3131. (_
  3132. (identifier? x)
  3133. #'proc-name))))))))))
  3134. (define using-readline?
  3135. (let ((using-readline? (make-fluid)))
  3136. (make-procedure-with-setter
  3137. (lambda () (fluid-ref using-readline?))
  3138. (lambda (v) (fluid-set! using-readline? v)))))
  3139. ;;; {Deprecated stuff}
  3140. ;;;
  3141. (begin-deprecated
  3142. (module-use! the-scm-module (resolve-interface '(ice-9 deprecated))))
  3143. ;;; Place the user in the guile-user module.
  3144. ;;;
  3145. ;; FIXME:
  3146. (module-use! the-scm-module (resolve-interface '(srfi srfi-4)))
  3147. ;; Set filename to #f to prevent reload.
  3148. (define-module (guile-user)
  3149. #:autoload (system base compile) (compile compile-file)
  3150. #:filename #f)
  3151. ;; Remain in the `(guile)' module at compilation-time so that the
  3152. ;; `-Wunused-toplevel' warning works as expected.
  3153. (eval-when (compile) (set-current-module the-root-module))
  3154. ;;; boot-9.scm ends here