Re: How to achieve "mechanical substitution" like C macro

2018-12-29 Thread juancarlospaco
Term Rewriting Templates?.

Re: How to achieve "mechanical substitution" like C macro

2018-12-29 Thread moerm
You are welcome. Plus another side note: Nim templates _are_ (what C calls) macros albeit pimped up and cleaner. One important point to remember is that templates (unlike Nim macros) are basically but smart text substitution. This also means that any variables one refers to must either be defin

Re: How to achieve "mechanical substitution" like C macro

2018-12-29 Thread liwt31
Exactly what I wanted. I've tried this before: template POP: int = result = stack[p] dec p Run and realized it won't work, then I went in the wrong direction (to put it in one line)... That solves my probem, many thanks to you all.

Re: How to achieve "mechanical substitution" like C macro

2018-12-29 Thread mashingan
Did you mean "textual substitution" instead? Also, reading manual about [template](https://nim-lang.org/docs/manual.html#templates) should provide you ideas on how you can do with it.

Re: How to achieve "mechanical substitution" like C macro

2018-12-29 Thread miran
If your `stack` and `p` are defined globally and at the beginning (before `PUSH` and `POP` procs), you can do as simple as: var stack: array[10, int] p = -1 proc PUSH(v: int) = inc p stack[p] = v proc POP: int = result = stack[p]

Re: How to achieve "mechanical substitution" like C macro

2018-12-28 Thread moerm
This should do what you want: template push(val: int) = stack[p] = val p.inc() template pop(): int = p.dec() stack[p] Run The relevant point isn't the stack logic (p++ vs p--) but that a template that is supposed to "return" something is

How to achieve "mechanical substitution" like C macro

2018-12-28 Thread liwt31
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 lik