The C have eval function?

S

sonet

Many Language have eval function.The C have eval function?
How to execute c code in c ?
Can i execute c code from a variable(like perl's code ref)?
Or i must do this job by using yacc & lex ?
All of the non-interpreted languages have no eval function?
 
I

Ian Collins

sonet said:
Many Language have eval function.The C have eval function?
No.

How to execute c code in c ?

Compile it and run the result.
Can i execute c code from a variable(like perl's code ref)?
No.

Or i must do this job by using yacc & lex ?
?

All of the non-interpreted languages have no eval function?
Probably
 
R

Richard Heathfield

sonet said:
Many Language have eval function.The C have eval function?

Not unless you write one.
How to execute c code in c ?

Write a C interpreter in C, or use an existing one.
Can i execute c code from a variable(like perl's code ref)?

C has no syntax for this. Nor does a typical implementation provide this
functionality as an extension.
Or i must do this job by using yacc & lex ?

You /could/ do it that way, yes.
All of the non-interpreted languages have no eval function?

C doesn't mandate either compilation or interpretation - only
translation - but yes, it is generally regarded as a "compiled
language" - i.e. a language for which typical implementations are
compilers rather than interpreters (although C interpreters do exist).
And yes, compilers do tend to throw away the symbol table once they're
done with it, making eval an impractical, and perhaps impossible, task.
 
C

Chris Dollin

sonet wrote:

(fx:foreshadowing "important question")
Many Language have eval function.The C have eval function?

Standard C has no built-in `eval` function.
How to execute c code in c ?

You'd have to do it in a non-portable way, such as using `system`
to run the C compiler, generate a loadable library, and then
system magic to load and execute that library. (fx:mumble)
POSIX, comp.unix.programmer.
Can i execute c code from a variable(like perl's code ref)?
No.

Or i must do this job by using yacc & lex ?

That would mean writing a C compiler. That's a lot of hard
work. Don't go there. (Someone else might have gone there,
or somewhere like it.)
All of the non-interpreted languages have no eval function?

There are compiled languages with an eval function; Common Lisp and
Poplog's Pop11, to name two.

(Depending on exactly what you mean by `compiled`. The Pop11 compiler
compiles on-the-fly to native code. So does at least one Common
Lisp.)

Important Question: /why do you want to know/? Is it mere curiosity,
or do you have an actual problem you want to solve? In that case,
telling us about that problem might get you a more useful answer.
 
S

sonet

Sorry my english is very poor!

In perl we can do this.

$a=1;
$coderef=qq{
\$b=\$a?1:0;
};
eval $coderef;
# The result is 1
print $b;
=====================================================================
The tcsh(c shell) is like the eval function. We can write a piece of code ,
and execute the
code.
The eval is very useful. I can prepare any code outside the program. If the
program is a filter
process. It can add any filter case to the program. And the filter case have
logic not only
normal data.
 
D

dan.barbus

Many Language have eval function.The C have eval function?
How to execute c code in c ?
Can i execute c code from a variable(like perl's code ref)?
Or i must do this job by using yacc & lex ?
All of the non-interpreted languages have no eval function?

There is no equivalent of eval in C. That is because in C the code has
t be known at compile time to be ... well ... compiled. In python/perl/
javascript, you have the code evaluated at runtime.
 
F

Flash Gordon

sonet wrote, On 22/05/07 11:56:
Sorry my english is very poor!

<snip>

Please do not top post, even when replying to yourself. Your reply
belongs under or interspersed with the message you are replying to. See
all the posts replying to you for examples.

Everyone understood what you meant when you referred to an eval
function, what did you not understand about the responses?
 
F

Francine.Neary

sonet said:


Not unless you write one.


Write a C interpreter in C, or use an existing one.


C has no syntax for this. Nor does a typical implementation provide this
functionality as an extension.

Probably in most implementations you can execute such code by cleverly
using the variable to overflow a buffer, possibly passing to inline
assembler to mess around with stack pointers or whatever... but any
such solution seems doomed to be non-portable and require assumptions
beyond those that can be justified from the C standard.
 
K

Keith Thompson

sonet said: [...]
Can i execute c code from a variable(like perl's code ref)?

C has no syntax for this. Nor does a typical implementation provide this
functionality as an extension.

Probably in most implementations you can execute such code by cleverly
using the variable to overflow a buffer, possibly passing to inline
assembler to mess around with stack pointers or whatever... but any
such solution seems doomed to be non-portable and require assumptions
beyond those that can be justified from the C standard.

A more plausible way to do that might be to build a buffer containing
the executable code, then somehow construct a function pointer from
the buffer's address. But that's still horribly non-portable.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top