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
use super::super::gc::{ *, tag::*, trace::* }; use super::*; use std::fmt::{Debug, Display}; use std::mem; #[derive(Clone, Copy, PartialEq, Eq)] pub struct Value { handle: TaggedHandle<Object>, } #[derive(Hash, Clone, PartialEq, Eq, Debug)] pub enum HashVariant { Bool(bool), Int(i64), Str(String), Nil, } #[derive(Hash, Clone, PartialEq, Eq, Debug)] pub struct HashValue { pub variant: HashVariant } #[derive(Debug, Clone, PartialEq)] pub enum Variant { Float(f64), True, False, Nil, Obj(Handle<Object>), } impl Variant { pub fn to_hash(&self, heap: &Heap<Object>) -> HashVariant { use self::Variant::*; match *self { Float(ref f) => { unsafe { HashVariant::Int( mem::transmute::<f64, i64>(*f) ) } }, True => HashVariant::Bool(true), False => HashVariant::Bool(false), Obj(ref n) => unsafe { HashVariant::Str(heap.get_unchecked(n).as_string().unwrap().clone().to_string()) }, Nil => HashVariant::Nil, } } } const TAG_TRUE: u8 = 0x01; const TAG_FALSE: u8 = 0x02; const TAG_NIL: u8 = 0x03; impl Value { #[inline] pub unsafe fn from_raw(raw: u64) -> Self { Value { handle: TaggedHandle::from_raw(raw), } } pub fn to_raw(self) -> u64 { self.handle.to_raw() } #[inline] pub fn as_float(&self) -> f64 { if let Variant::Float(f) = self.decode() { return f } panic!("non-float") } #[inline] pub fn decode(&self) -> Variant { use self::Tag::*; match self.handle.clone().decode() { Float(n) => Variant::Float(n), Handle(n) => Variant::Obj(n), Tag(t) if t == TAG_TRUE => Variant::True, Tag(t) if t == TAG_FALSE => Variant::False, Tag(t) if t == TAG_NIL => Variant::Nil, Tag(t) => panic!("Unknown tag: {}", t) } } #[inline] pub fn as_object<'a>(&self) -> Option<Handle<Object>> { match self.decode() { Variant::Obj(o) => Some(o), _ => None, } } pub fn with_heap<'h>(&self, heap: &'h Heap<Object>) -> WithHeap<'h, Self> { WithHeap::new(heap, *self) } pub fn float(float: f64) -> Self { Value { handle: TaggedHandle::from_float(float), } } pub fn truelit() -> Self { Value { handle: TaggedHandle::from_tag(TAG_TRUE), } } pub fn falselit() -> Self { Value { handle: TaggedHandle::from_tag(TAG_FALSE), } } pub fn truthy(&self) -> bool { match self.decode() { Variant::False | Variant::Nil => false, _ => true, } } pub fn nil() -> Self { Value { handle: TaggedHandle::from_tag(TAG_NIL), } } pub fn object(handle: Handle<Object>) -> Self { Value { handle: TaggedHandle::from_handle(handle) } } } impl Trace<Object> for Value { fn trace(&self, tracer: &mut Tracer<Object>) { if let Variant::Obj(obj) = self.decode() { obj.trace(tracer); } } } impl From<Handle<Object>> for Value { fn from(handle: Handle<Object>) -> Self { Value::object(handle) } } impl Debug for Value { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match self.decode() { Variant::Nil => write!(f, "nil"), Variant::False => write!(f, "false"), Variant::True => write!(f, "true"), Variant::Float(n) => write!(f, "{:?}", n), Variant::Obj(o) => write!(f, "{:?}", o), } } } impl Into<Value> for f64 { fn into(self) -> Value { Value::float(self) } } impl Into<Value> for bool { fn into(self) -> Value { if self { Value::truelit() } else { Value::falselit() } } } pub struct WithHeap<'h, T> { pub heap: &'h Heap<Object>, pub item: T, } impl<'h, T> WithHeap<'h, T> { pub fn new(heap: &'h Heap<Object>, item: T) -> WithHeap<'h, T> { WithHeap { heap, item } } pub fn with<U>(&self, item: U) -> WithHeap<U> { WithHeap { heap: self.heap, item } } } impl<'h> Display for WithHeap<'h, Value> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match self.item.decode() { Variant::Nil => write!(f, "nil"), Variant::False => write!(f, "false"), Variant::True => write!(f, "true"), Variant::Float(n) => write!(f, "{}", n), Variant::Obj(o) => { let o = self.heap.get(o).ok_or(::std::fmt::Error)?; write!(f, "{}", self.with(o)) }, } } }