Initial commit
[cl-x86-asm.git] / utils.lisp
blob6376e97594ba04dbdf3051691159cc5f18732b1f
2 (in-package :cl-x86-asm)
4 ;; -- convienence ---------------------------------------------------------------
6 ;;(defun dump-x86-asm-core ()
7 ;; (save-lisp-and-die (merge-pathnames (make-pathname :name "sbcl.x86asm" :type ".core"))))
9 ;;
10 ;; if form is nil, body is evaluated and value returned, otherwise
11 ;; result of evaluating form is returned. Form is evaluated only once
13 (defmacro eval-unless (form &rest body)
14 (let ((form-symbol (gensym)))
15 `(let ((,form-symbol ,form))
16 (if ,form-symbol
17 ,form-symbol
18 ,@body))))
20 ;; given a numeric value, potentially > 256,
21 ;; decompose it to a list of values 0 > v > 256
22 (defun decompose-to-bytes (value)
23 (assert (numberp value))
24 (reverse
25 (loop
26 for x = value then (ash x -8)
27 while (not (zerop x))
28 collect (logand x #XFF))))
30 ;; given a numeric value, potentially > 256,
31 ;; decompose it to a list of values 0 > v > 256
32 (defun decompose-to-n-bytes (value byte-count)
33 (assert (numberp value))
34 (reverse
35 (loop
36 for x = value then (ash x -8)
37 for i from 0 below byte-count
38 collect (logand x #XFF))))
41 ;; seemed like a good idea at the time..don't think I need
42 ;; it now
43 (defmacro predicated-bind ((symbols-and-predicates)
44 value-form &body form)
45 "(predicated-bind ((symbol predicate ..)) value-form body)
46 If the nth element of value-form satisfies the nth predicate, bind it to
47 the nth symbol, else bind it to nil, then evaluate body"
48 `(let
49 ,(loop
50 for (symbol predicate) in symbols-and-predicates
51 for val in value-form
52 collect
53 (list symbol
54 (let ((val-sym (gensym)))
55 `(let ((,val-sym ,val))
56 (when (funcall ,predicate ,val-sym)
57 ,val-sym)))))
58 ,@form))
60 (defun symbol-is-in-package (sym package-name)
61 "(symbol-is-in-package sym package-name) simple predicate to test what it says"
62 (eq (symbol-package sym) (find-package package-name)))
64 (defmacro zero-when-null (sym)
65 (let
66 ((sym-sym (gensym)))
67 `(let
68 ((,sym-sym ,sym))
69 (if (null ,sym-sym) 0 ,sym-sym))))
71 (defun mklist (obj)
72 "Make into list if atom"
73 (if (listp obj) obj (list obj)))
75 (defun map-and-remove-nils (fn lst)
76 "Map a list by function, eliminate elements where fn returns nil"
77 (let ((acc nil))
78 (dolist (x lst (nreverse acc))
79 (let ((val (funcall fn x)))
80 (when val (push val acc))))))
82 (defun filter (fn lst)
83 "Filter a list by function, eliminate elements where fn returns nil"
84 (let ((acc nil))
85 (dolist (x lst (nreverse acc))
86 (when (funcall fn x)
87 (push x acc)))))
89 (defun flatten (lis)
90 "Flatten a list with sublists, and remove nils."
91 (cond ((atom lis) lis)
92 ((listp (car lis))
93 (append (flatten (car lis)) (flatten (cdr lis))))
94 (t (append (list (car lis)) (flatten (cdr lis))))))
97 (defun contains-keyword (keywords list)
98 "Test to see if list contains one of the keywors in the list"
99 (mapcar #'(lambda (k) (member k list)) keywords))