misc: applied some clippy lints

This commit is contained in:
2025-06-17 19:43:35 +01:00
parent 868cba376f
commit 0b16246dd2
7 changed files with 114 additions and 58 deletions
Generated
+9 -16
View File
@@ -659,12 +659,6 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "colorful"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffb474a9c3219a8254ead020421ecf1b90427f29b55f6aae9a2471fa62c126ef"
[[package]] [[package]]
name = "combine" name = "combine"
version = "4.6.7" version = "4.6.7"
@@ -909,16 +903,6 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76"
[[package]]
name = "dsa_editor"
version = "0.1.0"
dependencies = [
"colorful",
"eframe",
"egui",
"serde",
]
[[package]] [[package]]
name = "ecolor" name = "ecolor"
version = "0.31.1" version = "0.31.1"
@@ -1021,6 +1005,14 @@ dependencies = [
"winit", "winit",
] ]
[[package]]
name = "egui_code_editor"
version = "0.2.13"
source = "git+https://github.com/zxq5-dev/egui_code_editor?rev=5eb313e#5eb313e38504410ce0a6b27231cda28842f542fe"
dependencies = [
"egui",
]
[[package]] [[package]]
name = "egui_glow" name = "egui_glow"
version = "0.31.1" version = "0.31.1"
@@ -1058,6 +1050,7 @@ dependencies = [
"discord-presence", "discord-presence",
"eframe", "eframe",
"egui", "egui",
"egui_code_editor",
"rfd", "rfd",
"toml", "toml",
] ]
+14 -13
View File
@@ -3,7 +3,7 @@ use std::{
collections::HashSet, collections::HashSet,
fs, fs,
hash::{DefaultHasher, Hash, Hasher}, hash::{DefaultHasher, Hash, Hasher},
path::PathBuf, path::{Path, PathBuf},
}; };
use common::prelude::Instruction; use common::prelude::Instruction;
@@ -17,27 +17,27 @@ pub mod lexer;
pub mod model; pub mod model;
pub mod parser; pub mod parser;
pub fn assemble(src: &PathBuf) -> Vec<Instruction> { pub fn assemble(src: &Path) -> Vec<Instruction> {
let mut modules = HashSet::<u64>::new(); let mut modules = HashSet::<u64>::new();
let mut program = Program::new(); let mut program = Program::new();
let hash = quick_hash(src); let hash = quick_hash(src);
modules.insert(hash); modules.insert(hash);
match prepare_dependency(src.clone(), &mut modules, &mut program) { match prepare_dependency(src, &mut modules, &mut program) {
Ok(_) => {} Ok(_) => {}
Err(err) => println!("BIG ERROR {:?}", err), Err(err) => println!("BIG ERROR {err:?}"),
} }
for node in program.nodes { for node in program.nodes {
println!("{:?}", node); println!("{node:?}");
} }
vec![] vec![]
} }
fn prepare_dependency( fn prepare_dependency(
path: PathBuf, path: &Path,
modules: &mut HashSet<u64>, modules: &mut HashSet<u64>,
program: &mut Program, program: &mut Program,
) -> Result<(), AssembleError> { ) -> Result<(), AssembleError> {
@@ -51,9 +51,9 @@ fn prepare_dependency(
)); ));
} }
let src = fs::read_to_string(&path) let src = fs::read_to_string(path)
.map_err(|_| AssembleError::InvalidFile(path.clone()))?; .map_err(|_| AssembleError::InvalidFile(path.to_path_buf()))?;
let file_hash = quick_hash(&path); let file_hash = quick_hash(path);
log(&format!("{:20} {:20}", "Tokenising", filename)); log(&format!("{:20} {:20}", "Tokenising", filename));
let tokens = lexer::lexer(src, file_hash)?; let tokens = lexer::lexer(src, file_hash)?;
@@ -77,14 +77,14 @@ fn prepare_dependency(
if !modules.contains(&quick_hash(&dep)) { if !modules.contains(&quick_hash(&dep)) {
modules.insert(quick_hash(&dep)); modules.insert(quick_hash(&dep));
prepare_dependency(dep, modules, program)? prepare_dependency(dep.as_path(), modules, program)?
} }
} }
Ok(()) Ok(())
} }
fn build(src: Vec<Node>) -> Result<Vec<Instruction>, AssembleError> { fn _build(_src: Vec<Node>) -> Result<Vec<Instruction>, AssembleError> {
Ok(vec![]) Ok(vec![])
} }
@@ -116,12 +116,13 @@ impl fmt::Display for AssembleError {
} }
} }
fn quick_hash(value: &PathBuf) -> u64 { fn quick_hash(value: &Path) -> u64 {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
value.canonicalize().unwrap().to_str().hash(&mut hasher); value.canonicalize().unwrap().to_str().hash(&mut hasher);
hasher.finish() hasher.finish()
} }
// TODO: Use an actual logging or tracing library for pretty (scoped) output.
fn log(message: &str) { fn log(message: &str) {
println!("\x1b[32mINFO:\x1b[0m {}", message); println!("\x1b[32mINFO:\x1b[0m {message}");
} }
+1 -4
View File
@@ -1,8 +1,5 @@
use std::{fs, io::Write, path::PathBuf}; use std::{fs, io::Write, path::PathBuf};
use assembler::{lexer, parser::Parser};
use common::prelude::{ITypeArgs, Instruction, RTypeArgs, Register};
fn main() { fn main() {
// parse args: // parse args:
let args: Vec<String> = std::env::args().collect(); let args: Vec<String> = std::env::args().collect();
@@ -16,7 +13,7 @@ fn main() {
let src = PathBuf::from(input_path); let src = PathBuf::from(input_path);
let mut output_file = fs::File::create(output_path).unwrap(); let mut output_file = fs::File::create(output_path).unwrap();
let res = assembler::assemble(&src) assembler::assemble(&src)
.iter() .iter()
.map(|i| i.encode()) .map(|i| i.encode())
.for_each(|i| { .for_each(|i| {
+54 -5
View File
@@ -3,13 +3,12 @@ use std::{fmt, str::FromStr};
use common::prelude::Register; use common::prelude::Register;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[expect(dead_code)]
pub struct Node(pub Option<Symbol>, pub Opcode, pub Vec<Token>); pub struct Node(pub Option<Symbol>, pub Opcode, pub Vec<Token>);
impl fmt::Display for Node { impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let symbol = match &self.0 { let symbol = match &self.0 {
Some(symbol) => format!("{}", symbol), Some(symbol) => format!("{symbol}"),
None => "".to_string(), None => "".to_string(),
}; };
@@ -26,15 +25,65 @@ impl fmt::Display for Symbol {
impl fmt::Display for Module { impl fmt::Display for Module {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Module::Unresolved(name) => write!(f, "{}", name), Module::Unresolved(name) => write!(f, "{name}"),
Module::Resolved(name) => write!(f, "{}", name), Module::Resolved(name) => write!(f, "{name}"),
} }
} }
} }
impl fmt::Display for Opcode { impl fmt::Display for Opcode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self) match self {
Opcode::Nop => write!(f, "nop"),
Opcode::Mov => write!(f, "mov"),
Opcode::Movs => write!(f, "movs"),
Opcode::Ldb => write!(f, "ldb"),
Opcode::Ldbs => write!(f, "ldbs"),
Opcode::Ldh => write!(f, "ldh"),
Opcode::Ldhs => write!(f, "ldhs"),
Opcode::Ldw => write!(f, "ldw"),
Opcode::Stb => write!(f, "stb"),
Opcode::Sth => write!(f, "sth"),
Opcode::Stw => write!(f, "stw"),
Opcode::Lli => write!(f, "lli"),
Opcode::Lui => write!(f, "lui"),
Opcode::Jmp => write!(f, "jmp"),
Opcode::Jeq => write!(f, "jeq"),
Opcode::Jne => write!(f, "jne"),
Opcode::Jgt => write!(f, "jgt"),
Opcode::Jge => write!(f, "jge"),
Opcode::Jlt => write!(f, "jlt"),
Opcode::Jle => write!(f, "jle"),
Opcode::Cmp => write!(f, "cmp"),
Opcode::Inc => write!(f, "inc"),
Opcode::Dec => write!(f, "dec"),
Opcode::Shl => write!(f, "shl"),
Opcode::Shr => write!(f, "shr"),
Opcode::Add => write!(f, "add"),
Opcode::Sub => write!(f, "sub"),
Opcode::And => write!(f, "and"),
Opcode::Or => write!(f, "or"),
Opcode::Not => write!(f, "not"),
Opcode::Xor => write!(f, "xor"),
Opcode::Nand => write!(f, "nand"),
Opcode::Nor => write!(f, "nor"),
Opcode::Xnor => write!(f, "xnor"),
Opcode::Int => write!(f, "int"),
Opcode::Irt => write!(f, "irt"),
Opcode::Hlt => write!(f, "hlt"),
Opcode::Iadd => write!(f, "iadd"),
Opcode::Isub => write!(f, "isub"),
Opcode::Db => write!(f, "db"),
Opcode::Dh => write!(f, "dh"),
Opcode::Dw => write!(f, "dw"),
Opcode::Resb => write!(f, "resb"),
Opcode::Resh => write!(f, "resh"),
Opcode::Resw => write!(f, "resw"),
Opcode::Push => write!(f, "push"),
Opcode::Pop => write!(f, "pop"),
Opcode::Lwi => write!(f, "lwi"),
Opcode::Include => write!(f, "include"),
}
} }
} }
+8 -4
View File
@@ -1,6 +1,4 @@
use core::fmt;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr;
use common::prelude::{Instruction, Register}; use common::prelude::{Instruction, Register};
@@ -33,6 +31,12 @@ impl Program {
} }
} }
impl Default for Program {
fn default() -> Self {
Self::new()
}
}
impl Parser { impl Parser {
pub fn new(tokens: Vec<Token>) -> Parser { pub fn new(tokens: Vec<Token>) -> Parser {
Parser { Parser {
@@ -68,7 +72,7 @@ impl Parser {
for node in &self.nodes { for node in &self.nodes {
if let Opcode::Include = node.1 { if let Opcode::Include = node.1 {
// we want the path, and the name // we want the path, and the name
let name = if let Token::Symbol(name) = node.2.get(0).unwrap() { let name = if let Token::Symbol(name) = node.2.first().unwrap() {
name.name.clone() name.name.clone()
} else { } else {
unreachable!() unreachable!()
@@ -129,7 +133,7 @@ impl Parser {
// inc SPR // inc SPR
// STW reg, SPR // STW reg, SPR
let label = node.0.clone(); let label = node.0.clone();
let reg = node.2.get(0).unwrap(); let reg = node.2.first().unwrap();
vec![ vec![
Node( Node(
+1 -1
View File
@@ -10,7 +10,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
common = { path = "../common" } common = { path = "../common" }
assembler = { path = "../assembler" } assembler = { path = "../assembler" }
dsa_editor = { path = "../dsa_editor" } dsa_editor = { git = "https://github.com/zxq5-dev/egui_code_editor", package = "egui_code_editor", rev = "5eb313e" }
eframe = "0.31.1" eframe = "0.31.1"
egui = "0.31.1" egui = "0.31.1"
rfd = "0.15.3" rfd = "0.15.3"
+27 -15
View File
@@ -43,7 +43,7 @@ impl Component for Editor {
if ui.input(|i| i.key_pressed(Key::S) && i.modifiers.ctrl) { if ui.input(|i| i.key_pressed(Key::S) && i.modifiers.ctrl) {
self.save(); self.save();
}; }
self.render_toolbar(state, ui, ctx); self.render_toolbar(state, ui, ctx);
@@ -87,17 +87,23 @@ impl Editor {
fn filename(&self) -> &str { fn filename(&self) -> &str {
self.path self.path
.file_name() .file_name()
.unwrap_or(OsStr::new("Unnamed!")) .unwrap_or_else(|| OsStr::new("Unnamed!"))
.to_str() .to_str()
.unwrap() .map_or_else(
|| unreachable!("File name should be valid UTF-8."),
|ext| ext,
)
} }
fn extension(&self) -> &str { fn extension(&self) -> &str {
self.path self.path
.extension() .extension()
.unwrap_or(OsStr::new("Unknown!")) .unwrap_or_else(|| OsStr::new("Unknown!"))
.to_str() .to_str()
.unwrap() .map_or_else(
|| unreachable!("File name should be valid UTF-8."),
|ext| ext,
)
} }
fn save(&mut self) { fn save(&mut self) {
@@ -210,10 +216,10 @@ impl Editor {
); );
// Instruction column // Instruction column
let instruction = match Instruction::decode(value) { let instruction = Instruction::decode(value).map_or_else(
Ok(instruction) => instruction.to_string(), |_| format!("{value:10}"),
Err(_) => format!("{value:10}"), |instruction| instruction.to_string(),
}; );
ui.label( ui.label(
egui::RichText::new(instruction) egui::RichText::new(instruction)
@@ -230,19 +236,25 @@ impl Editor {
fn render_editor(&mut self, _state: &mut State, ui: &mut Ui, _ctx: &Context) { fn render_editor(&mut self, _state: &mut State, ui: &mut Ui, _ctx: &Context) {
let available_width = ui.available_width(); let available_width = ui.available_width();
let syntax = match self.extension() { let syntax = match self.extension() {
"dsa" => Syntax::dsa(), "dsa" => Some(Syntax::new("dsa")),
_ => Syntax::dsa(), _ => None,
}; };
CodeEditor::default() let ed = CodeEditor::default()
.id_source("editor") .id_source("editor")
.with_fontsize(12.0) .with_fontsize(12.0)
.with_rows(0) .with_rows(0)
.with_theme(ColorTheme::default()) .with_theme(ColorTheme::default())
.with_syntax(syntax)
.with_numlines(true) .with_numlines(true)
.desired_width(available_width - 450.0) .desired_width(available_width - 450.0);
.show(ui, &mut self.text);
let mut editor = ed.clone();
if let Some(syntax) = syntax {
editor = ed.with_syntax(syntax);
}
editor.show(ui, &mut self.text);
} }
fn render_toolbar(&mut self, _state: &mut State, ui: &mut Ui, _ctx: &Context) { fn render_toolbar(&mut self, _state: &mut State, ui: &mut Ui, _ctx: &Context) {