diff --git a/src/assembler/AST.rs b/src/assembler/AST.rs index 8d1e15c..46497d7 100644 --- a/src/assembler/AST.rs +++ b/src/assembler/AST.rs @@ -1,5 +1,3 @@ -use crate::cpu::Word; - type RegisterMem = String; pub type ConstId = String; diff --git a/src/assembler/encoder.rs b/src/assembler/encoder.rs index 7b949e8..d7bf849 100644 --- a/src/assembler/encoder.rs +++ b/src/assembler/encoder.rs @@ -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 @@ -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); - }, + } } } } - diff --git a/src/assembler/mod.rs b/src/assembler/mod.rs index 535d247..9cd7914 100644 --- a/src/assembler/mod.rs +++ b/src/assembler/mod.rs @@ -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
) -> Option> { - // we start with a mock section that we'll just replace. - let mut res: Vec
= vec![Section { name: "".to_owned(), content: SectionContent::CString("".to_owned())}]; + let mut res: Vec
= vec![Section { + name: "".to_owned(), + content: SectionContent::CString("".to_owned()), + }]; let mut nocode: Vec
= vec![]; @@ -68,8 +66,7 @@ fn sort_sections(sections: Vec
) -> Option> { } else { res.push(section); } - } - else { + } else { nocode.push(section); } } @@ -80,32 +77,27 @@ fn sort_sections(sections: Vec
) -> Option> { } fn make_symbol_table<'a>(sections: &'a Vec
) -> Option { - 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
) -> Option> { - let sorted = sort_sections(sections)?; println!("sorted sections: {:?}", sorted); let sy = make_symbol_table(&sorted)?; println!("symbol table: {:?}", sy); - let k: Vec> = sorted.iter().map(|x| x.to_binary(&sy)).collect::>>>()?; + let k: Vec> = sorted + .iter() + .map(|x| x.to_binary(&sy)) + .collect::>>>()?; println!("binary sections: {:?}", k); - return Some(k.into_iter().flatten().collect()); - } - diff --git a/src/assembler/parser.rs b/src/assembler/parser.rs index 86a885e..2a1231b 100644 --- a/src/assembler/parser.rs +++ b/src/assembler/parser.rs @@ -90,7 +90,7 @@ fn take_between(i: &str, start: &str, stop: &str) -> Option<(String, String)> { return take_alpha_till(&s1, stop); } -/// finds special escaped characters in a string +/// finds special escaped characters in a string /// (such as \n) and replaces them with the actual special /// character fn escaped_codes() {} @@ -252,7 +252,6 @@ fn parse_code_line(i: &str) -> Result { // J-type match op { - "jal" => { return Ok(JAL(r1.to_owned(), r2.to_owned(), parse_const(&r3)?)); } diff --git a/src/main.rs b/src/main.rs index 37fd70e..bc4a217 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,35 +1,34 @@ -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 = args().collect(); - let args : Vec = args().collect(); + if args.len() < 2 { + println!("Scialla"); + return; + } - if args.len() < 2 { - println!("Scialla"); + // 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; } + }; - // 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;} - }; + let mut parser = parser::Parser::new(code); - let mut parser = parser::Parser::new(code); + let r = parser.parse_sections().unwrap(); - let r = parser.parse_sections().unwrap(); + println!("Parsed sections: {:?}", r); - println!("Parsed sections: {:?}", r); - - let code = to_binary(r).unwrap(); + let code = to_binary(r).unwrap(); // let mut k = make_string("Hello world!"); //