Skip to main content

rstm_core/rules/traits/
instruction.rs

1/*
2    Appellation: instruction <module>
3    Created At: 2026.01.15:11:50:07
4    Contrib: @FL03
5*/
6use crate::rules::{Head, RawHead, RawTail, Rule, Tail};
7use rstm_state::{RawState, State};
8
9/// The [`Instruction`] trait establishes the base interface for all compatible rules for the
10/// automata.
11pub trait Instruction<Q, A>
12where
13    Q: RawState,
14{
15    /// the type of head used by the instruction
16    type Head: RawHead<State = Q, Symbol = A>;
17    /// the type of tail used by the instruction
18    type Tail: RawTail<State = Q, Symbol = A>;
19    /// returns a reference to the head of the instruction
20    fn head(&self) -> &Self::Head;
21    /// returns a reference to the tail of the instruction
22    fn tail(&self) -> &Self::Tail;
23    /// returns a reference to the current state
24    fn current_state(&self) -> &State<Q>;
25    /// returns a reference to the current symbol
26    fn symbol(&self) -> &A;
27    /// returns the direction of the tail
28    fn direction(&self) -> crate::Direction {
29        self.tail().direction()
30    }
31    /// returns a reference to the next state
32    fn next_state(&self) -> &State<Q>;
33    /// returns a reference to the next symbol
34    fn next_symbol(&self) -> &A;
35}
36
37pub trait InstructionMut<Q, A>: Instruction<Q, A>
38where
39    Q: RawState,
40{
41    /// returns a mutable reference to the head of the instruction
42    fn head_mut(&mut self) -> &mut Self::Head;
43    /// returns a mutable reference to the tail of the instruction
44    fn tail_mut(&mut self) -> &mut Self::Tail;
45    /// returns a mutable reference to the current state
46    fn current_state_mut(&mut self) -> &mut State<Q>;
47    /// returns a mutable reference to the current symbol
48    fn symbol_mut(&mut self) -> &mut A;
49    /// returns a mutable reference to the next state
50    fn next_state_mut(&mut self) -> &mut State<Q>;
51    /// returns a mutable reference to the next symbol
52    fn next_symbol_mut(&mut self) -> &mut A;
53}
54
55/*
56 ************* Implementations *************
57*/
58
59impl<Q, A> Instruction<Q, A> for (Head<Q, A>, Tail<Q, A>)
60where
61    Q: RawState,
62{
63    type Head = Head<Q, A>;
64    type Tail = Tail<Q, A>;
65
66    fn head(&self) -> &Self::Head {
67        &self.0
68    }
69
70    fn tail(&self) -> &Self::Tail {
71        &self.1
72    }
73
74    fn current_state(&self) -> &State<Q> {
75        self.head().state()
76    }
77
78    fn symbol(&self) -> &A {
79        self.head().symbol()
80    }
81
82    fn next_state(&self) -> &State<Q> {
83        self.tail().next_state()
84    }
85
86    fn next_symbol(&self) -> &A {
87        self.tail().write_symbol()
88    }
89}
90
91impl<Q, A> Instruction<Q, A> for Rule<Q, A>
92where
93    Q: RawState,
94{
95    type Head = Head<Q, A>;
96    type Tail = Tail<Q, A>;
97
98    fn head(&self) -> &Self::Head {
99        &self.head
100    }
101
102    fn tail(&self) -> &Self::Tail {
103        &self.tail
104    }
105
106    fn current_state(&self) -> &State<Q> {
107        self.head().state()
108    }
109
110    fn symbol(&self) -> &A {
111        self.head().symbol()
112    }
113
114    fn next_state(&self) -> &State<Q> {
115        self.tail().next_state()
116    }
117
118    fn next_symbol(&self) -> &A {
119        self.tail().write_symbol()
120    }
121}
122
123impl<Q, A> InstructionMut<Q, A> for Rule<Q, A>
124where
125    Q: RawState,
126{
127    fn head_mut(&mut self) -> &mut Self::Head {
128        &mut self.head
129    }
130
131    fn tail_mut(&mut self) -> &mut Self::Tail {
132        &mut self.tail
133    }
134
135    fn current_state_mut(&mut self) -> &mut State<Q> {
136        self.head_mut().state_mut()
137    }
138
139    fn symbol_mut(&mut self) -> &mut A {
140        self.head_mut().symbol_mut()
141    }
142
143    fn next_state_mut(&mut self) -> &mut State<Q> {
144        self.tail_mut().state_mut()
145    }
146
147    fn next_symbol_mut(&mut self) -> &mut A {
148        self.tail_mut().symbol_mut()
149    }
150}
151
152impl<Q, A> InstructionMut<Q, A> for (Head<Q, A>, Tail<Q, A>)
153where
154    Q: RawState,
155{
156    fn head_mut(&mut self) -> &mut Self::Head {
157        &mut self.0
158    }
159
160    fn tail_mut(&mut self) -> &mut Self::Tail {
161        &mut self.1
162    }
163
164    fn current_state_mut(&mut self) -> &mut State<Q> {
165        self.head_mut().state_mut()
166    }
167
168    fn symbol_mut(&mut self) -> &mut A {
169        self.head_mut().symbol_mut()
170    }
171
172    fn next_state_mut(&mut self) -> &mut State<Q> {
173        self.tail_mut().state_mut()
174    }
175
176    fn next_symbol_mut(&mut self) -> &mut A {
177        self.tail_mut().symbol_mut()
178    }
179}