Support default values
This commit is contained in:
parent
c1c1852203
commit
3b89eeb9a7
1 changed files with 29 additions and 9 deletions
|
|
@ -394,7 +394,7 @@ template registerGodotField(classNameLit, classNameIdent, propNameLit,
|
||||||
|
|
||||||
var hintStrGodot = hintStr.toGodotString()
|
var hintStrGodot = hintStr.toGodotString()
|
||||||
{.push warning[ProveInit]: off.} # false warning, Nim bug
|
{.push warning[ProveInit]: off.} # false warning, Nim bug
|
||||||
let attr = GodotPropertyAttributes(
|
var attr = GodotPropertyAttributes(
|
||||||
typ: ord(variantType),
|
typ: ord(variantType),
|
||||||
hint: hint,
|
hint: hint,
|
||||||
hintString: hintStrGodot,
|
hintString: hintStrGodot,
|
||||||
|
|
@ -422,7 +422,7 @@ proc toGodotStyle(s: string): string {.compileTime.} =
|
||||||
proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
|
proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
|
||||||
result = newNimNode(nnkStmtList)
|
result = newNimNode(nnkStmtList)
|
||||||
|
|
||||||
# 1. Nim type definition
|
# Nim type definition
|
||||||
let typeDef = newNimNode(nnkTypeDef)
|
let typeDef = newNimNode(nnkTypeDef)
|
||||||
result.add(newNimNode(nnkTypeSection).add(typeDef))
|
result.add(newNimNode(nnkTypeSection).add(typeDef))
|
||||||
typeDef.add(postfix(ident(obj.name), "*"))
|
typeDef.add(postfix(ident(obj.name), "*"))
|
||||||
|
|
@ -437,20 +437,40 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
|
||||||
|
|
||||||
let recList = newNimNode(nnkRecList)
|
let recList = newNimNode(nnkRecList)
|
||||||
objTy.add(recList)
|
objTy.add(recList)
|
||||||
|
let initBody = newStmtList()
|
||||||
for decl in obj.fields:
|
for decl in obj.fields:
|
||||||
if not decl.defaultValue.isNil and decl.defaultValue.kind != nnkEmpty:
|
if not decl.defaultValue.isNil and decl.defaultValue.kind != nnkEmpty:
|
||||||
parseError(decl.defaultValue,
|
initBody.add(newNimNode(nnkAsgn).add(decl.name, decl.defaultValue))
|
||||||
"Default values are not supported for fields for now.")
|
|
||||||
let name = if not decl.isExported: decl.name
|
let name = if not decl.isExported: decl.name
|
||||||
else: postfix(decl.name, "*")
|
else: postfix(decl.name, "*")
|
||||||
recList.add(newIdentDefs(name, decl.typ))
|
recList.add(newIdentDefs(name, decl.typ))
|
||||||
|
|
||||||
# 2. {.this: self.} for convenience
|
# Add default values to init method
|
||||||
|
if initBody.len > 0:
|
||||||
|
var initMethod: NimNode
|
||||||
|
for meth in obj.methods:
|
||||||
|
if meth.name == "init" and meth.nimNode[3].len == 1:
|
||||||
|
initMethod = meth.nimNode
|
||||||
|
break
|
||||||
|
if initMethod.isNil:
|
||||||
|
obj.methods.add(MethodDecl(
|
||||||
|
name: "init",
|
||||||
|
args: newSeq[VarDecl](),
|
||||||
|
returnType: newEmptyNode(),
|
||||||
|
isVirtual: true,
|
||||||
|
isNoGodot: false,
|
||||||
|
nimNode: newProc(ident("init"), body = initBody,
|
||||||
|
procType = nnkMethodDef)
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
initMethod.body.insert(0, initBody)
|
||||||
|
|
||||||
|
# {.this: self.} for convenience
|
||||||
result.add(newNimNode(nnkPragma).add(newNimNode(nnkExprColonExpr).add(
|
result.add(newNimNode(nnkPragma).add(newNimNode(nnkExprColonExpr).add(
|
||||||
ident("this"), ident("self")
|
ident("this"), ident("self")
|
||||||
)))
|
)))
|
||||||
|
|
||||||
# 3. Nim proc defintions
|
# Nim proc defintions
|
||||||
var decls = newSeqOfCap[NimNode](obj.methods.len)
|
var decls = newSeqOfCap[NimNode](obj.methods.len)
|
||||||
for meth in obj.methods:
|
for meth in obj.methods:
|
||||||
let selfArg = newIdentDefs(ident("self"), ident(obj.name))
|
let selfArg = newIdentDefs(ident("self"), ident(obj.name))
|
||||||
|
|
@ -467,7 +487,7 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
|
||||||
for meth in obj.methods:
|
for meth in obj.methods:
|
||||||
result.add(meth.nimNode)
|
result.add(meth.nimNode)
|
||||||
|
|
||||||
# 4. Register Godot object
|
# Register Godot object
|
||||||
let parentName = if obj.parentName.isNil: newStrLitNode("Object")
|
let parentName = if obj.parentName.isNil: newStrLitNode("Object")
|
||||||
else: newStrLitNode(obj.parentName)
|
else: newStrLitNode(obj.parentName)
|
||||||
let classNameLit = newCStringLit(obj.name)
|
let classNameLit = newCStringLit(obj.name)
|
||||||
|
|
@ -478,7 +498,7 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
|
||||||
registerGodotClass(classNameIdent, classNameLit, isRef, parentName,
|
registerGodotClass(classNameIdent, classNameLit, isRef, parentName,
|
||||||
genSym(nskProc, "createFunc"), obj.isTool)))
|
genSym(nskProc, "createFunc"), obj.isTool)))
|
||||||
|
|
||||||
# 5. Register fields (properties)
|
# Register fields (properties)
|
||||||
for field in obj.fields:
|
for field in obj.fields:
|
||||||
if field.isNoGodot: continue
|
if field.isNoGodot: continue
|
||||||
let hintStr = if field.hintStr.isNil: "NIM"
|
let hintStr = if field.hintStr.isNil: "NIM"
|
||||||
|
|
@ -501,7 +521,7 @@ proc genType(obj: ObjectDecl): NimNode {.compileTime.} =
|
||||||
newStrLitNode(hintStr), hintIdent, usageIdent,
|
newStrLitNode(hintStr), hintIdent, usageIdent,
|
||||||
ident($hasDefaultValue), field.defaultValue)))
|
ident($hasDefaultValue), field.defaultValue)))
|
||||||
|
|
||||||
# 6. Register methods
|
# Register methods
|
||||||
template registerGodotMethod(classNameLit, classNameIdent, methodNameIdent,
|
template registerGodotMethod(classNameLit, classNameIdent, methodNameIdent,
|
||||||
methodNameLit, minArgs, maxArgs,
|
methodNameLit, minArgs, maxArgs,
|
||||||
argTypes, methFuncIdent, hasReturnValue) =
|
argTypes, methFuncIdent, hasReturnValue) =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue