Files
Lyrebird/Sources/LyrebirdGraphDSL/DSLInterpreter.swift
T

42 lines
1.6 KiB
Swift

// DSLInterpreter.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
// The one-call front door: lex + parse + build DSL source into playable outputs.
// Instant (no swiftc), pure Swift over core types — the whole point of the
// interpreted path.
import LyrebirdSynth
/// Evaluates DSL source into playable graphs.
public enum DSLInterpreter {
/// Lex, parse, and build `source` into one ``DSLOutput`` per statement,
/// resolving bare-identifier resource references through `resources`.
///
/// - Throws: ``DSLParseError`` for a lex/parse failure, or ``DSLBuildError``
/// for an unknown generator / bad argument / unresolved resource. Both
/// carry enough to surface a diagnostic.
public static func evaluate(
_ source: String,
resources: DSLResourceResolver = DSLEmptyResources()
) throws -> [DSLOutput] {
let program: DSLProgram = try DSLParser.parse(source)
return try program.statements.map { statement in
let (slot, expr): (String?, DSLExpr)
switch statement {
case .assignment(let name, let value, _): (slot, expr) = (name, value)
case .expression(let e): (slot, expr) = (nil, e)
}
// A top-level statement must produce a signal — you cannot play a
// bare buffer.
guard case .signal(let graph) = try DSLGraphBuilder.build(expr, resources: resources) else {
throw DSLBuildError.cannotPlayResource
}
return DSLOutput(slot: slot, graph: graph)
}
}
}