Unfortunately the latest GTK C examples like to use these C macros...
An offsetof() template seems to be available at
https://github.com/dom96/ParticleBench/blob/master/N.nim
template offsetof(typ, field): expr = (var dummy: typ; cast[int](addr(dummy.field)) - cast[int](addr(dummy)))
For
https://en.wikipedia.org/wiki/C_preprocessor#Token_stringification
I tried something like this, which seems to work well
import macros
macro stringify(n: expr): string =
result = newNimNode(nnkStmtList, n)
result.add(toStrLit(n))
proc show(i: int) =
echo stringify(i) & ": " & $i
var
j: int = 7
s: string
s = stringify(j)
echo s
show(j)
But still I have no clue for
https://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation
From the manual
http://nim-lang.org/docs/manual.html#templates-identifier-construction
I do not think that this can really work
template test(a, b: expr): expr {.immediate.} =
`ab`
var test(x, y) : int
#var xy : int # template should give this
I think in early c2nim manual there was a hint that C ## concatenation symbol was not supported, but I have not seen that remark in latest c2nim docs, so maybe there is a solution now?
[EDIT]
Well, google search in IRC logs gives this:
http://irclogs.nim-lang.org/30-09-2014.html Araq willwillson: c2nim already supports ##
No idea how that may work, for me all the GTK3 defines with ## failed with c2nim?
C2Nim supports token concatenation as part of the c2nim-specific #def preprocessor macro. This means that token concatenation is carried out when the file is translated by c2nim, not when the output file is compiled by the Nim compiler.
Is there any particular reason you need token concatenation as a native macro in Nim?
Is there any particular reason you need token concatenation as a native macro in Nim?
I think offsetof() and token stringification are indeed more important. It is good to know that token concatenation does not exist -- so it seems to be not too important.
I tried to port some of the more recent GTK3 examples to Nim, generally these are only available in C. One is this: https://developer.gnome.org/gtk3/stable/ch01s04.html#id-1.2.3.12.5
And there gtk_widget_class_bind_template_child_private() is used, which calls G_PRIVATE_OFFSET() macro, which is
#define G_PRIVATE_OFFSET(TypeName, field) \ (TypeName##_private_offset + (G_STRUCT_OFFSET (TypeName##Private, field)))
Generally these examples create/extent internal gtk classes, which makes porting to other languages not really easy. There exist only few Python or C++ examples, which are often restricted or outdated. So I think starting direct with the C ones is the best. I did one example this way already, see https://github.com/StefanSalewski/nim-gtksourceview/blob/master/test/test_completion.nim . That was easy, only non trivial task was writing macro g_define_type_extended().
Of course most gtk functionality is available without extending native gtk classes, which is much easier. (For that one can simple follow Python/Ruby examples.)
[EDIT]
Indeed my feeling is, that for the C macro mentioned above http://nim-lang.org/docs/manual.html#templates-identifier-construction should work fine. See
var txy: int = 7
template test(name: expr; n2: expr) {.immediate.} =
echo(`t name n2`)
echo(sizeof(`t name n2`))
test(x, y)
# output is:
# 7
# 8