-
Notifications
You must be signed in to change notification settings - Fork 5
/
reader-macros.lisp
801 lines (670 loc) · 25.2 KB
/
reader-macros.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
;; useful_macros.lisp -- A collection of really handy macros
;;
;; DM/HMSC 11/97
;; -----------------------------------------------------------
(in-package "USEFUL-MACROS")
;; -----------------------------------------------------------
#+:lispworks
(progn
(editor:setup-indent "nlet" 2)
(editor:setup-indent "nlet-tail" 2)
(editor:setup-indent "if-let" 2 2 4)
(editor:setup-indent "when-let" 1)
(editor:setup-indent "with-gensyms" 2 2 4)
(editor:setup-indent "with-tail-pure-code" 2 2 4)
(editor:setup-indent "aif" 2 2 4)
(editor:setup-indent "aif*" 2 2 4)
(editor:setup-indent "awhen" 2 2 4)
(editor:setup-indent "alet" 2)
(editor:setup-indent "alet-fsm" 2)
(editor:setup-indent "arun-fsm" 2 2 4)
(editor:setup-indent "with-slot-accessors" 2 2 4)
(editor:setup-indent "define-bind*-handler" 2 2)
(editor:setup-indent "defmacro!" 2)
(editor:setup-indent "defpan" 2 2 4)
;; (editor:setup-indent "dlambda" 2 2 4)
(editor:setup-indent "dlambda" 0 2 0 t)
(editor:setup-indent "plambda" 2 2 4)
(editor:setup-indent "ichain-before" 2 2 4)
(editor:setup-indent "ichain-after" 2 2 4)
(editor:setup-indent "ichain-intercept" 2 2 4)
(editor:setup-indent "alet-hotpatch" 2 2 4)
(editor:setup-indent "let-hotpatch" 2 2 4)
(editor:setup-indent "sublet" 2 2 4)
(editor:setup-indent "sublet*" 2 2 4)
(editor:setup-indent "pandoriclet" 2 2 4)
(editor:setup-indent "with-pandoric" 2 2 4)
(editor:setup-indent "defpan" 2)
(editor:setup-indent "dis" 2 2 4)
(editor:setup-indent "with-fast-stack" 2 2 4)
(editor:setup-indent "with-conses-counted" 2 2 4)
(editor:setup-indent "with-cons-pool" 2 2 4)
(editor:setup-indent "with-dynamic-cons-pool" 2 2 4)
(editor:setup-indent "nif" 2 2 4)
(editor:setup-indent "fast-progn" 2 2 4)
(editor:setup-indent "safe-progn" 2 2 4)
(editor:setup-indent "curried-lambda" 1)
)
;; ------------------------------------------------------
;; -----------------------------------------------------------
;; tools needed to support Doug Hoyte's DEFMACRO!
(eval-when (:compile-toplevel :load-toplevel :execute)
(defmacro perform (name bindings &body body)
(let ((args (mapcar 'first bindings))
(vals (mapcar 'second bindings)))
`(labels ((,name ,args ,@body))
(,name ,@vals))
))
(defmacro nlet (name bindings &body body)
;; NLET = Named LET
`(perform ,name ,bindings ,@body))
(defun flatten (x)
(nlet rec ((x x)
(acc nil))
(cond ((null x) acc)
((atom x) (cons x acc))
(t (rec (car x) (rec (cdr x) acc))))
))
;; in ML these are referred to as sections
;; these actually correspond to the Dylan operators
;; secr ::= rcurry, secl ::= curry
(defun curry (fn &rest pref-args)
(lambda (&rest suf-args)
(apply fn (append pref-args suf-args))))
(defun rcurry (fn &rest suf-args)
(lambda (&rest pref-args)
(apply fn (nconc pref-args suf-args))))
(defun collect-if (predicate proseq &rest rest)
(apply 'remove-if-not predicate proseq rest))
;; --------------------------------------------
;; Bang-symbols
(defun bang-symbol-p (prefix s)
(and (symbolp s)
(> (length (symbol-name s)) 2)
(string= (symbol-name s) prefix
:start1 0
:end1 2)))
(defun get-bang-symbols (prefix body)
(remove-duplicates
(collect-if (curry 'bang-symbol-p prefix) (flatten body))))
(defun bang-symbol-name (s)
(subseq (symbol-name s) 2))
;; --------------------------------------------
(defun raw-mkstr (&rest args)
(with-output-to-string (s)
(dolist (a args)
(princ a s))
))
(defun mkstr (&rest args)
(with-standard-io-syntax
(apply 'raw-mkstr args)))
(defun correct-for-symbol-character-case (str)
;; a portable way to make symbol strings
;; Modern Mode vs ANSI
(if (eql #\a (char (string :a) 0))
(string-downcase (string str))
(string-upcase (string str))))
(defun intern-symbol (str &rest package)
(apply 'intern (correct-for-symbol-character-case str) package))
(defun symb (&rest args)
(values (intern-symbol (apply 'mkstr args))))
;; --------------------------------------------
;; Reader macro for #` for producing parameterized BQ lists
;; Produces a function that can be applied to arguments
(defun |reader-for-#`| (stream sub-char numarg)
(declare (ignore sub-char))
(unless numarg (setq numarg 1))
(let ((a-args (loop for i from 1 to numarg
collect (symb 'a i))))
`(lambda ,a-args
(declare (ignorable ,@a-args))
,(funcall
(get-macro-character #\`) stream nil))))
(set-dispatch-macro-character
#\# #\` '|reader-for-#`|)
) ;; end of eval-when #1
#| ;; example -- a1 is first parameter
(mapcar #`(,a1 (intern ,(bang-symbol-name a1))) syms)
|#
(eval-when (:compile-toplevel :load-toplevel :execute)
;; --------------------------------------------
;; A-Bang symbols -- create anaphoric symbol names
;; in package of macro expansion
(defmacro defmacro/a! (name args &body body)
(let ((syms (get-bang-symbols #.(symbol-name :A!) body)))
(if syms
`(defmacro ,name ,args
(let ,(mapcar #`(,a1 (intern ,(bang-symbol-name a1))) syms)
,@body))
`(defmacro ,name ,args ,@body))))
;; --------------------------------------------
;; G-Bang symbols -- auto generated gensyms
(defmacro defmacro/g! (name args &body body)
(let ((syms (get-bang-symbols #.(symbol-name :G!) body)))
(if syms
`(defmacro/a! ,name ,args
(let ,(mapcar #`(,a1 (gensym ,(bang-symbol-name a1))) syms)
,@body))
`(defmacro/a! ,name ,args ,@body))))
;; --------------------------------------------
;; O-Bang symbols -- once-only eval gensyms
(defun o!-symbol-to-g!-symbol (s)
(symb #.(symbol-name :G!)
(bang-symbol-name s)))
(defmacro defmacro! (name args &body body)
(let* ((os (get-bang-symbols #.(symbol-name :O!) args))
(gs (mapcar 'o!-symbol-to-g!-symbol os)))
;; o-bang symbols can interfere with find-source
(if os
`(defmacro/g! ,name ,args
`(let ,(mapcar 'list (list ,@gs) (list ,@os))
,(progn
,@body)))
`(defmacro/g! ,name ,args ,@body)) ))
;; ---------------------------------------
;; This part from Doug Hoyte using Edi's ppcre
(defun segment-reader (stream ch n)
(if (> n 0)
(let ((chars))
(do ((curr (read-char stream)
(read-char stream)))
((char= ch curr))
(push curr chars))
(cons (coerce (nreverse chars) 'string)
(segment-reader stream ch (- n 1))))))
;; ---------------------------------------
#+cl-ppcre
(defmacro! match-mode-ppcre-lambda-form (o!args)
``(lambda (,',g!str)
(cl-ppcre:scan
,(car ,g!args)
,',g!str)))
#+cl-ppcre
(defmacro! subst-mode-ppcre-lambda-form (o!args)
``(lambda (,',g!str)
(cl-ppcre:regex-replace-all
,(car ,g!args)
,',g!str
,(cadr ,g!args))))
;; Reader macro for #~ for pattern matching/substitution
;; Produces a function that can be applied to strings
#+cl-ppcre
(defun |reader-for-#~| (stream sub-char numarg)
(declare (ignore sub-char numarg))
(let ((mode-char (read-char stream)))
(cond
((char= mode-char #\m)
(match-mode-ppcre-lambda-form
(segment-reader stream
(read-char stream)
1)))
((char= mode-char #\s)
(subst-mode-ppcre-lambda-form
(segment-reader stream
(read-char stream)
2)))
(t (error "Unknown #~~ mode character")))))
#+cl-ppcre
(set-dispatch-macro-character #\# #\~ '|reader-for-#~|)
#| ;; example
;; pattern matching
(#~m/^[+-]?[0-9][0-9_,]*(\.[0-9_,]*([eEdD][+-]?[0-9]+)?)?/ s)
;; pattern substitution
(#~s/[0-9]/N/ s)
|#
) ;; end of eval-when #2
;; --------------------------------------------------------------
;; Allow extended number syntax:
;; - embedded underscore separators 123_445.789_443
;; - allow 1+2j or 1-2j or just 2j, where j in [jJiI]
;; - allow dates in yyyy/mm/dd format
;; - allow sexigisimal time in |hh:mm:ss.ss| format (bars needed because of #\:)
;; - allow hyphenated numbers as in telephone numbers, SSN's, and UUID's
(defun remove-separators (s)
(delete #\, (delete #\_ s)))
(defun match-number (s)
(multiple-value-bind (start end)
(#~m/^[+-]?[0-9][0-9_,]*(\.[0-9_,]*([eEdD][+-]?[0-9]+)?)?/ s)
(when start
(values (read-from-string (remove-separators (subseq s start end)))
(subseq s end))
)))
(defun match-complex-ij (s)
(#~m/^[iIjJ]$/ s))
(defun convert-real-or-complex (s)
(multiple-value-bind (val srest)
(match-number s)
(when val
(cond ((= 0 (length srest)) val)
((match-complex-ij srest) (complex 0 val))
((multiple-value-bind (ival sresti)
(match-number srest)
(and ival
(match-complex-ij sresti)
(complex val ival))))
(t nil)))
))
(defun convert-sexigisimal (s)
;; hh:mm:ss.ss, or hh:mm
(multiple-value-bind (start end gstart gend)
(#~m/^([+-])?([0-9]+):([0-9]{1,2})(:[0-9]{1,2}(\.[0-9_,]*)?)?$/ s)
(declare (ignore end))
(when start
(symbol-macrolet
((sign (aref gstart 0))
(hstart (aref gstart 1))
(hend (aref gend 1))
(mstart (aref gstart 2))
(mend (aref gend 2))
(sstart (aref gstart 3))
(send (aref gend 3))
(sfrac (aref gstart 4)))
(ignore-errors
(let* ((hh (read-from-string (subseq s hstart hend)))
(mm (read-from-string (subseq s mstart mend)))
(ss (if sstart
(read-from-string (remove-separators
(subseq s (1+ sstart) send)))
0))
(val (+ (* 60 (+ (* 60 hh) mm))
(if sfrac
(float ss 1d0)
ss))))
(if (and sign
(char= (char s sign) #\-))
(- val)
val)
))))))
(defun convert-utc-date (s)
;; yyyy/mm/dd
(multiple-value-bind (start end gstart gend)
(#~m%^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2}) UTC$% s)
(declare (ignore end))
(when start
(symbol-macrolet
((ystart (aref gstart 0))
(yend (aref gend 0))
(mstart (aref gstart 1))
(mend (aref gend 1))
(dstart (aref gstart 2))
(dend (aref gend 2)))
(ignore-errors
(let* ((yyyy (read-from-string (subseq s ystart yend)))
(mm (read-from-string (subseq s mstart mend)))
(dd (read-from-string (subseq s dstart dend))))
(encode-universal-time 0 0 0 dd mm yyyy 0)) ;; makes UTC date
)))))
(defun convert-date (s)
;; yyyy/mm/dd
(multiple-value-bind (start end gstart gend)
(#~m%^([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})$% s)
(declare (ignore end))
(when start
(symbol-macrolet
((ystart (aref gstart 0))
(yend (aref gend 0))
(mstart (aref gstart 1))
(mend (aref gend 1))
(dstart (aref gstart 2))
(dend (aref gend 2)))
(ignore-errors
(let* ((yyyy (read-from-string (subseq s ystart yend)))
(mm (read-from-string (subseq s mstart mend)))
(dd (read-from-string (subseq s dstart dend))))
(encode-universal-time 0 0 0 dd mm yyyy))
)))))
(defun convert-american-short-date (s)
;; mm/dd/yy
(multiple-value-bind (start end gstart gend)
(#~m%^([0-9]{1,2})/([0-9]{1,2})/([0-9]{1,2})$% s)
(declare (ignore end))
(when start
(symbol-macrolet
((ystart (aref gstart 2))
(yend (aref gend 2))
(mstart (aref gstart 0))
(mend (aref gend 0))
(dstart (aref gstart 1))
(dend (aref gend 1)))
(ignore-errors
(let* ((yyyy (+ 2000 (read-from-string (subseq s ystart yend))))
(mm (read-from-string (subseq s mstart mend)))
(dd (read-from-string (subseq s dstart dend))))
(encode-universal-time 0 0 0 dd mm yyyy)
))))))
(defun convert-hyphenated-number (s)
;; xxxx-xx-xxxx as in telephone numbers, SSN's, and UUID's
(if (#~m/^[0-9]+(\-[0-9]+)*$/ s)
(read-from-string (delete #\- s))))
(defun convert-other-base-number (s)
;; #xNNNN_NNNN_NNN
(when (or (#~m/^0[xXoObB]/ s)
(#~m/^[0-9]+[rR]/ s))
(ignore-errors
(read-from-string (format nil "#~A" (remove-separators s))))))
(defun read-extended-number-syntax (s)
(cond ((convert-real-or-complex s))
((convert-sexigisimal s))
((convert-date s))
((convert-american-short-date s))
((convert-utc-date s))
((convert-hyphenated-number s))
((convert-other-base-number s))
))
;; Reader macro for #N
;; Parses a variety of numbers
(defun |reader-for-#N| (stream sub-char numarg)
(declare (ignore sub-char numarg))
(let ((v (read stream t nil t)))
(if (symbolp v)
(or (read-extended-number-syntax (symbol-name v))
v)
v)))
(set-dispatch-macro-character
#\# #\n '|reader-for-#N|)
#| ;; example
#n1_000
#n|12:45|
#n2009/08/15
#n1+2j
|#
;; --------------------------------------
;; Reader macro for #f
;; sets compiler optimization levels
;; #0f .. #3f #f defaults to #3f for fastest code
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun |reader-for-#F| (stream sub-char numarg)
(declare (ignore stream sub-char))
(setq numarg (or numarg 3))
(unless (<= numarg 3)
(error "Bad value for #f: ~a" numarg))
`(declare (optimize (speed ,numarg)
(safety ,(- 3 numarg))
(float ,(- 3 numarg)))))
(set-dispatch-macro-character #\# #\f '|reader-for-#F|)
) ;; end of eval-when
(defmacro fast-progn (&rest body)
`(locally #f ,@body))
(defmacro safe-progn (&rest body)
`(locally #0f ,@body))
;; --------------------------------------
(defmacro! alet (letargs &rest body)
`(let ((,a!this) ,@letargs)
(setq ,a!this ,@(last body))
,@(butlast body)
(lambda (&rest params)
(apply ,a!this params))))
(defmacro! alet-fsm (&rest states)
`(macrolet ((,a!state (s)
`(setq ,',a!this #',s)))
(labels (,@states) #',(caar states))))
(defmacro! arun-fsm (bindings feeder &rest clauses)
`(block ,g!block
(let ((,g!machine
(alet ,bindings
(macrolet ((,a!finish (val)
`(return-from ,',g!block ,val)))
(alet-fsm ,@clauses)))))
(tagbody
,g!again
(funcall ,g!machine ,feeder)
(go ,g!again))
)))
;; ----------------------------------------------------------------------
;; Nestable suggestion from Daniel Herring
;; rewritten (DM/RAL) using our state-machine macro
;; Nesting fails on patterns like #"#"# where one expects #
(defun |reader-for-#"| (stream sub-char numarg)
(declare (ignore sub-char numarg))
(arun-fsm ((chars (make-rubber-vector ;; initial bindings
:element-type 'character))
(depth 1))
(read-char stream) ;; feeder clause
(normal (curr) ;; state machine clauses - initial first
(cond ((char= curr #\#)
(state read-sharp)
(keep #\#))
((char= curr #\")
(state read-quote))
(t (keep curr)) ))
(read-sharp (curr)
(cond ((char= curr #\")
(state normal)
(keep #\")
(incf depth))
#|
((char= curr #\#)
(keep #\#))
|#
(t
(keep curr)
(state normal)) ))
(read-quote (curr)
(cond ((char= curr #\#)
(state normal)
(decf depth)
(when (zerop depth)
(finish (final-string)))
(keep #\")
(keep #\#))
((char= curr #\")
(keep #\"))
(t (state normal)
(keep #\")
(keep curr)) ))
;; not a state, but becomes a labels clause that can be used
(keep (ch) (vector-push-extend ch chars))
(final-string ()
(prog1
(coerce (subseq chars 0) 'string)
(setf (fill-pointer chars) 0)))
))
(set-dispatch-macro-character
#\# #\" '|reader-for-#"|)
;; --------------------------------------------
;; Reader macro for #>
;; like the Bourne shell > to-lists for surrounding strings
;;
;; This version is from Martin Dirichs
(defun |reader-for-#>| (stream sub-char numarg)
(declare (ignore sub-char numarg))
(let (chars) ;; collect the end tag
(do ((curr (read-char stream)
(read-char stream)))
((char= #\newline curr))
(push curr chars))
(let ((pattern (nreverse chars))
output)
;; collect chars until a sequence of them
;; matches the end-tag
(labels ((match (pos chars)
(if (null chars)
pos
(if (char= (nth pos pattern) (car chars))
(match (1+ pos) (cdr chars))
(match 0 (cdr (append (subseq pattern 0 pos) chars)))))))
(do (curr
(pos 0))
((= pos (length pattern)))
(setf curr (read-char stream)
pos (match pos (list curr)))
(push curr output))
(coerce
(nreverse
(nthcdr (length pattern) output))
'string)))))
(set-dispatch-macro-character
#\# #\> '|reader-for-#>|)
#| ;; example
#>.end
This is a test
of the #> reader macro
.end
|#
;; --------------------------------------------
;; --------------------------------------------
;; Reader macro for #$
;; Takes a list and applies the car as a function to the cdr as parameters
;;
;; This version is from Martin Dirichs
(defvar $-reader-macros (make-hash-table :test 'equalp))
(defun |reader-for-#$| (stream sub-char numarg)
(declare (ignore sub-char numarg))
(let ((inp (read stream t nil t)))
(if (and (consp inp)
(symbolp (car inp)))
(let ((fn (get-$-dispatch-reader (car inp))))
(if fn
(apply fn (cdr inp))
(error "No $-Reader Macro for ~A" (car inp))))
(error "badly formed #$ input: ~A" inp))))
(defun set-$-dispatch-reader (key fn)
(unless (symbolp key)
(error "$-dispatch names must be symbols"))
(setf (gethash (string key) $-reader-macros) fn))
(defun get-$-dispatch-reader (key)
(gethash (string key) $-reader-macros))
(set-dispatch-macro-character
#\# #\$ '|reader-for-#$|)
#|
(set-$-dispatch-reader :test (lambda (&rest data)
(match data
((x) :when (numberp x) (/ x))
(_ (list 'quote data)))))
#$(:test 15)
#$(:test :this)
|#
;; ----------------------------------------------------------
;; Reader for #/
;; Takes a function name and applies to stream following the second '/'
(defvar /-reader-macros (make-hash-table :test 'equalp))
(defun |reader-for-#/| (stream sub-char numarg)
(declare (ignore sub-char numarg))
(let* ((key (first (segment-reader stream #\/ 1)))
(reader (get-/-dispatch-reader key)))
(if reader
(funcall reader stream)
(error "No /-Reader Macro for ~A" key))))
(defun set-/-dispatch-reader (key fn)
(setf (gethash (string key) /-reader-macros) fn))
(defun get-/-dispatch-reader (key)
(gethash (string key) /-reader-macros))
(set-dispatch-macro-character
#\# #\/ '|reader-for-#/|)
#|
(set-/-dispatch-reader "test"
(lambda (stream)
(let ((data (read stream t nil t)))
(match data
((x) :when (numberp x) (/ x))
(_ (list 'quote data))))))
#/test/1.2
#/test/this
|#
;; ---------------------------------------------------
;; Symbol Aliases #?name
;; #?name looks up name in per-package alist and returns cdr symbol
(defvar *symbol-aliases-table* (make-hash-table)) ;; one alist per package
(defun aliases (&optional (package *package*))
(gethash (find-package package) *symbol-aliases-table*))
(defsetf aliases (&optional (package *package*)) (alist)
`(setf (gethash (find-package ,package) *symbol-aliases-table*) ,alist))
(defun lookup-alias (keysym &optional (package *package*))
(let* ((keysym-name (symbol-name keysym))
(package (find-package package)))
(unless (eq package (symbol-package keysym))
(setf keysym (intern keysym-name package)))
(or (cdr (assoc keysym (aliases package)))
(error "No alias named ~A" keysym))))
(defun alias (keysym sym &optional (package *package*))
(unless keysym
(error "Can't alias NIL"))
(let* ((package (find-package package))
(alist (aliases package)))
(unless (eq package (symbol-package keysym))
(setf keysym (intern (symbol-name keysym) package)))
(if sym
(let ((pair (assoc keysym alist)))
(if pair
(setf (cdr pair) sym)
(setf (aliases package) (acons keysym sym alist))) )
(setf (aliases package) (delete keysym alist :key 'first))) ))
(defun unalias (keysym &optional (package *package*))
(alias keysym nil package))
(defun |reader-for-#?| (stream sub-char numarg)
(declare (ignore sub-char numarg))
(let* ((key (read stream t nil t)))
(lookup-alias key)) ) ;; note: can't alias NIL
(set-dispatch-macro-character
#\# #\? '|reader-for-#?|)
#| ;; example
(alias 'this 'that)
(quote #?this)
(aliases)
(unalias 'this)
|#
;; ---------------------------------------------------
#|
;; Package Aliases #!name
;; #!name looks up name in per-package alist and returns cdr symbol
(defvar *symbol-aliases-table* (make-hash-table)) ;; one alist per package
(defun aliases (&optional (package *package*))
(gethash (find-package package) *symbol-aliases-table*))
(defsetf aliases (&optional (package *package*)) (alist)
`(setf (gethash (find-package ,package) *symbol-aliases-table*) ,alist))
(defun lookup-alias (keysym &optional (package *package*))
(let* ((keysym-name (symbol-name keysym))
(package (find-package package)))
#|
(unless (eq package (symbol-package keysym))
(setf keysym (intern keysym-name package)))
|#
(or (cdr (assoc keysym (aliases package)))
(error "No alias named ~A" keysym))))
(defun alias (keysym sym &optional (package *package*))
(unless keysym
(error "Can't alias NIL"))
(let* ((package (find-package package))
(alist (aliases package)))
#|
(unless (eq package (symbol-package keysym))
(setf keysym (intern (symbol-name keysym) package)))
|#
(if sym
(let ((pair (assoc keysym alist)))
(if pair
(setf (cdr pair) sym)
(setf (aliases package) (acons keysym sym alist))) )
(setf (aliases package) (delete keysym alist :key 'first))) ))
(defun unalias (keysym &optional (package *package*))
(alias keysym nil package))
(defun |reader-for-#?| (stream sub-char numarg)
(declare (ignore sub-char numarg))
(let* ((key (read stream t nil t)))
(lookup-alias key)) ) ;; note: can't alias NIL
(set-dispatch-macro-character
#\# #\? '|reader-for-#?|)
#| ;; example
(alias 'this 'that)
(quote #?this)
(aliases)
(unalias 'this)
|#
|#
;; ------------------------------------------------------------
(defun read-chars-till-delim (stream delims &rest first-char)
(let ((chars (copy-list first-char)))
(do ((ch (read-char stream)
(read-char stream)))
((find ch delims))
(push ch chars))
(coerce (nreverse chars) 'string)))
;; -------------------------------------------------------
(defmacro! defaliasfn (new-name old-name)
`(defun ,new-name (&rest ,g!args)
(apply ',old-name ,g!args)))
(defmacro! defcapture (new-name old-name)
`(unless (fboundp ',new-name)
(when (fboundp ',old-name)
(setf (symbol-function ',new-name) (symbol-function ',old-name)))))
;; -------------------------------------------------------