From a253c032d86dcf6a8836f29a0a58632b57d42c2e Mon Sep 17 00:00:00 2001 From: sofia Date: Sun, 27 Jul 2025 02:49:08 +0300 Subject: [PATCH] Add parsing for &self and &mut self --- reid/src/ast/mod.rs | 11 ++++++++++- reid/src/ast/parse.rs | 38 ++++++++++++++++++++++++++++++++++---- reid/src/ast/process.rs | 30 +++++++++++++++++++++++------- reid/src/mir/mod.rs | 6 ++++++ 4 files changed, 73 insertions(+), 12 deletions(-) diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index e2a64e1..00cd535 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -40,6 +40,7 @@ pub enum TypeKind { Custom(String), Borrow(Box, bool), Ptr(Box), + Unknown, } #[derive(Debug, Clone)] @@ -170,12 +171,20 @@ pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub To #[derive(Debug, Clone)] pub struct FunctionSignature { pub name: String, - pub args: Vec<(String, Type)>, + pub self_kind: SelfKind, + pub params: Vec<(String, Type)>, pub return_type: Option, #[allow(dead_code)] pub range: TokenRange, } +#[derive(Debug, Clone)] +pub enum SelfKind { + Borrow(TypeKind), + MutBorrow(TypeKind), + None, +} + #[derive(Debug, Clone, Copy)] pub enum ReturnType { Soft, diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index aecfd6a..15bc698 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -579,18 +579,47 @@ impl Parse for FunctionParam { } } +#[derive(Debug)] +pub struct SelfParam(SelfKind); + +impl Parse for SelfParam { + fn parse(mut stream: TokenStream) -> Result { + stream.expect(Token::Et)?; + let mutable = if let Some(Token::MutKeyword) = stream.peek() { + stream.next(); + true + } else { + false + }; + + let Some(Token::Identifier(name)) = stream.next() else { + return Err(stream.expected_err("parameter name")?); + }; + if name == "self" { + match mutable { + true => Ok(SelfParam(SelfKind::MutBorrow(TypeKind::Unknown))), + false => Ok(SelfParam(SelfKind::Borrow(TypeKind::Unknown))), + } + } else { + Err(stream.expected_err("self parameter")?) + } + } +} + impl Parse for FunctionSignature { fn parse(mut stream: TokenStream) -> Result { if let Some(Token::Identifier(name)) = stream.next() { stream.expect(Token::ParenOpen)?; - let mut args = Vec::new(); + let mut params = Vec::new(); + + let self_kind = stream.parse::().map(|s| s.0).unwrap_or(SelfKind::None); if let Ok(param) = stream.parse::() { - args.push((param.0, param.1)); + params.push((param.0, param.1)); while let Some(Token::Comma) = stream.peek() { stream.next(); let param = stream.parse::()?; - args.push((param.0, param.1)); + params.push((param.0, param.1)); } } @@ -603,7 +632,8 @@ impl Parse for FunctionSignature { Ok(FunctionSignature { name, - args, + params, + self_kind, return_type, range: stream.get_range().unwrap(), }) diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index b9c57b8..353704a 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -32,6 +32,26 @@ impl ast::Module { imports.push(mir::Import(import.0.clone(), import.1.as_meta(module_id))); } FunctionDefinition(ast::FunctionDefinition(signature, is_pub, block, range)) => { + let mut params = Vec::new(); + match &signature.self_kind { + ast::SelfKind::Borrow(type_kind) => params.push(( + "self".to_owned(), + mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), false), + )), + ast::SelfKind::MutBorrow(type_kind) => params.push(( + "self".to_owned(), + mir::TypeKind::Borrow(Box::new(type_kind.into_mir(module_id)), true), + )), + ast::SelfKind::None => {} + } + + params.extend( + signature + .params + .iter() + .cloned() + .map(|p| (p.0, p.1 .0.into_mir(module_id))), + ); let def = mir::FunctionDefinition { name: signature.name.clone(), is_pub: *is_pub, @@ -41,12 +61,7 @@ impl ast::Module { .clone() .map(|r| r.0.into_mir(module_id)) .unwrap_or(mir::TypeKind::Void), - parameters: signature - .args - .iter() - .cloned() - .map(|p| (p.0, p.1 .0.into_mir(module_id))) - .collect(), + parameters: params, kind: mir::FunctionDefinitionKind::Local( block.into_mir(module_id), (*range).as_meta(module_id), @@ -65,7 +80,7 @@ impl ast::Module { .map(|r| r.0.into_mir(module_id)) .unwrap_or(mir::TypeKind::Void), parameters: signature - .args + .params .iter() .cloned() .map(|p| (p.0, p.1 .0.into_mir(module_id))) @@ -475,6 +490,7 @@ impl ast::TypeKind { ast::TypeKind::F128 => mir::TypeKind::F128, ast::TypeKind::F128PPC => mir::TypeKind::F128PPC, ast::TypeKind::Char => mir::TypeKind::Char, + ast::TypeKind::Unknown => mir::TypeKind::Vague(mir::VagueType::Unknown), } } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 52f6eb2..96522eb 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -292,6 +292,12 @@ pub struct FunctionDefinition { pub kind: FunctionDefinitionKind, } +pub enum SelfKind { + Borrow, + MutBorrow, + None, +} + #[derive(Debug)] pub enum FunctionDefinitionKind { /// Actual definition block and surrounding signature range