Macro or eval() for constants at runtime?

M

Mark Richards

Perhaps this has been covered elsewhere, but can't find a reference to
it in FAQ and through diligent searching.

In some languages, it's possible to perform a runtime eval() that
essentially constructs a constant or other components of a statement and
executes it, returning the result. This is very handy in cases where
putting repetitive operations in a nice clean loop is an advantage.

Here's an example of a problem I'm working on...

The user inputs a range of ports that will be forwarded in the U/I of an
iptables management system I'm building. iptables doesn't accomodate
port range in forwarding, therefore you have to build a new statement
for each port.

The architecture, for reasons of sanity, allows 6 total forwarding input
lines and each holds a label, the to port, the from port (range), and
the target ip to forward to. Each variable is held in an array and
referenced by a pre-established constant.

Rather than write out each statement discretely, I'd rather put
processing for variables 1-6 in a loop and therefore need to reference
each constant within the loop:

for (i=0;i<7;i++)
{
for (j=nStartPort;j<=nEndPort;j++)
{
aVar[PORT_START_(j)] = GetStartPort(j);
aVar[PORT_END_(j)] = GetEndPort(j);
}
}

GetStartPort and GetEndPort return an integer based on the users entry
of port range for the entry line (i) that we are currently working.

I'd rather do this:

for (i=0;i<7;i++)
{
for (j=nStartPort;j<=nEndPort;j++)
{
sprintf(cString,"aVar[PORT_START_%i]");
eval(cString) = GetStartPort(j);
...
}
}

Or something equivalent, where eval(..) would (I guess) act as a pointer
to the variable being referenced.

Now that I write, I realize I could (perhaps) reference a pointer to
each variable in an array and point to that, but it *may* take some of
the modularity and flexibility I hoped to achieve away.

Well, am I barking up the wrong tree here? Maybe there's another way to
do this?
 
J

Joona I Palaste

Mark Richards said:
Perhaps this has been covered elsewhere, but can't find a reference to
it in FAQ and through diligent searching.
In some languages, it's possible to perform a runtime eval() that
essentially constructs a constant or other components of a statement and
executes it, returning the result. This is very handy in cases where
putting repetitive operations in a nice clean loop is an advantage.

This can't be done in C, or in any other purely compiled language for
that matter. The primary reason is that all local variable names are
lost in compilation and replaced by pure addresses. I suggest you
implement your own interpreter instead.
 
M

Mark Richards

Joona said:
This can't be done in C, or in any other purely compiled language for
that matter. The primary reason is that all local variable names are
lost in compilation and replaced by pure addresses. I suggest you
implement your own interpreter instead.
Thanks Joona. I think I'll hold on the interpreter idea. Maybe for
another day :)

-m-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top