Skip to content

Commit a852863

Browse files
committed
Implement library (lispkit date-time).
1 parent b2764cd commit a852863

File tree

10 files changed

+690
-231
lines changed

10 files changed

+690
-231
lines changed

LispKit.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
CC2C73E121A22F0D00B18661 /* hash-table.sld in Copy pre-installed Scheme libraries */ = {isa = PBXBuildFile; fileRef = CC2C73E021A22DDD00B18661 /* hash-table.sld */; };
6262
CC2D124F1DB40E7F009BDC72 /* LibraryManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC2D124E1DB40E7F009BDC72 /* LibraryManager.swift */; };
6363
CC31A6981D3AC223008D7728 /* Hash.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC31A6971D3AC223008D7728 /* Hash.swift */; };
64+
CC35F9C2222188BD00895A10 /* DateTimeLibrary.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC35F9C1222188BC00895A10 /* DateTimeLibrary.swift */; };
6465
CC35FD0D1C6FA4B700C8B992 /* VirtualMachine.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC35FD0C1C6FA4B700C8B992 /* VirtualMachine.swift */; };
6566
CC35FD0F1C711FEA00C8B992 /* BindingGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC35FD0E1C711FEA00C8B992 /* BindingGroup.swift */; };
6667
CC3A025A1F97FD72009B959C /* json.sld in Copy pre-installed LispKit libraries */ = {isa = PBXBuildFile; fileRef = CC3A02591F97F3E1009B959C /* json.sld */; };
@@ -625,6 +626,7 @@
625626
CC2C73E021A22DDD00B18661 /* hash-table.sld */ = {isa = PBXFileReference; lastKnownFileType = text; path = "hash-table.sld"; sourceTree = "<group>"; };
626627
CC2D124E1DB40E7F009BDC72 /* LibraryManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LibraryManager.swift; sourceTree = "<group>"; };
627628
CC31A6971D3AC223008D7728 /* Hash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Hash.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
629+
CC35F9C1222188BC00895A10 /* DateTimeLibrary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTimeLibrary.swift; sourceTree = "<group>"; };
628630
CC35FD0C1C6FA4B700C8B992 /* VirtualMachine.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = VirtualMachine.swift; sourceTree = "<group>"; tabWidth = 2; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
629631
CC35FD0E1C711FEA00C8B992 /* BindingGroup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BindingGroup.swift; sourceTree = "<group>"; };
630632
CC3A02591F97F3E1009B959C /* json.sld */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = json.sld; sourceTree = "<group>"; };
@@ -1291,6 +1293,7 @@
12911293
CC5A47221C529D4A00EFCF76 /* CoreLibrary.swift */,
12921294
CC5A47241C52E08900EFCF76 /* ControlFlowLibrary.swift */,
12931295
CC0BA16B1DFC843C0063B9DE /* SystemLibrary.swift */,
1296+
CC35F9C1222188BC00895A10 /* DateTimeLibrary.swift */,
12941297
CCDF99FB1D3D7A3100FD2A57 /* BoxLibrary.swift */,
12951298
CC6A3B631C52EED400E962E2 /* MathLibrary.swift */,
12961299
CC6A3B5B1C52E71F00E962E2 /* ListLibrary.swift */,
@@ -1581,6 +1584,7 @@
15811584
CCC531A01D18595500FAED6E /* TextOutputTarget.swift in Sources */,
15821585
CC14F4B01F939486000FB1E0 /* Features.swift in Sources */,
15831586
CC5A47211C5295E600EFCF76 /* NativeLibrary.swift in Sources */,
1587+
CC35F9C2222188BD00895A10 /* DateTimeLibrary.swift in Sources */,
15841588
CC54924121DC2A44004A0652 /* CharSet.swift in Sources */,
15851589
CC6A3B621C52ED7D00E962E2 /* CharLibrary.swift in Sources */,
15861590
CCA31283204CB7CB0026FF13 /* EvalError.swift in Sources */,

Sources/LispKit/Compiler/EvalError.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public enum EvalError: Int, Hashable {
8989
case cannotEncodeBytevector
9090
case invalidDateTime
9191
case invalidTimeZone
92+
case invalidDateStyle
9293
case invalidDefaultDrawing
9394
case invalidDefaultShape
9495
case invalidPoint
@@ -232,9 +233,11 @@ public enum EvalError: Int, Hashable {
232233
case .cannotEncodeBytevector:
233234
return "unable to encode bytevector $0"
234235
case .invalidDateTime:
235-
return "invalid/incomplete date time component list: $0"
236+
return "invalid/incomplete date-time components: $0"
236237
case .invalidTimeZone:
237-
return "invalid/incomplete time zone identifier: $0"
238+
return "invalid time zone: $0"
239+
case .invalidDateStyle:
240+
return "invalid date-time format style: $0"
238241
case .invalidDefaultDrawing:
239242
return "invalid default drawing: $0"
240243
case .invalidDefaultShape:

Sources/LispKit/Data/Expr.swift

+9
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public enum Expr: Trackable, Hashable {
135135
return .imageType
136136
} else if obj is ImmutableBox<NSRegularExpression> {
137137
return .regexpType
138+
} else if obj is ImmutableBox<DateComponents> {
139+
return .dateTimeType
138140
} else {
139141
return .objectType
140142
}
@@ -1000,6 +1002,13 @@ extension Expr: CustomStringConvertible {
10001002
return "#<image \(obj.identityString)>"
10011003
} else if let bx = obj as? ImmutableBox<NSRegularExpression> {
10021004
return "#<regexp \"\(Expr.escapeStr(bx.value.pattern))\">"
1005+
} else if let bx = obj as? ImmutableBox<DateComponents> {
1006+
guard let date = bx.value.date else {
1007+
return "#<date-time malformed>"
1008+
}
1009+
let formatter = ISO8601DateFormatter()
1010+
formatter.timeZone = bx.value.timeZone ?? TimeZone.current
1011+
return "#<date-time \(formatter.string(from: date))>"
10031012
} else {
10041013
return "#<\(obj.typeDescription) \(obj.identityString)>"
10051014
}

Sources/LispKit/Data/Procedure.swift

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public final class Procedure: Reference, CustomStringConvertible {
7979
case native1O((Expr, Expr?) throws -> Expr)
8080
case native2O((Expr, Expr, Expr?) throws -> Expr)
8181
case native3O((Expr, Expr, Expr, Expr?) throws -> Expr)
82+
case native0OO((Expr?, Expr?) throws -> Expr)
8283
case native1OO((Expr, Expr?, Expr?) throws -> Expr)
8384
case native2OO((Expr, Expr, Expr?, Expr?) throws -> Expr)
8485
case native3OO((Expr, Expr, Expr, Expr?, Expr?) throws -> Expr)
@@ -187,6 +188,13 @@ public final class Procedure: Reference, CustomStringConvertible {
187188
_ compiler: FormCompiler? = nil) {
188189
self.kind = .primitive(name, .native3O(proc), compiler)
189190
}
191+
192+
/// Initializer for primitive procedures
193+
public init(_ name: String,
194+
_ proc: @escaping (Expr?, Expr?) throws -> Expr,
195+
_ compiler: FormCompiler? = nil) {
196+
self.kind = .primitive(name, .native0OO(proc), compiler)
197+
}
190198

191199
/// Initializer for primitive procedures
192200
public init(_ name: String,

Sources/LispKit/Data/Type.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public enum Type: Int, CustomStringConvertible {
7777
case imageType
7878
case charSetType
7979
case regexpType
80+
case dateTimeType
8081

8182
public var description: String {
8283
switch self {
@@ -188,8 +189,10 @@ public enum Type: Int, CustomStringConvertible {
188189
return "image"
189190
case .charSetType:
190191
return "character set"
191-
case .regexpType:
192+
case .regexpType:
192193
return "regular expression"
194+
case .dateTimeType:
195+
return "date-time"
193196
}
194197
}
195198

@@ -282,4 +285,5 @@ private let objectSubtypes : Set<Type> = [.objectType,
282285
.drawingType,
283286
.imageType,
284287
.charSetType,
285-
.regexpType]
288+
.regexpType,
289+
.dateTimeType]

0 commit comments

Comments
 (0)