Files
Lyrebird/Sources/LyrebirdGraphDSL/DSLBuiltin.swift
T

58 lines
2.3 KiB
Swift

// DSLBuiltin.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
// One entry in the interpreted graph-DSL's builtin registry: a named generator
// the DSL can construct. Each builtin knows its parameter labels (for binding
// positional + named arguments and for diagnostics) and carries a closure that
// calls the real Swift generator init. The registry is produced by the
// `DSLRegistryGen` code-gen tool; this file only defines the contract the
// generated table conforms to, so the parser/builder are independent of how the
// table is populated.
import LyrebirdSynth
/// A generator the DSL can construct by name.
///
/// The graph builder binds a call's positional and named arguments to
/// ``parameters`` (positional first, then labelled), validates that every label
/// is known and every required parameter is present, and then invokes ``make``
/// with the bound arguments keyed by label. `make` calls the real Swift init,
/// applying its own defaults for any omitted optional parameter.
public struct DSLBuiltin: Sendable {
/// The name used in DSL source (`OscSin`, `Pan2`, …).
public let name: String
/// The rate this builtin evaluates at. A name may exist at both rates; the
/// registry keys by `(name, rate)`.
public let rate: DSLRate
/// Declared parameters, in positional order.
public let parameters: [DSLParameter]
/// Construct the generator from arguments already bound to parameter labels.
/// Only labels present in ``parameters`` appear; omitted optional parameters
/// are absent (the closure applies the Swift init's default). Each argument
/// is a ``DSLValue`` — a signal or a resource — from which the closure pulls
/// the concrete type its init needs via ``DSLArgument``.
///
/// `@Sendable` so the registry can be a `static let` — the generated
/// closures capture nothing.
public let make: @Sendable ([String: DSLValue]) throws -> any ValidGeneratorInput
public init(
name: String,
rate: DSLRate,
parameters: [DSLParameter],
make: @escaping @Sendable ([String: DSLValue]) throws -> any ValidGeneratorInput
) {
self.name = name
self.rate = rate
self.parameters = parameters
self.make = make
}
}