psyntax-pp.scm 178 KB

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