diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 2811250..219851d 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -103,14 +103,13 @@ pub struct IfExpression( ); #[derive(Debug, Clone)] -pub struct LetStatement( - pub String, - pub Option, - /// Mutability - pub bool, - pub Expression, - pub TokenRange, -); +pub struct LetStatement { + pub name: String, + pub ty: Option, + pub mutable: bool, + pub value: Expression, + pub name_range: TokenRange, +} #[derive(Debug, Clone)] pub struct ImportStatement(pub Vec, pub TokenRange); diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 47c1b59..02525ef 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -318,17 +318,18 @@ impl Parse for LetStatement { let mutability = stream.expect(Token::MutKeyword).is_ok(); if let Some(Token::Identifier(variable)) = stream.next() { + let range = stream.get_range_prev().unwrap(); stream.expect(Token::Equals)?; let expression = stream.parse()?; stream.expect(Token::Semi)?; - Ok(LetStatement( - variable, - None, // TODO add possibility to name type - mutability, - expression, - stream.get_range().unwrap(), - )) + Ok(LetStatement { + name: variable, + ty: None, + mutable: mutability, + value: expression, + name_range: range, + }) } else { Err(stream.expected_err("identifier")?) } diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 395602a..6d67098 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -114,17 +114,17 @@ impl ast::Block { mir::StmtKind::Let( mir::NamedVariableRef( s_let - .1 + .ty .clone() .map(|t| t.0.into()) .unwrap_or(mir::TypeKind::Vague(mir::VagueType::Unknown)), - s_let.0.clone(), - s_let.4.as_meta(module_id), + s_let.name.clone(), + s_let.name_range.as_meta(module_id), ), - s_let.2, - s_let.3.process(module_id), + s_let.mutable, + s_let.value.process(module_id), ), - s_let.4, + s_let.name_range + s_let.value.1, ), ast::BlockLevelStatement::Set(var_ref, expression, range) => ( StmtKind::Set(var_ref.process(module_id), expression.process(module_id)), diff --git a/reid/src/token_stream.rs b/reid/src/token_stream.rs index 5d86319..7f18a81 100644 --- a/reid/src/token_stream.rs +++ b/reid/src/token_stream.rs @@ -181,6 +181,15 @@ impl<'a, 'b> TokenStream<'a, 'b> { end: self.position, }) } + + /// Gets range from the previous position to the current. Useful when using + /// with [`TokenStream::next`] + pub fn get_range_prev(&self) -> Option { + self.ref_position.as_ref().map(|ref_pos| TokenRange { + start: **ref_pos, + end: self.position - 1, + }) + } } impl Drop for TokenStream<'_, '_> { diff --git a/reid_src/borrow_hard.reid b/reid_src/borrow_hard.reid index 38fe32e..8fc9e78 100644 --- a/reid_src/borrow_hard.reid +++ b/reid_src/borrow_hard.reid @@ -5,9 +5,9 @@ fn changer(param: &mut u32) { } fn main() -> u32 { - let mut value = 6; + let value = 6; - changer(&mut value); + let mut a = &value; return value; }