Change macro pass workflow a little bit
This commit is contained in:
		
							parent
							
								
									3d8f4bbd24
								
							
						
					
					
						commit
						a7ac974f46
					
				| @ -1,3 +1,7 @@ | |||||||
|  | import std::print; | ||||||
|  | import std::String; | ||||||
|  | 
 | ||||||
| fn main() -> u32 { | fn main() -> u32 { | ||||||
|  |     // let message = String::from(include_bytes!("./macro_easy_file.txt")); | ||||||
|     return test_macro!(); |     return test_macro!(); | ||||||
| } | } | ||||||
							
								
								
									
										1
									
								
								examples/macro_easy_file.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								examples/macro_easy_file.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | hello | ||||||
| @ -1,6 +1,6 @@ | |||||||
| use std::collections::HashMap; | use std::collections::HashMap; | ||||||
| 
 | 
 | ||||||
| use crate::mir; | use crate::mir::{self, FunctionCall, IfExpression, WhileStatement}; | ||||||
| 
 | 
 | ||||||
| use super::pass::{Pass, PassResult, PassState}; | use super::pass::{Pass, PassResult, PassState}; | ||||||
| 
 | 
 | ||||||
| @ -31,16 +31,62 @@ impl Pass for MacroPass { | |||||||
|     type TError = ErrorKind; |     type TError = ErrorKind; | ||||||
| 
 | 
 | ||||||
|     fn context(&mut self, _context: &mut mir::Context, mut _state: PassState<Self::Data, Self::TError>) -> PassResult { |     fn context(&mut self, _context: &mut mir::Context, mut _state: PassState<Self::Data, Self::TError>) -> PassResult { | ||||||
|         dbg!("hello??"); |  | ||||||
|         Ok(()) |         Ok(()) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fn expr(&mut self, expr: &mut super::Expression, mut state: MacroPassState) -> PassResult { |     fn module(&mut self, module: &mut mir::Module, mut state: PassState<Self::Data, Self::TError>) -> PassResult { | ||||||
|         dbg!("hello??"); |         for function in &mut module.functions { | ||||||
|         match &expr.0 { |             match &mut function.kind { | ||||||
|             super::ExprKind::FunctionCall(function_call) => { |                 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 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(); |                         let mut literals = Vec::new(); | ||||||
|                         for param in &function_call.parameters { |                         for param in &function_call.parameters { | ||||||
|                             match ¶m.0 { |                             match ¶m.0 { | ||||||
| @ -48,12 +94,12 @@ impl Pass for MacroPass { | |||||||
|                                 _ => state.note_errors(&vec![ErrorKind::InvalidMacroArgs], param.1), |                                 _ => state.note_errors(&vec![ErrorKind::InvalidMacroArgs], param.1), | ||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|                         *expr = state.or_else( |                         *self = state.or_else( | ||||||
|                             existing_macro |                             existing_macro | ||||||
|                                 .generate(&literals) |                                 .generate(&literals) | ||||||
|                                 .map(|kind| mir::Expression(kind, expr.1)), |                                 .map(|kind| mir::Expression(kind, self.1)), | ||||||
|                             expr.clone(), |                             self.clone(), | ||||||
|                             expr.1, |                             self.1, | ||||||
|                         ); |                         ); | ||||||
|                     } else { |                     } else { | ||||||
|                         state.note_errors( |                         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(()) |         Ok(()) | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user