Compare commits

...

3 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
5 changed files with 60 additions and 15 deletions

5
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"

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,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,12 +24,16 @@ 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::LeftMissing => println!("Left side expression missing."),
EvalError::RightMissing => println!("Right side expression missing."), EvalError::RightMissing => println!("Right side expression missing."),
EvalError::LeftMissing => println!("Left side expression missing."),
} }
} }
@ -38,9 +41,7 @@ impl Out {
let diffed_eval_chars = diffed_eval.chars().count(); let diffed_eval_chars = diffed_eval.chars().count();
let mut char_counts = [diffed_left.chars().count(), diffed_right.chars().count(), diffed_eval_chars]; let mut char_counts = [diffed_left.chars().count(), diffed_right.chars().count(), diffed_eval_chars];
char_counts.sort(); char_counts.sort();
let subtracted_count = char_counts.last().map_or(0, |biggest_len| { let subtracted_count = char_counts.last().map_or(0, |biggest_len| *biggest_len-diffed_eval_chars);
*biggest_len-diffed_eval_chars
});
format!("{}{diffed_eval}", "0".repeat(subtracted_count)) format!("{}{diffed_eval}", "0".repeat(subtracted_count))
} }
@ -48,11 +49,11 @@ impl Out {
eval_out.map_or_else(|e| self.error(e), |result| { eval_out.map_or_else(|e| self.error(e), |result| {
let mut differ = diff::Comparer::new(); let mut differ = diff::Comparer::new();
let diffed = differ.compare(Compare { let diffed = differ.compare(Compare {
right: base::binary(result.input.right), right: self.binary(result.input.right),
left: base::binary(result.input.left), left: self.binary(result.input.left),
}); });
let padded_eval = self.padded(&diffed.left, &diffed.right, base::binary(result.eval)); 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(padded_eval.chars().count())); println!("{}", "-".repeat(padded_eval.chars().count()));
@ -79,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 => {
}
}) })
}) })
} }

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>>();
}
}