2

Possible Duplicate:
compile and run c++ code runtime

I want to take as an input an expression from the user as a string and compile it into a callable c++ function. Are there any tools that allow you to do this easily?

Basically, How do I compile an Expression Tree into a callable method, C#? seems similar to what I want to do except that I need to do this in c++ and not c#.

I can certainly make a sort of generic evaluator using lex and yacc but I don't want to have to parse the string every time. Basically this expression will run in a critical inner loop so I'm looking for a way to "compile" it at run-time.

Community
  • 1
  • 1
owagh
  • 3,428
  • 2
  • 31
  • 53
  • This appears to be a duplicate of [Compile and run C++ code runtime](http://stackoverflow.com/q/11523023/62576) – Ken White Aug 30 '12 at 19:23
  • Hmmm, yes it's a duplicate. But that question doesn't have a good answer either... – owagh Aug 30 '12 at 19:25
  • I'd look into libclang and llvm. You should be aware that C# makes this task incredibly easy, while it is labour intensive in C++. – Magnus Hoff Aug 30 '12 at 19:29
  • 2
    @owagh That's because there really isn't a good answer. You can use a heavy-weight solution like using an actual compiler, either externally or embedded (e.g., LLVM can be used in an embedded JIT compiler), or you can implement your own bytecode compiler and evaluator (or use someone else's; Lua is implemented as an embeddable bytecode compiler/evaluator. of course it only processes the Lua language). – bames53 Aug 30 '12 at 19:30
  • There's a reason there is no answer that you find acceptable. There are no ways to do this easily. You can do it for special cases of method signatures - but I don't see how you can do it generically. Pick a language that allows this kind of thing. Scheme/Lisp for example – Tim Aug 30 '12 at 19:30
  • @Tim I wish I could use lisp but I can't... – owagh Aug 30 '12 at 20:12
  • 1
    Perhaps [this](http://code.google.com/p/asmjit/) is what you're looking for. -nick – mythagel Aug 31 '12 at 04:15

4 Answers4

1

You can write your mini-interpreter. With the commands same with c++ (not all of them). Of course your compiler will optimize it but not sure how much. I did it for assembly in qbasic (mov, add, sub...) but it was quite slow because of being an interpreter of an interpreter :D

Did you think about Evolutionary computation and fitness functions? Worth looking at.

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97
1

You have to parse the expression to an abstract syntax tree and walk it or evaluate it in-place. Something like this should satisfy your needs for a simple mathematical expression.

1

It's not easy... If you want my two cents, I will follow these steps:

  1. Create an interface for the code that you must create at runtime. At first, you create an interface for what you can do. For example your class must inherit from a pure virtual base class that will represent your interface. Take care that your program will use not arbitrary code, but code created in a specific way, because it must know how to use it.
  2. Call the compiler from inside your program. The compiler should create a library from your source code. You can use a predefined project that you store somewhere, and then replace its source file with your own. So it can be easy to obtain a right library.
  3. Put your library in a specified source where you can find it.
  4. Load the library at runtime. If you search, you will see that it's possible to load dynamic libraries at runtime, not only at linking time (in this way, for example, you can create plugins for programs). So your program can load your library and use it. For example you can find some information here.

But, as others have said, it's not a trivial task.

EDIT: Another solution is to check a parser like boost::spirit::qi, that is well used can give extremly helpful results.

Jepessen
  • 11,744
  • 14
  • 82
  • 149
0

You can create a data structure that represents your parsed expression tree, and the overhead of evaluating that at runtime will be small compared to parsing the string every time.

Actually getting a callable method in C++ will be quite difficult, in that you would have to generate object code and dynamically load it into your program. This would duplicate a lot of what the whole compiler tool-chain does.

antlersoft
  • 14,636
  • 4
  • 35
  • 55