Parse deref and borrow

This commit is contained in:
Sofia 2025-07-20 21:25:08 +03:00
parent 8bb337dbca
commit ba8ee770fb
5 changed files with 32 additions and 5 deletions

View File

@ -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<Expression>),
/// Array-indexed, e.g. <expr>[<expr>]

View File

@ -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")?)?,
})
}

View File

@ -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))

View File

@ -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<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
';' => Token::Semi,
':' => Token::Colon,
'+' => Token::Plus,
'*' => Token::Times,
'*' => Token::Star,
'-' => Token::Minus,
'>' => Token::GreaterThan,
'<' => Token::LessThan,

11
reid_src/borrow.reid Normal file
View File

@ -0,0 +1,11 @@
// Arithmetic, function calls and imports!
fn main() -> u32 {
let mut value = 6;
let mut borrow = &value;
*borrow = 17;
return borrow;
}