-
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.
- Loading branch information
1 parent
fd0d675
commit af7c089
Showing
3 changed files
with
54 additions
and
0 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
mod model; | ||
mod sim; |
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,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!() | ||
} | ||
} |
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,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") | ||
} | ||
} |