-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.lisp
590 lines (525 loc) · 18 KB
/
utilities.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
;;;; utilities.lisp
(in-package :utilities)
;;; "utilities" goes here. Hacks and glory await!
(defmacro var
(&rest vars)
(cons 'progn
(loop for v in (partition vars 2)
collect `(defparameter ,@v))))
(defmacro set!
(&rest vars)
(cons 'progn
(loop for v in (partition vars 2)
collect `(setf ,@v))))
(defmacro if-not (bool true &optional (false nil))
`(if (not ,bool) ,true ,false))
(defmacro fn (args &body body)
`(lambda ,args ,@body))
(defun ask (string)
(princ string *query-io*)
(read *query-io*))
(defmacro ignore-warnings
(&rest code)
`(locally
(declare #+sbcl(sb-ext:muffle-conditions sb-kernel:redefinition-warning))
(declare #+sbcl(sb-ext:muffle-conditions style-warning))
(handler-bind
(#+sbcl(sb-kernel:redefinition-warning #'muffle-warning))
,@code)))
(defmacro def-generic (name args)
(fmakunbound name) ;makes sure that this can be redifined!
`(defgeneric ,name
,(append args))) ; '(&key)
(defmacro ->
(init &body exps)
`(arrow-macros:-> ,init ,@exps))
(defgeneric length-of (coll))
(defmethod length-of ((hash hash-table))
(loop
with counter = 0
for key being the hash-keys of hash
do (setf counter (+ 1 counter))
finally (return counter)))
;;TODO cleanup overrides when you get better at lisp, there is too much redundant code
;;TODO should renaming be possible?
(defmacro override-getter
(obj method-name code &key re-name)
(if re-name
(progn
(fmakunbound method-name)
`(defmethod ,re-name
(,(list obj obj))
,(reverse (cons code
(reverse (create-with-accessor-for-overrides-and-setters obj))))))
`(defmethod ,method-name
(,(list obj obj))
,(reverse (cons code
(reverse (create-with-accessor-for-overrides-and-setters obj)))))))
(var *formatted-code-string* "")
;TODO NO SYMZ EXPOERTED
;no exporting syms '(conc-list-to-fcs *formatted-code-string* word paren whitespace number random-sym)
(defun conc-list-to-fcs
(word)
(setf *formatted-code-string*
(concatenate 'string
*formatted-code-string*
(loop
with str = ""
for char in word
do (if (consp char)
()
(setf str (concatenate 'string
str
(string char))))
finally (return str)))))
(defrule word
(+ (alpha-char-p character))
(:lambda (word)
(conc-list-to-fcs word)))
(defrule number
(+ (or (or "1" "2" "3" "4" "5" "6" "7" "8" "9" "0") "." "/"))
(:lambda (num)
(conc-list-to-fcs num)))
(defrule paren
(or #\( #\))
(:lambda (p)
(setf *formatted-code-string*
(concatenate 'string
*formatted-code-string*
(string p)))))
(defrule whitespace
(+ (or #\space #\tab #\newline))
(:lambda (ws)
(conc-list-to-fcs ws)))
(defrule random-sym
character
(:lambda (c)
(setf *formatted-code-string*
(concatenate 'string
*formatted-code-string*
(string c)))))
(defrule anything-but-quote (not #\"))
(defrule string-rule
(and #\" (+ anything-but-quote) #\")
(:lambda (c)
(loop
for elm in c
do (if (not (consp elm))
(setf *formatted-code-string*
(concatenate 'string
*formatted-code-string*
(to-string elm)))
(conc-list-to-fcs elm)))))
(defun swap-symbol
(text word-to-replace word)
(setf *formatted-code-string* "")
(eval `(defrule swap-word ,(write-to-string word-to-replace :case :downcase)
(:lambda (w)
(declare (ignore w))
(setf *formatted-code-string*
(concatenate 'string
*formatted-code-string*
,(write-to-string word :case :downcase))))))
(parse '(+ (or swap-word string-rule whitespace paren word number random-sym))
(write-to-string text :case :downcase))
(read-from-string *formatted-code-string*))
;;available words: value
(defmacro override-setter
(obj method-name code)
`(defmethod (setf ,method-name)
(value ,(list obj obj))
,(reverse (cons (swap-symbol code method-name `(slot-value ,obj ',method-name))
(reverse (create-with-accessor-for-overrides-and-setters obj))))
,obj))
(defmacro def-method
(name class args &rest body)
(let* ((args (setf args (append (list (list class class)) args))))
;(closer-mop:ensure-finalized (find-class class))
`(defmethod ,name
,args
(with-accessors ,(loop for x in (slots-of class) collect (list x (to-keyword x)))
,class
,@body))))
(def-generic fetch (coll item-to-fetch))
; (def-method fetch ((coll hash-table) k)
; (gethash k coll))
;;;!!! code for dependency initialization, conform it to the new system!
;;temporarily creating a new implimentation
;;TODO remove inlining, make raw slots instead
;(let ((raw-class-private (all-permutations '(:raw :private :class)))
; (raw-class-nil (all-permutations '(:raw :class nil)))
; (raw-private-nil (all-permutations '(:raw :private nil)))
; (private-class-nil (all-permutations '(:private :class nil)))
; (raw-nil-nil '((:raw nil nil) (nil :raw nil) (nil nil :raw)))
; (private-nil-nil '((:private nil nil) (nil :private nil) (nil nil :private)))
; (class-nil-nil '((:class nil nil) (nil :class nil) (nil nil :class)))
; (nil-nil-nil '(nil nil nil)))
; (interpose raw-class-private '(setf :initarg)))
(defmacro def-class
(name &key slots with)
(multiple-value-bind (symbols values) (loop
for (symbol value) in slots
collect symbol into symbols
collect value into vals
finally (return (values symbols vals)))
;define a function which will instantiate this object
`(progn (defun ,name
(&key ,@(partition (interleave symbols values) 2))
(make-instance ',name ,@(interleave (mapcar #'to-keyword symbols) symbols)))
,(append `(defclass ,name ,with)
(list (loop
for (symbol value) in slots
collect `(,symbol :initarg ,(to-keyword symbol)
:initform ,value
:accessor ,(to-keyword symbol))))))))
;(defmacro def-class
; (name &key extends slots constructor dependencies)
;
; (eval `(defclass ,name ,extends ,(loop for (slot value opt1 opt2) in slots collect slot)))
;
; (closer-mop:finalize-inheritance (find-class name))
;
; (setf (gethash name *class*) t)
;
;
; (if (and (nil? slots)
; (nil? extends))
; (warn (to-string name " has no slots...")))
;
; (if constructor
; (progn (if (not (eq (first constructor) 'lambda))
; (error "constructor must be a lambda expression"))
; (setf (gethash name *super-args*) (second constructor))))
;
; (if dependencies
; (setf (gethash name *class-dependencies*) dependencies))
;
; (if extends
; (setf (gethash name *hierarchy*) extends))
;
; (loop
; with objs-with-deps = (loop
; for obj in extends
; collect (if (gethash obj *class-dependencies*)
; (list obj (gethash obj *class-dependencies*))))
;
; with objs-to-check = (append extends (list name))
;
; for (object dependencies) in objs-with-deps
; do (loop
; for dep in dependencies
; do (if (not (in? objs-to-check dep))
; (error (to-string "The class, "
; object
; ", depends upon, "
; dep
; ", which is not this class or any class that "
; name
; " extends!")))))
;
; (setf extends (append extends '(printer-base)))
;
; (let* ((slots-and-values slots)
; (constructor-code nil)
; (hierarchy (find-hierarchy name))
; (sub-classes (sub-classes-of name))
; (all-classes-being-mixed (append hierarchy (list name)))
;
; (constructor-args (append (-> (loop
; for obj in hierarchy
; when (not (eq obj 'printer-base))
; collect (loop for slot in (gethash obj *super-args*)
; collect slot))
; flatten)
; (first (rest constructor))))
; (constructor-requirements constructor-args)
; (constructor-body (cddr constructor)))
;
; (loop
; for sub-class in sub-classes
; do (progn
; (loop
; for sub-slot in (slots-of sub-class)
; do (loop
; for (slot _) in slots
; do (if (eq slot sub-slot)
; (warn (to-string "The redifinition of "
; name
; " is now shadowing slot "
; slot
; " in the subclass "
; sub-class)))))
; (loop
; for sub-arg in (gethash sub-class *super-args*)
; do (loop
; for arg in (gethash name *super-args*)
; do (if (eq sub-arg arg)
; (warn (to-string "The redifinition of "
; name
; " is now shadowing the constructor arguement "
; arg
; " in the subclass "
; sub-class)))))))
;
; ;warn user when the constructor arguements from various classes are shadowing each other
;
; (loop
; with already-warned = '()
; for current-class in all-classes-being-mixed
; do (let* ((current-args (gethash current-class *super-args*))
; (current-slots (slots-of current-class)))
; (loop
; for class in all-classes-being-mixed
; when (and (not (eq class current-class))
; (not (in? already-warned class)))
; do (loop
; with slots = (slots-of class)
; for current-arg in current-args
; for current-slot in current-slots
; do (loop
; for slot in slots
; do (if (and (eq slot current-slot)
; (not (eq slot 'name)))
; (warn (to-string "the classes "
; current-class
; " and "
; class
; " are shadowing each other's slot: "
; slot))))))
; (loop
; for class in all-classes-being-mixed
; when (and (not (eq class current-class))
; (not (in? already-warned class)))
; do (loop
; with args = (gethash class *super-args*)
; for current-arg in current-args
; for current-slot in current-slots
; do (loop
; for arg in args
; do (if (eq arg current-arg)
; (warn (to-string "the classes "
; current-class
; " and "
; class
; " are shadowing each other's constructor arg: "
; arg))))))
; (setf already-warned (append already-warned (list current-class)))))
;
;
; ;warn user when slots from various classes are shadowing each other
;
; ;get rid of duplicates in the constructor args, all of the supers will share the same arguement
; (setf constructor-args (mapcar #'to-symbol (remove-duplicates (mapcar #'to-keyword constructor-args))))
; (setf constructor-code
; `(defmethod initialize-instance
; :after
; ((,name ,name) &key ,@constructor-args)
; ,@constructor-body
; (when (next-method-p)
; (call-next-method))))
;
; ;TODO this needs fixing, patterns are a fail for now
; (let* ((class nil)
; (export-list '()))
; (multiple-value-bind (slots-and-values setup-interface getters setters)
; (loop for (slot value opt1 opt2) in slots-and-values
; for slot-key = (to-keyword slot)
; collect (trivia:match `(,opt1 ,opt2)
; ('(:raw nil) (setf export-list (append export-list (list slot)))
; `(,slot :initarg ,(to-keyword slot) :initform ,value))
; ('(nil :raw) (setf export-list (append export-list (list slot)))
; `(,slot :initarg ,(to-keyword slot) :initform ,value))
; ('(:raw :private) `(,slot :initarg ,(to-keyword slot) :initform ,value))
; ('(:private :raw) `(,slot :initarg ,(to-keyword slot) :initform ,value))
; ('(:private nil) `(,slot :initarg ,(to-keyword slot) :initform ,value :accessor ,slot))
; ('(nil :private) `(,slot :initarg ,(to-keyword slot) :initform ,value :accessor ,slot))
; ('(nil nil) (setf export-list (append export-list (list slot)))
;
; `(,slot :initarg ,(to-keyword slot) :initform ,value :accessor ,slot-key))
; (otherwise (error
; (to-string "Error! Slot options are incorrect...
; expected option of :raw, :private, not kek"))))
; into slots-and-values
; collect (if (not (gethash slot-key *keys-that-exist-already*))
; `(progn
; ,(setf (gethash slot-key *keys-that-exist-already*) t)
; (declaim (inline ,slot-key))
; (defgeneric (setf ,slot-key)
; (value object-map-or-vector)
; (:generic-function-class inlined-generic-function:inlined-generic-function))
; (defgeneric ,slot-key
; (object-map-or-vector)
; (:generic-function-class inlined-generic-function:inlined-generic-function))))
; into setup-interface
; collect `(defmethod ,slot-key ((,name ,name)) (slot-value ,name ',slot))
; into getters
; collect `(defmethod (setf ,slot-key) (value (,name ,name)) (setf (slot-value ,name ',slot) value))
; into setters
; finally (return (values slots-and-values setup-interface getters setters)))
;
;
;
;
; (setf class `(defclass ,name ,extends ,slots-and-values))
;
; `(ignore-warnings
; (eval-when (:compile-toplevel :execute :load-toplevel)
; (progn
; ,class
; ;,@setup-interface
; ;,@getters
; ;,@setters
; ,constructor-code
;
;
; (defun ,name
; (&key ,@constructor-args)
;
; (if ',dependencies
; (error "This class has dependencies, it cannot be instantiated directly"))
;
; (if ',constructor-requirements
; (loop
; for arg in ',constructor-requirements
; do (if (eq nil arg)
; (warn (format nil "constructor arguement: ~a, is nil" arg)))))
;
; (make-instance ',name
; ,@(loop
; with code = '()
; for elm in
; (loop
; for (arg value) in (partition
; (interleave constructor-args
; constructor-args)
; 2)
; when (not (nil? value))
; collect (list (to-keyword arg) value))
; do (setf code (append code elm))
; finally (return code))))
; (export ',name)
; ',name)))))))
(defmacro -=
(var value)
`(setf ,var (- ,var ,value)))
(defmacro +=
(var value)
`(setf ,var (+ ,var ,value)))
(defmacro set-slots
(obj &rest slots-and-values)
;;finalizing inheritance every time we set slots of an object...
;;ensure finalize
(append `(progn (closer-mop:ensure-finalized (find-class ',obj)))
(loop for (slot value) in (partition slots-and-values 2)
collect `(setf (,slot ,obj) ,value))))
(defmacro defun-fast
(name args &rest code)
(multiple-value-bind (defun-args types-code)
(generate-type-information args)
`(progn (declaim (inline ,name))
(defun ,name
,defun-args
(declare (optimize (speed 3) (safety 0)))
,@types-code
,@code))))
;; TODO implement this
;; fast defmethods do not use accessors, are inlined, and they are not generic, use them only in speed
;; critical situations
(defmacro def-method-fast
(name args &rest code)
;;TODO FINISH THIS!!11
`(defun-fast ,name ,args ,@code))
;export every symbol in the current package except the ones listed package
(defmacro export-all-symbols-except
(&rest private-symbol-list)
`(let ((pack (find-package *PACKAGE*)))
(do-all-symbols (sym pack) (when (and (not (in? ',private-symbol-list sym))
(eql (symbol-package sym) pack))
(export sym)))))
;hash-table-macro
(defun read-separator (str)
(let
((*readtable* (copy-readtable *readtable* nil)))
(set-macro-character #\, (lambda (stream char)
(declare (ignore char) (ignore stream))
'break))
(read str nil)))
(set-macro-character #\{
(lambda (str char)
(declare (ignore char))
(let
((*readtable* (copy-readtable *readtable* nil)))
(set-macro-character #\} (lambda (stream char)
(declare (ignore char) (ignore stream))
'end))
(let
((pairs (loop for key = (read str nil nil t)
for sep = (read str nil nil t)
for value = (read str nil nil t)
for end? = (read-separator str)
do (when (not (eql '=> sep)) (error "Expected =>, did not get"))
do (when (not (or (eql 'end end?) (eql 'break end?))) (error "Expected , or }"))
collect (list key value)
while (not (eql 'end end?))))
(retn (gensym)))
`(let
((,retn (make-hash-table :test #'equal)))
,@(mapcar
(lambda (pair)
`(setf (gethash ,(car pair) ,retn) ,(cadr pair)))
pairs)
,retn)))))
(set-pprint-dispatch 'hash-table
(lambda (str ht)
(format str "{~{~{~S => ~S~}~^, ~}}"
(loop for key being the hash-keys of ht
for value being the hash-values of ht
collect (list key value)))))
;create instance struct
;twerking
;defun redefinition exports symbol automatically
(defmacro defn (name args &rest body)
`(progn (defun ,name ,args ,@body)
(export ',name)))
;private version
(defmacro defn- (name args &rest body)
`(defun ,name ,args ,@body))
;defmacro redefinition exports symbol automatically
(defmacro def-spel (name args &rest body)
`(progn (defmacro ,name ,args ,@body)
;(export ',name)
))
;private version
(defmacro def-spel- (name args &rest body)
`(defmacro ,name ,args ,@body))
(defmacro make-map (&rest args)
(let* ((map (make-hash-table))
(args (loop
for (key val) in (partition args 2)
collect (list key val)))
(generate-keyword-functions
(loop
for (key val) in args
do (eval `(setf (gethash ,key ,map) ',val))
collect (if (keywordp key)
`(defmethod ,key ((hash hash-table)) (gethash ,key hash))))))
(locally
(declare #+sbcl(sb-ext:muffle-conditions sb-kernel:redefinition-warning))
`(handler-bind
(#+sbcl(sb-kernel:redefinition-warning #'muffle-warning))
(progn ,@generate-keyword-functions
,(if (nil? args)
'(make-hash-table)
(read-from-string
(loop
with form = "{"
with switch = nil
for (key val) in args
do (progn
(if switch
(setf form (concatenate 'string form (to-string ", " key " => " val)))
(setf form (concatenate 'string form (to-string key " => " val))))
(setf switch t))
finally (return (to-string form "}"))))))))))
;NOTHING GOES PAST THIS.
(export-all-symbols-except nil)