Hmm, looks like you have quite a bit to understand.

Your macro line is also indented too much, it should be on the same indentation 
level as the type declaration.

This line:
    
    
    echo """{name} = enum\n  "
    
    
    Run

needs to be this:
    
    
    echo "{name} = enum\n  "
    
    
    Run

because Nim will get confused by the first two quotes and treat the rest of the 
file as a string. See 
[here](https://nim-lang.org/docs/tut1.html#lexical-elements-string-and-character-literals)
 for more info.

Your are also trying to write string interpolation without the `strformat` 
module imported and the `fmt` call before it. For example, the above should be:
    
    
    #top of file
    import strformat
    
    # ... your other code
        
        echo fmt"{name} = enum" # the \n is unnecessary because echo already 
ends with a newline by default
    
    # ... your other code
    
    
    Run

This is because Nim doesn't support string interpolation this way by default 
and needs a template called fmt that implements it.

So after all those your code then becomes (with some nice formatting for ease 
of reading):
    
    
    import strformat # This needs to be imported to use fmt"{variable}" in 
strings
    
    type
      progress_specenum_el = object
        name: cstring
        value: string
    
    # This needs to be indented and have a star (*) after the macro name to 
export it to another module
    macro generate_specenum*(name: string; elements: 
openarray[progress_specenum_el]): untyped =
      echo fmt"{name} = enum"
      echo "BAD,"
      
      for a in elements:
        echo fmt"{a.value},"
      
      echo "\n"
      echo fmt"proc string_to_{name}(rname: string): {name} ="
      echo fmt"  const map: seq[progress_specenum_el] = @[repr({elements})]"
      echo fmt"  var b: {name} = low(b)"
      echo fmt"  for a in map:"
      echo fmt"    if a.name == rname:"
      echo fmt"      return b"
      echo fmt"    b=b.succ()"
      echo fmt"  return BAD"
    
    
    Run

But, this still will not compile because macros interpret some arguments as 
`NimNode` instead of what they actually are (not sure if it is a bug or 
feature), so your `openarray[progress_specenum_el]` just becomes a `NimNode` 
and your for loop cannot iterate over it.

And after that is fixed, your code will run but only print out what you want it 
to do (and not actually generate anything) because `echo` simply prints values 
to the console.

I was trying to interpret what you are trying to do, and it looks like you are 
trying to make a macro that generates an enum and a proc that uses that enum. 
That can definitely be done but requires a greater understanding of the 
language and how macros work. I could give you a solution, but that would 
hardly help you learn.

A good starting point for macros might be 
[here](https://flenniken.net/blog/nim-macros/), since it goes over some simple 
macros and how to generate code. Feel free to update your code based on this, 
and post here if you have more trouble finding a solution.

Reply via email to