feat: 添加数组字面量和索引访问功能,更新解析器和解释器以支持数组操作
This commit is contained in:
39
examples/array.ast
Normal file
39
examples/array.ast
Normal file
@@ -0,0 +1,39 @@
|
||||
let arr = [10, 20, 30, 40, 50]
|
||||
print(arr)
|
||||
|
||||
print(arr[0] + arr[4])
|
||||
|
||||
let i = 1
|
||||
while(i < 4) {
|
||||
print(arr[i])
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
arr[0] = 99
|
||||
print(arr)
|
||||
|
||||
for (let j = 0; j < 3; j = j + 1) {
|
||||
arr[j] = arr[j] * 2
|
||||
}
|
||||
print(arr)
|
||||
|
||||
let nested = [[1, 2], [3, 4], [5, 6]]
|
||||
print(nested[1][0])
|
||||
|
||||
let empty = []
|
||||
print(empty)
|
||||
|
||||
let mixed = [1, "hello", true, nil]
|
||||
print(mixed)
|
||||
print(mixed[1][0])
|
||||
|
||||
let same = [1, 2, 3]
|
||||
let other = [1, 2, 3]
|
||||
print(same == other)
|
||||
print(same != [3, 2, 1])
|
||||
|
||||
let item = { name: "sword", price: 100 }
|
||||
let items = [item, { name: "shield", price: 50 }]
|
||||
print(items[0].name)
|
||||
items[1].price = 75
|
||||
print(items[1].price)
|
||||
Reference in New Issue
Block a user