<commit_before>import Foundation

private let template = """
// DO NOT EDIT. Generated by funcgen.
import Foundation

private class EncoderRuntime {
    private var stack = [Function]()

    var level: Int {
        return stack.count - 1
    }

    var current: Function {
        get {
            return stack[level]
        }
        set {
            stack[level] = newValue
        }
    }

    func push() {
        stack.append(Function())
    }

    func pop() -> Function {
        return stack.removeLast()
    }

    static var runtime: EncoderRuntime {
        return Thread.current.threadDictionary.object(forKey: "edu.jhu.Functions.Runtime") as! EncoderRuntime
    }
}

protocol _Producer {
    var producer: Function.A { get }
}

extension Function.A {
    init(argumentNumber: Int) {
        level = UInt32(EncoderRuntime.runtime.level)
        step = Int32(-argumentNumber)
    }

    @discardableResult
    init(producer: Function.Producer.OneOf_Producer) {
        let runtime = EncoderRuntime.runtime
        level = UInt32(runtime.level)
        step = Int32(runtime.current.steps.count)
        var p = Function.Producer()
        p.producer = producer
        runtime.current.steps.append(p)
    }

    var producer: Function.A { return self }
}

extension Function: _Producer {
    var producer: Function.A {
        return Function.A(producer: .functionRaw(self))
    }
}\n\n
"""

struct Swift: Language {
    static let protobufTypeToNative = [
        "double": "Double", "float": "Float",
        "int32": "Int32", "int64": "Int64",
        "uint32": "UInt32", "uint64": "UInt64",
        "sint32": "Int32", "sint64": "Int64",
        "fixed32": "UInt32", "fixed64": "UInt64",
        "sfixed32": "Int32", "sfixed64": "Int64",
        "bool": "Bool",
        "string": "String", "bytes": "Data"
    ]
    
    func nativeType(for type: String) -> String {
        return Swift.protobufTypeToNative[type] ?? type
    }
    
    let parser: Parser
    
    private func writeCommaSeparated<T: Sequence>(_ sequence: T, to output: inout String, body: (T.Element) -> ()) {
        var first = true
        sequence.forEach {
            if !first { output.append(", ") } else { first = false }
            body($0)
        }
    }
    
    private func writeArgumentsExtension(to output: inout String) {
        for i in parser.argumentNumber.sorted() {
            if i == 0 { continue }
            output.append("extension Function.Producer.A\(i) {\n")
            output.append("    init(")
            writeCommaSeparated(1...i, to: &output) {
                output.append("_ o\($0): Function.A")
            }
            output.append(") {\n")
            for j in 1...i {
                output.append("        self.o\(j) = o\(j)\n")
            }
            output.append("    }\n}\n\n")
        }
    }
    
    private func writeBasicType(name: String, to output: inout String) {
        let native = Swift.protobufTypeToNative[name]
        let nativeName = native ?? name
        output.append("""
            protocol \(nativeName)Producer: _Producer {}
            
            extension Function.A: \(nativeName)Producer {}\n\n
            """)
        if native != nil {
            output.append("""
                extension \(nativeName): \(nativeName)Producer {
                    var producer: Function.A {
                        return Function.A(producer: .\(name)Raw(self))
                    }
                }\n\n
                """)
        }
    }
    
    private func writeFunctionType(name: String, functionType: Parser.FunctionType, to output: inout String) {
        output.append("""
            extension Function.A {
                var \(name): \(name)Producer {
                    return {
            """)
        if functionType.argumentTypes.count > 0 {
            output.append(" ")
            writeCommaSeparated(functionType.argumentTypes.indices, to: &output) {
                output.append("o\($0 + 1)")
            }
            output.append(" in")
        }
        output.append(" Function.A(producer: .\(name)(.init(self")
        for i in functionType.argumentTypes.indices {
            output.append(", o\(i + 1).producer")
        }
        output.append(")))")
        if let returnType = functionType.returnType, case .function = parser.typesMap[returnType]! {
            output.append(".\(returnType)")
        }
        output.append(" }\n    }\n}\n\n")
    }
    
    private func writeTypealias(name: String, functionType: Parser.FunctionType, to output: inout String) {
        // producer
        output.append("typealias \(name)Producer = (")
        writeCommaSeparated(functionType.argumentTypes, to: &output) {
            output.append("\(nativeType(for: $0))Producer")
        }
        if let type = functionType.returnType {
            output.append(") -> \(nativeType(for: type))Producer\n")
        } else {
            output.append(") -> ()\n")
        }
        
        // real
        output.append("typealias \(name) = (")
        writeCommaSeparated(functionType.argumentTypes, to: &output) {
            output.append("\(nativeType(for: $0))")
        }
        if let type = functionType.returnType {
            output.append(") -> \(nativeType(for: type))\n\n")
        } else {
            output.append(") -> ()\n\n")
        }
    }
    
    private func writeSymbol(name: String, functionType: Parser.FunctionType, to output: inout String) {
        output.append("func \(name)(")
        writeCommaSeparated(functionType.argumentTypes.enumerated(), to: &output) {
            output.append("_ o\($0.offset + 1): \(nativeType(for: $0.element))Producer")
        }
        if let type = functionType.returnType {
            output.append(") -> \(nativeType(for: type))Producer {\n")
        } else {
            output.append(") {\n")
        }
        output.append("    return Function.A(producer: .\(name)(.init(")
        writeCommaSeparated(functionType.argumentTypes.enumerated(), to: &output) {
            if case .function? = parser.typesMap[$0.element] {
                output.append("\($0.element)EncodeInternal(o\($0.offset + 1)).producer")
            } else {
                output.append("o\($0.offset + 1).producer")
            }
        }
        output.append(")))")
        if let returnType = functionType.returnType, case .function = parser.typesMap[returnType]! {
            output.append(".\(returnType)")
        }
        output.append("\n}\n\n")
    }
    <commit_msg>Fix symbols without return type<commit_after>import Foundation

private let template = """
// DO NOT EDIT. Generated by funcgen.
import Foundation

private class EncoderRuntime {
    private var stack = [Function]()

    var level: Int {
        return stack.count - 1
    }

    var current: Function {
        get {
            return stack[level]
        }
        set {
            stack[level] = newValue
        }
    }

    func push() {
        stack.append(Function())
    }

    func pop() -> Function {
        return stack.removeLast()
    }

    static var runtime: EncoderRuntime {
        return Thread.current.threadDictionary.object(forKey: "edu.jhu.Functions.Runtime") as! EncoderRuntime
    }
}

protocol _Producer {
    var producer: Function.A { get }
}

extension Function.A {
    init(argumentNumber: Int) {
        level = UInt32(EncoderRuntime.runtime.level)
        step = Int32(-argumentNumber)
    }

    @discardableResult
    init(producer: Function.Producer.OneOf_Producer) {
        let runtime = EncoderRuntime.runtime
        level = UInt32(runtime.level)
        step = Int32(runtime.current.steps.count)
        var p = Function.Producer()
        p.producer = producer
        runtime.current.steps.append(p)
    }

    var producer: Function.A { return self }
}

extension Function: _Producer {
    var producer: Function.A {
        return Function.A(producer: .functionRaw(self))
    }
}\n\n
"""

struct Swift: Language {
    static let protobufTypeToNative = [
        "double": "Double", "float": "Float",
        "int32": "Int32", "int64": "Int64",
        "uint32": "UInt32", "uint64": "UInt64",
        "sint32": "Int32", "sint64": "Int64",
        "fixed32": "UInt32", "fixed64": "UInt64",
        "sfixed32": "Int32", "sfixed64": "Int64",
        "bool": "Bool",
        "string": "String", "bytes": "Data"
    ]
    
    func nativeType(for type: String) -> String {
        return Swift.protobufTypeToNative[type] ?? type
    }
    
    let parser: Parser
    
    private func writeCommaSeparated<T: Sequence>(_ sequence: T, to output: inout String, body: (T.Element) -> ()) {
        var first = true
        sequence.forEach {
            if !first { output.append(", ") } else { first = false }
            body($0)
        }
    }
    
    private func writeArgumentsExtension(to output: inout String) {
        for i in parser.argumentNumber.sorted() {
            if i == 0 { continue }
            output.append("extension Function.Producer.A\(i) {\n")
            output.append("    init(")
            writeCommaSeparated(1...i, to: &output) {
                output.append("_ o\($0): Function.A")
            }
            output.append(") {\n")
            for j in 1...i {
                output.append("        self.o\(j) = o\(j)\n")
            }
            output.append("    }\n}\n\n")
        }
    }
    
    private func writeBasicType(name: String, to output: inout String) {
        let native = Swift.protobufTypeToNative[name]
        let nativeName = native ?? name
        output.append("""
            protocol \(nativeName)Producer: _Producer {}
            
            extension Function.A: \(nativeName)Producer {}\n\n
            """)
        if native != nil {
            output.append("""
                extension \(nativeName): \(nativeName)Producer {
                    var producer: Function.A {
                        return Function.A(producer: .\(name)Raw(self))
                    }
                }\n\n
                """)
        }
    }
    
    private func writeFunctionType(name: String, functionType: Parser.FunctionType, to output: inout String) {
        output.append("""
            extension Function.A {
                var \(name): \(name)Producer {
                    return {
            """)
        if functionType.argumentTypes.count > 0 {
            output.append(" ")
            writeCommaSeparated(functionType.argumentTypes.indices, to: &output) {
                output.append("o\($0 + 1)")
            }
            output.append(" in")
        }
        output.append(" Function.A(producer: .\(name)(.init(self")
        for i in functionType.argumentTypes.indices {
            output.append(", o\(i + 1).producer")
        }
        output.append(")))")
        if let returnType = functionType.returnType, case .function = parser.typesMap[returnType]! {
            output.append(".\(returnType)")
        }
        output.append(" }\n    }\n}\n\n")
    }
    
    private func writeTypealias(name: String, functionType: Parser.FunctionType, to output: inout String) {
        // producer
        output.append("typealias \(name)Producer = (")
        writeCommaSeparated(functionType.argumentTypes, to: &output) {
            output.append("\(nativeType(for: $0))Producer")
        }
        if let type = functionType.returnType {
            output.append(") -> \(nativeType(for: type))Producer\n")
        } else {
            output.append(") -> ()\n")
        }
        
        // real
        output.append("typealias \(name) = (")
        writeCommaSeparated(functionType.argumentTypes, to: &output) {
            output.append("\(nativeType(for: $0))")
        }
        if let type = functionType.returnType {
            output.append(") -> \(nativeType(for: type))\n\n")
        } else {
            output.append(") -> ()\n\n")
        }
    }
    
    private func writeSymbol(name: String, functionType: Parser.FunctionType, to output: inout String) {
        output.append("func \(name)(")
        writeCommaSeparated(functionType.argumentTypes.enumerated(), to: &output) {
            output.append("_ o\($0.offset + 1): \(nativeType(for: $0.element))Producer")
        }
        if let type = functionType.returnType {
            output.append(") -> \(nativeType(for: type))Producer {\n    return ")
        } else {
            output.append(") {\n    ")
        }
        output.append("Function.A(producer: .\(name)(.init(")
        writeCommaSeparated(functionType.argumentTypes.enumerated(), to: &output) {
            if case .function? = parser.typesMap[$0.element] {
                output.append("\($0.element)EncodeInternal(o\($0.offset + 1)).producer")
            } else {
                output.append("o\($0.offset + 1).producer")
            }
        }
        output.append(")))")
        if let returnType = functionType.returnType, case .function = parser.typesMap[returnType]! {
            output.append(".\(returnType)")
        }
        output.append("\n}\n\n")
    }
    
{'fraction_non_alphanumeric': 0.1403213465952563, 'fraction_numerical': 0.010558530986993114, 'mean_word_length': 1.868334430546412, 'pattern_counts': {'":': 30, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6304306', 'n_tokens_mistral': 3890, 'n_tokens_neox': 3747, 'n_words': 789}