Several changes
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
// mod parser;
|
||||
use std::collections::{HashSet, HashMap};
|
||||
|
||||
use super::parser::{BAPair, BARule};
|
||||
|
||||
fn extract_base_expressions(pair: &BAPair, expressions: &mut HashSet<String>) {
|
||||
if pair.as_rule() == BARule::label {
|
||||
expressions.insert(pair.as_str().to_string());
|
||||
} else {
|
||||
for child_pair in pair.clone().into_inner() {
|
||||
extract_base_expressions(&child_pair, expressions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_expressions<'a>(pair: &BAPair, expressions: &mut Vec<&BAPair>) {
|
||||
for child_pair in pair.clone().into_inner() {
|
||||
extract_expressions(&child_pair, expressions);
|
||||
}
|
||||
// if pair.as_rule() == BARule::expression {
|
||||
// expressions.push(pair);
|
||||
// } else {
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
fn base_expression_combinations(base_expressions: &HashSet<String>) -> Vec<HashMap<String, bool>> {
|
||||
let combination_count = 2_u32.pow(base_expressions.len() as u32) as usize;
|
||||
let ordered_base_expressions: Vec<String> = base_expressions.clone().into_iter().collect();
|
||||
let mut combinations: Vec<HashMap<String, bool>> = Vec::with_capacity(combination_count);
|
||||
for n in 0..combination_count {
|
||||
let mut combination: HashMap<String, bool> = HashMap::with_capacity(base_expressions.len());
|
||||
for b in 0..base_expressions.len() {
|
||||
combination.insert(ordered_base_expressions[b as usize].clone(), (n >> b) % 2 == 1);
|
||||
}
|
||||
combinations.push(combination);
|
||||
}
|
||||
combinations
|
||||
}
|
||||
|
||||
fn calculate_expression(values: HashMap<String, bool>, pair: BAPair) {
|
||||
|
||||
}
|
||||
|
||||
pub fn generate_truth_table(pair: &BAPair) {
|
||||
let mut base_expressions = HashSet::new();
|
||||
extract_base_expressions(pair, &mut base_expressions);
|
||||
log::debug!("Base expressions: {:?}", base_expressions);
|
||||
// log::debug!("Base expressions: {:?}", base_expressions.iter().product::<String>().collect::<Vec<_>>());
|
||||
|
||||
let mut expressions = Vec::new();
|
||||
extract_expressions(pair, &mut expressions);
|
||||
|
||||
let combinations = base_expression_combinations(&base_expressions);
|
||||
log::debug!("{:#?}", combinations);
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
use crate::traits::{parser::Pair, self};
|
||||
use crate::traits::{self, parser::Pair};
|
||||
|
||||
use super::super::traits::graphvizable::Graphvizable;
|
||||
|
||||
pub use pest::iterators::Pair as BAPair;
|
||||
use pest::Parser;
|
||||
use pest_derive::Parser;
|
||||
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "boolean_algebra/boolean_algebra.pest"]
|
||||
pub struct BAParser;
|
||||
pub type BARule = Rule;
|
||||
pub type BAPair<'a> = pest::iterators::Pair<'a, BARule>;
|
||||
|
||||
impl Graphvizable for BAPair<'_, BARule> {
|
||||
impl Graphvizable for BAPair<'_> {
|
||||
fn print_graphviz_diagram(&self) {
|
||||
let mut i: u64 = 0;
|
||||
println!("digraph parse_tree {{");
|
||||
@@ -26,7 +27,7 @@ impl Graphvizable for BAPair<'_, BARule> {
|
||||
}
|
||||
}
|
||||
|
||||
impl traits::parser:: Parser for BAParser {
|
||||
impl traits::parser::Parser for BAParser {
|
||||
fn parse(input: &mut String) -> Pair {
|
||||
input.retain(|c| !c.is_whitespace());
|
||||
let result = <BAParser as Parser<BARule>>::parse(BARule::boolean_algebra, input.as_str())
|
||||
@@ -46,7 +47,7 @@ impl traits::parser:: Parser for BAParser {
|
||||
// str += ("}}");
|
||||
// }
|
||||
|
||||
fn _print_g_node(pair: &BAPair<BARule>, node_name: &str) {
|
||||
fn _print_g_node(pair: &BAPair, node_name: &str) {
|
||||
if pair.as_rule() == BARule::binop {
|
||||
println!(
|
||||
" {} [label=\"{}\", color=\"brown1\"];",
|
||||
@@ -72,7 +73,7 @@ fn _print_g_node(pair: &BAPair<BARule>, node_name: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
fn _print_graphviz_syntax_tree(pair: &BAPair<BARule>, i: &mut u64) {
|
||||
fn _print_graphviz_syntax_tree(pair: &BAPair, i: &mut u64) {
|
||||
let node_name = format!("n{}", *i);
|
||||
_print_g_node(&pair, &node_name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user