Add macro call convention
This commit is contained in:
		
							parent
							
								
									67a5fcd002
								
							
						
					
					
						commit
						33ed1fd813
					
				
							
								
								
									
										3
									
								
								examples/macro_easy.reid
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								examples/macro_easy.reid
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| fn main() -> u32 { | ||||
|     return test_macro!(); | ||||
| } | ||||
| @ -159,7 +159,12 @@ impl BinaryOperator { | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug, Clone)] | ||||
| pub struct FunctionCallExpression(pub String, pub Vec<Expression>, pub TokenRange); | ||||
| pub struct FunctionCallExpression { | ||||
|     pub name: String, | ||||
|     pub params: Vec<Expression>, | ||||
|     pub range: TokenRange, | ||||
|     pub is_macro: bool, | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug, Clone)] | ||||
| pub struct IfExpression( | ||||
|  | ||||
| @ -537,8 +537,20 @@ impl Parse for BinaryOperator { | ||||
| impl Parse for FunctionCallExpression { | ||||
|     fn parse(mut stream: TokenStream) -> Result<Self, Error> { | ||||
|         if let Some(Token::Identifier(name)) = stream.next() { | ||||
|             let is_macro = if let Some(Token::Exclamation) = stream.peek() { | ||||
|                 stream.next(); // Consume !
 | ||||
|                 true | ||||
|             } else { | ||||
|                 false | ||||
|             }; | ||||
| 
 | ||||
|             let args = stream.parse::<FunctionArgs>()?; | ||||
|             Ok(FunctionCallExpression(name, args.0, stream.get_range().unwrap())) | ||||
|             Ok(FunctionCallExpression { | ||||
|                 name, | ||||
|                 params: args.0, | ||||
|                 range: stream.get_range().unwrap(), | ||||
|                 is_macro, | ||||
|             }) | ||||
|         } else { | ||||
|             Err(stream.expected_err("identifier")?) | ||||
|         } | ||||
| @ -894,11 +906,12 @@ impl Parse for DotIndexKind { | ||||
|         stream.expect(Token::Dot)?; | ||||
|         if let Some(Token::Identifier(name)) = stream.next() { | ||||
|             if let Ok(args) = stream.parse::<FunctionArgs>() { | ||||
|                 Ok(Self::FunctionCall(FunctionCallExpression( | ||||
|                 Ok(Self::FunctionCall(FunctionCallExpression { | ||||
|                     name, | ||||
|                     args.0, | ||||
|                     stream.get_range_prev().unwrap(), | ||||
|                 ))) | ||||
|                     params: args.0, | ||||
|                     range: stream.get_range_prev().unwrap(), | ||||
|                     is_macro: false, | ||||
|                 })) | ||||
|             } else { | ||||
|                 Ok(Self::StructValueIndex(name)) | ||||
|             } | ||||
|  | ||||
| @ -340,10 +340,11 @@ impl ast::Expression { | ||||
|                 mir::TypeKind::Vague(mir::VagueType::Unknown), | ||||
|             ), | ||||
|             ast::ExpressionKind::FunctionCall(fn_call_expr) => mir::ExprKind::FunctionCall(mir::FunctionCall { | ||||
|                 name: fn_call_expr.0.clone(), | ||||
|                 name: fn_call_expr.name.clone(), | ||||
|                 return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), | ||||
|                 parameters: fn_call_expr.1.iter().map(|e| e.process(module_id)).collect(), | ||||
|                 meta: fn_call_expr.2.as_meta(module_id), | ||||
|                 parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(), | ||||
|                 meta: fn_call_expr.range.as_meta(module_id), | ||||
|                 is_macro: fn_call_expr.is_macro, | ||||
|             }), | ||||
|             ast::ExpressionKind::BlockExpr(block) => mir::ExprKind::Block(block.into_mir(module_id)), | ||||
|             ast::ExpressionKind::IfExpr(if_expression) => { | ||||
| @ -427,14 +428,15 @@ impl ast::Expression { | ||||
|             ast::ExpressionKind::AssociatedFunctionCall(ty, fn_call_expr) => mir::ExprKind::AssociatedFunctionCall( | ||||
|                 ty.0.into_mir(module_id), | ||||
|                 mir::FunctionCall { | ||||
|                     name: fn_call_expr.0.clone(), | ||||
|                     name: fn_call_expr.name.clone(), | ||||
|                     return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), | ||||
|                     parameters: fn_call_expr.1.iter().map(|e| e.process(module_id)).collect(), | ||||
|                     meta: fn_call_expr.2.as_meta(module_id), | ||||
|                     parameters: fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(), | ||||
|                     meta: fn_call_expr.range.as_meta(module_id), | ||||
|                     is_macro: fn_call_expr.is_macro, | ||||
|                 }, | ||||
|             ), | ||||
|             ast::ExpressionKind::AccessCall(expression, fn_call_expr) => { | ||||
|                 let mut params: Vec<_> = fn_call_expr.1.iter().map(|e| e.process(module_id)).collect(); | ||||
|                 let mut params: Vec<_> = fn_call_expr.params.iter().map(|e| e.process(module_id)).collect(); | ||||
|                 params.insert( | ||||
|                     0, | ||||
|                     mir::Expression( | ||||
| @ -442,13 +444,18 @@ impl ast::Expression { | ||||
|                         expression.1.as_meta(module_id), | ||||
|                     ), | ||||
|                 ); | ||||
|                 if fn_call_expr.is_macro { | ||||
|                     panic!("Macros aren't supported as access-calls!"); | ||||
|                 }; | ||||
| 
 | ||||
|                 mir::ExprKind::AssociatedFunctionCall( | ||||
|                     mir::TypeKind::Vague(mir::VagueType::Unknown), | ||||
|                     mir::FunctionCall { | ||||
|                         name: fn_call_expr.0.clone(), | ||||
|                         name: fn_call_expr.name.clone(), | ||||
|                         return_type: mir::TypeKind::Vague(mir::VagueType::Unknown), | ||||
|                         parameters: params, | ||||
|                         meta: fn_call_expr.2.as_meta(module_id), | ||||
|                         meta: fn_call_expr.range.as_meta(module_id), | ||||
|                         is_macro: fn_call_expr.is_macro, | ||||
|                     }, | ||||
|                 ) | ||||
|             } | ||||
|  | ||||
| @ -16,13 +16,13 @@ pub struct MacroPass { | ||||
|     macros: HashMap<String, Box<dyn MacroFunction>>, | ||||
| } | ||||
| 
 | ||||
| type LinkerPassState<'st, 'sc> = PassState<'st, 'sc, (), ErrorKind>; | ||||
| type MacroPassState<'st, 'sc> = PassState<'st, 'sc, (), ErrorKind>; | ||||
| 
 | ||||
| impl Pass for MacroPass { | ||||
|     type Data = (); | ||||
|     type TError = ErrorKind; | ||||
| 
 | ||||
|     fn expr(&mut self, expr: &mut super::Expression, state: PassState<Self::Data, Self::TError>) -> PassResult { | ||||
|     fn expr(&mut self, expr: &mut super::Expression, state: MacroPassState) -> PassResult { | ||||
|         Ok(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -285,6 +285,7 @@ pub struct FunctionCall { | ||||
|     pub name: String, | ||||
|     pub return_type: TypeKind, | ||||
|     pub parameters: Vec<Expression>, | ||||
|     pub is_macro: bool, | ||||
|     pub meta: Metadata, | ||||
| } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user