boot-9.scm 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. ;;;; Boston, MA 02111-1307 USA
  18. ;;;;
  19. ;;; This file is the first thing loaded into Guile. It adds many mundane
  20. ;;; definitions and a few that are interesting.
  21. ;;;
  22. ;;; The module system (hence the hierarchical namespace) are defined in this
  23. ;;; file.
  24. ;;;
  25. ;;; {Features}
  26. ;;
  27. (define (provide sym)
  28. (if (not (memq sym *features*))
  29. (set! *features* (cons sym *features*))))
  30. ;;; Return #t iff FEATURE is available to this Guile interpreter.
  31. ;;; In SLIB, provided? also checks to see if the module is available.
  32. ;;; We should do that too, but don't.
  33. (define (provided? feature)
  34. (and (memq feature *features*) #t))
  35. ;;; presumably deprecated.
  36. (define feature? provided?)
  37. ;;; let format alias simple-format until the more complete version is loaded
  38. (define format simple-format)
  39. ;;; {R4RS compliance}
  40. (primitive-load-path "ice-9/r4rs.scm")
  41. ;;; {Simple Debugging Tools}
  42. ;;
  43. ;; peek takes any number of arguments, writes them to the
  44. ;; current ouput port, and returns the last argument.
  45. ;; It is handy to wrap around an expression to look at
  46. ;; a value each time is evaluated, e.g.:
  47. ;;
  48. ;; (+ 10 (troublesome-fn))
  49. ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
  50. ;;
  51. (define (peek . stuff)
  52. (newline)
  53. (display ";;; ")
  54. (write stuff)
  55. (newline)
  56. (car (last-pair stuff)))
  57. (define pk peek)
  58. (define (warn . stuff)
  59. (with-output-to-port (current-error-port)
  60. (lambda ()
  61. (newline)
  62. (display ";;; WARNING ")
  63. (display stuff)
  64. (newline)
  65. (car (last-pair stuff)))))
  66. ;;; {Trivial Functions}
  67. ;;;
  68. (define (id x) x)
  69. (define (1+ n) (+ n 1))
  70. (define (-1+ n) (+ n -1))
  71. (define 1- -1+)
  72. (define return-it noop)
  73. (define (and=> value procedure) (and value (procedure value)))
  74. (define (make-hash-table k) (make-vector k '()))
  75. ;;; apply-to-args is functionally redunant with apply and, worse,
  76. ;;; is less general than apply since it only takes two arguments.
  77. ;;;
  78. ;;; On the other hand, apply-to-args is a syntacticly convenient way to
  79. ;;; perform binding in many circumstances when the "let" family of
  80. ;;; of forms don't cut it. E.g.:
  81. ;;;
  82. ;;; (apply-to-args (return-3d-mouse-coords)
  83. ;;; (lambda (x y z)
  84. ;;; ...))
  85. ;;;
  86. (define (apply-to-args args fn) (apply fn args))
  87. ;;; {Integer Math}
  88. ;;;
  89. (define (ipow-by-squaring x k acc proc)
  90. (cond ((zero? k) acc)
  91. ((= 1 k) (proc acc x))
  92. (else (ipow-by-squaring (proc x x)
  93. (quotient k 2)
  94. (if (even? k) acc (proc acc x))
  95. proc))))
  96. (define string-character-length string-length)
  97. ;; A convenience function for combining flag bits. Like logior, but
  98. ;; handles the cases of 0 and 1 arguments.
  99. ;;
  100. (define (flags . args)
  101. (cond
  102. ((null? args) 0)
  103. ((null? (cdr args)) (car args))
  104. (else (apply logior args))))
  105. ;;; {Symbol Properties}
  106. ;;;
  107. (define (symbol-property sym prop)
  108. (let ((pair (assoc prop (symbol-pref sym))))
  109. (and pair (cdr pair))))
  110. (define (set-symbol-property! sym prop val)
  111. (let ((pair (assoc prop (symbol-pref sym))))
  112. (if pair
  113. (set-cdr! pair val)
  114. (symbol-pset! sym (acons prop val (symbol-pref sym))))))
  115. (define (symbol-property-remove! sym prop)
  116. (let ((pair (assoc prop (symbol-pref sym))))
  117. (if pair
  118. (symbol-pset! sym (delq! pair (symbol-pref sym))))))
  119. ;;; {Line and Delimited I/O}
  120. ;;; corresponds to SCM_LINE_INCREMENTORS in libguile.
  121. (define scm-line-incrementors "\n")
  122. (define (read-line! string . maybe-port)
  123. (let* ((port (if (pair? maybe-port)
  124. (car maybe-port)
  125. (current-input-port))))
  126. (let* ((rv (%read-delimited! scm-line-incrementors
  127. string
  128. #t
  129. port))
  130. (terminator (car rv))
  131. (nchars (cdr rv)))
  132. (cond ((and (= nchars 0)
  133. (eof-object? terminator))
  134. terminator)
  135. ((not terminator) #f)
  136. (else nchars)))))
  137. (define (read-delimited! delims buf . args)
  138. (let* ((num-args (length args))
  139. (port (if (> num-args 0)
  140. (car args)
  141. (current-input-port)))
  142. (handle-delim (if (> num-args 1)
  143. (cadr args)
  144. 'trim))
  145. (start (if (> num-args 2)
  146. (caddr args)
  147. 0))
  148. (end (if (> num-args 3)
  149. (cadddr args)
  150. (string-length buf))))
  151. (let* ((rv (%read-delimited! delims
  152. buf
  153. (not (eq? handle-delim 'peek))
  154. port
  155. start
  156. end))
  157. (terminator (car rv))
  158. (nchars (cdr rv)))
  159. (cond ((or (not terminator) ; buffer filled
  160. (eof-object? terminator))
  161. (if (zero? nchars)
  162. (if (eq? handle-delim 'split)
  163. (cons terminator terminator)
  164. terminator)
  165. (if (eq? handle-delim 'split)
  166. (cons nchars terminator)
  167. nchars)))
  168. (else
  169. (case handle-delim
  170. ((trim peek) nchars)
  171. ((concat) (string-set! buf (+ nchars start) terminator)
  172. (+ nchars 1))
  173. ((split) (cons nchars terminator))
  174. (else (error "unexpected handle-delim value: "
  175. handle-delim))))))))
  176. (define (read-delimited delims . args)
  177. (let* ((port (if (pair? args)
  178. (let ((pt (car args)))
  179. (set! args (cdr args))
  180. pt)
  181. (current-input-port)))
  182. (handle-delim (if (pair? args)
  183. (car args)
  184. 'trim)))
  185. (let loop ((substrings ())
  186. (total-chars 0)
  187. (buf-size 100)) ; doubled each time through.
  188. (let* ((buf (make-string buf-size))
  189. (rv (%read-delimited! delims
  190. buf
  191. (not (eq? handle-delim 'peek))
  192. port))
  193. (terminator (car rv))
  194. (nchars (cdr rv))
  195. (join-substrings
  196. (lambda ()
  197. (apply string-append
  198. (reverse
  199. (cons (if (and (eq? handle-delim 'concat)
  200. (not (eof-object? terminator)))
  201. (string terminator)
  202. "")
  203. (cons (make-shared-substring buf 0 nchars)
  204. substrings))))))
  205. (new-total (+ total-chars nchars)))
  206. (cond ((not terminator)
  207. ;; buffer filled.
  208. (loop (cons (substring buf 0 nchars) substrings)
  209. new-total
  210. (* buf-size 2)))
  211. ((eof-object? terminator)
  212. (if (zero? new-total)
  213. (if (eq? handle-delim 'split)
  214. (cons terminator terminator)
  215. terminator)
  216. (if (eq? handle-delim 'split)
  217. (cons (join-substrings) terminator)
  218. (join-substrings))))
  219. (else
  220. (case handle-delim
  221. ((trim peek concat) (join-substrings))
  222. ((split) (cons (join-substrings) terminator))
  223. (else (error "unexpected handle-delim value: "
  224. handle-delim)))))))))
  225. ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
  226. ;;; from PORT. The return value depends on the value of HANDLE-DELIM,
  227. ;;; which may be one of the symbols `trim', `concat', `peek' and
  228. ;;; `split'. If it is `trim' (the default), the trailing newline is
  229. ;;; removed and the string is returned. If `concat', the string is
  230. ;;; returned with the trailing newline intact. If `peek', the newline
  231. ;;; is left in the input port buffer and the string is returned. If
  232. ;;; `split', the newline is split from the string and read-line
  233. ;;; returns a pair consisting of the truncated string and the newline.
  234. (define (read-line . args)
  235. (let* ((port (if (null? args)
  236. (current-input-port)
  237. (car args)))
  238. (handle-delim (if (> (length args) 1)
  239. (cadr args)
  240. 'trim))
  241. (line/delim (%read-line port))
  242. (line (car line/delim))
  243. (delim (cdr line/delim)))
  244. (case handle-delim
  245. ((trim) line)
  246. ((split) line/delim)
  247. ((concat) (if (and (string? line) (char? delim))
  248. (string-append line (string delim))
  249. line))
  250. ((peek) (if (char? delim)
  251. (unread-char delim port))
  252. line)
  253. (else
  254. (error "unexpected handle-delim value: " handle-delim)))))
  255. ;;; {Arrays}
  256. ;;;
  257. (if (provided? 'array)
  258. (primitive-load-path "ice-9/arrays.scm"))
  259. ;;; {Keywords}
  260. ;;;
  261. (define (symbol->keyword symbol)
  262. (make-keyword-from-dash-symbol (symbol-append '- symbol)))
  263. (define (keyword->symbol kw)
  264. (let ((sym (keyword-dash-symbol kw)))
  265. (string->symbol (substring sym 1 (string-length sym)))))
  266. (define (kw-arg-ref args kw)
  267. (let ((rem (member kw args)))
  268. (and rem (pair? (cdr rem)) (cadr rem))))
  269. ;;; {Structs}
  270. (define (struct-layout s)
  271. (struct-ref (struct-vtable s) vtable-index-layout))
  272. ;;; {Records}
  273. ;;;
  274. ;; Printing records: by default, records are printed as
  275. ;;
  276. ;; #<type-name field1: val1 field2: val2 ...>
  277. ;;
  278. ;; You can change that by giving a custom printing function to
  279. ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
  280. ;; will be called like
  281. ;;
  282. ;; (<printer> object port)
  283. ;;
  284. ;; It should print OBJECT to PORT.
  285. (define (inherit-print-state old-port new-port)
  286. (if (get-print-state old-port)
  287. (port-with-print-state new-port (get-print-state old-port))
  288. new-port))
  289. ;; 0: type-name, 1: fields
  290. (define record-type-vtable
  291. (make-vtable-vtable "prpr" 0
  292. (lambda (s p)
  293. (cond ((eq? s record-type-vtable)
  294. (display "#<record-type-vtable>" p))
  295. (else
  296. (display "#<record-type " p)
  297. (display (record-type-name s) p)
  298. (display ">" p))))))
  299. (define (record-type? obj)
  300. (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
  301. (define (make-record-type type-name fields . opt)
  302. (let ((printer-fn (and (pair? opt) (car opt))))
  303. (let ((struct (make-struct record-type-vtable 0
  304. (make-struct-layout
  305. (apply symbol-append
  306. (map (lambda (f) "pw") fields)))
  307. (or printer-fn
  308. (lambda (s p)
  309. (display "#<" p)
  310. (display type-name p)
  311. (let loop ((fields fields)
  312. (off 0))
  313. (cond
  314. ((not (null? fields))
  315. (display " " p)
  316. (display (car fields) p)
  317. (display ": " p)
  318. (display (struct-ref s off) p)
  319. (loop (cdr fields) (+ 1 off)))))
  320. (display ">" p)))
  321. type-name
  322. (copy-tree fields))))
  323. ;; Temporary solution: Associate a name to the record type descriptor
  324. ;; so that the object system can create a wrapper class for it.
  325. (set-struct-vtable-name! struct (if (symbol? type-name)
  326. type-name
  327. (string->symbol type-name)))
  328. struct)))
  329. (define (record-type-name obj)
  330. (if (record-type? obj)
  331. (struct-ref obj vtable-offset-user)
  332. (error 'not-a-record-type obj)))
  333. (define (record-type-fields obj)
  334. (if (record-type? obj)
  335. (struct-ref obj (+ 1 vtable-offset-user))
  336. (error 'not-a-record-type obj)))
  337. (define (record-constructor rtd . opt)
  338. (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
  339. (eval `(lambda ,field-names
  340. (make-struct ',rtd 0 ,@(map (lambda (f)
  341. (if (memq f field-names)
  342. f
  343. #f))
  344. (record-type-fields rtd)))))))
  345. (define (record-predicate rtd)
  346. (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
  347. (define (record-accessor 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. (eval `(lambda (obj)
  352. (and (eq? ',rtd (record-type-descriptor obj))
  353. (struct-ref obj ,pos))))))
  354. (define (record-modifier rtd field-name)
  355. (let* ((pos (list-index (record-type-fields rtd) field-name)))
  356. (if (not pos)
  357. (error 'no-such-field field-name))
  358. (eval `(lambda (obj val)
  359. (and (eq? ',rtd (record-type-descriptor obj))
  360. (struct-set! obj ,pos val))))))
  361. (define (record? obj)
  362. (and (struct? obj) (record-type? (struct-vtable obj))))
  363. (define (record-type-descriptor obj)
  364. (if (struct? obj)
  365. (struct-vtable obj)
  366. (error 'not-a-record obj)))
  367. (provide 'record)
  368. ;;; {Booleans}
  369. ;;;
  370. (define (->bool x) (not (not x)))
  371. ;;; {Symbols}
  372. ;;;
  373. (define (symbol-append . args)
  374. (string->symbol (apply string-append args)))
  375. (define (list->symbol . args)
  376. (string->symbol (apply list->string args)))
  377. (define (symbol . args)
  378. (string->symbol (apply string args)))
  379. (define (obarray-symbol-append ob . args)
  380. (string->obarray-symbol (apply string-append ob args)))
  381. (define (obarray-gensym obarray . opt)
  382. (if (null? opt)
  383. (gensym "%%gensym" obarray)
  384. (gensym (car opt) obarray)))
  385. ;;; {Lists}
  386. ;;;
  387. (define (list-index l k)
  388. (let loop ((n 0)
  389. (l l))
  390. (and (not (null? l))
  391. (if (eq? (car l) k)
  392. n
  393. (loop (+ n 1) (cdr l))))))
  394. (define (make-list n . init)
  395. (if (pair? init) (set! init (car init)))
  396. (let loop ((answer '())
  397. (n n))
  398. (if (<= n 0)
  399. answer
  400. (loop (cons init answer) (- n 1)))))
  401. ;;; {Multiple return values}
  402. (define *values-rtd*
  403. (make-record-type "values"
  404. '(values)))
  405. (define values
  406. (let ((make-values (record-constructor *values-rtd*)))
  407. (lambda x
  408. (if (and (not (null? x))
  409. (null? (cdr x)))
  410. (car x)
  411. (make-values x)))))
  412. (define call-with-values
  413. (let ((access-values (record-accessor *values-rtd* 'values))
  414. (values-predicate? (record-predicate *values-rtd*)))
  415. (lambda (producer consumer)
  416. (let ((result (producer)))
  417. (if (values-predicate? result)
  418. (apply consumer (access-values result))
  419. (consumer result))))))
  420. (provide 'values)
  421. ;;; {and-map and or-map}
  422. ;;;
  423. ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
  424. ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
  425. ;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst.
  426. ;;;
  427. ;; and-map f l
  428. ;;
  429. ;; Apply f to successive elements of l until exhaustion or f returns #f.
  430. ;; If returning early, return #f. Otherwise, return the last value returned
  431. ;; by f. If f has never been called because l is empty, return #t.
  432. ;;
  433. (define (and-map f lst)
  434. (let loop ((result #t)
  435. (l lst))
  436. (and result
  437. (or (and (null? l)
  438. result)
  439. (loop (f (car l)) (cdr l))))))
  440. ;; or-map f l
  441. ;;
  442. ;; Apply f to successive elements of l until exhaustion or while f returns #f.
  443. ;; If returning early, return the return value of f.
  444. ;;
  445. (define (or-map f lst)
  446. (let loop ((result #f)
  447. (l lst))
  448. (or result
  449. (and (not (null? l))
  450. (loop (f (car l)) (cdr l))))))
  451. (if (provided? 'posix)
  452. (primitive-load-path "ice-9/posix.scm"))
  453. (if (provided? 'socket)
  454. (primitive-load-path "ice-9/networking.scm"))
  455. (define file-exists?
  456. (if (provided? 'posix)
  457. (lambda (str)
  458. (access? str F_OK))
  459. (lambda (str)
  460. (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
  461. (lambda args #f))))
  462. (if port (begin (close-port port) #t)
  463. #f)))))
  464. (define file-is-directory?
  465. (if (provided? 'posix)
  466. (lambda (str)
  467. (eq? (stat:type (stat str)) 'directory))
  468. (lambda (str)
  469. (let ((port (catch 'system-error
  470. (lambda () (open-file (string-append str "/.")
  471. OPEN_READ))
  472. (lambda args #f))))
  473. (if port (begin (close-port port) #t)
  474. #f)))))
  475. (define (has-suffix? str suffix)
  476. (let ((sufl (string-length suffix))
  477. (sl (string-length str)))
  478. (and (> sl sufl)
  479. (string=? (substring str (- sl sufl) sl) suffix))))
  480. ;;; {Error Handling}
  481. ;;;
  482. (define (error . args)
  483. (save-stack)
  484. (if (null? args)
  485. (scm-error 'misc-error #f "?" #f #f)
  486. (let loop ((msg "~A")
  487. (rest (cdr args)))
  488. (if (not (null? rest))
  489. (loop (string-append msg " ~S")
  490. (cdr rest))
  491. (scm-error 'misc-error #f msg args #f)))))
  492. ;; bad-throw is the hook that is called upon a throw to a an unhandled
  493. ;; key (unless the throw has four arguments, in which case
  494. ;; it's usually interpreted as an error throw.)
  495. ;; If the key has a default handler (a throw-handler-default property),
  496. ;; it is applied to the throw.
  497. ;;
  498. (define (bad-throw key . args)
  499. (let ((default (symbol-property key 'throw-handler-default)))
  500. (or (and default (apply default key args))
  501. (apply error "unhandled-exception:" key args))))
  502. (define (tm:sec obj) (vector-ref obj 0))
  503. (define (tm:min obj) (vector-ref obj 1))
  504. (define (tm:hour obj) (vector-ref obj 2))
  505. (define (tm:mday obj) (vector-ref obj 3))
  506. (define (tm:mon obj) (vector-ref obj 4))
  507. (define (tm:year obj) (vector-ref obj 5))
  508. (define (tm:wday obj) (vector-ref obj 6))
  509. (define (tm:yday obj) (vector-ref obj 7))
  510. (define (tm:isdst obj) (vector-ref obj 8))
  511. (define (tm:gmtoff obj) (vector-ref obj 9))
  512. (define (tm:zone obj) (vector-ref obj 10))
  513. (define (set-tm:sec obj val) (vector-set! obj 0 val))
  514. (define (set-tm:min obj val) (vector-set! obj 1 val))
  515. (define (set-tm:hour obj val) (vector-set! obj 2 val))
  516. (define (set-tm:mday obj val) (vector-set! obj 3 val))
  517. (define (set-tm:mon obj val) (vector-set! obj 4 val))
  518. (define (set-tm:year obj val) (vector-set! obj 5 val))
  519. (define (set-tm:wday obj val) (vector-set! obj 6 val))
  520. (define (set-tm:yday obj val) (vector-set! obj 7 val))
  521. (define (set-tm:isdst obj val) (vector-set! obj 8 val))
  522. (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
  523. (define (set-tm:zone obj val) (vector-set! obj 10 val))
  524. (define (tms:clock obj) (vector-ref obj 0))
  525. (define (tms:utime obj) (vector-ref obj 1))
  526. (define (tms:stime obj) (vector-ref obj 2))
  527. (define (tms:cutime obj) (vector-ref obj 3))
  528. (define (tms:cstime obj) (vector-ref obj 4))
  529. (define (file-position . args) (apply ftell args))
  530. (define (file-set-position . args) (apply fseek args))
  531. (define (move->fdes fd/port fd)
  532. (cond ((integer? fd/port)
  533. (dup->fdes fd/port fd)
  534. (close fd/port)
  535. fd)
  536. (else
  537. (primitive-move->fdes fd/port fd)
  538. (set-port-revealed! fd/port 1)
  539. fd/port)))
  540. (define (release-port-handle port)
  541. (let ((revealed (port-revealed port)))
  542. (if (> revealed 0)
  543. (set-port-revealed! port (- revealed 1)))))
  544. (define (dup->port port/fd mode . maybe-fd)
  545. (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
  546. mode)))
  547. (if (pair? maybe-fd)
  548. (set-port-revealed! port 1))
  549. port))
  550. (define (dup->inport port/fd . maybe-fd)
  551. (apply dup->port port/fd "r" maybe-fd))
  552. (define (dup->outport port/fd . maybe-fd)
  553. (apply dup->port port/fd "w" maybe-fd))
  554. (define (dup port/fd . maybe-fd)
  555. (if (integer? port/fd)
  556. (apply dup->fdes port/fd maybe-fd)
  557. (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
  558. (define (duplicate-port port modes)
  559. (dup->port port modes))
  560. (define (fdes->inport fdes)
  561. (let loop ((rest-ports (fdes->ports fdes)))
  562. (cond ((null? rest-ports)
  563. (let ((result (fdopen fdes "r")))
  564. (set-port-revealed! result 1)
  565. result))
  566. ((input-port? (car rest-ports))
  567. (set-port-revealed! (car rest-ports)
  568. (+ (port-revealed (car rest-ports)) 1))
  569. (car rest-ports))
  570. (else
  571. (loop (cdr rest-ports))))))
  572. (define (fdes->outport fdes)
  573. (let loop ((rest-ports (fdes->ports fdes)))
  574. (cond ((null? rest-ports)
  575. (let ((result (fdopen fdes "w")))
  576. (set-port-revealed! result 1)
  577. result))
  578. ((output-port? (car rest-ports))
  579. (set-port-revealed! (car rest-ports)
  580. (+ (port-revealed (car rest-ports)) 1))
  581. (car rest-ports))
  582. (else
  583. (loop (cdr rest-ports))))))
  584. (define (port->fdes port)
  585. (set-port-revealed! port (+ (port-revealed port) 1))
  586. (fileno port))
  587. (define (setenv name value)
  588. (if value
  589. (putenv (string-append name "=" value))
  590. (putenv name)))
  591. ;;; {Load Paths}
  592. ;;;
  593. ;;; Here for backward compatability
  594. ;;
  595. (define scheme-file-suffix (lambda () ".scm"))
  596. (define (in-vicinity vicinity file)
  597. (let ((tail (let ((len (string-length vicinity)))
  598. (if (zero? len)
  599. #f
  600. (string-ref vicinity (- len 1))))))
  601. (string-append vicinity
  602. (if (or (not tail)
  603. (eq? tail #\/))
  604. ""
  605. "/")
  606. file)))
  607. ;;; {Help for scm_shell}
  608. ;;; The argument-processing code used by Guile-based shells generates
  609. ;;; Scheme code based on the argument list. This page contains help
  610. ;;; functions for the code it generates.
  611. (define (command-line) (program-arguments))
  612. ;; This is mostly for the internal use of the code generated by
  613. ;; scm_compile_shell_switches.
  614. (define (load-user-init)
  615. (let* ((home (or (getenv "HOME")
  616. (false-if-exception (passwd:dir (getpwuid (getuid))))
  617. "/")) ;; fallback for cygwin etc.
  618. (init-file (in-vicinity home ".guile")))
  619. (if (file-exists? init-file)
  620. (primitive-load init-file))))
  621. ;;; {Loading by paths}
  622. ;;; Load a Scheme source file named NAME, searching for it in the
  623. ;;; directories listed in %load-path, and applying each of the file
  624. ;;; name extensions listed in %load-extensions.
  625. (define (load-from-path name)
  626. (start-stack 'load-stack
  627. (primitive-load-path name)))
  628. ;;; {Transcendental Functions}
  629. ;;;
  630. ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
  631. ;;; Written by Jerry D. Hedden, (C) FSF.
  632. ;;; See the file `COPYING' for terms applying to this program.
  633. ;;;
  634. (define (exp z)
  635. (if (real? z) ($exp z)
  636. (make-polar ($exp (real-part z)) (imag-part z))))
  637. (define (log z)
  638. (if (and (real? z) (>= z 0))
  639. ($log z)
  640. (make-rectangular ($log (magnitude z)) (angle z))))
  641. (define (sqrt z)
  642. (if (real? z)
  643. (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
  644. ($sqrt z))
  645. (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
  646. (define expt
  647. (let ((integer-expt integer-expt))
  648. (lambda (z1 z2)
  649. (cond ((integer? z2)
  650. (if (>= z2 0)
  651. (integer-expt z1 z2)
  652. (/ 1 (integer-expt z1 (- z2)))))
  653. ((and (real? z2) (real? z1) (>= z1 0))
  654. ($expt z1 z2))
  655. (else
  656. (exp (* z2 (log z1))))))))
  657. (define (sinh z)
  658. (if (real? z) ($sinh z)
  659. (let ((x (real-part z)) (y (imag-part z)))
  660. (make-rectangular (* ($sinh x) ($cos y))
  661. (* ($cosh x) ($sin y))))))
  662. (define (cosh z)
  663. (if (real? z) ($cosh z)
  664. (let ((x (real-part z)) (y (imag-part z)))
  665. (make-rectangular (* ($cosh x) ($cos y))
  666. (* ($sinh x) ($sin y))))))
  667. (define (tanh z)
  668. (if (real? z) ($tanh z)
  669. (let* ((x (* 2 (real-part z)))
  670. (y (* 2 (imag-part z)))
  671. (w (+ ($cosh x) ($cos y))))
  672. (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
  673. (define (asinh z)
  674. (if (real? z) ($asinh z)
  675. (log (+ z (sqrt (+ (* z z) 1))))))
  676. (define (acosh z)
  677. (if (and (real? z) (>= z 1))
  678. ($acosh z)
  679. (log (+ z (sqrt (- (* z z) 1))))))
  680. (define (atanh z)
  681. (if (and (real? z) (> z -1) (< z 1))
  682. ($atanh z)
  683. (/ (log (/ (+ 1 z) (- 1 z))) 2)))
  684. (define (sin z)
  685. (if (real? z) ($sin z)
  686. (let ((x (real-part z)) (y (imag-part z)))
  687. (make-rectangular (* ($sin x) ($cosh y))
  688. (* ($cos x) ($sinh y))))))
  689. (define (cos z)
  690. (if (real? z) ($cos z)
  691. (let ((x (real-part z)) (y (imag-part z)))
  692. (make-rectangular (* ($cos x) ($cosh y))
  693. (- (* ($sin x) ($sinh y)))))))
  694. (define (tan z)
  695. (if (real? z) ($tan z)
  696. (let* ((x (* 2 (real-part z)))
  697. (y (* 2 (imag-part z)))
  698. (w (+ ($cos x) ($cosh y))))
  699. (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
  700. (define (asin z)
  701. (if (and (real? z) (>= z -1) (<= z 1))
  702. ($asin z)
  703. (* -i (asinh (* +i z)))))
  704. (define (acos z)
  705. (if (and (real? z) (>= z -1) (<= z 1))
  706. ($acos z)
  707. (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
  708. (define (atan z . y)
  709. (if (null? y)
  710. (if (real? z) ($atan z)
  711. (/ (log (/ (- +i z) (+ +i z))) +2i))
  712. ($atan2 z (car y))))
  713. (define (log10 arg)
  714. (/ (log arg) (log 10)))
  715. ;;; {Reader Extensions}
  716. ;;;
  717. ;;; Reader code for various "#c" forms.
  718. ;;;
  719. (read-hash-extend #\' (lambda (c port)
  720. (read port)))
  721. (read-hash-extend #\. (lambda (c port)
  722. (eval (read port))))
  723. ;;; {Command Line Options}
  724. ;;;
  725. (define (get-option argv kw-opts kw-args return)
  726. (cond
  727. ((null? argv)
  728. (return #f #f argv))
  729. ((or (not (eq? #\- (string-ref (car argv) 0)))
  730. (eq? (string-length (car argv)) 1))
  731. (return 'normal-arg (car argv) (cdr argv)))
  732. ((eq? #\- (string-ref (car argv) 1))
  733. (let* ((kw-arg-pos (or (string-index (car argv) #\=)
  734. (string-length (car argv))))
  735. (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
  736. (kw-opt? (member kw kw-opts))
  737. (kw-arg? (member kw kw-args))
  738. (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
  739. (substring (car argv)
  740. (+ kw-arg-pos 1)
  741. (string-length (car argv))))
  742. (and kw-arg?
  743. (begin (set! argv (cdr argv)) (car argv))))))
  744. (if (or kw-opt? kw-arg?)
  745. (return kw arg (cdr argv))
  746. (return 'usage-error kw (cdr argv)))))
  747. (else
  748. (let* ((char (substring (car argv) 1 2))
  749. (kw (symbol->keyword char)))
  750. (cond
  751. ((member kw kw-opts)
  752. (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
  753. (new-argv (if (= 0 (string-length rest-car))
  754. (cdr argv)
  755. (cons (string-append "-" rest-car) (cdr argv)))))
  756. (return kw #f new-argv)))
  757. ((member kw kw-args)
  758. (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
  759. (arg (if (= 0 (string-length rest-car))
  760. (cadr argv)
  761. rest-car))
  762. (new-argv (if (= 0 (string-length rest-car))
  763. (cddr argv)
  764. (cdr argv))))
  765. (return kw arg new-argv)))
  766. (else (return 'usage-error kw argv)))))))
  767. (define (for-next-option proc argv kw-opts kw-args)
  768. (let loop ((argv argv))
  769. (get-option argv kw-opts kw-args
  770. (lambda (opt opt-arg argv)
  771. (and opt (proc opt opt-arg argv loop))))))
  772. (define (display-usage-report kw-desc)
  773. (for-each
  774. (lambda (kw)
  775. (or (eq? (car kw) #t)
  776. (eq? (car kw) 'else)
  777. (let* ((opt-desc kw)
  778. (help (cadr opt-desc))
  779. (opts (car opt-desc))
  780. (opts-proper (if (string? (car opts)) (cdr opts) opts))
  781. (arg-name (if (string? (car opts))
  782. (string-append "<" (car opts) ">")
  783. ""))
  784. (left-part (string-append
  785. (with-output-to-string
  786. (lambda ()
  787. (map (lambda (x) (display (keyword-symbol x)) (display " "))
  788. opts-proper)))
  789. arg-name))
  790. (middle-part (if (and (< (string-length left-part) 30)
  791. (< (string-length help) 40))
  792. (make-string (- 30 (string-length left-part)) #\ )
  793. "\n\t")))
  794. (display left-part)
  795. (display middle-part)
  796. (display help)
  797. (newline))))
  798. kw-desc))
  799. (define (transform-usage-lambda cases)
  800. (let* ((raw-usage (delq! 'else (map car cases)))
  801. (usage-sans-specials (map (lambda (x)
  802. (or (and (not (list? x)) x)
  803. (and (symbol? (car x)) #t)
  804. (and (boolean? (car x)) #t)
  805. x))
  806. raw-usage))
  807. (usage-desc (delq! #t usage-sans-specials))
  808. (kw-desc (map car usage-desc))
  809. (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
  810. (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
  811. (transmogrified-cases (map (lambda (case)
  812. (cons (let ((opts (car case)))
  813. (if (or (boolean? opts) (eq? 'else opts))
  814. opts
  815. (cond
  816. ((symbol? (car opts)) opts)
  817. ((boolean? (car opts)) opts)
  818. ((string? (caar opts)) (cdar opts))
  819. (else (car opts)))))
  820. (cdr case)))
  821. cases)))
  822. `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
  823. (lambda (%argv)
  824. (let %next-arg ((%argv %argv))
  825. (get-option %argv
  826. ',kw-opts
  827. ',kw-args
  828. (lambda (%opt %arg %new-argv)
  829. (case %opt
  830. ,@ transmogrified-cases))))))))
  831. ;;; {Low Level Modules}
  832. ;;;
  833. ;;; These are the low level data structures for modules.
  834. ;;;
  835. ;;; !!! warning: The interface to lazy binder procedures is going
  836. ;;; to be changed in an incompatible way to permit all the basic
  837. ;;; module ops to be virtualized.
  838. ;;;
  839. ;;; (make-module size use-list lazy-binding-proc) => module
  840. ;;; module-{obarray,uses,binder}[|-set!]
  841. ;;; (module? obj) => [#t|#f]
  842. ;;; (module-locally-bound? module symbol) => [#t|#f]
  843. ;;; (module-bound? module symbol) => [#t|#f]
  844. ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
  845. ;;; (module-symbol-interned? module symbol) => [#t|#f]
  846. ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
  847. ;;; (module-variable module symbol) => [#<variable ...> | #f]
  848. ;;; (module-symbol-binding module symbol opt-value)
  849. ;;; => [ <obj> | opt-value | an error occurs ]
  850. ;;; (module-make-local-var! module symbol) => #<variable...>
  851. ;;; (module-add! module symbol var) => unspecified
  852. ;;; (module-remove! module symbol) => unspecified
  853. ;;; (module-for-each proc module) => unspecified
  854. ;;; (make-scm-module) => module ; a lazy copy of the symhash module
  855. ;;; (set-current-module module) => unspecified
  856. ;;; (current-module) => #<module...>
  857. ;;;
  858. ;;;
  859. ;;; {Printing Modules}
  860. ;; This is how modules are printed. You can re-define it.
  861. ;; (Redefining is actually more complicated than simply redefining
  862. ;; %print-module because that would only change the binding and not
  863. ;; the value stored in the vtable that determines how record are
  864. ;; printed. Sigh.)
  865. (define (%print-module mod port) ; unused args: depth length style table)
  866. (display "#<" port)
  867. (display (or (module-kind mod) "module") port)
  868. (let ((name (module-name mod)))
  869. (if name
  870. (begin
  871. (display " " port)
  872. (display name port))))
  873. (display " " port)
  874. (display (number->string (object-address mod) 16) port)
  875. (display ">" port))
  876. ;; module-type
  877. ;;
  878. ;; A module is characterized by an obarray in which local symbols
  879. ;; are interned, a list of modules, "uses", from which non-local
  880. ;; bindings can be inherited, and an optional lazy-binder which
  881. ;; is a (CLOSURE module symbol) which, as a last resort, can provide
  882. ;; bindings that would otherwise not be found locally in the module.
  883. ;;
  884. (define module-type
  885. (make-record-type 'module
  886. '(obarray uses binder eval-closure transformer name kind
  887. observers weak-observers observer-id)
  888. %print-module))
  889. ;; make-module &opt size uses binder
  890. ;;
  891. ;; Create a new module, perhaps with a particular size of obarray,
  892. ;; initial uses list, or binding procedure.
  893. ;;
  894. (define make-module
  895. (lambda args
  896. (define (parse-arg index default)
  897. (if (> (length args) index)
  898. (list-ref args index)
  899. default))
  900. (if (> (length args) 3)
  901. (error "Too many args to make-module." args))
  902. (let ((size (parse-arg 0 1021))
  903. (uses (parse-arg 1 '()))
  904. (binder (parse-arg 2 #f)))
  905. (if (not (integer? size))
  906. (error "Illegal size to make-module." size))
  907. (if (not (and (list? uses)
  908. (and-map module? uses)))
  909. (error "Incorrect use list." uses))
  910. (if (and binder (not (procedure? binder)))
  911. (error
  912. "Lazy-binder expected to be a procedure or #f." binder))
  913. (let ((module (module-constructor (make-vector size '())
  914. uses binder #f #f #f #f
  915. '()
  916. (make-weak-value-hash-table 31)
  917. 0)))
  918. ;; We can't pass this as an argument to module-constructor,
  919. ;; because we need it to close over a pointer to the module
  920. ;; itself.
  921. (set-module-eval-closure! module (standard-eval-closure module))
  922. module))))
  923. (define module-constructor (record-constructor module-type))
  924. (define module-obarray (record-accessor module-type 'obarray))
  925. (define set-module-obarray! (record-modifier module-type 'obarray))
  926. (define module-uses (record-accessor module-type 'uses))
  927. (define set-module-uses! (record-modifier module-type 'uses))
  928. (define module-binder (record-accessor module-type 'binder))
  929. (define set-module-binder! (record-modifier module-type 'binder))
  930. ;; NOTE: This binding is used in libguile/modules.c.
  931. (define module-eval-closure (record-accessor module-type 'eval-closure))
  932. (define module-transformer (record-accessor module-type 'transformer))
  933. (define set-module-transformer! (record-modifier module-type 'transformer))
  934. (define module-name (record-accessor module-type 'name))
  935. (define set-module-name! (record-modifier module-type 'name))
  936. (define module-kind (record-accessor module-type 'kind))
  937. (define set-module-kind! (record-modifier module-type 'kind))
  938. (define module-observers (record-accessor module-type 'observers))
  939. (define set-module-observers! (record-modifier module-type 'observers))
  940. (define module-weak-observers (record-accessor module-type 'weak-observers))
  941. (define module-observer-id (record-accessor module-type 'observer-id))
  942. (define set-module-observer-id! (record-modifier module-type 'observer-id))
  943. (define module? (record-predicate module-type))
  944. (define set-module-eval-closure!
  945. (let ((setter (record-modifier module-type 'eval-closure)))
  946. (lambda (module closure)
  947. (setter module closure)
  948. ;; Make it possible to lookup the module from the environment.
  949. ;; This implementation is correct since an eval closure can belong
  950. ;; to maximally one module.
  951. (set-procedure-property! closure 'module module))))
  952. (define (eval-in-module exp module)
  953. (eval2 exp (module-eval-closure module)))
  954. ;;; {Observer protocol}
  955. ;;;
  956. (define (module-observe module proc)
  957. (set-module-observers! module (cons proc (module-observers module)))
  958. (cons module proc))
  959. (define (module-observe-weak module proc)
  960. (let ((id (module-observer-id module)))
  961. (hash-set! (module-weak-observers module) id proc)
  962. (set-module-observer-id! module (+ 1 id))
  963. (cons module id)))
  964. (define (module-unobserve token)
  965. (let ((module (car token))
  966. (id (cdr token)))
  967. (if (integer? id)
  968. (hash-remove! (module-weak-observers module) id)
  969. (set-module-observers! module (delq1! id (module-observers module)))))
  970. *unspecified*)
  971. (define (module-modified m)
  972. (for-each (lambda (proc) (proc m)) (module-observers m))
  973. (hash-fold (lambda (id proc res) (proc m)) #f (module-weak-observers m)))
  974. ;;; {Module Searching in General}
  975. ;;;
  976. ;;; We sometimes want to look for properties of a symbol
  977. ;;; just within the obarray of one module. If the property
  978. ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
  979. ;;; DISPLAY is locally rebound in the module `safe-guile'.''
  980. ;;;
  981. ;;;
  982. ;;; Other times, we want to test for a symbol property in the obarray
  983. ;;; of M and, if it is not found there, try each of the modules in the
  984. ;;; uses list of M. This is the normal way of testing for some
  985. ;;; property, so we state these properties without qualification as
  986. ;;; in: ``The symbol 'fnord is interned in module M because it is
  987. ;;; interned locally in module M2 which is a member of the uses list
  988. ;;; of M.''
  989. ;;;
  990. ;; module-search fn m
  991. ;;
  992. ;; return the first non-#f result of FN applied to M and then to
  993. ;; the modules in the uses of m, and so on recursively. If all applications
  994. ;; return #f, then so does this function.
  995. ;;
  996. (define (module-search fn m v)
  997. (define (loop pos)
  998. (and (pair? pos)
  999. (or (module-search fn (car pos) v)
  1000. (loop (cdr pos)))))
  1001. (or (fn m v)
  1002. (loop (module-uses m))))
  1003. ;;; {Is a symbol bound in a module?}
  1004. ;;;
  1005. ;;; Symbol S in Module M is bound if S is interned in M and if the binding
  1006. ;;; of S in M has been set to some well-defined value.
  1007. ;;;
  1008. ;; module-locally-bound? module symbol
  1009. ;;
  1010. ;; Is a symbol bound (interned and defined) locally in a given module?
  1011. ;;
  1012. (define (module-locally-bound? m v)
  1013. (let ((var (module-local-variable m v)))
  1014. (and var
  1015. (variable-bound? var))))
  1016. ;; module-bound? module symbol
  1017. ;;
  1018. ;; Is a symbol bound (interned and defined) anywhere in a given module
  1019. ;; or its uses?
  1020. ;;
  1021. (define (module-bound? m v)
  1022. (module-search module-locally-bound? m v))
  1023. ;;; {Is a symbol interned in a module?}
  1024. ;;;
  1025. ;;; Symbol S in Module M is interned if S occurs in
  1026. ;;; of S in M has been set to some well-defined value.
  1027. ;;;
  1028. ;;; It is possible to intern a symbol in a module without providing
  1029. ;;; an initial binding for the corresponding variable. This is done
  1030. ;;; with:
  1031. ;;; (module-add! module symbol (make-undefined-variable))
  1032. ;;;
  1033. ;;; In that case, the symbol is interned in the module, but not
  1034. ;;; bound there. The unbound symbol shadows any binding for that
  1035. ;;; symbol that might otherwise be inherited from a member of the uses list.
  1036. ;;;
  1037. (define (module-obarray-get-handle ob key)
  1038. ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
  1039. (define (module-obarray-ref ob key)
  1040. ((if (symbol? key) hashq-ref hash-ref) ob key))
  1041. (define (module-obarray-set! ob key val)
  1042. ((if (symbol? key) hashq-set! hash-set!) ob key val))
  1043. (define (module-obarray-remove! ob key)
  1044. ((if (symbol? key) hashq-remove! hash-remove!) ob key))
  1045. ;; module-symbol-locally-interned? module symbol
  1046. ;;
  1047. ;; is a symbol interned (not neccessarily defined) locally in a given module
  1048. ;; or its uses? Interned symbols shadow inherited bindings even if
  1049. ;; they are not themselves bound to a defined value.
  1050. ;;
  1051. (define (module-symbol-locally-interned? m v)
  1052. (not (not (module-obarray-get-handle (module-obarray m) v))))
  1053. ;; module-symbol-interned? module symbol
  1054. ;;
  1055. ;; is a symbol interned (not neccessarily defined) anywhere in a given module
  1056. ;; or its uses? Interned symbols shadow inherited bindings even if
  1057. ;; they are not themselves bound to a defined value.
  1058. ;;
  1059. (define (module-symbol-interned? m v)
  1060. (module-search module-symbol-locally-interned? m v))
  1061. ;;; {Mapping modules x symbols --> variables}
  1062. ;;;
  1063. ;; module-local-variable module symbol
  1064. ;; return the local variable associated with a MODULE and SYMBOL.
  1065. ;;
  1066. ;;; This function is very important. It is the only function that can
  1067. ;;; return a variable from a module other than the mutators that store
  1068. ;;; new variables in modules. Therefore, this function is the location
  1069. ;;; of the "lazy binder" hack.
  1070. ;;;
  1071. ;;; If symbol is defined in MODULE, and if the definition binds symbol
  1072. ;;; to a variable, return that variable object.
  1073. ;;;
  1074. ;;; If the symbols is not found at first, but the module has a lazy binder,
  1075. ;;; then try the binder.
  1076. ;;;
  1077. ;;; If the symbol is not found at all, return #f.
  1078. ;;;
  1079. (define (module-local-variable m v)
  1080. ; (caddr
  1081. ; (list m v
  1082. (let ((b (module-obarray-ref (module-obarray m) v)))
  1083. (or (and (variable? b) b)
  1084. (and (module-binder m)
  1085. ((module-binder m) m v #f)))))
  1086. ;))
  1087. ;; module-variable module symbol
  1088. ;;
  1089. ;; like module-local-variable, except search the uses in the
  1090. ;; case V is not found in M.
  1091. ;;
  1092. ;; NOTE: This function is superseded with C code (see modules.c)
  1093. ;;; when using the standard eval closure.
  1094. ;;
  1095. (define (module-variable m v)
  1096. (module-search module-local-variable m v))
  1097. ;;; {Mapping modules x symbols --> bindings}
  1098. ;;;
  1099. ;;; These are similar to the mapping to variables, except that the
  1100. ;;; variable is dereferenced.
  1101. ;;;
  1102. ;; module-symbol-binding module symbol opt-value
  1103. ;;
  1104. ;; return the binding of a variable specified by name within
  1105. ;; a given module, signalling an error if the variable is unbound.
  1106. ;; If the OPT-VALUE is passed, then instead of signalling an error,
  1107. ;; return OPT-VALUE.
  1108. ;;
  1109. (define (module-symbol-local-binding m v . opt-val)
  1110. (let ((var (module-local-variable m v)))
  1111. (if var
  1112. (variable-ref var)
  1113. (if (not (null? opt-val))
  1114. (car opt-val)
  1115. (error "Locally unbound variable." v)))))
  1116. ;; module-symbol-binding module symbol opt-value
  1117. ;;
  1118. ;; return the binding of a variable specified by name within
  1119. ;; a given module, signalling an error if the variable is unbound.
  1120. ;; If the OPT-VALUE is passed, then instead of signalling an error,
  1121. ;; return OPT-VALUE.
  1122. ;;
  1123. (define (module-symbol-binding m v . opt-val)
  1124. (let ((var (module-variable m v)))
  1125. (if var
  1126. (variable-ref var)
  1127. (if (not (null? opt-val))
  1128. (car opt-val)
  1129. (error "Unbound variable." v)))))
  1130. ;;; {Adding Variables to Modules}
  1131. ;;;
  1132. ;;;
  1133. ;; module-make-local-var! module symbol
  1134. ;;
  1135. ;; ensure a variable for V in the local namespace of M.
  1136. ;; If no variable was already there, then create a new and uninitialzied
  1137. ;; variable.
  1138. ;;
  1139. (define (module-make-local-var! m v)
  1140. (or (let ((b (module-obarray-ref (module-obarray m) v)))
  1141. (and (variable? b)
  1142. (begin
  1143. (module-modified m)
  1144. b)))
  1145. (and (module-binder m)
  1146. ((module-binder m) m v #t))
  1147. (begin
  1148. (let ((answer (make-undefined-variable v)))
  1149. (module-obarray-set! (module-obarray m) v answer)
  1150. (module-modified m)
  1151. answer))))
  1152. ;; module-add! module symbol var
  1153. ;;
  1154. ;; ensure a particular variable for V in the local namespace of M.
  1155. ;;
  1156. (define (module-add! m v var)
  1157. (if (not (variable? var))
  1158. (error "Bad variable to module-add!" var))
  1159. (module-obarray-set! (module-obarray m) v var)
  1160. (module-modified m))
  1161. ;; module-remove!
  1162. ;;
  1163. ;; make sure that a symbol is undefined in the local namespace of M.
  1164. ;;
  1165. (define (module-remove! m v)
  1166. (module-obarray-remove! (module-obarray m) v)
  1167. (module-modified m))
  1168. (define (module-clear! m)
  1169. (vector-fill! (module-obarray m) '())
  1170. (module-modified m))
  1171. ;; MODULE-FOR-EACH -- exported
  1172. ;;
  1173. ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
  1174. ;;
  1175. (define (module-for-each proc module)
  1176. (let ((obarray (module-obarray module)))
  1177. (do ((index 0 (+ index 1))
  1178. (end (vector-length obarray)))
  1179. ((= index end))
  1180. (for-each
  1181. (lambda (bucket)
  1182. (proc (car bucket) (cdr bucket)))
  1183. (vector-ref obarray index)))))
  1184. (define (module-map proc module)
  1185. (let* ((obarray (module-obarray module))
  1186. (end (vector-length obarray)))
  1187. (let loop ((i 0)
  1188. (answer '()))
  1189. (if (= i end)
  1190. answer
  1191. (loop (+ 1 i)
  1192. (append!
  1193. (map (lambda (bucket)
  1194. (proc (car bucket) (cdr bucket)))
  1195. (vector-ref obarray i))
  1196. answer))))))
  1197. ;;; {Low Level Bootstrapping}
  1198. ;;;
  1199. ;; make-root-module
  1200. ;; A root module uses the symhash table (the system's privileged
  1201. ;; obarray). Being inside a root module is like using SCM without
  1202. ;; any module system.
  1203. ;;
  1204. (define (root-module-closure m s define?)
  1205. (let ((bi (and (symbol-interned? #f s)
  1206. (builtin-variable s))))
  1207. (and bi
  1208. (or define? (variable-bound? bi))
  1209. (begin
  1210. (module-add! m s bi)
  1211. bi))))
  1212. (define (make-root-module)
  1213. (make-module 1019 '() root-module-closure))
  1214. ;; make-scm-module
  1215. ;; An scm module is a module into which the lazy binder copies
  1216. ;; variable bindings from the system symhash table. The mapping is
  1217. ;; one way only; newly introduced bindings in an scm module are not
  1218. ;; copied back into the system symhash table (and can be used to override
  1219. ;; bindings from the symhash table).
  1220. ;;
  1221. (define (scm-module-closure m s define?)
  1222. (let ((bi (and (symbol-interned? #f s)
  1223. (builtin-variable s))))
  1224. (and bi
  1225. (variable-bound? bi)
  1226. (begin
  1227. (module-add! m s bi)
  1228. bi))))
  1229. (define (make-scm-module)
  1230. (make-module 1019 '() scm-module-closure))
  1231. ;; the-module
  1232. ;;
  1233. ;; NOTE: This binding is used in libguile/modules.c.
  1234. ;;
  1235. (define the-module #f)
  1236. ;; scm:eval-transformer
  1237. ;;
  1238. (define scm:eval-transformer #f)
  1239. ;; set-current-module module
  1240. ;;
  1241. ;; set the current module as viewed by the normalizer.
  1242. ;;
  1243. ;; NOTE: This binding is used in libguile/modules.c.
  1244. ;;
  1245. (define (set-current-module m)
  1246. (set! the-module m)
  1247. (if m
  1248. (begin
  1249. (set! *top-level-lookup-closure* (module-eval-closure the-module))
  1250. (set! scm:eval-transformer (module-transformer the-module)))
  1251. (set! *top-level-lookup-closure* #f)))
  1252. ;; current-module
  1253. ;;
  1254. ;; return the current module as viewed by the normalizer.
  1255. ;;
  1256. (define (current-module) the-module)
  1257. ;;; {Module-based Loading}
  1258. ;;;
  1259. (define (save-module-excursion thunk)
  1260. (let ((inner-module (current-module))
  1261. (outer-module #f))
  1262. (dynamic-wind (lambda ()
  1263. (set! outer-module (current-module))
  1264. (set-current-module inner-module)
  1265. (set! inner-module #f))
  1266. thunk
  1267. (lambda ()
  1268. (set! inner-module (current-module))
  1269. (set-current-module outer-module)
  1270. (set! outer-module #f)))))
  1271. (define basic-load load)
  1272. (define (load-module filename)
  1273. (save-module-excursion
  1274. (lambda ()
  1275. (let ((oldname (and (current-load-port)
  1276. (port-filename (current-load-port)))))
  1277. (basic-load (if (and oldname
  1278. (> (string-length filename) 0)
  1279. (not (char=? (string-ref filename 0) #\/))
  1280. (not (string=? (dirname oldname) ".")))
  1281. (string-append (dirname oldname) "/" filename)
  1282. filename))))))
  1283. ;;; {MODULE-REF -- exported}
  1284. ;;
  1285. ;; Returns the value of a variable called NAME in MODULE or any of its
  1286. ;; used modules. If there is no such variable, then if the optional third
  1287. ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
  1288. ;;
  1289. (define (module-ref module name . rest)
  1290. (let ((variable (module-variable module name)))
  1291. (if (and variable (variable-bound? variable))
  1292. (variable-ref variable)
  1293. (if (null? rest)
  1294. (error "No variable named" name 'in module)
  1295. (car rest) ; default value
  1296. ))))
  1297. ;; MODULE-SET! -- exported
  1298. ;;
  1299. ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
  1300. ;; to VALUE; if there is no such variable, an error is signaled.
  1301. ;;
  1302. (define (module-set! module name value)
  1303. (let ((variable (module-variable module name)))
  1304. (if variable
  1305. (variable-set! variable value)
  1306. (error "No variable named" name 'in module))))
  1307. ;; MODULE-DEFINE! -- exported
  1308. ;;
  1309. ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
  1310. ;; variable, it is added first.
  1311. ;;
  1312. (define (module-define! module name value)
  1313. (let ((variable (module-local-variable module name)))
  1314. (if variable
  1315. (begin
  1316. (variable-set! variable value)
  1317. (module-modified module))
  1318. (module-add! module name (make-variable value name)))))
  1319. ;; MODULE-DEFINED? -- exported
  1320. ;;
  1321. ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
  1322. ;; uses)
  1323. ;;
  1324. (define (module-defined? module name)
  1325. (let ((variable (module-variable module name)))
  1326. (and variable (variable-bound? variable))))
  1327. ;; MODULE-USE! module interface
  1328. ;;
  1329. ;; Add INTERFACE to the list of interfaces used by MODULE.
  1330. ;;
  1331. (define (module-use! module interface)
  1332. (set-module-uses! module
  1333. (cons interface (delq! interface (module-uses module))))
  1334. (module-modified module))
  1335. ;;; {Recursive Namespaces}
  1336. ;;;
  1337. ;;;
  1338. ;;; A hierarchical namespace emerges if we consider some module to be
  1339. ;;; root, and variables bound to modules as nested namespaces.
  1340. ;;;
  1341. ;;; The routines in this file manage variable names in hierarchical namespace.
  1342. ;;; Each variable name is a list of elements, looked up in successively nested
  1343. ;;; modules.
  1344. ;;;
  1345. ;;; (nested-ref some-root-module '(foo bar baz))
  1346. ;;; => <value of a variable named baz in the module bound to bar in
  1347. ;;; the module bound to foo in some-root-module>
  1348. ;;;
  1349. ;;;
  1350. ;;; There are:
  1351. ;;;
  1352. ;;; ;; a-root is a module
  1353. ;;; ;; name is a list of symbols
  1354. ;;;
  1355. ;;; nested-ref a-root name
  1356. ;;; nested-set! a-root name val
  1357. ;;; nested-define! a-root name val
  1358. ;;; nested-remove! a-root name
  1359. ;;;
  1360. ;;;
  1361. ;;; (current-module) is a natural choice for a-root so for convenience there are
  1362. ;;; also:
  1363. ;;;
  1364. ;;; local-ref name == nested-ref (current-module) name
  1365. ;;; local-set! name val == nested-set! (current-module) name val
  1366. ;;; local-define! name val == nested-define! (current-module) name val
  1367. ;;; local-remove! name == nested-remove! (current-module) name
  1368. ;;;
  1369. (define (nested-ref root names)
  1370. (let loop ((cur root)
  1371. (elts names))
  1372. (cond
  1373. ((null? elts) cur)
  1374. ((not (module? cur)) #f)
  1375. (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
  1376. (define (nested-set! root names val)
  1377. (let loop ((cur root)
  1378. (elts names))
  1379. (if (null? (cdr elts))
  1380. (module-set! cur (car elts) val)
  1381. (loop (module-ref cur (car elts)) (cdr elts)))))
  1382. (define (nested-define! root names val)
  1383. (let loop ((cur root)
  1384. (elts names))
  1385. (if (null? (cdr elts))
  1386. (module-define! cur (car elts) val)
  1387. (loop (module-ref cur (car elts)) (cdr elts)))))
  1388. (define (nested-remove! root names)
  1389. (let loop ((cur root)
  1390. (elts names))
  1391. (if (null? (cdr elts))
  1392. (module-remove! cur (car elts))
  1393. (loop (module-ref cur (car elts)) (cdr elts)))))
  1394. (define (local-ref names) (nested-ref (current-module) names))
  1395. (define (local-set! names val) (nested-set! (current-module) names val))
  1396. (define (local-define names val) (nested-define! (current-module) names val))
  1397. (define (local-remove names) (nested-remove! (current-module) names))
  1398. ;;; {The (app) module}
  1399. ;;;
  1400. ;;; The root of conventionally named objects not directly in the top level.
  1401. ;;;
  1402. ;;; (app modules)
  1403. ;;; (app modules guile)
  1404. ;;;
  1405. ;;; The directory of all modules and the standard root module.
  1406. ;;;
  1407. (define (module-public-interface m)
  1408. (module-ref m '%module-public-interface #f))
  1409. (define (set-module-public-interface! m i)
  1410. (module-define! m '%module-public-interface i))
  1411. (define (set-system-module! m s)
  1412. (set-procedure-property! (module-eval-closure m) 'system-module s))
  1413. (define the-root-module (make-root-module))
  1414. (define the-scm-module (make-scm-module))
  1415. (set-module-public-interface! the-root-module the-scm-module)
  1416. (set-module-name! the-root-module '(guile))
  1417. (set-module-name! the-scm-module '(guile))
  1418. (set-module-kind! the-scm-module 'interface)
  1419. (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t))
  1420. (set-current-module the-root-module)
  1421. (define app (make-module 31))
  1422. (local-define '(app modules) (make-module 31))
  1423. (local-define '(app modules guile) the-root-module)
  1424. ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
  1425. (define (try-load-module name)
  1426. (or (try-module-linked name)
  1427. (try-module-autoload name)
  1428. (try-module-dynamic-link name)))
  1429. ;; NOTE: This binding is used in libguile/modules.c.
  1430. ;;
  1431. (define (resolve-module name . maybe-autoload)
  1432. (let ((full-name (append '(app modules) name)))
  1433. (let ((already (local-ref full-name)))
  1434. (if already
  1435. ;; The module already exists...
  1436. (if (and (or (null? maybe-autoload) (car maybe-autoload))
  1437. (not (module-ref already '%module-public-interface #f)))
  1438. ;; ...but we are told to load and it doesn't contain source, so
  1439. (begin
  1440. (try-load-module name)
  1441. already)
  1442. ;; simply return it.
  1443. already)
  1444. (begin
  1445. ;; Try to autoload it if we are told so
  1446. (if (or (null? maybe-autoload) (car maybe-autoload))
  1447. (try-load-module name))
  1448. ;; Get/create it.
  1449. (make-modules-in (current-module) full-name))))))
  1450. (define (beautify-user-module! module)
  1451. (let ((interface (module-public-interface module)))
  1452. (if (or (not interface)
  1453. (eq? interface module))
  1454. (let ((interface (make-module 31)))
  1455. (set-module-name! interface (module-name module))
  1456. (set-module-kind! interface 'interface)
  1457. (set-module-public-interface! module interface))))
  1458. (if (and (not (memq the-scm-module (module-uses module)))
  1459. (not (eq? module the-root-module)))
  1460. (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
  1461. ;; NOTE: This binding is used in libguile/modules.c.
  1462. ;;
  1463. (define (make-modules-in module name)
  1464. (if (null? name)
  1465. module
  1466. (cond
  1467. ((module-ref module (car name) #f)
  1468. => (lambda (m) (make-modules-in m (cdr name))))
  1469. (else (let ((m (make-module 31)))
  1470. (set-module-kind! m 'directory)
  1471. (set-module-name! m (append (or (module-name module)
  1472. '())
  1473. (list (car name))))
  1474. (module-define! module (car name) m)
  1475. (make-modules-in m (cdr name)))))))
  1476. (define (resolve-interface name)
  1477. (let ((module (resolve-module name)))
  1478. (and module (module-public-interface module))))
  1479. (define %autoloader-developer-mode #t)
  1480. (define (process-define-module args)
  1481. (let* ((module-id (car args))
  1482. (module (resolve-module module-id #f))
  1483. (kws (cdr args)))
  1484. (beautify-user-module! module)
  1485. (let loop ((kws kws)
  1486. (reversed-interfaces '()))
  1487. (if (null? kws)
  1488. (for-each (lambda (interface)
  1489. (module-use! module interface))
  1490. reversed-interfaces)
  1491. (let ((keyword (cond ((keyword? (car kws))
  1492. (keyword->symbol (car kws)))
  1493. ((and (symbol? (car kws))
  1494. (eq? (string-ref (car kws) 0) #\:))
  1495. (string->symbol (substring (car kws) 1)))
  1496. (else #f))))
  1497. (case keyword
  1498. ((use-module use-syntax)
  1499. (if (not (pair? (cdr kws)))
  1500. (error "unrecognized defmodule argument" kws))
  1501. (let* ((used-name (cadr kws))
  1502. (used-module (resolve-module used-name)))
  1503. (if (not (module-ref used-module
  1504. '%module-public-interface
  1505. #f))
  1506. (begin
  1507. ((if %autoloader-developer-mode warn error)
  1508. "no code for module" (module-name used-module))
  1509. (beautify-user-module! used-module)))
  1510. (let ((interface (module-public-interface used-module)))
  1511. (if (not interface)
  1512. (error "missing interface for use-module"
  1513. used-module))
  1514. (if (eq? keyword 'use-syntax)
  1515. (set-module-transformer!
  1516. module
  1517. (module-ref interface (car (last-pair used-name))
  1518. #f)))
  1519. (loop (cddr kws)
  1520. (cons interface reversed-interfaces)))))
  1521. ((autoload)
  1522. (if (not (and (pair? (cdr kws)) (pair? (cddr kws))))
  1523. (error "unrecognized defmodule argument" kws))
  1524. (loop (cdddr kws)
  1525. (cons (make-autoload-interface module
  1526. (cadr kws)
  1527. (caddr kws))
  1528. reversed-interfaces)))
  1529. ((no-backtrace)
  1530. (set-system-module! module #t)
  1531. (loop (cdr kws) reversed-interfaces))
  1532. (else
  1533. (error "unrecognized defmodule argument" kws))))))
  1534. module))
  1535. ;;; {Autoload}
  1536. (define (make-autoload-interface module name bindings)
  1537. (let ((b (lambda (a sym definep)
  1538. (and (memq sym bindings)
  1539. (let ((i (module-public-interface (resolve-module name))))
  1540. (if (not i)
  1541. (error "missing interface for module" name))
  1542. ;; Replace autoload-interface with interface
  1543. (set-car! (memq a (module-uses module)) i)
  1544. (module-local-variable i sym))))))
  1545. (module-constructor #() '() b #f #f name 'autoload
  1546. '() (make-weak-value-hash-table 31) 0)))
  1547. ;;; {Autoloading modules}
  1548. (define autoloads-in-progress '())
  1549. (define (try-module-autoload module-name)
  1550. (let* ((reverse-name (reverse module-name))
  1551. (name (car reverse-name))
  1552. (dir-hint-module-name (reverse (cdr reverse-name)))
  1553. (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name))))
  1554. (resolve-module dir-hint-module-name #f)
  1555. (and (not (autoload-done-or-in-progress? dir-hint name))
  1556. (let ((didit #f))
  1557. (dynamic-wind
  1558. (lambda () (autoload-in-progress! dir-hint name))
  1559. (lambda ()
  1560. (let ((full (%search-load-path (in-vicinity dir-hint name))))
  1561. (if full
  1562. (begin
  1563. (save-module-excursion (lambda () (primitive-load full)))
  1564. (set! didit #t)))))
  1565. (lambda () (set-autoloaded! dir-hint name didit)))
  1566. didit))))
  1567. ;;; Dynamic linking of modules
  1568. ;; Initializing a module that is written in C is a two step process.
  1569. ;; First the module's `module init' function is called. This function
  1570. ;; is expected to call `scm_register_module_xxx' to register the `real
  1571. ;; init' function. Later, when the module is referenced for the first
  1572. ;; time, this real init function is called in the right context. See
  1573. ;; gtcltk-lib/gtcltk-module.c for an example.
  1574. ;;
  1575. ;; The code for the module can be in a regular shared library (so that
  1576. ;; the `module init' function will be called when libguile is
  1577. ;; initialized). Or it can be dynamically linked.
  1578. ;;
  1579. ;; You can safely call `scm_register_module_xxx' before libguile
  1580. ;; itself is initialized. You could call it from an C++ constructor
  1581. ;; of a static object, for example.
  1582. ;;
  1583. ;; To make your Guile extension into a dynamic linkable module, follow
  1584. ;; these easy steps:
  1585. ;;
  1586. ;; - Find a name for your module, like (ice-9 gtcltk)
  1587. ;; - Write a function with a name like
  1588. ;;
  1589. ;; scm_init_ice_9_gtcltk_module
  1590. ;;
  1591. ;; This is your `module init' function. It should call
  1592. ;;
  1593. ;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk);
  1594. ;;
  1595. ;; "ice-9 gtcltk" is the C version of the module name. Slashes are
  1596. ;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is
  1597. ;; the real init function that executes the usual initializations
  1598. ;; like making new smobs, etc.
  1599. ;;
  1600. ;; - Make a shared library with your code and a name like
  1601. ;;
  1602. ;; ice-9/libgtcltk.so
  1603. ;;
  1604. ;; and put it somewhere in %load-path.
  1605. ;;
  1606. ;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it
  1607. ;; will be linked automatically.
  1608. ;;
  1609. ;; This is all very experimental.
  1610. (define (split-c-module-name str)
  1611. (let loop ((rev '())
  1612. (start 0)
  1613. (pos 0)
  1614. (end (string-length str)))
  1615. (cond
  1616. ((= pos end)
  1617. (reverse (cons (string->symbol (substring str start pos)) rev)))
  1618. ((eq? (string-ref str pos) #\space)
  1619. (loop (cons (string->symbol (substring str start pos)) rev)
  1620. (+ pos 1)
  1621. (+ pos 1)
  1622. end))
  1623. (else
  1624. (loop rev start (+ pos 1) end)))))
  1625. (define (convert-c-registered-modules dynobj)
  1626. (let ((res (map (lambda (c)
  1627. (list (split-c-module-name (car c)) (cdr c) dynobj))
  1628. (c-registered-modules))))
  1629. (c-clear-registered-modules)
  1630. res))
  1631. (define registered-modules '())
  1632. (define (register-modules dynobj)
  1633. (set! registered-modules
  1634. (append! (convert-c-registered-modules dynobj)
  1635. registered-modules)))
  1636. (define (init-dynamic-module modname)
  1637. ;; Register any linked modules which has been registered on the C level
  1638. (register-modules #f)
  1639. (or-map (lambda (modinfo)
  1640. (if (equal? (car modinfo) modname)
  1641. (begin
  1642. (set! registered-modules (delq! modinfo registered-modules))
  1643. (let ((mod (resolve-module modname #f)))
  1644. (save-module-excursion
  1645. (lambda ()
  1646. (set-current-module mod)
  1647. (set-module-public-interface! mod mod)
  1648. (dynamic-call (cadr modinfo) (caddr modinfo))
  1649. ))
  1650. #t))
  1651. #f))
  1652. registered-modules))
  1653. (define (dynamic-maybe-call name dynobj)
  1654. (catch #t ; could use false-if-exception here
  1655. (lambda ()
  1656. (dynamic-call name dynobj))
  1657. (lambda args
  1658. #f)))
  1659. (define (dynamic-maybe-link filename)
  1660. (catch #t ; could use false-if-exception here
  1661. (lambda ()
  1662. (dynamic-link filename))
  1663. (lambda args
  1664. #f)))
  1665. (define (find-and-link-dynamic-module module-name)
  1666. (define (make-init-name mod-name)
  1667. (string-append "scm_init"
  1668. (list->string (map (lambda (c)
  1669. (if (or (char-alphabetic? c)
  1670. (char-numeric? c))
  1671. c
  1672. #\_))
  1673. (string->list mod-name)))
  1674. "_module"))
  1675. ;; Put the subdirectory for this module in the car of SUBDIR-AND-LIBNAME,
  1676. ;; and the `libname' (the name of the module prepended by `lib') in the cdr
  1677. ;; field. For example, if MODULE-NAME is the list (inet tcp-ip udp), then
  1678. ;; SUBDIR-AND-LIBNAME will be the pair ("inet/tcp-ip" . "libudp").
  1679. (let ((subdir-and-libname
  1680. (let loop ((dirs "")
  1681. (syms module-name))
  1682. (if (null? (cdr syms))
  1683. (cons dirs (string-append "lib" (car syms)))
  1684. (loop (string-append dirs (car syms) "/") (cdr syms)))))
  1685. (init (make-init-name (apply string-append
  1686. (map (lambda (s)
  1687. (string-append "_" s))
  1688. module-name)))))
  1689. (let ((subdir (car subdir-and-libname))
  1690. (libname (cdr subdir-and-libname)))
  1691. ;; Now look in each dir in %LOAD-PATH for `subdir/libfoo.la'. If that
  1692. ;; file exists, fetch the dlname from that file and attempt to link
  1693. ;; against it. If `subdir/libfoo.la' does not exist, or does not seem
  1694. ;; to name any shared library, look for `subdir/libfoo.so' instead and
  1695. ;; link against that.
  1696. (let check-dirs ((dir-list %load-path))
  1697. (if (null? dir-list)
  1698. #f
  1699. (let* ((dir (in-vicinity (car dir-list) subdir))
  1700. (sharlib-full
  1701. (or (try-using-libtool-name dir libname)
  1702. (try-using-sharlib-name dir libname))))
  1703. (if (and sharlib-full (file-exists? sharlib-full))
  1704. (link-dynamic-module sharlib-full init)
  1705. (check-dirs (cdr dir-list)))))))))
  1706. (define (try-using-libtool-name libdir libname)
  1707. (let ((libtool-filename (in-vicinity libdir
  1708. (string-append libname ".la"))))
  1709. (and (file-exists? libtool-filename)
  1710. libtool-filename)))
  1711. (define (try-using-sharlib-name libdir libname)
  1712. (in-vicinity libdir (string-append libname ".so")))
  1713. (define (link-dynamic-module filename initname)
  1714. ;; Register any linked modules which has been registered on the C level
  1715. (register-modules #f)
  1716. (let ((dynobj (dynamic-link filename)))
  1717. (dynamic-call initname dynobj)
  1718. (register-modules dynobj)))
  1719. (define (try-module-linked module-name)
  1720. (init-dynamic-module module-name))
  1721. (define (try-module-dynamic-link module-name)
  1722. (and (find-and-link-dynamic-module module-name)
  1723. (init-dynamic-module module-name)))
  1724. (define autoloads-done '((guile . guile)))
  1725. (define (autoload-done-or-in-progress? p m)
  1726. (let ((n (cons p m)))
  1727. (->bool (or (member n autoloads-done)
  1728. (member n autoloads-in-progress)))))
  1729. (define (autoload-done! p m)
  1730. (let ((n (cons p m)))
  1731. (set! autoloads-in-progress
  1732. (delete! n autoloads-in-progress))
  1733. (or (member n autoloads-done)
  1734. (set! autoloads-done (cons n autoloads-done)))))
  1735. (define (autoload-in-progress! p m)
  1736. (let ((n (cons p m)))
  1737. (set! autoloads-done
  1738. (delete! n autoloads-done))
  1739. (set! autoloads-in-progress (cons n autoloads-in-progress))))
  1740. (define (set-autoloaded! p m done?)
  1741. (if done?
  1742. (autoload-done! p m)
  1743. (let ((n (cons p m)))
  1744. (set! autoloads-done (delete! n autoloads-done))
  1745. (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
  1746. ;;; {Macros}
  1747. ;;;
  1748. (define (primitive-macro? m)
  1749. (and (macro? m)
  1750. (not (macro-transformer m))))
  1751. ;;; {Defmacros}
  1752. ;;;
  1753. (define macro-table (make-weak-key-hash-table 523))
  1754. (define xformer-table (make-weak-key-hash-table 523))
  1755. (define (defmacro? m) (hashq-ref macro-table m))
  1756. (define (assert-defmacro?! m) (hashq-set! macro-table m #t))
  1757. (define (defmacro-transformer m) (hashq-ref xformer-table m))
  1758. (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
  1759. (define defmacro:transformer
  1760. (lambda (f)
  1761. (let* ((xform (lambda (exp env)
  1762. (copy-tree (apply f (cdr exp)))))
  1763. (a (procedure->memoizing-macro xform)))
  1764. (assert-defmacro?! a)
  1765. (set-defmacro-transformer! a f)
  1766. a)))
  1767. (define defmacro
  1768. (let ((defmacro-transformer
  1769. (lambda (name parms . body)
  1770. (let ((transformer `(lambda ,parms ,@body)))
  1771. `(define ,name
  1772. (,(lambda (transformer)
  1773. (defmacro:transformer transformer))
  1774. ,transformer))))))
  1775. (defmacro:transformer defmacro-transformer)))
  1776. (define defmacro:syntax-transformer
  1777. (lambda (f)
  1778. (procedure->syntax
  1779. (lambda (exp env)
  1780. (copy-tree (apply f (cdr exp)))))))
  1781. ;; XXX - should the definition of the car really be looked up in the
  1782. ;; current module?
  1783. (define (macroexpand-1 e)
  1784. (cond
  1785. ((pair? e) (let* ((a (car e))
  1786. (val (and (symbol? a) (local-ref (list a)))))
  1787. (if (defmacro? val)
  1788. (apply (defmacro-transformer val) (cdr e))
  1789. e)))
  1790. (#t e)))
  1791. (define (macroexpand e)
  1792. (cond
  1793. ((pair? e) (let* ((a (car e))
  1794. (val (and (symbol? a) (local-ref (list a)))))
  1795. (if (defmacro? val)
  1796. (macroexpand (apply (defmacro-transformer val) (cdr e)))
  1797. e)))
  1798. (#t e)))
  1799. (define (gentemp)
  1800. (gensym "scm:G"))
  1801. (provide 'defmacro)
  1802. ;;; {Run-time options}
  1803. ((let* ((names '((eval-options-interface
  1804. (eval-options eval-enable eval-disable)
  1805. (eval-set!))
  1806. (debug-options-interface
  1807. (debug-options debug-enable debug-disable)
  1808. (debug-set!))
  1809. (evaluator-traps-interface
  1810. (traps trap-enable trap-disable)
  1811. (trap-set!))
  1812. (read-options-interface
  1813. (read-options read-enable read-disable)
  1814. (read-set!))
  1815. (print-options-interface
  1816. (print-options print-enable print-disable)
  1817. (print-set!))
  1818. (readline-options-interface
  1819. (readline-options readline-enable readline-disable)
  1820. (readline-set!))
  1821. ))
  1822. (option-name car)
  1823. (option-value cadr)
  1824. (option-documentation caddr)
  1825. (print-option (lambda (option)
  1826. (display (option-name option))
  1827. (if (< (string-length
  1828. (symbol->string (option-name option)))
  1829. 8)
  1830. (display #\tab))
  1831. (display #\tab)
  1832. (display (option-value option))
  1833. (display #\tab)
  1834. (display (option-documentation option))
  1835. (newline)))
  1836. ;; Below follows the macros defining the run-time option interfaces.
  1837. (make-options (lambda (interface)
  1838. `(lambda args
  1839. (cond ((null? args) (,interface))
  1840. ((list? (car args))
  1841. (,interface (car args)) (,interface))
  1842. (else (for-each ,print-option
  1843. (,interface #t)))))))
  1844. (make-enable (lambda (interface)
  1845. `(lambda flags
  1846. (,interface (append flags (,interface)))
  1847. (,interface))))
  1848. (make-disable (lambda (interface)
  1849. `(lambda flags
  1850. (let ((options (,interface)))
  1851. (for-each (lambda (flag)
  1852. (set! options (delq! flag options)))
  1853. flags)
  1854. (,interface options)
  1855. (,interface)))))
  1856. (make-set! (lambda (interface)
  1857. `((name exp)
  1858. (,'quasiquote
  1859. (begin (,interface (append (,interface)
  1860. (list '(,'unquote name)
  1861. (,'unquote exp))))
  1862. (,interface))))))
  1863. )
  1864. (procedure->macro
  1865. (lambda (exp env)
  1866. (cons 'begin
  1867. (apply append
  1868. (map (lambda (group)
  1869. (let ((interface (car group)))
  1870. (append (map (lambda (name constructor)
  1871. `(define ,name
  1872. ,(constructor interface)))
  1873. (cadr group)
  1874. (list make-options
  1875. make-enable
  1876. make-disable))
  1877. (map (lambda (name constructor)
  1878. `(defmacro ,name
  1879. ,@(constructor interface)))
  1880. (caddr group)
  1881. (list make-set!)))))
  1882. names)))))))
  1883. ;;; {Running Repls}
  1884. ;;;
  1885. (define (repl read evaler print)
  1886. (let loop ((source (read (current-input-port))))
  1887. (print (evaler source))
  1888. (loop (read (current-input-port)))))
  1889. ;; A provisional repl that acts like the SCM repl:
  1890. ;;
  1891. (define scm-repl-silent #f)
  1892. (define (assert-repl-silence v) (set! scm-repl-silent v))
  1893. (define *unspecified* (if #f #f))
  1894. (define (unspecified? v) (eq? v *unspecified*))
  1895. (define scm-repl-print-unspecified #f)
  1896. (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
  1897. (define scm-repl-verbose #f)
  1898. (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
  1899. (define scm-repl-prompt "guile> ")
  1900. (define (set-repl-prompt! v) (set! scm-repl-prompt v))
  1901. (define (default-lazy-handler key . args)
  1902. (save-stack lazy-handler-dispatch)
  1903. (apply throw key args))
  1904. (define enter-frame-handler default-lazy-handler)
  1905. (define apply-frame-handler default-lazy-handler)
  1906. (define exit-frame-handler default-lazy-handler)
  1907. (define (lazy-handler-dispatch key . args)
  1908. (case key
  1909. ((apply-frame)
  1910. (apply apply-frame-handler key args))
  1911. ((exit-frame)
  1912. (apply exit-frame-handler key args))
  1913. ((enter-frame)
  1914. (apply enter-frame-handler key args))
  1915. (else
  1916. (apply default-lazy-handler key args))))
  1917. (define abort-hook (make-hook))
  1918. ;; these definitions are used if running a script.
  1919. ;; otherwise redefined in error-catching-loop.
  1920. (define (set-batch-mode?! arg) #t)
  1921. (define (batch-mode?) #t)
  1922. (define (error-catching-loop thunk)
  1923. (let ((status #f)
  1924. (interactive #t))
  1925. (define (loop first)
  1926. (let ((next
  1927. (catch #t
  1928. (lambda ()
  1929. (lazy-catch #t
  1930. (lambda ()
  1931. (dynamic-wind
  1932. (lambda () (unmask-signals))
  1933. (lambda ()
  1934. (with-traps
  1935. (lambda ()
  1936. (first)
  1937. ;; This line is needed because mark
  1938. ;; doesn't do closures quite right.
  1939. ;; Unreferenced locals should be
  1940. ;; collected.
  1941. ;;
  1942. (set! first #f)
  1943. (let loop ((v (thunk)))
  1944. (loop (thunk)))
  1945. #f)))
  1946. (lambda () (mask-signals))))
  1947. lazy-handler-dispatch))
  1948. (lambda (key . args)
  1949. (case key
  1950. ((quit)
  1951. (set! status args)
  1952. #f)
  1953. ((switch-repl)
  1954. (apply throw 'switch-repl args))
  1955. ((abort)
  1956. ;; This is one of the closures that require
  1957. ;; (set! first #f) above
  1958. ;;
  1959. (lambda ()
  1960. (run-hook abort-hook)
  1961. (force-output (current-output-port))
  1962. (display "ABORT: " (current-error-port))
  1963. (write args (current-error-port))
  1964. (newline (current-error-port))
  1965. (if interactive
  1966. (begin
  1967. (if (and
  1968. (not has-shown-debugger-hint?)
  1969. (not (memq 'backtrace
  1970. (debug-options-interface)))
  1971. (stack? (fluid-ref the-last-stack)))
  1972. (begin
  1973. (newline (current-error-port))
  1974. (display
  1975. "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
  1976. (current-error-port))
  1977. (set! has-shown-debugger-hint? #t)))
  1978. (force-output (current-error-port)))
  1979. (begin
  1980. (primitive-exit 1)))
  1981. (set! stack-saved? #f)))
  1982. (else
  1983. ;; This is the other cons-leak closure...
  1984. (lambda ()
  1985. (cond ((= (length args) 4)
  1986. (apply handle-system-error key args))
  1987. (else
  1988. (apply bad-throw key args))))))))))
  1989. (if next (loop next) status)))
  1990. (set! set-batch-mode?! (lambda (arg)
  1991. (cond (arg
  1992. (set! interactive #f)
  1993. (restore-signals))
  1994. (#t
  1995. (error "sorry, not implemented")))))
  1996. (set! batch-mode? (lambda () (not interactive)))
  1997. (loop (lambda () #t))))
  1998. ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
  1999. (define before-signal-stack (make-fluid))
  2000. (define stack-saved? #f)
  2001. (define (save-stack . narrowing)
  2002. (or stack-saved?
  2003. (cond ((not (memq 'debug (debug-options-interface)))
  2004. (fluid-set! the-last-stack #f)
  2005. (set! stack-saved? #t))
  2006. (else
  2007. (fluid-set!
  2008. the-last-stack
  2009. (case (stack-id #t)
  2010. ((repl-stack)
  2011. (apply make-stack #t save-stack eval #t 0 narrowing))
  2012. ((load-stack)
  2013. (apply make-stack #t save-stack 0 #t 0 narrowing))
  2014. ((tk-stack)
  2015. (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
  2016. ((#t)
  2017. (apply make-stack #t save-stack 0 1 narrowing))
  2018. (else
  2019. (let ((id (stack-id #t)))
  2020. (and (procedure? id)
  2021. (apply make-stack #t save-stack id #t 0 narrowing))))))
  2022. (set! stack-saved? #t)))))
  2023. (define before-error-hook (make-hook))
  2024. (define after-error-hook (make-hook))
  2025. (define before-backtrace-hook (make-hook))
  2026. (define after-backtrace-hook (make-hook))
  2027. (define has-shown-debugger-hint? #f)
  2028. (define (handle-system-error key . args)
  2029. (let ((cep (current-error-port)))
  2030. (cond ((not (stack? (fluid-ref the-last-stack))))
  2031. ((memq 'backtrace (debug-options-interface))
  2032. (run-hook before-backtrace-hook)
  2033. (newline cep)
  2034. (display "Backtrace:\n")
  2035. (display-backtrace (fluid-ref the-last-stack) cep)
  2036. (newline cep)
  2037. (run-hook after-backtrace-hook)))
  2038. (run-hook before-error-hook)
  2039. (apply display-error (fluid-ref the-last-stack) cep args)
  2040. (run-hook after-error-hook)
  2041. (force-output cep)
  2042. (throw 'abort key)))
  2043. (define (quit . args)
  2044. (apply throw 'quit args))
  2045. (define exit quit)
  2046. ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
  2047. ;; Replaced by C code:
  2048. ;;(define (backtrace)
  2049. ;; (if (fluid-ref the-last-stack)
  2050. ;; (begin
  2051. ;; (newline)
  2052. ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
  2053. ;; (newline)
  2054. ;; (if (and (not has-shown-backtrace-hint?)
  2055. ;; (not (memq 'backtrace (debug-options-interface))))
  2056. ;; (begin
  2057. ;; (display
  2058. ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
  2059. ;;automatically if an error occurs in the future.\n")
  2060. ;; (set! has-shown-backtrace-hint? #t))))
  2061. ;; (display "No backtrace available.\n")))
  2062. (define (error-catching-repl r e p)
  2063. (error-catching-loop (lambda () (p (e (r))))))
  2064. (define (gc-run-time)
  2065. (cdr (assq 'gc-time-taken (gc-stats))))
  2066. (define before-read-hook (make-hook))
  2067. (define after-read-hook (make-hook))
  2068. ;;; The default repl-reader function. We may override this if we've
  2069. ;;; the readline library.
  2070. (define repl-reader
  2071. (lambda (prompt)
  2072. (display prompt)
  2073. (force-output)
  2074. (run-hook before-read-hook)
  2075. (read (current-input-port))))
  2076. (define (scm-style-repl)
  2077. (letrec (
  2078. (start-gc-rt #f)
  2079. (start-rt #f)
  2080. (repl-report-start-timing (lambda ()
  2081. (set! start-gc-rt (gc-run-time))
  2082. (set! start-rt (get-internal-run-time))))
  2083. (repl-report (lambda ()
  2084. (display ";;; ")
  2085. (display (inexact->exact
  2086. (* 1000 (/ (- (get-internal-run-time) start-rt)
  2087. internal-time-units-per-second))))
  2088. (display " msec (")
  2089. (display (inexact->exact
  2090. (* 1000 (/ (- (gc-run-time) start-gc-rt)
  2091. internal-time-units-per-second))))
  2092. (display " msec in gc)\n")))
  2093. (consume-trailing-whitespace
  2094. (lambda ()
  2095. (let ((ch (peek-char)))
  2096. (cond
  2097. ((eof-object? ch))
  2098. ((or (char=? ch #\space) (char=? ch #\tab))
  2099. (read-char)
  2100. (consume-trailing-whitespace))
  2101. ((char=? ch #\newline)
  2102. (read-char))))))
  2103. (-read (lambda ()
  2104. (let ((val
  2105. (let ((prompt (cond ((string? scm-repl-prompt)
  2106. scm-repl-prompt)
  2107. ((thunk? scm-repl-prompt)
  2108. (scm-repl-prompt))
  2109. (scm-repl-prompt "> ")
  2110. (else ""))))
  2111. (repl-reader prompt))))
  2112. ;; As described in R4RS, the READ procedure updates the
  2113. ;; port to point to the first character past the end of
  2114. ;; the external representation of the object. This
  2115. ;; means that it doesn't consume the newline typically
  2116. ;; found after an expression. This means that, when
  2117. ;; debugging Guile with GDB, GDB gets the newline, which
  2118. ;; it often interprets as a "continue" command, making
  2119. ;; breakpoints kind of useless. So, consume any
  2120. ;; trailing newline here, as well as any whitespace
  2121. ;; before it.
  2122. ;; But not if EOF, for control-D.
  2123. (if (not (eof-object? val))
  2124. (consume-trailing-whitespace))
  2125. (run-hook after-read-hook)
  2126. (if (eof-object? val)
  2127. (begin
  2128. (repl-report-start-timing)
  2129. (if scm-repl-verbose
  2130. (begin
  2131. (newline)
  2132. (display ";;; EOF -- quitting")
  2133. (newline)))
  2134. (quit 0)))
  2135. val)))
  2136. (-eval (lambda (sourc)
  2137. (repl-report-start-timing)
  2138. (start-stack 'repl-stack (eval sourc))))
  2139. (-print (lambda (result)
  2140. (if (not scm-repl-silent)
  2141. (begin
  2142. (if (or scm-repl-print-unspecified
  2143. (not (unspecified? result)))
  2144. (begin
  2145. (write result)
  2146. (newline)))
  2147. (if scm-repl-verbose
  2148. (repl-report))
  2149. (force-output)))))
  2150. (-quit (lambda (args)
  2151. (if scm-repl-verbose
  2152. (begin
  2153. (display ";;; QUIT executed, repl exitting")
  2154. (newline)
  2155. (repl-report)))
  2156. args))
  2157. (-abort (lambda ()
  2158. (if scm-repl-verbose
  2159. (begin
  2160. (display ";;; ABORT executed.")
  2161. (newline)
  2162. (repl-report)))
  2163. (repl -read -eval -print))))
  2164. (let ((status (error-catching-repl -read
  2165. -eval
  2166. -print)))
  2167. (-quit status))))
  2168. ;;; {IOTA functions: generating lists of numbers}
  2169. (define (iota n)
  2170. (let loop ((count (1- n)) (result '()))
  2171. (if (< count 0) result
  2172. (loop (1- count) (cons count result)))))
  2173. ;;; {While}
  2174. ;;;
  2175. ;;; with `continue' and `break'.
  2176. ;;;
  2177. (defmacro while (cond . body)
  2178. `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
  2179. (break (lambda val (apply throw 'break val))))
  2180. (catch 'break
  2181. (lambda () (continue))
  2182. (lambda v (cadr v)))))
  2183. ;;; {collect}
  2184. ;;;
  2185. ;;; Similar to `begin' but returns a list of the results of all constituent
  2186. ;;; forms instead of the result of the last form.
  2187. ;;; (The definition relies on the current left-to-right
  2188. ;;; order of evaluation of operands in applications.)
  2189. (defmacro collect forms
  2190. (cons 'list forms))
  2191. ;;; {with-fluids}
  2192. ;; with-fluids is a convenience wrapper for the builtin procedure
  2193. ;; `with-fluids*'. The syntax is just like `let':
  2194. ;;
  2195. ;; (with-fluids ((fluid val)
  2196. ;; ...)
  2197. ;; body)
  2198. (defmacro with-fluids (bindings . body)
  2199. `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings))
  2200. (lambda () ,@body)))
  2201. ;;; Environments
  2202. (define the-environment
  2203. (procedure->syntax
  2204. (lambda (x e)
  2205. e)))
  2206. (define (environment-module env)
  2207. (let ((closure (and (pair? env) (car (last-pair env)))))
  2208. (and closure (procedure-property closure 'module))))
  2209. ;;; {Macros}
  2210. ;;;
  2211. ;; actually....hobbit might be able to hack these with a little
  2212. ;; coaxing
  2213. ;;
  2214. (defmacro define-macro (first . rest)
  2215. (let ((name (if (symbol? first) first (car first)))
  2216. (transformer
  2217. (if (symbol? first)
  2218. (car rest)
  2219. `(lambda ,(cdr first) ,@rest))))
  2220. `(define ,name (defmacro:transformer ,transformer))))
  2221. (defmacro define-syntax-macro (first . rest)
  2222. (let ((name (if (symbol? first) first (car first)))
  2223. (transformer
  2224. (if (symbol? first)
  2225. (car rest)
  2226. `(lambda ,(cdr first) ,@rest))))
  2227. `(define ,name (defmacro:syntax-transformer ,transformer))))
  2228. ;;; {Module System Macros}
  2229. ;;;
  2230. (defmacro define-module args
  2231. `(let* ((process-define-module process-define-module)
  2232. (set-current-module set-current-module)
  2233. (module (process-define-module ',args)))
  2234. (set-current-module module)
  2235. module))
  2236. ;; the guts of the use-modules macro. add the interfaces of the named
  2237. ;; modules to the use-list of the current module, in order
  2238. (define (process-use-modules module-names)
  2239. (for-each (lambda (module-name)
  2240. (let ((mod-iface (resolve-interface module-name)))
  2241. (or mod-iface
  2242. (error "no such module" module-name))
  2243. (module-use! (current-module) mod-iface)))
  2244. (reverse module-names)))
  2245. (defmacro use-modules modules
  2246. `(process-use-modules ',modules))
  2247. (defmacro use-syntax (spec)
  2248. `(begin
  2249. ,@(if (pair? spec)
  2250. `((process-use-modules ',(list spec))
  2251. (set-module-transformer! (current-module)
  2252. ,(car (last-pair spec))))
  2253. `((set-module-transformer! (current-module) ,spec)))
  2254. (set! scm:eval-transformer (module-transformer (current-module)))))
  2255. (define define-private define)
  2256. (defmacro define-public args
  2257. (define (syntax)
  2258. (error "bad syntax" (list 'define-public args)))
  2259. (define (defined-name n)
  2260. (cond
  2261. ((symbol? n) n)
  2262. ((pair? n) (defined-name (car n)))
  2263. (else (syntax))))
  2264. (cond
  2265. ((null? args) (syntax))
  2266. (#t (let ((name (defined-name (car args))))
  2267. `(begin
  2268. (let ((public-i (module-public-interface (current-module))))
  2269. ;; Make sure there is a local variable:
  2270. ;;
  2271. (module-define! (current-module)
  2272. ',name
  2273. (module-ref (current-module) ',name #f))
  2274. ;; Make sure that local is exported:
  2275. ;;
  2276. (module-add! public-i ',name
  2277. (module-variable (current-module) ',name)))
  2278. ;; Now (re)define the var normally. Bernard URBAN
  2279. ;; suggests we use eval here to accomodate Hobbit; it lets
  2280. ;; the interpreter handle the define-private form, which
  2281. ;; Hobbit can't digest.
  2282. (eval '(define-private ,@ args)))))))
  2283. (defmacro defmacro-public args
  2284. (define (syntax)
  2285. (error "bad syntax" (list 'defmacro-public args)))
  2286. (define (defined-name n)
  2287. (cond
  2288. ((symbol? n) n)
  2289. (else (syntax))))
  2290. (cond
  2291. ((null? args) (syntax))
  2292. (#t (let ((name (defined-name (car args))))
  2293. `(begin
  2294. (let ((public-i (module-public-interface (current-module))))
  2295. ;; Make sure there is a local variable:
  2296. ;;
  2297. (module-define! (current-module)
  2298. ',name
  2299. (module-ref (current-module) ',name #f))
  2300. ;; Make sure that local is exported:
  2301. ;;
  2302. (module-add! public-i ',name (module-variable (current-module) ',name)))
  2303. ;; Now (re)define the var normally.
  2304. ;;
  2305. (defmacro ,@ args))))))
  2306. (defmacro export names
  2307. `(let* ((m (current-module))
  2308. (public-i (module-public-interface m)))
  2309. (for-each (lambda (name)
  2310. ;; Make sure there is a local variable:
  2311. (module-define! m name (module-ref m name #f))
  2312. ;; Make sure that local is exported:
  2313. (module-add! public-i name (module-variable m name)))
  2314. ',names)))
  2315. (define export-syntax export)
  2316. (define load load-module)
  2317. ;;; {Load emacs interface support if emacs option is given.}
  2318. (define (load-emacs-interface)
  2319. (if (memq 'debug-extensions *features*)
  2320. (debug-enable 'backtrace))
  2321. (define-module (guile-user) :use-module (ice-9 emacs)))
  2322. (define using-readline?
  2323. (let ((using-readline? (make-fluid)))
  2324. (make-procedure-with-setter
  2325. (lambda () (fluid-ref using-readline?))
  2326. (lambda (v) (fluid-set! using-readline? v)))))
  2327. ;; this is just (scm-style-repl) with a wrapper to install and remove
  2328. ;; signal handlers.
  2329. (define (top-repl)
  2330. ;; Load emacs interface support if emacs option is given.
  2331. (if (and (module-defined? the-root-module 'use-emacs-interface)
  2332. use-emacs-interface)
  2333. (load-emacs-interface))
  2334. ;; Place the user in the guile-user module.
  2335. (define-module (guile-user)
  2336. :use-module (guile) ;so that bindings will be checked here first
  2337. :use-module (ice-9 session)
  2338. :use-module (ice-9 debug)
  2339. :autoload (ice-9 debugger) (debug)) ;load debugger on demand
  2340. (if (memq 'threads *features*)
  2341. (define-module (guile-user) :use-module (ice-9 threads)))
  2342. (if (memq 'regex *features*)
  2343. (define-module (guile-user) :use-module (ice-9 regex)))
  2344. (let ((old-handlers #f)
  2345. (signals (if (provided? 'posix)
  2346. `((,SIGINT . "User interrupt")
  2347. (,SIGFPE . "Arithmetic error")
  2348. (,SIGBUS . "Bad memory access (bus error)")
  2349. (,SIGSEGV .
  2350. "Bad memory access (Segmentation violation)"))
  2351. '())))
  2352. (dynamic-wind
  2353. ;; call at entry
  2354. (lambda ()
  2355. (let ((make-handler (lambda (msg)
  2356. (lambda (sig)
  2357. ;; Make a backup copy of the stack
  2358. (fluid-set! before-signal-stack
  2359. (fluid-ref the-last-stack))
  2360. (save-stack %deliver-signals)
  2361. (scm-error 'signal
  2362. #f
  2363. msg
  2364. #f
  2365. (list sig))))))
  2366. (set! old-handlers
  2367. (map (lambda (sig-msg)
  2368. (sigaction (car sig-msg)
  2369. (make-handler (cdr sig-msg))))
  2370. signals))))
  2371. ;; the protected thunk.
  2372. (lambda ()
  2373. (let ((status (scm-style-repl)))
  2374. (run-hook exit-hook)
  2375. status))
  2376. ;; call at exit.
  2377. (lambda ()
  2378. (map (lambda (sig-msg old-handler)
  2379. (if (not (car old-handler))
  2380. ;; restore original C handler.
  2381. (sigaction (car sig-msg) #f)
  2382. ;; restore Scheme handler, SIG_IGN or SIG_DFL.
  2383. (sigaction (car sig-msg)
  2384. (car old-handler)
  2385. (cdr old-handler))))
  2386. signals old-handlers)))))
  2387. (defmacro false-if-exception (expr)
  2388. `(catch #t (lambda () ,expr)
  2389. (lambda args #f)))
  2390. ;;; This hook is run at the very end of an interactive session.
  2391. ;;;
  2392. (define exit-hook (make-hook))
  2393. (define-module (guile))
  2394. (append! %load-path (cons "." ()))