473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Macro or eval() for constants at runtime?

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?
Nov 14 '05 #1
2 1597
Mark Richards <no****@massmicro.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"No, Maggie, not Aztec, Olmec! Ol-mec!"
- Lisa Simpson
Nov 14 '05 #2
Joona I Palaste wrote:

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-
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
11
by: sneill | last post by:
I have read a number of posts on the use of eval() in Javascript, and I agree that its use is questionable. But it does beg the following question: "How arbitrary does a string need to be before...
13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
17
by: ethan | last post by:
Hi All, How to write a macro with variable length of argument? For example, if i need a macro to check return value of printf. #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end; ...
5
by: rachelm81 | last post by:
Hello, all! I'm creating a macro for help with some c/c++ projects, and within it, I'm trying to execute Find/Replace. Actually, it's executing fine. It's closing the prompt that I'm having a problem...
1
by: Benne Smith | last post by:
I can't seem to use my normal compiler constants from inside a macro ? It's possible to define NEW constants inside the macro, but this is not what i need. Does anybody know a way to get to...
11
by: Richard Meister | last post by:
Hi, I'd like to define several constants and make sure that all of them are smaller than a given other constant. I thought this could be done by a simple macro. Something like this: #define...
17
by: Francois Grieu | last post by:
Consider this macro // check if x, assumed of type unsigned char, is in range #define ISVALID(x) ((x)>=0x20 && (x)<=0x7E) Of course, this can't be safely used as in if (ISVALID(*p++)) foo();...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.