feat: 实现for循环语句的解析和执行逻辑,更新词法分析器以支持for关键字
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user