Several changes
This commit is contained in:
parent
1d1a72f322
commit
92232bea96
|
@ -58,6 +58,12 @@ version = "1.4.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "3.1.1"
|
version = "3.1.1"
|
||||||
|
@ -88,6 +94,17 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colored"
|
||||||
|
version = "2.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"lazy_static",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "digest"
|
name = "digest"
|
||||||
version = "0.8.1"
|
version = "0.8.1"
|
||||||
|
@ -102,10 +119,19 @@ name = "diskmat_tools"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"itertools",
|
||||||
|
"log",
|
||||||
"pest",
|
"pest",
|
||||||
"pest_derive",
|
"pest_derive",
|
||||||
|
"simple_logger",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fake-simd"
|
name = "fake-simd"
|
||||||
version = "0.1.2"
|
version = "0.1.2"
|
||||||
|
@ -152,6 +178,15 @@ dependencies = [
|
||||||
"hashbrown",
|
"hashbrown",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.8.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
|
@ -164,6 +199,15 @@ version = "0.2.119"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4"
|
checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "maplit"
|
name = "maplit"
|
||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
|
@ -288,6 +332,18 @@ dependencies = [
|
||||||
"opaque-debug",
|
"opaque-debug",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "simple_logger"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c75a9723083573ace81ad0cdfc50b858aa3c366c48636edb4109d73122a0c0ea"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"colored",
|
||||||
|
"log",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.10.0"
|
version = "0.10.0"
|
||||||
|
|
|
@ -6,6 +6,12 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = { version = "3.1.1", features = ["derive"] }
|
||||||
|
log = "0.4.14"
|
||||||
|
itertools = "0.8.2"
|
||||||
pest = "2.1.3"
|
pest = "2.1.3"
|
||||||
pest_derive = "2.0"
|
pest_derive = "2.0"
|
||||||
clap = { version = "3.1.1", features = ["derive"] }
|
|
||||||
|
[dependencies.simple_logger]
|
||||||
|
default-features = false
|
||||||
|
features = ["colors"]
|
||||||
|
|
|
@ -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;
|
use super::super::traits::graphvizable::Graphvizable;
|
||||||
|
|
||||||
pub use pest::iterators::Pair as BAPair;
|
|
||||||
use pest::Parser;
|
use pest::Parser;
|
||||||
use pest_derive::Parser;
|
use pest_derive::Parser;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[grammar = "boolean_algebra/boolean_algebra.pest"]
|
#[grammar = "boolean_algebra/boolean_algebra.pest"]
|
||||||
pub struct BAParser;
|
pub struct BAParser;
|
||||||
pub type BARule = Rule;
|
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) {
|
fn print_graphviz_diagram(&self) {
|
||||||
let mut i: u64 = 0;
|
let mut i: u64 = 0;
|
||||||
println!("digraph parse_tree {{");
|
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 {
|
fn parse(input: &mut String) -> Pair {
|
||||||
input.retain(|c| !c.is_whitespace());
|
input.retain(|c| !c.is_whitespace());
|
||||||
let result = <BAParser as Parser<BARule>>::parse(BARule::boolean_algebra, input.as_str())
|
let result = <BAParser as Parser<BARule>>::parse(BARule::boolean_algebra, input.as_str())
|
||||||
|
@ -46,7 +47,7 @@ impl traits::parser:: Parser for BAParser {
|
||||||
// str += ("}}");
|
// 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 {
|
if pair.as_rule() == BARule::binop {
|
||||||
println!(
|
println!(
|
||||||
" {} [label=\"{}\", color=\"brown1\"];",
|
" {} [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);
|
let node_name = format!("n{}", *i);
|
||||||
_print_g_node(&pair, &node_name);
|
_print_g_node(&pair, &node_name);
|
||||||
|
|
||||||
|
|
51
src/main.rs
51
src/main.rs
|
@ -2,13 +2,16 @@ mod boolean_algebra;
|
||||||
mod traits;
|
mod traits;
|
||||||
mod truthtable;
|
mod truthtable;
|
||||||
|
|
||||||
use boolean_algebra::parser::BAParser;
|
use boolean_algebra::{logic::generate_truth_table, parser::BAParser};
|
||||||
use truthtable::parser::TTParser;
|
use truthtable::parser::TTParser;
|
||||||
// use boolean_algebra::parser::parse;
|
// use boolean_algebra::parser::parse;
|
||||||
// use clap::Parser;
|
// use clap::Parser;
|
||||||
// use pest::iterators::Pair;
|
// use pest::iterators::Pair;
|
||||||
use std::{fs::File, io::Read};
|
use std::{fs::File, io::Read};
|
||||||
use traits::{graphvizable::Graphvizable, parser::{Pair, Parser}};
|
use traits::{
|
||||||
|
graphvizable::Graphvizable,
|
||||||
|
parser::{Pair, Parser},
|
||||||
|
};
|
||||||
|
|
||||||
/// Simple tool to visualize and process concepts from TMA4140 and MA0301
|
/// Simple tool to visualize and process concepts from TMA4140 and MA0301
|
||||||
#[derive(clap::Parser, Debug)]
|
#[derive(clap::Parser, Debug)]
|
||||||
|
@ -18,6 +21,14 @@ struct Args {
|
||||||
// #[clap(with_name)]
|
// #[clap(with_name)]
|
||||||
// t: String,
|
// t: String,
|
||||||
|
|
||||||
|
/// Debug mode
|
||||||
|
#[clap(short = 'd', long)]
|
||||||
|
debug: bool,
|
||||||
|
|
||||||
|
/// Verbose mode
|
||||||
|
#[clap(short = 'v', long)]
|
||||||
|
verbose: bool,
|
||||||
|
|
||||||
/// Input file
|
/// Input file
|
||||||
#[clap(short = 'i', long)]
|
#[clap(short = 'i', long)]
|
||||||
input: Option<String>,
|
input: Option<String>,
|
||||||
|
@ -47,13 +58,27 @@ struct Args {
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
let args = <Args as clap::Parser>::parse();
|
let args = <Args as clap::Parser>::parse();
|
||||||
|
|
||||||
|
|
||||||
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
|
|
||||||
|
if args.verbose {
|
||||||
|
log::set_max_level(log::LevelFilter::Warn)
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.debug {
|
||||||
|
log::set_max_level(log::LevelFilter::Debug)
|
||||||
|
}
|
||||||
|
|
||||||
|
simple_logger::SimpleLogger::new().init().unwrap();
|
||||||
|
|
||||||
|
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
let root: Pair = if args.input_text.is_some() {
|
let root: Pair = if args.input_text.is_some() {
|
||||||
input = args.input_text.unwrap();
|
input = args.input_text.unwrap();
|
||||||
match args.t.as_deref() {
|
match args.t.as_deref() {
|
||||||
Some("ba") => BAParser::parse(&mut input),
|
Some("ba") => BAParser::parse(&mut input),
|
||||||
Some("tt") => TTParser::parse(&mut input),
|
Some("tt") => TTParser::parse(&mut input),
|
||||||
_ => panic!("Doesn't recognize file"),
|
_ => panic!("Doesn't recognize file"),
|
||||||
}
|
}
|
||||||
// parse(&mut input)
|
// parse(&mut input)
|
||||||
} else if args.input.is_some() {
|
} else if args.input.is_some() {
|
||||||
|
@ -62,15 +87,21 @@ fn main() -> std::io::Result<()> {
|
||||||
let mut file = File::open(&t)?;
|
let mut file = File::open(&t)?;
|
||||||
file.read_to_string(&mut input)?;
|
file.read_to_string(&mut input)?;
|
||||||
match t.split('.').last() {
|
match t.split('.').last() {
|
||||||
Some("ba") => BAParser::parse(&mut input),
|
Some("ba") => BAParser::parse(&mut input),
|
||||||
Some("tt") => TTParser::parse(&mut input),
|
Some("tt") => TTParser::parse(&mut input),
|
||||||
_ => panic!("Doesn't recognize file"),
|
_ => panic!("Doesn't recognize file"),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic!("Provide an input");
|
panic!("Provide an input");
|
||||||
// Err(std::io::Error::new(std::io::ErrorKind::Other, "Provide some kind of input"))
|
// Err(std::io::Error::new(std::io::ErrorKind::Other, "Provide some kind of input"))
|
||||||
};
|
};
|
||||||
|
|
||||||
root.print_graphviz_diagram();
|
match root {
|
||||||
|
Pair::BAPair(pair) => generate_truth_table(&pair),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
// root.print_graphviz_diagram();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
|
|
||||||
use crate::{boolean_algebra, truthtable};
|
use crate::{boolean_algebra::parser::BAPair, truthtable::parser::TTPair};
|
||||||
|
|
||||||
use super::graphvizable::Graphvizable;
|
use super::graphvizable::Graphvizable;
|
||||||
|
|
||||||
pub enum Pair<'a> {
|
pub enum Pair<'a> {
|
||||||
BAPair(pest::iterators::Pair<'a, boolean_algebra::parser::BARule>),
|
BAPair(BAPair<'a>),
|
||||||
TTPair(pest::iterators::Pair<'a, truthtable::parser::TTRule>),
|
TTPair(TTPair<'a>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a>Graphvizable for Pair<'a> {
|
impl <'a>Graphvizable for Pair<'a> {
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
use pest::iterators::Pair as TTPair;
|
|
||||||
use pest::Parser;
|
use pest::Parser;
|
||||||
|
use crate::Pair;
|
||||||
|
|
||||||
use crate::traits;
|
use crate::traits;
|
||||||
use crate::traits::graphvizable::Graphvizable;
|
use crate::traits::graphvizable::Graphvizable;
|
||||||
use crate::traits::parser::Pair;
|
|
||||||
// use pest_derive::Parser;
|
// use pest_derive::Parser;
|
||||||
|
|
||||||
#[derive(pest_derive::Parser)]
|
#[derive(pest_derive::Parser)]
|
||||||
#[grammar = "truthtable/truthtable.pest"]
|
#[grammar = "truthtable/truthtable.pest"]
|
||||||
pub struct TTParser;
|
pub struct TTParser;
|
||||||
pub type TTRule = Rule;
|
pub type TTRule = Rule;
|
||||||
|
pub type TTPair<'a> = pest::iterators::Pair<'a, TTRule>;
|
||||||
|
|
||||||
impl Graphvizable for TTPair<'_, TTRule> {
|
impl Graphvizable for TTPair<'_> {
|
||||||
fn graphviz_diagram(&self) -> String {
|
fn graphviz_diagram(&self) -> String {
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
result += "digraph parse_tree {{\n";
|
result += "digraph parse_tree {{\n";
|
||||||
|
@ -37,7 +37,7 @@ impl Graphvizable for TTPair<'_, TTRule> {
|
||||||
|
|
||||||
impl traits::parser::Parser for TTParser {
|
impl traits::parser::Parser for TTParser {
|
||||||
fn parse(input: &mut String) -> Pair {
|
fn parse(input: &mut String) -> Pair {
|
||||||
input.retain(|c| !c.is_whitespace());
|
// input.retain(|c| !c.is_whitespace());
|
||||||
let result = <TTParser as Parser<TTRule>>::parse(TTRule::truthtable, input.as_str())
|
let result = <TTParser as Parser<TTRule>>::parse(TTRule::truthtable, input.as_str())
|
||||||
.expect("UNSUCCESSFUL PARSE")
|
.expect("UNSUCCESSFUL PARSE")
|
||||||
.next()
|
.next()
|
||||||
|
@ -46,7 +46,7 @@ impl traits::parser::Parser for TTParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn g_node(pair: &TTPair<TTRule>, node_name: &str) -> String {
|
fn g_node(pair: &TTPair, node_name: &str) -> String {
|
||||||
if pair.as_rule() == TTRule::binop {
|
if pair.as_rule() == TTRule::binop {
|
||||||
format!(
|
format!(
|
||||||
" {} [label=\"{}\", color=\"brown1\"];",
|
" {} [label=\"{}\", color=\"brown1\"];",
|
||||||
|
@ -74,7 +74,7 @@ fn g_node(pair: &TTPair<TTRule>, node_name: &str) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn graphviz_syntax_tree(pair: &TTPair<TTRule>, i: &mut u64) -> String {
|
fn graphviz_syntax_tree(pair: &TTPair, i: &mut u64) -> String {
|
||||||
let node_name = format!("n{}", *i);
|
let node_name = format!("n{}", *i);
|
||||||
let mut result: String = String::new();
|
let mut result: String = String::new();
|
||||||
result += &g_node(&pair, &node_name);
|
result += &g_node(&pair, &node_name);
|
||||||
|
@ -88,7 +88,7 @@ fn graphviz_syntax_tree(pair: &TTPair<TTRule>, i: &mut u64) -> String {
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _print_graphviz_syntax_tree(pair: &TTPair<TTRule>, i: &mut u64) {
|
fn _print_graphviz_syntax_tree(pair: &TTPair, i: &mut u64) {
|
||||||
let node_name = format!("n{}", *i);
|
let node_name = format!("n{}", *i);
|
||||||
println!("{}", g_node(&pair, &node_name));
|
println!("{}", g_node(&pair, &node_name));
|
||||||
|
|
||||||
|
@ -97,12 +97,4 @@ fn _print_graphviz_syntax_tree(pair: &TTPair<TTRule>, i: &mut u64) {
|
||||||
println!(" {} -> n{};", node_name, *i);
|
println!(" {} -> n{};", node_name, *i);
|
||||||
_print_graphviz_syntax_tree(&inner_pair, i);
|
_print_graphviz_syntax_tree(&inner_pair, i);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse(input: &mut String) -> TTPair<TTRule> {
|
|
||||||
input.retain(|c| !c.is_whitespace());
|
|
||||||
TTParser::parse(TTRule::truthtable, input.as_str())
|
|
||||||
.expect("UNSUCCESSFUL PARSE")
|
|
||||||
.next()
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue