compile-tree-il.scm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. (nil (null))
  222. ((&optional . ,tail) (optional tail))
  223. ((&rest . ,tail) (rest tail))
  224. ((,arg . ,tail) (guard (symbol? arg)) (symbol arg tail))
  225. (else (fail))))
  226. (define (return rreq ropt rest)
  227. (values #t (reverse rreq) (reverse ropt) rest))
  228. (define (fail)
  229. (values #f #f #f #f))
  230. (define (parse-req lst rreq)
  231. (%match lst
  232. (lambda () (return rreq '() #f))
  233. (lambda (tail) (parse-opt tail rreq '()))
  234. (lambda (tail) (parse-rest tail rreq '()))
  235. (lambda (arg tail) (parse-req tail (cons arg rreq)))))
  236. (define (parse-opt lst rreq ropt)
  237. (%match lst
  238. (lambda () (return rreq ropt #f))
  239. (lambda (tail) (fail))
  240. (lambda (tail) (parse-rest tail rreq ropt))
  241. (lambda (arg tail) (parse-opt tail rreq (cons arg ropt)))))
  242. (define (parse-rest lst rreq ropt)
  243. (%match lst
  244. (lambda () (fail))
  245. (lambda (tail) (fail))
  246. (lambda (tail) (fail))
  247. (lambda (arg tail) (parse-post-rest tail rreq ropt arg))))
  248. (define (parse-post-rest lst rreq ropt rest)
  249. (%match lst
  250. (lambda () (return rreq ropt rest))
  251. (lambda () (fail))
  252. (lambda () (fail))
  253. (lambda (arg tail) (fail))))
  254. (parse-req lst '()))
  255. (define (make-simple-lambda loc meta req opt init rest vars body)
  256. (make-lambda loc
  257. meta
  258. (make-lambda-case #f req opt rest #f init vars body #f)))
  259. (define (make-dynlet src fluids vals body)
  260. (let ((f (map (lambda (x) (gensym "fluid ")) fluids))
  261. (v (map (lambda (x) (gensym "valud ")) vals)))
  262. (make-let src (map (lambda (_) 'fluid) fluids) f fluids
  263. (make-let src (map (lambda (_) 'val) vals) v vals
  264. (let lp ((f f) (v v))
  265. (if (null? f)
  266. body
  267. (make-primcall
  268. src 'with-fluid*
  269. (list (make-lexical-ref #f 'fluid (car f))
  270. (make-lexical-ref #f 'val (car v))
  271. (make-lambda
  272. src '()
  273. (make-lambda-case
  274. src '() #f #f #f '() '()
  275. (lp (cdr f) (cdr v))
  276. #f))))))))))
  277. (define (compile-lambda loc meta args body)
  278. (receive (valid? req-ids opt-ids rest-id)
  279. (parse-lambda-list args)
  280. (if valid?
  281. (let* ((all-ids (append req-ids
  282. opt-ids
  283. (or (and=> rest-id list) '())))
  284. (all-vars (map (lambda (ignore) (gensym)) all-ids)))
  285. (let*-values (((decls intspec doc forms)
  286. (parse-lambda-body body))
  287. ((lexical dynamic)
  288. (partition
  289. (compose (cut bind-lexically? <> value-slot decls)
  290. car)
  291. (map list all-ids all-vars)))
  292. ((lexical-ids lexical-vars) (unzip2 lexical))
  293. ((dynamic-ids dynamic-vars) (unzip2 dynamic)))
  294. (with-dynamic-bindings
  295. (fluid-ref bindings-data)
  296. dynamic-ids
  297. (lambda ()
  298. (with-lexical-bindings
  299. (fluid-ref bindings-data)
  300. lexical-ids
  301. lexical-vars
  302. (lambda ()
  303. (ensure-globals!
  304. loc
  305. dynamic-ids
  306. (let* ((tree-il
  307. (compile-expr
  308. (if rest-id
  309. `(let ((,rest-id (if ,rest-id
  310. ,rest-id
  311. nil)))
  312. ,@forms)
  313. `(progn ,@forms))))
  314. (full-body
  315. (if (null? dynamic)
  316. tree-il
  317. (make-dynlet
  318. loc
  319. (map (cut make-module-ref loc value-slot <> #t)
  320. dynamic-ids)
  321. (map (cut make-lexical-ref loc <> <>)
  322. dynamic-ids
  323. dynamic-vars)
  324. tree-il))))
  325. (make-simple-lambda loc
  326. meta
  327. req-ids
  328. opt-ids
  329. (map (const (nil-value loc))
  330. opt-ids)
  331. rest-id
  332. all-vars
  333. full-body)))))))))
  334. (report-error "invalid function" `(lambda ,args ,@body)))))
  335. ;;; Handle the common part of defconst and defvar, that is, checking for
  336. ;;; a correct doc string and arguments as well as maybe in the future
  337. ;;; handling the docstring somehow.
  338. (define (handle-var-def loc sym doc)
  339. (cond
  340. ((not (symbol? sym)) (report-error loc "expected symbol, got" sym))
  341. ((> (length doc) 1) (report-error loc "too many arguments to defvar"))
  342. ((and (not (null? doc)) (not (string? (car doc))))
  343. (report-error loc "expected string as third argument of defvar, got"
  344. (car doc)))
  345. ;; TODO: Handle doc string if present.
  346. (else #t)))
  347. ;;; Handle macro and special operator bindings.
  348. (define (find-operator name type)
  349. (and
  350. (symbol? name)
  351. (module-defined? (resolve-interface function-slot) name)
  352. (let ((op (module-ref (resolve-module function-slot) name)))
  353. (if (and (pair? op) (eq? (car op) type))
  354. (cdr op)
  355. #f))))
  356. ;;; See if a (backquoted) expression contains any unquotes.
  357. (define (contains-unquotes? expr)
  358. (if (pair? expr)
  359. (if (or (unquote? (car expr)) (unquote-splicing? (car expr)))
  360. #t
  361. (or (contains-unquotes? (car expr))
  362. (contains-unquotes? (cdr expr))))
  363. #f))
  364. ;;; Process a backquoted expression by building up the needed
  365. ;;; cons/append calls. For splicing, it is assumed that the expression
  366. ;;; spliced in evaluates to a list. The emacs manual does not really
  367. ;;; state either it has to or what to do if it does not, but Scheme
  368. ;;; explicitly forbids it and this seems reasonable also for elisp.
  369. (define (unquote-cell? expr)
  370. (and (list? expr) (= (length expr) 2) (unquote? (car expr))))
  371. (define (unquote-splicing-cell? expr)
  372. (and (list? expr) (= (length expr) 2) (unquote-splicing? (car expr))))
  373. (define (process-backquote loc expr)
  374. (if (contains-unquotes? expr)
  375. (if (pair? expr)
  376. (if (or (unquote-cell? expr) (unquote-splicing-cell? expr))
  377. (compile-expr (cadr expr))
  378. (let* ((head (car expr))
  379. (processed-tail (process-backquote loc (cdr expr)))
  380. (head-is-list-2 (and (list? head)
  381. (= (length head) 2)))
  382. (head-unquote (and head-is-list-2
  383. (unquote? (car head))))
  384. (head-unquote-splicing (and head-is-list-2
  385. (unquote-splicing?
  386. (car head)))))
  387. (if head-unquote-splicing
  388. (call-primitive loc
  389. 'append
  390. (compile-expr (cadr head))
  391. processed-tail)
  392. (call-primitive loc 'cons
  393. (if head-unquote
  394. (compile-expr (cadr head))
  395. (process-backquote loc head))
  396. processed-tail))))
  397. (report-error loc
  398. "non-pair expression contains unquotes"
  399. expr))
  400. (make-const loc expr)))
  401. ;;; Special operators
  402. (defspecial progn (loc args)
  403. (list->seq loc
  404. (if (null? args)
  405. (list (nil-value loc))
  406. (map compile-expr args))))
  407. (defspecial eval-when-compile (loc args)
  408. (make-const loc (with-native-target
  409. (lambda ()
  410. (compile `(progn ,@args) #:from 'elisp #:to 'value)))))
  411. (defspecial if (loc args)
  412. (pmatch args
  413. ((,cond ,then . ,else)
  414. (make-conditional
  415. loc
  416. (call-primitive loc 'not
  417. (call-primitive loc 'nil? (compile-expr cond)))
  418. (compile-expr then)
  419. (compile-expr `(progn ,@else))))))
  420. (defspecial defconst (loc args)
  421. (pmatch args
  422. ((,sym ,value . ,doc)
  423. (if (handle-var-def loc sym doc)
  424. (make-seq loc
  425. (set-variable! loc sym (compile-expr value))
  426. (make-const loc sym))))))
  427. (defspecial defvar (loc args)
  428. (pmatch args
  429. ((,sym) (make-const loc sym))
  430. ((,sym ,value . ,doc)
  431. (if (handle-var-def loc sym doc)
  432. (make-seq
  433. loc
  434. (make-conditional
  435. loc
  436. (make-conditional
  437. loc
  438. (call-primitive
  439. loc
  440. 'module-bound?
  441. (call-primitive loc
  442. 'resolve-interface
  443. (make-const loc value-slot))
  444. (make-const loc sym))
  445. (call-primitive loc
  446. 'fluid-bound?
  447. (make-module-ref loc value-slot sym #t))
  448. (make-const loc #f))
  449. (make-void loc)
  450. (set-variable! loc sym (compile-expr value)))
  451. (make-const loc sym))))))
  452. (defspecial setq (loc args)
  453. (define (car* x) (if (null? x) '() (car x)))
  454. (define (cdr* x) (if (null? x) '() (cdr x)))
  455. (define (cadr* x) (car* (cdr* x)))
  456. (define (cddr* x) (cdr* (cdr* x)))
  457. (list->seq
  458. loc
  459. (let loop ((args args) (last (nil-value loc)))
  460. (if (null? args)
  461. (list last)
  462. (let ((sym (car args))
  463. (val (compile-expr (cadr* args))))
  464. (if (not (symbol? sym))
  465. (report-error loc "expected symbol in setq")
  466. (cons
  467. (set-variable! loc sym val)
  468. (loop (cddr* args)
  469. (reference-variable loc sym)))))))))
  470. (defspecial let (loc args)
  471. (pmatch args
  472. ((,varlist . ,body)
  473. (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
  474. (receive (decls forms) (parse-body body)
  475. (receive (lexical dynamic)
  476. (partition
  477. (compose (cut bind-lexically? <> value-slot decls)
  478. car)
  479. bindings)
  480. (let ((make-values (lambda (for)
  481. (map (lambda (el) (compile-expr (cdr el)))
  482. for)))
  483. (make-body (lambda () (compile-expr `(progn ,@forms)))))
  484. (ensure-globals!
  485. loc
  486. (map car dynamic)
  487. (if (null? lexical)
  488. (make-dynlet loc
  489. (map (compose (cut make-module-ref
  490. loc
  491. value-slot
  492. <>
  493. #t)
  494. car)
  495. dynamic)
  496. (map (compose compile-expr cdr)
  497. dynamic)
  498. (make-body))
  499. (let* ((lexical-syms (map (lambda (el) (gensym)) lexical))
  500. (dynamic-syms (map (lambda (el) (gensym)) dynamic))
  501. (all-syms (append lexical-syms dynamic-syms))
  502. (vals (append (make-values lexical)
  503. (make-values dynamic))))
  504. (make-let loc
  505. all-syms
  506. all-syms
  507. vals
  508. (with-lexical-bindings
  509. (fluid-ref bindings-data)
  510. (map car lexical)
  511. lexical-syms
  512. (lambda ()
  513. (if (null? dynamic)
  514. (make-body)
  515. (make-dynlet loc
  516. (map
  517. (compose
  518. (cut make-module-ref
  519. loc
  520. value-slot
  521. <>
  522. #t)
  523. car)
  524. dynamic)
  525. (map
  526. (lambda (sym)
  527. (make-lexical-ref
  528. loc
  529. sym
  530. sym))
  531. dynamic-syms)
  532. (make-body))))))))))))))))
  533. (defspecial let* (loc args)
  534. (pmatch args
  535. ((,varlist . ,body)
  536. (let ((bindings (map (cut parse-let-binding loc <>) varlist)))
  537. (receive (decls forms) (parse-body body)
  538. (let iterate ((tail bindings))
  539. (if (null? tail)
  540. (compile-expr `(progn ,@forms))
  541. (let ((sym (caar tail))
  542. (value (compile-expr (cdar tail))))
  543. (if (bind-lexically? sym value-slot decls)
  544. (let ((target (gensym)))
  545. (make-let loc
  546. `(,target)
  547. `(,target)
  548. `(,value)
  549. (with-lexical-bindings
  550. (fluid-ref bindings-data)
  551. `(,sym)
  552. `(,target)
  553. (lambda () (iterate (cdr tail))))))
  554. (ensure-globals!
  555. loc
  556. (list sym)
  557. (make-dynlet loc
  558. (list (make-module-ref loc value-slot sym #t))
  559. (list value)
  560. (iterate (cdr tail)))))))))))))
  561. (defspecial flet (loc args)
  562. (pmatch args
  563. ((,bindings . ,body)
  564. (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
  565. (receive (decls forms) (parse-body body)
  566. (let ((names (map car names+vals))
  567. (vals (map cdr names+vals))
  568. (gensyms (map (lambda (x) (gensym)) names+vals)))
  569. (with-function-bindings
  570. (fluid-ref bindings-data)
  571. names
  572. gensyms
  573. (lambda ()
  574. (make-let loc
  575. names
  576. gensyms
  577. (map compile-expr vals)
  578. (compile-expr `(progn ,@forms)))))))))))
  579. (defspecial labels (loc args)
  580. (pmatch args
  581. ((,bindings . ,body)
  582. (let ((names+vals (map (cut parse-flet-binding loc <>) bindings)))
  583. (receive (decls forms) (parse-body body)
  584. (let ((names (map car names+vals))
  585. (vals (map cdr names+vals))
  586. (gensyms (map (lambda (x) (gensym)) names+vals)))
  587. (with-function-bindings
  588. (fluid-ref bindings-data)
  589. names
  590. gensyms
  591. (lambda ()
  592. (make-letrec #f
  593. loc
  594. names
  595. gensyms
  596. (map compile-expr vals)
  597. (compile-expr `(progn ,@forms)))))))))))
  598. ;;; guile-ref allows building TreeIL's module references from within
  599. ;;; elisp as a way to access data within the Guile universe. The module
  600. ;;; and symbol referenced are static values, just like (@ module symbol)
  601. ;;; does!
  602. (defspecial guile-ref (loc args)
  603. (pmatch args
  604. ((,module ,sym) (guard (and (list? module) (symbol? sym)))
  605. (make-module-ref loc module sym #t))))
  606. ;;; guile-primitive allows to create primitive references, which are
  607. ;;; still a little faster.
  608. (defspecial guile-primitive (loc args)
  609. (pmatch args
  610. ((,sym)
  611. (make-primitive-ref loc sym))))
  612. (defspecial function (loc args)
  613. (pmatch args
  614. (((lambda ,args . ,body))
  615. (compile-lambda loc '() args body))
  616. ((,sym) (guard (symbol? sym))
  617. (reference-function loc sym))))
  618. (defspecial defmacro (loc args)
  619. (pmatch args
  620. ((,name ,args . ,body)
  621. (if (not (symbol? name))
  622. (report-error loc "expected symbol as macro name" name)
  623. (let* ((tree-il
  624. (make-seq
  625. loc
  626. (set-function!
  627. loc
  628. name
  629. (make-call
  630. loc
  631. (make-module-ref loc '(guile) 'cons #t)
  632. (list (make-const loc 'macro)
  633. (compile-lambda loc
  634. `((name . ,name))
  635. args
  636. body))))
  637. (make-const loc name))))
  638. (with-native-target
  639. (lambda ()
  640. (compile tree-il #:from 'tree-il #:to 'value)))
  641. tree-il)))))
  642. (defspecial defun (loc args)
  643. (pmatch args
  644. ((,name ,args . ,body)
  645. (if (not (symbol? name))
  646. (report-error loc "expected symbol as function name" name)
  647. (make-seq loc
  648. (set-function! loc
  649. name
  650. (compile-lambda loc
  651. `((name . ,name))
  652. args
  653. body))
  654. (make-const loc name))))))
  655. (defspecial #{`}# (loc args)
  656. (pmatch args
  657. ((,val)
  658. (process-backquote loc val))))
  659. (defspecial quote (loc args)
  660. (pmatch args
  661. ((,val)
  662. (make-const loc val))))
  663. (defspecial %funcall (loc args)
  664. (pmatch args
  665. ((,function . ,arguments)
  666. (make-call loc
  667. (compile-expr function)
  668. (map compile-expr arguments)))))
  669. (defspecial %set-lexical-binding-mode (loc args)
  670. (pmatch args
  671. ((,val)
  672. (fluid-set! lexical-binding val)
  673. (make-void loc))))
  674. ;;; Compile a compound expression to Tree-IL.
  675. (define (compile-pair loc expr)
  676. (let ((operator (car expr))
  677. (arguments (cdr expr)))
  678. (cond
  679. ((find-operator operator 'special-operator)
  680. => (lambda (special-operator-function)
  681. (special-operator-function loc arguments)))
  682. ((find-operator operator 'macro)
  683. => (lambda (macro-function)
  684. (compile-expr (apply macro-function arguments))))
  685. (else
  686. (compile-expr `(%funcall (function ,operator) ,@arguments))))))
  687. ;;; Compile a symbol expression. This is a variable reference or maybe
  688. ;;; some special value like nil.
  689. (define (compile-symbol loc sym)
  690. (case sym
  691. ((nil) (nil-value loc))
  692. ((t) (t-value loc))
  693. (else (reference-variable loc sym))))
  694. ;;; Compile a single expression to TreeIL.
  695. (define (compile-expr expr)
  696. (let ((loc (location expr)))
  697. (cond
  698. ((symbol? expr)
  699. (compile-symbol loc expr))
  700. ((pair? expr)
  701. (compile-pair loc expr))
  702. (else (make-const loc expr)))))
  703. ;;; Process the compiler options.
  704. ;;; FIXME: Why is '(()) passed as options by the REPL?
  705. (define (valid-symbol-list-arg? value)
  706. (or (eq? value 'all)
  707. (and (list? value) (and-map symbol? value))))
  708. (define (process-options! opt)
  709. (if (and (not (null? opt))
  710. (not (equal? opt '(()))))
  711. (if (null? (cdr opt))
  712. (report-error #f "Invalid compiler options" opt)
  713. (let ((key (car opt))
  714. (value (cadr opt)))
  715. (case key
  716. ((#:warnings #:to-file?) ; ignore
  717. #f)
  718. (else (report-error #f
  719. "Invalid compiler option"
  720. key)))))))
  721. (define (compile-tree-il expr env opts)
  722. (values
  723. (with-fluids ((bindings-data (make-bindings)))
  724. (process-options! opts)
  725. (compile-expr expr))
  726. env
  727. env))