psyntax-pp.scm 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433
  1. (eval-when (compile) (set-current-module (resolve-module (quote (guile)))))
  2. (if #f #f)
  3. (letrec*
  4. ((make-void
  5. (lambda (src) (make-struct (vector-ref %expanded-vtables 0) 0 src)))
  6. (make-const
  7. (lambda (src exp)
  8. (make-struct (vector-ref %expanded-vtables 1) 0 src exp)))
  9. (make-primitive-ref
  10. (lambda (src name)
  11. (make-struct (vector-ref %expanded-vtables 2) 0 src name)))
  12. (make-lexical-ref
  13. (lambda (src name gensym)
  14. (make-struct (vector-ref %expanded-vtables 3) 0 src name gensym)))
  15. (make-lexical-set
  16. (lambda (src name gensym exp)
  17. (make-struct (vector-ref %expanded-vtables 4) 0 src name gensym exp)))
  18. (make-module-ref
  19. (lambda (src mod name public?)
  20. (make-struct (vector-ref %expanded-vtables 5) 0 src mod name public?)))
  21. (make-module-set
  22. (lambda (src mod name public? exp)
  23. (make-struct
  24. (vector-ref %expanded-vtables 6)
  25. 0
  26. src
  27. mod
  28. name
  29. public?
  30. exp)))
  31. (make-toplevel-ref
  32. (lambda (src name)
  33. (make-struct (vector-ref %expanded-vtables 7) 0 src name)))
  34. (make-toplevel-set
  35. (lambda (src name exp)
  36. (make-struct (vector-ref %expanded-vtables 8) 0 src name exp)))
  37. (make-toplevel-define
  38. (lambda (src name exp)
  39. (make-struct (vector-ref %expanded-vtables 9) 0 src name exp)))
  40. (make-conditional
  41. (lambda (src test consequent alternate)
  42. (make-struct
  43. (vector-ref %expanded-vtables 10)
  44. 0
  45. src
  46. test
  47. consequent
  48. alternate)))
  49. (make-call
  50. (lambda (src proc args)
  51. (make-struct (vector-ref %expanded-vtables 11) 0 src proc args)))
  52. (make-primcall
  53. (lambda (src name args)
  54. (make-struct (vector-ref %expanded-vtables 12) 0 src name args)))
  55. (make-seq
  56. (lambda (src head tail)
  57. (make-struct (vector-ref %expanded-vtables 13) 0 src head tail)))
  58. (make-lambda
  59. (lambda (src meta body)
  60. (make-struct (vector-ref %expanded-vtables 14) 0 src meta body)))
  61. (make-lambda-case
  62. (lambda (src req opt rest kw inits gensyms body alternate)
  63. (make-struct
  64. (vector-ref %expanded-vtables 15)
  65. 0
  66. src
  67. req
  68. opt
  69. rest
  70. kw
  71. inits
  72. gensyms
  73. body
  74. alternate)))
  75. (make-let
  76. (lambda (src names gensyms vals body)
  77. (make-struct
  78. (vector-ref %expanded-vtables 16)
  79. 0
  80. src
  81. names
  82. gensyms
  83. vals
  84. body)))
  85. (make-letrec
  86. (lambda (src in-order? names gensyms vals body)
  87. (make-struct
  88. (vector-ref %expanded-vtables 17)
  89. 0
  90. src
  91. in-order?
  92. names
  93. gensyms
  94. vals
  95. body)))
  96. (lambda?
  97. (lambda (x)
  98. (and (struct? x)
  99. (eq? (struct-vtable x) (vector-ref %expanded-vtables 14)))))
  100. (lambda-meta (lambda (x) (struct-ref x 1)))
  101. (set-lambda-meta! (lambda (x v) (struct-set! x 1 v)))
  102. (top-level-eval-hook (lambda (x mod) (primitive-eval x)))
  103. (local-eval-hook (lambda (x mod) (primitive-eval x)))
  104. (session-id
  105. (let ((v (module-variable (current-module) 'syntax-session-id)))
  106. (lambda () ((variable-ref v)))))
  107. (put-global-definition-hook
  108. (lambda (symbol type val)
  109. (module-define!
  110. (current-module)
  111. symbol
  112. (make-syntax-transformer symbol type val))))
  113. (get-global-definition-hook
  114. (lambda (symbol module)
  115. (if (and (not module) (current-module))
  116. (warn "module system is booted, we should have a module" symbol))
  117. (and (not (equal? module '(primitive)))
  118. (let ((v (module-variable
  119. (if module (resolve-module (cdr module)) (current-module))
  120. symbol)))
  121. (and v
  122. (variable-bound? v)
  123. (let ((val (variable-ref v)))
  124. (and (macro? val)
  125. (macro-type val)
  126. (cons (macro-type val) (macro-binding val)))))))))
  127. (decorate-source
  128. (lambda (e s)
  129. (if (and s (supports-source-properties? e))
  130. (set-source-properties! e s))
  131. e))
  132. (maybe-name-value!
  133. (lambda (name val)
  134. (if (lambda? val)
  135. (let ((meta (lambda-meta val)))
  136. (if (not (assq 'name meta))
  137. (set-lambda-meta! val (acons 'name name meta)))))))
  138. (build-void (lambda (source) (make-void source)))
  139. (build-call
  140. (lambda (source fun-exp arg-exps)
  141. (make-call source fun-exp arg-exps)))
  142. (build-conditional
  143. (lambda (source test-exp then-exp else-exp)
  144. (make-conditional source test-exp then-exp else-exp)))
  145. (build-lexical-reference
  146. (lambda (type source name var) (make-lexical-ref source name var)))
  147. (build-lexical-assignment
  148. (lambda (source name var exp)
  149. (maybe-name-value! name exp)
  150. (make-lexical-set source name var exp)))
  151. (analyze-variable
  152. (lambda (mod var modref-cont bare-cont)
  153. (if (not mod)
  154. (bare-cont var)
  155. (let ((kind (car mod)) (mod (cdr mod)))
  156. (let ((key kind))
  157. (cond ((memv key '(public)) (modref-cont mod var #t))
  158. ((memv key '(private))
  159. (if (not (equal? mod (module-name (current-module))))
  160. (modref-cont mod var #f)
  161. (bare-cont var)))
  162. ((memv key '(bare)) (bare-cont var))
  163. ((memv key '(hygiene))
  164. (if (and (not (equal? mod (module-name (current-module))))
  165. (module-variable (resolve-module mod) var))
  166. (modref-cont mod var #f)
  167. (bare-cont var)))
  168. ((memv key '(primitive))
  169. (syntax-violation #f "primitive not in operator position" var))
  170. (else (syntax-violation #f "bad module kind" var mod))))))))
  171. (build-global-reference
  172. (lambda (source var mod)
  173. (analyze-variable
  174. mod
  175. var
  176. (lambda (mod var public?) (make-module-ref source mod var public?))
  177. (lambda (var) (make-toplevel-ref source var)))))
  178. (build-global-assignment
  179. (lambda (source var exp mod)
  180. (maybe-name-value! var exp)
  181. (analyze-variable
  182. mod
  183. var
  184. (lambda (mod var public?)
  185. (make-module-set source mod var public? exp))
  186. (lambda (var) (make-toplevel-set source var exp)))))
  187. (build-global-definition
  188. (lambda (source var exp)
  189. (maybe-name-value! var exp)
  190. (make-toplevel-define source var exp)))
  191. (build-simple-lambda
  192. (lambda (src req rest vars meta exp)
  193. (make-lambda
  194. src
  195. meta
  196. (make-lambda-case src req #f rest #f '() vars exp #f))))
  197. (build-case-lambda
  198. (lambda (src meta body) (make-lambda src meta body)))
  199. (build-lambda-case
  200. (lambda (src req opt rest kw inits vars body else-case)
  201. (make-lambda-case src req opt rest kw inits vars body else-case)))
  202. (build-primcall
  203. (lambda (src name args) (make-primcall src name args)))
  204. (build-primref (lambda (src name) (make-primitive-ref src name)))
  205. (build-data (lambda (src exp) (make-const src exp)))
  206. (build-sequence
  207. (lambda (src exps)
  208. (if (null? (cdr exps))
  209. (car exps)
  210. (make-seq src (car exps) (build-sequence #f (cdr exps))))))
  211. (build-let
  212. (lambda (src ids vars val-exps body-exp)
  213. (for-each maybe-name-value! ids val-exps)
  214. (if (null? vars) body-exp (make-let src ids vars val-exps body-exp))))
  215. (build-named-let
  216. (lambda (src ids vars val-exps body-exp)
  217. (let ((f (car vars)) (f-name (car ids)) (vars (cdr vars)) (ids (cdr ids)))
  218. (let ((proc (build-simple-lambda src ids #f vars '() body-exp)))
  219. (maybe-name-value! f-name proc)
  220. (for-each maybe-name-value! ids val-exps)
  221. (make-letrec
  222. src
  223. #f
  224. (list f-name)
  225. (list f)
  226. (list proc)
  227. (build-call src (build-lexical-reference 'fun src f-name f) val-exps))))))
  228. (build-letrec
  229. (lambda (src in-order? ids vars val-exps body-exp)
  230. (if (null? vars)
  231. body-exp
  232. (begin
  233. (for-each maybe-name-value! ids val-exps)
  234. (make-letrec src in-order? ids vars val-exps body-exp)))))
  235. (make-syntax-object
  236. (lambda (expression wrap module)
  237. (vector 'syntax-object expression wrap module)))
  238. (syntax-object?
  239. (lambda (x)
  240. (and (vector? x)
  241. (= (vector-length x) 4)
  242. (eq? (vector-ref x 0) 'syntax-object))))
  243. (syntax-object-expression (lambda (x) (vector-ref x 1)))
  244. (syntax-object-wrap (lambda (x) (vector-ref x 2)))
  245. (syntax-object-module (lambda (x) (vector-ref x 3)))
  246. (set-syntax-object-expression!
  247. (lambda (x update) (vector-set! x 1 update)))
  248. (set-syntax-object-wrap!
  249. (lambda (x update) (vector-set! x 2 update)))
  250. (set-syntax-object-module!
  251. (lambda (x update) (vector-set! x 3 update)))
  252. (source-annotation
  253. (lambda (x)
  254. (let ((props (source-properties
  255. (if (syntax-object? x) (syntax-object-expression x) x))))
  256. (and (pair? props) props))))
  257. (extend-env
  258. (lambda (labels bindings r)
  259. (if (null? labels)
  260. r
  261. (extend-env
  262. (cdr labels)
  263. (cdr bindings)
  264. (cons (cons (car labels) (car bindings)) r)))))
  265. (extend-var-env
  266. (lambda (labels vars r)
  267. (if (null? labels)
  268. r
  269. (extend-var-env
  270. (cdr labels)
  271. (cdr vars)
  272. (cons (cons (car labels) (cons 'lexical (car vars))) r)))))
  273. (macros-only-env
  274. (lambda (r)
  275. (if (null? r)
  276. '()
  277. (let ((a (car r)))
  278. (if (memq (cadr a) '(macro syntax-parameter ellipsis))
  279. (cons a (macros-only-env (cdr r)))
  280. (macros-only-env (cdr r)))))))
  281. (global-extend
  282. (lambda (type sym val) (put-global-definition-hook sym type val)))
  283. (nonsymbol-id?
  284. (lambda (x)
  285. (and (syntax-object? x) (symbol? (syntax-object-expression x)))))
  286. (id? (lambda (x)
  287. (if (symbol? x)
  288. #t
  289. (and (syntax-object? x) (symbol? (syntax-object-expression x))))))
  290. (id-sym-name&marks
  291. (lambda (x w)
  292. (if (syntax-object? x)
  293. (values
  294. (syntax-object-expression x)
  295. (join-marks (car w) (car (syntax-object-wrap x))))
  296. (values x (car w)))))
  297. (gen-label
  298. (lambda ()
  299. (string-append "l-" (session-id) (symbol->string (gensym "-")))))
  300. (gen-labels
  301. (lambda (ls)
  302. (if (null? ls) '() (cons (gen-label) (gen-labels (cdr ls))))))
  303. (make-ribcage
  304. (lambda (symnames marks labels)
  305. (vector 'ribcage symnames marks labels)))
  306. (ribcage?
  307. (lambda (x)
  308. (and (vector? x)
  309. (= (vector-length x) 4)
  310. (eq? (vector-ref x 0) 'ribcage))))
  311. (ribcage-symnames (lambda (x) (vector-ref x 1)))
  312. (ribcage-marks (lambda (x) (vector-ref x 2)))
  313. (ribcage-labels (lambda (x) (vector-ref x 3)))
  314. (set-ribcage-symnames! (lambda (x update) (vector-set! x 1 update)))
  315. (set-ribcage-marks! (lambda (x update) (vector-set! x 2 update)))
  316. (set-ribcage-labels! (lambda (x update) (vector-set! x 3 update)))
  317. (anti-mark
  318. (lambda (w) (cons (cons #f (car w)) (cons 'shift (cdr w)))))
  319. (extend-ribcage!
  320. (lambda (ribcage id label)
  321. (set-ribcage-symnames!
  322. ribcage
  323. (cons (syntax-object-expression id) (ribcage-symnames ribcage)))
  324. (set-ribcage-marks!
  325. ribcage
  326. (cons (car (syntax-object-wrap id)) (ribcage-marks ribcage)))
  327. (set-ribcage-labels! ribcage (cons label (ribcage-labels ribcage)))))
  328. (make-binding-wrap
  329. (lambda (ids labels w)
  330. (if (null? ids)
  331. w
  332. (cons (car w)
  333. (cons (let* ((labelvec (list->vector labels)) (n (vector-length labelvec)))
  334. (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
  335. (let f ((ids ids) (i 0))
  336. (if (not (null? ids))
  337. (call-with-values
  338. (lambda () (id-sym-name&marks (car ids) w))
  339. (lambda (symname marks)
  340. (vector-set! symnamevec i symname)
  341. (vector-set! marksvec i marks)
  342. (f (cdr ids) (+ i 1))))))
  343. (make-ribcage symnamevec marksvec labelvec)))
  344. (cdr w))))))
  345. (smart-append (lambda (m1 m2) (if (null? m2) m1 (append m1 m2))))
  346. (join-wraps
  347. (lambda (w1 w2)
  348. (let ((m1 (car w1)) (s1 (cdr w1)))
  349. (if (null? m1)
  350. (if (null? s1) w2 (cons (car w2) (smart-append s1 (cdr w2))))
  351. (cons (smart-append m1 (car w2)) (smart-append s1 (cdr w2)))))))
  352. (join-marks (lambda (m1 m2) (smart-append m1 m2)))
  353. (same-marks?
  354. (lambda (x y)
  355. (or (eq? x y)
  356. (and (not (null? x))
  357. (not (null? y))
  358. (eq? (car x) (car y))
  359. (same-marks? (cdr x) (cdr y))))))
  360. (id-var-name
  361. (lambda (id w mod)
  362. (letrec*
  363. ((search
  364. (lambda (sym subst marks mod)
  365. (if (null? subst)
  366. (values #f marks)
  367. (let ((fst (car subst)))
  368. (if (eq? fst 'shift)
  369. (search sym (cdr subst) (cdr marks) mod)
  370. (let ((symnames (ribcage-symnames fst)))
  371. (if (vector? symnames)
  372. (search-vector-rib sym subst marks symnames fst mod)
  373. (search-list-rib sym subst marks symnames fst mod))))))))
  374. (search-list-rib
  375. (lambda (sym subst marks symnames ribcage mod)
  376. (let f ((symnames symnames) (i 0))
  377. (cond ((null? symnames) (search sym (cdr subst) marks mod))
  378. ((and (eq? (car symnames) sym)
  379. (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
  380. (let ((n (list-ref (ribcage-labels ribcage) i)))
  381. (if (pair? n)
  382. (if (equal? mod (car n))
  383. (values (cdr n) marks)
  384. (f (cdr symnames) (+ i 1)))
  385. (values n marks))))
  386. (else (f (cdr symnames) (+ i 1)))))))
  387. (search-vector-rib
  388. (lambda (sym subst marks symnames ribcage mod)
  389. (let ((n (vector-length symnames)))
  390. (let f ((i 0))
  391. (cond ((= i n) (search sym (cdr subst) marks mod))
  392. ((and (eq? (vector-ref symnames i) sym)
  393. (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
  394. (let ((n (vector-ref (ribcage-labels ribcage) i)))
  395. (if (pair? n)
  396. (if (equal? mod (car n)) (values (cdr n) marks) (f (+ i 1)))
  397. (values n marks))))
  398. (else (f (+ i 1)))))))))
  399. (cond ((symbol? id) (or (search id (cdr w) (car w) mod) id))
  400. ((syntax-object? id)
  401. (let ((id (syntax-object-expression id))
  402. (w1 (syntax-object-wrap id))
  403. (mod (syntax-object-module id)))
  404. (let ((marks (join-marks (car w) (car w1))))
  405. (call-with-values
  406. (lambda () (search id (cdr w) marks mod))
  407. (lambda (new-id marks) (or new-id (search id (cdr w1) marks mod) id))))))
  408. (else (syntax-violation 'id-var-name "invalid id" id))))))
  409. (locally-bound-identifiers
  410. (lambda (w mod)
  411. (letrec*
  412. ((scan (lambda (subst results)
  413. (if (null? subst)
  414. results
  415. (let ((fst (car subst)))
  416. (if (eq? fst 'shift)
  417. (scan (cdr subst) results)
  418. (let ((symnames (ribcage-symnames fst)) (marks (ribcage-marks fst)))
  419. (if (vector? symnames)
  420. (scan-vector-rib subst symnames marks results)
  421. (scan-list-rib subst symnames marks results))))))))
  422. (scan-list-rib
  423. (lambda (subst symnames marks results)
  424. (let f ((symnames symnames) (marks marks) (results results))
  425. (if (null? symnames)
  426. (scan (cdr subst) results)
  427. (f (cdr symnames)
  428. (cdr marks)
  429. (cons (wrap (car symnames) (anti-mark (cons (car marks) subst)) mod)
  430. results))))))
  431. (scan-vector-rib
  432. (lambda (subst symnames marks results)
  433. (let ((n (vector-length symnames)))
  434. (let f ((i 0) (results results))
  435. (if (= i n)
  436. (scan (cdr subst) results)
  437. (f (+ i 1)
  438. (cons (wrap (vector-ref symnames i)
  439. (anti-mark (cons (vector-ref marks i) subst))
  440. mod)
  441. results))))))))
  442. (scan (cdr w) '()))))
  443. (resolve-identifier
  444. (lambda (id w r mod resolve-syntax-parameters?)
  445. (letrec*
  446. ((resolve-syntax-parameters
  447. (lambda (b)
  448. (if (and resolve-syntax-parameters? (eq? (car b) 'syntax-parameter))
  449. (or (assq-ref r (cdr b)) (cons 'macro (car (cdr b))))
  450. b)))
  451. (resolve-global
  452. (lambda (var mod)
  453. (let ((b (resolve-syntax-parameters
  454. (or (get-global-definition-hook var mod) '(global)))))
  455. (if (eq? (car b) 'global)
  456. (values 'global var mod)
  457. (values (car b) (cdr b) mod)))))
  458. (resolve-lexical
  459. (lambda (label mod)
  460. (let ((b (resolve-syntax-parameters
  461. (or (assq-ref r label) '(displaced-lexical)))))
  462. (values (car b) (cdr b) mod)))))
  463. (let ((n (id-var-name id w mod)))
  464. (cond ((syntax-object? n)
  465. (if (not (eq? n id))
  466. (resolve-identifier n w r mod resolve-syntax-parameters?)
  467. (resolve-identifier
  468. (syntax-object-expression n)
  469. (syntax-object-wrap n)
  470. r
  471. (syntax-object-module n)
  472. resolve-syntax-parameters?)))
  473. ((symbol? n)
  474. (resolve-global
  475. n
  476. (if (syntax-object? id) (syntax-object-module id) mod)))
  477. ((string? n)
  478. (resolve-lexical
  479. n
  480. (if (syntax-object? id) (syntax-object-module id) mod)))
  481. (else (error "unexpected id-var-name" id w n)))))))
  482. (transformer-environment
  483. (make-fluid
  484. (lambda (k)
  485. (error "called outside the dynamic extent of a syntax transformer"))))
  486. (with-transformer-environment
  487. (lambda (k) ((fluid-ref transformer-environment) k)))
  488. (free-id=?
  489. (lambda (i j)
  490. (let* ((mi (and (syntax-object? i) (syntax-object-module i)))
  491. (mj (and (syntax-object? j) (syntax-object-module j)))
  492. (ni (id-var-name i '(()) mi))
  493. (nj (id-var-name j '(()) mj)))
  494. (letrec*
  495. ((id-module-binding
  496. (lambda (id mod)
  497. (module-variable
  498. (if mod (resolve-module (cdr mod)) (current-module))
  499. (let ((x id)) (if (syntax-object? x) (syntax-object-expression x) x))))))
  500. (cond ((syntax-object? ni) (free-id=? ni j))
  501. ((syntax-object? nj) (free-id=? i nj))
  502. ((symbol? ni)
  503. (and (eq? nj
  504. (let ((x j)) (if (syntax-object? x) (syntax-object-expression x) x)))
  505. (let ((bi (id-module-binding i mi)))
  506. (if bi
  507. (eq? bi (id-module-binding j mj))
  508. (and (not (id-module-binding j mj)) (eq? ni nj))))
  509. (eq? (id-module-binding i mi) (id-module-binding j mj))))
  510. (else (equal? ni nj)))))))
  511. (bound-id=?
  512. (lambda (i j)
  513. (if (and (syntax-object? i) (syntax-object? j))
  514. (and (eq? (syntax-object-expression i) (syntax-object-expression j))
  515. (same-marks?
  516. (car (syntax-object-wrap i))
  517. (car (syntax-object-wrap j))))
  518. (eq? i j))))
  519. (valid-bound-ids?
  520. (lambda (ids)
  521. (and (let all-ids? ((ids ids))
  522. (or (null? ids) (and (id? (car ids)) (all-ids? (cdr ids)))))
  523. (distinct-bound-ids? ids))))
  524. (distinct-bound-ids?
  525. (lambda (ids)
  526. (let distinct? ((ids ids))
  527. (or (null? ids)
  528. (and (not (bound-id-member? (car ids) (cdr ids)))
  529. (distinct? (cdr ids)))))))
  530. (bound-id-member?
  531. (lambda (x list)
  532. (and (not (null? list))
  533. (or (bound-id=? x (car list)) (bound-id-member? x (cdr list))))))
  534. (wrap (lambda (x w defmod)
  535. (cond ((and (null? (car w)) (null? (cdr w))) x)
  536. ((syntax-object? x)
  537. (make-syntax-object
  538. (syntax-object-expression x)
  539. (join-wraps w (syntax-object-wrap x))
  540. (syntax-object-module x)))
  541. ((null? x) x)
  542. (else (make-syntax-object x w defmod)))))
  543. (source-wrap
  544. (lambda (x w s defmod) (wrap (decorate-source x s) w defmod)))
  545. (expand-sequence
  546. (lambda (body r w s mod)
  547. (build-sequence
  548. s
  549. (let dobody ((body body) (r r) (w w) (mod mod))
  550. (if (null? body)
  551. '()
  552. (let ((first (expand (car body) r w mod)))
  553. (cons first (dobody (cdr body) r w mod))))))))
  554. (expand-top-sequence
  555. (lambda (body r w s m esew mod)
  556. (let* ((r (cons '("placeholder" placeholder) r))
  557. (ribcage (make-ribcage '() '() '()))
  558. (w (cons (car w) (cons ribcage (cdr w)))))
  559. (letrec*
  560. ((record-definition!
  561. (lambda (id var)
  562. (let ((mod (cons 'hygiene (module-name (current-module)))))
  563. (extend-ribcage!
  564. ribcage
  565. id
  566. (cons (syntax-object-module id) (wrap var '((top)) mod))))))
  567. (macro-introduced-identifier?
  568. (lambda (id) (not (equal? (car (syntax-object-wrap id)) '(top)))))
  569. (fresh-derived-name
  570. (lambda (id orig-form)
  571. (symbol-append
  572. (syntax-object-expression id)
  573. '-
  574. (string->symbol
  575. (number->string
  576. (hash (syntax->datum orig-form) most-positive-fixnum)
  577. 16)))))
  578. (parse (lambda (body r w s m esew mod)
  579. (let lp ((body body) (exps '()))
  580. (if (null? body)
  581. exps
  582. (lp (cdr body) (append (parse1 (car body) r w s m esew mod) exps))))))
  583. (parse1
  584. (lambda (x r w s m esew mod)
  585. (call-with-values
  586. (lambda () (syntax-type x r w (source-annotation x) ribcage mod #f))
  587. (lambda (type value form e w s mod)
  588. (let ((key type))
  589. (cond ((memv key '(define-form))
  590. (let* ((id (wrap value w mod))
  591. (label (gen-label))
  592. (var (if (macro-introduced-identifier? id)
  593. (fresh-derived-name id x)
  594. (syntax-object-expression id))))
  595. (record-definition! id var)
  596. (list (if (eq? m 'c&e)
  597. (let ((x (build-global-definition s var (expand e r w mod))))
  598. (top-level-eval-hook x mod)
  599. (lambda () x))
  600. (call-with-values
  601. (lambda () (resolve-identifier id '(()) r mod #t))
  602. (lambda (type* value* mod*)
  603. (if (eq? type* 'macro)
  604. (top-level-eval-hook
  605. (build-global-definition s var (build-void s))
  606. mod))
  607. (lambda () (build-global-definition s var (expand e r w mod)))))))))
  608. ((memv key '(define-syntax-form define-syntax-parameter-form))
  609. (let* ((id (wrap value w mod))
  610. (label (gen-label))
  611. (var (if (macro-introduced-identifier? id)
  612. (fresh-derived-name id x)
  613. (syntax-object-expression id))))
  614. (record-definition! id var)
  615. (let ((key m))
  616. (cond ((memv key '(c))
  617. (cond ((memq 'compile esew)
  618. (let ((e (expand-install-global var type (expand e r w mod))))
  619. (top-level-eval-hook e mod)
  620. (if (memq 'load esew) (list (lambda () e)) '())))
  621. ((memq 'load esew)
  622. (list (lambda () (expand-install-global var type (expand e r w mod)))))
  623. (else '())))
  624. ((memv key '(c&e))
  625. (let ((e (expand-install-global var type (expand e r w mod))))
  626. (top-level-eval-hook e mod)
  627. (list (lambda () e))))
  628. (else
  629. (if (memq 'eval esew)
  630. (top-level-eval-hook
  631. (expand-install-global var type (expand e r w mod))
  632. mod))
  633. '())))))
  634. ((memv key '(begin-form))
  635. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ . each-any))))
  636. (if tmp
  637. (apply (lambda (e1) (parse e1 r w s m esew mod)) tmp)
  638. (syntax-violation
  639. #f
  640. "source expression failed to match any pattern"
  641. tmp-1))))
  642. ((memv key '(local-syntax-form))
  643. (expand-local-syntax
  644. value
  645. e
  646. r
  647. w
  648. s
  649. mod
  650. (lambda (forms r w s mod) (parse forms r w s m esew mod))))
  651. ((memv key '(eval-when-form))
  652. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any . each-any))))
  653. (if tmp
  654. (apply (lambda (x e1 e2)
  655. (let ((when-list (parse-when-list e x)) (body (cons e1 e2)))
  656. (letrec*
  657. ((recurse (lambda (m esew) (parse body r w s m esew mod))))
  658. (cond ((eq? m 'e)
  659. (if (memq 'eval when-list)
  660. (recurse (if (memq 'expand when-list) 'c&e 'e) '(eval))
  661. (begin
  662. (if (memq 'expand when-list)
  663. (top-level-eval-hook
  664. (expand-top-sequence body r w s 'e '(eval) mod)
  665. mod))
  666. '())))
  667. ((memq 'load when-list)
  668. (cond ((or (memq 'compile when-list)
  669. (memq 'expand when-list)
  670. (and (eq? m 'c&e) (memq 'eval when-list)))
  671. (recurse 'c&e '(compile load)))
  672. ((memq m '(c c&e)) (recurse 'c '(load)))
  673. (else '())))
  674. ((or (memq 'compile when-list)
  675. (memq 'expand when-list)
  676. (and (eq? m 'c&e) (memq 'eval when-list)))
  677. (top-level-eval-hook
  678. (expand-top-sequence body r w s 'e '(eval) mod)
  679. mod)
  680. '())
  681. (else '())))))
  682. tmp)
  683. (syntax-violation
  684. #f
  685. "source expression failed to match any pattern"
  686. tmp-1))))
  687. (else
  688. (list (if (eq? m 'c&e)
  689. (let ((x (expand-expr type value form e r w s mod)))
  690. (top-level-eval-hook x mod)
  691. (lambda () x))
  692. (lambda () (expand-expr type value form e r w s mod))))))))))))
  693. (let ((exps (map (lambda (x) (x)) (reverse (parse body r w s m esew mod)))))
  694. (if (null? exps) (build-void s) (build-sequence s exps)))))))
  695. (expand-install-global
  696. (lambda (name type e)
  697. (build-global-definition
  698. #f
  699. name
  700. (build-primcall
  701. #f
  702. 'make-syntax-transformer
  703. (if (eq? type 'define-syntax-parameter-form)
  704. (list (build-data #f name)
  705. (build-data #f 'syntax-parameter)
  706. (build-primcall #f 'list (list e)))
  707. (list (build-data #f name) (build-data #f 'macro) e))))))
  708. (parse-when-list
  709. (lambda (e when-list)
  710. (let ((result (strip when-list '(()))))
  711. (let lp ((l result))
  712. (cond ((null? l) result)
  713. ((memq (car l) '(compile load eval expand)) (lp (cdr l)))
  714. (else (syntax-violation 'eval-when "invalid situation" e (car l))))))))
  715. (syntax-type
  716. (lambda (e r w s rib mod for-car?)
  717. (cond ((symbol? e)
  718. (call-with-values
  719. (lambda () (resolve-identifier e w r mod #t))
  720. (lambda (type value mod*)
  721. (let ((key type))
  722. (cond ((memv key '(macro))
  723. (if for-car?
  724. (values type value e e w s mod)
  725. (syntax-type
  726. (expand-macro value e r w s rib mod)
  727. r
  728. '(())
  729. s
  730. rib
  731. mod
  732. #f)))
  733. ((memv key '(global)) (values type value e value w s mod*))
  734. (else (values type value e e w s mod)))))))
  735. ((pair? e)
  736. (let ((first (car e)))
  737. (call-with-values
  738. (lambda () (syntax-type first r w s rib mod #t))
  739. (lambda (ftype fval fform fe fw fs fmod)
  740. (let ((key ftype))
  741. (cond ((memv key '(lexical)) (values 'lexical-call fval e e w s mod))
  742. ((memv key '(global))
  743. (if (equal? fmod '(primitive))
  744. (values 'primitive-call fval e e w s mod)
  745. (values 'global-call (make-syntax-object fval w fmod) e e w s mod)))
  746. ((memv key '(macro))
  747. (syntax-type
  748. (expand-macro fval e r w s rib mod)
  749. r
  750. '(())
  751. s
  752. rib
  753. mod
  754. for-car?))
  755. ((memv key '(module-ref))
  756. (call-with-values
  757. (lambda () (fval e r w mod))
  758. (lambda (e r w s mod) (syntax-type e r w s rib mod for-car?))))
  759. ((memv key '(core)) (values 'core-form fval e e w s mod))
  760. ((memv key '(local-syntax))
  761. (values 'local-syntax-form fval e e w s mod))
  762. ((memv key '(begin)) (values 'begin-form #f e e w s mod))
  763. ((memv key '(eval-when)) (values 'eval-when-form #f e e w s mod))
  764. ((memv key '(define))
  765. (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any any))))
  766. (if (and tmp-1 (apply (lambda (name val) (id? name)) tmp-1))
  767. (apply (lambda (name val) (values 'define-form name e val w s mod))
  768. tmp-1)
  769. (let ((tmp-1 ($sc-dispatch tmp '(_ (any . any) any . each-any))))
  770. (if (and tmp-1
  771. (apply (lambda (name args e1 e2)
  772. (and (id? name) (valid-bound-ids? (lambda-var-list args))))
  773. tmp-1))
  774. (apply (lambda (name args e1 e2)
  775. (values
  776. 'define-form
  777. (wrap name w mod)
  778. (wrap e w mod)
  779. (decorate-source
  780. (cons '#(syntax-object lambda ((top)) (hygiene guile))
  781. (wrap (cons args (cons e1 e2)) w mod))
  782. s)
  783. '(())
  784. s
  785. mod))
  786. tmp-1)
  787. (let ((tmp-1 ($sc-dispatch tmp '(_ any))))
  788. (if (and tmp-1 (apply (lambda (name) (id? name)) tmp-1))
  789. (apply (lambda (name)
  790. (values
  791. 'define-form
  792. (wrap name w mod)
  793. (wrap e w mod)
  794. '(#(syntax-object if ((top)) (hygiene guile)) #f #f)
  795. '(())
  796. s
  797. mod))
  798. tmp-1)
  799. (syntax-violation
  800. #f
  801. "source expression failed to match any pattern"
  802. tmp))))))))
  803. ((memv key '(define-syntax))
  804. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
  805. (if (and tmp (apply (lambda (name val) (id? name)) tmp))
  806. (apply (lambda (name val) (values 'define-syntax-form name e val w s mod))
  807. tmp)
  808. (syntax-violation
  809. #f
  810. "source expression failed to match any pattern"
  811. tmp-1))))
  812. ((memv key '(define-syntax-parameter))
  813. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
  814. (if (and tmp (apply (lambda (name val) (id? name)) tmp))
  815. (apply (lambda (name val)
  816. (values 'define-syntax-parameter-form name e val w s mod))
  817. tmp)
  818. (syntax-violation
  819. #f
  820. "source expression failed to match any pattern"
  821. tmp-1))))
  822. (else (values 'call #f e e w s mod))))))))
  823. ((syntax-object? e)
  824. (syntax-type
  825. (syntax-object-expression e)
  826. r
  827. (join-wraps w (syntax-object-wrap e))
  828. (or (source-annotation e) s)
  829. rib
  830. (or (syntax-object-module e) mod)
  831. for-car?))
  832. ((self-evaluating? e) (values 'constant #f e e w s mod))
  833. (else (values 'other #f e e w s mod)))))
  834. (expand
  835. (lambda (e r w mod)
  836. (call-with-values
  837. (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
  838. (lambda (type value form e w s mod)
  839. (expand-expr type value form e r w s mod)))))
  840. (expand-expr
  841. (lambda (type value form e r w s mod)
  842. (let ((key type))
  843. (cond ((memv key '(lexical)) (build-lexical-reference 'value s e value))
  844. ((memv key '(core core-form)) (value e r w s mod))
  845. ((memv key '(module-ref))
  846. (call-with-values
  847. (lambda () (value e r w mod))
  848. (lambda (e r w s mod) (expand e r w mod))))
  849. ((memv key '(lexical-call))
  850. (expand-call
  851. (let ((id (car e)))
  852. (build-lexical-reference
  853. 'fun
  854. (source-annotation id)
  855. (if (syntax-object? id) (syntax->datum id) id)
  856. value))
  857. e
  858. r
  859. w
  860. s
  861. mod))
  862. ((memv key '(global-call))
  863. (expand-call
  864. (build-global-reference
  865. (source-annotation (car e))
  866. (if (syntax-object? value) (syntax-object-expression value) value)
  867. (if (syntax-object? value) (syntax-object-module value) mod))
  868. e
  869. r
  870. w
  871. s
  872. mod))
  873. ((memv key '(primitive-call))
  874. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ . each-any))))
  875. (if tmp
  876. (apply (lambda (e)
  877. (build-primcall s value (map (lambda (e) (expand e r w mod)) e)))
  878. tmp)
  879. (syntax-violation
  880. #f
  881. "source expression failed to match any pattern"
  882. tmp-1))))
  883. ((memv key '(constant))
  884. (build-data s (strip (source-wrap e w s mod) '(()))))
  885. ((memv key '(global)) (build-global-reference s value mod))
  886. ((memv key '(call))
  887. (expand-call (expand (car e) r w mod) e r w s mod))
  888. ((memv key '(begin-form))
  889. (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any . each-any))))
  890. (if tmp-1
  891. (apply (lambda (e1 e2) (expand-sequence (cons e1 e2) r w s mod))
  892. tmp-1)
  893. (let ((tmp-1 ($sc-dispatch tmp '(_))))
  894. (if tmp-1
  895. (apply (lambda ()
  896. (syntax-violation
  897. #f
  898. "sequence of zero expressions"
  899. (source-wrap e w s mod)))
  900. tmp-1)
  901. (syntax-violation
  902. #f
  903. "source expression failed to match any pattern"
  904. tmp))))))
  905. ((memv key '(local-syntax-form))
  906. (expand-local-syntax value e r w s mod expand-sequence))
  907. ((memv key '(eval-when-form))
  908. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any . each-any))))
  909. (if tmp
  910. (apply (lambda (x e1 e2)
  911. (let ((when-list (parse-when-list e x)))
  912. (if (memq 'eval when-list)
  913. (expand-sequence (cons e1 e2) r w s mod)
  914. (expand-void))))
  915. tmp)
  916. (syntax-violation
  917. #f
  918. "source expression failed to match any pattern"
  919. tmp-1))))
  920. ((memv key
  921. '(define-form define-syntax-form define-syntax-parameter-form))
  922. (syntax-violation
  923. #f
  924. "definition in expression context, where definitions are not allowed,"
  925. (source-wrap form w s mod)))
  926. ((memv key '(syntax))
  927. (syntax-violation
  928. #f
  929. "reference to pattern variable outside syntax form"
  930. (source-wrap e w s mod)))
  931. ((memv key '(displaced-lexical))
  932. (syntax-violation
  933. #f
  934. "reference to identifier outside its scope"
  935. (source-wrap e w s mod)))
  936. (else
  937. (syntax-violation #f "unexpected syntax" (source-wrap e w s mod)))))))
  938. (expand-call
  939. (lambda (x e r w s mod)
  940. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(any . each-any))))
  941. (if tmp
  942. (apply (lambda (e0 e1)
  943. (build-call s x (map (lambda (e) (expand e r w mod)) e1)))
  944. tmp)
  945. (syntax-violation
  946. #f
  947. "source expression failed to match any pattern"
  948. tmp-1)))))
  949. (expand-macro
  950. (lambda (p e r w s rib mod)
  951. (letrec*
  952. ((rebuild-macro-output
  953. (lambda (x m)
  954. (cond ((pair? x)
  955. (decorate-source
  956. (cons (rebuild-macro-output (car x) m)
  957. (rebuild-macro-output (cdr x) m))
  958. s))
  959. ((syntax-object? x)
  960. (let ((w (syntax-object-wrap x)))
  961. (let ((ms (car w)) (ss (cdr w)))
  962. (if (and (pair? ms) (eq? (car ms) #f))
  963. (make-syntax-object
  964. (syntax-object-expression x)
  965. (cons (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
  966. (syntax-object-module x))
  967. (make-syntax-object
  968. (decorate-source (syntax-object-expression x) s)
  969. (cons (cons m ms)
  970. (if rib (cons rib (cons 'shift ss)) (cons 'shift ss)))
  971. (syntax-object-module x))))))
  972. ((vector? x)
  973. (let* ((n (vector-length x)) (v (decorate-source (make-vector n) s)))
  974. (let loop ((i 0))
  975. (if (= i n)
  976. (begin (if #f #f) v)
  977. (begin
  978. (vector-set! v i (rebuild-macro-output (vector-ref x i) m))
  979. (loop (+ i 1)))))))
  980. ((symbol? x)
  981. (syntax-violation
  982. #f
  983. "encountered raw symbol in macro output"
  984. (source-wrap e w (cdr w) mod)
  985. x))
  986. (else (decorate-source x s))))))
  987. (let* ((t-1 transformer-environment) (t (lambda (k) (k e r w s rib mod))))
  988. (with-fluid*
  989. t-1
  990. t
  991. (lambda ()
  992. (rebuild-macro-output
  993. (p (source-wrap e (anti-mark w) s mod))
  994. (gensym (string-append "m-" (session-id) "-")))))))))
  995. (expand-body
  996. (lambda (body outer-form r w mod)
  997. (let* ((r (cons '("placeholder" placeholder) r))
  998. (ribcage (make-ribcage '() '() '()))
  999. (w (cons (car w) (cons ribcage (cdr w)))))
  1000. (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
  1001. (ids '())
  1002. (labels '())
  1003. (var-ids '())
  1004. (vars '())
  1005. (vals '())
  1006. (bindings '()))
  1007. (if (null? body)
  1008. (syntax-violation #f "no expressions in body" outer-form)
  1009. (let ((e (cdar body)) (er (caar body)))
  1010. (call-with-values
  1011. (lambda ()
  1012. (syntax-type e er '(()) (source-annotation e) ribcage mod #f))
  1013. (lambda (type value form e w s mod)
  1014. (let ((key type))
  1015. (cond ((memv key '(define-form))
  1016. (let ((id (wrap value w mod)) (label (gen-label)))
  1017. (let ((var (gen-var id)))
  1018. (extend-ribcage! ribcage id label)
  1019. (parse (cdr body)
  1020. (cons id ids)
  1021. (cons label labels)
  1022. (cons id var-ids)
  1023. (cons var vars)
  1024. (cons (cons er (wrap e w mod)) vals)
  1025. (cons (cons 'lexical var) bindings)))))
  1026. ((memv key '(define-syntax-form))
  1027. (let ((id (wrap value w mod))
  1028. (label (gen-label))
  1029. (trans-r (macros-only-env er)))
  1030. (extend-ribcage! ribcage id label)
  1031. (set-cdr!
  1032. r
  1033. (extend-env
  1034. (list label)
  1035. (list (cons 'macro (eval-local-transformer (expand e trans-r w mod) mod)))
  1036. (cdr r)))
  1037. (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
  1038. ((memv key '(define-syntax-parameter-form))
  1039. (let ((id (wrap value w mod))
  1040. (label (gen-label))
  1041. (trans-r (macros-only-env er)))
  1042. (extend-ribcage! ribcage id label)
  1043. (set-cdr!
  1044. r
  1045. (extend-env
  1046. (list label)
  1047. (list (cons 'syntax-parameter
  1048. (list (eval-local-transformer (expand e trans-r w mod) mod))))
  1049. (cdr r)))
  1050. (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
  1051. ((memv key '(begin-form))
  1052. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ . each-any))))
  1053. (if tmp
  1054. (apply (lambda (e1)
  1055. (parse (let f ((forms e1))
  1056. (if (null? forms)
  1057. (cdr body)
  1058. (cons (cons er (wrap (car forms) w mod)) (f (cdr forms)))))
  1059. ids
  1060. labels
  1061. var-ids
  1062. vars
  1063. vals
  1064. bindings))
  1065. tmp)
  1066. (syntax-violation
  1067. #f
  1068. "source expression failed to match any pattern"
  1069. tmp-1))))
  1070. ((memv key '(local-syntax-form))
  1071. (expand-local-syntax
  1072. value
  1073. e
  1074. er
  1075. w
  1076. s
  1077. mod
  1078. (lambda (forms er w s mod)
  1079. (parse (let f ((forms forms))
  1080. (if (null? forms)
  1081. (cdr body)
  1082. (cons (cons er (wrap (car forms) w mod)) (f (cdr forms)))))
  1083. ids
  1084. labels
  1085. var-ids
  1086. vars
  1087. vals
  1088. bindings))))
  1089. ((null? ids)
  1090. (build-sequence
  1091. #f
  1092. (map (lambda (x) (expand (cdr x) (car x) '(()) mod))
  1093. (cons (cons er (source-wrap e w s mod)) (cdr body)))))
  1094. (else
  1095. (if (not (valid-bound-ids? ids))
  1096. (syntax-violation
  1097. #f
  1098. "invalid or duplicate identifier in definition"
  1099. outer-form))
  1100. (set-cdr! r (extend-env labels bindings (cdr r)))
  1101. (build-letrec
  1102. #f
  1103. #t
  1104. (reverse (map syntax->datum var-ids))
  1105. (reverse vars)
  1106. (map (lambda (x) (expand (cdr x) (car x) '(()) mod)) (reverse vals))
  1107. (build-sequence
  1108. #f
  1109. (map (lambda (x) (expand (cdr x) (car x) '(()) mod))
  1110. (cons (cons er (source-wrap e w s mod)) (cdr body))))))))))))))))
  1111. (expand-local-syntax
  1112. (lambda (rec? e r w s mod k)
  1113. (let* ((tmp e)
  1114. (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
  1115. (if tmp
  1116. (apply (lambda (id val e1 e2)
  1117. (let ((ids id))
  1118. (if (not (valid-bound-ids? ids))
  1119. (syntax-violation #f "duplicate bound keyword" e)
  1120. (let* ((labels (gen-labels ids)) (new-w (make-binding-wrap ids labels w)))
  1121. (k (cons e1 e2)
  1122. (extend-env
  1123. labels
  1124. (let ((w (if rec? new-w w)) (trans-r (macros-only-env r)))
  1125. (map (lambda (x)
  1126. (cons 'macro (eval-local-transformer (expand x trans-r w mod) mod)))
  1127. val))
  1128. r)
  1129. new-w
  1130. s
  1131. mod)))))
  1132. tmp)
  1133. (syntax-violation
  1134. #f
  1135. "bad local syntax definition"
  1136. (source-wrap e w s mod))))))
  1137. (eval-local-transformer
  1138. (lambda (expanded mod)
  1139. (let ((p (local-eval-hook expanded mod)))
  1140. (if (procedure? p)
  1141. p
  1142. (syntax-violation #f "nonprocedure transformer" p)))))
  1143. (expand-void (lambda () (build-void #f)))
  1144. (ellipsis?
  1145. (lambda (e r mod)
  1146. (and (nonsymbol-id? e)
  1147. (call-with-values
  1148. (lambda ()
  1149. (resolve-identifier
  1150. (make-syntax-object
  1151. '#{ $sc-ellipsis }#
  1152. (syntax-object-wrap e)
  1153. (syntax-object-module e))
  1154. '(())
  1155. r
  1156. mod
  1157. #f))
  1158. (lambda (type value mod)
  1159. (if (eq? type 'ellipsis)
  1160. (bound-id=? e value)
  1161. (free-id=? e '#(syntax-object ... ((top)) (hygiene guile)))))))))
  1162. (lambda-formals
  1163. (lambda (orig-args)
  1164. (letrec*
  1165. ((req (lambda (args rreq)
  1166. (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
  1167. (if tmp-1
  1168. (apply (lambda () (check (reverse rreq) #f)) tmp-1)
  1169. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1170. (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
  1171. (apply (lambda (a b) (req b (cons a rreq))) tmp-1)
  1172. (let ((tmp-1 (list tmp)))
  1173. (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
  1174. (apply (lambda (r) (check (reverse rreq) r)) tmp-1)
  1175. (let ((else tmp))
  1176. (syntax-violation 'lambda "invalid argument list" orig-args args))))))))))
  1177. (check (lambda (req rest)
  1178. (if (distinct-bound-ids? (if rest (cons rest req) req))
  1179. (values req #f rest #f)
  1180. (syntax-violation
  1181. 'lambda
  1182. "duplicate identifier in argument list"
  1183. orig-args)))))
  1184. (req orig-args '()))))
  1185. (expand-simple-lambda
  1186. (lambda (e r w s mod req rest meta body)
  1187. (let* ((ids (if rest (append req (list rest)) req))
  1188. (vars (map gen-var ids))
  1189. (labels (gen-labels ids)))
  1190. (build-simple-lambda
  1191. s
  1192. (map syntax->datum req)
  1193. (and rest (syntax->datum rest))
  1194. vars
  1195. meta
  1196. (expand-body
  1197. body
  1198. (source-wrap e w s mod)
  1199. (extend-var-env labels vars r)
  1200. (make-binding-wrap ids labels w)
  1201. mod)))))
  1202. (lambda*-formals
  1203. (lambda (orig-args)
  1204. (letrec*
  1205. ((req (lambda (args rreq)
  1206. (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
  1207. (if tmp-1
  1208. (apply (lambda () (check (reverse rreq) '() #f '())) tmp-1)
  1209. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1210. (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
  1211. (apply (lambda (a b) (req b (cons a rreq))) tmp-1)
  1212. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1213. (if (and tmp-1
  1214. (apply (lambda (a b) (eq? (syntax->datum a) #:optional)) tmp-1))
  1215. (apply (lambda (a b) (opt b (reverse rreq) '())) tmp-1)
  1216. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1217. (if (and tmp-1
  1218. (apply (lambda (a b) (eq? (syntax->datum a) #:key)) tmp-1))
  1219. (apply (lambda (a b) (key b (reverse rreq) '() '())) tmp-1)
  1220. (let ((tmp-1 ($sc-dispatch tmp '(any any))))
  1221. (if (and tmp-1
  1222. (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
  1223. (apply (lambda (a b) (rest b (reverse rreq) '() '())) tmp-1)
  1224. (let ((tmp-1 (list tmp)))
  1225. (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
  1226. (apply (lambda (r) (rest r (reverse rreq) '() '())) tmp-1)
  1227. (let ((else tmp))
  1228. (syntax-violation
  1229. 'lambda*
  1230. "invalid argument list"
  1231. orig-args
  1232. args))))))))))))))))
  1233. (opt (lambda (args req ropt)
  1234. (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
  1235. (if tmp-1
  1236. (apply (lambda () (check req (reverse ropt) #f '())) tmp-1)
  1237. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1238. (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
  1239. (apply (lambda (a b) (opt b req (cons (cons a '(#f)) ropt))) tmp-1)
  1240. (let ((tmp-1 ($sc-dispatch tmp '((any any) . any))))
  1241. (if (and tmp-1 (apply (lambda (a init b) (id? a)) tmp-1))
  1242. (apply (lambda (a init b) (opt b req (cons (list a init) ropt)))
  1243. tmp-1)
  1244. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1245. (if (and tmp-1
  1246. (apply (lambda (a b) (eq? (syntax->datum a) #:key)) tmp-1))
  1247. (apply (lambda (a b) (key b req (reverse ropt) '())) tmp-1)
  1248. (let ((tmp-1 ($sc-dispatch tmp '(any any))))
  1249. (if (and tmp-1
  1250. (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
  1251. (apply (lambda (a b) (rest b req (reverse ropt) '())) tmp-1)
  1252. (let ((tmp-1 (list tmp)))
  1253. (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
  1254. (apply (lambda (r) (rest r req (reverse ropt) '())) tmp-1)
  1255. (let ((else tmp))
  1256. (syntax-violation
  1257. 'lambda*
  1258. "invalid optional argument list"
  1259. orig-args
  1260. args))))))))))))))))
  1261. (key (lambda (args req opt rkey)
  1262. (let* ((tmp args) (tmp-1 ($sc-dispatch tmp '())))
  1263. (if tmp-1
  1264. (apply (lambda () (check req opt #f (cons #f (reverse rkey)))) tmp-1)
  1265. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1266. (if (and tmp-1 (apply (lambda (a b) (id? a)) tmp-1))
  1267. (apply (lambda (a b)
  1268. (let* ((tmp (symbol->keyword (syntax->datum a))) (k tmp))
  1269. (key b req opt (cons (cons k (cons a '(#f))) rkey))))
  1270. tmp-1)
  1271. (let ((tmp-1 ($sc-dispatch tmp '((any any) . any))))
  1272. (if (and tmp-1 (apply (lambda (a init b) (id? a)) tmp-1))
  1273. (apply (lambda (a init b)
  1274. (let* ((tmp (symbol->keyword (syntax->datum a))) (k tmp))
  1275. (key b req opt (cons (list k a init) rkey))))
  1276. tmp-1)
  1277. (let ((tmp-1 ($sc-dispatch tmp '((any any any) . any))))
  1278. (if (and tmp-1
  1279. (apply (lambda (a init k b) (and (id? a) (keyword? (syntax->datum k))))
  1280. tmp-1))
  1281. (apply (lambda (a init k b) (key b req opt (cons (list k a init) rkey)))
  1282. tmp-1)
  1283. (let ((tmp-1 ($sc-dispatch tmp '(any))))
  1284. (if (and tmp-1
  1285. (apply (lambda (aok) (eq? (syntax->datum aok) #:allow-other-keys))
  1286. tmp-1))
  1287. (apply (lambda (aok) (check req opt #f (cons #t (reverse rkey))))
  1288. tmp-1)
  1289. (let ((tmp-1 ($sc-dispatch tmp '(any any any))))
  1290. (if (and tmp-1
  1291. (apply (lambda (aok a b)
  1292. (and (eq? (syntax->datum aok) #:allow-other-keys)
  1293. (eq? (syntax->datum a) #:rest)))
  1294. tmp-1))
  1295. (apply (lambda (aok a b) (rest b req opt (cons #t (reverse rkey))))
  1296. tmp-1)
  1297. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1298. (if (and tmp-1
  1299. (apply (lambda (aok r)
  1300. (and (eq? (syntax->datum aok) #:allow-other-keys) (id? r)))
  1301. tmp-1))
  1302. (apply (lambda (aok r) (rest r req opt (cons #t (reverse rkey))))
  1303. tmp-1)
  1304. (let ((tmp-1 ($sc-dispatch tmp '(any any))))
  1305. (if (and tmp-1
  1306. (apply (lambda (a b) (eq? (syntax->datum a) #:rest)) tmp-1))
  1307. (apply (lambda (a b) (rest b req opt (cons #f (reverse rkey))))
  1308. tmp-1)
  1309. (let ((tmp-1 (list tmp)))
  1310. (if (and tmp-1 (apply (lambda (r) (id? r)) tmp-1))
  1311. (apply (lambda (r) (rest r req opt (cons #f (reverse rkey))))
  1312. tmp-1)
  1313. (let ((else tmp))
  1314. (syntax-violation
  1315. 'lambda*
  1316. "invalid keyword argument list"
  1317. orig-args
  1318. args))))))))))))))))))))))
  1319. (rest (lambda (args req opt kw)
  1320. (let* ((tmp-1 args) (tmp (list tmp-1)))
  1321. (if (and tmp (apply (lambda (r) (id? r)) tmp))
  1322. (apply (lambda (r) (check req opt r kw)) tmp)
  1323. (let ((else tmp-1))
  1324. (syntax-violation 'lambda* "invalid rest argument" orig-args args))))))
  1325. (check (lambda (req opt rest kw)
  1326. (if (distinct-bound-ids?
  1327. (append
  1328. req
  1329. (map car opt)
  1330. (if rest (list rest) '())
  1331. (if (pair? kw) (map cadr (cdr kw)) '())))
  1332. (values req opt rest kw)
  1333. (syntax-violation
  1334. 'lambda*
  1335. "duplicate identifier in argument list"
  1336. orig-args)))))
  1337. (req orig-args '()))))
  1338. (expand-lambda-case
  1339. (lambda (e r w s mod get-formals clauses)
  1340. (letrec*
  1341. ((parse-req
  1342. (lambda (req opt rest kw body)
  1343. (let ((vars (map gen-var req)) (labels (gen-labels req)))
  1344. (let ((r* (extend-var-env labels vars r))
  1345. (w* (make-binding-wrap req labels w)))
  1346. (parse-opt
  1347. (map syntax->datum req)
  1348. opt
  1349. rest
  1350. kw
  1351. body
  1352. (reverse vars)
  1353. r*
  1354. w*
  1355. '()
  1356. '())))))
  1357. (parse-opt
  1358. (lambda (req opt rest kw body vars r* w* out inits)
  1359. (cond ((pair? opt)
  1360. (let* ((tmp-1 (car opt)) (tmp ($sc-dispatch tmp-1 '(any any))))
  1361. (if tmp
  1362. (apply (lambda (id i)
  1363. (let* ((v (gen-var id))
  1364. (l (gen-labels (list v)))
  1365. (r** (extend-var-env l (list v) r*))
  1366. (w** (make-binding-wrap (list id) l w*)))
  1367. (parse-opt
  1368. req
  1369. (cdr opt)
  1370. rest
  1371. kw
  1372. body
  1373. (cons v vars)
  1374. r**
  1375. w**
  1376. (cons (syntax->datum id) out)
  1377. (cons (expand i r* w* mod) inits))))
  1378. tmp)
  1379. (syntax-violation
  1380. #f
  1381. "source expression failed to match any pattern"
  1382. tmp-1))))
  1383. (rest
  1384. (let* ((v (gen-var rest))
  1385. (l (gen-labels (list v)))
  1386. (r* (extend-var-env l (list v) r*))
  1387. (w* (make-binding-wrap (list rest) l w*)))
  1388. (parse-kw
  1389. req
  1390. (and (pair? out) (reverse out))
  1391. (syntax->datum rest)
  1392. (if (pair? kw) (cdr kw) kw)
  1393. body
  1394. (cons v vars)
  1395. r*
  1396. w*
  1397. (and (pair? kw) (car kw))
  1398. '()
  1399. inits)))
  1400. (else
  1401. (parse-kw
  1402. req
  1403. (and (pair? out) (reverse out))
  1404. #f
  1405. (if (pair? kw) (cdr kw) kw)
  1406. body
  1407. vars
  1408. r*
  1409. w*
  1410. (and (pair? kw) (car kw))
  1411. '()
  1412. inits)))))
  1413. (parse-kw
  1414. (lambda (req opt rest kw body vars r* w* aok out inits)
  1415. (if (pair? kw)
  1416. (let* ((tmp-1 (car kw)) (tmp ($sc-dispatch tmp-1 '(any any any))))
  1417. (if tmp
  1418. (apply (lambda (k id i)
  1419. (let* ((v (gen-var id))
  1420. (l (gen-labels (list v)))
  1421. (r** (extend-var-env l (list v) r*))
  1422. (w** (make-binding-wrap (list id) l w*)))
  1423. (parse-kw
  1424. req
  1425. opt
  1426. rest
  1427. (cdr kw)
  1428. body
  1429. (cons v vars)
  1430. r**
  1431. w**
  1432. aok
  1433. (cons (list (syntax->datum k) (syntax->datum id) v) out)
  1434. (cons (expand i r* w* mod) inits))))
  1435. tmp)
  1436. (syntax-violation
  1437. #f
  1438. "source expression failed to match any pattern"
  1439. tmp-1)))
  1440. (parse-body
  1441. req
  1442. opt
  1443. rest
  1444. (and (or aok (pair? out)) (cons aok (reverse out)))
  1445. body
  1446. (reverse vars)
  1447. r*
  1448. w*
  1449. (reverse inits)
  1450. '()))))
  1451. (parse-body
  1452. (lambda (req opt rest kw body vars r* w* inits meta)
  1453. (let* ((tmp body) (tmp-1 ($sc-dispatch tmp '(any any . each-any))))
  1454. (if (and tmp-1
  1455. (apply (lambda (docstring e1 e2) (string? (syntax->datum docstring)))
  1456. tmp-1))
  1457. (apply (lambda (docstring e1 e2)
  1458. (parse-body
  1459. req
  1460. opt
  1461. rest
  1462. kw
  1463. (cons e1 e2)
  1464. vars
  1465. r*
  1466. w*
  1467. inits
  1468. (append meta (list (cons 'documentation (syntax->datum docstring))))))
  1469. tmp-1)
  1470. (let ((tmp-1 ($sc-dispatch tmp '(#(vector #(each (any . any))) any . each-any))))
  1471. (if tmp-1
  1472. (apply (lambda (k v e1 e2)
  1473. (parse-body
  1474. req
  1475. opt
  1476. rest
  1477. kw
  1478. (cons e1 e2)
  1479. vars
  1480. r*
  1481. w*
  1482. inits
  1483. (append meta (syntax->datum (map cons k v)))))
  1484. tmp-1)
  1485. (let ((tmp-1 ($sc-dispatch tmp '(any . each-any))))
  1486. (if tmp-1
  1487. (apply (lambda (e1 e2)
  1488. (values
  1489. meta
  1490. req
  1491. opt
  1492. rest
  1493. kw
  1494. inits
  1495. vars
  1496. (expand-body (cons e1 e2) (source-wrap e w s mod) r* w* mod)))
  1497. tmp-1)
  1498. (syntax-violation
  1499. #f
  1500. "source expression failed to match any pattern"
  1501. tmp))))))))))
  1502. (let* ((tmp clauses) (tmp-1 ($sc-dispatch tmp '())))
  1503. (if tmp-1
  1504. (apply (lambda () (values '() #f)) tmp-1)
  1505. (let ((tmp-1 ($sc-dispatch
  1506. tmp
  1507. '((any any . each-any) . #(each (any any . each-any))))))
  1508. (if tmp-1
  1509. (apply (lambda (args e1 e2 args* e1* e2*)
  1510. (call-with-values
  1511. (lambda () (get-formals args))
  1512. (lambda (req opt rest kw)
  1513. (call-with-values
  1514. (lambda () (parse-req req opt rest kw (cons e1 e2)))
  1515. (lambda (meta req opt rest kw inits vars body)
  1516. (call-with-values
  1517. (lambda ()
  1518. (expand-lambda-case
  1519. e
  1520. r
  1521. w
  1522. s
  1523. mod
  1524. get-formals
  1525. (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
  1526. e2*
  1527. e1*
  1528. args*)))
  1529. (lambda (meta* else*)
  1530. (values
  1531. (append meta meta*)
  1532. (build-lambda-case s req opt rest kw inits vars body else*)))))))))
  1533. tmp-1)
  1534. (syntax-violation
  1535. #f
  1536. "source expression failed to match any pattern"
  1537. tmp))))))))
  1538. (strip (lambda (x w)
  1539. (if (memq 'top (car w))
  1540. x
  1541. (let f ((x x))
  1542. (cond ((syntax-object? x)
  1543. (strip (syntax-object-expression x) (syntax-object-wrap x)))
  1544. ((pair? x)
  1545. (let ((a (f (car x))) (d (f (cdr x))))
  1546. (if (and (eq? a (car x)) (eq? d (cdr x))) x (cons a d))))
  1547. ((vector? x)
  1548. (let* ((old (vector->list x)) (new (map f old)))
  1549. (let lp ((l1 old) (l2 new))
  1550. (cond ((null? l1) x)
  1551. ((eq? (car l1) (car l2)) (lp (cdr l1) (cdr l2)))
  1552. (else (list->vector new))))))
  1553. (else x))))))
  1554. (gen-var
  1555. (lambda (id)
  1556. (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
  1557. (gensym (string-append (symbol->string id) "-")))))
  1558. (lambda-var-list
  1559. (lambda (vars)
  1560. (let lvl ((vars vars) (ls '()) (w '(())))
  1561. (cond ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
  1562. ((id? vars) (cons (wrap vars w #f) ls))
  1563. ((null? vars) ls)
  1564. ((syntax-object? vars)
  1565. (lvl (syntax-object-expression vars)
  1566. ls
  1567. (join-wraps w (syntax-object-wrap vars))))
  1568. (else (cons vars ls)))))))
  1569. (global-extend 'local-syntax 'letrec-syntax #t)
  1570. (global-extend 'local-syntax 'let-syntax #f)
  1571. (global-extend
  1572. 'core
  1573. 'syntax-parameterize
  1574. (lambda (e r w s mod)
  1575. (let* ((tmp e)
  1576. (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
  1577. (if (and tmp (apply (lambda (var val e1 e2) (valid-bound-ids? var)) tmp))
  1578. (apply (lambda (var val e1 e2)
  1579. (let ((names (map (lambda (x)
  1580. (call-with-values
  1581. (lambda () (resolve-identifier x w r mod #f))
  1582. (lambda (type value mod)
  1583. (let ((key type))
  1584. (cond ((memv key '(displaced-lexical))
  1585. (syntax-violation
  1586. 'syntax-parameterize
  1587. "identifier out of context"
  1588. e
  1589. (source-wrap x w s mod)))
  1590. ((memv key '(syntax-parameter)) value)
  1591. (else
  1592. (syntax-violation
  1593. 'syntax-parameterize
  1594. "invalid syntax parameter"
  1595. e
  1596. (source-wrap x w s mod))))))))
  1597. var))
  1598. (bindings
  1599. (let ((trans-r (macros-only-env r)))
  1600. (map (lambda (x)
  1601. (cons 'macro (eval-local-transformer (expand x trans-r w mod) mod)))
  1602. val))))
  1603. (expand-body
  1604. (cons e1 e2)
  1605. (source-wrap e w s mod)
  1606. (extend-env names bindings r)
  1607. w
  1608. mod)))
  1609. tmp)
  1610. (syntax-violation
  1611. 'syntax-parameterize
  1612. "bad syntax"
  1613. (source-wrap e w s mod))))))
  1614. (global-extend
  1615. 'core
  1616. 'quote
  1617. (lambda (e r w s mod)
  1618. (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any))))
  1619. (if tmp
  1620. (apply (lambda (e) (build-data s (strip e w))) tmp)
  1621. (syntax-violation 'quote "bad syntax" (source-wrap e w s mod))))))
  1622. (global-extend
  1623. 'core
  1624. 'syntax
  1625. (letrec*
  1626. ((gen-syntax
  1627. (lambda (src e r maps ellipsis? mod)
  1628. (if (id? e)
  1629. (call-with-values
  1630. (lambda () (resolve-identifier e '(()) r mod #f))
  1631. (lambda (type value mod)
  1632. (let ((key type))
  1633. (cond ((memv key '(syntax))
  1634. (call-with-values
  1635. (lambda () (gen-ref src (car value) (cdr value) maps))
  1636. (lambda (var maps) (values (list 'ref var) maps))))
  1637. ((ellipsis? e r mod)
  1638. (syntax-violation 'syntax "misplaced ellipsis" src))
  1639. (else (values (list 'quote e) maps))))))
  1640. (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(any any))))
  1641. (if (and tmp-1 (apply (lambda (dots e) (ellipsis? dots r mod)) tmp-1))
  1642. (apply (lambda (dots e) (gen-syntax src e r maps (lambda (e r mod) #f) mod))
  1643. tmp-1)
  1644. (let ((tmp-1 ($sc-dispatch tmp '(any any . any))))
  1645. (if (and tmp-1 (apply (lambda (x dots y) (ellipsis? dots r mod)) tmp-1))
  1646. (apply (lambda (x dots y)
  1647. (let f ((y y)
  1648. (k (lambda (maps)
  1649. (call-with-values
  1650. (lambda () (gen-syntax src x r (cons '() maps) ellipsis? mod))
  1651. (lambda (x maps)
  1652. (if (null? (car maps))
  1653. (syntax-violation 'syntax "extra ellipsis" src)
  1654. (values (gen-map x (car maps)) (cdr maps))))))))
  1655. (let* ((tmp y) (tmp ($sc-dispatch tmp '(any . any))))
  1656. (if (and tmp (apply (lambda (dots y) (ellipsis? dots r mod)) tmp))
  1657. (apply (lambda (dots y)
  1658. (f y
  1659. (lambda (maps)
  1660. (call-with-values
  1661. (lambda () (k (cons '() maps)))
  1662. (lambda (x maps)
  1663. (if (null? (car maps))
  1664. (syntax-violation 'syntax "extra ellipsis" src)
  1665. (values (gen-mappend x (car maps)) (cdr maps))))))))
  1666. tmp)
  1667. (call-with-values
  1668. (lambda () (gen-syntax src y r maps ellipsis? mod))
  1669. (lambda (y maps)
  1670. (call-with-values
  1671. (lambda () (k maps))
  1672. (lambda (x maps) (values (gen-append x y) maps)))))))))
  1673. tmp-1)
  1674. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  1675. (if tmp-1
  1676. (apply (lambda (x y)
  1677. (call-with-values
  1678. (lambda () (gen-syntax src x r maps ellipsis? mod))
  1679. (lambda (x maps)
  1680. (call-with-values
  1681. (lambda () (gen-syntax src y r maps ellipsis? mod))
  1682. (lambda (y maps) (values (gen-cons x y) maps))))))
  1683. tmp-1)
  1684. (let ((tmp ($sc-dispatch tmp '#(vector (any . each-any)))))
  1685. (if tmp
  1686. (apply (lambda (e1 e2)
  1687. (call-with-values
  1688. (lambda () (gen-syntax src (cons e1 e2) r maps ellipsis? mod))
  1689. (lambda (e maps) (values (gen-vector e) maps))))
  1690. tmp)
  1691. (values (list 'quote e) maps))))))))))))
  1692. (gen-ref
  1693. (lambda (src var level maps)
  1694. (cond ((= level 0) (values var maps))
  1695. ((null? maps) (syntax-violation 'syntax "missing ellipsis" src))
  1696. (else
  1697. (call-with-values
  1698. (lambda () (gen-ref src var (- level 1) (cdr maps)))
  1699. (lambda (outer-var outer-maps)
  1700. (let ((b (assq outer-var (car maps))))
  1701. (if b
  1702. (values (cdr b) maps)
  1703. (let ((inner-var (gen-var 'tmp)))
  1704. (values
  1705. inner-var
  1706. (cons (cons (cons outer-var inner-var) (car maps)) outer-maps)))))))))))
  1707. (gen-mappend
  1708. (lambda (e map-env)
  1709. (list 'apply '(primitive append) (gen-map e map-env))))
  1710. (gen-map
  1711. (lambda (e map-env)
  1712. (let ((formals (map cdr map-env))
  1713. (actuals (map (lambda (x) (list 'ref (car x))) map-env)))
  1714. (cond ((eq? (car e) 'ref) (car actuals))
  1715. ((and-map
  1716. (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
  1717. (cdr e))
  1718. (cons 'map
  1719. (cons (list 'primitive (car e))
  1720. (map (let ((r (map cons formals actuals)))
  1721. (lambda (x) (cdr (assq (cadr x) r))))
  1722. (cdr e)))))
  1723. (else (cons 'map (cons (list 'lambda formals e) actuals)))))))
  1724. (gen-cons
  1725. (lambda (x y)
  1726. (let ((key (car y)))
  1727. (cond ((memv key '(quote))
  1728. (cond ((eq? (car x) 'quote) (list 'quote (cons (cadr x) (cadr y))))
  1729. ((eq? (cadr y) '()) (list 'list x))
  1730. (else (list 'cons x y))))
  1731. ((memv key '(list)) (cons 'list (cons x (cdr y))))
  1732. (else (list 'cons x y))))))
  1733. (gen-append (lambda (x y) (if (equal? y ''()) x (list 'append x y))))
  1734. (gen-vector
  1735. (lambda (x)
  1736. (cond ((eq? (car x) 'list) (cons 'vector (cdr x)))
  1737. ((eq? (car x) 'quote) (list 'quote (list->vector (cadr x))))
  1738. (else (list 'list->vector x)))))
  1739. (regen (lambda (x)
  1740. (let ((key (car x)))
  1741. (cond ((memv key '(ref))
  1742. (build-lexical-reference 'value #f (cadr x) (cadr x)))
  1743. ((memv key '(primitive)) (build-primref #f (cadr x)))
  1744. ((memv key '(quote)) (build-data #f (cadr x)))
  1745. ((memv key '(lambda))
  1746. (if (list? (cadr x))
  1747. (build-simple-lambda #f (cadr x) #f (cadr x) '() (regen (caddr x)))
  1748. (error "how did we get here" x)))
  1749. (else (build-primcall #f (car x) (map regen (cdr x)))))))))
  1750. (lambda (e r w s mod)
  1751. (let* ((e (source-wrap e w s mod))
  1752. (tmp e)
  1753. (tmp ($sc-dispatch tmp '(_ any))))
  1754. (if tmp
  1755. (apply (lambda (x)
  1756. (call-with-values
  1757. (lambda () (gen-syntax e x r '() ellipsis? mod))
  1758. (lambda (e maps) (regen e))))
  1759. tmp)
  1760. (syntax-violation 'syntax "bad `syntax' form" e))))))
  1761. (global-extend
  1762. 'core
  1763. 'lambda
  1764. (lambda (e r w s mod)
  1765. (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any any . each-any))))
  1766. (if tmp
  1767. (apply (lambda (args e1 e2)
  1768. (call-with-values
  1769. (lambda () (lambda-formals args))
  1770. (lambda (req opt rest kw)
  1771. (let lp ((body (cons e1 e2)) (meta '()))
  1772. (let* ((tmp-1 body) (tmp ($sc-dispatch tmp-1 '(any any . each-any))))
  1773. (if (and tmp
  1774. (apply (lambda (docstring e1 e2) (string? (syntax->datum docstring)))
  1775. tmp))
  1776. (apply (lambda (docstring e1 e2)
  1777. (lp (cons e1 e2)
  1778. (append meta (list (cons 'documentation (syntax->datum docstring))))))
  1779. tmp)
  1780. (let ((tmp ($sc-dispatch tmp-1 '(#(vector #(each (any . any))) any . each-any))))
  1781. (if tmp
  1782. (apply (lambda (k v e1 e2)
  1783. (lp (cons e1 e2) (append meta (syntax->datum (map cons k v)))))
  1784. tmp)
  1785. (expand-simple-lambda e r w s mod req rest meta body)))))))))
  1786. tmp)
  1787. (syntax-violation 'lambda "bad lambda" e)))))
  1788. (global-extend
  1789. 'core
  1790. 'lambda*
  1791. (lambda (e r w s mod)
  1792. (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any any . each-any))))
  1793. (if tmp
  1794. (apply (lambda (args e1 e2)
  1795. (call-with-values
  1796. (lambda ()
  1797. (expand-lambda-case
  1798. e
  1799. r
  1800. w
  1801. s
  1802. mod
  1803. lambda*-formals
  1804. (list (cons args (cons e1 e2)))))
  1805. (lambda (meta lcase) (build-case-lambda s meta lcase))))
  1806. tmp)
  1807. (syntax-violation 'lambda "bad lambda*" e)))))
  1808. (global-extend
  1809. 'core
  1810. 'case-lambda
  1811. (lambda (e r w s mod)
  1812. (letrec*
  1813. ((build-it
  1814. (lambda (meta clauses)
  1815. (call-with-values
  1816. (lambda () (expand-lambda-case e r w s mod lambda-formals clauses))
  1817. (lambda (meta* lcase)
  1818. (build-case-lambda s (append meta meta*) lcase))))))
  1819. (let* ((tmp-1 e)
  1820. (tmp ($sc-dispatch tmp-1 '(_ . #(each (any any . each-any))))))
  1821. (if tmp
  1822. (apply (lambda (args e1 e2)
  1823. (build-it
  1824. '()
  1825. (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
  1826. e2
  1827. e1
  1828. args)))
  1829. tmp)
  1830. (let ((tmp ($sc-dispatch tmp-1 '(_ any . #(each (any any . each-any))))))
  1831. (if (and tmp
  1832. (apply (lambda (docstring args e1 e2) (string? (syntax->datum docstring)))
  1833. tmp))
  1834. (apply (lambda (docstring args e1 e2)
  1835. (build-it
  1836. (list (cons 'documentation (syntax->datum docstring)))
  1837. (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
  1838. e2
  1839. e1
  1840. args)))
  1841. tmp)
  1842. (syntax-violation 'case-lambda "bad case-lambda" e))))))))
  1843. (global-extend
  1844. 'core
  1845. 'case-lambda*
  1846. (lambda (e r w s mod)
  1847. (letrec*
  1848. ((build-it
  1849. (lambda (meta clauses)
  1850. (call-with-values
  1851. (lambda () (expand-lambda-case e r w s mod lambda*-formals clauses))
  1852. (lambda (meta* lcase)
  1853. (build-case-lambda s (append meta meta*) lcase))))))
  1854. (let* ((tmp-1 e)
  1855. (tmp ($sc-dispatch tmp-1 '(_ . #(each (any any . each-any))))))
  1856. (if tmp
  1857. (apply (lambda (args e1 e2)
  1858. (build-it
  1859. '()
  1860. (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
  1861. e2
  1862. e1
  1863. args)))
  1864. tmp)
  1865. (let ((tmp ($sc-dispatch tmp-1 '(_ any . #(each (any any . each-any))))))
  1866. (if (and tmp
  1867. (apply (lambda (docstring args e1 e2) (string? (syntax->datum docstring)))
  1868. tmp))
  1869. (apply (lambda (docstring args e1 e2)
  1870. (build-it
  1871. (list (cons 'documentation (syntax->datum docstring)))
  1872. (map (lambda (tmp-2 tmp-1 tmp) (cons tmp (cons tmp-1 tmp-2)))
  1873. e2
  1874. e1
  1875. args)))
  1876. tmp)
  1877. (syntax-violation 'case-lambda "bad case-lambda*" e))))))))
  1878. (global-extend
  1879. 'core
  1880. 'with-ellipsis
  1881. (lambda (e r w s mod)
  1882. (let* ((tmp e) (tmp ($sc-dispatch tmp '(_ any any . each-any))))
  1883. (if (and tmp (apply (lambda (dots e1 e2) (id? dots)) tmp))
  1884. (apply (lambda (dots e1 e2)
  1885. (let ((id (if (symbol? dots)
  1886. '#{ $sc-ellipsis }#
  1887. (make-syntax-object
  1888. '#{ $sc-ellipsis }#
  1889. (syntax-object-wrap dots)
  1890. (syntax-object-module dots)))))
  1891. (let ((ids (list id))
  1892. (labels (list (gen-label)))
  1893. (bindings (list (cons 'ellipsis (source-wrap dots w s mod)))))
  1894. (let ((nw (make-binding-wrap ids labels w))
  1895. (nr (extend-env labels bindings r)))
  1896. (expand-body (cons e1 e2) (source-wrap e nw s mod) nr nw mod)))))
  1897. tmp)
  1898. (syntax-violation
  1899. 'with-ellipsis
  1900. "bad syntax"
  1901. (source-wrap e w s mod))))))
  1902. (global-extend
  1903. 'core
  1904. 'let
  1905. (letrec*
  1906. ((expand-let
  1907. (lambda (e r w s mod constructor ids vals exps)
  1908. (if (not (valid-bound-ids? ids))
  1909. (syntax-violation 'let "duplicate bound variable" e)
  1910. (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
  1911. (let ((nw (make-binding-wrap ids labels w))
  1912. (nr (extend-var-env labels new-vars r)))
  1913. (constructor
  1914. s
  1915. (map syntax->datum ids)
  1916. new-vars
  1917. (map (lambda (x) (expand x r w mod)) vals)
  1918. (expand-body exps (source-wrap e nw s mod) nr nw mod))))))))
  1919. (lambda (e r w s mod)
  1920. (let* ((tmp-1 e)
  1921. (tmp ($sc-dispatch tmp-1 '(_ #(each (any any)) any . each-any))))
  1922. (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
  1923. (apply (lambda (id val e1 e2)
  1924. (expand-let e r w s mod build-let id val (cons e1 e2)))
  1925. tmp)
  1926. (let ((tmp ($sc-dispatch tmp-1 '(_ any #(each (any any)) any . each-any))))
  1927. (if (and tmp
  1928. (apply (lambda (f id val e1 e2) (and (id? f) (and-map id? id))) tmp))
  1929. (apply (lambda (f id val e1 e2)
  1930. (expand-let e r w s mod build-named-let (cons f id) val (cons e1 e2)))
  1931. tmp)
  1932. (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))))
  1933. (global-extend
  1934. 'core
  1935. 'letrec
  1936. (lambda (e r w s mod)
  1937. (let* ((tmp e)
  1938. (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
  1939. (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
  1940. (apply (lambda (id val e1 e2)
  1941. (let ((ids id))
  1942. (if (not (valid-bound-ids? ids))
  1943. (syntax-violation 'letrec "duplicate bound variable" e)
  1944. (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
  1945. (let ((w (make-binding-wrap ids labels w))
  1946. (r (extend-var-env labels new-vars r)))
  1947. (build-letrec
  1948. s
  1949. #f
  1950. (map syntax->datum ids)
  1951. new-vars
  1952. (map (lambda (x) (expand x r w mod)) val)
  1953. (expand-body (cons e1 e2) (source-wrap e w s mod) r w mod)))))))
  1954. tmp)
  1955. (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
  1956. (global-extend
  1957. 'core
  1958. 'letrec*
  1959. (lambda (e r w s mod)
  1960. (let* ((tmp e)
  1961. (tmp ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
  1962. (if (and tmp (apply (lambda (id val e1 e2) (and-map id? id)) tmp))
  1963. (apply (lambda (id val e1 e2)
  1964. (let ((ids id))
  1965. (if (not (valid-bound-ids? ids))
  1966. (syntax-violation 'letrec* "duplicate bound variable" e)
  1967. (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
  1968. (let ((w (make-binding-wrap ids labels w))
  1969. (r (extend-var-env labels new-vars r)))
  1970. (build-letrec
  1971. s
  1972. #t
  1973. (map syntax->datum ids)
  1974. new-vars
  1975. (map (lambda (x) (expand x r w mod)) val)
  1976. (expand-body (cons e1 e2) (source-wrap e w s mod) r w mod)))))))
  1977. tmp)
  1978. (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
  1979. (global-extend
  1980. 'core
  1981. 'set!
  1982. (lambda (e r w s mod)
  1983. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ any any))))
  1984. (if (and tmp (apply (lambda (id val) (id? id)) tmp))
  1985. (apply (lambda (id val)
  1986. (call-with-values
  1987. (lambda () (resolve-identifier id w r mod #t))
  1988. (lambda (type value id-mod)
  1989. (let ((key type))
  1990. (cond ((memv key '(lexical))
  1991. (build-lexical-assignment
  1992. s
  1993. (syntax->datum id)
  1994. value
  1995. (expand val r w mod)))
  1996. ((memv key '(global))
  1997. (build-global-assignment s value (expand val r w mod) id-mod))
  1998. ((memv key '(macro))
  1999. (if (procedure-property value 'variable-transformer)
  2000. (expand (expand-macro value e r w s #f mod) r '(()) mod)
  2001. (syntax-violation
  2002. 'set!
  2003. "not a variable transformer"
  2004. (wrap e w mod)
  2005. (wrap id w id-mod))))
  2006. ((memv key '(displaced-lexical))
  2007. (syntax-violation 'set! "identifier out of context" (wrap id w mod)))
  2008. (else (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))))
  2009. tmp)
  2010. (let ((tmp ($sc-dispatch tmp-1 '(_ (any . each-any) any))))
  2011. (if tmp
  2012. (apply (lambda (head tail val)
  2013. (call-with-values
  2014. (lambda () (syntax-type head r '(()) #f #f mod #t))
  2015. (lambda (type value ee* ee ww ss modmod)
  2016. (let ((key type))
  2017. (if (memv key '(module-ref))
  2018. (let ((val (expand val r w mod)))
  2019. (call-with-values
  2020. (lambda () (value (cons head tail) r w mod))
  2021. (lambda (e r w s* mod)
  2022. (let* ((tmp-1 e) (tmp (list tmp-1)))
  2023. (if (and tmp (apply (lambda (e) (id? e)) tmp))
  2024. (apply (lambda (e) (build-global-assignment s (syntax->datum e) val mod))
  2025. tmp)
  2026. (syntax-violation
  2027. #f
  2028. "source expression failed to match any pattern"
  2029. tmp-1))))))
  2030. (build-call
  2031. s
  2032. (expand
  2033. (list '#(syntax-object setter ((top)) (hygiene guile)) head)
  2034. r
  2035. w
  2036. mod)
  2037. (map (lambda (e) (expand e r w mod)) (append tail (list val)))))))))
  2038. tmp)
  2039. (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))))
  2040. (global-extend
  2041. 'module-ref
  2042. '@
  2043. (lambda (e r w mod)
  2044. (let* ((tmp-1 e) (tmp ($sc-dispatch tmp-1 '(_ each-any any))))
  2045. (if (and tmp
  2046. (apply (lambda (mod id) (and (and-map id? mod) (id? id))) tmp))
  2047. (apply (lambda (mod id)
  2048. (values
  2049. (syntax->datum id)
  2050. r
  2051. '((top))
  2052. #f
  2053. (syntax->datum
  2054. (cons '#(syntax-object public ((top)) (hygiene guile)) mod))))
  2055. tmp)
  2056. (syntax-violation
  2057. #f
  2058. "source expression failed to match any pattern"
  2059. tmp-1)))))
  2060. (global-extend
  2061. 'module-ref
  2062. '@@
  2063. (lambda (e r w mod)
  2064. (letrec*
  2065. ((remodulate
  2066. (lambda (x mod)
  2067. (cond ((pair? x) (cons (remodulate (car x) mod) (remodulate (cdr x) mod)))
  2068. ((syntax-object? x)
  2069. (make-syntax-object
  2070. (remodulate (syntax-object-expression x) mod)
  2071. (syntax-object-wrap x)
  2072. mod))
  2073. ((vector? x)
  2074. (let* ((n (vector-length x)) (v (make-vector n)))
  2075. (let loop ((i 0))
  2076. (if (= i n)
  2077. (begin (if #f #f) v)
  2078. (begin
  2079. (vector-set! v i (remodulate (vector-ref x i) mod))
  2080. (loop (+ i 1)))))))
  2081. (else x)))))
  2082. (let* ((tmp e)
  2083. (tmp-1 ($sc-dispatch
  2084. tmp
  2085. '(_ #(free-id #(syntax-object primitive ((top)) (hygiene guile))) any))))
  2086. (if (and tmp-1
  2087. (apply (lambda (id)
  2088. (and (id? id)
  2089. (equal?
  2090. (cdr (if (syntax-object? id) (syntax-object-module id) mod))
  2091. '(guile))))
  2092. tmp-1))
  2093. (apply (lambda (id) (values (syntax->datum id) r '((top)) #f '(primitive)))
  2094. tmp-1)
  2095. (let ((tmp-1 ($sc-dispatch tmp '(_ each-any any))))
  2096. (if (and tmp-1
  2097. (apply (lambda (mod id) (and (and-map id? mod) (id? id))) tmp-1))
  2098. (apply (lambda (mod id)
  2099. (values
  2100. (syntax->datum id)
  2101. r
  2102. '((top))
  2103. #f
  2104. (syntax->datum
  2105. (cons '#(syntax-object private ((top)) (hygiene guile)) mod))))
  2106. tmp-1)
  2107. (let ((tmp-1 ($sc-dispatch
  2108. tmp
  2109. '(_ #(free-id #(syntax-object @@ ((top)) (hygiene guile)))
  2110. each-any
  2111. any))))
  2112. (if (and tmp-1 (apply (lambda (mod exp) (and-map id? mod)) tmp-1))
  2113. (apply (lambda (mod exp)
  2114. (let ((mod (syntax->datum
  2115. (cons '#(syntax-object private ((top)) (hygiene guile)) mod))))
  2116. (values (remodulate exp mod) r w (source-annotation exp) mod)))
  2117. tmp-1)
  2118. (syntax-violation
  2119. #f
  2120. "source expression failed to match any pattern"
  2121. tmp))))))))))
  2122. (global-extend
  2123. 'core
  2124. 'if
  2125. (lambda (e r w s mod)
  2126. (let* ((tmp e) (tmp-1 ($sc-dispatch tmp '(_ any any))))
  2127. (if tmp-1
  2128. (apply (lambda (test then)
  2129. (build-conditional
  2130. s
  2131. (expand test r w mod)
  2132. (expand then r w mod)
  2133. (build-void #f)))
  2134. tmp-1)
  2135. (let ((tmp-1 ($sc-dispatch tmp '(_ any any any))))
  2136. (if tmp-1
  2137. (apply (lambda (test then else)
  2138. (build-conditional
  2139. s
  2140. (expand test r w mod)
  2141. (expand then r w mod)
  2142. (expand else r w mod)))
  2143. tmp-1)
  2144. (syntax-violation
  2145. #f
  2146. "source expression failed to match any pattern"
  2147. tmp)))))))
  2148. (global-extend 'begin 'begin '())
  2149. (global-extend 'define 'define '())
  2150. (global-extend 'define-syntax 'define-syntax '())
  2151. (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
  2152. (global-extend 'eval-when 'eval-when '())
  2153. (global-extend
  2154. 'core
  2155. 'syntax-case
  2156. (letrec*
  2157. ((convert-pattern
  2158. (lambda (pattern keys ellipsis?)
  2159. (letrec*
  2160. ((cvt* (lambda (p* n ids)
  2161. (let* ((tmp p*) (tmp ($sc-dispatch tmp '(any . any))))
  2162. (if tmp
  2163. (apply (lambda (x y)
  2164. (call-with-values
  2165. (lambda () (cvt* y n ids))
  2166. (lambda (y ids)
  2167. (call-with-values
  2168. (lambda () (cvt x n ids))
  2169. (lambda (x ids) (values (cons x y) ids))))))
  2170. tmp)
  2171. (cvt p* n ids)))))
  2172. (v-reverse
  2173. (lambda (x)
  2174. (let loop ((r '()) (x x))
  2175. (if (not (pair? x)) (values r x) (loop (cons (car x) r) (cdr x))))))
  2176. (cvt (lambda (p n ids)
  2177. (if (id? p)
  2178. (cond ((bound-id-member? p keys) (values (vector 'free-id p) ids))
  2179. ((free-id=? p '#(syntax-object _ ((top)) (hygiene guile)))
  2180. (values '_ ids))
  2181. (else (values 'any (cons (cons p n) ids))))
  2182. (let* ((tmp p) (tmp-1 ($sc-dispatch tmp '(any any))))
  2183. (if (and tmp-1 (apply (lambda (x dots) (ellipsis? dots)) tmp-1))
  2184. (apply (lambda (x dots)
  2185. (call-with-values
  2186. (lambda () (cvt x (+ n 1) ids))
  2187. (lambda (p ids)
  2188. (values (if (eq? p 'any) 'each-any (vector 'each p)) ids))))
  2189. tmp-1)
  2190. (let ((tmp-1 ($sc-dispatch tmp '(any any . any))))
  2191. (if (and tmp-1 (apply (lambda (x dots ys) (ellipsis? dots)) tmp-1))
  2192. (apply (lambda (x dots ys)
  2193. (call-with-values
  2194. (lambda () (cvt* ys n ids))
  2195. (lambda (ys ids)
  2196. (call-with-values
  2197. (lambda () (cvt x (+ n 1) ids))
  2198. (lambda (x ids)
  2199. (call-with-values
  2200. (lambda () (v-reverse ys))
  2201. (lambda (ys e) (values (vector 'each+ x ys e) ids))))))))
  2202. tmp-1)
  2203. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  2204. (if tmp-1
  2205. (apply (lambda (x y)
  2206. (call-with-values
  2207. (lambda () (cvt y n ids))
  2208. (lambda (y ids)
  2209. (call-with-values
  2210. (lambda () (cvt x n ids))
  2211. (lambda (x ids) (values (cons x y) ids))))))
  2212. tmp-1)
  2213. (let ((tmp-1 ($sc-dispatch tmp '())))
  2214. (if tmp-1
  2215. (apply (lambda () (values '() ids)) tmp-1)
  2216. (let ((tmp-1 ($sc-dispatch tmp '#(vector each-any))))
  2217. (if tmp-1
  2218. (apply (lambda (x)
  2219. (call-with-values
  2220. (lambda () (cvt x n ids))
  2221. (lambda (p ids) (values (vector 'vector p) ids))))
  2222. tmp-1)
  2223. (let ((x tmp)) (values (vector 'atom (strip p '(()))) ids))))))))))))))))
  2224. (cvt pattern 0 '()))))
  2225. (build-dispatch-call
  2226. (lambda (pvars exp y r mod)
  2227. (let ((ids (map car pvars)) (levels (map cdr pvars)))
  2228. (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
  2229. (build-primcall
  2230. #f
  2231. 'apply
  2232. (list (build-simple-lambda
  2233. #f
  2234. (map syntax->datum ids)
  2235. #f
  2236. new-vars
  2237. '()
  2238. (expand
  2239. exp
  2240. (extend-env
  2241. labels
  2242. (map (lambda (var level) (cons 'syntax (cons var level)))
  2243. new-vars
  2244. (map cdr pvars))
  2245. r)
  2246. (make-binding-wrap ids labels '(()))
  2247. mod))
  2248. y))))))
  2249. (gen-clause
  2250. (lambda (x keys clauses r pat fender exp mod)
  2251. (call-with-values
  2252. (lambda ()
  2253. (convert-pattern pat keys (lambda (e) (ellipsis? e r mod))))
  2254. (lambda (p pvars)
  2255. (cond ((not (and-map (lambda (x) (not (ellipsis? (car x) r mod))) pvars))
  2256. (syntax-violation 'syntax-case "misplaced ellipsis" pat))
  2257. ((not (distinct-bound-ids? (map car pvars)))
  2258. (syntax-violation 'syntax-case "duplicate pattern variable" pat))
  2259. (else
  2260. (let ((y (gen-var 'tmp)))
  2261. (build-call
  2262. #f
  2263. (build-simple-lambda
  2264. #f
  2265. (list 'tmp)
  2266. #f
  2267. (list y)
  2268. '()
  2269. (let ((y (build-lexical-reference 'value #f 'tmp y)))
  2270. (build-conditional
  2271. #f
  2272. (let* ((tmp fender) (tmp ($sc-dispatch tmp '#(atom #t))))
  2273. (if tmp
  2274. (apply (lambda () y) tmp)
  2275. (build-conditional
  2276. #f
  2277. y
  2278. (build-dispatch-call pvars fender y r mod)
  2279. (build-data #f #f))))
  2280. (build-dispatch-call pvars exp y r mod)
  2281. (gen-syntax-case x keys clauses r mod))))
  2282. (list (if (eq? p 'any)
  2283. (build-primcall #f 'list (list x))
  2284. (build-primcall #f '$sc-dispatch (list x (build-data #f p)))))))))))))
  2285. (gen-syntax-case
  2286. (lambda (x keys clauses r mod)
  2287. (if (null? clauses)
  2288. (build-primcall
  2289. #f
  2290. 'syntax-violation
  2291. (list (build-data #f #f)
  2292. (build-data #f "source expression failed to match any pattern")
  2293. x))
  2294. (let* ((tmp-1 (car clauses)) (tmp ($sc-dispatch tmp-1 '(any any))))
  2295. (if tmp
  2296. (apply (lambda (pat exp)
  2297. (if (and (id? pat)
  2298. (and-map
  2299. (lambda (x) (not (free-id=? pat x)))
  2300. (cons '#(syntax-object ... ((top)) (hygiene guile)) keys)))
  2301. (if (free-id=? pat '#(syntax-object _ ((top)) (hygiene guile)))
  2302. (expand exp r '(()) mod)
  2303. (let ((labels (list (gen-label))) (var (gen-var pat)))
  2304. (build-call
  2305. #f
  2306. (build-simple-lambda
  2307. #f
  2308. (list (syntax->datum pat))
  2309. #f
  2310. (list var)
  2311. '()
  2312. (expand
  2313. exp
  2314. (extend-env labels (list (cons 'syntax (cons var 0))) r)
  2315. (make-binding-wrap (list pat) labels '(()))
  2316. mod))
  2317. (list x))))
  2318. (gen-clause x keys (cdr clauses) r pat #t exp mod)))
  2319. tmp)
  2320. (let ((tmp ($sc-dispatch tmp-1 '(any any any))))
  2321. (if tmp
  2322. (apply (lambda (pat fender exp)
  2323. (gen-clause x keys (cdr clauses) r pat fender exp mod))
  2324. tmp)
  2325. (syntax-violation 'syntax-case "invalid clause" (car clauses))))))))))
  2326. (lambda (e r w s mod)
  2327. (let* ((e (source-wrap e w s mod))
  2328. (tmp-1 e)
  2329. (tmp ($sc-dispatch tmp-1 '(_ any each-any . each-any))))
  2330. (if tmp
  2331. (apply (lambda (val key m)
  2332. (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x r mod)))) key)
  2333. (let ((x (gen-var 'tmp)))
  2334. (build-call
  2335. s
  2336. (build-simple-lambda
  2337. #f
  2338. (list 'tmp)
  2339. #f
  2340. (list x)
  2341. '()
  2342. (gen-syntax-case
  2343. (build-lexical-reference 'value #f 'tmp x)
  2344. key
  2345. m
  2346. r
  2347. mod))
  2348. (list (expand val r '(()) mod))))
  2349. (syntax-violation 'syntax-case "invalid literals list" e)))
  2350. tmp)
  2351. (syntax-violation
  2352. #f
  2353. "source expression failed to match any pattern"
  2354. tmp-1))))))
  2355. (set! macroexpand
  2356. (lambda* (x #:optional (m 'e) (esew '(eval)))
  2357. (expand-top-sequence
  2358. (list x)
  2359. '()
  2360. '((top))
  2361. #f
  2362. m
  2363. esew
  2364. (cons 'hygiene (module-name (current-module))))))
  2365. (set! identifier? (lambda (x) (nonsymbol-id? x)))
  2366. (set! datum->syntax
  2367. (lambda (id datum)
  2368. (make-syntax-object
  2369. datum
  2370. (syntax-object-wrap id)
  2371. (syntax-object-module id))))
  2372. (set! syntax->datum (lambda (x) (strip x '(()))))
  2373. (set! syntax-source (lambda (x) (source-annotation x)))
  2374. (set! generate-temporaries
  2375. (lambda (ls)
  2376. (let ((x ls))
  2377. (if (not (list? x))
  2378. (syntax-violation 'generate-temporaries "invalid argument" x)))
  2379. (let ((mod (cons 'hygiene (module-name (current-module)))))
  2380. (map (lambda (x) (wrap (gensym "t-") '((top)) mod)) ls))))
  2381. (set! free-identifier=?
  2382. (lambda (x y)
  2383. (let ((x x))
  2384. (if (not (nonsymbol-id? x))
  2385. (syntax-violation 'free-identifier=? "invalid argument" x)))
  2386. (let ((x y))
  2387. (if (not (nonsymbol-id? x))
  2388. (syntax-violation 'free-identifier=? "invalid argument" x)))
  2389. (free-id=? x y)))
  2390. (set! bound-identifier=?
  2391. (lambda (x y)
  2392. (let ((x x))
  2393. (if (not (nonsymbol-id? x))
  2394. (syntax-violation 'bound-identifier=? "invalid argument" x)))
  2395. (let ((x y))
  2396. (if (not (nonsymbol-id? x))
  2397. (syntax-violation 'bound-identifier=? "invalid argument" x)))
  2398. (bound-id=? x y)))
  2399. (set! syntax-violation
  2400. (lambda* (who message form #:optional (subform #f))
  2401. (let ((x who))
  2402. (if (not (let ((x x)) (or (not x) (string? x) (symbol? x))))
  2403. (syntax-violation 'syntax-violation "invalid argument" x)))
  2404. (let ((x message))
  2405. (if (not (string? x))
  2406. (syntax-violation 'syntax-violation "invalid argument" x)))
  2407. (throw 'syntax-error
  2408. who
  2409. message
  2410. (or (source-annotation subform) (source-annotation form))
  2411. (strip form '(()))
  2412. (and subform (strip subform '(()))))))
  2413. (letrec*
  2414. ((syntax-module
  2415. (lambda (id)
  2416. (let ((x id))
  2417. (if (not (nonsymbol-id? x))
  2418. (syntax-violation 'syntax-module "invalid argument" x)))
  2419. (let ((mod (syntax-object-module id)))
  2420. (and (not (equal? mod '(primitive))) (cdr mod)))))
  2421. (syntax-local-binding
  2422. (lambda* (id
  2423. #:key
  2424. (resolve-syntax-parameters? #t #:resolve-syntax-parameters?))
  2425. (let ((x id))
  2426. (if (not (nonsymbol-id? x))
  2427. (syntax-violation 'syntax-local-binding "invalid argument" x)))
  2428. (with-transformer-environment
  2429. (lambda (e r w s rib mod)
  2430. (letrec*
  2431. ((strip-anti-mark
  2432. (lambda (w)
  2433. (let ((ms (car w)) (s (cdr w)))
  2434. (if (and (pair? ms) (eq? (car ms) #f))
  2435. (cons (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
  2436. (cons ms (if rib (cons rib s) s)))))))
  2437. (call-with-values
  2438. (lambda ()
  2439. (resolve-identifier
  2440. (syntax-object-expression id)
  2441. (strip-anti-mark (syntax-object-wrap id))
  2442. r
  2443. (syntax-object-module id)
  2444. resolve-syntax-parameters?))
  2445. (lambda (type value mod)
  2446. (let ((key type))
  2447. (cond ((memv key '(lexical)) (values 'lexical value))
  2448. ((memv key '(macro)) (values 'macro value))
  2449. ((memv key '(syntax-parameter))
  2450. (values 'syntax-parameter (car value)))
  2451. ((memv key '(syntax)) (values 'pattern-variable value))
  2452. ((memv key '(displaced-lexical)) (values 'displaced-lexical #f))
  2453. ((memv key '(global))
  2454. (if (equal? mod '(primitive))
  2455. (values 'primitive value)
  2456. (values 'global (cons value (cdr mod)))))
  2457. ((memv key '(ellipsis))
  2458. (values
  2459. 'ellipsis
  2460. (make-syntax-object
  2461. (syntax-object-expression value)
  2462. (anti-mark (syntax-object-wrap value))
  2463. (syntax-object-module value))))
  2464. (else (values 'other #f)))))))))))
  2465. (syntax-locally-bound-identifiers
  2466. (lambda (id)
  2467. (let ((x id))
  2468. (if (not (nonsymbol-id? x))
  2469. (syntax-violation
  2470. 'syntax-locally-bound-identifiers
  2471. "invalid argument"
  2472. x)))
  2473. (locally-bound-identifiers
  2474. (syntax-object-wrap id)
  2475. (syntax-object-module id)))))
  2476. (define! 'syntax-module syntax-module)
  2477. (define! 'syntax-local-binding syntax-local-binding)
  2478. (define!
  2479. 'syntax-locally-bound-identifiers
  2480. syntax-locally-bound-identifiers))
  2481. (letrec*
  2482. ((match-each
  2483. (lambda (e p w mod)
  2484. (cond ((pair? e)
  2485. (let ((first (match (car e) p w '() mod)))
  2486. (and first
  2487. (let ((rest (match-each (cdr e) p w mod)))
  2488. (and rest (cons first rest))))))
  2489. ((null? e) '())
  2490. ((syntax-object? e)
  2491. (match-each
  2492. (syntax-object-expression e)
  2493. p
  2494. (join-wraps w (syntax-object-wrap e))
  2495. (syntax-object-module e)))
  2496. (else #f))))
  2497. (match-each+
  2498. (lambda (e x-pat y-pat z-pat w r mod)
  2499. (let f ((e e) (w w))
  2500. (cond ((pair? e)
  2501. (call-with-values
  2502. (lambda () (f (cdr e) w))
  2503. (lambda (xr* y-pat r)
  2504. (if r
  2505. (if (null? y-pat)
  2506. (let ((xr (match (car e) x-pat w '() mod)))
  2507. (if xr (values (cons xr xr*) y-pat r) (values #f #f #f)))
  2508. (values '() (cdr y-pat) (match (car e) (car y-pat) w r mod)))
  2509. (values #f #f #f)))))
  2510. ((syntax-object? e)
  2511. (f (syntax-object-expression e) (join-wraps w e)))
  2512. (else (values '() y-pat (match e z-pat w r mod)))))))
  2513. (match-each-any
  2514. (lambda (e w mod)
  2515. (cond ((pair? e)
  2516. (let ((l (match-each-any (cdr e) w mod)))
  2517. (and l (cons (wrap (car e) w mod) l))))
  2518. ((null? e) '())
  2519. ((syntax-object? e)
  2520. (match-each-any
  2521. (syntax-object-expression e)
  2522. (join-wraps w (syntax-object-wrap e))
  2523. mod))
  2524. (else #f))))
  2525. (match-empty
  2526. (lambda (p r)
  2527. (cond ((null? p) r)
  2528. ((eq? p '_) r)
  2529. ((eq? p 'any) (cons '() r))
  2530. ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
  2531. ((eq? p 'each-any) (cons '() r))
  2532. (else
  2533. (let ((key (vector-ref p 0)))
  2534. (cond ((memv key '(each)) (match-empty (vector-ref p 1) r))
  2535. ((memv key '(each+))
  2536. (match-empty
  2537. (vector-ref p 1)
  2538. (match-empty
  2539. (reverse (vector-ref p 2))
  2540. (match-empty (vector-ref p 3) r))))
  2541. ((memv key '(free-id atom)) r)
  2542. ((memv key '(vector)) (match-empty (vector-ref p 1) r))))))))
  2543. (combine
  2544. (lambda (r* r)
  2545. (if (null? (car r*)) r (cons (map car r*) (combine (map cdr r*) r)))))
  2546. (match*
  2547. (lambda (e p w r mod)
  2548. (cond ((null? p) (and (null? e) r))
  2549. ((pair? p)
  2550. (and (pair? e)
  2551. (match (car e) (car p) w (match (cdr e) (cdr p) w r mod) mod)))
  2552. ((eq? p 'each-any)
  2553. (let ((l (match-each-any e w mod))) (and l (cons l r))))
  2554. (else
  2555. (let ((key (vector-ref p 0)))
  2556. (cond ((memv key '(each))
  2557. (if (null? e)
  2558. (match-empty (vector-ref p 1) r)
  2559. (let ((l (match-each e (vector-ref p 1) w mod)))
  2560. (and l
  2561. (let collect ((l l))
  2562. (if (null? (car l)) r (cons (map car l) (collect (map cdr l)))))))))
  2563. ((memv key '(each+))
  2564. (call-with-values
  2565. (lambda ()
  2566. (match-each+
  2567. e
  2568. (vector-ref p 1)
  2569. (vector-ref p 2)
  2570. (vector-ref p 3)
  2571. w
  2572. r
  2573. mod))
  2574. (lambda (xr* y-pat r)
  2575. (and r
  2576. (null? y-pat)
  2577. (if (null? xr*) (match-empty (vector-ref p 1) r) (combine xr* r))))))
  2578. ((memv key '(free-id))
  2579. (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
  2580. ((memv key '(atom)) (and (equal? (vector-ref p 1) (strip e w)) r))
  2581. ((memv key '(vector))
  2582. (and (vector? e) (match (vector->list e) (vector-ref p 1) w r mod)))))))))
  2583. (match (lambda (e p w r mod)
  2584. (cond ((not r) #f)
  2585. ((eq? p '_) r)
  2586. ((eq? p 'any) (cons (wrap e w mod) r))
  2587. ((syntax-object? e)
  2588. (match*
  2589. (syntax-object-expression e)
  2590. p
  2591. (join-wraps w (syntax-object-wrap e))
  2592. r
  2593. (syntax-object-module e)))
  2594. (else (match* e p w r mod))))))
  2595. (set! $sc-dispatch
  2596. (lambda (e p)
  2597. (cond ((eq? p 'any) (list e))
  2598. ((eq? p '_) '())
  2599. ((syntax-object? e)
  2600. (match*
  2601. (syntax-object-expression e)
  2602. p
  2603. (syntax-object-wrap e)
  2604. '()
  2605. (syntax-object-module e)))
  2606. (else (match* e p '(()) '() #f)))))))
  2607. (define with-syntax
  2608. (make-syntax-transformer
  2609. 'with-syntax
  2610. 'macro
  2611. (lambda (x)
  2612. (let ((tmp x))
  2613. (let ((tmp-1 ($sc-dispatch tmp '(_ () any . each-any))))
  2614. (if tmp-1
  2615. (apply (lambda (e1 e2)
  2616. (cons '#(syntax-object let ((top)) (hygiene guile))
  2617. (cons '() (cons e1 e2))))
  2618. tmp-1)
  2619. (let ((tmp-1 ($sc-dispatch tmp '(_ ((any any)) any . each-any))))
  2620. (if tmp-1
  2621. (apply (lambda (out in e1 e2)
  2622. (list '#(syntax-object syntax-case ((top)) (hygiene guile))
  2623. in
  2624. '()
  2625. (list out
  2626. (cons '#(syntax-object let ((top)) (hygiene guile))
  2627. (cons '() (cons e1 e2))))))
  2628. tmp-1)
  2629. (let ((tmp-1 ($sc-dispatch tmp '(_ #(each (any any)) any . each-any))))
  2630. (if tmp-1
  2631. (apply (lambda (out in e1 e2)
  2632. (list '#(syntax-object syntax-case ((top)) (hygiene guile))
  2633. (cons '#(syntax-object list ((top)) (hygiene guile)) in)
  2634. '()
  2635. (list out
  2636. (cons '#(syntax-object let ((top)) (hygiene guile))
  2637. (cons '() (cons e1 e2))))))
  2638. tmp-1)
  2639. (syntax-violation
  2640. #f
  2641. "source expression failed to match any pattern"
  2642. tmp)))))))))))
  2643. (define syntax-error
  2644. (make-syntax-transformer
  2645. 'syntax-error
  2646. 'macro
  2647. (lambda (x)
  2648. (let ((tmp-1 x))
  2649. (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any . each-any))))
  2650. (if (if tmp
  2651. (apply (lambda (keyword operands message arg)
  2652. (string? (syntax->datum message)))
  2653. tmp)
  2654. #f)
  2655. (apply (lambda (keyword operands message arg)
  2656. (syntax-violation
  2657. (syntax->datum keyword)
  2658. (string-join
  2659. (cons (syntax->datum message)
  2660. (map (lambda (x) (object->string (syntax->datum x))) arg)))
  2661. (if (syntax->datum keyword) (cons keyword operands) #f)))
  2662. tmp)
  2663. (let ((tmp ($sc-dispatch tmp-1 '(_ any . each-any))))
  2664. (if (if tmp
  2665. (apply (lambda (message arg) (string? (syntax->datum message))) tmp)
  2666. #f)
  2667. (apply (lambda (message arg)
  2668. (cons '#(syntax-object
  2669. syntax-error
  2670. ((top)
  2671. #(ribcage
  2672. #(syntax-error)
  2673. #((top))
  2674. #(((hygiene guile)
  2675. .
  2676. #(syntax-object syntax-error ((top)) (hygiene guile))))))
  2677. (hygiene guile))
  2678. (cons '(#f) (cons message arg))))
  2679. tmp)
  2680. (syntax-violation
  2681. #f
  2682. "source expression failed to match any pattern"
  2683. tmp-1)))))))))
  2684. (define syntax-rules
  2685. (make-syntax-transformer
  2686. 'syntax-rules
  2687. 'macro
  2688. (lambda (xx)
  2689. (letrec*
  2690. ((expand-clause
  2691. (lambda (clause)
  2692. (let ((tmp-1 clause))
  2693. (let ((tmp ($sc-dispatch
  2694. tmp-1
  2695. '((any . any)
  2696. (#(free-id #(syntax-object syntax-error ((top)) (hygiene guile)))
  2697. any
  2698. .
  2699. each-any)))))
  2700. (if (if tmp
  2701. (apply (lambda (keyword pattern message arg)
  2702. (string? (syntax->datum message)))
  2703. tmp)
  2704. #f)
  2705. (apply (lambda (keyword pattern message arg)
  2706. (list (cons '#(syntax-object dummy ((top)) (hygiene guile)) pattern)
  2707. (list '#(syntax-object syntax ((top)) (hygiene guile))
  2708. (cons '#(syntax-object syntax-error ((top)) (hygiene guile))
  2709. (cons (cons '#(syntax-object dummy ((top)) (hygiene guile)) pattern)
  2710. (cons message arg))))))
  2711. tmp)
  2712. (let ((tmp ($sc-dispatch tmp-1 '((any . any) any))))
  2713. (if tmp
  2714. (apply (lambda (keyword pattern template)
  2715. (list (cons '#(syntax-object dummy ((top)) (hygiene guile)) pattern)
  2716. (list '#(syntax-object syntax ((top)) (hygiene guile)) template)))
  2717. tmp)
  2718. (syntax-violation
  2719. #f
  2720. "source expression failed to match any pattern"
  2721. tmp-1))))))))
  2722. (expand-syntax-rules
  2723. (lambda (dots keys docstrings clauses)
  2724. (let ((tmp-1 (list keys docstrings clauses (map expand-clause clauses))))
  2725. (let ((tmp ($sc-dispatch
  2726. tmp-1
  2727. '(each-any each-any #(each ((any . any) any)) each-any))))
  2728. (if tmp
  2729. (apply (lambda (k docstring keyword pattern template clause)
  2730. (let ((tmp (cons '#(syntax-object lambda ((top)) (hygiene guile))
  2731. (cons '(#(syntax-object x ((top)) (hygiene guile)))
  2732. (append
  2733. docstring
  2734. (list (vector
  2735. '(#(syntax-object macro-type ((top)) (hygiene guile))
  2736. .
  2737. #(syntax-object
  2738. syntax-rules
  2739. ((top)
  2740. #(ribcage
  2741. #(syntax-rules)
  2742. #((top))
  2743. #(((hygiene guile)
  2744. .
  2745. #(syntax-object
  2746. syntax-rules
  2747. ((top))
  2748. (hygiene guile))))))
  2749. (hygiene guile)))
  2750. (cons '#(syntax-object patterns ((top)) (hygiene guile))
  2751. pattern))
  2752. (cons '#(syntax-object syntax-case ((top)) (hygiene guile))
  2753. (cons '#(syntax-object x ((top)) (hygiene guile))
  2754. (cons k clause)))))))))
  2755. (let ((form tmp))
  2756. (if dots
  2757. (let ((tmp dots))
  2758. (let ((dots tmp))
  2759. (list '#(syntax-object with-ellipsis ((top)) (hygiene guile))
  2760. dots
  2761. form)))
  2762. form))))
  2763. tmp)
  2764. (syntax-violation
  2765. #f
  2766. "source expression failed to match any pattern"
  2767. tmp-1)))))))
  2768. (let ((tmp xx))
  2769. (let ((tmp-1 ($sc-dispatch tmp '(_ each-any . #(each ((any . any) any))))))
  2770. (if tmp-1
  2771. (apply (lambda (k keyword pattern template)
  2772. (expand-syntax-rules
  2773. #f
  2774. k
  2775. '()
  2776. (map (lambda (tmp-2 tmp-1 tmp) (list (cons tmp tmp-1) tmp-2))
  2777. template
  2778. pattern
  2779. keyword)))
  2780. tmp-1)
  2781. (let ((tmp-1 ($sc-dispatch tmp '(_ each-any any . #(each ((any . any) any))))))
  2782. (if (if tmp-1
  2783. (apply (lambda (k docstring keyword pattern template)
  2784. (string? (syntax->datum docstring)))
  2785. tmp-1)
  2786. #f)
  2787. (apply (lambda (k docstring keyword pattern template)
  2788. (expand-syntax-rules
  2789. #f
  2790. k
  2791. (list docstring)
  2792. (map (lambda (tmp-2 tmp-1 tmp) (list (cons tmp tmp-1) tmp-2))
  2793. template
  2794. pattern
  2795. keyword)))
  2796. tmp-1)
  2797. (let ((tmp-1 ($sc-dispatch tmp '(_ any each-any . #(each ((any . any) any))))))
  2798. (if (if tmp-1
  2799. (apply (lambda (dots k keyword pattern template) (identifier? dots))
  2800. tmp-1)
  2801. #f)
  2802. (apply (lambda (dots k keyword pattern template)
  2803. (expand-syntax-rules
  2804. dots
  2805. k
  2806. '()
  2807. (map (lambda (tmp-2 tmp-1 tmp) (list (cons tmp tmp-1) tmp-2))
  2808. template
  2809. pattern
  2810. keyword)))
  2811. tmp-1)
  2812. (let ((tmp-1 ($sc-dispatch tmp '(_ any each-any any . #(each ((any . any) any))))))
  2813. (if (if tmp-1
  2814. (apply (lambda (dots k docstring keyword pattern template)
  2815. (if (identifier? dots) (string? (syntax->datum docstring)) #f))
  2816. tmp-1)
  2817. #f)
  2818. (apply (lambda (dots k docstring keyword pattern template)
  2819. (expand-syntax-rules
  2820. dots
  2821. k
  2822. (list docstring)
  2823. (map (lambda (tmp-2 tmp-1 tmp) (list (cons tmp tmp-1) tmp-2))
  2824. template
  2825. pattern
  2826. keyword)))
  2827. tmp-1)
  2828. (syntax-violation
  2829. #f
  2830. "source expression failed to match any pattern"
  2831. tmp))))))))))))))
  2832. (define define-syntax-rule
  2833. (make-syntax-transformer
  2834. 'define-syntax-rule
  2835. 'macro
  2836. (lambda (x)
  2837. (let ((tmp-1 x))
  2838. (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any))))
  2839. (if tmp
  2840. (apply (lambda (name pattern template)
  2841. (list '#(syntax-object define-syntax ((top)) (hygiene guile))
  2842. name
  2843. (list '#(syntax-object syntax-rules ((top)) (hygiene guile))
  2844. '()
  2845. (list (cons '#(syntax-object _ ((top)) (hygiene guile)) pattern)
  2846. template))))
  2847. tmp)
  2848. (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any any))))
  2849. (if (if tmp
  2850. (apply (lambda (name pattern docstring template)
  2851. (string? (syntax->datum docstring)))
  2852. tmp)
  2853. #f)
  2854. (apply (lambda (name pattern docstring template)
  2855. (list '#(syntax-object define-syntax ((top)) (hygiene guile))
  2856. name
  2857. (list '#(syntax-object syntax-rules ((top)) (hygiene guile))
  2858. '()
  2859. docstring
  2860. (list (cons '#(syntax-object _ ((top)) (hygiene guile)) pattern)
  2861. template))))
  2862. tmp)
  2863. (syntax-violation
  2864. #f
  2865. "source expression failed to match any pattern"
  2866. tmp-1)))))))))
  2867. (define let*
  2868. (make-syntax-transformer
  2869. 'let*
  2870. 'macro
  2871. (lambda (x)
  2872. (let ((tmp-1 x))
  2873. (let ((tmp ($sc-dispatch tmp-1 '(any #(each (any any)) any . each-any))))
  2874. (if (if tmp
  2875. (apply (lambda (let* x v e1 e2) (and-map identifier? x)) tmp)
  2876. #f)
  2877. (apply (lambda (let* x v e1 e2)
  2878. (let f ((bindings (map list x v)))
  2879. (if (null? bindings)
  2880. (cons '#(syntax-object let ((top)) (hygiene guile))
  2881. (cons '() (cons e1 e2)))
  2882. (let ((tmp-1 (list (f (cdr bindings)) (car bindings))))
  2883. (let ((tmp ($sc-dispatch tmp-1 '(any any))))
  2884. (if tmp
  2885. (apply (lambda (body binding)
  2886. (list '#(syntax-object let ((top)) (hygiene guile))
  2887. (list binding)
  2888. body))
  2889. tmp)
  2890. (syntax-violation
  2891. #f
  2892. "source expression failed to match any pattern"
  2893. tmp-1)))))))
  2894. tmp)
  2895. (syntax-violation
  2896. #f
  2897. "source expression failed to match any pattern"
  2898. tmp-1)))))))
  2899. (define quasiquote
  2900. (make-syntax-transformer
  2901. 'quasiquote
  2902. 'macro
  2903. (letrec*
  2904. ((quasi (lambda (p lev)
  2905. (let ((tmp p))
  2906. (let ((tmp-1 ($sc-dispatch
  2907. tmp
  2908. '(#(free-id #(syntax-object unquote ((top)) (hygiene guile))) any))))
  2909. (if tmp-1
  2910. (apply (lambda (p)
  2911. (if (= lev 0)
  2912. (list "value" p)
  2913. (quasicons
  2914. '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
  2915. (quasi (list p) (- lev 1)))))
  2916. tmp-1)
  2917. (let ((tmp-1 ($sc-dispatch
  2918. tmp
  2919. '(#(free-id
  2920. #(syntax-object
  2921. quasiquote
  2922. ((top)
  2923. #(ribcage
  2924. #(quasiquote)
  2925. #((top))
  2926. #(((hygiene guile)
  2927. .
  2928. #(syntax-object quasiquote ((top)) (hygiene guile))))))
  2929. (hygiene guile)))
  2930. any))))
  2931. (if tmp-1
  2932. (apply (lambda (p)
  2933. (quasicons
  2934. '("quote"
  2935. #(syntax-object
  2936. quasiquote
  2937. ((top)
  2938. #(ribcage
  2939. #(quasiquote)
  2940. #((top))
  2941. #(((hygiene guile)
  2942. .
  2943. #(syntax-object quasiquote ((top)) (hygiene guile))))))
  2944. (hygiene guile)))
  2945. (quasi (list p) (+ lev 1))))
  2946. tmp-1)
  2947. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  2948. (if tmp-1
  2949. (apply (lambda (p q)
  2950. (let ((tmp-1 p))
  2951. (let ((tmp ($sc-dispatch
  2952. tmp-1
  2953. '(#(free-id #(syntax-object unquote ((top)) (hygiene guile)))
  2954. .
  2955. each-any))))
  2956. (if tmp
  2957. (apply (lambda (p)
  2958. (if (= lev 0)
  2959. (quasilist*
  2960. (map (lambda (tmp) (list "value" tmp)) p)
  2961. (quasi q lev))
  2962. (quasicons
  2963. (quasicons
  2964. '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
  2965. (quasi p (- lev 1)))
  2966. (quasi q lev))))
  2967. tmp)
  2968. (let ((tmp ($sc-dispatch
  2969. tmp-1
  2970. '(#(free-id
  2971. #(syntax-object unquote-splicing ((top)) (hygiene guile)))
  2972. .
  2973. each-any))))
  2974. (if tmp
  2975. (apply (lambda (p)
  2976. (if (= lev 0)
  2977. (quasiappend
  2978. (map (lambda (tmp) (list "value" tmp)) p)
  2979. (quasi q lev))
  2980. (quasicons
  2981. (quasicons
  2982. '("quote"
  2983. #(syntax-object
  2984. unquote-splicing
  2985. ((top))
  2986. (hygiene guile)))
  2987. (quasi p (- lev 1)))
  2988. (quasi q lev))))
  2989. tmp)
  2990. (quasicons (quasi p lev) (quasi q lev))))))))
  2991. tmp-1)
  2992. (let ((tmp-1 ($sc-dispatch tmp '#(vector each-any))))
  2993. (if tmp-1
  2994. (apply (lambda (x) (quasivector (vquasi x lev))) tmp-1)
  2995. (let ((p tmp)) (list "quote" p)))))))))))))
  2996. (vquasi
  2997. (lambda (p lev)
  2998. (let ((tmp p))
  2999. (let ((tmp-1 ($sc-dispatch tmp '(any . any))))
  3000. (if tmp-1
  3001. (apply (lambda (p q)
  3002. (let ((tmp-1 p))
  3003. (let ((tmp ($sc-dispatch
  3004. tmp-1
  3005. '(#(free-id #(syntax-object unquote ((top)) (hygiene guile)))
  3006. .
  3007. each-any))))
  3008. (if tmp
  3009. (apply (lambda (p)
  3010. (if (= lev 0)
  3011. (quasilist* (map (lambda (tmp) (list "value" tmp)) p) (vquasi q lev))
  3012. (quasicons
  3013. (quasicons
  3014. '("quote" #(syntax-object unquote ((top)) (hygiene guile)))
  3015. (quasi p (- lev 1)))
  3016. (vquasi q lev))))
  3017. tmp)
  3018. (let ((tmp ($sc-dispatch
  3019. tmp-1
  3020. '(#(free-id #(syntax-object unquote-splicing ((top)) (hygiene guile)))
  3021. .
  3022. each-any))))
  3023. (if tmp
  3024. (apply (lambda (p)
  3025. (if (= lev 0)
  3026. (quasiappend
  3027. (map (lambda (tmp) (list "value" tmp)) p)
  3028. (vquasi q lev))
  3029. (quasicons
  3030. (quasicons
  3031. '("quote" #(syntax-object unquote-splicing ((top)) (hygiene guile)))
  3032. (quasi p (- lev 1)))
  3033. (vquasi q lev))))
  3034. tmp)
  3035. (quasicons (quasi p lev) (vquasi q lev))))))))
  3036. tmp-1)
  3037. (let ((tmp-1 ($sc-dispatch tmp '())))
  3038. (if tmp-1
  3039. (apply (lambda () '("quote" ())) tmp-1)
  3040. (syntax-violation
  3041. #f
  3042. "source expression failed to match any pattern"
  3043. tmp))))))))
  3044. (quasicons
  3045. (lambda (x y)
  3046. (let ((tmp-1 (list x y)))
  3047. (let ((tmp ($sc-dispatch tmp-1 '(any any))))
  3048. (if tmp
  3049. (apply (lambda (x y)
  3050. (let ((tmp y))
  3051. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") any))))
  3052. (if tmp-1
  3053. (apply (lambda (dy)
  3054. (let ((tmp x))
  3055. (let ((tmp ($sc-dispatch tmp '(#(atom "quote") any))))
  3056. (if tmp
  3057. (apply (lambda (dx) (list "quote" (cons dx dy))) tmp)
  3058. (if (null? dy) (list "list" x) (list "list*" x y))))))
  3059. tmp-1)
  3060. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . any))))
  3061. (if tmp-1
  3062. (apply (lambda (stuff) (cons "list" (cons x stuff))) tmp-1)
  3063. (let ((tmp ($sc-dispatch tmp '(#(atom "list*") . any))))
  3064. (if tmp
  3065. (apply (lambda (stuff) (cons "list*" (cons x stuff))) tmp)
  3066. (list "list*" x y)))))))))
  3067. tmp)
  3068. (syntax-violation
  3069. #f
  3070. "source expression failed to match any pattern"
  3071. tmp-1))))))
  3072. (quasiappend
  3073. (lambda (x y)
  3074. (let ((tmp y))
  3075. (let ((tmp ($sc-dispatch tmp '(#(atom "quote") ()))))
  3076. (if tmp
  3077. (apply (lambda ()
  3078. (if (null? x)
  3079. '("quote" ())
  3080. (if (null? (cdr x))
  3081. (car x)
  3082. (let ((tmp-1 x))
  3083. (let ((tmp ($sc-dispatch tmp-1 'each-any)))
  3084. (if tmp
  3085. (apply (lambda (p) (cons "append" p)) tmp)
  3086. (syntax-violation
  3087. #f
  3088. "source expression failed to match any pattern"
  3089. tmp-1)))))))
  3090. tmp)
  3091. (if (null? x)
  3092. y
  3093. (let ((tmp-1 (list x y)))
  3094. (let ((tmp ($sc-dispatch tmp-1 '(each-any any))))
  3095. (if tmp
  3096. (apply (lambda (p y) (cons "append" (append p (list y)))) tmp)
  3097. (syntax-violation
  3098. #f
  3099. "source expression failed to match any pattern"
  3100. tmp-1))))))))))
  3101. (quasilist*
  3102. (lambda (x y)
  3103. (let f ((x x)) (if (null? x) y (quasicons (car x) (f (cdr x)))))))
  3104. (quasivector
  3105. (lambda (x)
  3106. (let ((tmp x))
  3107. (let ((tmp ($sc-dispatch tmp '(#(atom "quote") each-any))))
  3108. (if tmp
  3109. (apply (lambda (x) (list "quote" (list->vector x))) tmp)
  3110. (let f ((y x)
  3111. (k (lambda (ls)
  3112. (let ((tmp-1 ls))
  3113. (let ((tmp ($sc-dispatch tmp-1 'each-any)))
  3114. (if tmp
  3115. (apply (lambda (t) (cons "vector" t)) tmp)
  3116. (syntax-violation
  3117. #f
  3118. "source expression failed to match any pattern"
  3119. tmp-1)))))))
  3120. (let ((tmp y))
  3121. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") each-any))))
  3122. (if tmp-1
  3123. (apply (lambda (y) (k (map (lambda (tmp) (list "quote" tmp)) y)))
  3124. tmp-1)
  3125. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . each-any))))
  3126. (if tmp-1
  3127. (apply (lambda (y) (k y)) tmp-1)
  3128. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list*") . #(each+ any (any) ())))))
  3129. (if tmp-1
  3130. (apply (lambda (y z) (f z (lambda (ls) (k (append y ls))))) tmp-1)
  3131. (let ((else tmp))
  3132. (let ((tmp x)) (let ((t tmp)) (list "list->vector" t)))))))))))))))))
  3133. (emit (lambda (x)
  3134. (let ((tmp x))
  3135. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "quote") any))))
  3136. (if tmp-1
  3137. (apply (lambda (x) (list '#(syntax-object quote ((top)) (hygiene guile)) x))
  3138. tmp-1)
  3139. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list") . each-any))))
  3140. (if tmp-1
  3141. (apply (lambda (x)
  3142. (let ((tmp-1 (map emit x)))
  3143. (let ((tmp ($sc-dispatch tmp-1 'each-any)))
  3144. (if tmp
  3145. (apply (lambda (t) (cons '#(syntax-object list ((top)) (hygiene guile)) t))
  3146. tmp)
  3147. (syntax-violation
  3148. #f
  3149. "source expression failed to match any pattern"
  3150. tmp-1)))))
  3151. tmp-1)
  3152. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list*") . #(each+ any (any) ())))))
  3153. (if tmp-1
  3154. (apply (lambda (x y)
  3155. (let f ((x* x))
  3156. (if (null? x*)
  3157. (emit y)
  3158. (let ((tmp-1 (list (emit (car x*)) (f (cdr x*)))))
  3159. (let ((tmp ($sc-dispatch tmp-1 '(any any))))
  3160. (if tmp
  3161. (apply (lambda (t-1 t)
  3162. (list '#(syntax-object cons ((top)) (hygiene guile)) t-1 t))
  3163. tmp)
  3164. (syntax-violation
  3165. #f
  3166. "source expression failed to match any pattern"
  3167. tmp-1)))))))
  3168. tmp-1)
  3169. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "append") . each-any))))
  3170. (if tmp-1
  3171. (apply (lambda (x)
  3172. (let ((tmp-1 (map emit x)))
  3173. (let ((tmp ($sc-dispatch tmp-1 'each-any)))
  3174. (if tmp
  3175. (apply (lambda (t)
  3176. (cons '#(syntax-object append ((top)) (hygiene guile)) t))
  3177. tmp)
  3178. (syntax-violation
  3179. #f
  3180. "source expression failed to match any pattern"
  3181. tmp-1)))))
  3182. tmp-1)
  3183. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "vector") . each-any))))
  3184. (if tmp-1
  3185. (apply (lambda (x)
  3186. (let ((tmp-1 (map emit x)))
  3187. (let ((tmp ($sc-dispatch tmp-1 'each-any)))
  3188. (if tmp
  3189. (apply (lambda (t)
  3190. (cons '#(syntax-object vector ((top)) (hygiene guile)) t))
  3191. tmp)
  3192. (syntax-violation
  3193. #f
  3194. "source expression failed to match any pattern"
  3195. tmp-1)))))
  3196. tmp-1)
  3197. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "list->vector") any))))
  3198. (if tmp-1
  3199. (apply (lambda (x)
  3200. (let ((tmp (emit x)))
  3201. (let ((t tmp))
  3202. (list '#(syntax-object list->vector ((top)) (hygiene guile)) t))))
  3203. tmp-1)
  3204. (let ((tmp-1 ($sc-dispatch tmp '(#(atom "value") any))))
  3205. (if tmp-1
  3206. (apply (lambda (x) x) tmp-1)
  3207. (syntax-violation
  3208. #f
  3209. "source expression failed to match any pattern"
  3210. tmp)))))))))))))))))))
  3211. (lambda (x)
  3212. (let ((tmp-1 x))
  3213. (let ((tmp ($sc-dispatch tmp-1 '(_ any))))
  3214. (if tmp
  3215. (apply (lambda (e) (emit (quasi e 0))) tmp)
  3216. (syntax-violation
  3217. #f
  3218. "source expression failed to match any pattern"
  3219. tmp-1))))))))
  3220. (define include
  3221. (make-syntax-transformer
  3222. 'include
  3223. 'macro
  3224. (lambda (x)
  3225. (letrec*
  3226. ((read-file
  3227. (lambda (fn dir k)
  3228. (let ((p (open-input-file
  3229. (if (absolute-file-name? fn)
  3230. fn
  3231. (if dir
  3232. (in-vicinity dir fn)
  3233. (syntax-violation
  3234. 'include
  3235. "relative file name only allowed when the include form is in a file"
  3236. x))))))
  3237. (let ((enc (file-encoding p)))
  3238. (set-port-encoding! p (let ((t enc)) (if t t "UTF-8")))
  3239. (let f ((x (read p)) (result '()))
  3240. (if (eof-object? x)
  3241. (begin (close-input-port p) (reverse result))
  3242. (f (read p) (cons (datum->syntax k x) result)))))))))
  3243. (let ((src (syntax-source x)))
  3244. (let ((file (if src (assq-ref src 'filename) #f)))
  3245. (let ((dir (if (string? file) (dirname file) #f)))
  3246. (let ((tmp-1 x))
  3247. (let ((tmp ($sc-dispatch tmp-1 '(any any))))
  3248. (if tmp
  3249. (apply (lambda (k filename)
  3250. (let ((fn (syntax->datum filename)))
  3251. (let ((tmp-1 (read-file fn dir filename)))
  3252. (let ((tmp ($sc-dispatch tmp-1 'each-any)))
  3253. (if tmp
  3254. (apply (lambda (exp)
  3255. (cons '#(syntax-object begin ((top)) (hygiene guile)) exp))
  3256. tmp)
  3257. (syntax-violation
  3258. #f
  3259. "source expression failed to match any pattern"
  3260. tmp-1))))))
  3261. tmp)
  3262. (syntax-violation
  3263. #f
  3264. "source expression failed to match any pattern"
  3265. tmp-1)))))))))))
  3266. (define include-from-path
  3267. (make-syntax-transformer
  3268. 'include-from-path
  3269. 'macro
  3270. (lambda (x)
  3271. (let ((tmp-1 x))
  3272. (let ((tmp ($sc-dispatch tmp-1 '(any any))))
  3273. (if tmp
  3274. (apply (lambda (k filename)
  3275. (let ((fn (syntax->datum filename)))
  3276. (let ((tmp (datum->syntax
  3277. filename
  3278. (let ((t (%search-load-path fn)))
  3279. (if t
  3280. t
  3281. (syntax-violation
  3282. 'include-from-path
  3283. "file not found in path"
  3284. x
  3285. filename))))))
  3286. (let ((fn tmp))
  3287. (list '#(syntax-object include ((top)) (hygiene guile)) fn)))))
  3288. tmp)
  3289. (syntax-violation
  3290. #f
  3291. "source expression failed to match any pattern"
  3292. tmp-1)))))))
  3293. (define unquote
  3294. (make-syntax-transformer
  3295. 'unquote
  3296. 'macro
  3297. (lambda (x)
  3298. (syntax-violation
  3299. 'unquote
  3300. "expression not valid outside of quasiquote"
  3301. x))))
  3302. (define unquote-splicing
  3303. (make-syntax-transformer
  3304. 'unquote-splicing
  3305. 'macro
  3306. (lambda (x)
  3307. (syntax-violation
  3308. 'unquote-splicing
  3309. "expression not valid outside of quasiquote"
  3310. x))))
  3311. (define make-variable-transformer
  3312. (lambda (proc)
  3313. (if (procedure? proc)
  3314. (let ((trans (lambda (x) (proc x))))
  3315. (set-procedure-property! trans 'variable-transformer #t)
  3316. trans)
  3317. (error "variable transformer not a procedure" proc))))
  3318. (define identifier-syntax
  3319. (make-syntax-transformer
  3320. 'identifier-syntax
  3321. 'macro
  3322. (lambda (xx)
  3323. (let ((tmp-1 xx))
  3324. (let ((tmp ($sc-dispatch tmp-1 '(_ any))))
  3325. (if tmp
  3326. (apply (lambda (e)
  3327. (list '#(syntax-object lambda ((top)) (hygiene guile))
  3328. '(#(syntax-object x ((top)) (hygiene guile)))
  3329. '#((#(syntax-object macro-type ((top)) (hygiene guile))
  3330. .
  3331. #(syntax-object
  3332. identifier-syntax
  3333. ((top)
  3334. #(ribcage
  3335. #(identifier-syntax)
  3336. #((top))
  3337. #(((hygiene guile)
  3338. .
  3339. #(syntax-object identifier-syntax ((top)) (hygiene guile))))))
  3340. (hygiene guile))))
  3341. (list '#(syntax-object syntax-case ((top)) (hygiene guile))
  3342. '#(syntax-object x ((top)) (hygiene guile))
  3343. '()
  3344. (list '#(syntax-object id ((top)) (hygiene guile))
  3345. '(#(syntax-object identifier? ((top)) (hygiene guile))
  3346. (#(syntax-object syntax ((top)) (hygiene guile))
  3347. #(syntax-object id ((top)) (hygiene guile))))
  3348. (list '#(syntax-object syntax ((top)) (hygiene guile)) e))
  3349. (list '(#(syntax-object _ ((top)) (hygiene guile))
  3350. #(syntax-object x ((top)) (hygiene guile))
  3351. #(syntax-object ... ((top)) (hygiene guile)))
  3352. (list '#(syntax-object syntax ((top)) (hygiene guile))
  3353. (cons e
  3354. '(#(syntax-object x ((top)) (hygiene guile))
  3355. #(syntax-object ... ((top)) (hygiene guile)))))))))
  3356. tmp)
  3357. (let ((tmp ($sc-dispatch
  3358. tmp-1
  3359. '(_ (any any)
  3360. ((#(free-id #(syntax-object set! ((top)) (hygiene guile))) any any)
  3361. any)))))
  3362. (if (if tmp
  3363. (apply (lambda (id exp1 var val exp2)
  3364. (if (identifier? id) (identifier? var) #f))
  3365. tmp)
  3366. #f)
  3367. (apply (lambda (id exp1 var val exp2)
  3368. (list '#(syntax-object make-variable-transformer ((top)) (hygiene guile))
  3369. (list '#(syntax-object lambda ((top)) (hygiene guile))
  3370. '(#(syntax-object x ((top)) (hygiene guile)))
  3371. '#((#(syntax-object macro-type ((top)) (hygiene guile))
  3372. .
  3373. #(syntax-object variable-transformer ((top)) (hygiene guile))))
  3374. (list '#(syntax-object syntax-case ((top)) (hygiene guile))
  3375. '#(syntax-object x ((top)) (hygiene guile))
  3376. '(#(syntax-object set! ((top)) (hygiene guile)))
  3377. (list (list '#(syntax-object set! ((top)) (hygiene guile)) var val)
  3378. (list '#(syntax-object syntax ((top)) (hygiene guile)) exp2))
  3379. (list (cons id
  3380. '(#(syntax-object x ((top)) (hygiene guile))
  3381. #(syntax-object ... ((top)) (hygiene guile))))
  3382. (list '#(syntax-object syntax ((top)) (hygiene guile))
  3383. (cons exp1
  3384. '(#(syntax-object x ((top)) (hygiene guile))
  3385. #(syntax-object ... ((top)) (hygiene guile))))))
  3386. (list id
  3387. (list '#(syntax-object identifier? ((top)) (hygiene guile))
  3388. (list '#(syntax-object syntax ((top)) (hygiene guile)) id))
  3389. (list '#(syntax-object syntax ((top)) (hygiene guile)) exp1))))))
  3390. tmp)
  3391. (syntax-violation
  3392. #f
  3393. "source expression failed to match any pattern"
  3394. tmp-1)))))))))
  3395. (define define*
  3396. (make-syntax-transformer
  3397. 'define*
  3398. 'macro
  3399. (lambda (x)
  3400. (let ((tmp-1 x))
  3401. (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any . each-any))))
  3402. (if tmp
  3403. (apply (lambda (id args b0 b1)
  3404. (list '#(syntax-object define ((top)) (hygiene guile))
  3405. id
  3406. (cons '#(syntax-object lambda* ((top)) (hygiene guile))
  3407. (cons args (cons b0 b1)))))
  3408. tmp)
  3409. (let ((tmp ($sc-dispatch tmp-1 '(_ any any))))
  3410. (if (if tmp (apply (lambda (id val) (identifier? id)) tmp) #f)
  3411. (apply (lambda (id val)
  3412. (list '#(syntax-object define ((top)) (hygiene guile)) id val))
  3413. tmp)
  3414. (syntax-violation
  3415. #f
  3416. "source expression failed to match any pattern"
  3417. tmp-1)))))))))