magic.scm 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. (define (digit->number d)
  2. (case d
  3. ((#\0) 0)
  4. ((#\1) 1)
  5. ((#\2) 2)
  6. ((#\3) 3)
  7. ((#\4) 4)
  8. ((#\5) 5)
  9. ((#\6) 6)
  10. ((#\7) 7)
  11. ((#\8) 8)
  12. ((#\9) 9)
  13. (else 44)))
  14. (define (string->number s)
  15. (let ((l (string-length s)))
  16. (let loop ((n 0) (i 0))
  17. (if (= i l)
  18. n
  19. (let ((digit (string-ref s i)))
  20. (loop (+ (* 10 n) (digit->number digit)) (+ i 1)))))))
  21. (define (list->string chrs)
  22. (let ((l (length chrs)))
  23. (let ((s (make-string l #\?)))
  24. (let loop ((i 0) (chrs chrs))
  25. (if (null? chrs)
  26. s
  27. (begin
  28. (string-set! s i (car chrs))
  29. (loop (+ i 1) (cdr chrs))))))))
  30. (define (make-collector) (vector '() #f))
  31. (define (collect! c x)
  32. (let ((l (cons x '())))
  33. (cond ((vector-ref c 1)
  34. => (lambda (tail)
  35. (set-cdr! tail l)
  36. (vector-set! c 1 l)))
  37. (else (vector-set! c 0 l)
  38. (vector-set! c 1 l)))))
  39. (define (find-tail l)
  40. (if (null? l)
  41. (error 'find-tail 'no-tail l)
  42. (if (null? (cdr l))
  43. l
  44. (find-tail (cdr l)))))
  45. (define (collect-append! c l)
  46. ;; input list must be ours to mutate
  47. (if (null? l)
  48. #t
  49. (cond ((vector-ref c 1) =>
  50. (lambda (tail)
  51. (set-cdr! tail l)
  52. (vector-set! c 1 (find-tail l))))
  53. (else
  54. (vector-set! c 0 l)
  55. (vector-set! c 1 (find-tail l))))))
  56. (define (collector-get c)
  57. (vector-ref c 0))
  58. (define (make-token type metadata s)
  59. (cons type (cons s (cons metadata '()))))
  60. (define (token? s) (and (pair? s) (eq? 'token (car s))))
  61. (define token-type car)
  62. (define token-metadata caddr)
  63. (define token-data cadr)
  64. (define (atomic-token? token)
  65. (member (token-type token)
  66. '(symbol number boolean character string)))
  67. (define (mark-token? token)
  68. (member (token-type token)
  69. '(quote unquote quasiquote)))
  70. (define (dot-token? token)
  71. (equal? (token-type token) 'dot))
  72. (define (open-token? token)
  73. (equal? (token-type token) 'open))
  74. (define (close-token? token)
  75. (equal? (token-type token) 'close))
  76. ;; token-builders that the generated code uses
  77. (define (whitespace-token metadata tok)
  78. #f)
  79. (define (symbol-token metadata tok)
  80. (make-token 'symbol metadata (string->symbol tok)))
  81. (define (number-token metadata tok)
  82. (make-token 'number metadata (string->number tok)))
  83. (define (boolean-token metadata tok)
  84. (cond
  85. ((equal? (string-ref tok 1) #\t) (make-token 'boolean metadata #t))
  86. ((equal? (string-ref tok 1) #\f) (make-token 'boolean metadata #f))
  87. (else (error "invalid boolean token" (make-token metadata tok)))))
  88. (define (character-token metadata tok)
  89. (if (= 3 (string-length tok))
  90. (make-token 'character metadata (string-ref tok 2))
  91. (if (string=? tok "#\\newline")
  92. (make-token 'character metadata #\newline)
  93. (if (string=? tok "#\\space")
  94. (make-token 'character metadata #\space)
  95. (if (string=? tok "#\\tab")
  96. (make-token 'character metadata #\tab)
  97. (error 'character-token "weird" tok))))))
  98. (define (string-token metadata tok)
  99. (make-token 'string metadata
  100. (list->string (let ((l (string-length tok)))
  101. (let loop ((i 1))
  102. (if (= i (- l 1))
  103. '()
  104. (let ((c (string-ref tok i)))
  105. (if (eq? c #\\)
  106. (cons (string-ref tok (+ i 1)) (loop (+ i 2)))
  107. (cons c (loop (+ i 1)))))))))))
  108. (define (quote-mark-token metadata tok)
  109. (make-token 'quote metadata #f))
  110. (define (unquote-mark-token metadata tok)
  111. (make-token 'unquote metadata #f))
  112. (define (quasiquote-mark-token metadata tok)
  113. (make-token 'quasiquote metadata #f))
  114. (define (dot-token metadata tok)
  115. (make-token 'dot metadata #f))
  116. (define (open-token metadata tok)
  117. (make-token 'open metadata #f))
  118. (define (close-token metadata tok)
  119. (make-token 'close metadata #f))
  120. ;; execute the state machine
  121. ;(include "t/boot/tokenizer.gen.scm")
  122. (define (tell) 0)
  123. (define (tokenize)
  124. (let ((collect (make-collector)))
  125. (let loop ((state start-state) (t '()))
  126. (cond ((eof-object? (peek-char))
  127. (collector-get collect))
  128. ((step state (peek-char)) =>
  129. (lambda (state)
  130. (loop state (cons (read-char) t))))
  131. ((accepting? state) =>
  132. (lambda (make-token)
  133. (let ((tok (make-token (tell) (list->string (reverse t)))))
  134. (when tok (collect! collect tok))
  135. (loop start-state '()))))
  136. (else (error 'tokenize "no parsable token"))))))
  137. (define start-state 0)
  138. (define (accepting? state)
  139. (case state
  140. ((14) whitespace-token)
  141. ((15) symbol-token)
  142. ((12) close-token)
  143. ((13) open-token)
  144. ((10) quasiquote-mark-token)
  145. ((11) dot-token)
  146. ((8) unquote-mark-token)
  147. ((9) quote-mark-token)
  148. ((6) symbol-token)
  149. ((7) number-token)
  150. ((4) boolean-token)
  151. ((5) string-token)
  152. ((2) number-token)
  153. ((3) number-token)
  154. ((1) character-token)
  155. (else #f)))
  156. (define (step state symbol)
  157. (case state
  158. ((2) (case symbol ((#\1) 2) ((#\0) 2) (else #f)))
  159. ((3)
  160. (case symbol
  161. ((#\F) 3)
  162. ((#\E) 3)
  163. ((#\D) 3)
  164. ((#\C) 3)
  165. ((#\B) 3)
  166. ((#\A) 3)
  167. ((#\9) 3)
  168. ((#\8) 3)
  169. ((#\7) 3)
  170. ((#\6) 3)
  171. ((#\5) 3)
  172. ((#\4) 3)
  173. ((#\3) 3)
  174. ((#\2) 3)
  175. ((#\1) 3)
  176. ((#\0) 3)
  177. (else #f)))
  178. ((1)
  179. (case symbol
  180. ((#\~) 1)
  181. ((#\}) 1)
  182. ((#\{) 1)
  183. ((#\_) 1)
  184. ((#\^) 1)
  185. ((#\]) 1)
  186. ((#\\) 1)
  187. ((#\[) 1)
  188. ((#\@) 1)
  189. ((#\?) 1)
  190. ((#\>) 1)
  191. ((#\=) 1)
  192. ((#\<) 1)
  193. ((#\:) 1)
  194. ((#\/) 1)
  195. ((#\-) 1)
  196. ((#\+) 1)
  197. ((#\*) 1)
  198. ((#\&) 1)
  199. ((#\%) 1)
  200. ((#\$) 1)
  201. ((#\!) 1)
  202. ((#\Z) 1)
  203. ((#\Y) 1)
  204. ((#\X) 1)
  205. ((#\W) 1)
  206. ((#\V) 1)
  207. ((#\U) 1)
  208. ((#\T) 1)
  209. ((#\S) 1)
  210. ((#\R) 1)
  211. ((#\Q) 1)
  212. ((#\P) 1)
  213. ((#\O) 1)
  214. ((#\N) 1)
  215. ((#\M) 1)
  216. ((#\L) 1)
  217. ((#\K) 1)
  218. ((#\J) 1)
  219. ((#\I) 1)
  220. ((#\H) 1)
  221. ((#\G) 1)
  222. ((#\F) 1)
  223. ((#\E) 1)
  224. ((#\D) 1)
  225. ((#\C) 1)
  226. ((#\B) 1)
  227. ((#\A) 1)
  228. ((#\z) 1)
  229. ((#\y) 1)
  230. ((#\x) 1)
  231. ((#\w) 1)
  232. ((#\v) 1)
  233. ((#\u) 1)
  234. ((#\t) 1)
  235. ((#\s) 1)
  236. ((#\r) 1)
  237. ((#\q) 1)
  238. ((#\p) 1)
  239. ((#\o) 1)
  240. ((#\n) 1)
  241. ((#\m) 1)
  242. ((#\l) 1)
  243. ((#\k) 1)
  244. ((#\j) 1)
  245. ((#\i) 1)
  246. ((#\h) 1)
  247. ((#\g) 1)
  248. ((#\f) 1)
  249. ((#\e) 1)
  250. ((#\d) 1)
  251. ((#\c) 1)
  252. ((#\b) 1)
  253. ((#\a) 1)
  254. ((#\9) 1)
  255. ((#\8) 1)
  256. ((#\7) 1)
  257. ((#\6) 1)
  258. ((#\5) 1)
  259. ((#\4) 1)
  260. ((#\3) 1)
  261. ((#\2) 1)
  262. ((#\1) 1)
  263. ((#\0) 1)
  264. (else #f)))
  265. ((16)
  266. (case symbol
  267. ((#\~) 1)
  268. ((#\}) 1)
  269. ((#\{) 1)
  270. ((#\|) 1)
  271. ((#\_) 1)
  272. ((#\^) 1)
  273. ((#\]) 1)
  274. ((#\\) 1)
  275. ((#\[) 1)
  276. ((#\@) 1)
  277. ((#\?) 1)
  278. ((#\>) 1)
  279. ((#\=) 1)
  280. ((#\<) 1)
  281. ((#\:) 1)
  282. ((#\/) 1)
  283. ((#\-) 1)
  284. ((#\+) 1)
  285. ((#\*) 1)
  286. ((#\&) 1)
  287. ((#\%) 1)
  288. ((#\$) 1)
  289. ((#\!) 1)
  290. ((#\)) 1)
  291. ((#\() 1)
  292. ((#\") 1)
  293. ((#\#) 1)
  294. ((#\`) 1)
  295. ((#\;) 1)
  296. ((#\.) 1)
  297. ((#\,) 1)
  298. ((#\') 1)
  299. ((#\Z) 1)
  300. ((#\Y) 1)
  301. ((#\X) 1)
  302. ((#\W) 1)
  303. ((#\V) 1)
  304. ((#\U) 1)
  305. ((#\T) 1)
  306. ((#\S) 1)
  307. ((#\R) 1)
  308. ((#\Q) 1)
  309. ((#\P) 1)
  310. ((#\O) 1)
  311. ((#\N) 1)
  312. ((#\M) 1)
  313. ((#\L) 1)
  314. ((#\K) 1)
  315. ((#\J) 1)
  316. ((#\I) 1)
  317. ((#\H) 1)
  318. ((#\G) 1)
  319. ((#\F) 1)
  320. ((#\E) 1)
  321. ((#\D) 1)
  322. ((#\C) 1)
  323. ((#\B) 1)
  324. ((#\A) 1)
  325. ((#\z) 1)
  326. ((#\y) 1)
  327. ((#\x) 1)
  328. ((#\w) 1)
  329. ((#\v) 1)
  330. ((#\u) 1)
  331. ((#\t) 1)
  332. ((#\s) 1)
  333. ((#\r) 1)
  334. ((#\q) 1)
  335. ((#\p) 1)
  336. ((#\o) 1)
  337. ((#\n) 1)
  338. ((#\m) 1)
  339. ((#\l) 1)
  340. ((#\k) 1)
  341. ((#\j) 1)
  342. ((#\i) 1)
  343. ((#\h) 1)
  344. ((#\g) 1)
  345. ((#\f) 1)
  346. ((#\e) 1)
  347. ((#\d) 1)
  348. ((#\c) 1)
  349. ((#\b) 1)
  350. ((#\a) 1)
  351. ((#\9) 1)
  352. ((#\8) 1)
  353. ((#\7) 1)
  354. ((#\6) 1)
  355. ((#\5) 1)
  356. ((#\4) 1)
  357. ((#\3) 1)
  358. ((#\2) 1)
  359. ((#\1) 1)
  360. ((#\0) 1)
  361. (else #f)))
  362. ((17)
  363. (case symbol
  364. ((#\F) 3)
  365. ((#\E) 3)
  366. ((#\D) 3)
  367. ((#\C) 3)
  368. ((#\B) 3)
  369. ((#\A) 3)
  370. ((#\9) 3)
  371. ((#\8) 3)
  372. ((#\7) 3)
  373. ((#\6) 3)
  374. ((#\5) 3)
  375. ((#\4) 3)
  376. ((#\3) 3)
  377. ((#\2) 3)
  378. ((#\1) 3)
  379. ((#\0) 3)
  380. (else #f)))
  381. ((4) (case symbol (else #f)))
  382. ((18) (case symbol ((#\1) 2) ((#\0) 2) (else #f)))
  383. ((19) (case symbol ((#\\) 20) ((#\") 20) (else #f)))
  384. ((5) (case symbol (else #f)))
  385. ((14)
  386. (case symbol
  387. ((#\newline) 14)
  388. ((#\tab) 14)
  389. ((#\space) 14)
  390. ((#\;) 22)
  391. (else #f)))
  392. ((15)
  393. (case symbol
  394. ((#\~) 6)
  395. ((#\}) 6)
  396. ((#\{) 6)
  397. ((#\_) 6)
  398. ((#\^) 6)
  399. ((#\]) 6)
  400. ((#\\) 6)
  401. ((#\[) 6)
  402. ((#\@) 6)
  403. ((#\?) 6)
  404. ((#\>) 6)
  405. ((#\=) 6)
  406. ((#\<) 6)
  407. ((#\:) 6)
  408. ((#\/) 6)
  409. ((#\-) 6)
  410. ((#\+) 6)
  411. ((#\*) 6)
  412. ((#\&) 6)
  413. ((#\%) 6)
  414. ((#\$) 6)
  415. ((#\!) 6)
  416. ((#\Z) 6)
  417. ((#\Y) 6)
  418. ((#\X) 6)
  419. ((#\W) 6)
  420. ((#\V) 6)
  421. ((#\U) 6)
  422. ((#\T) 6)
  423. ((#\S) 6)
  424. ((#\R) 6)
  425. ((#\Q) 6)
  426. ((#\P) 6)
  427. ((#\O) 6)
  428. ((#\N) 6)
  429. ((#\M) 6)
  430. ((#\L) 6)
  431. ((#\K) 6)
  432. ((#\J) 6)
  433. ((#\I) 6)
  434. ((#\H) 6)
  435. ((#\G) 6)
  436. ((#\F) 6)
  437. ((#\E) 6)
  438. ((#\D) 6)
  439. ((#\C) 6)
  440. ((#\B) 6)
  441. ((#\A) 6)
  442. ((#\z) 6)
  443. ((#\y) 6)
  444. ((#\x) 6)
  445. ((#\w) 6)
  446. ((#\v) 6)
  447. ((#\u) 6)
  448. ((#\t) 6)
  449. ((#\s) 6)
  450. ((#\r) 6)
  451. ((#\q) 6)
  452. ((#\p) 6)
  453. ((#\o) 6)
  454. ((#\n) 6)
  455. ((#\m) 6)
  456. ((#\l) 6)
  457. ((#\k) 6)
  458. ((#\j) 6)
  459. ((#\i) 6)
  460. ((#\h) 6)
  461. ((#\g) 6)
  462. ((#\f) 6)
  463. ((#\e) 6)
  464. ((#\d) 6)
  465. ((#\c) 6)
  466. ((#\b) 6)
  467. ((#\a) 6)
  468. ((#\9) 7)
  469. ((#\8) 7)
  470. ((#\7) 7)
  471. ((#\6) 7)
  472. ((#\5) 7)
  473. ((#\4) 7)
  474. ((#\3) 7)
  475. ((#\2) 7)
  476. ((#\1) 7)
  477. ((#\0) 7)
  478. (else #f)))
  479. ((12) (case symbol (else #f)))
  480. ((13) (case symbol (else #f)))
  481. ((20)
  482. (case symbol
  483. ((#\newline) 20)
  484. ((#\tab) 20)
  485. ((#\space) 20)
  486. ((#\~) 20)
  487. ((#\}) 20)
  488. ((#\{) 20)
  489. ((#\_) 20)
  490. ((#\^) 20)
  491. ((#\]) 20)
  492. ((#\\) 19)
  493. ((#\[) 20)
  494. ((#\@) 20)
  495. ((#\?) 20)
  496. ((#\>) 20)
  497. ((#\=) 20)
  498. ((#\<) 20)
  499. ((#\:) 20)
  500. ((#\/) 20)
  501. ((#\-) 20)
  502. ((#\+) 20)
  503. ((#\*) 20)
  504. ((#\&) 20)
  505. ((#\%) 20)
  506. ((#\$) 20)
  507. ((#\!) 20)
  508. ((#\)) 20)
  509. ((#\() 20)
  510. ((#\") 5)
  511. ((#\#) 20)
  512. ((#\`) 20)
  513. ((#\;) 20)
  514. ((#\.) 20)
  515. ((#\,) 20)
  516. ((#\') 20)
  517. ((#\Z) 20)
  518. ((#\Y) 20)
  519. ((#\X) 20)
  520. ((#\W) 20)
  521. ((#\V) 20)
  522. ((#\U) 20)
  523. ((#\T) 20)
  524. ((#\S) 20)
  525. ((#\R) 20)
  526. ((#\Q) 20)
  527. ((#\P) 20)
  528. ((#\O) 20)
  529. ((#\N) 20)
  530. ((#\M) 20)
  531. ((#\L) 20)
  532. ((#\K) 20)
  533. ((#\J) 20)
  534. ((#\I) 20)
  535. ((#\H) 20)
  536. ((#\G) 20)
  537. ((#\F) 20)
  538. ((#\E) 20)
  539. ((#\D) 20)
  540. ((#\C) 20)
  541. ((#\B) 20)
  542. ((#\A) 20)
  543. ((#\z) 20)
  544. ((#\y) 20)
  545. ((#\x) 20)
  546. ((#\w) 20)
  547. ((#\v) 20)
  548. ((#\u) 20)
  549. ((#\t) 20)
  550. ((#\s) 20)
  551. ((#\r) 20)
  552. ((#\q) 20)
  553. ((#\p) 20)
  554. ((#\o) 20)
  555. ((#\n) 20)
  556. ((#\m) 20)
  557. ((#\l) 20)
  558. ((#\k) 20)
  559. ((#\j) 20)
  560. ((#\i) 20)
  561. ((#\h) 20)
  562. ((#\g) 20)
  563. ((#\f) 20)
  564. ((#\e) 20)
  565. ((#\d) 20)
  566. ((#\c) 20)
  567. ((#\b) 20)
  568. ((#\a) 20)
  569. ((#\9) 20)
  570. ((#\8) 20)
  571. ((#\7) 20)
  572. ((#\6) 20)
  573. ((#\5) 20)
  574. ((#\4) 20)
  575. ((#\3) 20)
  576. ((#\2) 20)
  577. ((#\1) 20)
  578. ((#\0) 20)
  579. (else #f)))
  580. ((21)
  581. (case symbol
  582. ((#\\) 16)
  583. ((#\x) 17)
  584. ((#\t) 4)
  585. ((#\f) 4)
  586. ((#\b) 18)
  587. (else #f)))
  588. ((10) (case symbol (else #f)))
  589. ((22)
  590. (case symbol
  591. ((#\newline) 14)
  592. ((#\tab) 22)
  593. ((#\space) 22)
  594. ((#\|) 22)
  595. ((#\~) 22)
  596. ((#\}) 22)
  597. ((#\{) 22)
  598. ((#\_) 22)
  599. ((#\^) 22)
  600. ((#\]) 22)
  601. ((#\\) 22)
  602. ((#\[) 22)
  603. ((#\@) 22)
  604. ((#\?) 22)
  605. ((#\>) 22)
  606. ((#\=) 22)
  607. ((#\<) 22)
  608. ((#\:) 22)
  609. ((#\/) 22)
  610. ((#\-) 22)
  611. ((#\+) 22)
  612. ((#\*) 22)
  613. ((#\&) 22)
  614. ((#\%) 22)
  615. ((#\$) 22)
  616. ((#\!) 22)
  617. ((#\)) 22)
  618. ((#\() 22)
  619. ((#\") 22)
  620. ((#\#) 22)
  621. ((#\`) 22)
  622. ((#\;) 22)
  623. ((#\.) 22)
  624. ((#\,) 22)
  625. ((#\') 22)
  626. ((#\Z) 22)
  627. ((#\Y) 22)
  628. ((#\X) 22)
  629. ((#\W) 22)
  630. ((#\V) 22)
  631. ((#\U) 22)
  632. ((#\T) 22)
  633. ((#\S) 22)
  634. ((#\R) 22)
  635. ((#\Q) 22)
  636. ((#\P) 22)
  637. ((#\O) 22)
  638. ((#\N) 22)
  639. ((#\M) 22)
  640. ((#\L) 22)
  641. ((#\K) 22)
  642. ((#\J) 22)
  643. ((#\I) 22)
  644. ((#\H) 22)
  645. ((#\G) 22)
  646. ((#\F) 22)
  647. ((#\E) 22)
  648. ((#\D) 22)
  649. ((#\C) 22)
  650. ((#\B) 22)
  651. ((#\A) 22)
  652. ((#\z) 22)
  653. ((#\y) 22)
  654. ((#\x) 22)
  655. ((#\w) 22)
  656. ((#\v) 22)
  657. ((#\u) 22)
  658. ((#\t) 22)
  659. ((#\s) 22)
  660. ((#\r) 22)
  661. ((#\q) 22)
  662. ((#\p) 22)
  663. ((#\o) 22)
  664. ((#\n) 22)
  665. ((#\m) 22)
  666. ((#\l) 22)
  667. ((#\k) 22)
  668. ((#\j) 22)
  669. ((#\i) 22)
  670. ((#\h) 22)
  671. ((#\g) 22)
  672. ((#\f) 22)
  673. ((#\e) 22)
  674. ((#\d) 22)
  675. ((#\c) 22)
  676. ((#\b) 22)
  677. ((#\a) 22)
  678. ((#\9) 22)
  679. ((#\8) 22)
  680. ((#\7) 22)
  681. ((#\6) 22)
  682. ((#\5) 22)
  683. ((#\4) 22)
  684. ((#\3) 22)
  685. ((#\2) 22)
  686. ((#\1) 22)
  687. ((#\0) 22)
  688. (else #f)))
  689. ((11) (case symbol (else #f)))
  690. ((8) (case symbol (else #f)))
  691. ((9) (case symbol (else #f)))
  692. ((6)
  693. (case symbol
  694. ((#\~) 6)
  695. ((#\}) 6)
  696. ((#\{) 6)
  697. ((#\_) 6)
  698. ((#\^) 6)
  699. ((#\]) 6)
  700. ((#\\) 6)
  701. ((#\[) 6)
  702. ((#\@) 6)
  703. ((#\?) 6)
  704. ((#\>) 6)
  705. ((#\=) 6)
  706. ((#\<) 6)
  707. ((#\:) 6)
  708. ((#\/) 6)
  709. ((#\-) 6)
  710. ((#\+) 6)
  711. ((#\*) 6)
  712. ((#\&) 6)
  713. ((#\%) 6)
  714. ((#\$) 6)
  715. ((#\!) 6)
  716. ((#\Z) 6)
  717. ((#\Y) 6)
  718. ((#\X) 6)
  719. ((#\W) 6)
  720. ((#\V) 6)
  721. ((#\U) 6)
  722. ((#\T) 6)
  723. ((#\S) 6)
  724. ((#\R) 6)
  725. ((#\Q) 6)
  726. ((#\P) 6)
  727. ((#\O) 6)
  728. ((#\N) 6)
  729. ((#\M) 6)
  730. ((#\L) 6)
  731. ((#\K) 6)
  732. ((#\J) 6)
  733. ((#\I) 6)
  734. ((#\H) 6)
  735. ((#\G) 6)
  736. ((#\F) 6)
  737. ((#\E) 6)
  738. ((#\D) 6)
  739. ((#\C) 6)
  740. ((#\B) 6)
  741. ((#\A) 6)
  742. ((#\z) 6)
  743. ((#\y) 6)
  744. ((#\x) 6)
  745. ((#\w) 6)
  746. ((#\v) 6)
  747. ((#\u) 6)
  748. ((#\t) 6)
  749. ((#\s) 6)
  750. ((#\r) 6)
  751. ((#\q) 6)
  752. ((#\p) 6)
  753. ((#\o) 6)
  754. ((#\n) 6)
  755. ((#\m) 6)
  756. ((#\l) 6)
  757. ((#\k) 6)
  758. ((#\j) 6)
  759. ((#\i) 6)
  760. ((#\h) 6)
  761. ((#\g) 6)
  762. ((#\f) 6)
  763. ((#\e) 6)
  764. ((#\d) 6)
  765. ((#\c) 6)
  766. ((#\b) 6)
  767. ((#\a) 6)
  768. ((#\9) 6)
  769. ((#\8) 6)
  770. ((#\7) 6)
  771. ((#\6) 6)
  772. ((#\5) 6)
  773. ((#\4) 6)
  774. ((#\3) 6)
  775. ((#\2) 6)
  776. ((#\1) 6)
  777. ((#\0) 6)
  778. (else #f)))
  779. ((7)
  780. (case symbol
  781. ((#\9) 7)
  782. ((#\8) 7)
  783. ((#\7) 7)
  784. ((#\6) 7)
  785. ((#\5) 7)
  786. ((#\4) 7)
  787. ((#\3) 7)
  788. ((#\2) 7)
  789. ((#\1) 7)
  790. ((#\0) 7)
  791. (else #f)))
  792. ((0)
  793. (case symbol
  794. ((#\newline) 14)
  795. ((#\tab) 14)
  796. ((#\space) 14)
  797. ((#\~) 6)
  798. ((#\}) 6)
  799. ((#\{) 6)
  800. ((#\_) 6)
  801. ((#\^) 6)
  802. ((#\]) 6)
  803. ((#\\) 6)
  804. ((#\[) 6)
  805. ((#\@) 6)
  806. ((#\?) 6)
  807. ((#\>) 6)
  808. ((#\=) 6)
  809. ((#\<) 6)
  810. ((#\:) 6)
  811. ((#\/) 6)
  812. ((#\-) 15)
  813. ((#\+) 6)
  814. ((#\*) 6)
  815. ((#\&) 6)
  816. ((#\%) 6)
  817. ((#\$) 6)
  818. ((#\!) 6)
  819. ((#\)) 12)
  820. ((#\() 13)
  821. ((#\") 20)
  822. ((#\#) 21)
  823. ((#\`) 10)
  824. ((#\;) 22)
  825. ((#\.) 11)
  826. ((#\,) 8)
  827. ((#\') 9)
  828. ((#\Z) 6)
  829. ((#\Y) 6)
  830. ((#\X) 6)
  831. ((#\W) 6)
  832. ((#\V) 6)
  833. ((#\U) 6)
  834. ((#\T) 6)
  835. ((#\S) 6)
  836. ((#\R) 6)
  837. ((#\Q) 6)
  838. ((#\P) 6)
  839. ((#\O) 6)
  840. ((#\N) 6)
  841. ((#\M) 6)
  842. ((#\L) 6)
  843. ((#\K) 6)
  844. ((#\J) 6)
  845. ((#\I) 6)
  846. ((#\H) 6)
  847. ((#\G) 6)
  848. ((#\F) 6)
  849. ((#\E) 6)
  850. ((#\D) 6)
  851. ((#\C) 6)
  852. ((#\B) 6)
  853. ((#\A) 6)
  854. ((#\z) 6)
  855. ((#\y) 6)
  856. ((#\x) 6)
  857. ((#\w) 6)
  858. ((#\v) 6)
  859. ((#\u) 6)
  860. ((#\t) 6)
  861. ((#\s) 6)
  862. ((#\r) 6)
  863. ((#\q) 6)
  864. ((#\p) 6)
  865. ((#\o) 6)
  866. ((#\n) 6)
  867. ((#\m) 6)
  868. ((#\l) 6)
  869. ((#\k) 6)
  870. ((#\j) 6)
  871. ((#\i) 6)
  872. ((#\h) 6)
  873. ((#\g) 6)
  874. ((#\f) 6)
  875. ((#\e) 6)
  876. ((#\d) 6)
  877. ((#\c) 6)
  878. ((#\b) 6)
  879. ((#\a) 6)
  880. ((#\9) 7)
  881. ((#\8) 7)
  882. ((#\7) 7)
  883. ((#\6) 7)
  884. ((#\5) 7)
  885. ((#\4) 7)
  886. ((#\3) 7)
  887. ((#\2) 7)
  888. ((#\1) 7)
  889. ((#\0) 7)
  890. (else #f)))))
  891. (define parser:strip #t)
  892. (define (strip-token t)
  893. (if (mark-token? t)
  894. (strip-token (make-token 'symbol
  895. (token-metadata t)
  896. (token-type t)))
  897. (if parser:strip
  898. (if (atomic-token? t)
  899. (token-data t)
  900. (token-type t))
  901. t)))
  902. (define (read-s* ts)
  903. (let loop ((co (make-collector)) (ts ts))
  904. (if (null? ts)
  905. (collector-get co)
  906. (read-s ts
  907. (lambda (s ts)
  908. (collect! co s)
  909. (loop co ts))))))
  910. (define (read-s ts k)
  911. (if (null? ts)
  912. (error 'read-s "read-s")
  913. (let ((t (car ts)) (ts (cdr ts)))
  914. (cond ((atomic-token? t)
  915. (k (strip-token t) ts))
  916. ((mark-token? t)
  917. (read-s ts
  918. (lambda (s ts)
  919. (k (cons (strip-token t) (cons s '())) ts))))
  920. ((open-token? t)
  921. (read-s+/close ts k))
  922. ((close-token? t)
  923. (error 'read-s "too many closed brackets" t))
  924. (else
  925. (error 'read-s "unknown token" t))))))
  926. (define (read-s+/close ts k)
  927. (if (null? ts)
  928. (error 'read-s+/close "read-s+/close")
  929. (let ((t (car ts)) (ts (cdr ts)))
  930. (cond ((close-token? t)
  931. (k '() ts))
  932. (else
  933. (read-s (cons t ts)
  934. (lambda (s ts)
  935. (let ((co (make-collector)))
  936. (collect! co s)
  937. (read-s+ co ts k)))))))))
  938. (define (read-s+ co ts k)
  939. (if (null? ts)
  940. (error 'read-s+ "read-s+")
  941. (let ((t (car ts)) (ts (cdr ts)))
  942. (cond ((close-token? t)
  943. (k (collector-get co) ts))
  944. ((dot-token? t)
  945. (read-s ts
  946. (lambda (s ts)
  947. (unless (close-token? (car ts))
  948. (error 'read-s+ "only one form after a dot token"))
  949. (k (append (collector-get co) s) (cdr ts)))))
  950. (else
  951. (read-s (cons t ts)
  952. (lambda (s ts)
  953. (collect! co s)
  954. (read-s+ co ts k))))))))
  955. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  956. (define (box val) (make-vector 1 val))
  957. (define (unbox b) (vector-ref b 0))
  958. (define (set-box! b v) (vector-set! b 0 v))
  959. (define put-hash-table! hash-table-set!)
  960. (define (get-hash-table h k) (hash-table-ref h k #f))
  961. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  962. ;; match macro
  963. (define (moo-match e)
  964. (let ((exp (cadr e))
  965. (clauses (cddr e))
  966. (tmp (gensym "tmp")))
  967. `(let ((,tmp ,exp))
  968. ,(moo-match-aux tmp clauses))))
  969. (define (quote? p)
  970. (and (pair? p) (eq? (car p) 'quote)))
  971. ; takes: expression to match on, pattern, body, place, failure continuation
  972. ; returns: code that returns #t if pattern matches and #f if not, and an assoc list of
  973. ; bindings
  974. (define (subpattern-match s p)
  975. (begin ;(print `("in subpatterN" ,s ,p))
  976. (cond
  977. ((null? p)
  978. (cons `((null? ,s))
  979. '()))
  980. ((number? p)
  981. (cons `((number? ,s) (= ,s ,p))
  982. '()))
  983. ((symbol? p)
  984. (cons `(#t)
  985. `((,p ,s))))
  986. ((quote? p)
  987. (cons `((equal? ',(cadr p) ,s))
  988. '()))
  989. ((pair? p)
  990. (let ((l (subpattern-match `(car ,s) (car p)))
  991. (r (subpattern-match `(cdr ,s) (cdr p))))
  992. (cons (cons `(pair? ,s) (append (car l) (car r)))
  993. (append (cdr l) (cdr r)))))
  994. (else
  995. (begin
  996. (print "undefined subpattern")
  997. (print p))))))
  998. (define (try t pat body fk)
  999. (let ((m (subpattern-match t pat)))
  1000. `(if (and . ,(car m))
  1001. ,(if (not (null? (cdr m)))
  1002. `(let ,(cdr m)
  1003. ,body)
  1004. body)
  1005. (,fk))))
  1006. (define (moo-match-aux t clauses)
  1007. (if (null? clauses)
  1008. `(error 'match "match fail")
  1009. (let ((pat (caar clauses))
  1010. (body `(begin . ,(cdar clauses)))
  1011. (fk (gensym "fk")))
  1012. `(let ((,fk (lambda ()
  1013. ,(moo-match-aux t (cdr clauses)))))
  1014. ,(try t pat body fk)))))
  1015. (defmacro match moo-match)
  1016. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1017. ;; scan
  1018. (define (pass-scan tbl)
  1019. (transform-assoc tbl (globals toplevel)
  1020. (let ((c (make-collector)))
  1021. (letrec ((process
  1022. (lambda (top)
  1023. ;(print `(processing ,top))
  1024. (match top
  1025. (('define (nm . args) . body)
  1026. (process `(define ,nm (lambda ,args . ,body))))
  1027. (('define nm body)
  1028. (let ((idx (- (extend! globals nm) 1)))
  1029. (collect! c `(define ,nm ,idx ,body))))
  1030. (('define nm . body)
  1031. (let ((idx (- (extend! globals nm) 1)))
  1032. (collect! c `(define ,nm ,idx (begin ,body)))))
  1033. (('defmacro nm body)
  1034. (let ((gen (gensym nm))
  1035. (idx (- (extend! globals nm) 1)))
  1036. (collect! c `(defmacro ,nm ,gen ,idx ,body))))
  1037. ;(('include filename)
  1038. ; (for-each process (read-file filename)))
  1039. (else
  1040. (collect! c top))))))
  1041. (for-each process toplevel))
  1042. `((globals . ,globals)
  1043. (toplevel . ,(collector-get c))))))
  1044. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1045. ;; hoist
  1046. (define (quote-builder exp)
  1047. (cond ((or (symbol? exp) (null? exp) (datum? exp))
  1048. `(datum ,exp))
  1049. ((pair? exp)
  1050. `(builtin cons ,(quote-builder (car exp)) ,(quote-builder (cdr exp))))))
  1051. (define (mk-var sort)
  1052. (lambda (i)
  1053. `(var ,sort ,i)))
  1054. (define hoist-code (box #f))
  1055. (define (label! obj)
  1056. (let ((lbl (gensym 'clo)))
  1057. (collect! (unbox hoist-code) `(code ,lbl ,obj))
  1058. lbl))
  1059. (define dbg:form (box #f))
  1060. (define (my-extend! b x)
  1061. ;(print `(extend! ,b ,x))
  1062. (let ((res (extend! b x)))
  1063. ;(print `(done-extending ,b ,res))
  1064. res))
  1065. (define (hoist-var var scope)
  1066. (match-assoc scope (tmp loc env glo caps)
  1067. (cond ((reverse-index var tmp) => (mk-var 'tmp))
  1068. ((index var loc) => (mk-var 'loc))
  1069. ((member var env)
  1070. (cond ((index var (unbox caps)) => (mk-var 'env))
  1071. (else ((mk-var 'env) (- (my-extend! caps var) 1)))))
  1072. ((index var glo) => (mk-var 'glo))
  1073. (else
  1074. (print `(while processing ,(unbox dbg:form)))
  1075. (print `(unbound ,var in scope ,scope))
  1076. (error 'hoist-variable "unbound variable?" var)))))
  1077. (define (hoist exp scope)
  1078. (cond ((datum? exp) `(datum ,exp))
  1079. ((quote? exp) (quote-builder (cadr exp)))
  1080. ((variable? exp) (hoist-var exp scope))
  1081. ((if? exp)
  1082. (let ((t (cadr exp))
  1083. (c (caddr exp))
  1084. (a (cadddr exp)))
  1085. `(if ,(hoist t scope)
  1086. ,(hoist c scope)
  1087. ,(hoist a scope))))
  1088. ((begin? exp)
  1089. (let ((sts (cdr exp)))
  1090. `(begin . ,(mapply hoist sts scope))))
  1091. ((lambda? exp)
  1092. (let ((bind (cadr exp))
  1093. (body (cddr exp)))
  1094. (match-assoc scope (tmp loc env glo caps)
  1095. (let ((caps^ (box '())))
  1096. (let ((body^ (hoist `(begin . ,body)
  1097. `((tmp . ())
  1098. (loc . ,bind)
  1099. (env . ,(append tmp (append loc env)))
  1100. (glo . ,glo)
  1101. (caps . ,caps^)))))
  1102. (let ((label (label! body^)))
  1103. `(closure ,label ,(mapply hoist (unbox caps^) scope))))))))
  1104. ((let? exp)
  1105. (match-assoc scope (tmp loc env glo caps)
  1106. (let ((tmp^ (box (copy-list tmp))))
  1107. (let ((bindings (map* (lambda (entry)
  1108. (let ((exp (hoist (cadr entry) scope))
  1109. (var (- (my-extend! tmp^ (car entry)) 1)))
  1110. (list var exp)))
  1111. (cadr exp))))
  1112. `(let ,bindings ,(hoist (caddr exp)
  1113. `((tmp . ,(unbox tmp^))
  1114. (loc . ,loc)
  1115. (env . ,env)
  1116. (glo . ,glo)
  1117. (caps . ,caps))))))))
  1118. ((letrec? exp)
  1119. (match-assoc scope (tmp loc env glo caps)
  1120. (let ((tmp^ (box (copy-list tmp))))
  1121. (for-each (lambda (entry)
  1122. (my-extend! tmp^ (car entry)))
  1123. (cadr exp))
  1124. (let ((scope^ `((tmp . ,(unbox tmp^))
  1125. (loc . ,loc)
  1126. (env . ,env)
  1127. (glo . ,glo)
  1128. (caps . ,caps))))
  1129. (let ((bindings (map* (lambda (entry)
  1130. (let ((exp (hoist (cadr entry) scope^))
  1131. (var (index (car entry) (unbox tmp^))))
  1132. (list var exp)))
  1133. (cadr exp))))
  1134. `(letrec ,bindings ,(hoist (caddr exp) scope^)))))))
  1135. ((builtin-app? exp)
  1136. `(builtin ,(cadr exp) . ,(mapply hoist (cddr exp) scope)))
  1137. ((app? exp)
  1138. `(app . ,(mapply hoist exp scope)))
  1139. (else
  1140. (error 'hoist "unknown exp" exp))))
  1141. (define (hoist^ exp glo)
  1142. (set-box! dbg:form exp)
  1143. (hoist exp `((tmp . ())
  1144. (loc . ())
  1145. (env . ())
  1146. (glo . ,glo)
  1147. (caps . ,(box '())))))
  1148. (define (pass-hoist tbl)
  1149. (set-box! hoist-code (make-collector))
  1150. (transform-assoc tbl (globals toplevel)
  1151. ;(set! code (make-collector))
  1152. (let ((toplevel (map* (lambda (top)
  1153. (match top
  1154. (('define nm id exp)
  1155. `(define ,nm ,id ,(hoist^ exp (unbox globals))))
  1156. (else
  1157. (hoist^ top (unbox globals)))))
  1158. toplevel)))
  1159. `((globals . ,globals)
  1160. (toplevel . ,toplevel)
  1161. (closures . ,(collector-get (unbox hoist-code)))))))
  1162. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1163. ;; denest
  1164. (define (let-form? p)
  1165. (and (pair? p) (eq? (car p) 'let-form)))
  1166. (define (make-let-form bindings body)
  1167. (list 'let-form bindings body))
  1168. (define let-form-bindings cadr)
  1169. (define let-form-body caddr)
  1170. ;; LET OBJECTS
  1171. ;;
  1172. (define (let-form->let l)
  1173. (if (let-form? l)
  1174. `(let ,(sequence->list (let-form-bindings l)) ,(let-form-body l))
  1175. l))
  1176. (define (let-bindings exp)
  1177. (match exp
  1178. (('let-form bindings body) bindings)
  1179. ;(`(let ,bindings ,body) `(cat ,bindings))
  1180. (else 'nil)
  1181. ))
  1182. (define (let-body exp)
  1183. (match exp
  1184. (('let-form bindings body) body)
  1185. ;(`(let ,bindings ,body) body)
  1186. (else exp)))
  1187. ;; TMPALLOC
  1188. ;;
  1189. (define (temporary! name b)
  1190. (cond ((index name (unbox b)) => (lambda (i) i))
  1191. (else (- (extend! b name) 1))))
  1192. (define (process-bindings bindings b)
  1193. (map* (lambda (binding)
  1194. (match binding
  1195. ((tmp exp)
  1196. (let ((i (temporary! tmp b)))
  1197. `(,i ,(process-body exp b))))
  1198. (else (error 'process-bindings ""))))
  1199. bindings))
  1200. (define (process-body body b)
  1201. (match body
  1202. (('datum d) body)
  1203. (('var 'tmp nm)
  1204. (let ((i (index nm (unbox b))))
  1205. (unless i (error 'process-body "index error" nm))
  1206. `(var tmp ,i)))
  1207. (('var sort nm) body)
  1208. (('closure lbl size)
  1209. body)
  1210. (('set-closure! clo-tmp i val)
  1211. `(set-closure! ,(process-body clo-tmp b) ,i ,(process-body val b)))
  1212. (('if t c a)
  1213. (let ((t^ (process-body t b))
  1214. (c^ (tmpalloc^ c b))
  1215. (a^ (tmpalloc^ a b)))
  1216. `(if ,t^ ,c^ ,a^)))
  1217. (('builtin nm . args)
  1218. `(builtin ,nm . ,(mapply process-body args b)))
  1219. (('app . args)
  1220. `(app . ,(mapply process-body args b)))
  1221. (else
  1222. (error 'process-body "Unhandled expression:" body))))
  1223. (define (tmpalloc^ l b)
  1224. (match l
  1225. (('let bindings body)
  1226. (let ((bindings^ (process-bindings bindings b))
  1227. (body^ (process-body body b)))
  1228. `(let ,(length (unbox b)) ,bindings^ ,body^)))
  1229. (else
  1230. (let ((body (process-body l b)))
  1231. `(let ,(length (unbox b)) () ,body)))))
  1232. (define (tmpalloc l)
  1233. (let ((b (box '())))
  1234. (tmpalloc^ l b)))
  1235. ;; DENEST PASS
  1236. ;;
  1237. (define (denest-binding binding)
  1238. (let ((nm (car binding))
  1239. (exp (cadr binding)))
  1240. (let ((l (denest-aux exp #f)))
  1241. `(join ,(let-bindings l) (elt ,(list nm (let-body l)))))))
  1242. (define (denest-letrec-binding binding b)
  1243. (match (cadr binding)
  1244. (('closure lbl env)
  1245. (if (null? env)
  1246. (denest-binding binding)
  1247. (let ((tmp (car binding))
  1248. (i (box 0)))
  1249. (map* (lambda (elt)
  1250. (extend! b `(elt (#f (set-closure! (var tmp ,tmp) ,(unbox i) ,elt))))
  1251. (set-box! i (+ (unbox i) 1)))
  1252. env)
  1253. `(elt ,(list tmp `(closure ,lbl ,(length env)))))))
  1254. (else (denest-binding binding))))
  1255. (define (denest-aux^ exp)
  1256. (match exp
  1257. (('datum d) exp)
  1258. (('var sort i) exp)
  1259. (('app . args)
  1260. (let ((args^ (mapply denest-aux args #t)))
  1261. (let ((bindings (map let-bindings args^))
  1262. (bodies (map let-body args^)))
  1263. (make-let-form `(cat ,bindings) `(app . ,bodies)))))
  1264. (('builtin nm . args)
  1265. (let ((args^ (mapply denest-aux args #t)))
  1266. (let ((bindings (map let-bindings args^))
  1267. (bodies (map let-body args^)))
  1268. (make-let-form `(cat ,bindings) `(builtin ,nm . ,bodies)))))
  1269. (('closure lbl env)
  1270. (if (null? env)
  1271. `(closure ,lbl 0)
  1272. (let ((tmp (gensym "tmp")))
  1273. (make-let-form
  1274. `(join
  1275. (elt ,(list tmp `(closure ,lbl ,(length env))))
  1276. (cat ,(let ((i (box 0)))
  1277. (map* (lambda (elt)
  1278. (let ((res `(set-closure! (var tmp ,tmp) ,(unbox i) ,elt)))
  1279. (set-box! i (+ (unbox i) 1))
  1280. `(elt ,(list #f res))))
  1281. env))))
  1282. `(var tmp ,tmp)))))
  1283. (('if t c a)
  1284. (let ((l (denest-aux t #f)))
  1285. (make-let-form (let-bindings l)
  1286. `(if ,(let-body l)
  1287. ,(denest c)
  1288. ,(denest a)))))
  1289. (('let bindings body)
  1290. (let ((l (denest-aux body #f)))
  1291. (make-let-form `(join (cat ,(map* denest-binding bindings))
  1292. ,(let-bindings l))
  1293. (let-body l))))
  1294. (('letrec bindings body)
  1295. (let ((b (box '()))
  1296. (l (denest-aux body #f)))
  1297. (let ((creation (mapply denest-letrec-binding bindings b))
  1298. (setup (unbox b)))
  1299. (make-let-form `(join (cat ,creation)
  1300. (join (cat ,setup)
  1301. ,(let-bindings l)))
  1302. (let-body l)))))
  1303. (('begin . stmts)
  1304. (let loop ((stmts stmts) (bindings 'nil))
  1305. (cond ((null? stmts)
  1306. (error 'denest-aux^ "empty begin"))
  1307. (else
  1308. (let ((l (denest-aux (car stmts) #f)))
  1309. (let ((binds (let-bindings l))
  1310. (body (let-body l)))
  1311. (if (null? (cdr stmts))
  1312. (make-let-form `(join ,bindings ,binds)
  1313. body)
  1314. (loop (cdr stmts)
  1315. `(join ,bindings (join ,binds (elt (#f ,body))))))))))))))
  1316. (define (simple? exp)
  1317. (match exp
  1318. (('datum v) #t)
  1319. (('var sort nm) #t)
  1320. (else #f)))
  1321. (define (denest-aux exp simple)
  1322. ;; simple = #t let bodies must be variables
  1323. ;; simple = #f allow complex let bodies
  1324. ;;
  1325. (let ((l (denest-aux^ exp)))
  1326. (if (and simple (not (simple? (let-body l))))
  1327. (let ((t (gensym "tmp")))
  1328. (make-let-form `(join ,(let-bindings l) (elt ,(list t (let-body l))))
  1329. `(var tmp ,t)))
  1330. l)))
  1331. (define (denest exp)
  1332. (let-form->let (denest-aux exp #f)))
  1333. (define (pass-denest tbl)
  1334. (transform-assoc tbl (toplevel closures)
  1335. `((toplevel . ,(map* (lambda (t)
  1336. (match t
  1337. (('define nm id body)
  1338. `(define ,nm ,id ,(tmpalloc (denest body))))
  1339. (else
  1340. (tmpalloc (denest t)))
  1341. (else (error 'pass-denest "" t))))
  1342. toplevel))
  1343. (closures . ,(map* (lambda (st)
  1344. (match st
  1345. (('code lbl body)
  1346. `(code ,lbl ,(tmpalloc (denest body))))
  1347. (else (error 'pass-denest "" st))))
  1348. closures)))))
  1349. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1350. ;; instructions
  1351. (define emitter (box #f))
  1352. (define (emit! elt) (collect! (unbox emitter) elt))
  1353. (define (instructions^ exp place tail?)
  1354. (match exp
  1355. (('datum d)
  1356. (emit! `(move ,place ,exp)))
  1357. (('var sort nm)
  1358. (emit! `(move ,place ,exp)))
  1359. (('app . args)
  1360. (cond ((equal? place '(reg return))
  1361. (if tail?
  1362. (emit! `(tail-call . ,args))
  1363. (emit! `(call . ,args))))
  1364. (else (emit! `(call . ,args))
  1365. (emit! `(move ,place (reg return))))))
  1366. (('builtin nm . args)
  1367. (emit! `(builtin ,place ,nm . ,args)))
  1368. (('closure lbl size)
  1369. (emit! `(make-closure ,place ,lbl ,size)))
  1370. (('set-closure! clo i elt)
  1371. (emit! `(set-closure! ,clo ,i ,elt)))
  1372. (('if t c a)
  1373. (let ((lbl-tru (gensym 'if-tru))
  1374. (lbl-end (gensym 'if-end)))
  1375. (instructions^ t '(reg return) #f)
  1376. (emit! `(branch ,lbl-tru))
  1377. (instructions a place #f tail?)
  1378. (emit! `(jump ,lbl-end))
  1379. (emit! lbl-tru)
  1380. (instructions c place #f tail?)
  1381. (emit! lbl-end)))
  1382. (('begin . stmts)
  1383. (let loop ((stmts stmts))
  1384. (if (null? (cdr stmts))
  1385. (instructions^ (car stmts) place tail?)
  1386. (begin
  1387. (instructions^ (car stmts) #f #f)
  1388. (loop (cdr stmts))))))
  1389. (else (error 'instructions "" exp))))
  1390. (define (instructions exp place entry? tail?)
  1391. (match exp
  1392. (('let tmps bindings body)
  1393. (when entry?
  1394. (emit! `(alloc-tmps ,tmps)))
  1395. (for-each binding-instructions bindings)
  1396. (instructions^ body place tail?))
  1397. (else (instructions^ exp place tail?))))
  1398. (define (binding-instructions binding)
  1399. (let ((place (car binding))
  1400. (exp (cadr binding)))
  1401. (instructions^ exp `(var tmp ,place) #t)))
  1402. (define (pass-instructions tbl)
  1403. (match-assoc tbl (toplevel closures)
  1404. (let ((start (gensym 'start)))
  1405. (set-box! emitter (make-collector))
  1406. (for-each (lambda (t)
  1407. (match t
  1408. (('define nm id body)
  1409. (instructions body `(var glo ,id) #t #f))
  1410. (else
  1411. (instructions t '(reg return) #t #f))
  1412. (else (error 'pass-instructions "" t))))
  1413. toplevel)
  1414. (emit! `(halt))
  1415. (for-each (lambda (st)
  1416. (match st
  1417. (('code lbl body)
  1418. (emit! lbl)
  1419. (instructions body '(reg return) #t #t)
  1420. (emit! '(return)))
  1421. (else (error 'pass-denest "" st))))
  1422. closures)
  1423. `(,start . ,(collector-get (unbox emitter))))))
  1424. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1425. ;; execute!
  1426. ;; INSTRUCTION CODE ::=
  1427. ;; <label>
  1428. ;; (halt)
  1429. ;; (builtin <bltn-name> <args>*)
  1430. ;; (move <place> <var>)
  1431. ;; (alloc-tmps <num>)
  1432. ;; (make-closure <place> <lbl> <size>)
  1433. ;; (set-closure! <var> <index> <arg>)
  1434. ;; (call <clo> <args>*)
  1435. ;; (tail-call <clo> <args>*)
  1436. ;; (return)
  1437. ;; (branch <lbl>)
  1438. ;; (jump <lbl>)
  1439. (define bytecode (box (make-vector 100000 #f)))
  1440. (define bytecode-size (box 1))
  1441. (define index-table (make-hash-table))
  1442. (define execute:globals (make-vector 2000 #f))
  1443. (define (append-bytecode! insts)
  1444. (vector-set! (unbox bytecode) 0 `(halt))
  1445. (vector-overlay! (unbox bytecode) (unbox bytecode-size) insts)
  1446. (let ((i (box (unbox bytecode-size))))
  1447. (vector-for-each
  1448. (lambda (elt)
  1449. (when (symbol? elt)
  1450. (put-hash-table! index-table elt (unbox i)))
  1451. (set-box! i (+ (unbox i) 1)))
  1452. insts))
  1453. (set-box! bytecode-size (+ (unbox bytecode-size) (vector-length insts))))
  1454. (defmacro magic-assoc
  1455. (lambda (exp)
  1456. `(list . ,(map (lambda (nm)
  1457. `(cons ',nm ,nm))
  1458. (cdr exp)))))
  1459. (define string->list #f)
  1460. ;(define symbol->string #f)
  1461. (define builtins (magic-assoc
  1462. exit
  1463. gensym
  1464. display newline print
  1465. eq? equal?
  1466. cons car cdr set-car! set-cdr!
  1467. null? pair?
  1468. number? boolean? string? char? symbol?
  1469. + - * = < > <= >=
  1470. quotient modulo
  1471. list
  1472. box
  1473. unbox
  1474. set-box!
  1475. vector-ref
  1476. vector-set!
  1477. make-vector
  1478. vector-length
  1479. ;string->list
  1480. ;symbol->string
  1481. ;list->string
  1482. make-string
  1483. string-set!
  1484. string-ref
  1485. string->symbol
  1486. string-length
  1487. string=?
  1488. eof-object?
  1489. read-char
  1490. peek-char
  1491. vector?
  1492. symbol->string
  1493. char->integer
  1494. ))
  1495. (define ip (box #f))
  1496. (define (inc! b)
  1497. (set-box! b (+ 1 (unbox b))))
  1498. (define return-register (box #f))
  1499. (define stack (box '()))
  1500. (define (push! b e)
  1501. (set-box! b (cons e (unbox b))))
  1502. (define (pop! b)
  1503. (let ((s (unbox b)))
  1504. (set-box! b (cdr s))
  1505. (car s)))
  1506. (define (make-stack-frame tmp loc env return-address) (vector tmp loc env return-address))
  1507. (define (stack-frame-tmp s) (vector-ref s 0))
  1508. (define (stack-frame-loc s) (vector-ref s 1))
  1509. (define (stack-frame-env s) (vector-ref s 2))
  1510. (define (stack-frame-return-address s) (vector-ref s 3))
  1511. (define (set-stack-frame-tmp! s v)
  1512. (vector-set! s 0 v))
  1513. (define (make-closure lbl env) (vector 'closure lbl env))
  1514. (define (closure-label clo) (vector-ref clo 1))
  1515. (define (closure-env clo) (vector-ref clo 2))
  1516. (define (evaluate place)
  1517. (case (car place)
  1518. ((datum) (cadr place))
  1519. ((reg)
  1520. (case (cadr place)
  1521. ((return) (unbox return-register))
  1522. (else (error 'evaluate "reg" place))))
  1523. ((var)
  1524. (case (cadr place)
  1525. ((glo) (vector-ref execute:globals (caddr place)))
  1526. ((tmp) (vector-ref (stack-frame-tmp (car (unbox stack))) (caddr place)))
  1527. ((loc) (vector-ref (stack-frame-loc (car (unbox stack))) (caddr place)))
  1528. ((env) (vector-ref (stack-frame-env (car (unbox stack))) (caddr place)))
  1529. (else (error 'evaluate "var" place))))
  1530. (else
  1531. (error 'evaluate "" place))))
  1532. (define (move! place value)
  1533. (case (car place)
  1534. ((reg)
  1535. (case (cadr place)
  1536. ((return) (set-box! return-register value))
  1537. (else (error 'move! "reg" place))))
  1538. ((var)
  1539. (case (cadr place)
  1540. ((glo) (vector-set! execute:globals (caddr place) value))
  1541. ((tmp) (vector-set! (stack-frame-tmp (car (unbox stack))) (caddr place) value))
  1542. ((loc) (vector-set! (stack-frame-loc (car (unbox stack))) (caddr place) value))
  1543. ((env) (vector-set! (stack-frame-env (car (unbox stack))) (caddr place) value))
  1544. (else (error 'move! "var" place))))
  1545. (else
  1546. (error 'move! "" place))))
  1547. (define (go)
  1548. (let ((inst (vector-ref (unbox bytecode) (unbox ip))))
  1549. ;(print inst)
  1550. (inc! ip)
  1551. (if (symbol? inst)
  1552. (go)
  1553. (case (car inst)
  1554. ;; HALT
  1555. ((halt)
  1556. #t)
  1557. ;; BUILTIN <place> <name> <args>*
  1558. ((builtin)
  1559. (let ((place (cadr inst))
  1560. (name (caddr inst))
  1561. (args (map evaluate (cdddr inst))))
  1562. (cond ((assoc name builtins)
  1563. => (lambda (entry)
  1564. (case (length args)
  1565. ((0) (move! place ((cdr entry))))
  1566. ((1) (move! place ((cdr entry) (car args))))
  1567. ((2) (move! place ((cdr entry) (car args) (cadr args))))
  1568. ((3) (move! place ((cdr entry) (car args) (cadr args) (caddr args))))
  1569. ((4) (move! place ((cdr entry) (car args) (cadr args) (caddr args) (cadddr args))))
  1570. (else (error 'go "builtin with too many args" name)))
  1571. (go)))
  1572. (else (error 'go "unknown builtin" name)))))
  1573. ;; MOVE <place> <value>
  1574. ((move)
  1575. (let ((place (cadr inst))
  1576. (value (evaluate (caddr inst))))
  1577. (move! place value)
  1578. (go)))
  1579. ;; ALLOC-TMPS <num>
  1580. ((alloc-tmps)
  1581. (let ((value (cadr inst)))
  1582. (set-stack-frame-tmp! (car (unbox stack)) (make-vector value #f))
  1583. (go)))
  1584. ;; MAKE-CLOSURE <place> <label> <num>
  1585. ((make-closure)
  1586. (let ((place (cadr inst))
  1587. (label (get-hash-table index-table (caddr inst) #f))
  1588. (env (make-vector (cadddr inst) #f)))
  1589. (unless label
  1590. (error 'go "make-closure: missing label" label))
  1591. (move! place (make-closure label env))
  1592. (go)))
  1593. ;; SET-CLOSURE! <closure> <index> <value>
  1594. ((set-closure!)
  1595. (let ((clo (evaluate (cadr inst)))
  1596. (i (caddr inst))
  1597. (val (evaluate (cadddr inst))))
  1598. (vector-set! (closure-env clo) i val)
  1599. (go)))
  1600. ;; CALL <closure> <args>*
  1601. ((call)
  1602. (let ((clo (evaluate (cadr inst)))
  1603. (args (list->vector (map evaluate (cddr inst)))))
  1604. (push! stack (make-stack-frame #f args (closure-env clo) (unbox ip)))
  1605. (set-box! ip (closure-label clo))
  1606. (go)))
  1607. ;; TAIL-CALL <closure> <args>*
  1608. ((tail-call)
  1609. (let ((clo (evaluate (cadr inst)))
  1610. (args (list->vector (map evaluate (cddr inst)))))
  1611. (let ((ret (stack-frame-return-address (car (unbox stack)))))
  1612. (set-car! (unbox stack) (make-stack-frame #f args (closure-env clo) ret))
  1613. (set-box! ip (closure-label clo))
  1614. (go))))
  1615. ;; RETURN
  1616. ((return)
  1617. (when (null? (unbox stack)) (error 'go "returning too many times"))
  1618. (let ((frame (pop! stack)))
  1619. (set-box! ip (stack-frame-return-address frame))
  1620. (go)))
  1621. ;; BRANCH <label>
  1622. ((branch)
  1623. (let ((lbl (get-hash-table index-table (cadr inst) #f)))
  1624. (when (unbox return-register) (set-box! ip lbl))
  1625. (go)))
  1626. ;; JUMP <label>
  1627. ((jump)
  1628. (let ((lbl (get-hash-table index-table (cadr inst) #f)))
  1629. (set-box! ip lbl)
  1630. (go)))
  1631. (else
  1632. (error 'go "unknown instruction" inst))))))
  1633. (define (execute start)
  1634. (set-box! ip (get-hash-table index-table start #f))
  1635. (unless (unbox ip)
  1636. (error 'execute "no such label" start))
  1637. (set-box! return-register #f)
  1638. (set-box! stack (list (make-stack-frame #f #f #f 0)))
  1639. (go)
  1640. (unbox return-register))
  1641. (define (execute-call clo-name args)
  1642. (set-box! return-register #f)
  1643. (let ((clo (vector-ref execute:globals clo-name)))
  1644. (set-box! ip (closure-label clo))
  1645. (set-box! stack (list (make-stack-frame #f (list->vector args) (closure-env clo) 0))))
  1646. (go)
  1647. (unbox return-register))
  1648. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1649. ;; pass-bytes
  1650. (define bytes:bytecode-size (box 2))
  1651. (define bytes:index-table (make-hash-table))
  1652. (define (pass-bytes insts)
  1653. ;; first pass resolves all the labels
  1654. (let ((i (box (unbox bytes:bytecode-size))))
  1655. (for-each
  1656. (lambda (elt)
  1657. ;(print `(pass-bytes elt ,elt))
  1658. (when (symbol? elt)
  1659. (put-hash-table! bytes:index-table elt (unbox i)))
  1660. (set-box! i (+ (unbox i) (emit-bytes #t elt))))
  1661. insts)
  1662. (set-box! bytes:bytecode-size (+ (unbox i) 1)))
  1663. ;; +1 for the newline
  1664. ;; second pass is printing it out
  1665. (for-each (lambda (i) (emit-bytes #f i)) insts)
  1666. (newline))
  1667. (define builtins-list
  1668. '(exit
  1669. gensym
  1670. display newline print
  1671. eq? equal?
  1672. cons car cdr set-car! set-cdr!
  1673. null? pair?
  1674. number? boolean? string? char? symbol?
  1675. ;;19
  1676. + - * = < > <= >=
  1677. ;;27
  1678. quotient modulo
  1679. list
  1680. box
  1681. unbox
  1682. set-box!
  1683. vector-ref
  1684. vector-set!
  1685. make-vector ;; 35
  1686. vector-length
  1687. make-string
  1688. string-set!
  1689. string-ref
  1690. string->symbol
  1691. string-length
  1692. string=?
  1693. ;string->list
  1694. ;symbol->string
  1695. ;list->string
  1696. eof-object?
  1697. read-char
  1698. peek-char
  1699. vector?
  1700. symbol->string
  1701. char->integer
  1702. ))
  1703. (define (+/3 a b c) (+ a (+ b c)))
  1704. (define (+/4 a b c d) (+ a (+ b (+ c d))))
  1705. (define (+/5 a b c d e) (+ a (+ b (+ c (+ d e)))))
  1706. (define (sum/for-each func count? args)
  1707. (let loop ((sum 0) (args args))
  1708. (if (null? args)
  1709. sum
  1710. (loop (+ sum (func count? (car args))) (cdr args)))))
  1711. (define (puts count? s)
  1712. (if (symbol? s)
  1713. (puts count? (builtin symbol->string s))
  1714. (if count?
  1715. (string-length s)
  1716. (begin (display s) 0))))
  1717. (define (hx/char c)
  1718. (if (= c 0)
  1719. #\0
  1720. (if (= c 1)
  1721. #\1
  1722. (if (= c 2)
  1723. #\2
  1724. (if (= c 3)
  1725. #\3
  1726. (if (= c 4)
  1727. #\4
  1728. (if (= c 5)
  1729. #\5
  1730. (if (= c 6)
  1731. #\6
  1732. (if (= c 7)
  1733. #\7
  1734. (if (= c 8)
  1735. #\8
  1736. (if (= c 9)
  1737. #\9
  1738. (if (= c 10)
  1739. #\A
  1740. (if (= c 11)
  1741. #\B
  1742. (if (= c 12)
  1743. #\C
  1744. (if (= c 13)
  1745. #\D
  1746. (if (= c 14)
  1747. #\E
  1748. (if (= c 15)
  1749. #\F
  1750. (print '(builtin exit 2)))))))))))))))))))
  1751. (define (hx num width)
  1752. (if (= width 0)
  1753. (if (= num 0)
  1754. #t
  1755. (print '(builtin exit)))
  1756. (begin (hx (quotient num 16) (- width 1))
  1757. (display (hx/char (modulo num 16))))))
  1758. (define (emit-num/4 count? i)
  1759. (unless count? (hx i 4))
  1760. 4)
  1761. (define (emit-num/6 count? i)
  1762. (unless count? (hx i 6))
  1763. 6)
  1764. (define (emit-num/16 count? i)
  1765. (unless count? (hx i 16))
  1766. 16)
  1767. (define (emit-num/2 count? i)
  1768. (unless count? (hx i 2))
  1769. 2)
  1770. (defmacro plus
  1771. (lambda (exp)
  1772. (let ((v (gensym "v")))
  1773. `(let ((,v ,(cadr exp)))
  1774. (+ ,v ,(caddr exp))))))
  1775. (define (emit-place count? place)
  1776. (match place
  1777. (('reg return) (puts count? "r"))
  1778. (('var 'glo i) (plus (puts count? "g") (emit-num/4 count? i)))
  1779. (('var 'tmp i) (plus (puts count? "t") (emit-num/2 count? i)))
  1780. (('var 'loc i) (plus (puts count? "l") (emit-num/2 count? i)))
  1781. (('var 'env i) (plus (puts count? "e") (emit-num/2 count? i)))
  1782. (else (error 'emit-place "" place))))
  1783. (define (write-char count? ch)
  1784. (unless count?
  1785. (builtin display #\#)
  1786. (builtin display #\\)
  1787. (display ch))
  1788. 3)
  1789. (define (put-dot count?)
  1790. (unless count?
  1791. (display #\.))
  1792. 1)
  1793. (define (emit-value count? val)
  1794. (match val
  1795. (('datum d)
  1796. (cond ((null? d)
  1797. (puts count? "N"))
  1798. ((boolean? d)
  1799. (puts count? (if d "#t" "#f")))
  1800. ((char? d)
  1801. (if (equal? d #\newline)
  1802. (puts count? "#N")
  1803. (if (equal? d #\tab)
  1804. (puts count? "#T")
  1805. (write-char count? d))))
  1806. ((number? d)
  1807. (if (< d 65535)
  1808. (plus (puts count? "x")
  1809. (emit-num/4 count? d))
  1810. (plus (puts count? "X")
  1811. (emit-num/16 count? d))))
  1812. ((symbol? d)
  1813. (let ((t1 (puts count? "S"))
  1814. (t2 (puts count? d)) ;; todo escape .
  1815. (t3 (put-dot count?)))
  1816. (+ t1 (+ t2 t3))))
  1817. ((string? d)
  1818. (unless count?
  1819. (builtin display #\"))
  1820. (let ((t2 (puts count? d)) ;; todo escape .
  1821. (t3 (put-dot count?)))
  1822. (+ 1 (+ t2 t3))))
  1823. (else (error 'emit-value "todo" val))))
  1824. (else (emit-place count? val))))
  1825. (define (emit-bytes count? inst)
  1826. ;(print inst)
  1827. (match inst
  1828. (('halt)
  1829. (puts count? "H"))
  1830. (('builtin place nm . args)
  1831. (let ((t1 (puts count? "B"))
  1832. (t2 (emit-place count? place))
  1833. (t3 (emit-num/2 count? (index nm builtins-list)))
  1834. (t4 (sum/for-each emit-value count? args))
  1835. (t5 (put-dot count?)))
  1836. (+ t1 (+ t2 (+ t3 (+ t4 t5))))))
  1837. (('move place value)
  1838. (let ((t1 (puts count? "M"))
  1839. (t2 (emit-place count? place))
  1840. (t3 (emit-value count? value)))
  1841. (+ t1 (+ t2 t3))))
  1842. (('alloc-tmps num)
  1843. (let ((t1 (puts count? "A"))
  1844. (t2 (emit-num/2 count? num)))
  1845. (+ t1 t2)))
  1846. (('make-closure place lbl size)
  1847. (let ((t1 (puts count? "c"))
  1848. (t2 (emit-place count? place))
  1849. (t3 (emit-num/6 count? (get-hash-table bytes:index-table lbl #f)))
  1850. (t4 (emit-num/2 count? size)))
  1851. (+ (+ t1 t2) (+ t3 t4))))
  1852. (('set-closure! clo idx value)
  1853. (let ((t1 (puts count? "E"))
  1854. (t2 (emit-value count? clo))
  1855. (t3 (emit-num/2 count? idx))
  1856. (t4 (emit-value count? value)))
  1857. (+ (+ (+ t1 t2) t3) t4)))
  1858. (('call clo . args)
  1859. (let ((t1 (puts count? "C"))
  1860. (t2 (emit-value count? clo))
  1861. (t3 (sum/for-each emit-value count? args))
  1862. (t4 (put-dot count?)))
  1863. (+ (+ t1 t2) (+ t3 t4))))
  1864. (('tail-call clo . args)
  1865. (let ((t1 (puts count? "T"))
  1866. (t2 (emit-value count? clo))
  1867. (t3 (sum/for-each emit-value count? args))
  1868. (t4 (put-dot count?)))
  1869. (+ t1 (+ t2 (+ t3 t4)))))
  1870. (('return)
  1871. (puts count? "R"))
  1872. (('branch lbl)
  1873. (let ((t1 (puts count? "b"))
  1874. (t2 (emit-num/6 count? (get-hash-table bytes:index-table lbl #f))))
  1875. (+ t1 t2)))
  1876. (('jump lbl)
  1877. (let ((t1 (puts count? "J"))
  1878. (t2 (emit-num/6 count? (get-hash-table bytes:index-table lbl #f))))
  1879. (+ t1 t2)))
  1880. (else
  1881. (unless (symbol? inst)
  1882. (print 'bad-error)
  1883. (print inst)
  1884. (error 'emit-bytes "TODO" inst)
  1885. (puts count? "-"))
  1886. 0)))
  1887. ;;;; pass expand
  1888. (define glo (box #f))
  1889. (define mac (box #f))
  1890. (define (macro? m) (assoc m (unbox mac)))
  1891. (define (interpretdef exp)
  1892. (let ((tbl (box `((globals . ,glo)
  1893. (macros . ,mac)
  1894. (toplevel ,exp)))))
  1895. (set-box! tbl (pass-hoist (unbox tbl)))
  1896. (set-box! tbl (pass-denest (unbox tbl)))
  1897. (set-box! tbl (pass-instructions (unbox tbl)))
  1898. (append-bytecode! (list->vector (unbox tbl)))
  1899. (execute (car (unbox tbl)))))
  1900. (define (interpretmac exp)
  1901. (let ((name (assoc (car exp) (unbox mac))))
  1902. (execute-call (cdr name) (cons exp '()))))
  1903. (define (expandm exp)
  1904. (cond ((datum? exp) exp)
  1905. ((quote? exp) exp)
  1906. ((variable? exp) exp)
  1907. ((if? exp)
  1908. (let ((t (cadr exp))
  1909. (c (caddr exp))
  1910. (a (cadddr exp)))
  1911. `(if ,(expandm t)
  1912. ,(expandm c)
  1913. ,(expandm a))))
  1914. ((begin? exp)
  1915. (let ((sts (cdr exp)))
  1916. (if (null? (cdr sts))
  1917. (expandm (car sts))
  1918. `(begin . ,(mapply expandm sts)))))
  1919. ((lambda? exp)
  1920. (let ((bind (cadr exp))
  1921. (body `(begin . ,(cddr exp))))
  1922. `(lambda ,bind ,(expandm body))))
  1923. ((named-let? exp)
  1924. (let ((name (cadr exp))
  1925. (bindings (caddr exp))
  1926. (body `(begin . ,(cdddr exp))))
  1927. (expandm
  1928. `(letrec ((,name (lambda ,(map car bindings)
  1929. ,body)))
  1930. (,name . ,(map cadr bindings))))))
  1931. ((let? exp)
  1932. `(let ,(map (lambda (bind)
  1933. (list (car bind) (expandm (cadr bind)))) (cadr exp))
  1934. ,(expandm `(begin . ,(cddr exp)))))
  1935. ((letrec? exp)
  1936. `(letrec ,(map (lambda (bind)
  1937. (list (car bind) (expandm (cadr bind)))) (cadr exp))
  1938. ,(expandm `(begin . ,(cddr exp)))))
  1939. ((builtin-app? exp)
  1940. `(builtin ,(cadr exp) . ,(mapply expandm (cddr exp))))
  1941. ((app? exp)
  1942. (cond ((macro? (car exp))
  1943. (expandm (interpretmac exp)))
  1944. (else
  1945. (mapply expandm exp))))
  1946. (else
  1947. (error 'expandm "unknown exp" exp))))
  1948. (define (pass-expand tbl)
  1949. (transform-assoc tbl (globals macros toplevel)
  1950. (let ((c (make-collector)))
  1951. (set-box! glo (unbox globals))
  1952. (set-box! mac (unbox macros))
  1953. (letrec ((process
  1954. (lambda (top)
  1955. (match top
  1956. (('define nm idx body)
  1957. (collect! c `(define ,nm ,idx ,(expandm body))))
  1958. (('defmacro nm gen idx body)
  1959. (begin
  1960. (set-box! mac (cons (cons nm idx) (unbox mac)))
  1961. (interpretdef `(define ,gen ,idx ,(expandm body)))))
  1962. (else
  1963. (collect! c (expandm top)))))))
  1964. (for-each process toplevel))
  1965. (set-box! globals (unbox glo))
  1966. (set-box! macros (unbox mac))
  1967. `((globals . ,globals)
  1968. (macros . ,macros)
  1969. (toplevel . ,(collector-get c))))))
  1970. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1971. (define globals (box '()))
  1972. (define macros (box '()))
  1973. (define (definition? x)
  1974. (and (pair? x)
  1975. (or (eq? 'define (car x))
  1976. (eq? 'defmacro (car x)))))
  1977. (define (program) (read-s* (tokenize))) ;; parlse
  1978. ;; and then i just added on each pass 1 by 1,
  1979. ;; then the VM execute a the end;
  1980. (display "H") (newline)
  1981. (let ((tbl `((globals . ,globals)
  1982. (toplevel . ,(program)))))
  1983. (let ((tbl (pass-scan tbl)))
  1984. (let loop ((exps (cdr (assoc 'toplevel tbl))))
  1985. (unless (null? exps)
  1986. (let ((tbl `((globals . ,globals)
  1987. (macros . ,macros)
  1988. (toplevel ,(car exps))))
  1989. (def? (definition? (car exps))))
  1990. ;(print 'expand)
  1991. ;(print tbl)
  1992. (let ((tbl (pass-expand tbl)))
  1993. ;(print 'hoist)
  1994. ;(print tbl)
  1995. (let ((tbl (pass-hoist tbl)))
  1996. ;(print 'denest)
  1997. ;(print tbl)
  1998. (let ((tbl (pass-denest tbl)))
  1999. ;(print 'instructions)
  2000. ;(print tbl)
  2001. (let ((tbl (pass-instructions tbl)))
  2002. (append-bytecode! (list->vector tbl))
  2003. (when def?
  2004. (execute (car tbl)))
  2005. (pass-bytes tbl)
  2006. (loop (cdr exps)))))))))))
  2007. (display "Q") (newline)
  2008. ; (include "t/boot/try-compiler.scm")