I'm trying to change the type of integer literals in a const block using a 
macro. The macro looks like this:
    
    
    import macros
    
    macro uint32_const_type(s: stmt): stmt =
        proc walk(s: NimNode) {. compiletime .} =
            case s.kind:
                of nnkStmtList:
                    for node in s:
                        walk(node)
                of nnkConstSection:
                    walk(s[0])
                of nnkConstDef:
                    let new_node = newNimNode(nnkUInt32Lit)
                    new_node.intVal = s[2].intVal
                    s[2] = new_node
                else:
                    discard
        walk(s)
        result = s
    

compiling and running the following program, which uses the above macro 
    
    
    import typetraits
    
    uint32_const_type:
        const
            a = 0
    
    const
        b = 0'u32
    
    const
        c = 0
    
    echo a.type.name
    echo b.type.name
    echo c.type.name
    

results in this output (which is not what I was expecting): 
    
    
    $ nim c -r const_macro.nim
    int
    uint32
    int
    

dumping the tree and using repr before and after the call to _walk_ in the 
macro produces the following: 
    
    
    ConstSection
      ConstDef
        Sym "a"
        Empty
        IntLit 0
    const
      a = 0
    ConstSection
      ConstDef
        Sym "a"
        Empty
        UInt32Lit
    const
      a = 0'u32
    

which would lead me to expect that the output I would get from running the 
above program should be: _uint32_, _uint32_, _int_?. Should this work, or am I 
doing something wrong in the definition of the macro? I am pretty new to Nim, 
so feel free to let me know if I'm doing anything stupid ;)

* * *

Why? I was trying to mangle c2nim's output to make a bunch of const literals 
(generated from c defines) unsigned by being lazy and doing this:
    
    
    uint32_const_type:
        include c2nimmed_file
    

for various low-level hardware related header files.

Reply via email to