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;
pub type ConstId = String;

View File

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

View File

@ -3,18 +3,15 @@ pub mod encoder;
pub mod parser;
mod tests;
use encoder::{SymbolTable};
use encoder::CodeFormat;
use encoder::SymbolTable;
use parser::Section;
use crate::loader::unloader::make_string;
use self::parser::SectionContent;
impl Section {
fn get_size(&self) -> usize {
match &self.content {
@ -22,9 +19,11 @@ impl Section {
SectionContent::CString(s) => {
let c = s.len();
if c % 2 != 0 {
(c+1)/2 + 1
} else {c/2 + 1}
},
(c + 1) / 2 + 1
} else {
c / 2 + 1
}
}
SectionContent::CVec() => todo!(),
}
}
@ -41,22 +40,21 @@ impl Section {
pc += 1;
}
return Some(res);
},
}
SectionContent::CString(s) => {
return Some(make_string(s));
},
}
SectionContent::CVec() => todo!(),
}
}
}
fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
// 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![];
@ -68,8 +66,7 @@ fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
} else {
res.push(section);
}
}
else {
} else {
nocode.push(section);
}
}
@ -80,32 +77,27 @@ fn sort_sections(sections: Vec<Section>) -> Option<Vec<Section>> {
}
fn make_symbol_table<'a>(sections: &'a Vec<Section>) -> Option<SymbolTable> {
let mut res = vec![];
let mut pos : u16 = 0;
let mut pos: u16 = 0;
for sec in sections {
res.push((sec.name.clone(), pos));
pos += sec.get_size() as u16;
}
return Some(SymbolTable(res));
}
pub fn to_binary(sections: Vec<Section>) -> Option<Vec<u16>> {
let sorted = sort_sections(sections)?;
println!("sorted sections: {:?}", sorted);
let sy = make_symbol_table(&sorted)?;
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);
return Some(k.into_iter().flatten().collect());
}

View File

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

View File

@ -1,16 +1,12 @@
use std::env::Args;
use std::env::args;
use dekejit::assembler::parser;
use dekejit::assembler::to_binary;
use dekejit::cpu::IOBuffer;
use dekejit::cpu::CPU;
use dekejit::loader::unloader::*;
fn main() {
let args : Vec<String> = args().collect();
let args: Vec<String> = args().collect();
if args.len() < 2 {
println!("Scialla");
@ -20,7 +16,10 @@ fn main() {
// let code = std::fs::read_to_string("./tests/assembly/hello_world.grasm").unwrap();
let code = match std::fs::read_to_string(&args[1]) {
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);