Skip to content

Commit

Permalink
Change REPL commands call structure
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarLiner committed Sep 26, 2019
1 parent 09cb331 commit 0325703
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 36 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ logos = "0.10.0-rc2"
clap = "2.33.0"
colored = "1.8"
atty = "0.2.13"

regex = "1.3.1"
lazy_static = "1.4.0"
91 changes: 56 additions & 35 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::solve::{distribute_or, extract_clauses, remove_implications, remove_n
use crate::util::cartesian_product;
use atty::Stream;
use colored::*;
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::fmt::{self, Display};

Expand Down Expand Up @@ -54,43 +56,62 @@ impl<'s> Display for TruthTable<'s> {
}

pub fn eval<'s>(input: String) -> Result<String, String> {
let mut parser = Parser::new(&input);
if input.contains(":") {
match parser.parse_command() {
Ok((name, expr)) => match name {
"ast" => Ok(format!("{:?}", expr)),
"table" => Ok(format!("{}", TruthTable::from(expr))),
"cnf" => {
let ast = distribute_or(Box::new(expr)).ok_or("Couldn't distribute or")?;
let ast = remove_implications(Box::new(ast));
let ast = remove_negations(Box::new(ast.clone()))
.ok_or(format!("Couldn't remove negations: {}", ast.clone()))?;
Ok(format!("{}", ast))
}
"clauses" => {
let ast = distribute_or(Box::new(expr)).ok_or("Couldn't distribute or")?;
let ast = remove_implications(Box::new(ast));
let ast = remove_negations(Box::new(ast.clone()))
.ok_or(format!("Couldn't remove negations: {}", ast.clone()))?;
let clauses = extract_clauses(Box::new(ast.clone()))
.ok_or(format!("Couldn't extract clauses: {}", ast))?;
Ok(format!(
"[{}]",
clauses
.iter()
.map(print_clause)
.collect::<Vec<String>>()
.join(", ")
))
}
x => Err(format!("Command not found: {}", x)),
},
Err(err) => Err(format!("Couldn't parse input {:?}", err)),
let input = input.trim();
if input.starts_with(":") {
lazy_static! {
static ref input_re: Regex = Regex::new(r"^:([a-z]+) (.*)$").unwrap();
}
if !input_re.is_match(input) {
Err("Malformed command input".to_string())
} else {
let matches = input_re.captures(input).ok_or("Malformed command input")?;
match Parser::new(
matches
.get(2)
.ok_or("Couldn't expression from input")?
.as_str(),
)
.parse()
{
Ok(ast) => match matches
.get(1)
.ok_or("Couldn't get command from input")?
.as_str()
{
"ast" => Ok(format!("{:?}", ast)),
"table" => Ok(format!("{}", TruthTable::from(ast))),
"cnf" => {
let ast = distribute_or(Box::new(ast)).ok_or("Couldn't distribute or")?;
let ast = remove_implications(Box::new(ast));
let ast = remove_negations(Box::new(ast.clone()))
.ok_or(format!("Couldn't remove negations: {}", ast.clone()))?;
Ok(format!("{}", ast))
}
"clauses" => {
let ast = distribute_or(Box::new(ast)).ok_or("Couldn't distribute or")?;
let ast = remove_implications(Box::new(ast));
let ast = remove_negations(Box::new(ast.clone()))
.ok_or(format!("Couldn't remove negations: {}", ast.clone()))?;
let clauses = extract_clauses(Box::new(ast.clone()))
.ok_or(format!("Couldn't extract clauses: {}", ast))?;
Ok(format!(
"[{}]",
clauses
.iter()
.map(print_clause)
.collect::<Vec<String>>()
.join(", ")
))
}
x => Err(format!("Command not found: {}", x)),
},
Err(err) => Err(format!("Error parsing input {:?}", err)),
}
}
} else {
match parser.parse() {
Ok(node) => Ok(format!("{}", TruthTable::from(node))),
Err(err) => Err(format!("Couldn't parse input {:?}", err)),
match Parser::new(input).parse() {
Ok(ast) => Ok(format!("{}", ast)),
Err(err) => Err(format!("Error parsing input {:?}", err)),
}
}
}
Expand Down

0 comments on commit 0325703

Please sign in to comment.