diff --git a/examples/macro_easy.reid b/examples/macro_easy.reid new file mode 100644 index 0000000..246ea6c --- /dev/null +++ b/examples/macro_easy.reid @@ -0,0 +1,3 @@ +fn main() -> u32 { + return test_macro!(); +} \ No newline at end of file diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 8a307f0..4fd9c56 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -159,7 +159,12 @@ impl BinaryOperator { } #[derive(Debug, Clone)] -pub struct FunctionCallExpression(pub String, pub Vec, pub TokenRange); +pub struct FunctionCallExpression { + pub name: String, + pub params: Vec, + pub range: TokenRange, + pub is_macro: bool, +} #[derive(Debug, Clone)] pub struct IfExpression( diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index b26460b..6c7c82e 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -537,8 +537,20 @@ impl Parse for BinaryOperator { impl Parse for FunctionCallExpression { fn parse(mut stream: TokenStream) -> Result { 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::()?; - 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::() { - 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)) } diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 6b86dd4..1088cba 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -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, }, ) } diff --git a/reid/src/mir/macros.rs b/reid/src/mir/macros.rs index 754118c..a1648be 100644 --- a/reid/src/mir/macros.rs +++ b/reid/src/mir/macros.rs @@ -16,13 +16,13 @@ pub struct MacroPass { macros: HashMap>, } -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) -> PassResult { + fn expr(&mut self, expr: &mut super::Expression, state: MacroPassState) -> PassResult { Ok(()) } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index fe5287a..12bef16 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -285,6 +285,7 @@ pub struct FunctionCall { pub name: String, pub return_type: TypeKind, pub parameters: Vec, + pub is_macro: bool, pub meta: Metadata, }