rstm_core/traits/
instruction.rs1use crate::{Direction, Head, Rule, Symbol, Tail};
7use rstm_state::{RawState, State};
8
9pub trait Scope<Q, S>
11where
12 Q: RawState,
13{
14 fn current_state(&self) -> &State<Q>;
16
17 fn current_symbol(&self) -> &S;
18}
19
20pub trait Directive<Q, S>
23where
24 Q: RawState,
25{
26 fn direction(&self) -> Direction;
27
28 fn next_state(&self) -> &State<Q>;
29
30 fn next_symbol(&self) -> &S;
31}
32
33pub trait Instruction<Q, S>
36where
37 Q: RawState,
38{
39 fn direction(&self) -> Direction;
41 fn current_state(&self) -> &State<Q>;
43 fn next_state(&self) -> &State<Q>;
45 fn symbol(&self) -> &S;
47 fn write_symbol(&self) -> &S;
49 fn head(&self) -> Head<&Q, &S> {
51 Head {
52 state: self.current_state().view(),
53 symbol: self.symbol(),
54 }
55 }
56 fn tail(&self) -> Tail<&Q, &S> {
58 Tail {
59 direction: self.direction(),
60 next_state: self.next_state().view(),
61 write_symbol: self.write_symbol(),
62 }
63 }
64 fn as_rule(&self) -> Rule<&Q, &S> {
66 Rule {
67 head: self.head(),
68 tail: self.tail(),
69 }
70 }
71}
72
73impl<A, Q, S> Instruction<Q, S> for A
77where
78 A: Scope<Q, S> + Directive<Q, S>,
79 Q: RawState,
80 S: Symbol,
81{
82 fn direction(&self) -> Direction {
83 self.direction()
84 }
85
86 fn current_state(&self) -> &State<Q> {
87 self.current_state()
88 }
89
90 fn next_state(&self) -> &State<Q> {
91 self.next_state()
92 }
93
94 fn symbol(&self) -> &S {
95 self.current_symbol()
96 }
97
98 fn write_symbol(&self) -> &S {
99 self.next_symbol()
100 }
101}
102
103impl<Q, S> Scope<Q, S> for (State<Q>, S)
104where
105 Q: RawState,
106{
107 fn current_state(&self) -> &State<Q> {
108 &self.0
109 }
110
111 fn current_symbol(&self) -> &S {
112 &self.1
113 }
114}
115
116impl<Q, S> Scope<Q, S> for Head<Q, S>
117where
118 Q: RawState,
119{
120 fn current_state(&self) -> &State<Q> {
121 self.state()
122 }
123
124 fn current_symbol(&self) -> &S {
125 &self.symbol
126 }
127}
128
129impl<Q, S> Scope<Q, S> for Rule<Q, S>
130where
131 Q: RawState,
132{
133 fn current_state(&self) -> &State<Q> {
134 self.state()
135 }
136
137 fn current_symbol(&self) -> &S {
138 self.symbol()
139 }
140}
141
142impl<Q, S> Directive<Q, S> for (Direction, State<Q>, S)
143where
144 Q: RawState,
145{
146 fn direction(&self) -> Direction {
147 self.0
148 }
149
150 fn next_state(&self) -> &State<Q> {
151 &self.1
152 }
153
154 fn next_symbol(&self) -> &S {
155 &self.2
156 }
157}
158
159impl<Q, S> Directive<Q, S> for Tail<Q, S>
160where
161 Q: RawState,
162{
163 fn direction(&self) -> Direction {
164 self.direction
165 }
166
167 fn next_state(&self) -> &State<Q> {
168 self.state()
169 }
170
171 fn next_symbol(&self) -> &S {
172 self.symbol()
173 }
174}
175
176impl<Q, S> Directive<Q, S> for Rule<Q, S>
177where
178 Q: RawState,
179{
180 fn direction(&self) -> Direction {
181 self.direction()
182 }
183
184 fn next_state(&self) -> &State<Q> {
185 self.tail().state()
186 }
187
188 fn next_symbol(&self) -> &S {
189 self.write_symbol()
190 }
191}