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
// 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.

//! Base for closures

use std::fmt;
use std::cmp::Ordering;
use memory::*;
use dispatch::*;
use value::*;
use handle::Handle;
use transduce::Process;

pub static FUNC_SENTINEL: u8 = 0;

/// Func dispatch.
pub struct Func { }

// Func lifecycle
// Seg alloc, set prism
// Set meta-data, which arities
// Set closed values
// ...
// invoke!
// choose arity, calculate f-table index, invoke extern
// [method written by my compiler] receives aline and args
//

// Func prism, guide, IFn table base, closed-over values

#[no_mangle]
pub extern fn new_func(arity_bitmap: u32, vtable: u32, capture_ct: u32) -> u32 {
    let cap = 4 /*prism, arity bitmap, vtable base, capture count*/ + capture_ct;
    let s = Segment::new(cap);
    let prism = s.line_at(0);
    //prism.set(0, mechanism::prism::<Func>());
    prism.set(1, arity_bitmap.into());
    prism.set(2, vtable.into());
    prism.set(3, 0.into());
    s.unit().u32()
}
#[no_mangle]
pub extern fn add_capture(func: u32, capture: u32) {
    let s = Segment::from(Unit::from(func));
    assert!(!s.anchor().is_aliased());
    //assert_eq!(s.get(0), mechanism::prism::<Func>());
    let capture_ct = s.get(3).u32();
    let captures = s.line_at(4);
    captures.set(capture_ct as i32, capture.into());
    s.set(3, (capture_ct + 1).into());
}

#[link(wasm_import_module = "ifn")]
extern {
    fn func_invoke0(segment: u32, index: u32, table_idx: u32);
    fn func_invoke1(segment: u32, index: u32, arg0: u32, table_idx: u32);
    fn func_invoke2(segment: u32, index: u32, arg0: u32, arg1: u32, table_idx: u32);
}

/*
impl Func {
    pub fn new() -> Unit {
        log!("vector new");
        let guide = {
            let cap = 1 /*prism*/ + Guide::units() + size(1);
            let s = Segment::new(cap);
            let prism = s.line_at(0);
            prism.set(0, mechanism::prism::<Vector>());
            let mut g = Guide::hydrate_top_bot(prism, 0, 0);
            g.is_compact_bit = 0x1;
            g
        };
        guide.store().segment().unit()
    }

    pub fn new_value() -> Value { Vector::new().handle().value() }
}

impl Dispatch for Func {
    fn tear_down(&self, prism: AnchoredLine) {
        group!("vector tear down");
        tear_down::tear_down(prism);
        group_end!();
    }
    fn unaliased(&self, prism: AnchoredLine) -> Unit {
        unaliased_root(Guide::hydrate(prism)).segment().unit()
    }
}

impl Identification for Vector {
    fn type_name(&self) -> &'static str { "Vector" }
    fn type_sentinel(&self) -> *const u8 { (& VECTOR_SENTINEL) as *const u8 }
}

impl Distinguish for Vector {
    fn hash(&self, prism: AnchoredLine) -> u32 {
        let guide = Guide::hydrate(prism);
        if guide.has_hash() {
            return guide.hash;
        }
        group!("vector hash");
        use random::{PI, cycle_abc};
        struct Pointer {
            pub ptr: *mut u64,
        }
        impl Process for Pointer {
            fn inges(&mut self, _stack: &mut [Box<dyn Process>], v: &Value) -> Option<Value> {
                let h = v.hash() as u64;
                unsafe {
                    *self.ptr = cycle_abc(34, *self.ptr + h);
                }
                None
            }
            fn last_call(&mut self, _stack: &mut [Box<dyn Process>]) -> Value { Handle::nil().value() }
        }

        let mut y = cycle_abc(7, PI[321] + guide.count as u64);
        let mut procs: [Box<dyn Process>; 1] = [Box::new(Pointer { ptr: (&mut y) as *mut u64 })];
        let _ = reduce::reduce(prism, &mut procs);
        let h = cycle_abc(210, y) as u32;
        log!("hash of vector {:#08X}", h);
        group_end!();
        guide.set_hash(h).store_hash().hash
    }

    fn eq(&self, prism: AnchoredLine, other: Unit) -> bool {
        let o = other.handle();
        if o.is_ref() {
            let o_prism = o.logical_value();
            if prism[0] == o_prism[0] {
                group!("vector eq");
                let res = eq::eq(Guide::hydrate(prism), Guide::hydrate(o_prism));
                group_end!();
                res
            } else {
                use list::LIST_SENTINEL;
                let p = o_prism[0];
                if mechanism::as_dispatch(&p).type_sentinel() == (& LIST_SENTINEL) as *const u8 {
                    unimplemented!()
                } else {
                    false
                }
            }
        } else {
            false
        }
    }

    fn cmp(&self, _prism: AnchoredLine, _other: Unit) -> Option<Ordering> {
        // cast other to vector, compare pairwise
        unimplemented!("Vector compare")
    }
}

impl Aggregate for Vector {
    fn is_aggregate(&self) -> bool { true }
    fn count(&self, prism: AnchoredLine) -> u32 {
        let guide = Guide::hydrate(prism);
        guide.count
    }
    fn empty(&self, _prism: AnchoredLine) -> Unit { Vector::new() }
    fn conj(&self, prism: AnchoredLine, x: Unit) -> Unit {
        group!("vector conj");
        let res = conj::conj(prism, x);
        group_end!();
        res
    }
    fn meta(&self, prism: AnchoredLine) -> *const Unit { meta::meta(prism) }
    fn with_meta(&self, prism: AnchoredLine, m: Unit) -> Unit { meta::with_meta(prism, m) }
    fn peek(&self, prism: AnchoredLine) -> *const Unit {
        let guide = Guide::hydrate(prism);
        self.nth(prism, guide.count - 1)
    }
    fn pop(&self, prism: AnchoredLine) -> (Unit, Unit) { pop::pop(prism) }
    fn reduce(&self, prism: AnchoredLine, process: &mut [Box<dyn Process>]) -> Value {
        reduce::reduce(prism, process)
    }
}

impl Sequential for Vector {
    fn is_sequential(&self) -> bool { true }
    fn nth(&self, prism: AnchoredLine, idx: u32) -> *const Unit {
        nth::nth(prism, idx).line().star()
    }
}

impl Associative for Vector {
    fn assoc(&self, prism: AnchoredLine, k: Unit, v: Unit) -> (Unit, Unit) {
        let guide = Guide::hydrate(prism);
        let idx = k.handle().as_i64();
        k.handle().retire();
        if idx < 0 || (idx as u32) >= guide.count {
            panic!("Index out of bounds: {} in vector of count {}", idx, guide.count);
        }
        assoc::assoc(prism, idx as u32, v)
    }
}

impl Reversible for Vector {}
impl Sorted for Vector {}
impl Notation for Vector {
    fn edn(&self, prism: AnchoredLine, f: &mut fmt::Formatter) -> fmt::Result {
        // conversion to and from &Formatter
        // factor out Printer parts
        struct Printer {
            pub is_first: bool,
            pub f: usize,
        }

        impl Printer {
            pub fn new(f: &mut fmt::Formatter) -> Printer {
                use std::mem::transmute;
                unsafe { Printer { is_first: true, f: transmute::<& fmt::Formatter, usize>(f) } }
            }
        }

        impl Process for Printer {
            fn inges(&mut self, _stack: &mut [Box<dyn Process>], v: &Value) -> Option<Value> {
                use std::mem::transmute;
                write!(unsafe { transmute::<usize, &mut fmt::Formatter>(self.f) },
                       "{}{}",
                       if self.is_first { self.is_first = false; "" } else { " " },
                       v).unwrap();
                None
            }
            fn last_call(&mut self, _stack: &mut [Box<dyn Process>]) -> Value {
                Handle::nil().value()
            }
        }

        write!(f, "[")?;
        let mut procs: [Box<dyn Process>; 1] = [Box::new(Printer::new(f))];
        let _ = reduce::reduce(prism, &mut procs);
        write!(f, "]")
    }
}

impl Numeral for Vector {}
impl Callable for Vector {}

#[cfg(test)]
mod tests {
    use super::*;

}

*/