Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Evaluating a string as C++ Code

Reply
Thread Tools

Evaluating a string as C++ Code

 
 
Miro
Guest
Posts: n/a
 
      07-30-2011
Hello, Usenet.

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++?

Thanks for your help,
Miro
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-30-2011
Miro <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Carlo Milanesi
Guest
Posts: n/a
 
      07-30-2011
On 30/07/2011 18:52, Miro wrote:
> 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));


You can save the string to a file, launch a C++ compiler to generate the
corresponding executable program file, launch the generated executable
program, giving to that program the required input, and probably
receiving from that program the result.
Of course the target platform needs to have a C++ compiler installed
(but you already knew that to run a Perl program the target platform
needs to have a Perl interpreter installed).
Communication between the main program and the auxiliary program may be
performed through:
- command line and return code (command line is a string and return code
a byte)
- unnamed pipes
- named pipes
- temporary files (deprecated, but more portable)
- other IPC
- networking

An alternative (and probably better) solution is to evaluate your string
with a built-in interpreter for your language, that is probably way
simpler than C++.
There are many open-source embeddable interpreters for simple languages.

--

Carlo Milanesi
http://carlomilanesi.wordpress.com/
 
Reply With Quote
 
BGB
Guest
Posts: n/a
 
      07-30-2011
On 7/30/2011 1:33 PM, Paavo Helde wrote:
> Miro<(E-Mail Removed)> wrote in news:c129d820-b60e-4e46-
> (E-Mail Removed):
>
>> Hello, Usenet.
>>
>> 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++?

>
> You can't do that; what you can do is to write an expression interpreter
> in C++ which evaluates the string. A lot of such have been already
> written; for arithmetic expressions I think libformulc is freely
> available; so is libperl, coming to think of it...
>


one can also use a VM for a more capable and "similar" language, such as
JavaScript.

implementations like SpiderMonkey and V8 are relatively well-known and
relatively mature (not perfect though, but maybe good enough).

some people have also used CINT and TinyC.
there is also LLVM / CLang.


in my case, I have my own scripting VM and language (BGBScript, which is
currently mostly like JavaScript and ActionScript, but includes some
more Java-like and C-like syntax extensions, and puts some effort in
trying to have relatively transparent cross-language interop), but sadly
it is a bit far from being a mature solution.

 
Reply With Quote
 
Miro
Guest
Posts: n/a
 
      08-06-2011
On Jul 30, 9:40*pm, (E-Mail Removed) (Jens Thoms Toerring) wrote:
> Miro <(E-Mail Removed)> wrote:
> > I come from a background of mostly Perl and Ruby, soC++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 astring
> > ascodelike this:
> > #Getting thestring
> > my $string= <STDIN>;
> > chomp( $string);
> > # Evaluate it and print it
> > print(eval($string));
> > How, pray tell, might I do that inC++?

>
> Simple (and rather conplete) answer: you don't. C+ isn't an
> interpreted language, so there's no way to use sourcecode
> 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 fromC++
> sourcecode, nothing ofC++(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 thecodeto 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 *___ * * *(E-Mail Removed)
> * *\__________________________ * * *http://toerring.de


Thanks, this group as a whole has probably given me the sanest and
soberest sounding answer I've heard all over the internet.
I'll probably just, as suggested, keep it in Perl or some other
scripting language and do the rest in CPP.

Miro
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      08-07-2011
Miro <(E-Mail Removed)> wrote:
> I'll probably just, as suggested, keep it in Perl or some other
> scripting language and do the rest in CPP.


That's a very sound descision: use the best tool for the job
you've got to get done and don't get stuck in "that's-better-
than-something-else" for purely ideological reasons. If you
need something like Perl's 'eval EXPR' then C or C++ isn't
where you will find it (without a lot of pain) so use Perl
(or some other interpreted language supplying the required
functionality). If you've got the chance to mix interpreted
and compiled languages so that you can pick the best of both
worlds don't hesitate unless for good reasons.

Regards, Jens
--
\ Jens Thoms Toerring ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-07-2011
On Jul 30, 7:08*pm, Carlo Milanesi <(E-Mail Removed)>
wrote:
> On 30/07/2011 18:52, Miro wrote:
>
> > 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));

>
> You can save the string to a file, launch a C++ compiler to generate the
> corresponding executable program file, launch the generated executable
> program, giving to that program the required input, and probably
> receiving from that program the result.
> Of course the target platform needs to have a C++ compiler installed
> (but you already knew that to run a Perl program the target platform
> needs to have a Perl interpreter installed).
> Communication between the main program and the auxiliary program may be
> performed through:
> - command line and return code (command line is a string and return code
> a byte)
> - unnamed pipes
> - named pipes
> - temporary files (deprecated, but more portable)
> - other IPC
> - networking
>
> An alternative (and probably better) solution is to evaluate your string
> with a built-in interpreter for your language, that is probably way
> simpler than C++.
> There are many open-source embeddable interpreters for simple languages.
>
> --
>
> Carlo Milanesihttp://carlomilanesi.wordpress.com/


You could also compile and link the code to a DLL, then load that.
(Not that I'm recommending such things.)

--
James Kanze
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      08-11-2011
On Sun, 2011-08-07, James Kanze wrote:
> On Jul 30, 7:08*pm, Carlo Milanesi <(E-Mail Removed)>

....
>> You can save the string to a file, launch a C++ compiler to generate the
>> corresponding executable program file, launch the generated executable
>> program, giving to that program the required input, and probably
>> receiving from that program the result.

....

> You could also compile and link the code to a DLL, then load that.
> (Not that I'm recommending such things.)


Normally I agree. But I've seen at least one application where it
would have been a good idea. Embedded system, IP packet filtering with
large amounts of configurable, complex rules, high performance
needed.

We're so used to assume that there is no compiler on the target
system. But nowadays, on Linux and *BSDs, if we have a need for one
it's not more troublesome than any other dependency. It's not as if
you, as an end user, have to buy a license for gcc ...

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      08-12-2011
Le 30/07/11 18:52, Miro a écrit :
> Hello, Usenet.
>
> 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++?
>
> Thanks for your help,
> Miro


If you limit yourself to the C language (not C++) you do:

#include <jit.h>

int (*pfn)(void);

int main(void)
{
pfn = compile("#include <stdio.h>\nint main(void){ printf(\"hello\");}\n");
if (pfn)
pfn();
}

You can do this using a Just In Time compiler. For more
information (and pricing) just contact

jacob at jacob dot remcomp dot fr


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      08-12-2011
jacob navia <(E-Mail Removed)> wrote:
> You can do this using a Just In Time compiler. For more
> information (and pricing) just contact
>
> jacob at jacob dot remcomp dot fr


Yeah, that's not suspicious at all.

(Come on, not even a website? Just an obscure email address?)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re-evaluating a string? bugnthecode Python 6 07-24-2006 10:20 AM
Evaluating a string of Ruby code within an anonymous module: donecorrectly? Asfand Yar Qazi Ruby 6 02-17-2005 09:54 PM
Java program is not evaluating String value correctly... Shelly Java 8 12-10-2004 04:12 PM
negative number evaluating greater than string.size() Jason C++ 25 02-22-2004 08:43 PM
newbie question on evaluating a string variable John Boik C Programming 2 08-16-2003 07:03 PM



Advertisments