psyntax-pp.scm 181 KB

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