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
/*
    Appellation: machine <module>
    Contrib: FL03 <jo3mccain@icloud.com>
    Description: ... Summary ...
*/
use super::Driver;
use crate::instructions::Instruction;
use crate::{Alphabet, Program, Scope, State, Symbolic, Tape, Translate, Turing};
use contained_core::{states::Stateful, ArrayLike, Error};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub struct Machine<S: Symbolic = String> {
    pub driver: Driver<S>,
    pub program: Program<S>,
}

impl<S: Symbolic> Machine<S> {
    pub fn new(driver: Driver<S>, program: Program<S>) -> Self {
        Self { driver, program }
    }
    pub fn program(&self) -> Program<S> {
        self.program.clone()
    }
    pub fn scope(&self) -> Driver<S> {
        self.driver.clone()
    }
    pub fn tape(&self) -> Tape<S> {
        self.driver.tape()
    }
}

impl<S: Symbolic> Extend<S> for Machine<S> {
    fn extend<T: IntoIterator<Item = S>>(&mut self, iter: T) {
        self.driver.memory.extend(iter)
    }
}

impl<S: Symbolic> Extend<Instruction<S>> for Machine<S> {
    fn extend<T: IntoIterator<Item = Instruction<S>>>(&mut self, iter: T) {
        self.program.extend(iter)
    }
}

impl<S: Symbolic> Iterator for Machine<S> {
    type Item = Instruction<S>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(cur) = self.clone().driver.tape().get(self.driver.cursor()) {
            // Get the instruction
            self.program
                .get((self.state(), cur.clone()).into())
                .cloned()
        } else {
            None
        }
    }
}

impl<S: Symbolic> Alphabet<S> for Machine<S> {
    fn is_viable(&self, symbol: &S) -> bool {
        self.program.is_viable(symbol)
    }
    fn default_symbol(&self) -> S {
        self.program.default_symbol()
    }
}

impl<S: Symbolic> Stateful<State> for Machine<S> {
    fn state(&self) -> State {
        self.driver.state()
    }

    fn update_state(&mut self, state: State) {
        self.driver.update_state(state)
    }
}

impl<S: Symbolic> Turing<S> for Machine<S> {
    type Scope = Driver<S>;

    fn execute(&mut self) -> Result<&Self, Self::Error> {
        let until = |actor: &Driver<S>| actor.state() == State::Invalid;
        self.execute_until(until)
    }

    fn execute_once(&mut self) -> Result<&Self, Self::Error> {
        let head = self.driver.clone().into();
        let inst = self.program.get(head).expect("").clone();
        self.driver.update_state(inst.tail().state());
        self.driver.set_symbol(inst.tail().symbol());
        self.driver
            .shift(inst.tail().action(), self.program.default_symbol());
        Ok(self)
    }

    fn execute_until(
        &mut self,
        until: impl Fn(&Self::Scope) -> bool,
    ) -> Result<&Self, Self::Error> {
        while !until(&self.driver) {
            self.execute_once()?;
        }
        Ok(self)
    }
}

impl<S: Symbolic> Translate<S> for Machine<S> {
    type Error = Error;

    fn translate(&mut self, tape: Tape<S>) -> Result<Tape<S>, Self::Error> {
        self.driver = Driver::from(tape);
        self.execute()?;
        Ok(self.driver.tape())
    }
}