|
1 | 1 | use crate::environment::environment::Environment; |
2 | | -use crate::ir::ast::{Expression, Name, Statement, Type}; |
| 2 | +use crate::ir::ast::{Expression, Name, Statement, Type, Function, FormalArgument}; |
3 | 3 |
|
4 | 4 | type ErrorMessage = String; |
5 | 5 |
|
@@ -114,11 +114,12 @@ pub fn check_stmt( |
114 | 114 | Statement::FuncDef(function) => { |
115 | 115 | let mut new_env = env.clone(); |
116 | 116 | new_env.push(); |
117 | | - if let Some(params) = function.params.clone() { |
118 | | - for (param_name, param_type) in params { |
119 | | - new_env.map_variable(param_name, param_type) |
120 | | - } |
| 117 | + |
| 118 | + // Since params is now a Vec<FormalArgument>, we can iterate directly |
| 119 | + for formal_arg in function.params.iter() { |
| 120 | + new_env.map_variable(formal_arg.argumentName.clone(), formal_arg.argumentType.clone()); |
121 | 121 | } |
| 122 | + |
122 | 123 | if let Some(body) = function.body.clone() { |
123 | 124 | new_env = check_stmt(*body, &new_env)?; |
124 | 125 | } |
@@ -207,55 +208,6 @@ pub fn check_stmt( |
207 | 208 | // } |
208 | 209 | // } |
209 | 210 |
|
210 | | -// fn check_func_call( |
211 | | -// name: String, |
212 | | -// args: Vec<Expression>, |
213 | | -// env: &Environment<Type>, |
214 | | -// ) -> Result<Type, ErrorMessage> { |
215 | | -// match check_var_name(name.clone(), env, false) { |
216 | | -// Ok(Type::TFunction(kind, type_vec)) => { |
217 | | -// if args.len() != type_vec.len() { |
218 | | -// return Err(format!( |
219 | | -// "[Type Error on '{}()'] '{}()' expected {} arguments, found {}.", |
220 | | -// env.scope_name(), |
221 | | -// name, |
222 | | -// type_vec.len(), |
223 | | -// args.len() |
224 | | -// )); |
225 | | -// } |
226 | | - |
227 | | -// for (arg, param_type) in args.iter().zip(type_vec) { |
228 | | -// let arg_type = check_exp(arg.clone(), env)?; |
229 | | -// if arg_type != param_type { |
230 | | -// return Err(format!("[Type Error on '{}()'] '{}()' has mismatched arguments: expected '{:?}', found '{:?}'.", env.scope_name(), name, param_type, arg_type)); |
231 | | -// } |
232 | | -// } |
233 | | - |
234 | | -// Ok(kind.unwrap()) |
235 | | -// } |
236 | | -// _ => Err(format!( |
237 | | -// "[Name Error on '{}()'] '{}()' is not defined.", |
238 | | -// env.scope_name(), |
239 | | -// name |
240 | | -// )), |
241 | | -// } |
242 | | -// } |
243 | | - |
244 | | -// fn check_duplicate_params(params: &Vec<(Name, Type)>) -> Result<(), ErrorMessage> { |
245 | | -// let mut seen_params = std::collections::HashSet::new(); |
246 | | - |
247 | | -// for (name, _) in params { |
248 | | -// if !seen_params.insert(name.clone()) { |
249 | | -// return Err(format!( |
250 | | -// "[Parameter Error] Duplicate parameter name '{}'", |
251 | | -// name |
252 | | -// )); |
253 | | -// } |
254 | | -// } |
255 | | - |
256 | | -// Ok(()) |
257 | | -// } |
258 | | - |
259 | 211 | fn check_var_name(name: Name, env: &Environment<Type>, scoped: bool) -> Result<Type, ErrorMessage> { |
260 | 212 | let var_type = env.lookup(&name); |
261 | 213 | match var_type { |
@@ -773,10 +725,10 @@ mod tests { |
773 | 725 | let func = FuncDef(Function { |
774 | 726 | name: "add".to_string(), |
775 | 727 | kind: Type::TInteger, |
776 | | - params: Some(vec![ |
777 | | - ("a".to_string(), TInteger), |
778 | | - ("b".to_string(), TInteger), |
779 | | - ]), |
| 728 | + params: vec![ |
| 729 | + FormalArgument::new("a".to_string(), Type::TInteger), |
| 730 | + FormalArgument::new("b".to_string(), Type::TInteger), |
| 731 | + ], |
780 | 732 | body: Some(Box::new(Return(Box::new(Add( |
781 | 733 | Box::new(Var("a".to_string())), |
782 | 734 | Box::new(Var("b".to_string())), |
@@ -916,14 +868,14 @@ mod tests { |
916 | 868 | let global_func = Function { |
917 | 869 | name: "global".to_string(), |
918 | 870 | kind: Type::TVoid, |
919 | | - params: None, |
| 871 | + params: Vec::new(), |
920 | 872 | body: None, |
921 | 873 | }; |
922 | 874 |
|
923 | 875 | let local_func = Function { |
924 | 876 | name: "local".to_string(), |
925 | 877 | kind: Type::TVoid, |
926 | | - params: None, |
| 878 | + params: Vec::new(), |
927 | 879 | body: None, |
928 | 880 | }; |
929 | 881 |
|
|
0 commit comments