Skip to content

Commit

Permalink
Start implementing NFA simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
exellentcoin26 committed Jul 16, 2023
1 parent fd0d675 commit af7c089
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/fsm/nfa/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod model;
mod sim;
25 changes: 25 additions & 0 deletions src/fsm/nfa/sim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::{super::traits::Simulateable, model::Nfa};

struct NfaSimulator<'a> {
nfa: &'a Nfa,
}

impl<'a> NfaSimulator<'a> {
fn new(nfa: &'a Nfa) -> Self {
Self { nfa }
}
}

impl Simulateable for NfaSimulator<'_> {
fn start_closure(&self) -> bool {
todo!()
}

fn feed(&mut self, input: char) -> bool {
todo!()
}

fn feed_str(&mut self, input: &str) -> bool {
todo!()
}
}
28 changes: 28 additions & 0 deletions src/fsm/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub(super) trait Simulateable {
/// Simulates the finite-state machine from start to finish and returns whether it accepts the
/// input.
fn run(mut self, input: &str) -> bool
where
Self: Sized,
{
let accept = self.start_closure();
input.chars().map(|c| self.feed(c)).last().unwrap_or(accept)
}

/// Returns whether the epsilon closure of the start state has an accepting state.
fn start_closure(&self) -> bool;

/// Feeds a single character to the finite-state machine and returns whether it has reached an
/// accepting state.
fn feed(&mut self, input: char) -> bool;

/// Feeds an entire string to the finite-state machine at once and returns whether it has
/// reached an accepting state.
fn feed_str(&mut self, input: &str) -> bool {
input
.chars()
.map(|c| self.feed(c))
.last()
.expect("cannot feed an empty string")
}
}

0 comments on commit af7c089

Please sign in to comment.