Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6131 > unrolled thread

Compiling and executing during runtime in C++?

Started byKasper Middelboe Petersen <fantyfant@gmail.com>
First post2011-06-03 04:22 -0700
Last post2011-06-04 05:57 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  Compiling and executing during runtime in C++? Kasper Middelboe Petersen <fantyfant@gmail.com> - 2011-06-03 04:22 -0700
    Re: Compiling and executing during runtime in C++? Michael Doubez <michael.doubez@free.fr> - 2011-06-03 05:41 -0700
    Re: Compiling and executing during runtime in C++? Nobody <nobody@nowhere.com> - 2011-06-03 23:54 +0100
      Re: Compiling and executing during runtime in C++? Kasper Middelboe Petersen <fantyfant@gmail.com> - 2011-06-04 05:57 -0700

#6131 — Compiling and executing during runtime in C++?

FromKasper Middelboe Petersen <fantyfant@gmail.com>
Date2011-06-03 04:22 -0700
SubjectCompiling and executing during runtime in C++?
Message-ID<c98b9bc9-74af-40a3-885c-d1a2955206cd@v10g2000yqn.googlegroups.com>
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

[toc] | [next] | [standalone]


#6134

FromMichael Doubez <michael.doubez@free.fr>
Date2011-06-03 05:41 -0700
Message-ID<9808def6-7a51-4cb4-97ae-66297cccd333@u26g2000vby.googlegroups.com>
In reply to#6131
On 3 juin, 13:22, Kasper Middelboe Petersen <fantyf...@gmail.com>
wrote:
> 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.

--
Michael DOUBEZ

[toc] | [prev] | [next] | [standalone]


#6159

FromNobody <nobody@nowhere.com>
Date2011-06-03 23:54 +0100
Message-ID<pan.2011.06.03.22.54.41.313000@nowhere.com>
In reply to#6131
On Fri, 03 Jun 2011 04:22:15 -0700, Kasper Middelboe Petersen wrote:

> 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.

[toc] | [prev] | [next] | [standalone]


#6195

FromKasper Middelboe Petersen <fantyfant@gmail.com>
Date2011-06-04 05:57 -0700
Message-ID<687893aa-a580-4479-8ce9-813c1428eede@n11g2000yqf.googlegroups.com>
In reply to#6159
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

On Jun 4, 12:54 am, Nobody <nob...@nowhere.com> wrote:
> On Fri, 03 Jun 2011 04:22:15 -0700, Kasper Middelboe Petersen wrote:
> > 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web