Skip to content

Commit

Permalink
More work on default arguments.
Browse files Browse the repository at this point in the history
Over 176 APIs that had declared default values but were missing are
now covered.  Color, Rect*, Vector*, Transform* are now handled as
well as assorted default arguments that were nil.

API changes:

https://gist.github.com/migueldeicaza/0eefb83b81b72ca47ebb3bf339ad5f8c
  • Loading branch information
migueldeicaza committed Nov 29, 2023
1 parent 4084ae6 commit f905de8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
4 changes: 0 additions & 4 deletions Generator/Generator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
492B8AF229D12CF00083C150 /* ClassGen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 492B8AF129D12CF00083C150 /* ClassGen.swift */; };
494DF6A629F080C500485C49 /* Printer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 494DF6A529F080C500485C49 /* Printer.swift */; };
4982B71929CE3296008D2CCD /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4982B71829CE3296008D2CCD /* main.swift */; };
4982B72029CE33DA008D2CCD /* ApiJsonModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4982B71F29CE33DA008D2CCD /* ApiJsonModel.swift */; };
4982B72229CE355A008D2CCD /* StringOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4982B72129CE355A008D2CCD /* StringOperations.swift */; };
49C9403629CF9E2F006CC78F /* TypeHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49C9403529CF9E2F006CC78F /* TypeHelpers.swift */; };
49CF7E2529DDBB6600C7EE35 /* Arguments.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49CF7E2429DDBB6600C7EE35 /* Arguments.swift */; };
Expand Down Expand Up @@ -43,7 +42,6 @@
494DF6A529F080C500485C49 /* Printer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Printer.swift; sourceTree = "<group>"; };
4982B71529CE3296008D2CCD /* Generator */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Generator; sourceTree = BUILT_PRODUCTS_DIR; };
4982B71829CE3296008D2CCD /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
4982B71F29CE33DA008D2CCD /* ApiJsonModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApiJsonModel.swift; sourceTree = "<group>"; };
4982B72129CE355A008D2CCD /* StringOperations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringOperations.swift; sourceTree = "<group>"; };
49C9403529CF9E2F006CC78F /* TypeHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeHelpers.swift; sourceTree = "<group>"; };
49CF7E2429DDBB6600C7EE35 /* Arguments.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Arguments.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -83,7 +81,6 @@
children = (
4982B71829CE3296008D2CCD /* main.swift */,
494DF6A529F080C500485C49 /* Printer.swift */,
4982B71F29CE33DA008D2CCD /* ApiJsonModel.swift */,
4982B72129CE355A008D2CCD /* StringOperations.swift */,
49C9403529CF9E2F006CC78F /* TypeHelpers.swift */,
492B8AEF29D12C440083C150 /* BuiltinGen.swift */,
Expand Down Expand Up @@ -163,7 +160,6 @@
files = (
49CF7E2929DE12F400C7EE35 /* DocModel.swift in Sources */,
49008AB12A12794100F2B874 /* MethodGen.swift in Sources */,
4982B72029CE33DA008D2CCD /* ApiJsonModel.swift in Sources */,
49008AAF2A11CC0D00F2B874 /* UtilityGen.swift in Sources */,
490DA41C29F083460093E32B /* Enums.swift in Sources */,
492B8AF229D12CF00083C150 /* ClassGen.swift in Sources */,
Expand Down
88 changes: 72 additions & 16 deletions Generator/Generator/Arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,29 @@ func getArgumentDeclaration (_ argument: JGodotArgument, eliminate: String, kind

var def: String = ""
if let dv = argument.defaultValue, dv != "" {
func dvMissing (_ kind: String) {
print ("Generator/default_value: no support for [\(kind)] = \(dv)")
}

let argumentType = argument.type

// This method is useful to customize the output of a call to a constructor
// it splits out the arguments into an array of strings, calls the constructor
// and then puts together the constructor invocation
func makeWith (_ callback: ([String]) -> String) -> String {
let values = String (dv [dv.firstIndex(of: "(")!...].dropFirst ().dropLast()).split (separator: ", ").map { String ($0) }
let res = callback (values)
return " = \(argumentType) (\(res))"
}
// Given a dv of "Vector (1,0)" returns a SwiftGodot suitable "Vector(x: 1, y: 0)" based on the
// args value that contains the desired labels
func makeDef (_ args: [String]) -> String {
// Turn a string like 'Vector2(0, 1)' into the array of values ["0", "1"]
makeWith { values in
return zip (args, values).map { v in String ("\(v.0): \(v.1)") }.joined (separator: ", ")
}
}

// TODO:
// - handle creating initializers from enums (builtint)
// - empty arrays
Expand All @@ -49,7 +71,7 @@ func getArgumentDeclaration (_ argument: JGodotArgument, eliminate: String, kind
def = " = \(ev)"
}
} else if argumentType == "Variant" {
// Not supported
dvMissing ("Variant")
} else {
def = " = \(dv)"
}
Expand All @@ -58,21 +80,22 @@ func getArgumentDeclaration (_ argument: JGodotArgument, eliminate: String, kind
// above, as the do-not-run conditions are becoming large and difficult
// to parse - they were fine to bootstrap, but not for the long term.

// Handle empty type arrays
if argumentType.starts(with: "typedarray::") {
if dv == "[]" {
def = " = \(getGodotType(argument, kind: kind)) ()"
} else {
// Tracked: https://github.com/migueldeicaza/SwiftGodot/issues/7
//print ("Generator: \(argumentType) support for default value: \(dv)")
}
} else if argumentType == "Dictionary" {
switch argumentType {
// Handle empty type arrays
case let at where at.hasPrefix("typedarray::"):
if dv == "[]" {
def = " = \(getGodotType(argument, kind: kind)) ()"
} else {
// Tracked: https://github.com/migueldeicaza/SwiftGodot/issues/7
dvMissing (argumentType)
}
case "Dictionary":
if dv == "{}" {
def = " = GDictionary ()"
} else {
print ("Generator: \(argumentType) missing support for default value: \(dv)")
dvMissing (argumentType)
}
} else if argumentType.starts(with: "bitfield::") {
case let bt where bt.hasPrefix("bitfield::"):
if let defIntValue = Int (dv) {
if defIntValue == 0 {
def = " = []"
Expand All @@ -93,18 +116,51 @@ func getArgumentDeclaration (_ argument: JGodotArgument, eliminate: String, kind
}
def = " = [\(setValues)]"
} else {
print ("Generator: \(argumentType) could not produce default value for \(dv) because I can not find the type")
dvMissing ("\(argumentType) due to not being able to lookup the type")
}
}
} else {
print ("Generator: bitfield:: with a non-integer default value")
dvMissing ("bitfield:: with a non-integer default value")
}
} else if argumentType == "Array" {
case "Array":
if dv == "[]" {
def = " = GArray ()"
} else {
// Tracked: https://github.com/migueldeicaza/SwiftGodot/issues/7
//print ("Generator: no support for arrays with values: \(dv)")
dvMissing ("arrays with values")
print ("Generator: no support for arrays with values: \(dv)")
}
case "Vector2":
def = makeDef (["x", "y"])
case "Vector2i":
def = makeDef (["x", "y"])
case "Vector3":
def = makeDef (["x", "y", "z"])
case "Color":
def = makeDef (["r", "g", "b", "a"])
case "Rect2":
def = makeDef (["x", "y", "width", "height"])
case "Rect2i":
def = makeDef (["x", "y", "width", "height"])
case "Transform2D":
def = makeWith { a in
return "xAxis: Vector2 (x: \(a[0]), y: \(a[1])), yAxis: Vector2 (x: \(a[2]), y: \(a[3])), origin: Vector2 (x: \(a[4]), y: \(a[5]))"
}
case "NodePath":
def = " = \(dv)"
case "Transform3D":
def = makeWith { a in
return "xAxis: Vector3 (x: \(a[0]), y: \(a[1]), z: \(a[2])), yAxis: Vector3 (x: \(a[3]), y: \(a[4]), z: \(a[5])), zAxis: Vector3(x: \(a[6]), y: \(a[7]), z: \(a[8])), origin: Vector3 (x: \(a[9]), y: \(a[10]), z: \(a[11]))"
}
default:
if dv == "null" {
if argumentType == "Variant" {
def = " = Variant ()"
} else {
def = " = nil"
}
} else {
dvMissing ("General \(argumentType)")
}
}
}
Expand Down

0 comments on commit f905de8

Please sign in to comment.