// DSLGraphBuilder.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT // Turns a parsed `DSLExpr` into a `Generator` graph by resolving each call // against the builtin registry and binding its arguments. Numbers become // scalars, `+ - * /` become the corresponding generator operators, and a call // invokes the generated `make` closure. Construction happens on the caller's // thread (main, in the host) — no RT-thread involvement, same as any Swift // `build()`. import LyrebirdSynth /// Builds a generator graph from a DSL expression. public enum DSLGraphBuilder { /// Build `expr` into a signal graph (its top-level value must be a signal, /// not a resource). Convenience over ``build(_:resources:)`` for expressions /// that reference no resources. /// - Throws: ``DSLBuildError`` for an unknown generator, a bad argument, or /// a type mismatch. public static func build(_ expr: DSLExpr) throws -> any ValidGeneratorInput { try signal(from: try build(expr, resources: DSLEmptyResources())) } /// Build `expr` into a ``DSLValue`` (a signal or a resource), resolving bare /// identifiers through `resources`. public static func build( _ expr: DSLExpr, resources: DSLResourceResolver ) throws -> DSLValue { switch expr { case .number(let value, _): return .signal(value) case .reference(let name, _): guard let buffer: LyrebirdBuffer = resources.buffer(named: name) else { // A better hint when the bare identifier is actually a generator // missing its call parentheses. if !DSLBuiltinRegistry.rates(of: name).isEmpty { throw DSLBuildError.referenceIsGenerator(name) } throw DSLBuildError.unknownReference(name) } return .buffer(buffer) case .binary(let op, let lhs, let rhs, _): let l: any ValidGeneratorInput = try signal(from: try build(lhs, resources: resources)) let r: any ValidGeneratorInput = try signal(from: try build(rhs, resources: resources)) return .signal(apply(op, l, r)) case .call(let name, let rate, let arguments, _): let builtin: DSLBuiltin = try resolve(name, rate: rate) let bound: [String: DSLValue] = try bind(arguments, to: builtin, resources: resources) return .signal(try builtin.make(bound)) } } /// Unwrap a signal, rejecting a resource used where a signal is required. private static func signal(from value: DSLValue) throws -> any ValidGeneratorInput { guard case .signal(let s) = value else { throw DSLBuildError.operatorRequiresSignal } return s } /// Resolve a name (and optional explicit rate) to a builtin. /// /// With an explicit rate (`.Audio`/`.Control`), looks up exactly that rate and /// throws ``DSLBuildError/rateUnavailable(builtin:rate:)`` if the name has /// no such variant. Without one, prefers audio and falls back to a name's /// sole rate (so a control-only generator like `MouseX` is reachable). private static func resolve(_ name: String, rate: DSLRate?) throws -> DSLBuiltin { let rates: Set = DSLBuiltinRegistry.rates(of: name) guard !rates.isEmpty else { throw DSLBuildError.unknownBuiltin(name) } if let rate: DSLRate = rate { return try DSLBuiltinRegistry.lookup(name, rate: rate) } if rates.contains(.audio) { return try DSLBuiltinRegistry.lookup(name, rate: .audio) } return try DSLBuiltinRegistry.lookup(name, rate: .control) } /// Bind call arguments to parameter labels: positional arguments fill /// parameters in declaration order; labelled arguments bind by label. private static func bind( _ arguments: [DSLExpr.Argument], to builtin: DSLBuiltin, resources: DSLResourceResolver ) throws -> [String: DSLValue] { let labels: [String] = builtin.parameters.map { $0.label } var bound: [String: DSLValue] = [:] var nextPositional: Int = 0 for argument: DSLExpr.Argument in arguments { let value: DSLValue = try build(argument.value, resources: resources) let targetLabel: String if let label: String = argument.label { guard labels.contains(label) else { throw DSLBuildError.unknownArgument(builtin: builtin.name, label: label) } targetLabel = label } else { // Advance past parameters already filled (labelled or positional). while nextPositional < labels.count, bound[labels[nextPositional]] != nil { nextPositional += 1 } guard nextPositional < labels.count else { throw DSLBuildError.tooManyArguments(builtin: builtin.name, max: labels.count) } targetLabel = labels[nextPositional] nextPositional += 1 } // A parameter bound twice (a positional then a label, or two labels) // is an error, not a silent overwrite. guard bound[targetLabel] == nil else { throw DSLBuildError.duplicateArgument(builtin: builtin.name, label: targetLabel) } bound[targetLabel] = value } return bound } private static func apply( _ op: DSLExpr.BinaryOp, _ lhs: any ValidGeneratorInput, _ rhs: any ValidGeneratorInput ) -> any ValidGeneratorInput { switch op { case .add: return lhs + rhs case .subtract: return lhs - rhs case .multiply: return lhs * rhs case .divide: return lhs / rhs } } }