Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit 5f0503e

Browse files
committed
增强数组类型匹配和类型检查功能,新增辅助方法以支持更复杂的类型验证
1 parent e55fbb4 commit 5f0503e

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/interpreter/statement_executor.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ impl<'a> StatementExecutor for Interpreter<'a> {
9393
}
9494
},
9595
(Type::FunctionPointer(_, _), Value::None) => true, // 未初始化的函数指针
96+
(Type::Array(expected_element_type), Value::Array(arr)) => {
97+
// 检查数组元素类型是否匹配
98+
if arr.is_empty() {
99+
true // 空数组可以匹配任何数组类型
100+
} else {
101+
// 检查所有元素是否匹配期望的元素类型
102+
arr.iter().all(|element| {
103+
self.value_matches_type(element, expected_element_type)
104+
})
105+
}
106+
},
96107
_ => false
97108
};
98109

@@ -218,6 +229,17 @@ impl<'a> StatementExecutor for Interpreter<'a> {
218229
params_match && return_match
219230
}
220231
},
232+
(Type::Array(expected_element_type), Value::Array(arr)) => {
233+
// 检查数组元素类型是否匹配
234+
if arr.is_empty() {
235+
true // 空数组可以匹配任何数组类型
236+
} else {
237+
// 检查所有元素是否匹配期望的元素类型
238+
arr.iter().all(|element| {
239+
self.value_matches_type(element, expected_element_type)
240+
})
241+
}
242+
},
221243
_ => false
222244
};
223245

@@ -598,4 +620,51 @@ impl<'a> Interpreter<'a> {
598620
_ => false,
599621
}
600622
}
623+
624+
// 辅助方法:检查值是否匹配指定类型
625+
fn value_matches_type(&self, value: &Value, expected_type: &Type) -> bool {
626+
match (expected_type, value) {
627+
(Type::Int, Value::Int(_)) => true,
628+
(Type::Float, Value::Float(_)) => true,
629+
(Type::Bool, Value::Bool(_)) => true,
630+
(Type::String, Value::String(_)) => true,
631+
(Type::Long, Value::Long(_)) => true,
632+
(Type::Array(expected_element_type), Value::Array(arr)) => {
633+
if arr.is_empty() {
634+
true
635+
} else {
636+
arr.iter().all(|element| self.value_matches_type(element, expected_element_type))
637+
}
638+
},
639+
(Type::FunctionPointer(expected_params, expected_return), Value::FunctionPointer(func_ptr)) => {
640+
if func_ptr.param_types.len() != expected_params.len() {
641+
false
642+
} else {
643+
let params_match = func_ptr.param_types.iter()
644+
.zip(expected_params.iter())
645+
.all(|(actual, expected)| actual == expected);
646+
let return_match = &*func_ptr.return_type == expected_return.as_ref();
647+
params_match && return_match
648+
}
649+
},
650+
(Type::FunctionPointer(expected_params, expected_return), Value::LambdaFunctionPointer(lambda_ptr)) => {
651+
if lambda_ptr.param_types.len() != expected_params.len() {
652+
false
653+
} else {
654+
let params_match = lambda_ptr.param_types.iter()
655+
.zip(expected_params.iter())
656+
.all(|(actual, expected)| {
657+
actual == expected ||
658+
*actual == crate::ast::Type::Auto ||
659+
*expected == crate::ast::Type::Auto
660+
});
661+
let return_match = &*lambda_ptr.return_type == expected_return.as_ref() ||
662+
*lambda_ptr.return_type == crate::ast::Type::Auto ||
663+
**expected_return == crate::ast::Type::Auto;
664+
params_match && return_match
665+
}
666+
},
667+
_ => false
668+
}
669+
}
601670
}

0 commit comments

Comments
 (0)