cargo fmt
This commit is contained in:
parent
18a5f17b80
commit
0186f99678
@ -1,5 +1,5 @@
|
||||
mod AST;
|
||||
mod tests;
|
||||
mod parser;
|
||||
mod tests;
|
||||
|
||||
struct Assembler {}
|
||||
|
@ -1,6 +1,4 @@
|
||||
use crate::cpu::Registers;
|
||||
|
||||
use super::AST::{Operation, Const};
|
||||
use super::AST::{Const, Operation};
|
||||
use Operation::*;
|
||||
|
||||
type Loc = u16;
|
||||
@ -11,7 +9,7 @@ pub enum ParseError {
|
||||
UnknownSectionKind,
|
||||
UnexpectedEOF,
|
||||
BadSectionContent,
|
||||
BadInstruction
|
||||
BadInstruction,
|
||||
}
|
||||
|
||||
/// represents the state of our parser.
|
||||
@ -49,27 +47,23 @@ fn remove_comments(i: &str) -> &str {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Checks if the string i starts with pat.
|
||||
/// Returns the rest of the input string on success
|
||||
/// else, returns None
|
||||
fn match_string(i: &str, pat: &str) -> Option<String> {
|
||||
|
||||
let mut in_chars = i.chars();
|
||||
|
||||
for pat_c in pat.chars() {
|
||||
if let Some(c) = in_chars.next() {
|
||||
if c != pat_c {
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
let rest = in_chars.collect();
|
||||
|
||||
return Some(rest);
|
||||
|
||||
}
|
||||
|
||||
/// Matches till the "stop" string is found.
|
||||
@ -79,41 +73,38 @@ fn match_string(i: &str, pat: &str) -> Option<String> {
|
||||
/// Ex: assert_eq!(Ok("Lorem ", " Ipsum"), match_alpha_till("Lorem X Ipsum", "X") );
|
||||
///
|
||||
fn take_alpha_till(i: &str, stop: &str) -> Option<(String, String)> {
|
||||
|
||||
//
|
||||
if let Some((matched, rest)) = i.split_once(stop) {
|
||||
return Some((matched.to_string(), rest.to_string()))
|
||||
return Some((matched.to_string(), rest.to_string()));
|
||||
} else {
|
||||
return None
|
||||
return None;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// Matches inside the `start` and `stop` delimiters.
|
||||
/// Return a tuple with the string in between the two
|
||||
/// togheter with the rest of the string.
|
||||
fn take_between(i: &str, start: &str, stop: &str) -> Option<(String, String)> {
|
||||
|
||||
let s1 = match_string(i, start)?;
|
||||
|
||||
return take_alpha_till(&s1, stop);
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn take_between_test() {
|
||||
assert_eq!(take_between("\"wow\" etc", "\"", "\""), Some(("wow".to_string(), " etc".to_string())));
|
||||
assert_eq!(
|
||||
take_between("\"wow\" etc", "\"", "\""),
|
||||
Some(("wow".to_string(), " etc".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
//// SECTION PARSING
|
||||
|
||||
#[derive(Debug)]
|
||||
enum SectionContent {
|
||||
Code(Vec<Operation>),
|
||||
CString(String),
|
||||
CVec()
|
||||
CVec(),
|
||||
}
|
||||
|
||||
use SectionContent::*;
|
||||
@ -126,7 +117,6 @@ pub struct Section {
|
||||
|
||||
// A .section has a name and variable content.
|
||||
impl Parser {
|
||||
|
||||
pub fn parse_sections(&mut self) -> Result<Vec<Section>, ParseError> {
|
||||
let mut res = vec![];
|
||||
|
||||
@ -141,50 +131,48 @@ impl Parser {
|
||||
|
||||
match kind.as_str() {
|
||||
"text" => {
|
||||
let s : Vec<&str> = lines.clone().take_while(|&x| !(x).starts_with(".")).map(|x| x).collect();
|
||||
res.push(Section { name: name.trim().to_owned(), content: Code(parse_code(&s)?)})
|
||||
let s: Vec<&str> = lines
|
||||
.clone()
|
||||
.take_while(|&x| !(x).starts_with("."))
|
||||
.map(|x| x)
|
||||
.collect();
|
||||
res.push(Section {
|
||||
name: name.trim().to_owned(),
|
||||
content: Code(parse_code(&s)?),
|
||||
})
|
||||
}
|
||||
"asciiz" => {
|
||||
let Some(s) = lines.next() else {return Err(ParseError::UnexpectedEOF)};
|
||||
let Some((s, _)) = take_between(s.trim(), "\"", "\"") else {return Err(ParseError::BadSectionContent)};
|
||||
res.push(Section { name: name.trim().to_owned(), content: CString(s)})
|
||||
|
||||
res.push(Section {
|
||||
name: name.trim().to_owned(),
|
||||
content: CString(s),
|
||||
})
|
||||
}
|
||||
"i16" => {
|
||||
let s = lines.next();
|
||||
|
||||
let _s = lines.next();
|
||||
}
|
||||
"u16" => {
|
||||
let s = lines.next();
|
||||
|
||||
let _s = lines.next();
|
||||
}
|
||||
"vi16" => {
|
||||
let s = lines.next();
|
||||
|
||||
let _s = lines.next();
|
||||
}
|
||||
"vu16" => {
|
||||
let s = lines.next();
|
||||
|
||||
let _s = lines.next();
|
||||
}
|
||||
_ => {
|
||||
return Err(ParseError::UnknownSectionKind);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
fn parse_code(i: &[&str]) -> Result<Vec<Operation>, ParseError> {
|
||||
|
||||
let mut res = vec![];
|
||||
|
||||
for line in i {
|
||||
@ -194,9 +182,7 @@ fn parse_code(i: &[&str]) -> Result<Vec<Operation>, ParseError> {
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
|
||||
fn parse_code_line(i: &str) -> Result<Operation, ParseError> {
|
||||
|
||||
// every operation has at most 3 arguments
|
||||
let mut bits = i.split_whitespace();
|
||||
println!("current parse code line: {}", i);
|
||||
@ -204,8 +190,12 @@ fn parse_code_line(i: &str) -> Result<Operation, ParseError> {
|
||||
|
||||
// no type
|
||||
match op {
|
||||
"nop" => {return Ok(NOP);},
|
||||
"halt" => {return Ok(HALT);},
|
||||
"nop" => {
|
||||
return Ok(NOP);
|
||||
}
|
||||
"halt" => {
|
||||
return Ok(HALT);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
@ -219,28 +209,20 @@ fn parse_code_line(i: &str) -> Result<Operation, ParseError> {
|
||||
}
|
||||
"sli" => {
|
||||
return Ok(SLI(r1.to_owned(), parse_const(r2)?));
|
||||
|
||||
}
|
||||
"call" => {
|
||||
|
||||
return Ok(CALL(r1.to_owned(), parse_const(r2)?));
|
||||
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
||||
return Err(ParseError::BadInstruction);
|
||||
|
||||
}
|
||||
|
||||
fn parse_const(i: &str) -> Result<Const, ParseError> {
|
||||
|
||||
// we try to parse the number, if we fail, we treat it as a string.
|
||||
let Ok(num) = i.parse() else {
|
||||
return Ok(Const::CS(i.to_owned()));
|
||||
};
|
||||
return Ok(Const::C(num));
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
// use super::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::assembler::parser;
|
||||
|
||||
#[test]
|
||||
fn parser_test() {
|
||||
|
||||
println!("Parser test begins");
|
||||
let code = std::fs::read_to_string("./tests/assembly/hello_world.grasm").unwrap();
|
||||
|
||||
@ -14,3 +15,4 @@ fn parser_test() {
|
||||
|
||||
println!("Parsed sections: {:?}", r);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,9 @@ mod decoder;
|
||||
mod ram;
|
||||
mod registers;
|
||||
mod sysenv;
|
||||
|
||||
mod tests;
|
||||
|
||||
pub use sysenv::*;
|
||||
|
||||
pub use registers::*;
|
||||
|
@ -70,7 +70,7 @@ impl Sys for IOBuffer {
|
||||
.slice(pos, MAX_MEM as Word - 1)
|
||||
.ok_or(ExecErr::InvalidMemoryAddr)?;
|
||||
let (s, _) = find_and_read_string(&data)
|
||||
.map_err(|p| ExecErr::SyscallError("parse error!".to_owned()))?;
|
||||
.map_err(|_p| ExecErr::SyscallError("parse error!".to_owned()))?;
|
||||
cpu.env.output.push_str(&s);
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,13 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
use crate::loader::unloader::*;
|
||||
|
||||
use crate::{
|
||||
cpu::{IOBuffer, CPU},
|
||||
loader::unloader::make_string,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn hello_world_binary_test() {
|
||||
|
||||
let hw = String::from("Hello world!");
|
||||
|
||||
let mut k = make_string(&hw);
|
||||
@ -31,5 +32,4 @@ fn hello_world_binary_test() {
|
||||
|
||||
assert_eq!(hw, cpu.env.output);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ pub mod loader;
|
||||
pub mod assembler;
|
||||
//
|
||||
|
||||
|
||||
pub fn interpret_as_signed(x: u16) -> i16 {
|
||||
// the two types have the same size.
|
||||
unsafe {
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::cpu::Word;
|
||||
|
||||
use super::{constants::MAGIC, Section};
|
||||
use super::Section;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ParseError {
|
||||
@ -71,7 +71,7 @@ pub fn read_binary(b: &[u16]) -> Result<Vec<Section>, ParseError> {
|
||||
|
||||
/// Parses binary headers
|
||||
fn parse_header(b: &[Word]) -> Result<Vec<(u16, u16)>, ParseError> {
|
||||
let Some([m, s]) = b.get(0..2) else {return Err(ParseError::EmptyHeader)};
|
||||
let Some([_m, s]) = b.get(0..2) else {return Err(ParseError::EmptyHeader)};
|
||||
|
||||
// Magic number check. Can go unchecked, check spec.
|
||||
// if (*m != MAGIC) {
|
||||
|
@ -1,10 +1,13 @@
|
||||
use super::loader::*;
|
||||
use super::unloader::*;
|
||||
use super::*;
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::loader::Section;
|
||||
|
||||
use super::super::loader::*;
|
||||
use super::super::unloader::*;
|
||||
|
||||
// fuzzable, TODO
|
||||
fn write_read_str_identity(s: &str) {
|
||||
let mut bytes = make_string(s);
|
||||
let bytes = make_string(s);
|
||||
|
||||
// pop null-terminator;
|
||||
// bytes.pop().expect("String doesn't even have a single byte?");
|
||||
@ -15,7 +18,7 @@ fn write_read_str_identity(s: &str) {
|
||||
|
||||
fn read_write_str_identity(b: Vec<u16>) {
|
||||
let (string, _) = find_and_read_string(&b).unwrap();
|
||||
let mut n_term = b.clone();
|
||||
let n_term = b.clone();
|
||||
// n_term.push(0);
|
||||
// println!("r-w s: {:?}, b: {:?}", string, n_term);
|
||||
assert_eq!(n_term, make_string(&string));
|
||||
@ -59,3 +62,4 @@ fn sy_test() {
|
||||
let parsed_symbol_table = read_binary(&bin).expect("Wtf! We got error!");
|
||||
println!("{:?}", parsed_symbol_table);
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,4 @@ fn main() {
|
||||
}
|
||||
Err(e) => println!("Err: {:?}", e),
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user