Compiling and executing during runtime in C++?

  • Thread starter Kasper Middelboe Petersen
  • Start date
K

Kasper Middelboe Petersen

Hello,

I'm porting a python library to C++.
Part of this takes a string (in this case the variable action could be
something like a function call or simply "i = 25") and both compiles
and runs it within the context it is running as shown here:

....snip...
# Compiling and executing string
# Fetch process frame and namespace
processframe= inspect.currentframe()
steps = self.execute_frame
while (steps < 0):
processframe = processframe.f_back
steps += 1

# Compile source provided in a string.
code = compile(action,processframe.f_code.co_filename + ' line ' +
str(processframe.f_lineno) + ' in string' ,'exec')
f_globals = processframe.f_globals
f_locals = processframe.f_locals
if op==READ:
f_locals.update({'channel_input':req.msg})

# Execute action
exec(code, f_globals, f_locals)
....snip...


Is this possible to do in C++? And if so, please point me in the right
direction :)


Thanks,
Kasper
 
M

Michael Doubez

Hello,

I'm porting a python library to C++.
Part of this takes a string (in this case the variable action could be
something like a function call or simply "i = 25") and both compiles
and runs it within the context it is running as shown here:

...snip...
# Compiling and executing string
# Fetch process frame and namespace
processframe= inspect.currentframe()
steps = self.execute_frame
while (steps < 0):
  processframe = processframe.f_back
  steps += 1

# Compile source provided in a string.
code = compile(action,processframe.f_code.co_filename + ' line ' +
str(processframe.f_lineno) + ' in string' ,'exec')
f_globals = processframe.f_globals
f_locals = processframe.f_locals
if op==READ:
  f_locals.update({'channel_input':req.msg})

# Execute action
exec(code, f_globals, f_locals)
...snip...

Is this possible to do in C++? And if so, please point me in the right
direction :)

It is not trivial. To roll your own, you could look toward the The
LLVM Compiler Infrastructure:
http://llvm.org/
But you would have to have some kind of introspection/reflexion which
is not that easy in C++.

The common solution is to implement "higher order functions" which
consists in exposing some functions and a syntax and generate some
sort of executable object. This is easily implement with some
libraries implementing python or lua language. Since your library was
first written in python, it could be a good step.
 
N

Nobody

I'm porting a python library to C++.
Part of this takes a string (in this case the variable action could be
something like a function call or simply "i = 25") and both compiles
and runs it within the context it is running as shown here:
Is this possible to do in C++? And if so, please point me in the right
direction :)

Is the "code" supposed to be in C++ or Python?

If it's in Python, then you need to link against the Python library and
use its C API. See the "Extending and Embedding" section in the Python
documentation. And direct further questions to a Python forum.

If it's in C++ ... well C++ really isn't that sort of language. It isn't
particularly meaningful to evaluate fragments of code in the sense of a
high-level language's eval() function.

If you just need some form of run-time extensibility which includes
expressions, you might want to consider Lua, as it's specifically
oriented towards embedding in larger programs.
 
K

Kasper Middelboe Petersen

The pasted code is the Python code I want to make a C++ version of.

It is as I suspected, and Michael underlined, way more complicated
than what is acceptable for this particular project timewise. My post
was just to be sure I was right :)

So yea, I'm going in a simpler direction :)


/Kasper
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top