psyntax-pp.scm 167 KB

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