From d2797e1acc16aa8d089aff8e5d2077ca44542cff Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Fri, 29 May 2026 17:36:15 -0400 Subject: [PATCH] compiler: Use Macro to Derive CompilableStatement Implementations Signed-off-by: Will Hawkins --- Sources/Common/Support.swift | 3 - Sources/Macros/Macros.swift | 10 +-- Sources/P4Compiler/Common.swift | 3 + Sources/P4Compiler/Statement.swift | 98 ++++++------------------------ 4 files changed, 23 insertions(+), 91 deletions(-) diff --git a/Sources/Common/Support.swift b/Sources/Common/Support.swift index dc93615..606389b 100644 --- a/Sources/Common/Support.swift +++ b/Sources/Common/Support.swift @@ -106,6 +106,3 @@ public func Fold(input: [T], initial: A, block: (T, A) -> A) -> A { @freestanding(codeItem) public macro MustOr(result: E, thing: E?, or: N) = #externalMacro(module: "Macros", type: "MustOr") - -@attached(peer) public macro DeriveCompilableStatement() = - #externalMacro(module: "Macros", type: "DeriveCompilableStatement") diff --git a/Sources/Macros/Macros.swift b/Sources/Macros/Macros.swift index d3ff755..0382fb6 100644 --- a/Sources/Macros/Macros.swift +++ b/Sources/Macros/Macros.swift @@ -439,17 +439,14 @@ public struct CliTestDeclarationMacro: PeerMacro, Sendable { } } -public struct DeriveCompilableStatement: PeerMacro, Sendable { +public enum DeriveCompilableStatement: MemberMacro { public static func expansion( - of node: AttributeSyntax, - providingPeersOf declaration: some DeclSyntaxProtocol, - in context: some MacroExpansionContext + of: AttributeSyntax, providingMembersOf type: some DeclGroupSyntax, conformingTo: [TypeSyntax], + in: some MacroExpansionContext ) throws -> [DeclSyntax] { - let type_name = declaration.cast(ExtensionDeclSyntax.self).extendedType let implementation = DeclSyntax( """ - extension VariableDeclarationStatement: CompilableStatement { public static func CompileStatement( node: Node, withContext context: CompilerContext ) -> Result { @@ -458,7 +455,6 @@ public struct DeriveCompilableStatement: PeerMacro, Sendable { case .Error(let e): .Error(e) } } - } """) return [implementation] } diff --git a/Sources/P4Compiler/Common.swift b/Sources/P4Compiler/Common.swift index 6e61417..103770d 100644 --- a/Sources/P4Compiler/Common.swift +++ b/Sources/P4Compiler/Common.swift @@ -384,3 +384,6 @@ extension Node { return SourceLocation(self.range.location, self.range.length) } } + +@attached(member, names: named(CompileStatement)) +public macro deriveCompilableStatement() = #externalMacro(module: "Macros", type: "DeriveCompilableStatement") \ No newline at end of file diff --git a/Sources/P4Compiler/Statement.swift b/Sources/P4Compiler/Statement.swift index 1db0e19..8275985 100644 --- a/Sources/P4Compiler/Statement.swift +++ b/Sources/P4Compiler/Statement.swift @@ -90,16 +90,8 @@ extension BlockStatement: Compilable { } } -extension BlockStatement: CompilableStatement { - public static func CompileStatement( - node: Node, withContext context: CompilerContext - ) -> Result { - return switch Compile(node: node, withContext: context) { - case .Ok(let res): .Ok(res) - case .Error(let e): .Error(e) - } - } -} +@deriveCompilableStatement +extension BlockStatement: CompilableStatement {} extension ConditionalStatement: Compilable { public typealias C = ConditionalStatement @@ -173,16 +165,8 @@ extension ConditionalStatement: Compilable { } } -extension ConditionalStatement: CompilableStatement { - public static func CompileStatement( - node: Node, withContext context: CompilerContext - ) -> Result { - return switch Compile(node: node, withContext: context) { - case .Ok(let res): .Ok(res) - case .Error(let e): .Error(e) - } - } -} +@deriveCompilableStatement +extension ConditionalStatement: CompilableStatement {} extension VariableDeclarationStatement: Compilable { public typealias C = VariableDeclarationStatement @@ -279,16 +263,8 @@ extension VariableDeclarationStatement: Compilable { } } -extension VariableDeclarationStatement: CompilableStatement { - public static func CompileStatement( - node: Node, withContext context: CompilerContext - ) -> Result { - return switch Compile(node: node, withContext: context) { - case .Ok(let res): .Ok(res) - case .Error(let e): .Error(e) - } - } -} +@deriveCompilableStatement +extension VariableDeclarationStatement: CompilableStatement {} extension ExpressionStatement: Compilable { public typealias C = ExpressionStatement @@ -307,16 +283,8 @@ extension ExpressionStatement: Compilable { } } -extension ExpressionStatement: CompilableStatement { - public static func CompileStatement( - node: Node, withContext context: CompilerContext - ) -> Result { - return switch Compile(node: node, withContext: context) { - case .Ok(let res): .Ok(res) - case .Error(let e): .Error(e) - } - } -} +@deriveCompilableStatement +extension ExpressionStatement: CompilableStatement {} extension ParserAssignmentStatement: Compilable { public typealias C = ParserAssignmentStatement @@ -372,16 +340,8 @@ extension ParserAssignmentStatement: Compilable { } } -extension ParserAssignmentStatement: CompilableStatement { - public static func CompileStatement( - node: Node, withContext context: CompilerContext - ) -> Result { - return switch Compile(node: node, withContext: context) { - case .Ok(let res): .Ok(res) - case .Error(let e): .Error(e) - } - } -} +@deriveCompilableStatement +extension ParserAssignmentStatement: CompilableStatement {} extension ReturnStatement: Compilable { public typealias C = ReturnStatement @@ -410,16 +370,8 @@ extension ReturnStatement: Compilable { } } -extension ReturnStatement: CompilableStatement { - public static func CompileStatement( - node: Node, withContext context: CompilerContext - ) -> Result { - return switch Compile(node: node, withContext: context) { - case .Ok(let res): .Ok(res) - case .Error(let e): .Error(e) - } - } -} +@deriveCompilableStatement +extension ReturnStatement: CompilableStatement {} extension ApplyStatement: Compilable { public typealias C = ApplyStatement @@ -433,22 +385,14 @@ extension ApplyStatement: Compilable { return switch BlockStatement.Compile(node: expression_node, withContext: context) { case .Ok(let statement): - .Ok(ApplyStatement(statement as! BlockStatement)) + .Ok(ApplyStatement(statement)) case .Error(let e): .Error(e) } } } -extension ApplyStatement: CompilableStatement { - public static func CompileStatement( - node: Node, withContext context: CompilerContext - ) -> Result { - return switch Compile(node: node, withContext: context) { - case .Ok(let res): .Ok(res) - case .Error(let e): .Error(e) - } - } -} +@deriveCompilableStatement +extension ApplyStatement: CompilableStatement {} extension Instantiation: Compilable { public typealias C = Instantiation @@ -582,13 +526,5 @@ extension Instantiation: Compilable { } } -extension Instantiation: CompilableStatement { - public static func CompileStatement( - node: Node, withContext context: CompilerContext - ) -> Result { - return switch Compile(node: node, withContext: context) { - case .Ok(let res): .Ok(res) - case .Error(let e): .Error(e) - } - } -} +@deriveCompilableStatement +extension Instantiation: CompilableStatement {}