Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #8334
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Evaluating a string as C++ Code |
| Date | 2011-07-30 17:40 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <99iu05Fa63U1@mid.uni-berlin.de> (permalink) |
| References | <c129d820-b60e-4e46-9608-5fb6ff3e6de8@a12g2000vbf.googlegroups.com> |
Miro <nickbergeronsemail@gmail.com> wrote:
> I come from a background of mostly Perl and Ruby, so C++ is the first
> truly low level programming language that I know. However, I have
> something I'd like to ask the community about. I'm rewriting a BC-
> esque terminal calculator I did in Perl. (You can find it on
> SourceForge as PerlCalc, I believe.), and I've encountered a problem
> that my manual book doesn't cover. In Perl, you can evaluate a string
> as code like this:
> #Getting the string
> my $string = <STDIN>;
> chomp( $string );
> # Evaluate it and print it
> print(eval($string));
> How, pray tell, might I do that in C++?
Simple (and rather conplete) answer: you don't. C+ isn't an
interpreted language, so there's no way to use source code
in a compiled program like you're used to from interpreted
languages. Consider the difference: if you have a Perl script
to run it the perl prgram must be installed on that machine
and it's that what's running (interpreting) your program
while, if you have a program that was created from C++
source code, nothing of C++ (well, beside the run-time li-
brary) must be available. It's a standalone program and
it's more or less immaterial what language it was written
in (there are enough programs that are assembled from parts
that originally were written in different languages, e.g.
C++, C, FORTRAN etc.).
This isn't about high-level versus low-level languages but
it's due to the fundamental difference between interpreted
and compiled languages.
The only thing you can do is write out the code to a file,
compile that file and then run the result from within your
program (using system() or something like that) and read
what that new program outputs. Not simple, hard to impos-
sible to write in a system independent way and requires
that a compiler is installed on the machine that this is
to be done on. And, of course, this new program has no
access at all to variables of the program it was created
from. In short: not a good idea. But then I can count the
number if cases where I have really needed Perl's eval()
(i.e. the 'eval EXPR' kind of Perl's eval) in the last 10
years on the fingers of one hand.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Evaluating a string as C++ Code Miro <nickbergeronsemail@gmail.com> - 2011-07-30 09:52 -0700
Re: Evaluating a string as C++ Code jt@toerring.de (Jens Thoms Toerring) - 2011-07-30 17:40 +0000
Re: Evaluating a string as C++ Code Miro <nickbergeronsemail@gmail.com> - 2011-08-06 13:55 -0700
Re: Evaluating a string as C++ Code jt@toerring.de (Jens Thoms Toerring) - 2011-08-07 00:53 +0000
Re: Evaluating a string as C++ Code Carlo Milanesi <carlo.nospam.milanesi@libero.it> - 2011-07-30 20:08 +0200
Re: Evaluating a string as C++ Code James Kanze <james.kanze@gmail.com> - 2011-08-07 10:26 -0700
Re: Evaluating a string as C++ Code Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-11 21:33 +0000
Re: Evaluating a string as C++ Code Paavo Helde <myfirstname@osa.pri.ee> - 2011-07-30 15:33 -0500
Re: Evaluating a string as C++ Code BGB <cr88192@hotmail.com> - 2011-07-30 14:45 -0700
Re: Evaluating a string as C++ Code jacob navia <jacob@spamsink.net> - 2011-08-12 10:25 +0200
Re: Evaluating a string as C++ Code Juha Nieminen <nospam@thanks.invalid> - 2011-08-12 12:07 +0000
Re: Evaluating a string as C++ Code jacob navia <jacob@spamsink.net> - 2011-08-12 14:31 +0200
Re: Evaluating a string as C++ Code Alexei Zakharov <alexeiz@gmail.com> - 2011-08-18 01:53 -0400
Re: Evaluating a string as C++ Code Juha Nieminen <nospam@thanks.invalid> - 2011-08-18 06:22 +0000
Re: Evaluating a string as C++ Code Paavo Helde <myfirstname@osa.pri.ee> - 2011-08-18 12:26 -0500
Re: Evaluating a string as C++ Code Juha Nieminen <nospam@thanks.invalid> - 2011-08-18 18:13 +0000
Re: Evaluating a string as C++ Code Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-20 14:57 +0000
Re: Evaluating a string as C++ Code Nobody <nobody@nowhere.com> - 2011-08-20 19:35 +0100
Re: Evaluating a string as C++ Code Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-21 19:15 +0000
Re: Evaluating a string as C++ Code Nobody <nobody@nowhere.com> - 2011-08-21 21:50 +0100
csiph-web