-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nim
109 lines (83 loc) · 3.05 KB
/
main.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import std/[strformat, strutils, tables, sugar]
import pkg/[sunny]
import ./types
const
BuildConfig = Float64
RenameEnum = {
"MIDIMessage": "MidiMessage"
}.toTable
RenameEnumValues = {
"ClockDirection": {
"Counterclockwise": "CounterClockwise"
}.toTable
}.toTable
RenameEnumValuesCallbacks = {
"EulerOrder": proc(name: string): string = name.toUpperAscii,
"Error": proc(name: string): string = (if name.startsWith("Err"): name[3..^1] else: name),
"PropertyUsageFlags": proc(name: string): string = (if name.startsWith("PropertyUsage"): name[13..^1] else: name),
"MethodFlags": proc(name: string): string = (if name.startsWith("MethodFlag"): name[10..^1] else: name),
"VariantType": proc(name: string): string = (if name.startsWith("Type"): name[4..^1] else: name),
"VariantOperator": proc(name: string): string = (if name.startsWith("Op"): name[2..^1] else: name)
}.toTable
var api = GDExtApi.fromJson(readFile("extension_api.json"))
func numCharAsStr(c: char): string =
case c
of '0': "Zero"
of '1': "One"
of '2': "Two"
of '3': "Three"
of '4': "Four"
of '5': "Five"
of '6': "Six"
of '7': "Seven"
of '8': "Eight"
of '9': "Nine"
else: ""
func normalizeEnumName(name, enumName: string): tuple[name: string, shouldPrefix: bool] =
var nm = name.split('_')
for i in 0..nm.high:
nm[i][1..^1] = nm[i][1..^1].toLowerAscii
var shouldPrefix = false
var valName = (if enumName == nm.join(""):
shouldPrefix = true
nm.join("")
else:
nm.join("").replace(enumName, ""))
if valName.len == 1 and valName[0].isDigit: valName = numCharAsStr(valName[0])
(name: valName, shouldPrefix: shouldPrefix)
proc generateGlobalEnums(api: GDExtApi): string =
for enm in api.enums:
let enumName = RenameEnum.getOrDefault(enm.name, enm.name.replace(".", ""))
result &= &" {enumName}* = enum\n"
var prefix = ""
prefix &= enumName[0]
for c in enumName.toOpenArray(1, enumName.high):
if c.isUpperAscii:
prefix &= c.toLowerAscii
var enums: OrderedTable[int64, string]
for val in enm.values:
let normalizedName = normalizeEnumName(val.name, enumName)
var valName =
if enumName in RenameEnumValues:
RenameEnumValues[enumName].getOrDefault(normalizedName.name, normalizedName.name)
else:
normalizedName.name
if enumName in RenameEnumValuesCallbacks:
valName = RenameEnumValuesCallbacks[enumName](valName)
if normalizedName.shouldPrefix: valName = prefix & valName
enums[val.value] = &"{valName} = {val.value}"
enums.sort(cmp)
for str in enums.values: result &= &" {str}\n"
result &= '\n'
proc generate(api: GDExtApi): string =
result = &"""## AUTOGENERATED, DO NOT EDIT ##
const
GodotMajor* = {api.header.major}
GodotMinor* = {api.header.minor}
GodotPatch* = {api.header.patch}
GodotStatus* = {dup("", addQuoted(api.header.status))}
GodotBuild* = {dup("", addQuoted(api.header.build))}
type
"""
result &= generateGlobalEnums(api)
writeFile("gdext.nim", generate(api))