From 362af7e292c14d7bdaa9f3a7659d16ce61dbc3b4 Mon Sep 17 00:00:00 2001 From: quephird Date: Sat, 30 Mar 2024 17:48:46 -0700 Subject: [PATCH] Added `keys` and `values` properties to `Dictionary`. --- slox.xcodeproj/project.pbxproj | 2 ++ slox/NativeFunction.swift | 22 ++++++++++++++++++++++ slox/StandardLibrary.swift | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/slox.xcodeproj/project.pbxproj b/slox.xcodeproj/project.pbxproj index 8908bc0..9d17009 100644 --- a/slox.xcodeproj/project.pbxproj +++ b/slox.xcodeproj/project.pbxproj @@ -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 */; }; @@ -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 */, diff --git a/slox/NativeFunction.swift b/slox/NativeFunction.swift index 712e72e..effb7a5 100644 --- a/slox/NativeFunction.swift +++ b/slox/NativeFunction.swift @@ -12,6 +12,8 @@ enum NativeFunction: LoxCallable, Equatable, CaseIterable { case appendNative case deleteAtNative case removeValueNative + case keysNative + case valuesNative var arity: Int { switch self { @@ -23,6 +25,10 @@ enum NativeFunction: LoxCallable, Equatable, CaseIterable { return 2 case .removeValueNative: return 2 + case .keysNative: + return 1 + case .valuesNative: + return 1 } } @@ -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) } } } diff --git a/slox/StandardLibrary.swift b/slox/StandardLibrary.swift index 26e15be..1ab65f8 100644 --- a/slox/StandardLibrary.swift +++ b/slox/StandardLibrary.swift @@ -44,6 +44,14 @@ class List { } class Dictionary { + keys { + return keysNative(this); + } + + values { + return valuesNative(this); + } + removeValue(key) { return removeValueNative(this, key); }