/
LoxList.swift
45 lines (39 loc) · 1.04 KB
/
LoxList.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// LoxList.swift
// slox
//
// Created by Danielle Kefford on 3/12/24.
//
class LoxList: LoxInstance {
var elements: [LoxValue]
var count: Int {
return elements.count
}
init(elements: [LoxValue], klass: LoxClass) {
self.elements = elements
super.init(klass: klass)
}
override func get(propertyName: String) throws -> LoxValue {
switch propertyName {
case "count":
return .int(elements.count)
default:
return try super.get(propertyName: propertyName)
}
}
override func set(propertyName: String, propertyValue: LoxValue) throws {
throw RuntimeError.onlyInstancesHaveProperties
}
static func == (lhs: LoxList, rhs: LoxList) -> Bool {
return lhs.elements == rhs.elements
}
// TODO: Need to think about how to handle invalid indices!!!
subscript(index: Int) -> LoxValue {
get {
return elements[index]
}
set(newValue) {
elements[index] = newValue
}
}
}