seax_svm 0.2.8

SECD virtual machine for interpreting programs in FP languages
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#![crate_name = "seax_svm"]
#![crate_type = "lib"]
#![cfg_attr(feature = "nightly", stable(feature="vm_core", since="0.1.2") )]
#![cfg_attr(test, feature(test))]
#![cfg_attr(feature = "nightly", feature(vec_push_all) )]
#![cfg_attr(feature = "nightly", feature(staged_api) )]
#![cfg_attr(feature = "nightly", staged_api)]
#![feature(box_patterns)]


#[cfg(test)] extern crate quickcheck;
#[cfg(test)] extern crate test;

#[macro_use] extern crate log;
extern crate byteorder;

/// Singly-linked list and stack implementations.
///
/// `List<T>` is a singly-linked `cons` list.
/// `Stack<T>` is a trait providing stack operations(`push()`, `pop()`, and
/// `peek()`), and an implementation for `List`.
#[macro_use]
#[cfg_attr(feature = "nightly", stable(feature="list", since="0.1.0"))]
pub mod slist;

/// SVM cell types.
///
/// A cell in the VM can be either an atom (single item, either unsigned
/// int, signed int, float, or string), a pointer to a list cell, or an
/// instruction.
#[macro_use]
#[cfg_attr(feature = "nightly", stable(feature="vm_core", since="0.1.2"))]
pub mod cell;

#[cfg_attr(feature = "nightly", unstable(feature="bytecode"))]
pub mod bytecode;

#[cfg(test)]
mod tests;

/// Contains the Seax Virtual Machine (SVM) and miscellaneous
/// support code.

// Reexports
pub use self::slist::{List, Stack};
pub use self::slist::List::{Cons,Nil};
pub use self::cell::{SVMCell,Atom,Inst};

use self::cell::SVMCell::*;
use self::cell::Atom::*;
use self::cell::Inst::*;

/// Represents a SVM machine state
#[derive(PartialEq,Clone,Debug)]
#[cfg_attr(feature = "nightly", stable(feature="vm_core", since="0.1.0"))]
pub struct State {
    stack:  List<SVMCell>,
    env:    List<SVMCell>,
    control:List<SVMCell>,
    dump:   List<SVMCell>
}

/// A VM state's IO action
///
/// Take note that this will eventually be replaced with memory-mapped IO
/// when the main memory management scheme is done; therefore, it should
/// never be marked as stable. Consider this struct and anything that depends
/// on it to be an ugly hack.
#[derive(PartialEq,Clone,Debug)]
#[cfg_attr(feature = "nightly", unstable(feature="eval"))]
pub enum IOEvent {
    /// A character was requested from the buffer
    Req,
    /// A character was buffered
    Buf(char)
}

#[cfg_attr(feature = "nightly", unstable(feature="eval"))]
pub type EvalResult = Result<(State,Option<IOEvent>), String>;

#[cfg_attr(feature = "nightly", stable(feature="vm_core", since="0.1.0"))]
impl State {

    /// Creates a new empty state
    #[cfg_attr(feature = "nightly", stable(feature="vm_core", since="0.1.0"))]
    pub fn new() -> State {
        State {
            stack: Stack::empty(),
            env: Stack::empty(),
            control: Stack::empty(),
            dump: Stack::empty()
        }
    }


    /// Dump state to string
    ///
    /// This produces state dumps suitable for printing as part of
    /// an error report. This is different from fmt::Debug since it
    /// includes a tag for the error reporter.
    #[cfg_attr(feature = "nightly", stable(feature="debug", since="0.2.0"))]
    pub fn dump_state(&self, tag: &str) -> String {
        format!(
            "[{t}] State dump:\n \
             [{t}]\t\tStack:\t {s:?}\n \
             [{t}]\t\tEnv:\t {e:?}\n \
             [{t}]\t\tControl: {c:?}\n \
             [{t}]\t\tDump:\t {d:?}\n",
                t = tag,
                s = &self.stack,
                e = &self.env,
                c = &self.control,
                d = &self.dump
            )
    }

    /// Evaluates an instruction.
    ///
    /// Evaluates an instruction against a state, returning a new state.
    ///
    /// # Arguments:
    ///
    ///  - `inp`: an input stream implementing `io::Read`
    ///  - `outp`: an output stream implementing `io::Write`
    ///  - `debug`: whether or not to snapshot the state before evaluating. This
    ///     provides more detailed debugging information on errors, but may have
    ///     a significant impact on performance.
    ///
    #[cfg_attr(feature = "nightly", stable(feature="vm_core", since="0.3.0"))]
    pub fn eval(self,
                input: Option<u8>,
                debug: bool)
                -> EvalResult {
        debug!("[eval]: Evaluating {:?}", self.control);
        // TODO: this (by which I mean "the whole caching deal") could likely be made
        // better and/or faster with some clever (mis?)use of RefCell; look into that.
        let mut prev = if debug { Some(self.clone()) } else { None };
        // in ths pattern match, we begin The Great Work
        match self.control.pop().unwrap() {
            // NIL: pop an empty list onto the stack
            (InstCell(NIL), new_control) => Ok((State {
                stack: self.stack.push(list_cell![]),
                env: self.env,
                control: new_control,
                dump: self.dump
            }, None)),
            // LDC: load constant
            (InstCell(LDC), new_control) => {
                let (atom,newer_control) = try!(new_control.pop().ok_or(
                    format!("[fatal][LDC]: pop on empty stack\n{}",
                        prev.take().map_or(String::new(), |x| x.dump_state("fatal") ))) );
                Ok((State {
                    stack: self.stack.push(atom),
                    env: self.env,
                    control: newer_control,
                    dump: self.dump
                }, None))
            },
            // LD: load variable
            (InstCell(LD), new_control) => match new_control.pop() {
                Some((ListCell(
                    box Cons(AtomCell(UInt(lvl)),
                    box Cons(AtomCell(UInt(idx)),
                    box Nil))
                    ), newer_control)) => match self.env[(lvl-1)] {
                        ListCell(ref level) => Ok((State {
                            stack: match level.get(idx-1) {
                                Some(thing) => self.stack.push(thing.clone()),
                                None        => self.stack
                            },
                            env: self.env.clone(),
                            control: newer_control,
                            dump: self.dump
                        }, None)),
                        // This is a special case for something that, as far as I know,
                        // should never happen. But despite everything, it DOES happen.
                        ref thing @ AtomCell(_) => Ok((State {
                        // I give up. Have your special case.
                            stack: self.stack.push(thing.clone()),
                            env: self.env.clone(),
                            control: newer_control,
                            dump: self.dump
                        }, None)),
                        _ => Err(format!(
                            "[fatal][LD]: expected list in $e, found {:?}\n{}",
                            self.env[lvl-1], prev.map_or(String::new(), |x| x.dump_state("fatal") )))
                },
               Some((ListCell( // TODO: this uses deprecated signed int indexing, remove
                    box Cons(AtomCell(SInt(lvl)),
                    box Cons(AtomCell(SInt(idx)),
                    box Nil))
                    ), newer_control)) =>  match self.env[(lvl-1)] {
                        ListCell(ref level) => Ok((State {
                            stack: self.stack.push(level[(idx-1)].clone()),
                            env: self.env.clone(),
                            control: newer_control,
                            dump: self.dump
                        }, None)),
                        _ => Err(format!(
                            "[fatal][LD]: expected list in $e, found {:?}\n{}",
                            self.env[lvl-1], prev.map_or(String::new(), |x| x.dump_state("fatal") )))
                },
               Some((thing,newer_control)) => Err(format!(
                    "[fatal][LD]: expected pair, found {:?}\n[fatal] new control: {:?}\n{}",
                    thing,
                    newer_control,
                    prev.map_or(String::new(), |x| x.dump_state("fatal") ))),
               None => Err(format!(
                    "[fatal][LD]: expected pair, found empty stack\n{}",
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )))
            },

            // LDF: load function
            (InstCell(LDF), new_control) => {
                let (func, newer_control) = try!(match new_control.pop() {
                    Some(thing) => Ok(thing),
                    None        => Err(format!(
                        "[fatal][LDF]: pop on empty control stack\n{}",
                        prev.map_or(String::new(), |x| x.dump_state("fatal") )))
                });
                Ok((State {
                    stack: self.stack.push(list_cell![
                        func,
                        self.env.get(0)
                            .map_or(list_cell![], |it| it.clone())
                        ]),
                    env: self.env,
                    control: newer_control,
                    dump: self.dump
                }, None))
            },

            (InstCell(JOIN), new_control) => {
                let (top, new_dump) = try!(match self.dump.pop() {
                    Some(thing) => Ok(thing),
                    None        => Err(format!(
                        "[fatal][JOIN]: pop on empty dump stack") )
                });
                match top {
                    ListCell(box Nil) => Ok((State {
                        stack: self.stack,
                        env: self.env,
                        control: new_control,
                        dump: new_dump
                    }, None)),
                    ListCell(box it)  => Ok((State {
                        stack: self.stack,
                        env: self.env,
                        control: it,
                        dump: new_dump
                    }, None)),
                    anything          => Err(format!(
                        "[fatal][JOIN]: expected list on dump, found {:?}\n{}",
                        anything, prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                }
            },
            (InstCell(ADD), new_control) => match self.stack.pop() {
                Some((AtomCell(op1), new_stack)) => match new_stack.pop() {
                    Some((AtomCell(op2), newer_stack)) => Ok((State {
                            stack: newer_stack.push(AtomCell(op1 + op2)),
                            env: self.env,
                            control: new_control,
                            dump: self.dump
                        }, None)),
                    any => Err(format!(
                        "[fatal][ADD]: expected second operand, found {:?}\n{}",
                        any,
                        prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                    },
                any => Err(format!(
                    "[fatal][ADD]: expected first operand, found {:?}\n{}",
                    any,
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(SUB), new_control) => match self.stack.pop() {
                Some((AtomCell(op1), new_stack)) => match new_stack.pop() {
                    Some((AtomCell(op2), newer_stack)) => Ok((State {
                            stack: newer_stack.push(AtomCell(op1 - op2)),
                            env: self.env,
                            control: new_control,
                            dump: self.dump
                        }, None)),
                    any => Err(format!(
                        "[fatal][SUB]: expected second operand, found {:?}\n{}",
                        any,
                        prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                    },
                any => Err(format!(
                    "[fatal][SUB]: expected first operand, found {:?}\n{}",
                    any,
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(FDIV), new_control) => {
                let (op1, new_stack) = try!(match self.stack.pop() {
                    Some(thing) => Ok(thing),
                    None        => Err(format!(
                        "[fatal][FDIV]: pop on empty stack\n{}",
                        prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                });
                match op1 {
                    AtomCell(a) => {
                        let (op2, newer_stack) = try!(match new_stack.pop() {
                            Some(thing) => Ok(thing),
                            None        => Err("[fatal][FDIV]: pop on empty stack")
                        });
                        match op2 {
                            AtomCell(b) => Ok((State {
                                stack: newer_stack.push(AtomCell(
                                    match (a, b) {
                                        // same type: coerce to float
                                        (SInt(a), SInt(b))      => Float(a as f64 / b as f64),
                                        (UInt(a), UInt(b))      => Float(a as f64 / b as f64),
                                        (Float(a), Float(b))    => Float(a / b),
                                        // float + int: coerce to float
                                        (Float(a), SInt(b))     => Float(a / b as f64),
                                        (Float(a), UInt(b))     => Float(a / b as f64),
                                        (SInt(a), Float(b))     => Float(a as f64 / b),
                                        (UInt(a), Float(b))     => Float(a as f64 / b),
                                        // uint + sint: coerce to float
                                        (UInt(a), SInt(b))      => Float(a as f64 / b as f64),
                                        (SInt(a), UInt(b))      => Float(a as f64 / b as f64),
                                        // char + any: coerce to int -> float
                                        // but if you ever actually do this, then ...wat?
                                        (Char(a), Char(b))      => Float(a as u8 as f64 / b as u8 as f64),
                                        (Char(a), UInt(b))      => Float(a as u8 as f64 / b as f64),
                                        (Char(a), SInt(b))      => Float(a as u8 as f64 / b as f64),
                                        (Char(a), Float(b))     => Float(a as u8 as f64 / b as f64),
                                        (UInt(a), Char(b))      => Float(a as f64 / b as u8 as f64),
                                        (SInt(a), Char(b))      => Float(a as f64 / b as u8 as f64),
                                        (Float(a), Char(b))     => Float(a as f64 / b as u8 as f64)
                                    }
                                    )),
                                env: self.env,
                                control: new_control,
                                dump: self.dump
                            }, None)),
                            b => Err(format!(
                                "[fatal][FDIV]: TypeError: expected compatible operands, found (FDIV {:?} {:?})", a, b) )
                        }
                    },
                    _ => Err(format!(
                            "[fatal][FDIV]: Expected first operand to be atom, found list or instruction" )),
                }
            },
            (InstCell(DIV), new_control) => match self.stack.pop() {
                Some((AtomCell(op1), new_stack)) => match new_stack.pop() {
                    Some((AtomCell(op2), newer_stack)) => Ok((State {
                            stack: newer_stack.push(AtomCell(op1 / op2)),
                            env: self.env,
                            control: new_control,
                            dump: self.dump
                        },None)),
                    any => Err(format!(
                        "[fatal][DIV]: expected second operand, found {:?}\n{}",
                        any,
                        prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                    },
                any => Err(format!(
                    "[fatal][DIV]: expected first operand, found {:?}\n{}",
                    any,
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(MUL), new_control) => match self.stack.pop() {
                Some((AtomCell(op1), new_stack)) => match new_stack.pop() {
                    Some((AtomCell(op2), newer_stack)) => Ok((State {
                            stack: newer_stack.push(AtomCell(op1 * op2)),
                            env: self.env,
                            control: new_control,
                            dump: self.dump
                        }, None)),
                    any => Err(format!(
                        "[fatal][MUL]: expected second operand, found {:?}\n{}",
                        any,
                        prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                    },
                any => Err(format!(
                    "[fatal][MUL]: expected first operand, found {:?}\n{}",
                    any,
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(MOD), new_control) => match self.stack.pop() {
                Some((AtomCell(op1), new_stack)) => match new_stack.pop() {
                    Some((AtomCell(op2), newer_stack)) => Ok((State {
                            stack: newer_stack.push(AtomCell(op1 % op2)),
                            env: self.env,
                            control: new_control,
                            dump: self.dump
                        }, None)),
                    any => Err(format!(
                        "[fatal][MOD]: expected second operand, found {:?}\n{}",
                        any,
                        prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                    },
                any => Err(format!(
                    "[fatal][MOD]: expected first operand, found {:?}\n{}",
                    any,
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(EQ), new_control) => {
                let (op1, new_stack) = self.stack.pop().unwrap();
                let (op2, newer_stack) = new_stack.pop().unwrap();
                match (op1,op2) {
                    (AtomCell(a), AtomCell(b)) => Ok((State {
                        stack: newer_stack.push(
                            match a == b {
                                true    => list_cell![AtomCell(SInt(1))],
                                false   => list_cell![]
                            }),
                        env: self.env,
                        control: new_control,
                        dump: self.dump
                    }, None)),
                    (_,_) => unimplemented!() // TODO: sane error pls
                }
            },
            (InstCell(GT), new_control) => {
                let (op1, new_stack) = self.stack.pop().unwrap();
                let (op2, newer_stack) = new_stack.pop().unwrap();
                match (op1,op2) {
                    (AtomCell(a), AtomCell(b)) => Ok((State {
                        stack: newer_stack.push(
                            match a > b {
                                true    => list_cell![AtomCell(SInt(1))],
                                false   => list_cell![]
                            }
                        ),
                        env: self.env,
                        control: new_control,
                        dump: self.dump
                    }, None)),
                (_,_) => unimplemented!()
                }
            },
            (InstCell(GTE), new_control) => {
                let (op1, new_stack) = self.stack.pop().unwrap();
                let (op2, newer_stack) = new_stack.pop().unwrap();
                match (op1,op2) {
                    (AtomCell(a), AtomCell(b)) => Ok((State {
                        stack: newer_stack.push(
                            match a >= b {
                                true    => list_cell![AtomCell(SInt(1))],
                                false   => list_cell![]
                            }
                        ),
                        env: self.env,
                        control: new_control,
                        dump: self.dump
                    }, None)),
                (_,_) => unimplemented!()
                }
            },
            (InstCell(LT), new_control) => {
                let (op1, new_stack) = self.stack.pop().unwrap();
                let (op2, newer_stack) = new_stack.pop().unwrap();
                match (op1,op2) {
                    (AtomCell(a), AtomCell(b)) => Ok((State {
                        stack: newer_stack.push(
                            match a < b {
                                true    => list_cell![AtomCell(SInt(1))],
                                false   => list_cell![]
                            }
                        ),
                        env: self.env,
                        control: new_control,
                        dump: self.dump
                    }, None)),
                (_,_) => unimplemented!()
                }
            },
            (InstCell(LTE), new_control) => {
                let (op1, new_stack) = self.stack.pop().unwrap();
                let (op2, newer_stack) = new_stack.pop().unwrap();
                match (op1,op2) {
                    (AtomCell(a), AtomCell(b)) => Ok((State {
                        stack: newer_stack.push(
                            match a <= b {
                                true    => list_cell![AtomCell(SInt(1))],
                                false   => list_cell![]
                            }),
                        env: self.env,
                        control: new_control,
                        dump: self.dump
                    }, None)),
                (_,_) => unimplemented!()
                }
            },
            (InstCell(ATOM), new_control) => {
                let (target, new_stack) = self.stack.pop().unwrap();
                Ok((State {
                    stack: new_stack.push(
                        match target {
                            AtomCell(_) => list_cell![AtomCell(SInt(1))],
                            _           => list_cell![]
                        }
                        ),
                    env: self.env,
                    control: new_control,
                    dump: self.dump
                },None))
            },
            (InstCell(AP), new_control) => match self.stack.pop().unwrap() {
                (ListCell(box Cons(ListCell(box func), box Cons(ListCell(params), box Nil))), new_stack) => {
                        match new_stack.pop() {
                            Some((v, newer_stack)) => Ok((State {
                                stack: Stack::empty(),
                                env: match v {
                                    ListCell(_) => params.push(v),
                                    _           => list!(v)
                                },
                                control: func,
                                dump: self.dump
                                    .push(ListCell(Box::new(newer_stack)))
                                    .push(ListCell(Box::new(self.env)))
                                    .push(ListCell(Box::new(new_control)))
                            }, None)),/*
                            Some((v @ AtomCell(_), newer_stack)) => State {
                                stack: Stack::empty(),
                                env: list!( params,ListCell(box list!(v)) ),
                                control: func,
                                dump: self.dump
                                    .push(ListCell(box newer_stack))
                                    .push(ListCell(box self.env))
                                    .push(ListCell(box new_control))
                            },
                            Some((thing, _)) => panic!(
                                "[fatal][AP]: Expected closure on stack, got:\n[fatal]\t{:?}\n{}",
                                thing,
                                prev.map_or(String::new(), |x| x.dump_state("fatal") )),*/
                            None => Err(format!(
                                "[fatal][AP]: expected non-empty stack\n{}",
                                prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                        }
                },
                (_, thing) => Err(format!(
                    "[fatal][AP]: Expected closure on stack, got:\n[fatal]\t{:?}\n{}",
                    thing, prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(RAP), new_control) => match self.stack.pop().unwrap() {
                (ListCell(box Cons(ListCell(box func), box Cons(ListCell(box params), box Nil))), new_stack) => {
                    match new_stack.pop() {
                        Some((v @ ListCell(_), newer_stack)) => Ok(( State {
                            stack: Stack::empty(),
                            env: params.push(v),
                            control: func,
                            dump: self.dump
                                    .push(ListCell(Box::new(new_control)))
                                    .push(ListCell(Box::new(self.env.pop().unwrap().1)))
                                    .push(ListCell(Box::new(newer_stack)))
                        }, None)),
                        Some((thing, _)) => Err(format!(
                            "[fatal][RAP]: Expected closure on stack, got:\n[fatal]\t{:?}\n{}",
                            thing, prev.map_or(String::new(), |x| x.dump_state("fatal") )) ),
                        None => Err(format!(
                            "[fatal][RAP]: expected non-empty stack\n{}",
                            prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                    }
                },
                (_, thing) => Err(format!(
                    "[fatal][RAP]: Expected closure on stack, got:\n[fatal]\t{:?}\n{}",
                    thing, prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(RET), _) => {
                let (head, _) = self.stack.pop().unwrap();
                let (new_stack, new_dump) = try!(match self.dump.pop()  {
                    Some((ListCell(s), d))      => Ok((*s, d)),
                    Some(it @ (AtomCell(_),_))  => Ok((list!(it.0), it.1)),
                    _                           => Err(
                        "[fatal][RET]: Expected non-empty stack")
                });
                let (new_env, newer_dump) = try!(match new_dump.pop() {
                    Some((ListCell(e), d))  => Ok((*e, d)),
                    _                       => Err(
                        "[fatal][RET]: Expected new environment on dump stack")
                });
                let (newer_control, newest_dump) = try!(match newer_dump.pop() {
                    Some((ListCell(c), d))      => Ok((*c, d)),
                    Some(it @ (InstCell(_),_))  => Ok((list!(it.0), it.1)),
                    _                           => Err(
                        "[fatal][RET]: Expected new control stack on dump stack")
                });
                Ok((State {
                    stack: new_stack.push(head),
                    env: new_env,
                    control: newer_control,
                    dump: newest_dump
                }, None))
            },
            (InstCell(DUM), new_control) => Ok((State {
                stack: self.stack,
                env: self.env.push(ListCell(list!())),
                control: new_control,
                dump: self.dump
            }, None)),
            (InstCell(SEL), new_control) => match new_control.pop() {
                Some((ListCell(box true_case), newer_control)) => {
                    match newer_control.pop() {
                        Some((ListCell(box false_case), newest_control)) => {
                            match self.stack.pop() {
                                // False case
                                Some((ListCell(box Nil), new_stack)) => Ok((State {
                                    stack: new_stack,
                                    env: self.env,
                                    control: false_case,
                                    dump: self.dump.push(ListCell(Box::new(newest_control)))
                                }, None)),
                                // True case
                                Some((_, new_stack)) => Ok((State {
                                    stack: new_stack,
                                    env: self.env,
                                    control: true_case,
                                    dump: self.dump.push(ListCell(Box::new(newest_control)))
                                }, None)),
                                None => Err(format!(
                                    "[fatal][SEL]: expected non-empty stack\n{}",
                                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                            }
                        },
                        Some((thing, _)) => Err(format!(
                            "[fatal][SEL]: expected list on control, found {:?}\n{}",
                            thing,prev.map_or(String::new(), |x| x.dump_state("fatal") )) ),
                        None             => Err(format!(
                            "[fatal][SEL]: expected list on control, found nothing\n{}",
                            prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                    }
                },
                Some((thing, _)) => Err(format!(
                    "[fatal][SEL]: expected list on control, found {:?}\n{}",
                    thing,prev.map_or(String::new(), |x| x.dump_state("fatal") )) ),
                None             => Err(format!(
                    "[fatal][SEL]: expected list on control, found nothing\n{}",
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(CAR), new_control) => match self.stack.pop() {
                Some((ListCell(box Cons(car, _)), new_stack)) => Ok(( State {
                    stack: new_stack.push(car),
                    env: self.env,
                    control: new_control,
                    dump: self.dump
                }, None)),
                Some((ListCell(box Nil), _)) => Err(format!(
                    "[fatal][CAR]: expected non-empty list, found Nil\n{}",
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) ),
                Some((thing, _))             => Err(format!(
                    "[fatal][CAR]: expected non-empty list, found {:?}\n{}",
                    thing, prev.map_or(String::new(), |x| x.dump_state("fatal") )) ),
                None                         => Err(format!(
                    "[fatal][CAR]: Expected non-empty list, found nothing\n{}",
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(CDR), new_control) => match self.stack.pop() {
                Some((ListCell(box Cons(_, cdr)), new_stack)) => Ok((State {
                    stack: new_stack.push(ListCell(cdr)),
                    env: self.env,
                    control: new_control,
                    dump: self.dump
                }, None)),
                Some((ListCell(box Nil), _)) => panic!(
                    "[fatal][CDR]: expected non-empty list, found Nil\n{}",
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )),
                Some((thing, _))             => panic!(
                    "[fatal][CDR]: expected non-empty list, found {:?}\n{}",
                    thing, prev.map_or(String::new(), |x| x.dump_state("fatal") )),
                None                        => panic!(
                    "[fatal][CDR]: Expected non-empty list, found nothing\n{}",
                    prev.map_or(String::new(), |x| x.dump_state("fatal") ))
            },
            (InstCell(CONS), new_control) => match self.stack.pop() {
                Some((thing, new_stack)) => {
                    match new_stack.pop() {
                        Some((ListCell(list), newer_stack)) => Ok((State {
                            stack: newer_stack.push(ListCell(Box::new(Cons(thing, list)))),
                            env: self.env,
                            control: new_control,
                            dump: self.dump
                        }, None)),
                        Some((thing_else, _)) => Err(format!(
                            "[fatal][CONS]: Expected a list on the stack, found {:?}\n{}",
                            thing_else,
                            prev.map_or(String::new(), |x| x.dump_state("fatal") )) ),
                        None  => Err(format!(
                            "[fatal][CONS]: Expected a list on the stack, found nothing.\n{}",
                            prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
                    }
                },
                None => Err(format!(
                    "[fatal][CONS]: Expected an item on the stack, found nothing\n{}",
                    prev.map_or(String::new(), |x| x.dump_state("fatal") )) )
            },
            (InstCell(NULL), new_control) => {
                let (target, new_stack) = self.stack.pop().unwrap();
                Ok((State {
                    stack: new_stack.push(
                        match target {
                            ListCell(box Nil) => list_cell![AtomCell(SInt(1))],
                            _                 => list_cell![]
                        }
                        ),
                    env: self.env,
                    control: new_control,
                    dump: self.dump
                }, None))
            },
            (InstCell(WRITEC), new_control) => match self.stack.pop() {
                Some((AtomCell(Char(ch)), new_stack)) => {/*
                    if let Err(msg) = outp.write(&[ch as u8,1]) {
                        panic!("[fatal][WRITEC]: writing failed: {:?}\n{}",
                            msg,
                            prev.map_or(String::new(), |x| x.dump_state("fatal") ))
                    };*/
                    Ok((State {
                        stack: new_stack,
                        env: self.env,
                        control: new_control,
                        dump: self.dump
                    }, Some(IOEvent::Buf(ch))) )
                },
                Some((thing_else,_)) => panic!(
                    "[fatal][WRITEC]: expected char, found {:?}\n{}",
                    thing_else,prev.map_or(String::new(), |x| x.dump_state("fatal") )),
                None => panic!(
                    "[fatal][WRITEC]: expected char, found nothing\n{}",
                    prev.map_or(String::new(), |x| x.dump_state("fatal") ))
            },
            (InstCell(READC), new_control) => {
                // todo: figure out how to make it work with the new thing
                match input {
                    Some(ch) => Ok((State {
                        stack: self.stack.push(AtomCell(Char(ch as char))),
                        env: self.env,
                        control: new_control,
                        dump: self.dump
                    }, None)),
                    _       => panic!("No input, something went wrong (this is not supposed to happen")
                } /*,
                    .map_err(|msg| format!(
                        "[fatal][READC]: could not read, {:?}\n{}",
                        msg,prev.map_or(String::new(), |x| x.dump_state("fatal") )))*/
            },
            (InstCell(STOP), _) => panic!(
                "[fatal]: undefined behaviour\n[fatal]: evaluation of STOP word\n{}",
                prev.map_or(String::new(), |x| x.dump_state("fatal") )
                ),
            (thing, _) => panic!(
                "[fatal]: Tried to evaluate an unsupported cell type {:?}.\n{}",
                thing,
                prev.map_or(String::new(), |x| x.dump_state("fatal") ))
        }
    }
}


/// Evaluates a program.
///
/// Evaluates a program (control stack) and returns the final state.
/// TODO: add (optional?) parameters for stdin and stdout
#[cfg_attr(feature = "nightly", stable(feature="vm_core",since="0.2.0"))]
pub fn eval_program(program: List<SVMCell>,
                    debug: bool)
    -> Result<List<SVMCell>,String> {
    debug!("evaluating {:?}", program);
    let mut machine = State {
        stack:      Stack::empty(),
        env:        Stack::empty(),
        control:    program,
        dump:       Stack::empty()
    };
    // while there are more instructions,
    while {
        machine.control.length() > 0usize &&
        machine.control.peek()!= Some(&InstCell(STOP))
    } {  //TODO: this is kinda heavyweight
        machine = try!(machine.eval(None,debug)).0 // continue evaling
    };
    Ok(machine.stack)
}