refactored the code
This commit is contained in:
parent
4b3e7e9462
commit
ef67e11601
10 changed files with 112 additions and 53 deletions
12
Cargo.lock
generated
12
Cargo.lock
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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<String>, output: String) {
|
||||
let mut functions: HashMap<String, u8> = HashMap::new();
|
||||
let mut functions: HashMap<String, i32> = HashMap::new();
|
||||
let mut hex_code: Vec<Instruction> = 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<String>, output: String) {
|
|||
};
|
||||
|
||||
let mut line_split = code_line.split(' ').collect::<Vec<&str>>();
|
||||
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<String>, 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<u8> = 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();
|
||||
}
|
||||
|
||||
|
|
|
|||
23
src/instruction_type_handlers/call.rs
Normal file
23
src/instruction_type_handlers/call.rs
Normal file
|
|
@ -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<String, i32>,
|
||||
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!()
|
||||
}
|
||||
3
src/instruction_type_handlers/mod.rs
Normal file
3
src/instruction_type_handlers/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod call;
|
||||
pub mod normal;
|
||||
pub mod word;
|
||||
37
src/instruction_type_handlers/normal.rs
Normal file
37
src/instruction_type_handlers/normal.rs
Normal file
|
|
@ -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<u8> = 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
|
||||
}
|
||||
3
src/instruction_type_handlers/word.rs
Normal file
3
src/instruction_type_handlers/word.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub fn build_word_instruction() {
|
||||
unimplemented!()
|
||||
}
|
||||
|
|
@ -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<String> {
|
|||
exit(1);
|
||||
}
|
||||
};
|
||||
return file.lines().map(String::from).collect();
|
||||
file.lines().map(String::from).collect()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
BIN
test
Normal file
BIN
test
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue