syntax.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber, Robert Ransom
  3. ; Macro expansion.
  4. ;----------------
  5. ; Scanning for definitions.
  6. ;
  7. ; Returns a list of forms expanded to the point needed to distinguish
  8. ; definitions from other forms. Definitions and syntax definitions are
  9. ; added to ENV.
  10. (define (scan-forms forms env)
  11. (let loop ((forms forms) (expanded '()))
  12. (if (null? forms)
  13. (reverse expanded)
  14. (let ((form (expand-head (car forms) env))
  15. (more-forms (cdr forms)))
  16. (cond ((define? form)
  17. (loop more-forms
  18. (cons (scan-define form env) expanded)))
  19. ((define-syntax? form)
  20. (loop more-forms
  21. (append (scan-define-syntax form env)
  22. expanded)))
  23. ((begin? form)
  24. (loop (append (cdr form) more-forms)
  25. expanded))
  26. (else
  27. (loop more-forms (cons form expanded))))))))
  28. (define (expand-scanned-form form env)
  29. (if (define? form)
  30. (expand-define form env)
  31. (expand form env)))
  32. (define (scan-define form env)
  33. (let ((new-form (destructure-define form)))
  34. (if new-form
  35. (begin
  36. (comp-env-define! env (cadr new-form) usual-variable-type)
  37. new-form)
  38. (syntax-violation 'syntax-rules "ill-formed definition" form))))
  39. (define (expand-define form env)
  40. (make-node operator/define
  41. (list (car form)
  42. (expand (cadr form) env)
  43. (expand (caddr form) env))))
  44. (define (scan-define-syntax form env)
  45. (if (and (or (this-long? form 3)
  46. (this-long? form 4)) ; may have name list for reifier
  47. (name? (cadr form)))
  48. (let ((name (cadr form))
  49. (source (caddr form))
  50. (package (extract-package-from-comp-env env)))
  51. (comp-env-define! env
  52. name
  53. syntax-type
  54. (process-syntax (if (null? (cdddr form))
  55. source
  56. `(cons ,source ',(cadddr form)))
  57. env
  58. name
  59. package))
  60. '())
  61. (syntax-violation 'define-syntax "ill-formed syntax definition" form)))
  62. ; This is used by the ,expand command.
  63. (define (expand-form form env)
  64. (let loop ((forms (list form)) (expanded '()))
  65. (if (null? forms)
  66. (if (= (length expanded) 1)
  67. (car expanded)
  68. (make-node operator/begin (cons 'begin (reverse expanded))))
  69. (let ((form (expand-head (car forms) env))
  70. (more-forms (cdr forms)))
  71. (cond ((define? form)
  72. (let* ((new-form (destructure-define form))
  73. (temp (if new-form
  74. (expand-define new-form env)
  75. (syntax-violation 'expand "ill-formed definition"
  76. form))))
  77. (loop more-forms (cons temp expanded))))
  78. ((define-syntax? form)
  79. (loop more-forms
  80. (cons (make-node operator/define-syntax
  81. (list (car form)
  82. (expand (cadr form) env)
  83. (make-node operator/quote
  84. `',(caddr form))))
  85. expanded)))
  86. ((begin? form)
  87. (loop (append (cdr form) more-forms)
  88. expanded))
  89. (else
  90. (loop more-forms
  91. (cons (expand form env) expanded))))))))
  92. ;----------------
  93. ; Looking for definitions.
  94. ; This expands the form until it reaches a name, a form whose car is an
  95. ; operator, a form whose car is unknown, or a literal.
  96. (define (expand-head form env)
  97. (cond ((node? form)
  98. (if (and (name-node? form)
  99. (not (node-ref form 'binding)))
  100. (expand-name (node-form form) env)
  101. form))
  102. ((name? form)
  103. (expand-name form env))
  104. ((pair? form)
  105. (let ((op (expand-head (car form) env)))
  106. (if (and (node? op)
  107. (name-node? op))
  108. (let ((probe (node-ref op 'binding)))
  109. (if (binding? probe)
  110. (let ((s (binding-static probe)))
  111. (cond ((and (transform? s)
  112. (eq? (binding-type probe) syntax-type))
  113. (expand-macro-application
  114. s (cons op (cdr form)) env expand-head))
  115. ((and (operator? s)
  116. (eq? s operator/structure-ref))
  117. (expand-structure-ref form env expand-head))
  118. (else
  119. (cons op (cdr form)))))
  120. (cons op (cdr form))))
  121. (cons op (cdr form)))))
  122. (else
  123. form)))
  124. ; Returns a DEFINE of the form (define <id> <value>). This handles the following
  125. ; kinds of defines:
  126. ; (define <id> <value>)
  127. ; (define <id>) ; value is unassigned
  128. ; (define (<id> . <formals>) <value>) ; value is a lambda
  129. ; The return value is #f if any syntax error is found.
  130. (define (destructure-define form)
  131. (if (at-least-this-long? form 2)
  132. (let ((pat (cadr form))
  133. (operator (car form)))
  134. (cond ((pair? pat)
  135. (if (and (name? (car pat))
  136. (names? (cdr pat))
  137. (not (null? (cddr form))))
  138. `(,operator ,(car pat)
  139. (,operator/lambda ,(cdr pat)
  140. . ,(cddr form)))
  141. #f))
  142. ((null? (cddr form))
  143. `(,operator ,pat (,operator/unassigned)))
  144. ((null? (cdddr form))
  145. `(,operator ,pat ,(caddr form)))
  146. (else
  147. #f)))
  148. #f))
  149. (define (make-operator-predicate operator-id)
  150. (let ((operator (get-operator operator-id syntax-type)))
  151. (lambda (form)
  152. (and (pair? form)
  153. (eq? operator
  154. (static-value (car form)))))))
  155. (define define? (make-operator-predicate 'define))
  156. (define begin? (make-operator-predicate 'begin))
  157. (define define-syntax? (make-operator-predicate 'define-syntax))
  158. (define (static-value form)
  159. (if (and (node? form)
  160. (name-node? form))
  161. (let ((probe (node-ref form 'binding)))
  162. (if (binding? probe)
  163. (binding-static probe)
  164. #f))
  165. #f))
  166. ; --------------------
  167. ; The horror of internal defines
  168. ; This returns a single node, either a LETREC, if there are internal definitions,
  169. ; or a BEGIN if there aren't any. If there are no expressions we turn the last
  170. ; definition back into an expression, thus causing the correct warning to be
  171. ; printed by the compiler.
  172. (define (expand-body body env)
  173. (if (null? (cdr body)) ;++
  174. (expand (car body) env)
  175. (call-with-values
  176. (lambda ()
  177. (scan-body-forms body env '()))
  178. (lambda (defs exps env)
  179. (if (null? defs)
  180. (make-node operator/begin (cons 'begin (expand-list exps env)))
  181. (call-with-values
  182. (lambda ()
  183. (if (null? exps)
  184. (values (reverse (cdr defs))
  185. `((,operator/define ,(caar defs) ,(cdar defs))))
  186. (values (reverse defs)
  187. exps)))
  188. (lambda (defs exps)
  189. (expand-letrec operator/letrec
  190. (map car defs)
  191. (map cdr defs)
  192. exps
  193. env))))))))
  194. ; Walk through FORMS looking for definitions. ENV is the current environment,
  195. ; DEFS a list of definitions found so far.
  196. ;
  197. ; Returns three values: a list of (define <name> <value>) lists, a list of
  198. ; remaining forms, and the environment to use for expanding all of the above.
  199. (define (scan-body-forms forms env defs)
  200. (if (null? forms)
  201. (values defs '() env)
  202. (let ((form (expand-head (car forms) env))
  203. (more-forms (cdr forms)))
  204. (cond ((define? form)
  205. (let ((new-form (destructure-define form)))
  206. (if new-form
  207. (let* ((name (cadr new-form))
  208. (node (make-node operator/name name)))
  209. (scan-body-forms more-forms
  210. (bind1 name node env)
  211. (cons (cons node
  212. (caddr new-form))
  213. defs)))
  214. (syntax-violation 'scan-body-forms
  215. "ill-formed definition" form))))
  216. ((begin? form)
  217. (call-with-values
  218. (lambda ()
  219. (scan-body-forms (cdr form)
  220. env
  221. defs))
  222. (lambda (new-defs exps env)
  223. (cond ((null? exps)
  224. (scan-body-forms more-forms env new-defs))
  225. ((eq? new-defs defs)
  226. (values defs (append exps more-forms) env))
  227. (else
  228. (body-lossage forms env))))))
  229. (else
  230. (values defs (cons form more-forms) env))))))
  231. (define (body-lossage node env)
  232. (syntax-violation 'body
  233. "definitions and expressions intermixed"
  234. (schemify node env)))
  235. ;--------------------
  236. ; Expands all macros in FORM and returns a node.
  237. (define (expand form env)
  238. (cond ((node? form)
  239. (if (and (name-node? form)
  240. (not (node-ref form 'binding)))
  241. (expand-name (node-form form) env)
  242. form))
  243. ((name? form)
  244. (expand-name form env))
  245. ((pair? form)
  246. (if (operator? (car form))
  247. (expand-operator-form (car form) (car form) form env)
  248. (let ((op-node (expand (car form) env)))
  249. (if (name-node? op-node)
  250. (let ((probe (node-ref op-node 'binding)))
  251. (if (binding? probe)
  252. (let ((s (binding-static probe)))
  253. (cond ((operator? s)
  254. (expand-operator-form s op-node form env))
  255. ((and (transform? s)
  256. (eq? (binding-type probe) syntax-type))
  257. ;; Non-syntax transforms get done later
  258. (expand-macro-application
  259. s (cons op-node (cdr form)) env expand))
  260. (else
  261. (expand-call op-node form env))))
  262. (expand-call op-node form env)))
  263. (expand-call op-node form env)))))
  264. ((literal? form)
  265. (expand-literal form))
  266. ;; ((qualified? form) ...)
  267. (else
  268. (syntax-violation 'expand "invalid expression" form))))
  269. (define (expand-list exps env)
  270. (map (lambda (exp)
  271. (expand exp env))
  272. exps))
  273. (define (expand-literal exp)
  274. (make-node operator/literal (make-immutable! exp)))
  275. (define (expand-call proc-node exp env)
  276. (if (list? exp)
  277. (make-node operator/call
  278. (cons proc-node (expand-list (cdr exp) env)))
  279. (syntax-violation 'expand-call "invalid expression" exp)))
  280. ; An environment is a procedure that takes a name and returns one of
  281. ; the following:
  282. ;
  283. ; 1. A binding record.
  284. ; 2. A pair (<binding-record> . <path>)
  285. ; 3. A node, which is taken to be a substitution for the name.
  286. ; Or, for lexically bound variables, this is just a name node.
  287. ; 4. #f, for unbound variables
  288. ;
  289. ; In case 1, EXPAND caches the binding as the node's BINDING property.
  290. ; In case 2, it simply returns the node.
  291. (define (expand-name name env)
  292. (let ((binding (lookup env name)))
  293. (if (node? binding)
  294. binding
  295. (let ((node (make-node operator/name name)))
  296. (node-set! node 'binding (or binding 'unbound))
  297. node))))
  298. ; Expand a macro. EXPAND may either be expand or expand-head.
  299. (define (expand-macro-application transform form env-of-use expand)
  300. (call-with-values
  301. (lambda ()
  302. (maybe-apply-macro-transform transform
  303. form
  304. (node-form (car form))
  305. env-of-use))
  306. (lambda (new-form new-env)
  307. (if (eq? new-form form)
  308. (syntax-violation (schemify (car form) env-of-use)
  309. "use of macro doesn't match definition"
  310. (cons (schemify (car form) env-of-use)
  311. (desyntaxify (cdr form))))
  312. (expand new-form new-env)))))
  313. ;--------------------
  314. ; Specialist classifiers for particular operators
  315. (define (expand-operator-form op op-node form env)
  316. ((operator-table-ref expanders (operator-uid op))
  317. op op-node form env))
  318. (define expanders
  319. (make-operator-table (lambda (op op-node form env)
  320. (if (let ((nargs (operator-nargs op)))
  321. (or (not nargs)
  322. (and (list? (cdr form))
  323. (= nargs (length (cdr form))))))
  324. (make-node op
  325. (cons op-node
  326. (expand-list (cdr form) env)))
  327. (expand-call op-node form env)))))
  328. (define (define-expander name proc)
  329. (operator-define! expanders name syntax-type proc))
  330. ; Definitions are not expressions.
  331. (define-expander 'define
  332. (lambda (op op-node exp env)
  333. (syntax-violation 'define
  334. (if (destructure-define exp)
  335. "definition in expression context"
  336. "ill-formed definition")
  337. exp)))
  338. ; Remove generated names from quotations.
  339. (define-expander 'quote
  340. (lambda (op op-node exp env)
  341. (if (this-long? exp 2)
  342. (make-node op (list op (desyntaxify (cadr exp))))
  343. (syntax-violation 'quote "invalid expression" exp))))
  344. ; Don't evaluate, but don't remove generated names either. This is
  345. ; used when writing macro-defining macros. Once we have avoided the
  346. ; use of DESYNTAXIFY it is safe to replace this with regular QUOTE.
  347. (define-expander 'code-quote
  348. (lambda (op op-node exp env)
  349. (if (this-long? exp 2)
  350. (make-node operator/quote (list op (cadr exp)))
  351. (syntax-violation 'code-quote "invalid expression" exp))))
  352. ; Convert one-armed IF to two-armed IF.
  353. (define-expander 'if
  354. (lambda (op op-node exp env)
  355. (cond ((this-long? exp 3)
  356. (make-node op
  357. (cons op
  358. (expand-list (append (cdr exp)
  359. (list (unspecific-node)))
  360. env))))
  361. ((this-long? exp 4)
  362. (make-node op
  363. (cons op (expand-list (cdr exp) env))))
  364. (else
  365. (syntax-violation 'if "invalid expression" exp)))))
  366. (define (unspecific-node)
  367. (make-node operator/unspecific '(unspecific)))
  368. ; For the module system:
  369. (define-expander 'structure-ref
  370. (lambda (op op-node form env)
  371. (expand-structure-ref form env expand)))
  372. ; This is also called by EXPAND-HEAD, which passes in a different expander.
  373. (define (expand-structure-ref form env expander)
  374. (let ((struct-node (expand (cadr form) env))
  375. (lose (lambda ()
  376. (syntax-violation 'structure-ref "invalid structure reference" form))))
  377. (if (and (this-long? form 3)
  378. (name? (caddr form))
  379. (name-node? struct-node))
  380. (let ((b (node-ref struct-node 'binding)))
  381. (if (and (binding? b)
  382. (binding-static b)) ; (structure? ...)
  383. (expander (generate-name (desyntaxify (caddr form))
  384. (binding-static b)
  385. (node-form struct-node))
  386. env)
  387. (lose)))
  388. (lose))))
  389. ; Scheme 48 internal special form principally for use by the
  390. ; DEFINE-STRUCTURES macro.
  391. (define-expander '%file-name%
  392. (lambda (op op-node form env)
  393. (make-node operator/quote `',(source-file-name env))))
  394. ; Checking the syntax of others special forms
  395. (define-expander 'lambda
  396. (lambda (op op-node exp env)
  397. (if (and (at-least-this-long? exp 3)
  398. (names? (cadr exp)))
  399. (expand-lambda (cadr exp) (cddr exp) env)
  400. (syntax-violation 'lambda "invalid expression" exp))))
  401. (define (expand-lambda names body env)
  402. (call-with-values
  403. (lambda ()
  404. (bind-names names env))
  405. (lambda (names env)
  406. (make-node operator/lambda
  407. (list 'lambda names (expand-body body env))))))
  408. (define (bind-names names env)
  409. (let loop ((names names) (nodes '()) (out-names '()))
  410. (cond ((null? names)
  411. (values (reverse nodes)
  412. (bind out-names nodes env)))
  413. ((name? names)
  414. (let ((last (make-node operator/name names)))
  415. (values (append (reverse nodes) last)
  416. (bind (cons names out-names) (cons last nodes) env))))
  417. (else
  418. (let ((node (make-node operator/name (car names))))
  419. (loop (cdr names) (cons node nodes) (cons (car names) out-names)))))))
  420. (define (names? l)
  421. (or (null? l)
  422. (name? l)
  423. (and (pair? l)
  424. (name? (car l))
  425. (names? (cdr l)))))
  426. (define-expander 'set!
  427. (lambda (op op-node exp env)
  428. (if (and (this-long? exp 3)
  429. (name? (cadr exp)))
  430. (make-node op (cons op (expand-list (cdr exp) env)))
  431. (syntax-violation 'set! "invalid expression" exp))))
  432. (define (letrec-expander op/letrec)
  433. (lambda (op op-node exp env)
  434. (if (and (at-least-this-long? exp 3)
  435. (let-specs? (cadr exp)))
  436. (let ((specs (cadr exp))
  437. (body (cddr exp)))
  438. (let* ((names (map (lambda (spec)
  439. (make-node operator/name (car spec)))
  440. specs))
  441. (env (bind (map car specs) names env)))
  442. (expand-letrec op/letrec names (map cadr specs) body env)))
  443. (syntax-violation 'letrec "invalid expression" exp))))
  444. (define-expander 'letrec
  445. (letrec-expander operator/letrec))
  446. (define-expander 'letrec*
  447. (letrec-expander operator/letrec*))
  448. (define (expand-letrec op/letrec names values body env)
  449. (let* ((new-specs (map (lambda (name value)
  450. (list name
  451. (expand value env)))
  452. names
  453. values)))
  454. (make-node op/letrec
  455. (list 'letrec new-specs (expand-body body env)))))
  456. (define-expander 'loophole
  457. (lambda (op op-node exp env)
  458. (if (this-long? exp 3)
  459. (make-node op (list op
  460. (sexp->type (desyntaxify (cadr exp)) #t)
  461. (expand (caddr exp) env)))
  462. (syntax-violation 'loophole "invalid expression" exp))))
  463. (define-expander 'let-syntax
  464. (lambda (op op-node exp env)
  465. (if (and (at-least-this-long? exp 3)
  466. (let-specs? (cadr exp)))
  467. (let ((specs (cadr exp)))
  468. (expand-body (cddr exp)
  469. (bind (map car specs)
  470. (map (lambda (spec)
  471. (make-binding syntax-type
  472. (list 'let-syntax)
  473. (process-syntax (cadr spec)
  474. env
  475. (car spec)
  476. env)))
  477. specs)
  478. env)))
  479. (syntax-violation 'let-syntax "invalid expression" exp))))
  480. (define-expander 'letrec-syntax
  481. (lambda (op op-node exp env)
  482. (if (and (at-least-this-long? exp 3)
  483. (let-specs? (cadr exp)))
  484. (let* ((specs (cadr exp))
  485. (bindings (map (lambda (spec)
  486. (make-binding syntax-type
  487. (list 'letrec-syntax)
  488. 'unassigned))
  489. specs))
  490. (new-env (bind (map car specs) bindings env)))
  491. (for-each (lambda (spec binding)
  492. (set-binding-static! binding
  493. (process-syntax (cadr spec)
  494. new-env
  495. (car spec)
  496. new-env)))
  497. specs bindings)
  498. (expand-body (cddr exp) new-env))
  499. (syntax-violation 'letrec-syntax "invalid expression" exp))))
  500. (define (process-syntax form env name env-or-package)
  501. (let ((eval+env (force (comp-env-macro-eval env))))
  502. (make-transform/macro ((car eval+env) form (cdr eval+env))
  503. env-or-package
  504. syntax-type
  505. form
  506. name)))
  507. ; This just looks up the names that the LAP code will want and replaces them
  508. ; with the appropriate node.
  509. ;
  510. ; (lap <id> (<free name> ...) <instruction> ...)
  511. (define-expander 'lap
  512. (lambda (op op-node exp env)
  513. (if (and (at-least-this-long? exp 4)
  514. (name? (cdr exp))
  515. (every name? (caddr exp)))
  516. (make-node op `(,op
  517. ,(desyntaxify (cadr exp))
  518. ,(map (lambda (name)
  519. (expand-name (cadr exp) env))
  520. (caddr exp))
  521. . ,(cdddr exp)))
  522. (syntax-violation 'lap "invalid expression" exp))))
  523. ; --------------------
  524. ; Syntax checking utilities
  525. (define (this-long? l n)
  526. (cond ((null? l)
  527. (= n 0))
  528. ((pair? l)
  529. (this-long? (cdr l) (- n 1)))
  530. (else
  531. #f)))
  532. (define (at-least-this-long? l n)
  533. (cond ((null? l)
  534. (<= n 0))
  535. ((pair? l)
  536. (at-least-this-long? (cdr l) (- n 1)))
  537. (else
  538. #f)))
  539. (define (let-specs? x)
  540. (or (null? x)
  541. (and (pair? x)
  542. (let ((s (car x)))
  543. (and (pair? s)
  544. (name? (car s))
  545. (pair? (cdr s))
  546. (null? (cddr s))))
  547. (let-specs? (cdr x)))))
  548. ; --------------------
  549. ; Utilities
  550. (define (literal? exp)
  551. (or (number? exp) (char? exp) (string? exp) (boolean? exp)
  552. (code-vector? exp)))
  553. (define (syntax? d)
  554. (cond ((operator? d)
  555. (eq? (operator-type d) syntax-type))
  556. ((transform? d)
  557. (eq? (transform-type d) syntax-type))
  558. (else #f)))