I am trying to write a macro:
macro genModelInstance*( NUMBER_OF_REALS, NUMBER_OF_INTEGERS,
NUMBER_OF_BOOLEANS, NUMBER_OF_STRINGS,
NUMBER_OF_STATES, NUMBER_OF_EVENT_INDICATORS: static[int]): untyped =
#echo NUMBER_OF_REALS # just an int (7), not ``NimNode``
result = quote do:
type
ModelInstance* = ref object
r*: array[`NUMBER_OF_REALS`, fmi2Real]
i*: array[`NUMBER_OF_INTEGERS`, fmi2Integer]
b*: array[`NUMBER_OF_BOOLEANS`, fmi2Boolean]
s*: array[`NUMBER_OF_STRINGS`, fmi2String]
isPositive*: array[`NUMBER_OF_EVENT_INDICATORS`, fmi2Boolean]
time*: fmi2Real
instanceName*: fmi2String
`type`*: fmi2Type
....
The question is about the last line. I want to name the field as type which requires backquotes: `type` so that it is not confused with a type definition. But the backquotes in the macro refer to macro arguments.
Is there a way to fix this?
I am following that approach:
macro genModelInstance*( NUMBER_OF_REALS, NUMBER_OF_INTEGERS,
NUMBER_OF_BOOLEANS, NUMBER_OF_STRINGS,
NUMBER_OF_STATES, NUMBER_OF_EVENT_INDICATORS: static[int]): untyped =
let modelInstance = nnkPostfix.newTree(
newIdentNode("*"),
newIdentNode("ModelInstance")
)
let mytype = nnkPostfix.newTree(
newIdentNode("*"),
nnkAccQuoted.newTree( newIdentNode("type") )
)
result = quote do:
type
`modelInstance` = ref object
r*: array[`NUMBER_OF_REALS`, fmi2Real]
i*: array[`NUMBER_OF_INTEGERS`, fmi2Integer]
b*: array[`NUMBER_OF_BOOLEANS`, fmi2Boolean]
s*: array[`NUMBER_OF_STRINGS`, fmi2String]
isPositive*: array[`NUMBER_OF_EVENT_INDICATORS`, fmi2Boolean]
time*: fmi2Real
instanceName*: fmi2String
`mytype`: fmi2Type
...
But for some reason I am getting:
type
ModelInstance = ref object
r*: array[3, fmi2Real]
i*: array[2, fmi2Integer]
b*: array[1, fmi2Boolean]
s*: array[4, fmi2String]
isPositive*: array[6, fmi2Boolean]
time*: fmi2Real
instanceName*: fmi2String
`type`*: fmi2Type
...
where I get ModelInstance instead of ModelInstance*.
@arak you are absolutely right. I need to reorganize the whole code and to get closer to the style guide. It is just copied from the original C code.