From 57bfe559eae559cee0b45adc52e9d2617c0a5eca Mon Sep 17 00:00:00 2001 From: isaac <> Date: Tue, 21 Apr 2026 15:56:05 +0400 Subject: [PATCH] SwiftTL: main.swift branches on ParsedSchema Flat schemas keep the existing generate(...) pipeline. Layered schemas iterate resolveLayeredTypes and write one {apiPrefix}Layer{N}.swift per layer via generateLayered. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../SwiftTL/Sources/SwiftTL/main.swift | 101 ++++++++++-------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/build-system/SwiftTL/Sources/SwiftTL/main.swift b/build-system/SwiftTL/Sources/SwiftTL/main.swift index c861eeb6b6..df56ef9afa 100644 --- a/build-system/SwiftTL/Sources/SwiftTL/main.swift +++ b/build-system/SwiftTL/Sources/SwiftTL/main.swift @@ -44,54 +44,63 @@ guard let data = try? String(contentsOfFile: schemeFilePath) else { exit(1) } -do { - let parsedData = try DescriptionParser.parse(data: data) - let resolvedTypes = try Resolver.resolveTypes(constructors: parsedData.constructors) - var resolvedFunctions = try Resolver.resolveFunctions(types: resolvedTypes, functionDescriptions: parsedData.functions) - - resolvedFunctions.append(Resolver.Function(name: QualifiedName(namespace: "help", value: "test"), id: 0xc0e202f7, arguments: [], result: .boxedType(QualifiedName(namespace: nil, value: "Bool")))) - - var constructorOrder: [(typeName: QualifiedName, constructorName: String)] = [] - var typeOrder: [(types: [(typeName: QualifiedName, constructorNames: [String])], functions: [QualifiedName])] = [] - - let sortedTypes = resolvedTypes.sorted(by: { $0.name < $1.name }) +do { + let parsedSchema = try DescriptionParser.parse(data: data) - for type in sortedTypes { - for constructor in type.constructors.values.sorted(by: { $0.name < $1.name }) { - constructorOrder.append((type.name, constructor.name.value)) - } - } - - var totalConstructorCount = 0 - var currentConstructorCount = 0 - for type in sortedTypes { - if typeOrder.isEmpty || currentConstructorCount >= 32 { - typeOrder.append(([], [])) - currentConstructorCount = 0 - } - - typeOrder[typeOrder.count - 1].types.append((type.name, type.constructors.values.sorted(by: { $0.name < $1.name }).map(\.name.value))) - - currentConstructorCount += type.constructors.count - totalConstructorCount += type.constructors.count - - if totalConstructorCount > 40 { - } - } - - typeOrder.append(([], [])) - for function in resolvedFunctions.sorted(by: { $0.name < $1.name }) { - typeOrder[typeOrder.count - 1].functions.append(function.name) - } - try FileManager.default.createDirectory(at: URL(fileURLWithPath: outputDirectoryPath), withIntermediateDirectories: true, attributes: nil) - - let generatedFiles = try CodeGenerator.generate(apiPrefix: apiPrefix, types: resolvedTypes, functions: resolvedFunctions, constructorOrder: constructorOrder, typeOrder: typeOrder) - - for (name, fileData) in generatedFiles { - let filePath = URL(fileURLWithPath: outputDirectoryPath).appendingPathComponent(name).path - let _ = try? FileManager.default.removeItem(atPath: filePath) - try fileData.write(toFile: filePath, atomically: true, encoding: .utf8) + + switch parsedSchema { + case let .flat(constructors, functions): + let resolvedTypes = try Resolver.resolveTypes(constructors: constructors) + var resolvedFunctions = try Resolver.resolveFunctions(types: resolvedTypes, functionDescriptions: functions) + + resolvedFunctions.append(Resolver.Function(name: QualifiedName(namespace: "help", value: "test"), id: 0xc0e202f7, arguments: [], result: .boxedType(QualifiedName(namespace: nil, value: "Bool")))) + + var constructorOrder: [(typeName: QualifiedName, constructorName: String)] = [] + var typeOrder: [(types: [(typeName: QualifiedName, constructorNames: [String])], functions: [QualifiedName])] = [] + + let sortedTypes = resolvedTypes.sorted(by: { $0.name < $1.name }) + + for type in sortedTypes { + for constructor in type.constructors.values.sorted(by: { $0.name < $1.name }) { + constructorOrder.append((type.name, constructor.name.value)) + } + } + + var totalConstructorCount = 0 + var currentConstructorCount = 0 + for type in sortedTypes { + if typeOrder.isEmpty || currentConstructorCount >= 32 { + typeOrder.append(([], [])) + currentConstructorCount = 0 + } + typeOrder[typeOrder.count - 1].types.append((type.name, type.constructors.values.sorted(by: { $0.name < $1.name }).map(\.name.value))) + currentConstructorCount += type.constructors.count + totalConstructorCount += type.constructors.count + if totalConstructorCount > 40 { } + } + + typeOrder.append(([], [])) + for function in resolvedFunctions.sorted(by: { $0.name < $1.name }) { + typeOrder[typeOrder.count - 1].functions.append(function.name) + } + + let generatedFiles = try CodeGenerator.generate(apiPrefix: apiPrefix, types: resolvedTypes, functions: resolvedFunctions, constructorOrder: constructorOrder, typeOrder: typeOrder) + + for (name, fileData) in generatedFiles { + let filePath = URL(fileURLWithPath: outputDirectoryPath).appendingPathComponent(name).path + let _ = try? FileManager.default.removeItem(atPath: filePath) + try fileData.write(toFile: filePath, atomically: true, encoding: .utf8) + } + + case let .layered(layers): + let resolvedLayers = try Resolver.resolveLayeredTypes(layers: layers) + for (layerNumber, types) in resolvedLayers { + let (filename, source) = try CodeGenerator.generateLayered(apiPrefix: apiPrefix, layerNumber: layerNumber, types: types) + let filePath = URL(fileURLWithPath: outputDirectoryPath).appendingPathComponent(filename).path + let _ = try? FileManager.default.removeItem(atPath: filePath) + try source.write(toFile: filePath, atomically: true, encoding: .utf8) + } } } catch let e { print("\(e)")