feat: 添加数组字面量和索引访问功能,更新解析器和解释器以支持数组操作

This commit is contained in:
0264408
2026-04-28 10:14:13 +08:00
parent 5dc55c3476
commit dd3f18da6f
7 changed files with 177 additions and 0 deletions
+13
View File
@@ -19,6 +19,7 @@ pub enum Value {
Bool(bool),
Nil,
Object(Rc<RefCell<HashMap<String, Value>>>),
Array(Rc<RefCell<Vec<Value>>>),
Function(Rc<Function>),
NativeFunction(NativeFn),
}
@@ -31,6 +32,7 @@ impl std::fmt::Debug for Value {
Value::Bool(b) => write!(f, "Bool({:?})", b),
Value::Nil => write!(f, "Nil"),
Value::Object(_) => write!(f, "Object(...)"),
Value::Array(_) => write!(f, "Array(...)"),
Value::Function(_) => write!(f, "Function(...)"),
Value::NativeFunction(_) => write!(f, "NativeFunction(...)"),
}
@@ -52,6 +54,17 @@ impl fmt::Display for Value {
}
write!(f, "}}")
},
Value::Array(arr) => {
let arr = arr.borrow();
write!(f, "[")?;
for (i, val) in arr.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", val)?;
}
write!(f, "]")
},
Value::Function(_) => write!(f, "<function>"),
Value::NativeFunction(_) => write!(f, "<native function>"),
}