From ef67e11601db0d77d3fc4755ffa2872ae76d1447 Mon Sep 17 00:00:00 2001 From: Shadowbee27 Date: Fri, 30 Jan 2026 17:53:46 +0100 Subject: [PATCH] refactored the code --- Cargo.lock | 12 ++-- Cargo.toml | 2 +- src/assemble.rs | 78 +++++++++++------------- src/instruction_type_handlers/call.rs | 23 +++++++ src/instruction_type_handlers/mod.rs | 3 + src/instruction_type_handlers/normal.rs | 37 +++++++++++ src/instruction_type_handlers/word.rs | 3 + src/main.rs | 3 +- src/opt_codes.rs | 4 +- test | Bin 0 -> 300 bytes 10 files changed, 112 insertions(+), 53 deletions(-) create mode 100644 src/instruction_type_handlers/call.rs create mode 100644 src/instruction_type_handlers/mod.rs create mode 100644 src/instruction_type_handlers/normal.rs create mode 100644 src/instruction_type_handlers/word.rs create mode 100644 test diff --git a/Cargo.lock b/Cargo.lock index 6a4ba4e..2fe01c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,9 +63,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.54" +version = "4.5.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394" +checksum = "a75ca66430e33a14957acc24c5077b503e7d374151b2b4b3a10c83b4ceb4be0e" dependencies = [ "clap_builder", "clap_derive", @@ -73,9 +73,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.54" +version = "4.5.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00" +checksum = "793207c7fa6300a0608d1080b858e5fdbe713cdc1c8db9fb17777d8a13e63df0" dependencies = [ "anstream", "anstyle", @@ -85,9 +85,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.49" +version = "4.5.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" dependencies = [ "heck", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 61fe664..cf80c74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [dependencies] -clap = { version = "4.5.54", features = ["derive"] } +clap = { version = "4.5.56", features = ["derive"] } progress_bar = "1.4.0" data-encoding = "2.10.0" struct_iterable = "0.1.1" diff --git a/src/assemble.rs b/src/assemble.rs index c9940e0..f253f86 100644 --- a/src/assemble.rs +++ b/src/assemble.rs @@ -1,19 +1,19 @@ +use crate::instruction_type_handlers::{call, normal}; +use crate::opt_codes::get_hex; use crate::{exit, instruction::*}; use log::debug; use progress_bar::*; use std::collections::HashMap; use std::fs::File; -use crate::opt_codes::get_hex; - pub fn assemble(code: Vec, output: String) { - let mut functions: HashMap = HashMap::new(); + let mut functions: HashMap = HashMap::new(); let mut hex_code: Vec = Vec::new(); init_progress_bar_with_eta(code.len()); set_progress_bar_action("Assembling:", Color::Blue, Style::Bold); - let mut count: u8 = 0; + let mut count: i32 = 0; for line in code.iter() { count += 1; @@ -24,7 +24,7 @@ pub fn assemble(code: Vec, output: String) { }; let mut line_split = code_line.split(' ').collect::>(); - line_split.retain(|&x| x != ""); + line_split.retain(|&x| !x.is_empty()); debug!("({:?})", line_split); if line_split.is_empty() { @@ -45,53 +45,45 @@ pub fn assemble(code: Vec, output: String) { continue; } + if line_split[0].starts_with('_') { + let word_name = line_split[0].replace('_', ""); + if word_name.is_empty() { + set_progress_bar_action("Error", Color::Red, Style::Normal); + finalize_progress_bar(); + eprintln!("Found invalid function name: ({})", word_name); + exit(1) + } + // TODO: Somehow create a Instruction for the word + inc_progress_bar(); + continue; + } + let opt_code = line_split[0]; - let opt_code_hex = match get_hex(&opt_code) { + let opt_code_hex = match get_hex(opt_code) { Some(i) => i, None => { - let func_pos = match functions.get(opt_code) { - Some(r) => *r, - None => { - set_progress_bar_action("Error", Color::Red, Style::Normal); - finalize_progress_bar(); - - eprintln!( - "Invalid or unkonw function/instruction called: ({})", - opt_code - ); - exit(1) - } - }; - (func_pos, 0, InstructionType::Call) + eprintln!( + "Invalid or unkonw function/instruction called: ({})", + opt_code + ); + exit(1) } }; - if !(opt_code_hex.1 == (line_split.len() as i32 - 1)) { - set_progress_bar_action("Error", Color::Red, Style::Normal); - finalize_progress_bar(); - - eprintln!( - "Found {} arguments for {}, but expected {}", - line_split.len() as i32 - 1, + match opt_code_hex.2 { + InstructionType::Call => hex_code.push(call::build_call_instruction( + line_split[1], + &functions, + count, + )), + InstructionType::Instruction => hex_code.push(normal::build_normal_instruction( + opt_code_hex, opt_code, - opt_code_hex.1 - ); - exit(1) + line_split, + )), + InstructionType::Word => print!(""), } - let mut args: Vec = Vec::new(); - - for arg in &line_split[1..] { - args.push(u8::from_str_radix(arg.strip_prefix("0x").unwrap_or(arg), 16).unwrap()); - } - debug!("{:?}", args); - - let len = args.len(); - let instruction = new_instruction(opt_code_hex.0, args, len); - - debug!("{:?}", instruction); - - hex_code.push(instruction); inc_progress_bar(); } diff --git a/src/instruction_type_handlers/call.rs b/src/instruction_type_handlers/call.rs new file mode 100644 index 0000000..5cdca3c --- /dev/null +++ b/src/instruction_type_handlers/call.rs @@ -0,0 +1,23 @@ +use crate::exit; +use crate::instruction::Instruction; +use progress_bar::*; +use std::collections::HashMap; + +pub fn build_call_instruction( + func_name: &str, + functions: &HashMap, + current_pos: i32, +) -> Instruction { + let func_pos = match functions.get(func_name) { + Some(i) => i, + None => { + set_progress_bar_action("Error", Color::Red, Style::Normal); + finalize_progress_bar(); + eprintln!("Found invalid/unknown function name: ({})", func_name); + exit(1) + } + }; + + let offset = current_pos - func_pos; + unimplemented!() +} diff --git a/src/instruction_type_handlers/mod.rs b/src/instruction_type_handlers/mod.rs new file mode 100644 index 0000000..0ad6929 --- /dev/null +++ b/src/instruction_type_handlers/mod.rs @@ -0,0 +1,3 @@ +pub mod call; +pub mod normal; +pub mod word; diff --git a/src/instruction_type_handlers/normal.rs b/src/instruction_type_handlers/normal.rs new file mode 100644 index 0000000..b72620e --- /dev/null +++ b/src/instruction_type_handlers/normal.rs @@ -0,0 +1,37 @@ +use crate::instruction::{Instruction, InstructionType}; +use crate::{exit, instruction::*}; +use log::debug; +use progress_bar::*; + +pub fn build_normal_instruction( + opt_code_hex: (u8, i32, InstructionType), + opt_code: &str, + line: Vec<&str>, +) -> Instruction { + if opt_code_hex.1 != (line.len() as i32 - 1) { + set_progress_bar_action("Error", Color::Red, Style::Normal); + finalize_progress_bar(); + + eprintln!( + "Found {} arguments for {}, but expected {}", + line.len() as i32 - 1, + opt_code, + opt_code_hex.1 + ); + exit(1) + } + + let mut args: Vec = Vec::new(); + + for arg in &line[1..] { + args.push(u8::from_str_radix(arg.strip_prefix("0x").unwrap_or(arg), 16).unwrap()); + } + debug!("{:?}", args); + + let len = args.len(); + let instruction = new_instruction(opt_code_hex.0, args, len); + + debug!("{:?}", instruction); + + instruction +} diff --git a/src/instruction_type_handlers/word.rs b/src/instruction_type_handlers/word.rs new file mode 100644 index 0000000..e10ea98 --- /dev/null +++ b/src/instruction_type_handlers/word.rs @@ -0,0 +1,3 @@ +pub fn build_word_instruction() { + unimplemented!() +} diff --git a/src/main.rs b/src/main.rs index 27e7f34..3f00053 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::process::exit; pub mod assemble; pub mod instruction; +pub mod instruction_type_handlers; pub mod opt_codes; #[derive(Parser, Debug)] @@ -28,5 +29,5 @@ fn get_code(path: String) -> Vec { exit(1); } }; - return file.lines().map(String::from).collect(); + file.lines().map(String::from).collect() } diff --git a/src/opt_codes.rs b/src/opt_codes.rs index a8dccbb..349512f 100644 --- a/src/opt_codes.rs +++ b/src/opt_codes.rs @@ -1,7 +1,7 @@ use crate::instruction::InstructionType; pub fn get_hex(function: &str) -> Option<(u8, i32, InstructionType)> { - return match function { + match function { // General "NOP" => Some(( u8::from_str_radix("00", 16).unwrap(), @@ -309,5 +309,5 @@ pub fn get_hex(function: &str) -> Option<(u8, i32, InstructionType)> { "CALL" => Some((0, 1, InstructionType::Call)), _ => None, - }; + } } diff --git a/test b/test new file mode 100644 index 0000000000000000000000000000000000000000..88698b17e03d8480eb664380658a32cc5b19560b GIT binary patch literal 300 zcmZ|KHx2?p5Cp)z~X*mC!BG?)wLV$o)0|n_k$mF(+zY0 literal 0 HcmV?d00001