-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.lisp
91 lines (75 loc) · 3.64 KB
/
symbols.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
;;;; symbols.lisp
(in-package :cl-tuples)
;; package used to hold tuple type info
(defpackage :tuple-types)
(defun make-tuple-symbol (type-name tuple-element-type tuple-initial-element elements)
"Makes a symbol used to identify a typle type and interns it in the
package used for holding metadata about the tuple types. Information
about the tuple type is stored in the property list of the symbol."
(assert (listp elements))
(let*
((type-string (string-upcase (string type-name)))
(type-name-sym (intern type-string :tuple-types))
(value-name-sym (intern (concatenate 'string type-string "*") :tuple-types)))
(progn
;; deqfine the symbol
;; store-value the elements
(setf (get type-name-sym :elements) elements)
(setf (get value-name-sym :elements) elements)
;; store the # of elements ( a bit redundant )
(setf (get type-name-sym :tuple-length) (length elements))
(setf (get value-name-sym :tuple-length) (length elements))
;; store-value to use as inital array element
(setf (get type-name-sym :initial-element) tuple-initial-element)
(setf (get value-name-sym :initial-element) tuple-initial-element)
;; store-value the type of the elements
(setf (get type-name-sym :element-type) tuple-element-type)
(setf (get value-name-sym :element-type) tuple-element-type)
;; store-value a flag us to make sure it's a tuple-type symbol
(setf (get type-name-sym :is-tuple) t)
(setf (get value-name-sym :is-tuple) t))))
(defun tuple-typep (type-name)
"Test to see if this symbol represents a tuple type"
(when (or (symbolp type-name) (stringp type-name))
(get (find-symbol (string-upcase (string type-name)) :tuple-types) :is-tuple)))
(defun tuple-size (type-name)
"Return the size of the type"
(assert (or (symbolp type-name) (stringp type-name)))
(get (find-symbol (string-upcase (string type-name)) :tuple-types) :tuple-length))
(defun tuple-initial-element (type-name)
"Return the inital element type of a tuple array"
(assert (or (symbolp type-name) (stringp type-name)))
(get (find-symbol (string-upcase (string type-name)) :tuple-types) :initial-element))
(defun tuple-element-type (type-name)
"Return the size of the type"
(assert (or (symbolp type-name) (stringp type-name)))
(get (find-symbol (string-upcase (string type-name)) :tuple-types) :element-type))
(defun tuple-elements (type-name)
"Return a list of element names"
(assert (or (symbolp type-name) (stringp type-name)))
(get (find-symbol (string-upcase (string type-name)) :tuple-types) :elements))
(defun tuple-gensyms (type-name)
"Return a list of gensyms, one for each element of the tuple"
(assert (or (symbolp type-name) (stringp type-name)))
(loop
for i from 0 below (tuple-size type-name)
collect (gensym)))
(defun tuple-typespec (type-name)
"Return typespec of tuple as multiple value."
`(values ,@(loop
for i from 0 below (tuple-size type-name)
collect (tuple-element-type type-name))))
(defun tuple-typespec* (type-name)
"Return typespec of tuple as bounded array"
`(vector ,(tuple-element-type type-name) ,(tuple-size type-name)))
(defun tuple-typespec** (type-name)
"Return typespec of tuple as unbounded array"
`(vector ,(tuple-element-type type-name) *))
(defun simple-tuple-typespec* (type-name)
"Return typespec of tuple as bounded array"
`(simple-vector ,(tuple-size type-name)))
(defun tuple-places (type-name array-name)
"Return a list of (aref *) forms to turn at tuple represeted and array into individual places."
(loop
for i from 0 below (tuple-size type-name)
collect `(aref ,array-name ,i)))