From a7ac974f46c226012777279df4174275ebb147fa Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 28 Jul 2025 22:57:06 +0300 Subject: [PATCH] Change macro pass workflow a little bit --- examples/macro_easy.reid | 4 ++ examples/macro_easy_file.txt | 1 + reid/src/mir/macros.rs | 121 +++++++++++++++++++++++++++++++---- 3 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 examples/macro_easy_file.txt diff --git a/examples/macro_easy.reid b/examples/macro_easy.reid index 246ea6c..6b63281 100644 --- a/examples/macro_easy.reid +++ b/examples/macro_easy.reid @@ -1,3 +1,7 @@ +import std::print; +import std::String; + fn main() -> u32 { + // let message = String::from(include_bytes!("./macro_easy_file.txt")); return test_macro!(); } \ No newline at end of file diff --git a/examples/macro_easy_file.txt b/examples/macro_easy_file.txt new file mode 100644 index 0000000..b6fc4c6 --- /dev/null +++ b/examples/macro_easy_file.txt @@ -0,0 +1 @@ +hello \ No newline at end of file diff --git a/reid/src/mir/macros.rs b/reid/src/mir/macros.rs index 796a537..20399e5 100644 --- a/reid/src/mir/macros.rs +++ b/reid/src/mir/macros.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use crate::mir; +use crate::mir::{self, FunctionCall, IfExpression, WhileStatement}; use super::pass::{Pass, PassResult, PassState}; @@ -31,16 +31,62 @@ impl Pass for MacroPass { type TError = ErrorKind; fn context(&mut self, _context: &mut mir::Context, mut _state: PassState) -> PassResult { - dbg!("hello??"); Ok(()) } - fn expr(&mut self, expr: &mut super::Expression, mut state: MacroPassState) -> PassResult { - dbg!("hello??"); - match &expr.0 { - super::ExprKind::FunctionCall(function_call) => { + fn module(&mut self, module: &mut mir::Module, mut state: PassState) -> PassResult { + for function in &mut module.functions { + match &mut function.kind { + mir::FunctionDefinitionKind::Local(block, _) => block.gen_macros(self, &mut state)?, + _ => {} + }; + } + Ok(()) + } +} + +impl mir::Block { + fn gen_macros(&mut self, data: &MacroPass, state: &mut MacroPassState) -> PassResult { + for statement in &mut self.statements { + statement.gen_macros(data, state)?; + } + if let Some((_, Some(return_expr))) = &mut self.return_expression { + return_expr.gen_macros(data, state)?; + } + Ok(()) + } +} + +impl mir::Statement { + fn gen_macros(&mut self, data: &MacroPass, state: &mut MacroPassState) -> PassResult { + match &mut self.0 { + mir::StmtKind::Let(.., expr) => { + expr.gen_macros(data, state)?; + } + mir::StmtKind::Set(lhs, rhs) => { + lhs.gen_macros(data, state)?; + rhs.gen_macros(data, state)?; + } + mir::StmtKind::Import(_) => {} + mir::StmtKind::Expression(expr) => { + expr.gen_macros(data, state)?; + } + mir::StmtKind::While(WhileStatement { condition, block, .. }) => { + condition.gen_macros(data, state)?; + block.gen_macros(data, state)?; + } + }; + Ok(()) + } +} + +impl mir::Expression { + fn gen_macros(&mut self, data: &MacroPass, state: &mut MacroPassState) -> PassResult { + dbg!("asd?"); + match &mut self.0 { + mir::ExprKind::FunctionCall(function_call) => { if function_call.is_macro { - if let Some(existing_macro) = self.macros.get(&function_call.name) { + if let Some(existing_macro) = data.macros.get(&function_call.name) { let mut literals = Vec::new(); for param in &function_call.parameters { match ¶m.0 { @@ -48,12 +94,12 @@ impl Pass for MacroPass { _ => state.note_errors(&vec![ErrorKind::InvalidMacroArgs], param.1), } } - *expr = state.or_else( + *self = state.or_else( existing_macro .generate(&literals) - .map(|kind| mir::Expression(kind, expr.1)), - expr.clone(), - expr.1, + .map(|kind| mir::Expression(kind, self.1)), + self.clone(), + self.1, ); } else { state.note_errors( @@ -63,7 +109,58 @@ impl Pass for MacroPass { } } } - _ => {} + mir::ExprKind::Variable(_) => {} + mir::ExprKind::Indexed(expression, _, expression1) => { + expression.gen_macros(data, state)?; + expression1.gen_macros(data, state)?; + } + mir::ExprKind::Accessed(expression, ..) => { + expression.gen_macros(data, state)?; + } + mir::ExprKind::Array(expressions) => { + for expression in expressions { + expression.gen_macros(data, state)?; + } + } + mir::ExprKind::Struct(_, items) => { + for item in items { + item.1.gen_macros(data, state)?; + } + } + mir::ExprKind::Literal(_) => {} + mir::ExprKind::BinOp(_, expression, expression1, _) => { + expression.gen_macros(data, state)?; + expression1.gen_macros(data, state)?; + } + mir::ExprKind::AssociatedFunctionCall( + _, + FunctionCall { + parameters, is_macro, .. + }, + ) => { + for expression in parameters { + expression.gen_macros(data, state)?; + } + } + mir::ExprKind::If(IfExpression(cond, lhs, rhs)) => { + cond.gen_macros(data, state)?; + lhs.gen_macros(data, state)?; + if let Some(rhs) = rhs.as_mut() { + rhs.gen_macros(data, state)?; + } + } + mir::ExprKind::Block(block) => { + block.gen_macros(data, state)?; + } + mir::ExprKind::Borrow(expression, _) => { + expression.gen_macros(data, state)?; + } + mir::ExprKind::Deref(expression) => { + expression.gen_macros(data, state)?; + } + mir::ExprKind::CastTo(expression, _) => { + expression.gen_macros(data, state)?; + } } Ok(()) }