feat: 实现for循环语句的解析和执行逻辑,更新词法分析器以支持for关键字

This commit is contained in:
0264408
2026-02-11 09:39:06 +08:00
parent 0e0077d96b
commit 5dc55c3476
6 changed files with 71 additions and 0 deletions
+16
View File
@@ -64,6 +64,22 @@ impl Interpreter {
}
Ok(None)
}
Stmt::For { initializer, condition, step, body } => {
if let Some(init) = initializer {
self.execute(*init)?;
}
while let Some(cond) = &condition {
let cond_val = self.evaluate(cond.clone())?;
if !self.is_truthy(&cond_val) {
break;
}
self.execute(*body.clone())?;
if let Some(step) = &step {
self.evaluate(step.clone())?;
}
}
Ok(None)
}
Stmt::Function { name, params, body } => {
let func = Value::Function(Rc::new(Function {
params,