compile-tree-il.scm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. ;;; Guile Emacs Lisp
  2. ;; Copyright (C) 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
  3. ;; This program is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation; either version 3, or (at your option)
  6. ;; any later version.
  7. ;;
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program; see the file COPYING. If not, write to
  15. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. ;; Boston, MA 02111-1307, USA.
  17. ;;; Code:
  18. (define-module (language elisp compile-tree-il)
  19. #:use-module (language elisp bindings)
  20. #:use-module (language elisp runtime)
  21. #:use-module (language tree-il)
  22. #:use-module (system base pmatch)
  23. #:use-module (system base compile)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-8)
  26. #:use-module (srfi srfi-11)
  27. #:use-module (srfi srfi-26)
  28. #:export (compile-tree-il
  29. compile-progn
  30. compile-eval-when-compile
  31. compile-if
  32. compile-defconst
  33. compile-defvar
  34. compile-setq
  35. compile-let
  36. compile-flet
  37. compile-labels
  38. compile-let*
  39. compile-guile-ref
  40. compile-guile-primitive
  41. compile-function
  42. compile-defmacro
  43. compile-defun
  44. #{compile-`}#
  45. compile-quote
  46. compile-%funcall
  47. compile-%set-lexical-binding-mode))
  48. ;;; Certain common parameters (like the bindings data structure or
  49. ;;; compiler options) are not always passed around but accessed using
  50. ;;; fluids to simulate dynamic binding (hey, this is about elisp).
  51. ;;; The bindings data structure to keep track of symbol binding related
  52. ;;; data.
  53. (define bindings-data (make-fluid))
  54. (define lexical-binding (make-fluid))
  55. ;;; Find the source properties of some parsed expression if there are
  56. ;;; any associated with it.
  57. (define (location x)
  58. (and (pair? x)
  59. (let ((props (source-properties x)))
  60. (and (not (null? props))
  61. props))))
  62. ;;; Values to use for Elisp's nil and t.
  63. (define (nil-value loc)
  64. (make-const loc (@ (language elisp runtime) nil-value)))
  65. (define (t-value loc)
  66. (make-const loc (@ (language elisp runtime) t-value)))
  67. ;;; Modules that contain the value and function slot bindings.
  68. (define runtime '(language elisp runtime))
  69. (define value-slot (@ (language elisp runtime) value-slot-module))
  70. (define function-slot (@ (language elisp runtime) function-slot-module))
  71. ;;; The backquoting works the same as quasiquotes in Scheme, but the
  72. ;;; forms are named differently; to make easy adaptions, we define these
  73. ;;; predicates checking for a symbol being the car of an
  74. ;;; unquote/unquote-splicing/backquote form.
  75. (define (unquote? sym)
  76. (and (symbol? sym) (eq? sym '#{,}#)))
  77. (define (unquote-splicing? sym)
  78. (and (symbol? sym) (eq? sym '#{,@}#)))
  79. ;;; Build a call to a primitive procedure nicely.
  80. (define (call-primitive loc sym . args)
  81. (make-primcall loc sym args))
  82. ;;; Error reporting routine for syntax/compilation problems or build
  83. ;;; code for a runtime-error output.
  84. (define (report-error loc . args)
  85. (apply error args))
  86. (define (access-variable loc symbol handle-lexical handle-dynamic)
  87. (cond
  88. ((get-lexical-binding (fluid-ref bindings-data) symbol)
  89. => handle-lexical)
  90. (else
  91. (handle-dynamic))))
  92. (define (reference-variable loc symbol)
  93. (access-variable
  94. loc
  95. symbol
  96. (lambda (lexical)
  97. (make-lexical-ref loc lexical lexical))
  98. (lambda ()
  99. (call-primitive loc
  100. 'fluid-ref
  101. (make-module-ref loc value-slot symbol #t)))))
  102. (define (global? module symbol)
  103. (module-variable module symbol))
  104. (define (ensure-globals! loc names body)
  105. (if (and (every (cut global? (resolve-module value-slot) <>) names)
  106. (every symbol-interned? names))
  107. body
  108. (list->seq
  109. loc
  110. `(,@(map
  111. (lambda (name)
  112. (ensure-fluid! value-slot name)
  113. (make-call loc
  114. (make-module-ref loc runtime 'ensure-fluid! #t)
  115. (list (make-const loc value-slot)
  116. (make-const loc name))))
  117. names)
  118. ,body))))
  119. (define (set-variable! loc symbol value)
  120. (access-variable
  121. loc
  122. symbol
  123. (lambda (lexical)
  124. (make-lexical-set loc lexical lexical value))
  125. (lambda ()
  126. (ensure-globals!
  127. loc
  128. (list symbol)
  129. (call-primitive loc
  130. 'fluid-set!
  131. (make-module-ref loc value-slot symbol #t)
  132. value)))))
  133. (define (access-function loc symbol handle-lexical handle-global)
  134. (cond
  135. ((get-function-binding (fluid-ref bindings-data) symbol)
  136. => handle-lexical)
  137. (else
  138. (handle-global))))
  139. (define (reference-function loc symbol)
  140. (access-function
  141. loc
  142. symbol
  143. (lambda (gensym) (make-lexical-ref loc symbol gensym))
  144. (lambda () (make-module-ref loc function-slot symbol #t))))
  145. (define (set-function! loc symbol value)
  146. (access-function
  147. loc
  148. symbol
  149. (lambda (gensym) (make-lexical-set loc symbol gensym value))
  150. (lambda ()
  151. (make-call
  152. loc
  153. (make-module-ref loc runtime 'set-symbol-function! #t)
  154. (list (make-const loc symbol) value)))))
  155. (define (bind-lexically? sym module decls)
  156. (or (eq? module function-slot)
  157. (let ((decl (assq-ref decls sym)))
  158. (and (equal? module value-slot)
  159. (or
  160. (eq? decl 'lexical)
  161. (and
  162. (fluid-ref lexical-binding)
  163. (not (global? (resolve-module module) sym))))))))
  164. (define (parse-let-binding loc binding)
  165. (pmatch binding
  166. ((unquote var)
  167. (guard (symbol? var))
  168. (cons var #nil))
  169. ((,var)
  170. (guard (symbol? var))
  171. (cons var #nil))
  172. ((,var ,val)
  173. (guard (symbol? var))
  174. (cons var val))
  175. (else
  176. (report-error loc "malformed variable binding" binding))))
  177. (define (parse-flet-binding loc binding)
  178. (pmatch binding
  179. ((,var ,args . ,body)
  180. (guard (symbol? var))
  181. (cons var `(function (lambda ,args ,@body))))
  182. (else
  183. (report-error loc "malformed function binding" binding))))
  184. (define (parse-declaration expr)
  185. (pmatch expr
  186. ((lexical . ,vars)
  187. (map (cut cons <> 'lexical) vars))
  188. (else
  189. '())))
  190. (define (parse-body-1 body lambda?)
  191. (let loop ((lst body)
  192. (decls '())
  193. (intspec #f)
  194. (doc #f))
  195. (pmatch lst
  196. (((declare . ,x) . ,tail)
  197. (loop tail (append-reverse x decls) intspec doc))
  198. (((interactive . ,x) . ,tail)
  199. (guard lambda? (not intspec))
  200. (loop tail decls x doc))
  201. ((,x . ,tail)
  202. (guard lambda? (string? x) (not doc) (not (null? tail)))
  203. (loop tail decls intspec x))
  204. (else
  205. (values (append-map parse-declaration decls)
  206. intspec
  207. doc
  208. lst)))))
  209. (define (parse-lambda-body body)
  210. (parse-body-1 body #t))
  211. (define (parse-body body)
  212. (receive (decls intspec doc body) (parse-body-1 body #f)
  213. (values decls body)))
  214. ;;; Partition the argument list of a lambda expression into required,
  215. ;;; optional and rest arguments.
  216. (define (parse-lambda-list lst)
  217. (define (%match lst null optional rest symbol)
  218. (pmatch lst
  219. (() (null))
  220. ((&optional . ,tail) (optional tail))
  221. ((&rest . ,tail) (rest tail))
  222. ((,arg . ,tail) (guard (symbol? arg)) (symbol arg tail))
  223. (else (fail))))
  224. (define (return rreq ropt rest)
  225. (values #t (reverse rreq) (reverse ropt) rest))
  226. (define (fail)
  227. (values #f #f #f #f))
  228. (define (parse-req lst rreq)
  229. (%match lst
  230. (lambda () (return rreq '() #f))
  231. (lambda (tail) (parse-opt tail rreq '()))
  232. (lambda (tail) (parse-rest tail rreq '()))
  233. (lambda (arg tail) (parse-req tail (cons arg rreq)))))
  234. (define (parse-opt lst rreq ropt)
  235. (%match lst
  236. (lambda () (return rreq ropt #f))
  237. (lambda (tail) (fail))
  238. (lambda (tail) (parse-rest tail rreq ropt))
  239. (lambda (arg tail) (parse-opt tail rreq (cons arg ropt)))))
  240. (define (parse-rest lst rreq ropt)
  241. (%match lst
  242. (lambda () (fail))
  243. (lambda (tail) (fail))
  244. (lambda (tail) (fail))
  245. (lambda (arg tail) (parse-post-rest tail rreq ropt arg))))
  246. (define (parse-post-rest lst rreq ropt rest)
  247. (%match lst
  248. (lambda () (return rreq ropt rest))
  249. (lambda () (fail))
  250. (lambda () (fail))
  251. (lambda (arg tail) (fail))))
  252. (parse-req lst '()))
  253. (define (make-simple-lambda loc meta req opt init rest vars body)
  254. (make-lambda loc
  255. meta
  256. (make-lambda-case #f req opt rest #f init vars body #f)))
  257. (define (make-dynlet src fluids vals body)
  258. (let ((f (map (lambda (x) (gensym "fluid ")) fluids))
  259. (v (map (lambda (x) (gensym "valud ")) vals)))
  260. (make-let src (map (lambda (_) 'fluid) fluids) f fluids
  261. (make-let src (map (lambda (_) 'val) vals) v vals
  262. (let lp ((f f) (v v))
  263. (if (null? f)
  264. body
  265. (make-primcall
  266. src 'with-fluid*
  267. (list (make-lexical-ref #f 'fluid (car f))
  268. (make-lexical-ref #f 'val (car v))
  269. (make-lambda
  270. src '()
  271. (make-lambda-case
  272. src '() #f #f #f '() '()
  273. (lp (cdr f) (cdr v))
  274. #f))))))))))
  275. (define (compile-lambda loc meta args body)
  276. (receive (valid? req-ids opt-ids rest-id)
  277. (parse-lambda-list args)
  278. (if valid?
  279. (let* ((all-ids (append req-ids
  280. opt-ids
  281. (or (and=> rest-id list) '())))
  282. (all-vars (map (lambda (ignore) (gensym)) all-ids)))
  283. (let*-values (((decls intspec doc forms)
  284. (parse-lambda-body body))
  285. ((lexical dynamic)
  286. (partition
  287. (compose (cut bind-lexically? <> value-slot decls)
  288. car)
  289. (map list all-ids all-vars)))
  290. ((lexical-ids lexical-vars) (unzip2 lexical))
  291. ((dynamic-ids dynamic-vars) (unzip2 dynamic)))
  292. (with-dynamic-bindings
  293. (fluid-ref bindings-data)
  294. dynamic-ids
  295. (lambda ()
  296. (with-lexical-bindings
  297. (fluid-ref bindings-data)
  298. lexical-ids
  299. lexical-vars
  300. (lambda ()
  301. (ensure-globals!
  302. loc
  303. dynamic-ids
  304. (let* ((tree-il
  305. (compile-expr
  306. (if rest-id
  307. `(let ((,rest-id (if ,rest-id
  308. ,rest-id
  309. nil)))
  310. ,@forms)
  311. `(progn ,@forms))))
  312. (full-body
  313. (if (null? dynamic)
  314. tree-il
  315. (make-dynlet
  316. loc
  317. (map (cut make-module-ref loc value-slot <> #t)
  318. dynamic-ids)
  319. (map (cut make-lexical-ref loc <> <>)
  320. dynamic-ids
  321. dynamic-vars)
  322. tree-il))))
  323. (make-simple-lambda loc
  324. meta
  325. req-ids
  326. opt-ids
  327. (map (const (nil-value loc))
  328. opt-ids)
  329. rest-id
  330. all-vars
  331. full-body)))))))))
  332. (report-error "invalid function" `(lambda ,args ,@body)))))
  333. ;;; Handle the common part of defconst and defvar, that is, checking for
  334. ;;; a correct doc string and arguments as well as maybe in the future
  335. ;;; handling the docstring somehow.
  336. (define (handle-var-def loc sym doc)
  337. (cond
  338. ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
  339. ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
  340. ((and (not (null? doc)) (not (string? (car doc))))
  341. (report-error loc "expected string as third argument of defvar, got"
  342. (car doc)))
  343. ;; TODO: Handle doc string if present.
  344. (else #t)))
  345. ;;; Handle macro and special operator bindings.
  346. (define (find-operator name type)
  347. (and
  348. (symbol? name)
  349. (module-defined? (resolve-interface function-slot) name)
  350. (let ((op (module-ref (resolve-module function-slot) name)))
  351. (if (and (pair? op) (eq? (car op) type))
  352. (cdr op)
  353. #f))))
  354. ;;; See if a (backquoted) expression contains any unquotes.
  355. (define (contains-unquotes? expr)
  356. (if (pair? expr)
  357. (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
  358. #t
  359. (or (contains-unquotes? (car expr))
  360. (contains-unquotes? (cdr expr))))
  361. #f))
  362. ;;; Process a backquoted expression by building up the needed
  363. ;;; cons/append calls. For splicing, it is assumed that the expression
  364. ;;; spliced in evaluates to a list. The emacs manual does not really
  365. ;;; state either it has to or what to do if it does not, but Scheme
  366. ;;; explicitly forbids it and this seems reasonable also for elisp.
  367. (define (unquote-cell? expr)
  368. (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
  369. (define (unquote-splicing-cell? expr)
  370. (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
  371. (define (process-backquote loc expr)
  372. (if (contains-unquotes? expr)
  373. (if (pair? expr)
  374. (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
  375. (compile-expr (cadr expr))
  376. (let* ((head (car expr))
  377. (processed-tail (process-backquote loc (cdr expr)))
  378. (head-is-list-2 (and (list? head)
  379. (= (length head) 2)))
  380. (head-unquote (and head-is-list-2
  381. (unquote? (car head))))
  382. (head-unquote-splicing (and head-is-list-2
  383. (unquote-splicing?
  384. (car head)))))
  385. (if head-unquote-splicing
  386. (call-primitive loc
  387. 'append
  388. (compile-expr (cadr head))
  389. processed-tail)
  390. (call-primitive loc 'cons
  391. (if head-unquote
  392. (compile-expr (cadr head))
  393. (process-backquote loc head))
  394. processed-tail))))
  395. (report-error loc
  396. "non-pair expression contains unquotes"
  397. expr))
  398. (make-const loc expr)))
  399. ;;; Special operators
  400. (defspecial progn (loc args)
  401. (list->seq loc
  402. (if (null? args)
  403. (list (nil-value loc))
  404. (map compile-expr args))))
  405. (defspecial eval-when-compile (loc args)
  406. (make-const loc (compile `(progn ,@args) #:from 'elisp #:to 'value)))
  407. (defspecial if (loc args)
  408. (pmatch args
  409. ((,cond ,then . ,else)
  410. (make-conditional
  411. loc
  412. (call-primitive loc 'not
  413. (call-primitive loc 'nil? (compile-expr cond)))
  414. (compile-expr then)
  415. (compile-expr `(progn ,@else))))))
  416. (defspecial defconst (loc args)
  417. (pmatch args
  418. ((,sym ,value . ,doc)
  419. (if (handle-var-def loc sym doc)
  420. (make-seq loc
  421. (set-variable! loc sym (compile-expr value))
  422. (make-const loc sym))))))
  423. (defspecial defvar (loc args)
  424. (pmatch args
  425. ((,sym) (make-const loc sym))
  426. ((,sym ,value . ,doc)
  427. (if (handle-var-def loc sym doc)
  428. (make-seq
  429. loc
  430. (make-conditional
  431. loc
  432. (make-conditional
  433. loc
  434. (call-primitive
  435. loc
  436. 'module-bound?
  437. (call-primitive loc
  438. 'resolve-interface
  439. (make-const loc value-slot))
  440. (make-const loc sym))
  441. (call-primitive loc
  442. 'fluid-bound?
  443. (make-module-ref loc value-slot sym #t))
  444. (make-const loc #f))
  445. (make-void loc)
  446. (set-variable! loc sym (compile-expr value)))
  447. (make-const loc sym))))))
  448. (defspecial setq (loc args)
  449. (define (car* x) (if (null? x) '() (car x)))
  450. (define (cdr* x) (if (null? x) '() (cdr x)))
  451. (define (cadr* x) (car* (cdr* x)))
  452. (define (cddr* x) (cdr* (cdr* x)))
  453. (list->seq
  454. loc
  455. (let loop ((args args) (last (nil-value loc)))
  456. (if (null? args)
  457. (list last)
  458. (let ((sym (car args))
  459. (val (compile-expr (cadr* args))))
  460. (if (not (symbol? sym))
  461. (report-error loc "expected symbol in setq")
  462. (cons
  463. (set-variable! loc sym val)
  464. (loop (cddr* args)
  465. (reference-variable loc sym)))))))))
  466. (defspecial let (loc args)
  467. (pmatch args
  468. ((,varlist . ,body)
  469. (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
  470. (receive (decls forms) (parse-body body)
  471. (receive (lexical dynamic)
  472. (partition
  473. (compose (cut bind-lexically? <> value-slot decls)
  474. car)
  475. bindings)
  476. (let ((make-values (lambda (for)
  477. (map (lambda (el) (compile-expr (cdr el)))
  478. for)))
  479. (make-body (lambda () (compile-expr `(progn ,@forms)))))
  480. (ensure-globals!
  481. loc
  482. (map car dynamic)
  483. (if (null? lexical)
  484. (make-dynlet loc
  485. (map (compose (cut make-module-ref
  486. loc
  487. value-slot
  488. <>
  489. #t)
  490. car)
  491. dynamic)
  492. (map (compose compile-expr cdr)
  493. dynamic)
  494. (make-body))
  495. (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
  496. (dynamic-syms (map (lambda (el) (gensym)) dynamic))
  497. (all-syms (append lexical-syms dynamic-syms))
  498. (vals (append (make-values lexical)
  499. (make-values dynamic))))
  500. (make-let loc
  501. all-syms
  502. all-syms
  503. vals
  504. (with-lexical-bindings
  505. (fluid-ref bindings-data)
  506. (map car lexical)
  507. lexical-syms
  508. (lambda ()
  509. (if (null? dynamic)
  510. (make-body)
  511. (make-dynlet loc
  512. (map
  513. (compose
  514. (cut make-module-ref
  515. loc
  516. value-slot
  517. <>
  518. #t)
  519. car)
  520. dynamic)
  521. (map
  522. (lambda (sym)
  523. (make-lexical-ref
  524. loc
  525. sym
  526. sym))
  527. dynamic-syms)
  528. (make-body))))))))))))))))
  529. (defspecial let* (loc args)
  530. (pmatch args
  531. ((,varlist . ,body)
  532. (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
  533. (receive (decls forms) (parse-body body)
  534. (let iterate ((tail bindings))
  535. (if (null? tail)
  536. (compile-expr `(progn ,@forms))
  537. (let ((sym (caar tail))
  538. (value (compile-expr (cdar tail))))
  539. (if (bind-lexically? sym value-slot decls)
  540. (let ((target (gensym)))
  541. (make-let loc
  542. `(,target)
  543. `(,target)
  544. `(,value)
  545. (with-lexical-bindings
  546. (fluid-ref bindings-data)
  547. `(,sym)
  548. `(,target)
  549. (lambda () (iterate (cdr tail))))))
  550. (ensure-globals!
  551. loc
  552. (list sym)
  553. (make-dynlet loc
  554. (list (make-module-ref loc value-slot sym #t))
  555. (list value)
  556. (iterate (cdr tail)))))))))))))
  557. (defspecial flet (loc args)
  558. (pmatch args
  559. ((,bindings . ,body)
  560. (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
  561. (receive (decls forms) (parse-body body)
  562. (let ((names (map car names+vals))
  563. (vals (map cdr names+vals))
  564. (gensyms (map (lambda (x) (gensym)) names+vals)))
  565. (with-function-bindings
  566. (fluid-ref bindings-data)
  567. names
  568. gensyms
  569. (lambda ()
  570. (make-let loc
  571. names
  572. gensyms
  573. (map compile-expr vals)
  574. (compile-expr `(progn ,@forms)))))))))))
  575. (defspecial labels (loc args)
  576. (pmatch args
  577. ((,bindings . ,body)
  578. (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
  579. (receive (decls forms) (parse-body body)
  580. (let ((names (map car names+vals))
  581. (vals (map cdr names+vals))
  582. (gensyms (map (lambda (x) (gensym)) names+vals)))
  583. (with-function-bindings
  584. (fluid-ref bindings-data)
  585. names
  586. gensyms
  587. (lambda ()
  588. (make-letrec #f
  589. loc
  590. names
  591. gensyms
  592. (map compile-expr vals)
  593. (compile-expr `(progn ,@forms)))))))))))
  594. ;;; guile-ref allows building TreeIL's module references from within
  595. ;;; elisp as a way to access data within the Guile universe. The module
  596. ;;; and symbol referenced are static values, just like (@ module symbol)
  597. ;;; does!
  598. (defspecial guile-ref (loc args)
  599. (pmatch args
  600. ((,module ,sym) (guard (and (list? module) (symbol? sym)))
  601. (make-module-ref loc module sym #t))))
  602. ;;; guile-primitive allows to create primitive references, which are
  603. ;;; still a little faster.
  604. (defspecial guile-primitive (loc args)
  605. (pmatch args
  606. ((,sym)
  607. (make-primitive-ref loc sym))))
  608. (defspecial function (loc args)
  609. (pmatch args
  610. (((lambda ,args . ,body))
  611. (compile-lambda loc '() args body))
  612. ((,sym) (guard (symbol? sym))
  613. (reference-function loc sym))))
  614. (defspecial defmacro (loc args)
  615. (pmatch args
  616. ((,name ,args . ,body)
  617. (if (not (symbol? name))
  618. (report-error loc "expected symbol as macro name" name)
  619. (let* ((tree-il
  620. (make-seq
  621. loc
  622. (set-function!
  623. loc
  624. name
  625. (make-call
  626. loc
  627. (make-module-ref loc '(guile) 'cons #t)
  628. (list (make-const loc 'macro)
  629. (compile-lambda loc
  630. `((name . ,name))
  631. args
  632. body))))
  633. (make-const loc name))))
  634. (compile tree-il #:from 'tree-il #:to 'value)
  635. tree-il)))))
  636. (defspecial defun (loc args)
  637. (pmatch args
  638. ((,name ,args . ,body)
  639. (if (not (symbol? name))
  640. (report-error loc "expected symbol as function name" name)
  641. (make-seq loc
  642. (set-function! loc
  643. name
  644. (compile-lambda loc
  645. `((name . ,name))
  646. args
  647. body))
  648. (make-const loc name))))))
  649. (defspecial #{`}# (loc args)
  650. (pmatch args
  651. ((,val)
  652. (process-backquote loc val))))
  653. (defspecial quote (loc args)
  654. (pmatch args
  655. ((,val)
  656. (make-const loc val))))
  657. (defspecial %funcall (loc args)
  658. (pmatch args
  659. ((,function . ,arguments)
  660. (make-call loc
  661. (compile-expr function)
  662. (map compile-expr arguments)))))
  663. (defspecial %set-lexical-binding-mode (loc args)
  664. (pmatch args
  665. ((,val)
  666. (fluid-set! lexical-binding val)
  667. (make-void loc))))
  668. ;;; Compile a compound expression to Tree-IL.
  669. (define (compile-pair loc expr)
  670. (let ((operator (car expr))
  671. (arguments (cdr expr)))
  672. (cond
  673. ((find-operator operator 'special-operator)
  674. => (lambda (special-operator-function)
  675. (special-operator-function loc arguments)))
  676. ((find-operator operator 'macro)
  677. => (lambda (macro-function)
  678. (compile-expr (apply macro-function arguments))))
  679. (else
  680. (compile-expr `(%funcall (function ,operator) ,@arguments))))))
  681. ;;; Compile a symbol expression. This is a variable reference or maybe
  682. ;;; some special value like nil.
  683. (define (compile-symbol loc sym)
  684. (case sym
  685. ((nil) (nil-value loc))
  686. ((t) (t-value loc))
  687. (else (reference-variable loc sym))))
  688. ;;; Compile a single expression to TreeIL.
  689. (define (compile-expr expr)
  690. (let ((loc (location expr)))
  691. (cond
  692. ((symbol? expr)
  693. (compile-symbol loc expr))
  694. ((pair? expr)
  695. (compile-pair loc expr))
  696. (else (make-const loc expr)))))
  697. ;;; Process the compiler options.
  698. ;;; FIXME: Why is '(()) passed as options by the REPL?
  699. (define (valid-symbol-list-arg? value)
  700. (or (eq? value 'all)
  701. (and (list? value) (and-map symbol? value))))
  702. (define (process-options! opt)
  703. (if (and (not (null? opt))
  704. (not (equal? opt '(()))))
  705. (if (null? (cdr opt))
  706. (report-error #f "Invalid compiler options" opt)
  707. (let ((key (car opt))
  708. (value (cadr opt)))
  709. (case key
  710. ((#:warnings #:to-file?) ; ignore
  711. #f)
  712. (else (report-error #f
  713. "Invalid compiler option"
  714. key)))))))
  715. (define (compile-tree-il expr env opts)
  716. (values
  717. (with-fluids ((bindings-data (make-bindings)))
  718. (process-options! opts)
  719. (compile-expr expr))
  720. env
  721. env))