Files
swift-mirror/test/Interpreter/Inputs/variadic_generic_library.swift
Rintaro Ishizaki ababa1e461 [Parse] Accept 'self' after 'each'
Also move 'repeat', 'each', and 'any' expression parsing to
'parseExprSequenceElement'

rdar://107450487
2023-07-10 15:37:00 -07:00

66 lines
1.3 KiB
Swift

public struct Key: Hashable {
static var index = 0
public let id: Int
init() {
self.id = Self.index
Self.index += 1
}
}
public struct Variable<Value> {
public let key = Key()
}
public struct Bindings {
private var storage: [Key : Any] = [:]
public init<each T>(
_ value: repeat (Variable<each T>, each T)
) {
_ = (repeat storage[(each value).0.key] = (each value).1)
}
}
public protocol Expression<Result> {
associatedtype Result
func evaluate(_ bindings: Bindings) throws -> Result
}
public struct True: Expression {
public init() {}
public func evaluate(_ bindings: Bindings) throws -> Bool {
true
}
}
public struct Predicate<each Input> {
var variables: (repeat Variable<each Input>)
var expression: any Expression<Bool>
public init<Expr>(
builder: (repeat Variable<each Input>) -> Expr
) where Expr: Expression<Bool> {
self.variables = (repeat Variable<each Input>())
self.expression = builder(repeat each self.variables)
}
public func evaluate(
_ input: repeat each Input
) throws -> Bool {
return try expression.evaluate(
.init(repeat (each self.variables, each input))
)
}
}
extension Sequence {
public func filter(_ predicate: Predicate<Element>) throws -> [Element] {
try self.filter {
try predicate.evaluate($0)
}
}
}