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 #36 from quephird/implement_dictionary
Implement dictionary
- Loading branch information
Showing
19 changed files
with
597 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// LoxDictionary.swift | ||
// slox | ||
// | ||
// Created by Danielle Kefford on 3/25/24. | ||
// | ||
|
||
class LoxDictionary: LoxInstance { | ||
var kvPairs: [LoxValue: LoxValue] | ||
|
||
convenience init(kvPairs: [LoxValue: LoxValue], klass: LoxClass) { | ||
self.init(klass: klass) | ||
self.kvPairs = kvPairs | ||
} | ||
|
||
required init(klass: LoxClass?) { | ||
self.kvPairs = [:] | ||
super.init(klass: klass) | ||
} | ||
|
||
override func get(propertyName: String) throws -> LoxValue { | ||
switch propertyName { | ||
case "count": | ||
return .int(self.kvPairs.count) | ||
default: | ||
return try super.get(propertyName: propertyName) | ||
} | ||
} | ||
|
||
override func set(propertyName: String, propertyValue: LoxValue) throws { | ||
throw RuntimeError.onlyInstancesHaveProperties | ||
} | ||
|
||
subscript(key: LoxValue) -> LoxValue { | ||
get { | ||
return kvPairs[key] ?? .nil | ||
} | ||
set(newValue) { | ||
kvPairs[key] = newValue | ||
} | ||
} | ||
} |
Oops, something went wrong.