Add parsing for &self and &mut self
This commit is contained in:
		
							parent
							
								
									bee31f4b92
								
							
						
					
					
						commit
						a253c032d8
					
				| @ -40,6 +40,7 @@ pub enum TypeKind { | |||||||
|     Custom(String), |     Custom(String), | ||||||
|     Borrow(Box<TypeKind>, bool), |     Borrow(Box<TypeKind>, bool), | ||||||
|     Ptr(Box<TypeKind>), |     Ptr(Box<TypeKind>), | ||||||
|  |     Unknown, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone)] | #[derive(Debug, Clone)] | ||||||
| @ -170,12 +171,20 @@ pub struct FunctionDefinition(pub FunctionSignature, pub bool, pub Block, pub To | |||||||
| #[derive(Debug, Clone)] | #[derive(Debug, Clone)] | ||||||
| pub struct FunctionSignature { | pub struct FunctionSignature { | ||||||
|     pub name: String, |     pub name: String, | ||||||
|     pub args: Vec<(String, Type)>, |     pub self_kind: SelfKind, | ||||||
|  |     pub params: Vec<(String, Type)>, | ||||||
|     pub return_type: Option<Type>, |     pub return_type: Option<Type>, | ||||||
|     #[allow(dead_code)] |     #[allow(dead_code)] | ||||||
|     pub range: TokenRange, |     pub range: TokenRange, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #[derive(Debug, Clone)] | ||||||
|  | pub enum SelfKind { | ||||||
|  |     Borrow(TypeKind), | ||||||
|  |     MutBorrow(TypeKind), | ||||||
|  |     None, | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[derive(Debug, Clone, Copy)] | #[derive(Debug, Clone, Copy)] | ||||||
| pub enum ReturnType { | pub enum ReturnType { | ||||||
|     Soft, |     Soft, | ||||||
|  | |||||||
| @ -579,18 +579,47 @@ impl Parse for FunctionParam { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #[derive(Debug)] | ||||||
|  | pub struct SelfParam(SelfKind); | ||||||
|  | 
 | ||||||
|  | impl Parse for SelfParam { | ||||||
|  |     fn parse(mut stream: TokenStream) -> Result<Self, Error> { | ||||||
|  |         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 { | impl Parse for FunctionSignature { | ||||||
|     fn parse(mut stream: TokenStream) -> Result<Self, Error> { |     fn parse(mut stream: TokenStream) -> Result<Self, Error> { | ||||||
|         if let Some(Token::Identifier(name)) = stream.next() { |         if let Some(Token::Identifier(name)) = stream.next() { | ||||||
|             stream.expect(Token::ParenOpen)?; |             stream.expect(Token::ParenOpen)?; | ||||||
|             let mut args = Vec::new(); |             let mut params = Vec::new(); | ||||||
|  | 
 | ||||||
|  |             let self_kind = stream.parse::<SelfParam>().map(|s| s.0).unwrap_or(SelfKind::None); | ||||||
| 
 | 
 | ||||||
|             if let Ok(param) = stream.parse::<FunctionParam>() { |             if let Ok(param) = stream.parse::<FunctionParam>() { | ||||||
|                 args.push((param.0, param.1)); |                 params.push((param.0, param.1)); | ||||||
|                 while let Some(Token::Comma) = stream.peek() { |                 while let Some(Token::Comma) = stream.peek() { | ||||||
|                     stream.next(); |                     stream.next(); | ||||||
|                     let param = stream.parse::<FunctionParam>()?; |                     let param = stream.parse::<FunctionParam>()?; | ||||||
|                     args.push((param.0, param.1)); |                     params.push((param.0, param.1)); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
| @ -603,7 +632,8 @@ impl Parse for FunctionSignature { | |||||||
| 
 | 
 | ||||||
|             Ok(FunctionSignature { |             Ok(FunctionSignature { | ||||||
|                 name, |                 name, | ||||||
|                 args, |                 params, | ||||||
|  |                 self_kind, | ||||||
|                 return_type, |                 return_type, | ||||||
|                 range: stream.get_range().unwrap(), |                 range: stream.get_range().unwrap(), | ||||||
|             }) |             }) | ||||||
|  | |||||||
| @ -32,6 +32,26 @@ impl ast::Module { | |||||||
|                     imports.push(mir::Import(import.0.clone(), import.1.as_meta(module_id))); |                     imports.push(mir::Import(import.0.clone(), import.1.as_meta(module_id))); | ||||||
|                 } |                 } | ||||||
|                 FunctionDefinition(ast::FunctionDefinition(signature, is_pub, block, range)) => { |                 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 { |                     let def = mir::FunctionDefinition { | ||||||
|                         name: signature.name.clone(), |                         name: signature.name.clone(), | ||||||
|                         is_pub: *is_pub, |                         is_pub: *is_pub, | ||||||
| @ -41,12 +61,7 @@ impl ast::Module { | |||||||
|                             .clone() |                             .clone() | ||||||
|                             .map(|r| r.0.into_mir(module_id)) |                             .map(|r| r.0.into_mir(module_id)) | ||||||
|                             .unwrap_or(mir::TypeKind::Void), |                             .unwrap_or(mir::TypeKind::Void), | ||||||
|                         parameters: signature |                         parameters: params, | ||||||
|                             .args |  | ||||||
|                             .iter() |  | ||||||
|                             .cloned() |  | ||||||
|                             .map(|p| (p.0, p.1 .0.into_mir(module_id))) |  | ||||||
|                             .collect(), |  | ||||||
|                         kind: mir::FunctionDefinitionKind::Local( |                         kind: mir::FunctionDefinitionKind::Local( | ||||||
|                             block.into_mir(module_id), |                             block.into_mir(module_id), | ||||||
|                             (*range).as_meta(module_id), |                             (*range).as_meta(module_id), | ||||||
| @ -65,7 +80,7 @@ impl ast::Module { | |||||||
|                             .map(|r| r.0.into_mir(module_id)) |                             .map(|r| r.0.into_mir(module_id)) | ||||||
|                             .unwrap_or(mir::TypeKind::Void), |                             .unwrap_or(mir::TypeKind::Void), | ||||||
|                         parameters: signature |                         parameters: signature | ||||||
|                             .args |                             .params | ||||||
|                             .iter() |                             .iter() | ||||||
|                             .cloned() |                             .cloned() | ||||||
|                             .map(|p| (p.0, p.1 .0.into_mir(module_id))) |                             .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::F128 => mir::TypeKind::F128, | ||||||
|             ast::TypeKind::F128PPC => mir::TypeKind::F128PPC, |             ast::TypeKind::F128PPC => mir::TypeKind::F128PPC, | ||||||
|             ast::TypeKind::Char => mir::TypeKind::Char, |             ast::TypeKind::Char => mir::TypeKind::Char, | ||||||
|  |             ast::TypeKind::Unknown => mir::TypeKind::Vague(mir::VagueType::Unknown), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -292,6 +292,12 @@ pub struct FunctionDefinition { | |||||||
|     pub kind: FunctionDefinitionKind, |     pub kind: FunctionDefinitionKind, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | pub enum SelfKind { | ||||||
|  |     Borrow, | ||||||
|  |     MutBorrow, | ||||||
|  |     None, | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub enum FunctionDefinitionKind { | pub enum FunctionDefinitionKind { | ||||||
|     /// Actual definition block and surrounding signature range
 |     /// Actual definition block and surrounding signature range
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user