#parser #logical #logic #circuit #gate

rustlogic-march1917

A Rust Library for basic parsing and handling of logical formulas(forked from coastalwhite)

1 unstable release

0.1.0 Feb 23, 2023

#24 in #gate


Used in bool2cnf

MIT license

48KB
818 lines

a forked from rustlogic


lib.rs:

Hello fellow Rustacians! RustLogic is a crate for parsing and handling simple logical expressings.

This crate contains the basic foundation for handling stringed logical formulas. If you want to want to submit an issue or a pull request, please visit the GitHub page.

Thanks for your time and enjoy this crate!

Examples

4-1 Multiplexer

let multiplexer_4_to_1 =
    rustlogic::parse(
        "([a]&~[x]&~[y])|([b]&~[x]&[y])|([c]&[x]&~[y])|([d]&[x]&[y])"
    )
    .expect("Failed to parse 4-1 multiplexer");

let mut variable_map = HashMap::new();

// Input: 1001
variable_map.insert("a", true);
variable_map.insert("b", false);
variable_map.insert("c", false);
variable_map.insert("d", true);

// Selector: 11
variable_map.insert("x", true);
variable_map.insert("y", true);

// Should select fourth item from bus so true
let value = multiplexer_4_to_1.get_value_from_variables(&variable_map).unwrap();
println!("{}", value); // Will print true!

Evaluating a logical string with variables and custom logical operators

use rustlogic::operators;
use std::collections::HashMap;

// Define the set
let operator_set = operators::common_sets::worded();

let parsed = rustlogic::custom_parse("(NOT $[A] AND TRUE) OR $[B]", &operator_set);

// We assign the variables to their values
let mut hm = HashMap::new();
hm.insert("A", false);
hm.insert("B", false);

// Now contains the value of the logical expression
let value = parsed.unwrap().get_value_from_variables(&hm).unwrap();

No runtime deps