Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from quephird/implement_lists
Implement lists
- Loading branch information
Showing
19 changed files
with
636 additions
and
954 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 .number(Double(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 | ||
} | ||
} | ||
} |
Oops, something went wrong.