Hello everyone! I'm hand-writing a "stack" located in runtime function "stack" 
for some CPU-intensive task. What I wish to achieve is something like:
    
    
    proc main =
      # in my case no need for boundary check
      stack = array[10, int]
      p = -1
      # define some operations like PUSH, POP
      PUSH(1)
      let v = POP
    
    Run

In C both `PUSH` and `POP` can be easily defined by C macro. But in Nim I'm 
struggling to get this syntax work, especially for `POP`. If I use template:
    
    
    template POP =
      stack[p]; dec p
    
    Run

it fails to compile because `stack[p]` is regarded as a statement (if I 
understand correctly) and its return value has to be discarded.

I finally come up with the inline proc workaround:
    
    
    proc POP(stack=stack, p:var int = p):int {. inline .} =
      result = stack[p]
      dec p
    
    Run

which I think isn't some really nice code. So my question is, is there a way to 
achieve the "mechanical substitution" like C macro in Nim? Or are there better 
solutions for my problem here? Thank you in advance!

Reply via email to