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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) Cole Frederick. All rights reserved.
// The use and distribution terms for this software are covered by the
// Eclipse Public License 1.0 (https://opensource.org/licenses/eclipse-1.0.php)
// which can be found in the file epl-v10.html at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the terms of this license.
// You must not remove this notice, or any other, from this software.

use std::fmt;
use std::cmp::Ordering;
use memory::*;
use dispatch::*;
use handle::Handle;
use Value;

pub mod guide;
use self::guide::Guide;

// Numbers. immediate i60 (28), f60 (28). boxed integral, rational, float point.
// Layout: i60I f60F, [prism guide{chunk_count} contents]
// methods (on guide?) to get/set chunks by index (32/64, LE/BE)
// inc dec, + -, * /, % mod, neg
// zero? neg? pos?, type tests, modular exponentiation,

// inc dec -> if(int) _ else fn_call to dispatch here
// neg if imm
// + - * / if both imm (x & y & 0x1) == 0x1 then _ else dispatch here
// zero? neg? pos? -> special case imm

pub struct Integral_ { }
pub fn prism_unit() -> Unit { mechanism::prism::<Integral_>() }
pub fn is_prism(prism: AnchoredLine) -> bool { prism[0] == prism_unit() }
pub fn find_prism(h: Handle) -> Option<AnchoredLine> { h.find_prism(prism_unit()) }
pub fn is_integral(h: Handle) -> bool { find_prism(h).is_some() }

pub fn new(x: i64) -> Unit {
    //log!("New integral, {}", x);
    let guide = blank();
    store(guide.root, x);
    guide.store().segment().unit()
}
pub fn blank() -> Guide {
    let needed = 1 /*prism*/ + Guide::units() + if cfg!(target_pointer_width = "32") { 2 } else { 1 };
    let s = Segment::new(needed);
    let prism = s.line_at(0);
    prism.set(0, prism_unit());
    let guide = Guide::new(prism);
    guide
}
pub fn new_value(x: i64) -> Value { new(x).handle().value() }

pub fn parse(negate: bool, m: &[u8], promote: bool) -> Handle {
    if promote {
        return big_int(negate, m)
    }
    let mut x = 0i64;
    for b in m.iter() {
        if *b == b'_' {
            continue
        }
        x = x * 10 + (*b - b'0') as i64;
    }
    if negate { x = -x; }
    let guide = {
        let g = blank();
        if promote { g.set_big() } else { g }
    };
    store(guide.root, x);
    guide.store().segment().unit().handle()
}
pub fn parse_hex(negate: bool, m: &[u8], promote: bool) -> Handle {
    if promote { unimplemented!() }
    let mut x = 0i64;
    for b in m.iter() {
        if *b == b'_' {
            continue
        }
        let d = if *b <= b'9' { *b - b'0' }
        else if *b <= b'F' { *b - b'A' + 10 }
        else { *b - b'a' + 10 };
        assert!(d < 16);
        x = (x << 4) + d as i64;
    }
    if negate { x = -x; }
    let guide = {
        let g = blank();
        if promote { g.set_big() } else { g }
    };
    store(guide.root, x);
    guide.store().segment().unit().handle()
}
pub fn parse_radix(negate: bool, radix: u32, m: &[u8]) -> Option<Handle> {
    let mut x = 0i64;
    for b in m.iter() {
        if *b == b'_' {
            continue
        }
        let d = if *b <= b'9' { *b - b'0' }
        else if *b <= b'Z' { *b - b'A' + 10 }
        else { *b - b'a' + 10 };
        if d >= radix as u8 { return None }
        x = x * radix as i64 + d as i64;
    }
    if negate { x = -x; }
    Some(new(x).handle())
}
pub fn as_i64(prism: AnchoredLine) -> i64 {
    assert!(is_prism(prism));
    let guide = Guide::hydrate(prism);
    let x = hydrate(guide.root);
    x
}

pub fn store(line: AnchoredLine, x: i64) {
    if cfg!(target_pointer_width = "32") {
        line.set(0, Unit::from(x as i32));
        line.set(1, Unit::from((x >> 32) as i32));
    } else {
        line.set(0, Unit::from(x));
    }
}

pub fn hydrate(line: AnchoredLine) -> i64 {
    if cfg!(target_pointer_width = "32") {
        let low: u32 = line[0].into();
        let hi:  u32 = line[1].into();
        let res = ((hi as u64) << 32) | (low as u64);
        res as i64
    } else {
        line[0].into()
    }
}

impl Dispatch for Integral_ { /*default tear_down, alias_components*/ }
impl Identification for Integral_ {
    fn type_name(&self) -> &'static str { "Integral" }
}
impl Distinguish for Integral_ {
    fn hash(&self, prism: AnchoredLine) -> u32 {
        let guide = Guide::hydrate(prism);
        if guide.has_hash() { return guide.hash; }

        let h = {
            use hash::hash_64;
            let x = hydrate(guide.root) as u64;
            hash_64(x, 8)
        };
        //log!("Hash integral {} {:#08X}", prism.segment().unit().handle(), h);
        guide.set_hash(h).store_hash().hash
    }
    fn eq(&self, prism: AnchoredLine, other: Unit) -> bool {
        let o = other.handle();
        if let Some(o_int) = find_prism(o) {
            let guide = Guide::hydrate(prism);
            let guide2 = Guide::hydrate(o_int);
            let x = hydrate(guide.root);
            let y = hydrate(guide2.root);
            return x == y
        }
        false
    }
    fn cmp(&self, prism: AnchoredLine, other: Unit) -> Option<Ordering> {
        let o = other.handle();
        if let Some(o_int) = find_prism(o) {
            let guide = Guide::hydrate(prism);
            let guide2 = Guide::hydrate(o_int);
            let x = hydrate(guide.root);
            let y = hydrate(guide2.root);
            return Some(x.cmp(&y))
        }
        if o.is_ref() {
            let o_prism_unit = o.logical_value()[0];
            Some(prism_unit().cmp(&o_prism_unit))
        } else {
            Some(Ordering::Greater)
        }
    }
}
impl Aggregate for Integral_ { }
impl Sequential for Integral_ { }
impl Associative for Integral_ { }
impl Reversible for Integral_ {}
impl Sorted for Integral_ {}
impl Notation for Integral_ {
    fn edn(&self, prism: AnchoredLine, f: &mut fmt::Formatter) -> fmt::Result {
        let guide = Guide::hydrate(prism);
        let x = hydrate(guide.root);
        write!(f, "{}", x)
    }
}
impl Numeral for Integral_ {
    fn inc(&self, prism: AnchoredLine) -> Unit {
        let guide = Guide::hydrate(prism);
        let x = hydrate(guide.root);
        let s = guide.segment();
        if s.is_aliased() {
            if s.unalias() == 0 {
                Segment::free(s);
            }
            new(x + 1)
        } else {
            store(guide.root, x + 1);
            guide.clear_hash().store().segment().unit()
        }
    }
    fn dec(&self, prism: AnchoredLine) -> Unit {
        unimplemented!()
    }
    fn add(&self, prism: AnchoredLine, other: Unit) -> Unit {
        let o = other.handle();
        if let Some(o_int) = find_prism(o) {
            let guide = Guide::hydrate(prism);
            let guide2 = Guide::hydrate(o_int);
            let x = hydrate(guide.root);
            let y = hydrate(guide2.root);
            guide.segment().unit().handle().retire();
            guide2.segment().unit().handle().retire();
            if let Some(sum) = x.checked_add(y) {
                return new(sum)
            } else {
                panic!("Overflowing add of {} + {}", x, y);
            }
        }
        unimplemented!()
    }
    fn subtract(&self, prism: AnchoredLine, other: Unit) -> Unit {
        unimplemented!()
    }
    fn neg(&self, prism: AnchoredLine) -> Unit {
        unimplemented!()
    }
    fn multiply(&self, prism: AnchoredLine, other: Unit) -> Unit {
        unimplemented!()
    }
    fn divide(&self, prism: AnchoredLine, other: Unit) -> Unit {
        unimplemented!()
    }
    fn remainder(&self, prism: AnchoredLine, other: Unit) -> Unit {
        unimplemented!()
    }
    fn modulus(&self, prism: AnchoredLine, other: Unit) -> Unit {
        unimplemented!()
    }
}
impl Callable for Integral_ {}


pub fn big_int(negate: bool, m: &[u8]) -> Handle {
    /*
    use std::str::from_utf8;
    let temp = format!("{}{}N", if negate { "-" } else { "" }, from_utf8(m).unwrap());
    let t = string::new_from_str(&temp).unit();
    let needed = 1 /*prism*/ + 1 /*string*/;
    let s = Segment::new(needed);
    let prism = s.line_at(0);
    prism.set(0, mechanism::prism::<BigInt>());
    prism.set(1, t);
    s.unit().handle()
    */
    unimplemented!()
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn read() {
        assert_eq!("0xFF".parse::<Value>(), Ok(255.into()));
        assert_eq!("2r101".parse::<Value>(), Ok(5.into()));
    }
    #[test]
    fn increment() {
        let v: Value = 5.into();
        assert_eq!(v.inc(), 6.into());
    }
    #[test]
    fn add() {
        let v: Value = 1.into();
        let w: Value = 2.into();
        //assert_eq!(v + w, 3.into());
    }
}