Skip to content

Commit

Permalink
Added keys and values properties to Dictionary.
Browse files Browse the repository at this point in the history
  • Loading branch information
quephird committed Mar 31, 2024
1 parent d78e012 commit 362af7e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions slox.xcodeproj/project.pbxproj
Expand Up @@ -38,6 +38,7 @@
873CCB342B8EE0FF00FC249A /* Environment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 873CCB322B8ED8B900FC249A /* Environment.swift */; };
8755B8B42B91983F00530DC4 /* UserDefinedFunction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87BAFC4A2B918C520013E5FE /* UserDefinedFunction.swift */; };
8755B8B52B91984C00530DC4 /* LoxCallable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87BAFC482B9179CB0013E5FE /* LoxCallable.swift */; };
8764AB632BB8E5A7006D4B9D /* StandardLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8792A9982BB36C66009842D8 /* StandardLibrary.swift */; };
876560032B882259002BDE42 /* TokenType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 876560022B882259002BDE42 /* TokenType.swift */; };
876560052B8825AC002BDE42 /* Token.swift in Sources */ = {isa = PBXBuildFile; fileRef = 876560042B8825AC002BDE42 /* Token.swift */; };
876560072B8827F9002BDE42 /* Scanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 876560062B8827F9002BDE42 /* Scanner.swift */; };
Expand Down Expand Up @@ -319,6 +320,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8764AB632BB8E5A7006D4B9D /* StandardLibrary.swift in Sources */,
8702307C2B9575610056FE57 /* ResolvedStatement.swift in Sources */,
873CCB2D2B8E88C900FC249A /* Interpreter.swift in Sources */,
876A31632B8987740085A350 /* ScanError.swift in Sources */,
Expand Down
22 changes: 22 additions & 0 deletions slox/NativeFunction.swift
Expand Up @@ -12,6 +12,8 @@ enum NativeFunction: LoxCallable, Equatable, CaseIterable {
case appendNative
case deleteAtNative
case removeValueNative
case keysNative
case valuesNative

var arity: Int {
switch self {
Expand All @@ -23,6 +25,10 @@ enum NativeFunction: LoxCallable, Equatable, CaseIterable {
return 2
case .removeValueNative:
return 2
case .keysNative:
return 1
case .valuesNative:
return 1
}
}

Expand Down Expand Up @@ -57,6 +63,22 @@ enum NativeFunction: LoxCallable, Equatable, CaseIterable {
let key = args[1]

return loxDictionary.kvPairs.removeValue(forKey: key) ?? .nil
case .keysNative:
guard case .instance(let loxDictionary as LoxDictionary) = args[0] else {
throw RuntimeError.notAListOrDictionary
}

let keys = Array(loxDictionary.kvPairs.keys)

return try! interpreter.makeList(elements: keys)
case .valuesNative:
guard case .instance(let loxDictionary as LoxDictionary) = args[0] else {
throw RuntimeError.notAListOrDictionary
}

let values = Array(loxDictionary.kvPairs.values)

return try! interpreter.makeList(elements: values)
}
}
}
8 changes: 8 additions & 0 deletions slox/StandardLibrary.swift
Expand Up @@ -44,6 +44,14 @@ class List {
}
class Dictionary {
keys {
return keysNative(this);
}
values {
return valuesNative(this);
}
removeValue(key) {
return removeValueNative(this, key);
}
Expand Down

0 comments on commit 362af7e

Please sign in to comment.