cargo fix + cargo fmt

This commit is contained in:
raphy 2023-05-02 16:26:54 +02:00
parent 3023e578b4
commit d17d946fec
5 changed files with 40 additions and 54 deletions

View File

@ -1,5 +1,3 @@
use crate::cpu::Word;
type RegisterMem = String; type RegisterMem = String;
pub type ConstId = String; pub type ConstId = String;

View File

@ -1,5 +1,5 @@
use super::{AST::*}; use super::AST::*;
use crate::cpu::{get_num, OP::*, get_memo}; use crate::cpu::get_num;
pub trait CodeFormat { pub trait CodeFormat {
fn encode_op(op: &Operation, sy: &SymbolTable, current_pc: u16) -> Option<Self> fn encode_op(op: &Operation, sy: &SymbolTable, current_pc: u16) -> Option<Self>
@ -57,7 +57,6 @@ impl CodeFormat for u16 {
return Some((0b0011 << 12) + (r1b << 8) + (r2b << 4) + r3b); return Some((0b0011 << 12) + (r1b << 8) + (r2b << 4) + r3b);
} }
Operation::XOR(r1, r2, r3) => { Operation::XOR(r1, r2, r3) => {
let (r1b, r2b, r3b) = ( let (r1b, r2b, r3b) = (
get_num(&r1)? as u16, get_num(&r1)? as u16,
get_num(&r2)? as u16, get_num(&r2)? as u16,
@ -112,7 +111,7 @@ impl CodeFormat for u16 {
Const::C(n) => (*n) as u16, Const::C(n) => (*n) as u16,
}; };
return Some((0b0110 << 12) + (r1b << 8) + cb); return Some((0b0110 << 12) + (r1b << 8) + cb);
}, }
Operation::ADDI(r1, c) => { Operation::ADDI(r1, c) => {
let r1b = get_num(&r1)? as u16; let r1b = get_num(&r1)? as u16;
let cb = match c { let cb = match c {
@ -120,7 +119,7 @@ impl CodeFormat for u16 {
Const::C(n) => (*n) as u16, Const::C(n) => (*n) as u16,
}; };
return Some((0b0111 << 12) + (r1b << 8) + cb); return Some((0b0111 << 12) + (r1b << 8) + cb);
}, }
Operation::CALL(r1, c) => { Operation::CALL(r1, c) => {
let r1b = get_num(&r1)? as u16; let r1b = get_num(&r1)? as u16;
let cb = match c { let cb = match c {
@ -128,7 +127,7 @@ impl CodeFormat for u16 {
Const::C(n) => (*n) as u16, Const::C(n) => (*n) as u16,
}; };
return Some((0b1110 << 12) + (r1b << 8) + cb); return Some((0b1110 << 12) + (r1b << 8) + cb);
}, }
Operation::JAL(r1, r2, c) => { Operation::JAL(r1, r2, c) => {
let r1b = get_num(&r1)? as u16; let r1b = get_num(&r1)? as u16;
let r2b = get_num(&r2)? as u16; let r2b = get_num(&r2)? as u16;
@ -137,8 +136,7 @@ impl CodeFormat for u16 {
Const::C(n) => (*n) as u16, Const::C(n) => (*n) as u16,
}; };
return Some((0b1010 << 12) + (r1b << 8) + (r2b << 4) + cb); return Some((0b1010 << 12) + (r1b << 8) + (r2b << 4) + cb);
},
} }
} }
} }
}

View File

@ -3,18 +3,15 @@ pub mod encoder;
pub mod parser; pub mod parser;
mod tests; mod tests;
use encoder::{SymbolTable};
use encoder::CodeFormat; use encoder::CodeFormat;
use encoder::SymbolTable;
use parser::Section; use parser::Section;
use crate::loader::unloader::make_string; use crate::loader::unloader::make_string;
use self::parser::SectionContent; use self::parser::SectionContent;
impl Section { impl Section {
fn get_size(&self) -> usize { fn get_size(&self) -> usize {
match &self.content { match &self.content {
@ -23,8 +20,10 @@ impl Section {
let c = s.len(); let c = s.len();
if c % 2 != 0 { if c % 2 != 0 {
(c + 1) / 2 + 1 (c + 1) / 2 + 1
} else {c/2 + 1} } else {
}, c / 2 + 1
}
}
SectionContent::CVec() => todo!(), SectionContent::CVec() => todo!(),
} }
} }
@ -41,22 +40,21 @@ impl Section {
pc += 1; pc += 1;
} }
return Some(res); return Some(res);
}
},
SectionContent::CString(s) => { SectionContent::CString(s) => {
return Some(make_string(s)); return Some(make_string(s));
}
},
SectionContent::CVec() => todo!(), SectionContent::CVec() => todo!(),
} }
} }
} }
fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> { fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
// we start with a mock section that we'll just replace. // we start with a mock section that we'll just replace.
let mut res: Vec<Section> = vec![Section { name: "".to_owned(), content: SectionContent::CString("".to_owned())}]; let mut res: Vec<Section> = vec![Section {
name: "".to_owned(),
content: SectionContent::CString("".to_owned()),
}];
let mut nocode: Vec<Section> = vec![]; let mut nocode: Vec<Section> = vec![];
@ -68,8 +66,7 @@ fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
} else { } else {
res.push(section); res.push(section);
} }
} } else {
else {
nocode.push(section); nocode.push(section);
} }
} }
@ -80,7 +77,6 @@ fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
} }
fn make_symbol_table<'a>(sections: &'a Vec<Section>) -> Option<SymbolTable> { fn make_symbol_table<'a>(sections: &'a Vec<Section>) -> Option<SymbolTable> {
let mut res = vec![]; let mut res = vec![];
let mut pos: u16 = 0; let mut pos: u16 = 0;
@ -89,23 +85,19 @@ fn make_symbol_table<'a>(sections: &'a Vec<Section>) -> Option<SymbolTable> {
pos += sec.get_size() as u16; pos += sec.get_size() as u16;
} }
return Some(SymbolTable(res)); return Some(SymbolTable(res));
} }
pub fn to_binary(sections: Vec<Section>) -> Option<Vec<u16>> { pub fn to_binary(sections: Vec<Section>) -> Option<Vec<u16>> {
let sorted = sort_sections(sections)?; let sorted = sort_sections(sections)?;
println!("sorted sections: {:?}", sorted); println!("sorted sections: {:?}", sorted);
let sy = make_symbol_table(&sorted)?; let sy = make_symbol_table(&sorted)?;
println!("symbol table: {:?}", sy); println!("symbol table: {:?}", sy);
let k: Vec<Vec<u16>> = sorted.iter().map(|x| x.to_binary(&sy)).collect::<Option<Vec<Vec<u16>>>>()?; let k: Vec<Vec<u16>> = sorted
.iter()
.map(|x| x.to_binary(&sy))
.collect::<Option<Vec<Vec<u16>>>>()?;
println!("binary sections: {:?}", k); println!("binary sections: {:?}", k);
return Some(k.into_iter().flatten().collect()); return Some(k.into_iter().flatten().collect());
} }

View File

@ -252,7 +252,6 @@ fn parse_code_line(i: &str) -> Result<Operation, ParseError> {
// J-type // J-type
match op { match op {
"jal" => { "jal" => {
return Ok(JAL(r1.to_owned(), r2.to_owned(), parse_const(&r3)?)); return Ok(JAL(r1.to_owned(), r2.to_owned(), parse_const(&r3)?));
} }

View File

@ -1,15 +1,11 @@
use std::env::Args;
use std::env::args; use std::env::args;
use dekejit::assembler::parser; use dekejit::assembler::parser;
use dekejit::assembler::to_binary; use dekejit::assembler::to_binary;
use dekejit::cpu::IOBuffer; use dekejit::cpu::IOBuffer;
use dekejit::cpu::CPU; use dekejit::cpu::CPU;
use dekejit::loader::unloader::*;
fn main() { fn main() {
let args: Vec<String> = args().collect(); let args: Vec<String> = args().collect();
if args.len() < 2 { if args.len() < 2 {
@ -20,7 +16,10 @@ fn main() {
// let code = std::fs::read_to_string("./tests/assembly/hello_world.grasm").unwrap(); // let code = std::fs::read_to_string("./tests/assembly/hello_world.grasm").unwrap();
let code = match std::fs::read_to_string(&args[1]) { let code = match std::fs::read_to_string(&args[1]) {
Ok(p) => p, Ok(p) => p,
Err(_) => {println!("Could not open file '{}'", &args[1]); return;} Err(_) => {
println!("Could not open file '{}'", &args[1]);
return;
}
}; };
let mut parser = parser::Parser::new(code); let mut parser = parser::Parser::new(code);