-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.lisp
48 lines (38 loc) · 1.29 KB
/
data.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
(in-package :minerva)
(defconstant +word-size+ 4) ; octets
(defconstant +tag-integer+ #b00)
(defconstant +tag-bool+ #b0011111)
(defconstant +tag-character+ #b00001111)
(defconstant +tag-nil+ #b00101111)
(defconstant +tag-pair+ #b001)
(defconstant +tag-vector+ #b010)
(defconstant +tag-string+ #b011)
(defconstant +tag-symbol+ #b101)
(defconstant +tag-closure+ #b110)
(defconstant +mask-integer+ #b11)
(defconstant +mask-bool+ #b1111111)
(defconstant +shift-integer+ 2)
(defconstant +shift-character+ 8)
(defconstant +shift-bool+ 7)
(defun immediate-rep (x)
(cond ((integerp x) (encode-integer x))
((characterp x) (encode-character x))
((boolp x) (encode-bool x))
((null x) (encode-nil))))
(defun encode-integer (x)
(let ((int 0))
(setf (ldb (byte +shift-integer+ 0) int) +tag-integer+)
(setf (ldb (byte 30 +shift-integer+) int) x)
int))
(defun encode-character (x)
(let ((char 0))
(setf (ldb (byte +shift-character+ 0) char) +tag-character+)
(setf (ldb (byte 24 +shift-character+) char) (char-code x))
char))
(defun encode-bool (x)
(let ((bool 0))
(setf (ldb (byte +shift-bool+ 0) bool) +tag-bool+)
(setf (ldb (byte 1 +shift-bool+) bool) (if (value x) 1 0))
bool))
(defun encode-nil ()
+tag-nil+)