SPB Git

Repository Public

AST.swift 3.4 KB 115 lines swift

1// ORIX DJ2// File: Packages/ORIXDJCore/Sources/ORIXScript/AST.swift3// Description: ORIX Script syntax tree produced by the parser.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89/// A dotted path such as `deck.A.filter.highpass` or a plain variable name.10public struct PathExpr: Equatable, Sendable {11    public var segments: [String]12    public var location: SourceLocation13    public var length: Int14    public var text: String { segments.joined(separator: ".") }15}1617public indirect enum Expr: Equatable, Sendable {18    case literal(Value, SourceLocation)19    case path(PathExpr)20    case call(PathExpr, [Expr])21    case unary(String, Expr, SourceLocation)22    case binary(String, Expr, Expr, SourceLocation)23    case array([Expr], SourceLocation)24    case index(Expr, Expr, SourceLocation)2526    public var location: SourceLocation {27        switch self {28        case let .literal(_, l), let .unary(_, _, l), let .binary(_, _, _, l), let .array(_, l), let .index(_, _, l): l29        case let .path(p), let .call(p, _): p.location30        }31    }32}3334public enum Anchor: Equatable, Sendable {35    /// `at bar N` (1-based).36    case bar(Expr)37    /// `at beat N` (1-based).38    case beat(Expr)39    /// `at 30s`, `at 16beats`: offset from the start of the automation.40    case offset(Expr)41    case nextBeat42    case nextBar43}4445public enum WaitSpec: Equatable, Sendable {46    case duration(Expr)47    case nextBeat48    case nextBar49    /// `wait until bar 9`.50    case until(Anchor)51}5253public struct EventSpec: Equatable, Sendable {54    public var path: PathExpr55    public var argument: Expr?56}5758public struct FnParam: Equatable, Sendable {59    public var name: String60    public var type: ScriptType61    public var location: SourceLocation62}6364public struct FnDecl: Equatable, Sendable {65    public var name: String66    public var params: [FnParam]67    public var returnType: ScriptType?68    public var body: [Stmt]69    public var location: SourceLocation70}7172public indirect enum StmtKind: Equatable, Sendable {73    case declare(name: String, type: ScriptType?, value: Expr, mutable: Bool)74    case assign(PathExpr, Expr)75    case ramp(PathExpr, Expr)76    case call(PathExpr, [Expr])77    case wait(WaitSpec)78    case at(Anchor, [Stmt])79    case over(Expr, curve: String?, [Stmt])80    case every(period: Expr, total: Expr, [Stmt])81    case repeatLoop(Expr, [Stmt])82    case forRange(name: String, from: Expr, to: Expr, inclusive: Bool, [Stmt])83    case forIn(name: String, Expr, [Stmt])84    case whileLoop(Expr, limit: Expr, [Stmt])85    case ifElse(Expr, [Stmt], [Stmt]?)86    case on(EventSpec, once: Bool, [Stmt])87    case returnValue(Expr?)88    case stop89    case breakLoop90    case continueLoop91}9293public struct Stmt: Equatable, Sendable {94    public var kind: StmtKind95    public var location: SourceLocation96    public init(_ kind: StmtKind, _ location: SourceLocation) { self.kind = kind; self.location = location }97}9899public enum StartQuantize: String, Sendable, Equatable, Codable { case none, beat, bar }100101public struct AutomationDecl: Equatable, Sendable {102    public var name: String103    public var quantize: StartQuantize104    public var body: [Stmt]105    public var location: SourceLocation106}107108public struct ScriptFile: Equatable, Sendable {109    public var constants: [Stmt] = []110    public var functions: [FnDecl] = []111    public var automations: [AutomationDecl] = []112    /// Statements outside any automation (console input).113    public var loose: [Stmt] = []114}115
Files main
Menu

Theme