I tried to clean up some of the gobject macros...
While I still have some strange problems with the real code, I just discovered that this minimal example works fine:
import macros, strutils
macro stringify*(n: expr): string =
result = newNimNode(nnkStmtList, n)
result.add(toStrLit(n))
macro genStrConst*(name, str: static[string]): stmt =
var s = """
const
$1 = "$2"
""" % [name, str]
echo s
result = parseStmt(s)
genStrConst("Nim", "Great")
echo Nim
template callMacro(s: expr) =
genStrConst(s, s)
callMacro("Stefan")
echo Stefan
template callMacro2(s: expr) =
const t = toLower(s[0]) & substr(s, 1)
genStrConst(s, t)
callMacro2("Salewski")
echo Salewski
template callMacro3(s1: expr) =
const s = stringify(s1)
const t = toLower(s[0]) & substr(s, 1)
genStrConst((s), (t))
callMacro3(NoQuotes)
echo NoQuotes
Great Stefan salewski noQuotes
I think this examples does all what is needed for the gobject C macros -- inserting substrings as arguments in larger fixed strings, converting case, and allow unquoted macro arguments as used in C macros. For the latter I used that stringify macro, so that I need not to type "" when I call callMacro3(). Works fine, but do I really have to use my own stringify macro for that, or is there something predefined available?
I think the genStrConst() should be more like:
macro genStrConst*(name, str: static[string]): stmt =
newConstStmt(newIdentNode(name), newStrLitNode(str))
And stringify is just astToStr() afais.
template callMacro3(s1: expr) =
const s = astToStr(s1)
const t = toLower(s[0]) & substr(s, 1)
genStrConst((s), (t))
So all together you get (run me on glot.io)
import macros, strutils
macro genStrConst*(name, str: static[string]): stmt =
newConstStmt(newIdentNode(name), newStrLitNode(str))
genStrConst("Nim", "Great")
echo Nim
template callMacro(s: expr) =
genStrConst(s, s)
callMacro("Stefan")
echo Stefan
template callMacro2(s: expr) =
const t = toLower(s[0]) & substr(s, 1)
genStrConst(s, t)
callMacro2("Salewski")
echo Salewski
template callMacro3(s1: expr) =
const s = astToStr(s1)
const t = toLower(s[0]) & substr(s, 1)
genStrConst((s), (t))
callMacro3(NoQuotes)
echo NoQuotes