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
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
- command line and return code (command line is a string and return code
- unnamed pipes
- named pipes
- temporary files (deprecated, but more portable)
- other IPC
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.
You could also compile and link the code to a DLL, then load that.