Compare commits

..

5 Commits

Author SHA1 Message Date
c338f55094 cooler kid syntax 2025-04-12 19:14:29 -04:00
43457aae9f implement a custom shunting yard algorithm for bit manipulation only 2025-04-12 19:11:23 -04:00
90b2087c82 remove libbitfyre 2025-04-12 19:10:41 -04:00
96f5213264 diff view 2025-04-09 19:43:23 -03:00
1049f9bcf6 cargo update 2025-04-07 13:09:43 -04:00
7 changed files with 95 additions and 45 deletions

13
Cargo.lock generated
View File

@ -71,7 +71,6 @@ dependencies = [
"clap", "clap",
"color-print", "color-print",
"crossterm", "crossterm",
"libbitfyre",
] ]
[[package]] [[package]]
@ -194,10 +193,6 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libbitfyre"
version = "0.1.0"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.171" version = "0.2.171"
@ -309,9 +304,9 @@ dependencies = [
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.5.10" version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" checksum = "d2f103c6d277498fbceb16e84d317e2a400f160f46904d5f5410848c829511a3"
dependencies = [ dependencies = [
"bitflags", "bitflags",
] ]
@ -367,9 +362,9 @@ dependencies = [
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.14.0" version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" checksum = "8917285742e9f3e1683f0a9c4e6b57960b7314d0b08d30d1ecd426713ee2eee9"
[[package]] [[package]]
name = "strsim" name = "strsim"

View File

@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2024" edition = "2024"
[dependencies] [dependencies]
libbitfyre = { path = "../libbitfyre" }
clap = { version = "4.5.35", features = ["derive"] } clap = { version = "4.5.35", features = ["derive"] }
color-print = "0.3.7" color-print = "0.3.7"
crossterm = "0.28.1" crossterm = "0.28.1"

View File

@ -1,31 +1,33 @@
use color_print::cformat;
pub struct Compare { pub struct Compare {
pub right: String, pub right: String,
pub left: String, pub left: String,
pub eval: String
} }
type Compared = Compare; pub struct Compared {
pub right: String,
pub left: String,
}
pub struct Comparer { pub struct Comparer {
formatted_right: Vec<String>, formatted_right: String,
formatted_left: Vec<String>, formatted_left: String
formatted_eval: Vec<String>
} }
impl Comparer { impl Comparer {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
formatted_right: Vec::new(), formatted_right: String::new(),
formatted_left: Vec::new(), formatted_left: String::new(),
formatted_eval: Vec::new(),
} }
} }
fn format_compare(&mut self, left_char: char, right_char: char) { fn format_compare(&mut self, left_char: char, right_char: char) {
if left_char == right_char { if left_char == right_char {
self.formatted_left.push(left_char.to_string()); self.formatted_left.push(left_char);
self.formatted_right.push(right_char.to_string()); self.formatted_right.push(right_char);
} else { } else {
self.formatted_left.push(color_print::cformat!("<r>{left_char}</>")); self.formatted_left.push_str(&cformat!("<r>{left_char}</>"));
self.formatted_right.push(color_print::cformat!("<r>{right_char}</>")); self.formatted_right.push_str(&cformat!("<r>{right_char}</>"));
}; };
} }
@ -35,18 +37,12 @@ impl Comparer {
for (i, left_char) in compare.left.chars().enumerate() { for (i, left_char) in compare.left.chars().enumerate() {
match right_chars.get(i) { match right_chars.get(i) {
Some(right_char) => self.format_compare(left_char, *right_char), Some(right_char) => self.format_compare(left_char, *right_char),
None => { None => break,
compare.eval.chars().for_each(|char| {
// self.formatted_eval
});
break
},
} }
} }
Compared { Compared {
right: self.formatted_right.concat(), right: self.formatted_right.clone(),
left: self.formatted_left.concat(), left: self.formatted_left.clone()
eval: self.formatted_eval.concat()
} }
} }
} }

View File

@ -1,5 +1,6 @@
use std::num::ParseIntError; use std::num::ParseIntError;
#[allow(dead_code)]
pub enum EvalError { pub enum EvalError {
LeftMissing, LeftMissing,
RightMissing, RightMissing,
@ -44,4 +45,4 @@ pub fn eval(expression: Vec<&str>) -> Result<EvalResult, EvalError> {
"^" | "xor" => Ok(wrap_result(left ^ right)), "^" | "xor" => Ok(wrap_result(left ^ right)),
str => Err(EvalError::InvalidOperator(str.to_owned())) str => Err(EvalError::InvalidOperator(str.to_owned()))
} }
} }

View File

@ -1,5 +1,4 @@
use std::io; use std::io;
use libbitfyre::base;
use crate::{diff::{self, Compare}, eval::{eval, EvalError, EvalResult}, map::{input_error, MapDisplay}}; use crate::{diff::{self, Compare}, eval::{eval, EvalError, EvalResult}, map::{input_error, MapDisplay}};
@ -25,30 +24,40 @@ fn init_message() {
pub struct Out; pub struct Out;
impl Out { impl Out {
fn binary(&self, val: i64) -> String {
format!("{:b}", val)
}
fn error(&self, eval_error: EvalError) { fn error(&self, eval_error: EvalError) {
match eval_error { match eval_error {
EvalError::I64Conversion(parse_int_error) => println!("{parse_int_error}"), EvalError::I64Conversion(parse_int_error) => println!("{parse_int_error}"),
EvalError::InvalidOperator(invalid_op) => println!("The operator {:?} is invalid.", invalid_op), EvalError::InvalidOperator(invalid_op) => println!("The operator {:?} is invalid.", invalid_op),
EvalError::RightMissing => println!("Right side expression missing."),
EvalError::LeftMissing => println!("Left side expression missing."),
} }
} }
fn padded(&self, diffed_left: &str, diffed_right: &str, diffed_eval: String) -> String {
let diffed_eval_chars = diffed_eval.chars().count();
let mut char_counts = [diffed_left.chars().count(), diffed_right.chars().count(), diffed_eval_chars];
char_counts.sort();
let subtracted_count = char_counts.last().map_or(0, |biggest_len| *biggest_len-diffed_eval_chars);
format!("{}{diffed_eval}", "0".repeat(subtracted_count))
}
pub fn evaled(&self, eval_out: Result<EvalResult, EvalError>) { pub fn evaled(&self, eval_out: Result<EvalResult, EvalError>) {
eval_out.map_or_else(|e| self.error(e), |result| { eval_out.map_or_else(|e| self.error(e), |result| {
let eval_base2 = base::binary(result.eval);
let left = base::binary(result.input.left);
let right = base::binary(result.input.right);
let mut differ = diff::Comparer::new(); let mut differ = diff::Comparer::new();
let diffed = differ.compare(Compare { let diffed = differ.compare(Compare {
right: right.clone(), right: self.binary(result.input.right),
left: left.clone(), left: self.binary(result.input.left),
eval: eval_base2.clone(),
}); });
let padded_eval = self.padded(&diffed.left, &diffed.right, self.binary(result.eval));
println!("{}", diffed.left); println!("{}", diffed.left);
println!("{}", diffed.right); println!("{}", diffed.right);
println!("{}", "-".repeat(eval_base2.chars().count())); println!("{}", "-".repeat(padded_eval.chars().count()));
println!("{}", diffed.eval) println!("{}", padded_eval)
}) })
} }
} }
@ -71,7 +80,9 @@ impl Bitfyre {
io::stdin().read_line(&mut input).map_or_display(|_size| match input.trim() { io::stdin().read_line(&mut input).map_or_display(|_size| match input.trim() {
"exit" => self.terminate(None), "exit" => self.terminate(None),
"help" => help_docs(), "help" => help_docs(),
trim => Out.evaled(eval(trim.split_whitespace().collect())) trim => {
}
}) })
}) })
} }
@ -88,4 +99,4 @@ impl Bitfyre {
} }
} }
} }
} }

View File

@ -1,6 +1,7 @@
use input::Bitfyre; use input::Bitfyre;
use cli::parser; use cli::parser;
mod shunting_yard;
mod input; mod input;
mod eval; mod eval;
mod diff; mod diff;

47
src/shunting_yard.rs Normal file
View File

@ -0,0 +1,47 @@
pub type Precision = i64;
struct ShuntingYard {
input: String,
output: Vec<String>,
stack: Vec<String>,
}
impl ShuntingYard {
pub const fn new(input: String) -> Self {
Self {
input,
output: Vec::new(),
stack: Vec::new()
}
}
fn is_shift_operator(&mut self, value: char) -> bool {
match value {
'&' | '^' | '|' => {
self.stack.push(value.to_string());
true
},
'>' | '<' => {
match self.stack.last_mut() {
None => self.stack.push(value.to_string()),
Some(last) => match last.as_str() {
">" => *last = ">>".to_owned(),
"<" => *last = "<<".to_owned(),
_ => self.stack.push(value.to_string())
}
}
true
}
_ => false
}
}
pub fn eval(&mut self) {
self.output = self.input.chars()
.collect::<Vec<char>>()
.into_iter()
.filter(|char| self.is_shift_operator(*char))
.map(|char_num| char_num.to_string())
.collect::<Vec<String>>();
}
}