Fix improper handling of Loc on newline

This commit is contained in:
Lumi Kalt 2024-01-22 11:20:31 +00:00
parent 2122535f56
commit e90f615e70
4 changed files with 33 additions and 19 deletions

View file

@ -192,7 +192,7 @@ impl Env {
} }
_ => unimplemented!(), _ => unimplemented!(),
})?; })?;
Ok(u32::from_str_radix(&with(i, imm, regs).0.to_string(), 2).unwrap()) Ok(u32::from_str_radix(dbg!(&with(i, imm, regs).0.to_string()), 2).unwrap())
} else { } else {
unreachable!() unreachable!()
} }

View file

@ -111,7 +111,7 @@ pub mod kind {
impl Display for Pseudo { impl Display for Pseudo {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
// (pseudo) padded on either side with - to make it 32 characters // (pseudo) padded on either side with - to make it 32 characters
write!(f, "{:-^32}", "(pseudo)") write!(f, "{:0^32}", 0)
} }
} }
impl Display for R { impl Display for R {
@ -253,7 +253,10 @@ pub fn instruction(op: &str) -> (Kind, Vec<Arg>) {
"nop" => (Kind::Pseudo(Pseudo {}), vec![]), "nop" => (Kind::Pseudo(Pseudo {}), vec![]),
// Move // Move
"li" => (Kind::Pseudo(Pseudo {}), vec![Arg::Register(0), Arg::Immediate]), "li" => (
Kind::Pseudo(Pseudo {}),
vec![Arg::Register(0), Arg::Immediate],
),
"lui" => ( "lui" => (
Kind::U(U { Kind::U(U {
imm: to_bits(0), imm: to_bits(0),

View file

@ -1,7 +1,6 @@
/// TODO: Strings, Symbols /// TODO: Strings, Symbols
use crate::{env::Env, err::SyntaxErr}; use crate::{env::Env, err::SyntaxErr};
use itertools::Itertools; use itertools::Itertools;
use rayon::prelude::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Token { pub enum Token {
@ -57,14 +56,12 @@ pub struct Loc {
pub end: usize, pub end: usize,
} }
fn parse_line(env: &Env, input: &str, line: usize, loc: &mut Loc) -> Result<Vec<(Token, Loc)>, ParseErr> { fn parse_line(env: &Env, input: &str, loc: &mut Loc) -> Result<Vec<(Token, Loc)>, ParseErr> {
let mut tokens: Vec<(Token, Loc)> = Vec::new(); let mut tokens: Vec<(Token, Loc)> = Vec::new();
let mut chars = input.chars().peekable(); let mut chars = input.chars().peekable();
use Token::*; use Token::*;
loc.line = line;
while let Some(c) = chars.next() { while let Some(c) = chars.next() {
let token = match c { let token = match c {
'\t' | ' ' => Spacing, '\t' | ' ' => Spacing,
@ -89,7 +86,7 @@ fn parse_line(env: &Env, input: &str, line: usize, loc: &mut Loc) -> Result<Vec<
return Err(( return Err((
SyntaxErr::UnexpectedChar, SyntaxErr::UnexpectedChar,
Loc { Loc {
line, line: loc.line,
start: loc.end + 1, start: loc.end + 1,
end: loc.end + 1, end: loc.end + 1,
}, },
@ -103,7 +100,7 @@ fn parse_line(env: &Env, input: &str, line: usize, loc: &mut Loc) -> Result<Vec<
while let Some('0'..='9') = chars.peek() { while let Some('0'..='9') = chars.peek() {
num.push(chars.next().unwrap()); num.push(chars.next().unwrap());
} }
Immediate(num.parse().unwrap()) Immediate(num.parse::<i32>().unwrap() as u32)
} }
'(' => { '(' => {
let start = loc.start + 2; let start = loc.start + 2;
@ -134,7 +131,11 @@ fn parse_line(env: &Env, input: &str, line: usize, loc: &mut Loc) -> Result<Vec<
if !env.is_valid_register(reg) { if !env.is_valid_register(reg) {
return Err(( return Err((
SyntaxErr::MemoryInvalidRegister, SyntaxErr::MemoryInvalidRegister,
Loc { line, start, end }, Loc {
line: loc.line,
start,
end,
},
tokens.clone(), tokens.clone(),
None, None,
)); ));
@ -185,10 +186,13 @@ fn parse_line(env: &Env, input: &str, line: usize, loc: &mut Loc) -> Result<Vec<
_ => return Err((SyntaxErr::UnexpectedChar, loc.clone(), tokens.clone(), None)), _ => return Err((SyntaxErr::UnexpectedChar, loc.clone(), tokens.clone(), None)),
}; };
tokens.push((token, loc.clone())); tokens.push((token, loc.clone()));
loc.end += 1; // Newline loc.end += 1;
loc.start = loc.end; loc.start = loc.end;
} }
loc.end += 1; // Newline
loc.start = loc.end;
let tokens = tokens let tokens = tokens
.into_iter() .into_iter()
.filter(|(token, _)| !matches!(token, Token::Spacing)) .filter(|(token, _)| !matches!(token, Token::Spacing))
@ -261,14 +265,16 @@ pub fn parse(env: &Env, input: &str) -> Result<Vec<(Token, Loc)>, Vec<ParseErr>>
let parsed_lines = input let parsed_lines = input
.lines() .lines()
.enumerate() .enumerate()
.map(|(i, line)| parse_line(env, line, i + 1, &mut loc)) .map(|(i, line)| {
loc.line = i + 1;
parse_line(env, line, &mut loc)
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let (ok, err): (Vec<_>, Vec<_>) = parsed_lines.into_iter() let (ok, err): (Vec<_>, Vec<_>) = parsed_lines
.into_iter()
.partition(|line| matches!(line, Ok(_))); .partition(|line| matches!(line, Ok(_)));
dbg!(err.clone());
if err.is_empty() { if err.is_empty() {
Ok(ok.into_iter().flat_map(|line| line.unwrap()).collect()) Ok(ok.into_iter().flat_map(|line| line.unwrap()).collect())
} else { } else {

13
test.s
View file

@ -1,4 +1,9 @@
a: li a0 5
a0 addi a0 zero 1 li a1 1
addi a1 zero 2
a0 add a2 a1 a0 factorial:
beqz a0 end
mul a1 a1 a0
addi a0 a0 -1
bneqz factorial
end: