feat: implement a single turing machine step
This commit is contained in:
1 parent
d2ad40262b
commit
31a5c96fbf
2 files changed
+46
-14
No files matched your search
+38
-13
@@ -2,32 +2,57 @@ use std::{collections::HashMap, num::NonZeroUsize};
|
|||||||
|
|
||||||
use crate::tape::{Tape, TapeSymbol};
|
use crate::tape::{Tape, TapeSymbol};
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct TuringMachine {
|
struct TuringMachine {
|
||||||
state_table: HashMap<(MachineState, TapeSymbol), MachineAction>,
|
state_table: HashMap<(StateNumberType, TapeSymbol), MachineAction>,
|
||||||
|
current_state: MachineState,
|
||||||
tape: Tape,
|
tape: Tape,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TuringMachine {
|
impl TuringMachine {
|
||||||
fn new() -> Self {
|
fn with_state_entries(
|
||||||
TuringMachine::default()
|
state_entries: impl Iterator<Item = (StateNumberType, TapeSymbol, MachineAction)>,
|
||||||
|
initial_state: MachineState,
|
||||||
|
) -> Self {
|
||||||
|
let mut state_table = HashMap::new();
|
||||||
|
for (state, symbol, action) in state_entries {
|
||||||
|
state_table.insert((state, symbol), action);
|
||||||
|
}
|
||||||
|
TuringMachine {
|
||||||
|
state_table,
|
||||||
|
current_state: initial_state,
|
||||||
|
tape: Tape::default(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_state_entries(
|
fn step(&mut self) {
|
||||||
state_entries: impl Iterator<Item = (MachineState, TapeSymbol, MachineAction)>,
|
let state_number = match self.current_state {
|
||||||
) -> Self {
|
MachineState::Halt => return,
|
||||||
let mut machine = TuringMachine::new();
|
MachineState::StateNumber(n) => n,
|
||||||
for (state, symbol, action) in state_entries {
|
};
|
||||||
machine.state_table.insert((state, symbol), action);
|
let symbol = self.tape.get_symbol();
|
||||||
|
match self.state_table.get(&(state_number, symbol)) {
|
||||||
|
None => {
|
||||||
|
self.current_state = MachineState::Halt;
|
||||||
|
}
|
||||||
|
Some(MachineAction {
|
||||||
|
tape_write,
|
||||||
|
movement,
|
||||||
|
new_state,
|
||||||
|
}) => {
|
||||||
|
self.tape.write_symbol(*tape_write);
|
||||||
|
self.tape.move_head(movement);
|
||||||
|
self.current_state = *new_state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
machine
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, Hash, PartialEq)]
|
type StateNumberType = NonZeroUsize;
|
||||||
|
|
||||||
|
#[derive(Eq, Hash, PartialEq, Clone, Copy)]
|
||||||
enum MachineState {
|
enum MachineState {
|
||||||
Halt,
|
Halt,
|
||||||
StateNumber(NonZeroUsize),
|
StateNumber(StateNumberType),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MachineAction {
|
struct MachineAction {
|
||||||
|
|||||||
+8
-1
@@ -50,6 +50,13 @@ impl Tape {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_symbol(&self) -> TapeSymbol {
|
||||||
|
match self.head.side {
|
||||||
|
TapeSide::Left => self.left_half[self.head.index],
|
||||||
|
TapeSide::Right => self.right_half[self.head.index],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn write_symbol(&mut self, symbol: TapeSymbol) {
|
pub fn write_symbol(&mut self, symbol: TapeSymbol) {
|
||||||
match self.head.side {
|
match self.head.side {
|
||||||
TapeSide::Left => self.left_half[self.head.index] = symbol,
|
TapeSide::Left => self.left_half[self.head.index] = symbol,
|
||||||
@@ -58,7 +65,7 @@ impl Tape {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Eq, Hash, PartialEq)]
|
#[derive(Eq, Hash, PartialEq, Clone, Copy)]
|
||||||
pub enum TapeSymbol {
|
pub enum TapeSymbol {
|
||||||
Zero,
|
Zero,
|
||||||
One,
|
One,
|
||||||
|
|||||||
Reference in new issue
Block a user