Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: FL03 <[email protected]>
  • Loading branch information
FL03 committed Sep 12, 2024
1 parent 0f3236a commit 8363893
Show file tree
Hide file tree
Showing 25 changed files with 500 additions and 558 deletions.
6 changes: 3 additions & 3 deletions core/src/actors/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions core/src/actors/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
///
Expand All @@ -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,
Expand All @@ -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()
}
Expand All @@ -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")
Expand Down
16 changes: 8 additions & 8 deletions core/src/actors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ pub(crate) mod prelude {

use crate::{Direction, Error, Head, Rule, State, Tail};

/// [Handle] describes the step-by-step execution of a program; the trait is generalized
/// with the introduction of a single generic parameter, [Args], capable of sufficiently
/// [Handle] describes the step-by-step execution of a program; the trait is generalized
/// with the introduction of a single generic parameter, `T`, capable of sufficiently
/// representing any possible object that may be passed to the [handle] method.
///
/// This notion is particularly useful as it allows us to define the process using an actor,
/// before generically implementing the [Engine] trait for the [Executor] struct. Doing so
/// allows for further abstraction by considering the
pub trait Handle<Args> {
///
/// This notion is particularly useful as it allows us to define the process using an actor,
/// before generically implementing the [Engine] trait for the [Executor] struct. Doing so
/// allows for further abstraction by considering the
pub trait Handle<T> {
type Output;

fn handle(&mut self, args: Args) -> Self::Output;
fn handle(&mut self, args: T) -> Self::Output;
}

#[doc(hidden)]
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ extern crate alloc;

#[doc(inline)]
pub use self::{
actors::Actor,
actors::{Actor, Executor, Handle},
error::Error,
rules::{Rule, Ruleset},
rules::{Rule, RuleSet},
state::State,
traits::prelude::*,
types::prelude::*,
Expand Down
215 changes: 215 additions & 0 deletions core/src/mem/memory.rs
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)
}
}
}
6 changes: 5 additions & 1 deletion core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
//!
//!
#[doc(inline)]
pub use self::store::*;
pub use self::memory::*;

pub mod memory;
#[doc(hidden)]
pub mod store;
pub mod tape;

pub(crate) mod prelude {
pub use super::tape::prelude::*;
}

pub(crate) type Cell<Q, S> = crate::Head<Q, S>;
Loading

0 comments on commit 8363893

Please sign in to comment.