-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: FL03 <[email protected]>
- Loading branch information
Showing
25 changed files
with
500 additions
and
558 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
Contrib: FL03 <[email protected]> | ||
*/ | ||
use super::Executor; | ||
use crate::rules::Ruleset; | ||
use crate::rules::RuleSet; | ||
use crate::{Direction, Error, Head, State}; | ||
|
||
/// An [Actor] is an implementation of a Turing machine with a moving head (TMH). | ||
|
@@ -71,7 +71,7 @@ impl<Q, A> Actor<Q, A> { | |
&self.tape | ||
} | ||
/// Returns a mutable reference of the tape as a slice | ||
pub fn tape_mu(&mut self) -> &mut [A] { | ||
pub fn tape_mut(&mut self) -> &mut [A] { | ||
&mut self.tape | ||
} | ||
/// Returns an immutable reference to the head of the tape | ||
|
@@ -104,7 +104,7 @@ impl<Q, A> Actor<Q, A> { | |
} | ||
/// Executes the given program; the method is lazy, meaning it will not compute immediately | ||
/// but will return an [Executor] that is better suited for managing the runtime. | ||
pub fn execute(self, program: Ruleset<Q, A>) -> Executor<Q, A> { | ||
pub fn execute(self, program: RuleSet<Q, A>) -> Executor<Q, A> { | ||
Executor::new(self, program) | ||
} | ||
/// Checks if the tape is empty | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
Contrib: FL03 <[email protected]> | ||
*/ | ||
use super::Actor; | ||
use crate::{Error, Head, Ruleset, State, Symbolic}; | ||
use crate::{Error, Head, RuleSet, State, Symbolic}; | ||
|
||
/// # [Executor] | ||
/// | ||
|
@@ -14,13 +14,13 @@ pub struct Executor<Q, S> { | |
/// the actor that will be executing the program | ||
pub(crate) actor: Actor<Q, S>, | ||
/// the program being executed | ||
pub(crate) program: Ruleset<Q, S>, | ||
pub(crate) program: RuleSet<Q, S>, | ||
/// the number of steps taken by the actor | ||
pub(crate) steps: usize, | ||
} | ||
|
||
impl<Q, S> Executor<Q, S> { | ||
pub(crate) fn new(actor: Actor<Q, S>, program: Ruleset<Q, S>) -> Self { | ||
pub(crate) fn new(actor: Actor<Q, S>, program: RuleSet<Q, S>) -> Self { | ||
Self { | ||
actor, | ||
program, | ||
|
@@ -34,22 +34,26 @@ impl<Q, S> Executor<Q, S> { | |
{ | ||
Self { | ||
actor, | ||
program: Ruleset { | ||
program: RuleSet { | ||
initial_state: Default::default(), | ||
rules: Vec::new(), | ||
}, | ||
steps: 0, | ||
} | ||
} | ||
/// Load a program into the executor | ||
pub fn load(self, program: Ruleset<Q, S>) -> Self { | ||
pub fn load(self, program: RuleSet<Q, S>) -> Self { | ||
Executor { program, ..self } | ||
} | ||
|
||
pub const fn actor(&self) -> &Actor<Q, S> { | ||
&self.actor | ||
} | ||
|
||
pub fn steps(&self) -> usize { | ||
self.steps | ||
} | ||
|
||
pub fn current_state(&self) -> State<&'_ Q> { | ||
self.actor.state() | ||
} | ||
|
@@ -58,6 +62,14 @@ impl<Q, S> Executor<Q, S> { | |
self.actor.read() | ||
} | ||
|
||
/// Reads the current symbol at the head of the tape | ||
pub fn read_or_default(&self) -> Head<&Q, &S> { | ||
self.actor.read().unwrap_or_else(|_| Head { | ||
state: self.actor.state(), | ||
symbol: &S::default(), | ||
}) | ||
} | ||
|
||
#[cfg_attr( | ||
feature = "tracing", | ||
tracing::instrument(skip_all, name = "run", target = "actor") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
/* | ||
Appellation: store <module> | ||
Contrib: FL03 <[email protected]> | ||
*/ | ||
|
||
/// [RawMemory] is a trait that provides a common interface for memory storage. | ||
pub trait RawMemory { | ||
type Elem; | ||
|
||
private!(); | ||
|
||
fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
|
||
fn len(&self) -> usize; | ||
} | ||
|
||
pub trait Memory: RawMemory { | ||
fn to_vec(&self) -> Vec<Self::Elem> | ||
where | ||
Self::Elem: Clone; | ||
} | ||
|
||
pub trait MemoryMut: Memory { | ||
fn clear(&mut self); | ||
|
||
fn insert(&mut self, index: usize, elem: Self::Elem); | ||
|
||
fn remove(&mut self, index: usize) -> Option<Self::Elem>; | ||
} | ||
|
||
/// [Sequential] extends the base trait [RawMemory] to provide sequential access to memory. | ||
pub trait SeqMemory: Memory { | ||
fn as_ptr(&self) -> *const Self::Elem; | ||
|
||
fn as_mut_ptr(&mut self) -> *mut Self::Elem; | ||
|
||
fn as_slice(&self) -> &[Self::Elem]; | ||
|
||
fn as_mut_slice(&mut self) -> &mut [Self::Elem]; | ||
|
||
fn get<I>(&self, index: I) -> Option<&I::Output> | ||
where | ||
I: core::slice::SliceIndex<[Self::Elem]>; | ||
|
||
fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output> | ||
where | ||
I: core::slice::SliceIndex<[Self::Elem]>; | ||
} | ||
|
||
#[doc(hidden)] | ||
pub trait Fetch<K> { | ||
type Output; | ||
|
||
fn fetch(&self, index: K) -> Option<&Self::Output>; | ||
|
||
fn fetch_mut(&mut self, index: K) -> Option<&mut Self::Output>; | ||
} | ||
|
||
/* | ||
************* Implementations ************* | ||
*/ | ||
impl<I, T> Fetch<I> for [T] | ||
where | ||
I: core::slice::SliceIndex<[T]>, | ||
I::Output: Sized, | ||
{ | ||
type Output = I::Output; | ||
|
||
fn fetch(&self, index: I) -> Option<&Self::Output> { | ||
<[T]>::get(self, index) | ||
} | ||
|
||
fn fetch_mut(&mut self, index: I) -> Option<&mut Self::Output> { | ||
<[T]>::get_mut(self, index) | ||
} | ||
} | ||
|
||
impl<T> RawMemory for [T] { | ||
type Elem = T; | ||
|
||
seal!(); | ||
|
||
fn is_empty(&self) -> bool { | ||
<[T]>::is_empty(self) | ||
} | ||
|
||
fn len(&self) -> usize { | ||
<[T]>::len(self) | ||
} | ||
} | ||
|
||
impl<T> Memory for [T] { | ||
fn to_vec(&self) -> Vec<T> | ||
where | ||
T: Clone, | ||
{ | ||
<[T]>::to_vec(self) | ||
} | ||
} | ||
|
||
impl<T> SeqMemory for [T] { | ||
fn as_ptr(&self) -> *const T { | ||
<[T]>::as_ptr(self) | ||
} | ||
|
||
fn as_mut_ptr(&mut self) -> *mut T { | ||
<[T]>::as_mut_ptr(self) | ||
} | ||
|
||
fn as_slice(&self) -> &[T] { | ||
self | ||
} | ||
|
||
fn as_mut_slice(&mut self) -> &mut [T] { | ||
self | ||
} | ||
|
||
fn get<I>(&self, index: I) -> Option<&I::Output> | ||
where | ||
I: core::slice::SliceIndex<[T]>, | ||
{ | ||
<[T]>::get(self, index) | ||
} | ||
|
||
fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output> | ||
where | ||
I: core::slice::SliceIndex<[T]>, | ||
{ | ||
<[T]>::get_mut(self, index) | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
mod impl_alloc { | ||
use alloc::vec::Vec; | ||
|
||
impl<T> super::RawMemory for Vec<T> { | ||
type Elem = T; | ||
|
||
seal!(); | ||
|
||
fn is_empty(&self) -> bool { | ||
Vec::is_empty(self) | ||
} | ||
|
||
fn len(&self) -> usize { | ||
Vec::len(self) | ||
} | ||
} | ||
|
||
impl<T> super::Memory for Vec<T> { | ||
fn to_vec(&self) -> Vec<T> | ||
where | ||
T: Clone, | ||
{ | ||
self.clone() | ||
} | ||
} | ||
|
||
impl<T> super::SeqMemory for Vec<T> | ||
where | ||
Vec<T>: AsRef<[T]>, | ||
{ | ||
fn as_ptr(&self) -> *const T { | ||
Vec::as_ptr(self) | ||
} | ||
|
||
fn as_mut_ptr(&mut self) -> *mut T { | ||
Vec::as_mut_ptr(self) | ||
} | ||
|
||
fn as_slice(&self) -> &[T] { | ||
Vec::as_slice(self) | ||
} | ||
|
||
fn as_mut_slice(&mut self) -> &mut [T] { | ||
Vec::as_mut_slice(self) | ||
} | ||
|
||
fn get<I>(&self, index: I) -> Option<&I::Output> | ||
where | ||
I: core::slice::SliceIndex<[T]>, | ||
{ | ||
<[T]>::get(self, index) | ||
} | ||
|
||
fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output> | ||
where | ||
I: core::slice::SliceIndex<[T]>, | ||
{ | ||
<[T]>::get_mut(self, index) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
mod impl_std { | ||
use std::collections::HashMap; | ||
|
||
impl<K, V> super::RawMemory for HashMap<K, V> { | ||
type Elem = V; | ||
|
||
seal!(); | ||
|
||
fn is_empty(&self) -> bool { | ||
HashMap::is_empty(self) | ||
} | ||
|
||
fn len(&self) -> usize { | ||
HashMap::len(self) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.