Skip to content

Commit

Permalink
fix formatting dpd output
Browse files Browse the repository at this point in the history
  • Loading branch information
sakateka committed Apr 5, 2018
1 parent 4bc3dbc commit 63a8e72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
19 changes: 0 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,6 @@ fn main() {
for line in buf.lines() {
let text = line.unwrap();
let result = dpda_design.accepts(&text);
if let Some(path) = result.path {
println!(
"{} -> [{}]",
join(
path.iter()
.cloned()
.map(|x| {
if let Some(rule) = x {
format!("{}", rule)
} else {
String::new()
}
})
.collect::<Vec<String>>(),
" -> "
),
result.cfg.state,
);
}
if result.ok {
println!("{} -> OK", text);
} else {
Expand Down
19 changes: 13 additions & 6 deletions src/pda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ pub struct PDAConfiguration {
pub state: PDAState,
pub stack: Vec<char>,
}
impl fmt::Display for PDAConfiguration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "state: {}, stack: {:?}", self.state, self.stack,)
}
}

impl PDAConfiguration {
pub fn new(state: u32, stack: Vec<char>) -> PDAConfiguration {
Expand Down Expand Up @@ -245,18 +250,20 @@ impl DPDA {
current_cfg = self.rulebook.follow_free_moves(current_cfg)
}

let save_path = self.traversed_path.is_some();
if save_path {
let rule = self.next_rule(&current_cfg, character);
self.traversed_path.as_mut().unwrap().push(rule);
if let Some(rule) = self.next_rule(&current_cfg, character) {
println!("\n{}", rule);
println!(" Configuration:\n current: {}", current_cfg);
}
let next_cfg;
if let Some(cfg) = self.rulebook
.next_configuration(&current_cfg, Some(character))
{
cfg
next_cfg = cfg;
} else {
self._current_cfg.stuck()
next_cfg = self._current_cfg.stuck();
}
println!(" next: {}", next_cfg);
next_cfg
}

pub fn next_rule(&self, cfg: &PDAConfiguration, character: char) -> Option<PDARule> {
Expand Down
12 changes: 6 additions & 6 deletions src/pdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ impl fmt::Display for PDTRule {
f,
"Rule (state {}):\n{}\n{}\n{}\n{}",
self.state,
format!("\tcharacter: {:?}", self.character),
format!("\ttranslated: {:?}", self.translated),
format!("\tpop: {:?}", self.pop_character),
format!("\tpush: {:?}", self.push_characters),
format!(" character: {:?}", self.character),
format!(" translated: {:?}", self.translated),
format!(" pop: {:?}", self.pop_character),
format!(" push: {:?}", self.push_characters),
)
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ impl DPDT {

if let Some(rule) = self.next_rule(&current_cfg, character) {
println!("\n{}", rule);
println!("\tConfiguration:\n\t\tcurrent: {}", current_cfg);
println!(" Configuration:\n current: {}", current_cfg);
}
let next_cfg;
if let Some(cfg) = self.rulebook
Expand All @@ -270,7 +270,7 @@ impl DPDT {
} else {
next_cfg = self._current_cfg.stuck();
}
println!("\t\tnext: {}", next_cfg);
println!(" next: {}", next_cfg);
next_cfg
}

Expand Down

0 comments on commit 63a8e72

Please sign in to comment.