Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoiding repeated replacements in the C pre-processor
    text
    copied!<p>I've been hacking on a program that itself creates simulation programs in <strong>C</strong>. The user specifies the top-level design, and this programs inserts small C-fragments and a helluvalot glue-code (a few thousand lines).</p> <p>It does local naming by <code>#defines</code>:</p> <pre><code>#define x local_x #define vx local_vx /* user code that uses x, ex */ vx = x / 2 #undef vx #undef x </code></pre> <p>This approx expands to the following:</p> <pre><code>local_vx = local_x / 2 </code></pre> <p>BUT if I use structs for the <code>local_*</code>-variables (optimize away having to pass 11 variables to every single function...):</p> <pre><code>#define x local-&gt;position.x #define vx local-&gt;velocity.x vx = x / 2 #undef vx #undef x </code></pre> <p>Which gets expanded to</p> <pre><code>local-&gt;velocity.x = local-&gt;position.x </code></pre> <p>And - here's the problem - the <code>x</code> in the velocity gets expanded again:</p> <pre><code>local-&gt;velocity.local-&gt;position.x = local-&gt;position.x </code></pre> <p>I can't put parenthesis around, as it is not allowed to assign to the variables (<code>(x) = 1</code> is illegal <strong>C</strong>, unfortunately...). Any hints?</p> <p><strong>Update:</strong> The generated simulations generally weigh in around 15 to 20.000 LOC, and there are roughly ten years worth of simulations to be backwards compatible with. Alas, <em>simply</em> renaming anything is not simple at all... As there does not seem to be any easy way to get around this particular problem without some major re-engineering (I thought I'd missed some particularities of the <strong>C</strong> pre-processor), I've chosen to take a step back and see what other options I have.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload