Improve associated functions so you can now call them on numbers too
This commit is contained in:
		
							parent
							
								
									a51a2c8f56
								
							
						
					
					
						commit
						c7f1b81c9d
					
				| @ -358,6 +358,7 @@ pub fn analyze_expr( | |||||||
|                     }) |                     }) | ||||||
|                     .collect::<Vec<_>>(), |                     .collect::<Vec<_>>(), | ||||||
|             ); |             ); | ||||||
|  |             set_autocomplete(map, meta.range.start, function_autocomplete.clone()); | ||||||
|             set_autocomplete(map, meta.range.end, function_autocomplete.clone()); |             set_autocomplete(map, meta.range.end, function_autocomplete.clone()); | ||||||
|         } |         } | ||||||
|         mir::ExprKind::If(IfExpression(cond, then_e, else_e)) => { |         mir::ExprKind::If(IfExpression(cond, then_e, else_e)) => { | ||||||
|  | |||||||
| @ -175,6 +175,20 @@ impl Parse for AssociatedFunctionCall { | |||||||
|         let ty = stream.parse()?; |         let ty = stream.parse()?; | ||||||
|         stream.expect(Token::Colon)?; |         stream.expect(Token::Colon)?; | ||||||
|         stream.expect(Token::Colon)?; |         stream.expect(Token::Colon)?; | ||||||
|  | 
 | ||||||
|  |         if stream.next_is_whitespace() { | ||||||
|  |             stream.expecting_err_nonfatal("associated function name"); | ||||||
|  |             return Ok(AssociatedFunctionCall( | ||||||
|  |                 ty, | ||||||
|  |                 FunctionCallExpression { | ||||||
|  |                     name: String::new(), | ||||||
|  |                     params: Vec::new(), | ||||||
|  |                     range: stream.get_range_prev_curr().unwrap(), | ||||||
|  |                     is_macro: false, | ||||||
|  |                 }, | ||||||
|  |             )); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         match stream.parse() { |         match stream.parse() { | ||||||
|             Ok(fn_call) => Ok(AssociatedFunctionCall(ty, fn_call)), |             Ok(fn_call) => Ok(AssociatedFunctionCall(ty, fn_call)), | ||||||
|             _ => { |             _ => { | ||||||
| @ -187,7 +201,7 @@ impl Parse for AssociatedFunctionCall { | |||||||
|                         FunctionCallExpression { |                         FunctionCallExpression { | ||||||
|                             name: fn_name, |                             name: fn_name, | ||||||
|                             params: Vec::new(), |                             params: Vec::new(), | ||||||
|                             range: stream.get_range_prev_single().unwrap(), |                             range: stream.get_range_prev_curr().unwrap(), | ||||||
|                             is_macro: false, |                             is_macro: false, | ||||||
|                         }, |                         }, | ||||||
|                     )) |                     )) | ||||||
| @ -198,7 +212,7 @@ impl Parse for AssociatedFunctionCall { | |||||||
|                         FunctionCallExpression { |                         FunctionCallExpression { | ||||||
|                             name: String::new(), |                             name: String::new(), | ||||||
|                             params: Vec::new(), |                             params: Vec::new(), | ||||||
|                             range: stream.get_range_prev_single().unwrap(), |                             range: stream.get_range_prev_curr().unwrap(), | ||||||
|                             is_macro: false, |                             is_macro: false, | ||||||
|                         }, |                         }, | ||||||
|                     )) |                     )) | ||||||
| @ -660,11 +674,11 @@ impl Parse for ImportStatement { | |||||||
|         let mut import_list = Vec::new(); |         let mut import_list = Vec::new(); | ||||||
| 
 | 
 | ||||||
|         if let Some(Token::Identifier(name)) = stream.next() { |         if let Some(Token::Identifier(name)) = stream.next() { | ||||||
|             import_list.push((name, stream.get_range_prev_single().unwrap())); |             import_list.push((name, stream.get_range_prev_curr().unwrap())); | ||||||
|             while stream.expect(Token::Colon).is_ok() && stream.expect(Token::Colon).is_ok() { |             while stream.expect(Token::Colon).is_ok() && stream.expect(Token::Colon).is_ok() { | ||||||
|                 if let Some(Token::Identifier(name)) = stream.peek() { |                 if let Some(Token::Identifier(name)) = stream.peek() { | ||||||
|                     stream.next(); // Consume identifier
 |                     stream.next(); // Consume identifier
 | ||||||
|                     import_list.push((name, stream.get_range_prev_single().unwrap())); |                     import_list.push((name, stream.get_range_prev_curr().unwrap())); | ||||||
|                 } else { |                 } else { | ||||||
|                     stream.expected_err_nonfatal("identifier"); |                     stream.expected_err_nonfatal("identifier"); | ||||||
|                     break; |                     break; | ||||||
| @ -954,7 +968,7 @@ impl Parse for DotIndexKind { | |||||||
|                 stream.expecting_err_nonfatal("struct index"); |                 stream.expecting_err_nonfatal("struct index"); | ||||||
|                 Ok(Self::StructValueIndex( |                 Ok(Self::StructValueIndex( | ||||||
|                     String::new(), |                     String::new(), | ||||||
|                     stream.get_range_prev_single().unwrap(), |                     stream.get_range_prev_curr().unwrap(), | ||||||
|                 )) |                 )) | ||||||
|             } else { |             } else { | ||||||
|                 Err(stream.expecting_err("struct index")?) |                 Err(stream.expecting_err("struct index")?) | ||||||
|  | |||||||
| @ -216,8 +216,8 @@ impl<'a, 'b> TokenStream<'a, 'b> { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Gets range of the previous token only.
 |     /// Gets range of the previous token only.
 | ||||||
|     pub fn get_range_prev_single(&self) -> Option<TokenRange> { |     pub fn get_range_prev_curr(&self) -> Option<TokenRange> { | ||||||
|         self.ref_position.as_ref().map(|ref_pos| TokenRange { |         Some(TokenRange { | ||||||
|             start: self.previous_token(self.position).0, |             start: self.previous_token(self.position).0, | ||||||
|             end: self.previous_token(self.position).0, |             end: self.previous_token(self.position).0, | ||||||
|         }) |         }) | ||||||
|  | |||||||
| @ -721,6 +721,7 @@ impl Expression { | |||||||
|                 expr.resolve_ref(typerefs).cast_into(type_kind) |                 expr.resolve_ref(typerefs).cast_into(type_kind) | ||||||
|             } |             } | ||||||
|             ExprKind::AssociatedFunctionCall(type_kind, function_call) => { |             ExprKind::AssociatedFunctionCall(type_kind, function_call) => { | ||||||
|  |                 *type_kind = type_kind.or_default().unwrap(); | ||||||
|                 let true_function = state |                 let true_function = state | ||||||
|                     .scope |                     .scope | ||||||
|                     .get_associated_function(&pass::AssociatedFunctionKey( |                     .get_associated_function(&pass::AssociatedFunctionKey( | ||||||
| @ -732,7 +733,7 @@ impl Expression { | |||||||
|                         type_kind.clone(), |                         type_kind.clone(), | ||||||
|                     )); |                     )); | ||||||
| 
 | 
 | ||||||
|                 if let Some(f) = state.ok(true_function, self.1) { |                 if let Some(f) = state.ok(true_function, function_call.meta) { | ||||||
|                     let param_len_given = function_call.parameters.len(); |                     let param_len_given = function_call.parameters.len(); | ||||||
|                     let param_len_expected = f.params.len(); |                     let param_len_expected = f.params.len(); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -636,10 +636,12 @@ impl Expression { | |||||||
|                                 } |                                 } | ||||||
|                             } |                             } | ||||||
|                         } else { |                         } else { | ||||||
|                             let ExprKind::Borrow(val, _) = &first_param.0 else { |                             if let ExprKind::Borrow(val, _) = &first_param.0 { | ||||||
|                                 panic!() |                                 *first_param = *val.clone(); | ||||||
|                             }; |                             } | ||||||
|                             *first_param = *val.clone(); |                             if let TypeKind::Borrow(inner_ty, _) = type_kind { | ||||||
|  |                                 *type_kind = *inner_ty.clone(); | ||||||
|  |                             } | ||||||
|                         } |                         } | ||||||
| 
 | 
 | ||||||
|                         if !is_mutable { |                         if !is_mutable { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user