From ba8ee770fb5ff4f5a688ab7080c8563ac0bdfec8 Mon Sep 17 00:00:00 2001 From: sofia Date: Sun, 20 Jul 2025 21:25:08 +0300 Subject: [PATCH] Parse deref and borrow --- reid/src/ast/mod.rs | 2 ++ reid/src/ast/parse.rs | 14 +++++++++++++- reid/src/ast/process.rs | 2 ++ reid/src/lexer.rs | 8 ++++---- reid_src/borrow.reid | 11 +++++++++++ 5 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 reid_src/borrow.reid diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 51a8e97..56b5a2f 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -42,6 +42,8 @@ pub struct Expression(pub ExpressionKind, pub TokenRange); #[derive(Debug, Clone)] pub enum ExpressionKind { VariableName(String), + Borrow(String), + Deref(String), Literal(Literal), Array(Vec), /// Array-indexed, e.g. [] diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index fed01fd..26f374d 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -83,6 +83,18 @@ impl Parse for PrimaryExpression { Kind::StructExpression(stream.parse()?), stream.get_range().unwrap(), ) + } else if let (Some(Token::Et), Some(Token::Identifier(name))) = + (stream.peek(), stream.peek2()) + { + stream.next(); // Consume Et + stream.next(); // Consume identifier + Expression(Kind::Borrow(name), stream.get_range().unwrap()) + } else if let (Some(Token::Star), Some(Token::Identifier(name))) = + (stream.peek(), stream.peek2()) + { + stream.next(); // Consume Et + stream.next(); // Consume identifier + Expression(Kind::Deref(name), stream.get_range().unwrap()) } else if let Some(token) = stream.next() { match &token { Token::Identifier(v) => { @@ -229,7 +241,7 @@ impl Parse for BinaryOperator { (Some(Token::Plus), _) => BinaryOperator::Add, (Some(Token::Minus), _) => BinaryOperator::Minus, - (Some(Token::Times), _) => BinaryOperator::Mult, + (Some(Token::Star), _) => BinaryOperator::Mult, (_, _) => Err(stream.expected_err("expected operator")?)?, }) } diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 41a7f34..2fa3dc4 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -225,6 +225,8 @@ impl ast::Expression { mir::TypeKind::Vague(mir::VagueType::Unknown), name.clone(), ), + ast::ExpressionKind::Borrow(_) => todo!(), + ast::ExpressionKind::Deref(_) => todo!(), }; mir::Expression(kind, self.1.as_meta(module_id)) diff --git a/reid/src/lexer.rs b/reid/src/lexer.rs index 51fc49c..5eaec8f 100644 --- a/reid/src/lexer.rs +++ b/reid/src/lexer.rs @@ -49,7 +49,7 @@ pub enum Token { /// `+` Plus, /// `*` - Times, + Star, /// `-` Minus, @@ -87,7 +87,7 @@ impl Token { match &self { Token::Plus => 10, Token::Minus => 10, - Token::Times => 20, + Token::Star => 20, _ => -1, } } @@ -128,7 +128,7 @@ impl ToString for Token { Token::Equals => String::from('='), Token::Colon => String::from(':'), Token::Plus => String::from('+'), - Token::Times => String::from('*'), + Token::Star => String::from('*'), Token::Minus => String::from('-'), Token::GreaterThan => String::from('>'), Token::LessThan => String::from('<'), @@ -311,7 +311,7 @@ pub fn tokenize>(to_tokenize: T) -> Result, Error ';' => Token::Semi, ':' => Token::Colon, '+' => Token::Plus, - '*' => Token::Times, + '*' => Token::Star, '-' => Token::Minus, '>' => Token::GreaterThan, '<' => Token::LessThan, diff --git a/reid_src/borrow.reid b/reid_src/borrow.reid new file mode 100644 index 0000000..cb52e06 --- /dev/null +++ b/reid_src/borrow.reid @@ -0,0 +1,11 @@ +// Arithmetic, function calls and imports! + + +fn main() -> u32 { + let mut value = 6; + + let mut borrow = &value; + *borrow = 17; + + return borrow; +}