@@ -2,7 +2,7 @@ use std::collections::HashSet;
22
33use crate :: environment:: environment:: Environment ;
44use crate :: ir:: ast:: {
5- Expression , FormalArgument , FuncSignature , Function , Name , Statement , Type , ValueConstructor
5+ Expression , FormalArgument , FuncSignature , Function , Name , Statement , Type , ValueConstructor ,
66} ;
77use crate :: type_checker:: expression_type_checker:: check_expr;
88
@@ -58,25 +58,35 @@ fn check_assignment_stmt(
5858 env : & Environment < Type > ,
5959) -> Result < Environment < Type > , ErrorMessage > {
6060 let mut new_env = env. clone ( ) ;
61- let exp_type = check_expr ( * exp, & new_env) ?;
61+ let exp_type = check_expr ( * exp. clone ( ) , & new_env) ?;
6262
63- match new_env. lookup ( & name) {
64- Some ( ( mutable, var_type) ) => {
65- if !mutable {
66- Err ( format ! ( "[Type Error] cannot reassign '{:?}' variable, since it was declared as a constant value." , name) )
67- } else if var_type == Type :: TAny {
68- new_env. map_variable ( name. clone ( ) , true , exp_type) ;
69- Ok ( new_env)
70- } else if var_type == exp_type {
71- Ok ( new_env)
72- } else {
73- Err ( format ! (
74- "[Type Error] expected '{:?}', found '{:?}'." ,
75- var_type, exp_type
76- ) )
77- }
63+ match * exp {
64+ Expression :: Lambda ( mut func) => {
65+ func. name = name;
66+ new_env = check_func_def_stmt ( func, env) ?;
67+ Ok ( new_env)
7868 }
79- None => Err ( format ! ( "[Type Error] variable '{:?}' not declared." , name) ) ,
69+ _ => match new_env. lookup ( & name) {
70+ Some ( ( mutable, var_type) ) => {
71+ if !mutable {
72+ Err ( format ! (
73+ "[Type Error] cannot reassign '{:?}' variable, since it was declared as a constant value." ,
74+ name
75+ ) )
76+ } else if var_type == Type :: TAny {
77+ new_env. map_variable ( name. clone ( ) , true , exp_type) ;
78+ Ok ( new_env)
79+ } else if var_type == exp_type {
80+ Ok ( new_env)
81+ } else {
82+ Err ( format ! (
83+ "[Type Error] expected '{:?}', found '{:?}'." ,
84+ var_type, exp_type
85+ ) )
86+ }
87+ }
88+ None => Err ( format ! ( "[Type Error] variable '{:?}' not declared." , name) ) ,
89+ } ,
8090 }
8191}
8292
@@ -205,9 +215,9 @@ fn check_func_def_stmt(
205215 // Previous environment functions and the formal parameters are regarded as global
206216 new_env. set_global_functions ( env. get_all_functions ( ) ) ;
207217
208- // Ensure that each function is defined only once
209- if new_env
210- . globals
218+ // Ensure that each function is defined only once in current scope
219+ let current_scope = env . get_current_scope ( ) ;
220+ if current_scope
211221 . functions
212222 . contains_key ( & FuncSignature :: from_func ( & function) )
213223 {
@@ -230,21 +240,29 @@ fn check_func_def_stmt(
230240
231241 for formal_arg in function. params . iter ( ) {
232242 match formal_arg. argument_type . clone ( ) {
233- Type :: TFunction ( arg_func_ret_type, arg_func_params_type ) => {
243+ Type :: TFunction ( arg_func_ret_type, arg_func_params_type ) => {
234244 let mut params: Vec < FormalArgument > = Vec :: new ( ) ;
235- let mut count: u64 = 0 ;
236- for arg_type in & arg_func_params_type
237- {
238- params. push ( FormalArgument { argument_name : count. to_string ( ) , argument_type : arg_type. clone ( ) } ) ;
245+ let mut count: u64 = 0 ;
246+ for arg_type in & arg_func_params_type {
247+ params. push ( FormalArgument {
248+ argument_name : count. to_string ( ) ,
249+ argument_type : arg_type. clone ( ) ,
250+ } ) ;
239251 count += 1 ;
240252 }
241- new_env. map_function ( Function { name : formal_arg. argument_name . clone ( ) , kind : * arg_func_ret_type, params : params, body : None } ) ;
253+ new_env. map_function ( Function {
254+ name : formal_arg. argument_name . clone ( ) ,
255+ kind : * arg_func_ret_type,
256+ params : params,
257+ body : None ,
258+ } ) ;
242259 }
243- _ =>
244- {
245- new_env. map_variable ( formal_arg. argument_name . clone ( ) ,
246- false ,
247- formal_arg. argument_type . clone ( ) ) ;
260+ _ => {
261+ new_env. map_variable (
262+ formal_arg. argument_name . clone ( ) ,
263+ false ,
264+ formal_arg. argument_type . clone ( ) ,
265+ ) ;
248266 }
249267 }
250268 }
0 commit comments