From 8d49c0b9108afe1e6d00dd3c31272d96544695e3 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Tue, 15 Aug 2017 10:45:57 +0700 Subject: [PATCH] Register singleton-derived types. Fixes #8 --- godot.nimble | 2 +- godot/godotapigen.nim | 45 +++++++++++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/godot.nimble b/godot.nimble index 5fdb57c..57b2cc0 100644 --- a/godot.nimble +++ b/godot.nimble @@ -1,4 +1,4 @@ -version = "0.5.2" +version = "0.5.3" author = "Xored Software, Inc." description = "Godot Engine bindings" license = "MIT" diff --git a/godot/godotapigen.nim b/godot/godotapigen.nim index ac50246..a77bca9 100644 --- a/godot/godotapigen.nim +++ b/godot/godotapigen.nim @@ -765,14 +765,14 @@ proc shouldExport(typ: GodotType, types: Table[string, GodotType]): bool = curTyp = types.getOrDefault(curTyp.baseName) result = curTyp.isNil -proc genTypeFile(types: var Table[string, GodotType], targetDir: string) = - for typ in types.values(): - types.incDerivedCount(typ.baseName) - - var sortedTypes = toSeq(types.values()) - sortedTypes.sort do (x, y: GodotType) -> int: +proc sortByDerivedCount(types: Table[string, GodotType]): seq[GodotType] = + result = toSeq(types.values()) + result.sort do (x, y: GodotType) -> int: cmp(y.derivedCount, x.derivedCount) +proc genTypeFile(types: Table[string, GodotType], targetDir: string) = + let sortedTypes = sortByDerivedCount(types) + let godotApiTypesTree = newNode(nkStmtList) godotApiTypesTree.add(newNode(nkImportStmt).addChain(ident("godot"))) let typeSection = newNode(nkTypeSection) @@ -793,6 +793,28 @@ proc genTypeFile(types: var Table[string, GodotType], targetDir: string) = renderModule(godotApiTypesTree, targetDir / "godottypes.nim") +proc genSingletonWithDerived(tree: PNode, typ: GodotType, + types: Table[string, GodotType]) = + let sortedTypes = sortByDerivedCount(types) + + var parents = initSet[string]() + parents.incl(typ.name) + + let typeDecl = newNode(nkTypeSection) + tree.add(typeDecl) + proc genDecl(typ: GodotType) = + newRefTypeNode(typeDecl, typ.name, typ.baseName, typ.doc, + isExported = false) + tree.add(newRegisterClassNode(typ)) + + genDecl(typ) + for otherType in sortedTypes: + if otherType.baseName in parents: + parents.incl(otherType.baseName) + genDecl(otherType) + + tree.add(genSingletonDecl(typ.name)) + proc genApi*(targetDir: string, apiJsonFile: string) = ## Generates .nim files in the ``targetDir`` based on the ``apiJsonFile``. ## The JSON file can be generated by executing Godot with @@ -821,6 +843,10 @@ proc genApi*(targetDir: string, apiJsonFile: string) = jsonNode: obj, isSingleton: obj["singleton"].bval ) + + for typ in types.values(): + types.incDerivedCount(typ.baseName) + for typ in types.values(): let moduleName = typeNameToModuleName(typ.name) echo "Generating ", moduleName, ".nim..." @@ -850,12 +876,7 @@ proc genApi*(targetDir: string, apiJsonFile: string) = tree.add(makeConstSection(obj["constants"])) if typ.isSingleton: - let typeDecl = newNode(nkTypeSection) - newRefTypeNode(typeDecl, typ.name, typ.baseName, typ.doc, - isExported = false) - tree.add(typeDecl) - tree.add(newRegisterClassNode(typ)) - tree.add(genSingletonDecl(typ.name)) + genSingletonWithDerived(tree, typ, types) # first, generate declarations only for ease of # human readability of the file