syntax.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ;;; Guile VM specific syntaxes and utilities
  2. ;; Copyright (C) 2001, 2009 Free Software Foundation, Inc
  3. ;;; This library is free software; you can redistribute it and/or
  4. ;;; modify it under the terms of the GNU Lesser General Public
  5. ;;; License as published by the Free Software Foundation; either
  6. ;;; version 3 of the License, or (at your option) any later version.
  7. ;;;
  8. ;;; This library 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 GNU
  11. ;;; Lesser General Public License for more details.
  12. ;;;
  13. ;;; You should have received a copy of the GNU Lesser General Public
  14. ;;; License along with this library; if not, write to the Free Software
  15. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (system base syntax)
  18. #:export (%compute-initargs)
  19. #:export-syntax (define-type define-record define-record/keywords
  20. record-case transform-record))
  21. (define (symbol-trim-both sym pred)
  22. (string->symbol (string-trim-both (symbol->string sym) pred)))
  23. (define (trim-brackets sym)
  24. (symbol-trim-both sym (list->char-set '(#\< #\>))))
  25. ;;;
  26. ;;; Type
  27. ;;;
  28. (define-macro (define-type name . rest)
  29. (let ((name (if (pair? name) (car name) name))
  30. (opts (if (pair? name) (cdr name) '())))
  31. (let ((printer (kw-arg-ref opts #:printer))
  32. (common-slots (or (kw-arg-ref opts #:common-slots) '())))
  33. `(begin ,@(map (lambda (def)
  34. `(define-record ,(if printer
  35. `(,(car def) ,printer)
  36. (car def))
  37. ,@common-slots
  38. ,@(cdr def)))
  39. rest)
  40. ,@(map (lambda (common-slot i)
  41. `(define ,(symbol-append (trim-brackets name)
  42. '- common-slot)
  43. (make-procedure-with-setter
  44. (lambda (x) (struct-ref x ,i))
  45. (lambda (x v) (struct-set! x ,i v)))))
  46. common-slots (iota (length common-slots)))))))
  47. ;;;
  48. ;;; Record
  49. ;;;
  50. (define-macro (define-record name-form . slots)
  51. (let* ((name (if (pair? name-form) (car name-form) name-form))
  52. (printer (and (pair? name-form) (cadr name-form)))
  53. (slot-names (map (lambda (slot) (if (pair? slot) (car slot) slot))
  54. slots))
  55. (stem (trim-brackets name)))
  56. `(begin
  57. (define ,name (make-record-type ,(symbol->string name) ',slot-names
  58. ,@(if printer (list printer) '())))
  59. ,(let* ((reqs (let lp ((slots slots))
  60. (if (or (null? slots) (not (symbol? (car slots))))
  61. '()
  62. (cons (car slots) (lp (cdr slots))))))
  63. (opts (list-tail slots (length reqs)))
  64. (tail (gensym)))
  65. `(define (,(symbol-append 'make- stem) ,@reqs . ,tail)
  66. (let ,(map (lambda (o)
  67. `(,(car o) (cond ((null? ,tail) ,(cadr o))
  68. (else (let ((_x (car ,tail)))
  69. (set! ,tail (cdr ,tail))
  70. _x)))))
  71. opts)
  72. (make-struct ,name 0 ,@slot-names))))
  73. (define ,(symbol-append stem '?) (record-predicate ,name))
  74. ,@(map (lambda (sname)
  75. `(define ,(symbol-append stem '- sname)
  76. (make-procedure-with-setter
  77. (record-accessor ,name ',sname)
  78. (record-modifier ,name ',sname))))
  79. slot-names))))
  80. ;; like the former, but accepting keyword arguments in addition to
  81. ;; optional arguments
  82. (define-macro (define-record/keywords name-form . slots)
  83. (let* ((name (if (pair? name-form) (car name-form) name-form))
  84. (printer (and (pair? name-form) (cadr name-form)))
  85. (slot-names (map (lambda (slot) (if (pair? slot) (car slot) slot))
  86. slots))
  87. (stem (trim-brackets name)))
  88. `(begin
  89. (define ,name (make-record-type ,(symbol->string name) ',slot-names
  90. ,@(if printer (list printer) '())))
  91. (define ,(symbol-append 'make- stem)
  92. (let ((slots (list ,@(map (lambda (slot)
  93. (if (pair? slot)
  94. `(cons ',(car slot) ,(cadr slot))
  95. `',slot))
  96. slots)))
  97. (constructor (record-constructor ,name)))
  98. (lambda args
  99. (apply constructor (%compute-initargs args slots)))))
  100. (define ,(symbol-append stem '?) (record-predicate ,name))
  101. ,@(map (lambda (sname)
  102. `(define ,(symbol-append stem '- sname)
  103. (make-procedure-with-setter
  104. (record-accessor ,name ',sname)
  105. (record-modifier ,name ',sname))))
  106. slot-names))))
  107. (define (%compute-initargs args slots)
  108. (define (finish out)
  109. (map (lambda (slot)
  110. (let ((name (if (pair? slot) (car slot) slot)))
  111. (cond ((assq name out) => cdr)
  112. ((pair? slot) (cdr slot))
  113. (else (error "unbound slot" args slots name)))))
  114. slots))
  115. (let lp ((in args) (positional slots) (out '()))
  116. (cond
  117. ((null? in)
  118. (finish out))
  119. ((keyword? (car in))
  120. (let ((sym (keyword->symbol (car in))))
  121. (cond
  122. ((and (not (memq sym slots))
  123. (not (assq sym (filter pair? slots))))
  124. (error "unknown slot" sym))
  125. ((assq sym out) (error "slot already set" sym out))
  126. (else (lp (cddr in) '() (acons sym (cadr in) out))))))
  127. ((null? positional)
  128. (error "too many initargs" args slots))
  129. (else
  130. (lp (cdr in) (cdr positional)
  131. (let ((slot (car positional)))
  132. (acons (if (pair? slot) (car slot) slot)
  133. (car in)
  134. out)))))))
  135. ;; So, dear reader. It is pleasant indeed around this fire or at this
  136. ;; cafe or in this room, is it not? I think so too.
  137. ;;
  138. ;; This macro used to generate code that looked like this:
  139. ;;
  140. ;; `(((record-predicate ,record-type) ,r)
  141. ;; (let ,(map (lambda (slot)
  142. ;; (if (pair? slot)
  143. ;; `(,(car slot) ((record-accessor ,record-type ',(cadr slot)) ,r))
  144. ;; `(,slot ((record-accessor ,record-type ',slot) ,r))))
  145. ;; slots)
  146. ;; ,@body)))))
  147. ;;
  148. ;; But this was a hot spot, so computing all those predicates and
  149. ;; accessors all the time was getting expensive, so we did a terrible
  150. ;; thing: we decided that since above we're already defining accessors
  151. ;; and predicates with computed names, we might as well just rely on that fact here.
  152. ;;
  153. ;; It's a bit nasty, I agree. But it is fast.
  154. ;;
  155. ;;scheme@(guile-user)> (with-statprof #:hz 1000 #:full-stacks? #t (resolve-module '(oop goops)))% cumulative self
  156. ;; time seconds seconds name
  157. ;; 8.82 0.03 0.01 glil->assembly
  158. ;; 8.82 0.01 0.01 record-type-fields
  159. ;; 5.88 0.01 0.01 %compute-initargs
  160. ;; 5.88 0.01 0.01 list-index
  161. ;;; So ugly... but I am too ignorant to know how to make it better.
  162. (define-syntax record-case
  163. (lambda (x)
  164. (syntax-case x ()
  165. ((_ record clause ...)
  166. (let ((r (syntax r))
  167. (rtd (syntax rtd)))
  168. (define (process-clause tag fields exprs)
  169. (let ((infix (trim-brackets (syntax->datum tag))))
  170. (with-syntax ((tag tag)
  171. (((f . accessor) ...)
  172. (let lp ((fields fields))
  173. (syntax-case fields ()
  174. (() (syntax ()))
  175. (((v0 f0) f1 ...)
  176. (acons (syntax v0)
  177. (datum->syntax x
  178. (symbol-append infix '- (syntax->datum
  179. (syntax f0))))
  180. (lp (syntax (f1 ...)))))
  181. ((f0 f1 ...)
  182. (acons (syntax f0)
  183. (datum->syntax x
  184. (symbol-append infix '- (syntax->datum
  185. (syntax f0))))
  186. (lp (syntax (f1 ...))))))))
  187. ((e0 e1 ...)
  188. (syntax-case exprs ()
  189. (() (syntax (#t)))
  190. ((e0 e1 ...) (syntax (e0 e1 ...))))))
  191. (syntax
  192. ((eq? rtd tag)
  193. (let ((f (accessor r))
  194. ...)
  195. e0 e1 ...))))))
  196. (with-syntax
  197. ((r r)
  198. (rtd rtd)
  199. ((processed ...)
  200. (let lp ((clauses (syntax (clause ...)))
  201. (out '()))
  202. (syntax-case clauses (else)
  203. (()
  204. (reverse! (cons (syntax
  205. (else (error "unhandled record" r)))
  206. out)))
  207. (((else e0 e1 ...))
  208. (reverse! (cons (syntax (else e0 e1 ...)) out)))
  209. (((else e0 e1 ...) . rest)
  210. (syntax-violation 'record-case
  211. "bad else clause placement"
  212. (syntax x)
  213. (syntax (else e0 e1 ...))))
  214. ((((<foo> f0 ...) e0 ...) . rest)
  215. (lp (syntax rest)
  216. (cons (process-clause (syntax <foo>)
  217. (syntax (f0 ...))
  218. (syntax (e0 ...)))
  219. out)))))))
  220. (syntax
  221. (let* ((r record)
  222. (rtd (struct-vtable r)))
  223. (cond processed ...)))))))))
  224. ;; Here we take the terrorism to another level. Nasty, but the client
  225. ;; code looks good.
  226. (define-macro (transform-record type-and-common record . clauses)
  227. (let ((r (gensym))
  228. (rtd (gensym))
  229. (type-stem (trim-brackets (car type-and-common))))
  230. (define (make-stem s)
  231. (symbol-append type-stem '- s))
  232. (define (further-predicates x record-stem slots)
  233. (define (access slot)
  234. `(,(symbol-append (make-stem record-stem) '- slot) ,x))
  235. (let lp ((in slots) (out '()))
  236. (cond ((null? in) out)
  237. ((pair? (car in))
  238. (let ((slot (caar in))
  239. (arg (cadar in)))
  240. (cond ((symbol? arg)
  241. (lp (cdr in) out))
  242. ((pair? arg)
  243. (lp (cdr in)
  244. (append (further-predicates (access slot)
  245. (car arg)
  246. (cdr arg))
  247. out)))
  248. (else (lp (cdr in) (cons `(eq? ,(access slot) ',arg)
  249. out))))))
  250. (else (lp (cdr in) out)))))
  251. (define (let-clauses x record-stem slots)
  252. (define (access slot)
  253. `(,(symbol-append (make-stem record-stem) '- slot) ,x))
  254. (let lp ((in slots) (out '()))
  255. (cond ((null? in) out)
  256. ((pair? (car in))
  257. (let ((slot (caar in))
  258. (arg (cadar in)))
  259. (cond ((symbol? arg)
  260. (lp (cdr in)
  261. (cons `(,arg ,(access slot)) out)))
  262. ((pair? arg)
  263. (lp (cdr in)
  264. (append (let-clauses (access slot)
  265. (car arg)
  266. (cdr arg))
  267. out)))
  268. (else
  269. (lp (cdr in) out)))))
  270. (else
  271. (lp (cdr in)
  272. (cons `(,(car in) ,(access (car in))) out))))))
  273. (define (transform-expr x)
  274. (cond ((not (pair? x)) x)
  275. ((eq? (car x) '->)
  276. (if (= (length x) 2)
  277. (let ((form (cadr x)))
  278. `(,(symbol-append 'make- (make-stem (car form)))
  279. ,@(cdr type-and-common)
  280. ,@(map (lambda (y)
  281. (if (and (pair? y) (eq? (car y) 'unquote))
  282. (transform-expr (cadr y))
  283. y))
  284. (cdr form))))
  285. (error "bad -> form" x)))
  286. (else (cons (car x) (map transform-expr (cdr x))))))
  287. (define (process-clause clause)
  288. (if (eq? (car clause) 'else)
  289. clause
  290. (let ((stem (caar clause))
  291. (slots (cdar clause))
  292. (body (cdr clause)))
  293. (let ((record-type (symbol-append '< (make-stem stem) '>)))
  294. `((and (eq? ,rtd ,record-type)
  295. ,@(reverse (further-predicates r stem slots)))
  296. (let ,(reverse (let-clauses r stem slots))
  297. ,@(if (pair? body)
  298. (map transform-expr body)
  299. '((if #f #f)))))))))
  300. `(let* ((,r ,record)
  301. (,rtd (struct-vtable ,r))
  302. ,@(map (lambda (slot)
  303. `(,slot (,(make-stem slot) ,r)))
  304. (cdr type-and-common)))
  305. (cond ,@(let ((clauses (map process-clause clauses)))
  306. (if (assq 'else clauses)
  307. clauses
  308. (append clauses `((else (error "unhandled record" ,r))))))))))