Repository navigation
Expand file tree
/
Copy pathComputer.swift
More file actions
76 lines (67 loc) · 2.62 KB
/
Copy pathComputer.swift
File metadata and controls
76 lines (67 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import _JSON
public struct Computer: Sendable {
public let functions: [String: any AnyReturnsKeyword]
public static let `default` = Computer([
Compute.Keyword.This.function,
Compute.Keyword.Yes.function,
Compute.Keyword.Contains.function,
Compute.Keyword.ApproximatelyEqual.function,
Compute.Keyword.Comparison.function,
Compute.Keyword.Not.function,
Compute.Keyword.Either.function,
Compute.Keyword.Explain.function,
Compute.Keyword.Error.function,
Compute.Keyword.HTTP.Function(),
Compute.Keyword.Text.function,
Compute.Keyword.Date.function,
Compute.Keyword.Calendar.function,
Compute.Keyword.Seconds.function,
Compute.Keyword.Item.function,
Compute.Keyword.Get.function,
Compute.Keyword.Literal.function,
Compute.Keyword.Count.function,
Compute.Keyword.Exists.function,
Compute.Keyword.Map.function,
Compute.Keyword.ArraySort.function,
Compute.Keyword.ArraySlice.function,
Compute.Keyword.ArraySubscript.function,
Compute.Keyword.ArrayZip.function,
Compute.Keyword.ArrayGroup.function,
Compute.Keyword.ArrayMap.function,
Compute.Keyword.ArrayFilter.function,
Compute.Keyword.ArrayReduce.function,
])
public init(_ functions: [any AnyReturnsKeyword] = []) {
self.functions = Dictionary(functions.map { ($0.name, $0) }, uniquingKeysWith: { _, last in last })
}
public subscript(keyword: String) -> RegisteredKeyword? {
functions[keyword].map { RegisteredKeyword(name: keyword, function: $0) }
}
public func merging(_ functions: [any AnyReturnsKeyword]) -> Computer {
Computer(Array(self.functions.values) + functions)
}
}
extension Computer {
public struct RegisteredKeyword: Sendable {
public let name: String
public let function: any AnyReturnsKeyword
public var isComputeKeyword: Bool {
!(function is any Compute.ReturnsKeywordDefinition)
}
public var isReturnsKeyword: Bool {
function is any Compute.ReturnsKeywordDefinition
}
}
}
extension Computer.RegisteredKeyword: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.name == rhs.name
}
}
extension Computer.RegisteredKeyword {
public init?(returns data: [String: JSON]?, computer: Computer = .default) {
guard let data, let invocation = Compute.Invocation(object: data) else { return nil }
guard let keyword = computer[invocation.keyword] else { return nil }
self = keyword
}
}