boot-9.scm 102 KB

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