compile-tree-il.scm 28 KB

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