compiler: Use Macro to Derive CompilableStatement Implementations

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-29 17:36:15 -04:00
parent 4f6de341cc
commit d2797e1acc
4 changed files with 23 additions and 91 deletions
-3
View File
@@ -106,6 +106,3 @@ public func Fold<T, A>(input: [T], initial: A, block: (T, A) -> A) -> A {
@freestanding(codeItem) public macro MustOr<E, N>(result: E, thing: E?, or: N) = @freestanding(codeItem) public macro MustOr<E, N>(result: E, thing: E?, or: N) =
#externalMacro(module: "Macros", type: "MustOr") #externalMacro(module: "Macros", type: "MustOr")
@attached(peer) public macro DeriveCompilableStatement() =
#externalMacro(module: "Macros", type: "DeriveCompilableStatement")
+3 -7
View File
@@ -439,17 +439,14 @@ public struct CliTestDeclarationMacro: PeerMacro, Sendable {
} }
} }
public struct DeriveCompilableStatement: PeerMacro, Sendable { public enum DeriveCompilableStatement: MemberMacro {
public static func expansion( public static func expansion(
of node: AttributeSyntax, of: AttributeSyntax, providingMembersOf type: some DeclGroupSyntax, conformingTo: [TypeSyntax],
providingPeersOf declaration: some DeclSyntaxProtocol, in: some MacroExpansionContext
in context: some MacroExpansionContext
) throws -> [DeclSyntax] { ) throws -> [DeclSyntax] {
let type_name = declaration.cast(ExtensionDeclSyntax.self).extendedType
let implementation = DeclSyntax( let implementation = DeclSyntax(
""" """
extension VariableDeclarationStatement: CompilableStatement {
public static func CompileStatement( public static func CompileStatement(
node: Node, withContext context: CompilerContext node: Node, withContext context: CompilerContext
) -> Result<P4Statement> { ) -> Result<P4Statement> {
@@ -458,7 +455,6 @@ public struct DeriveCompilableStatement: PeerMacro, Sendable {
case .Error(let e): .Error(e) case .Error(let e): .Error(e)
} }
} }
}
""") """)
return [implementation] return [implementation]
} }
+3
View File
@@ -384,3 +384,6 @@ extension Node {
return SourceLocation(self.range.location, self.range.length) return SourceLocation(self.range.location, self.range.length)
} }
} }
@attached(member, names: named(CompileStatement))
public macro deriveCompilableStatement() = #externalMacro(module: "Macros", type: "DeriveCompilableStatement")
+17 -81
View File
@@ -90,16 +90,8 @@ extension BlockStatement: Compilable {
} }
} }
extension BlockStatement: CompilableStatement { @deriveCompilableStatement
public static func CompileStatement( extension BlockStatement: CompilableStatement {}
node: Node, withContext context: CompilerContext
) -> Result<P4Statement> {
return switch Compile(node: node, withContext: context) {
case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e)
}
}
}
extension ConditionalStatement: Compilable { extension ConditionalStatement: Compilable {
public typealias C = ConditionalStatement public typealias C = ConditionalStatement
@@ -173,16 +165,8 @@ extension ConditionalStatement: Compilable {
} }
} }
extension ConditionalStatement: CompilableStatement { @deriveCompilableStatement
public static func CompileStatement( extension ConditionalStatement: CompilableStatement {}
node: Node, withContext context: CompilerContext
) -> Result<P4Statement> {
return switch Compile(node: node, withContext: context) {
case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e)
}
}
}
extension VariableDeclarationStatement: Compilable { extension VariableDeclarationStatement: Compilable {
public typealias C = VariableDeclarationStatement public typealias C = VariableDeclarationStatement
@@ -279,16 +263,8 @@ extension VariableDeclarationStatement: Compilable {
} }
} }
extension VariableDeclarationStatement: CompilableStatement { @deriveCompilableStatement
public static func CompileStatement( extension VariableDeclarationStatement: CompilableStatement {}
node: Node, withContext context: CompilerContext
) -> Result<P4Statement> {
return switch Compile(node: node, withContext: context) {
case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e)
}
}
}
extension ExpressionStatement: Compilable { extension ExpressionStatement: Compilable {
public typealias C = ExpressionStatement public typealias C = ExpressionStatement
@@ -307,16 +283,8 @@ extension ExpressionStatement: Compilable {
} }
} }
extension ExpressionStatement: CompilableStatement { @deriveCompilableStatement
public static func CompileStatement( extension ExpressionStatement: CompilableStatement {}
node: Node, withContext context: CompilerContext
) -> Result<P4Statement> {
return switch Compile(node: node, withContext: context) {
case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e)
}
}
}
extension ParserAssignmentStatement: Compilable { extension ParserAssignmentStatement: Compilable {
public typealias C = ParserAssignmentStatement public typealias C = ParserAssignmentStatement
@@ -372,16 +340,8 @@ extension ParserAssignmentStatement: Compilable {
} }
} }
extension ParserAssignmentStatement: CompilableStatement { @deriveCompilableStatement
public static func CompileStatement( extension ParserAssignmentStatement: CompilableStatement {}
node: Node, withContext context: CompilerContext
) -> Result<P4Statement> {
return switch Compile(node: node, withContext: context) {
case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e)
}
}
}
extension ReturnStatement: Compilable { extension ReturnStatement: Compilable {
public typealias C = ReturnStatement public typealias C = ReturnStatement
@@ -410,16 +370,8 @@ extension ReturnStatement: Compilable {
} }
} }
extension ReturnStatement: CompilableStatement { @deriveCompilableStatement
public static func CompileStatement( extension ReturnStatement: CompilableStatement {}
node: Node, withContext context: CompilerContext
) -> Result<P4Statement> {
return switch Compile(node: node, withContext: context) {
case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e)
}
}
}
extension ApplyStatement: Compilable { extension ApplyStatement: Compilable {
public typealias C = ApplyStatement public typealias C = ApplyStatement
@@ -433,22 +385,14 @@ extension ApplyStatement: Compilable {
return switch BlockStatement.Compile(node: expression_node, withContext: context) { return switch BlockStatement.Compile(node: expression_node, withContext: context) {
case .Ok(let statement): case .Ok(let statement):
.Ok(ApplyStatement(statement as! BlockStatement)) .Ok(ApplyStatement(statement))
case .Error(let e): .Error(e) case .Error(let e): .Error(e)
} }
} }
} }
extension ApplyStatement: CompilableStatement { @deriveCompilableStatement
public static func CompileStatement( extension ApplyStatement: CompilableStatement {}
node: Node, withContext context: CompilerContext
) -> Result<P4Statement> {
return switch Compile(node: node, withContext: context) {
case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e)
}
}
}
extension Instantiation: Compilable { extension Instantiation: Compilable {
public typealias C = Instantiation public typealias C = Instantiation
@@ -582,13 +526,5 @@ extension Instantiation: Compilable {
} }
} }
extension Instantiation: CompilableStatement { @deriveCompilableStatement
public static func CompileStatement( extension Instantiation: CompilableStatement {}
node: Node, withContext context: CompilerContext
) -> Result<P4Statement> {
return switch Compile(node: node, withContext: context) {
case .Ok(let res): .Ok(res)
case .Error(let e): .Error(e)
}
}
}