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
|
// The data representations of values of different types are given below.
// - Pointers are 64 bit unsigned (positive) ints which are
// word-aligned, i.e. 0 mod 8. All types not listed below are
// allocated on the heap behind a pointer.
// - Int:
// < 63-bit signed int > 1
// Ints are a 63 bit int with a 1 in the low bit.
// - Booleans and nil are 2 mod 8:
// - False is 0x2
// - True is 0xA
// - Nil is 0x12
// - A symbol's low byte is 0x4, and the high 56 bytes are an unsigned integer constant
// representing a pointer into the symbol table.
//
// n.b. 6 mod 8 is unused.
#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
pub struct Pointer(pub usize);
impl Pointer {
pub fn offset(self, i: i64) -> Self {
let Pointer(u) = self;
if i > 0 {
return Pointer(u + usize::try_from(i).unwrap());
}
return Pointer(u - usize::try_from(-i).unwrap());
}
}
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub struct Value(pub u64);
impl Value {
pub fn is_nil(self) -> bool {
let Value(stack_representation) = self;
return stack_representation == 0;
}
pub fn is_pointer(self) -> bool {
let Value(stack_representation) = self;
return stack_representation & 0x7 == 0;
}
pub fn from_pointer(p: Pointer) -> Self {
let Pointer(x) = p;
return Value(u64::try_from(x).unwrap());
}
pub fn to_pointer(self) -> Result<Pointer, String> {
let Value(stack_representation) = self;
if !self.is_pointer() {
return Err(format!("value {:x} is not a pointer", stack_representation));
}
return Ok(Pointer(usize::try_from(stack_representation).unwrap()));
}
pub fn is_int(self) -> bool {
let Value(stack_representation) = self;
return stack_representation & 0x1 == 1;
}
pub fn from_int(i: i64) -> Self {
return Value((i as u64) << 1 | 1);
}
pub fn to_int(self) -> Result<i64, String> {
let Value(stack_representation) = self;
if !self.is_int() {
return Err(format!("value {:x} is not an int", stack_representation));
}
return Ok(stack_representation as i64 >> 1);
}
pub fn is_bool(self) -> bool {
let Value(stack_representation) = self;
return stack_representation == 0xa || stack_representation == 0x2;
}
pub fn from_bool(b: bool) -> Self {
if b {
return Value(0xa);
}
return Value(0x2);
}
pub fn to_bool(self) -> Result<bool, String> {
let Value(stack_representation) = self;
if !self.is_bool() {
return Err(format!("value {:x} is not a boolean", stack_representation));
}
return Ok(stack_representation == 0xa);
}
pub fn is_char(self) -> bool {
let Value(stack_representation) = self;
return stack_representation & 0x7 == 4;
}
pub fn from_char(c: char) -> Self {
return Value(u64::from(c) << 32 | 0x4);
}
pub fn to_char(self) -> Result<char, String> {
let Value(stack_representation) = self;
if !self.is_char() {
return Err(format!("value {:x} is not a char", stack_representation));
}
return Ok(
char::from_u32((stack_representation >> 32) as u32).ok_or(format!(
"value {:x} is not a valid char",
stack_representation >> 32
))?,
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_nil() {
assert_eq!(true, Value(0).is_nil());
}
#[test]
fn is_not_nil() {
assert_eq!(false, Value(1).is_nil());
}
#[test]
fn is_pointer() {
assert_eq!(true, Value(0xf8).is_pointer());
}
#[test]
fn is_not_pointer() {
assert_eq!(false, Value(1).is_pointer());
}
#[test]
fn from_pointer() {
assert_eq!(Value(0xf8), Value::from_pointer(Pointer(0xf8)));
}
#[test]
fn to_pointer() {
assert_eq!(Ok(Pointer(0xf8)), Value(0xf8).to_pointer());
}
#[test]
fn is_int() {
assert_eq!(true, Value(1).is_int());
}
#[test]
fn is_not_int() {
assert_eq!(false, Value(0).is_int());
}
#[test]
fn from_int() {
assert_eq!(Value(0xb), Value::from_int(5));
}
#[test]
fn to_int() {
assert_eq!(Ok(5), Value(0xb).to_int());
}
#[test]
fn true_is_bool() {
assert_eq!(true, Value(0xa).is_bool());
}
#[test]
fn false_is_bool() {
assert_eq!(true, Value(2).is_bool());
}
#[test]
fn is_not_bool() {
assert_eq!(false, Value(0).is_bool());
}
#[test]
fn true_from_bool() {
assert_eq!(Value(0xa), Value::from_bool(true));
}
#[test]
fn false_from_bool() {
assert_eq!(Value(2), Value::from_bool(false));
}
#[test]
fn true_to_bool() {
assert_eq!(Ok(true), Value(0xa).to_bool());
}
#[test]
fn false_to_bool() {
assert_eq!(Ok(false), Value(2).to_bool());
}
#[test]
fn is_char() {
assert_eq!(true, Value(0x6100000004).is_char());
}
#[test]
fn is_not_char() {
assert_eq!(false, Value(0).is_char());
}
#[test]
fn from_char() {
assert_eq!(Value(0x5800000004), Value::from_char('X'));
}
#[test]
fn to_char() {
assert_eq!(Ok('😂'), Value(0x1f60200000004).to_char());
}
}
|