Skip to content

Commit

Permalink
simplify errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timbeurskens committed Oct 5, 2023
1 parent a05e201 commit cf02d2d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = ["Tim Beurskens <timbeurskens.97@gmail.com>"]
[package]
name = "rsbdd"
description = "A BDD-based SAT solver"
version = "0.12.0"
version = "0.13.0"
edition.workspace = true
authors.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion max_clique_gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn main() -> Result<()> {
}

// flush the writer before dropping it
writer.flush().expect("Could not flush write buffer");
writer.flush()?;

Ok(())
}
16 changes: 9 additions & 7 deletions src/bdd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use crate::{BDDSymbol, NamedSymbol, TruthTableEntry};

#[macro_export]
macro_rules! bdd {
($($expr:tt)+) => {{
let input = stringify!($($expr)+);
let mut input_reader = std::io::BufReader::new(input.as_bytes());
let parsed_formula = rsbdd::parser::ParsedFormula::new(&mut input_reader, None).expect("could not parse expression");

parsed_formula.eval()
}};
($($expr:tt)+) => {
(|| -> anyhow::Result<_> {
let input = stringify!($($expr)+);
let mut input_reader = std::io::BufReader::new(input.as_bytes());
let parsed_formula = rsbdd::parser::ParsedFormula::new(&mut input_reader, None)?;

Ok(parsed_formula.eval())
})()
};
}

#[derive(Debug, Clone, Default, Eq, PartialEq, Hash)]
Expand Down
42 changes: 18 additions & 24 deletions src/bin/rsbdd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct Args {
export_ordering: bool,
}

fn main() {
fn main() -> anyhow::Result<()> {
let wild_args = wild::args_os();
let args_in =
argfile::expand_args_from(wild_args, argfile::parse_fromfile, argfile::PREFIX).unwrap();
Expand All @@ -88,34 +88,30 @@ fn main() {
let mut reader = if let Some(inline_str) = &inline_eval {
Box::new(BufReader::new(inline_str.as_bytes())) as Box<dyn BufRead>
} else if let Some(some_input_filename) = input_filename {
let file = File::open(some_input_filename).expect("Could not open input file");
let file = File::open(some_input_filename)?;
Box::new(BufReader::new(file)) as Box<dyn BufRead>
} else {
Box::new(BufReader::new(io::stdin())) as Box<dyn BufRead>
};

let pre_variable_ordering = if let Some(ord_filename) = args.ordering {
let file = File::open(ord_filename).expect("Could not open variable ordering file");
let file = File::open(ord_filename)?;
let mut contents = Box::new(BufReader::new(file)) as Box<dyn BufRead>;
let tokens = SymbolicBDD::tokenize(&mut contents, None)
.expect("Could not extract tokens from variable ordering");
let tokens = SymbolicBDD::tokenize(&mut contents, None)?;
let vars = ParsedFormula::extract_vars(&tokens);
Some(vars)
} else {
None
};

let input_parsed =
ParsedFormula::new(&mut reader, pre_variable_ordering).expect("Could not parse input file");
let input_parsed = ParsedFormula::new(&mut reader, pre_variable_ordering)?;

if let Some(parsetree_filename) = args.parsetree {
let mut f = File::create(parsetree_filename).expect("Could not create parsetree dot file");
let mut f = File::create(parsetree_filename)?;

let graph = SymbolicParseTree::new(&input_parsed.bdd);

graph
.render_dot(&mut f)
.expect("Could not write parsetree to dot file");
graph.render_dot(&mut f)?;
}

let mut result: Rc<BDD<NamedSymbol>> = Rc::default();
Expand Down Expand Up @@ -143,7 +139,7 @@ fn main() {
print_performance_results(&exec_times);

if args.plot {
plot_performance_results(&exec_times);
plot_performance_results(&exec_times)?;
}
}

Expand Down Expand Up @@ -209,14 +205,14 @@ fn main() {
}

if let Some(dot_filename) = args.dot {
let mut f = File::create(dot_filename).expect("Could not create dot file");
let mut f = File::create(dot_filename)?;

let graph = BDDGraph::new(&result, args.filter);

graph
.render_dot(&mut f)
.expect("Could not write BDD to dot file");
graph.render_dot(&mut f)?
}

Ok(())
}

fn print_sized_line<B, C, D>(labels: &Vec<D>, widths: &B, result: &BDD<C>)
Expand Down Expand Up @@ -294,15 +290,14 @@ fn print_performance_results(results: &[Duration]) {
}

// invoke gnuplot to show the run-time distribution plot
fn plot_performance_results(results: &[Duration]) {
fn plot_performance_results(results: &[Duration]) -> anyhow::Result<()> {
let (_, _, _, mean, stddev) = stats(results);

let mut gnuplot_cmd = Command::new("gnuplot")
.arg("-p") // persistent mode
.arg("-") // piped mode
.stdin(Stdio::piped())
.spawn()
.expect("Could not spawn gnuplot");
.spawn()?;

let stdin = gnuplot_cmd.stdin.as_mut().unwrap();
write_gnuplot_normal_distribution(
Expand All @@ -311,12 +306,11 @@ fn plot_performance_results(results: &[Duration]) {
mean + (stddev * 2.0),
mean,
stddev,
)
.expect("Could not write to gnuplot command");
)?;

gnuplot_cmd.wait()?;

gnuplot_cmd
.wait()
.expect("Could not wait for gnuplot to finish");
Ok(())
}

// print all variables which can take a 'true' value in the bdd
Expand Down
2 changes: 1 addition & 1 deletion sudoku_gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn main() -> io::Result<()> {

writeln!(writer, "true")?;

writer.flush().expect("Could not flush write buffer");
writer.flush()?;

Ok(())
}
6 changes: 3 additions & 3 deletions tests/bdd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@ fn test_queens() {

#[test]
fn test_basic_syntax_1() {
let e1 = bdd!(a | -a);
let e2 = bdd!(a | b | c);
let e3 = bdd!(false);
let e1 = bdd!(a | -a).unwrap();
let e2 = bdd!(a | b | c).unwrap();
let e3 = bdd!(false).unwrap();

println!("{:#?}\n{:#?}\n{:#?}", e1, e2, e3);
}

0 comments on commit cf02d2d

Please sign in to comment.