Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: FL03 <jo3mccain@icloud.com>
  • Loading branch information
FL03 committed Sep 12, 2024
1 parent 8363893 commit 17e154e
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 19 deletions.
11 changes: 0 additions & 11 deletions core/src/actors/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,6 @@ impl<Q, A> Actor<Q, A> {
}
}

use crate::traits::transform::Handle;
use crate::Tail;

impl<Q, A> Handle<Tail<Q, A>> for Actor<Q, A> {
type Output = Head<Q, usize>;

fn handle(&mut self, args: Tail<Q, A>) -> Self::Output {
self.step(args.direction, args.state, args.symbol)
}
}

impl<Q, S> core::fmt::Debug for Actor<Q, S>
where
S: core::fmt::Debug,
Expand Down
76 changes: 76 additions & 0 deletions core/src/actors/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Appellation: base <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::{Alphabet, Head, State};

pub struct ActorBase<Q, S>

Check warning

Code scanning / clippy

field tape is never read Warning

field tape is never read
where
S: Alphabet,
{
/// the head of the tape
pub(crate) head: Head<Q, *mut S::Elem>,
/// the input alphabet
pub(crate) tape: S,

Check warning

Code scanning / clippy

field tape is never read Warning

field tape is never read
}

impl<A, Q, S> ActorBase<Q, S>
where
S: Alphabet<Elem = A>,
{
pub fn new(alpha: S, state: State<Q>, symbol: *mut A) -> Self {
Self {
head: Head { state, symbol },
tape: alpha,
}
}
/// Constructs a new [Actor] with the given state; assumes the tape is empty and the head
/// is located at `0`.
pub fn from_state(state: State<Q>) -> Self
where
S: Default,
{
Self {
head: Head {
state,
symbol: core::ptr::null_mut(),
},
tape: S::default(),
}
}
/// Consumes the current instance and returns a new instance with the given alphabet
pub fn with_alpha(self, alpha: S) -> Self {
Self {
tape: alpha,
..self
}
}
/// Consumes the current instance and returns a new instance with the given head
pub fn with_head(self, head: Head<Q, *mut A>) -> Self {
Self { head, ..self }
}
/// Consumes the current instance and returns a new instance with the given position
pub fn with_ptr(self, symbol: *mut A) -> Self {
Self {
head: Head {
symbol,
..self.head
},
..self
}
}
/// Consumes the current instance and returns a new instance with the given state
pub fn with_state(self, state: State<Q>) -> Self {
Self {
head: Head { state, ..self.head },
..self
}
}
/// Consumes the current instance and returns a new instance with the given state
pub fn with_state_symbol(self, state: State<Q>, symbol: *mut S::Elem) -> Self {
Self {
head: Head { state, symbol },
..self
}
}
}
32 changes: 27 additions & 5 deletions core/src/actors/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,33 @@ impl<Q, S> Executor<Q, S> {
}

/// 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(),
})
pub fn read_option(&self) -> Head<&Q, Option<&S>> {
if let Ok(Head { state, symbol }) = self.read() {
Head {
state,
symbol: Some(symbol),
}
} else {
Head {
state: self.actor.state(),
symbol: None,
}
}
}

/// Reads the current symbol at the head of the tape
pub fn read_uninit(&self) -> Head<&Q, core::mem::MaybeUninit<&S>> {
if let Ok(Head { state, symbol }) = self.read() {
Head {
state,
symbol: core::mem::MaybeUninit::new(symbol),
}
} else {
Head {
state: self.actor.state(),
symbol: core::mem::MaybeUninit::uninit(),
}
}
}

#[cfg_attr(
Expand Down
2 changes: 2 additions & 0 deletions core/src/actors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
pub use self::{actor::Actor, exec::Executor};

pub(crate) mod actor;
#[doc(hidden)]
pub mod base;
pub(crate) mod exec;

pub(crate) mod prelude {
Expand Down
2 changes: 1 addition & 1 deletion core/src/rules/impls/impl_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Appellation: impl_rule <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::rule::Rule;
use crate::rules::Rule;
use crate::types::{Head, Tail};

impl<Q, S> core::convert::AsRef<Head<Q, S>> for Rule<Q, S> {
Expand Down
2 changes: 1 addition & 1 deletion core/src/rules/impls/impl_rule_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Appellation: impl_rule_repr <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::rule::Rule;
use crate::rules::Rule;

impl<'a, 'b, Q, A> Rule<&'a Q, &'b A> {
pub fn cloned(&self) -> Rule<Q, A>
Expand Down
2 changes: 1 addition & 1 deletion core/src/traits/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait Alphabet {
self.as_slice().len()
}

fn to_vec(&self) -> Vec<(K, Self::Elem)>;
fn to_vec(&self) -> Vec<Self::Elem>;
}

/// [Symbolic] is a trait denoting types that can be used as symbols;
Expand Down

0 comments on commit 17e154e

Please sign in to comment.