compiler: Use Macro to Derive CompilableStatement Implementations
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
@@ -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) =
|
||||
#externalMacro(module: "Macros", type: "MustOr")
|
||||
|
||||
@attached(peer) public macro DeriveCompilableStatement() =
|
||||
#externalMacro(module: "Macros", type: "DeriveCompilableStatement")
|
||||
|
||||
@@ -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<P4Statement> {
|
||||
@@ -458,7 +455,6 @@ public struct DeriveCompilableStatement: PeerMacro, Sendable {
|
||||
case .Error(let e): .Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
return [implementation]
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
@@ -90,16 +90,8 @@ extension BlockStatement: Compilable {
|
||||
}
|
||||
}
|
||||
|
||||
extension BlockStatement: CompilableStatement {
|
||||
public static func CompileStatement(
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@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<P4Statement> {
|
||||
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<P4Statement> {
|
||||
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<P4Statement> {
|
||||
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<P4Statement> {
|
||||
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<P4Statement> {
|
||||
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<P4Statement> {
|
||||
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<P4Statement> {
|
||||
return switch Compile(node: node, withContext: context) {
|
||||
case .Ok(let res): .Ok(res)
|
||||
case .Error(let e): .Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@deriveCompilableStatement
|
||||
extension Instantiation: CompilableStatement {}
|
||||
|
||||
Reference in New Issue
Block a user