Skip to content

Commit

Permalink
Merge branch 'main' of codeberg.org:functional-tim/rusty_turing_machine
Browse files Browse the repository at this point in the history
merging
  • Loading branch information
functional-tim committed Nov 9, 2024
2 parents ce402a8 + d27a2b8 commit 52e5f4b
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 46 deletions.
79 changes: 64 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[package]
name = "rusty-turing-machine"
version = "0.1.0"
version = "0.1.1"
authors = ["functional-tim <[email protected]>"]
edition = "2018"
edition = "2021"
description = "A simple Turing machine command line program. It is reasonably fast."
readme = "README.md"
homepage = "https://github.com/functional-tim/rusty-turing-machine"
repository = "https://github.com/functional-tim/rusty-turing-machine"
homepage = "https://github.com/functional-tim/rusty_turing_machine"
repository = "https://github.com/functional-tim/rusty_turing_machine"
license = "MIT OR Apache-2.0"
keywords = ["education", "turing-machine"]
categories = ["command-line-utilities"]

[dependencies]
exitcode = "^1.1"
indexmap = { version = "^1.8", features = ["serde-1"] }
indexmap = { version = "^2.2", features = ["serde"] }
serde = "^1.0"
serde_derive = "^1.0"
structopt = "^0.3"
toml = "^0.5"
toml = "^0.8"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# rusty-turing-machine

[![Crates.io](https://img.shields.io/crates/v/rusty-turing-machine.svg)](https://crates.io/crates/rusty-turing-machine)
[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/functional-tim/blob/main/LICENSE)
[![License](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/functional-tim/rusty-turing-machine/blob/main/LICENSE-MIT)
[![dependency status](https://deps.rs/repo/github/functional-tim/rusty-turing-machine/status.svg)](https://deps.rs/repo/github/functional-tim/rusty-turing-machine)

-----------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use structopt::StructOpt;

mod turing_machine;

/// Struct for the parameters of the app.
// Struct for the parameters of the app.
#[derive(StructOpt)]
#[structopt(
name = "rusty-turing-machine",
Expand All @@ -39,7 +39,7 @@ struct OptR {
#[structopt(short = "p", long = "print")]
pr: bool,

/// Count 1s especially for busy beavers
/// Count 1s especially useful for busy beavers
#[structopt(short = "c", long = "count")]
count: bool,

Expand Down
51 changes: 29 additions & 22 deletions src/turing_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,77 @@
*/

use indexmap::map::IndexMap;
use serde_derive::{Serialize, Deserialize};
use serde_derive::{Deserialize, Serialize};
//use std::collections::HashMap;
use std::collections::VecDeque;
use std::fmt;

/// Implementation of a Turing machine
// Implementation of a Turing machine
#[derive(Debug, Deserialize, Serialize)]
pub struct TuringMachine {
// Number of steps taken by the Turing machine
steps: usize,
// Current state of the Turing machine
state: String,
// Table of instructions for the Turing machine
table: IndexMap<String, IndexMap<String, (String, Move, String)>>,
// Tape of the Turing machine
tape: Tape,
}

impl TuringMachine {
/// Count the ones on the tape especially useful for the busy beavers game
// Count the ones on the tape especially useful for the busy beaver game
pub fn count1s(&mut self) -> u128 {
self.tape.count1s()
}

/// Run the Turing machine until it halts (if it halts ;) ).
// Run the Turing machine until it halts (if it halts ;) ).
pub fn run(&mut self) {
while self.state != "HALT" {
self.step();
}
}

/// Run the Turing machine until it halts (if it halts). Print every step of that.
// Run the Turing machine until it halts (if it halts). Print every step of that.
pub fn run_print(&mut self) {
while self.state != "HALT" {
self.step();
println!("{}", self);
}
}

/// Do one step of the Turing machine.
// Do one step of the Turing machine.
pub fn step(&mut self) {
if self.state != "HALT" {
self.steps += 1;
// Panic if the current value is not in the table
let next = match self.table.get(&self.state) {
Some(x) => match x.get(&self.tape.center) {
Some(x) => x,
None => panic!("Error1"),
},
None => panic!("Error2"),
};
// Get the new value for the position
self.tape.center = next.0.clone();
// Move according to the rule
self.tape.mov(next.1);
// Set the new state according to the rule
self.state = next.2.to_string();
}
}
}

/// Implementation of the movement of the head of the tape.
// Implementation of the movement instructions of the head of the tape.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum Move {
L,
R,
N,
}

/// Implementation of the tape of the Turing machine.
// Implementation of the tape of the Turing machine.
// Using VecDeque to have fast speed
#[derive(Debug, Deserialize, Serialize)]
pub struct Tape {
left: VecDeque<String>,
Expand All @@ -78,24 +87,26 @@ pub struct Tape {
}

impl Tape {
// Function to count the ones on the tape for something like the busy beaver game
fn count1s(&mut self) -> u128 {
let mut steps = 0;
let mut ones = 0;
for t in self.left.iter() {
if t == "1" {
steps += 1;
ones += 1;
}
}
if self.center == "1" {
steps += 1;
ones += 1;
}
for t in self.right.iter() {
if t == "1" {
steps += 1;
ones += 1;
}
}
steps
ones
}

// Move the head of the Turing machine on the tape
fn mov(&mut self, dir: Move) {
if dir == Move::L {
self.right.push_front(self.center.clone());
Expand All @@ -116,11 +127,7 @@ impl Tape {

impl fmt::Display for TuringMachine {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Steps: {}\nState: {}\nTable:\n",
self.steps, self.state
)?;
write!(f, "Steps: {}\nState: {}\nTable:\n", self.steps, self.state)?;
for (count, (s, c)) in self.table.iter().enumerate() {
if count != 0 {
writeln!(f)?;
Expand All @@ -147,17 +154,17 @@ impl fmt::Display for Move {

impl fmt::Display for Tape {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "|")?;
write!(f, "--")?;
for t in self.left.iter().rev() {
write!(f, "|{}", t)?;
write!(f, "-{}", t)?;
}
write!(f, "[{}]", self.center)?;
for (count, t) in self.right.iter().enumerate() {
if count != 0 {
write!(f, "|")?;
write!(f, "-")?;
}
write!(f, "{}", t)?;
}
write!(f, "||")
write!(f, "---")
}
}

0 comments on commit 52e5f4b

Please sign in to comment.