117 lines
4.1 KiB
Swift
117 lines
4.1 KiB
Swift
// DSLArgument.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Coercion helpers used by the generated registry to turn a bound DSL argument
|
|
// (a `DSLValue`) into the concrete type a generator's Swift init expects.
|
|
//
|
|
// A `.signal` argument (number or graph) feeds an `any ValidGeneratorInput`
|
|
// parameter straight through, or coerces to `Double`/`Int`/`Float` when the init
|
|
// wants a literal (delay times, table sizes, …) — throwing when handed a graph.
|
|
// A `.buffer` argument feeds a `LyrebirdBuffer` parameter. The generated code
|
|
// supplies each parameter's Swift default (as a `.signal`) via `?? …` for
|
|
// optional parameters; a required parameter is passed `nil` and throws.
|
|
|
|
import LyrebirdSynth
|
|
|
|
/// Argument coercion for generated `DSLBuiltin.make` closures.
|
|
public enum DSLArgument {
|
|
|
|
/// A required signal-or-number parameter.
|
|
public static func input(
|
|
_ value: DSLValue?,
|
|
_ builtin: String,
|
|
_ label: String
|
|
) throws -> any ValidGeneratorInput {
|
|
switch value {
|
|
case .signal(let s):
|
|
return s
|
|
case .buffer:
|
|
throw DSLBuildError.expectedSignal(builtin: builtin, label: label)
|
|
case nil:
|
|
throw DSLBuildError.missingArgument(builtin: builtin, label: label)
|
|
}
|
|
}
|
|
|
|
/// A parameter that must be a literal number. Throws when the DSL supplied a
|
|
/// signal (a generator) or a resource, or when a required value is absent.
|
|
public static func double(
|
|
_ value: DSLValue?,
|
|
_ builtin: String,
|
|
_ label: String
|
|
) throws -> Double {
|
|
guard case .signal(let s)? = value else {
|
|
if value == nil {
|
|
throw DSLBuildError.missingArgument(builtin: builtin, label: label)
|
|
}
|
|
throw DSLBuildError.expectedNumber(builtin: builtin, label: label)
|
|
}
|
|
// DSL number literals are `Double` or `Int` — `Float` is not a
|
|
// `ValidGeneratorInput`, so it never reaches here.
|
|
if let d: Double = s as? Double { return d }
|
|
if let i: Int = s as? Int { return Double(i) }
|
|
throw DSLBuildError.expectedNumber(builtin: builtin, label: label)
|
|
}
|
|
|
|
/// `Int` variant of ``double(_:_:_:)``. Truncates a fractional literal, and
|
|
/// throws ``DSLBuildError/numberOutOfRange(builtin:label:)`` rather than
|
|
/// trapping when the value is non-finite or outside the `Int` range.
|
|
public static func int(
|
|
_ value: DSLValue?,
|
|
_ builtin: String,
|
|
_ label: String
|
|
) throws -> Int {
|
|
let d: Double = try double(value, builtin, label)
|
|
guard d.isFinite, d >= Double(Int.min), d <= Double(Int.max) else {
|
|
throw DSLBuildError.numberOutOfRange(builtin: builtin, label: label)
|
|
}
|
|
return Int(d)
|
|
}
|
|
|
|
/// `Float` variant of ``double(_:_:_:)``.
|
|
public static func float(
|
|
_ value: DSLValue?,
|
|
_ builtin: String,
|
|
_ label: String
|
|
) throws -> Float {
|
|
Float(try double(value, builtin, label))
|
|
}
|
|
|
|
/// A parameter that must be a buffer resource.
|
|
public static func buffer(
|
|
_ value: DSLValue?,
|
|
_ builtin: String,
|
|
_ label: String
|
|
) throws -> LyrebirdBuffer {
|
|
switch value {
|
|
case .buffer(let b):
|
|
return b
|
|
case .signal:
|
|
throw DSLBuildError.expectedBuffer(builtin: builtin, label: label)
|
|
case nil:
|
|
throw DSLBuildError.missingArgument(builtin: builtin, label: label)
|
|
}
|
|
}
|
|
|
|
/// An optional buffer parameter (`LyrebirdBuffer?`): `nil` when the argument
|
|
/// is omitted, otherwise the buffer. (The code-gen only emits this for
|
|
/// parameters whose Swift default is `nil`, so passing `nil` on omission
|
|
/// matches that default.)
|
|
public static func optionalBuffer(
|
|
_ value: DSLValue?,
|
|
_ builtin: String,
|
|
_ label: String
|
|
) throws -> LyrebirdBuffer? {
|
|
switch value {
|
|
case .buffer(let b):
|
|
return b
|
|
case .signal:
|
|
throw DSLBuildError.expectedBuffer(builtin: builtin, label: label)
|
|
case nil:
|
|
return nil
|
|
}
|
|
}
|
|
}
|