How to run c++ code from c++? - c++

If I have some c++ code as a string quantity (data) in a c++ program, can I execute the contents of that string?
As using the CodeDOM in C# or the eval function present in perl, python, etc..

Short answer: you can't.
Slightly longer answer: c++ has no reflection, and is generally compiled, so there so is no support for this kind of thing, and it can not be easily added..
Work arounds:
Use an embeddable dynamic language like [python|tcl|ruby|...] in concert with your c++ code. Now you need to have the dynamic language (rather then c++) in the data.
Use a c++ interpreter like cint or ch. This binds you to the interpreter.
Use the system c++ compiler to construct a dynamic library from your code and link to it on the fly. Risky and system dependent.
Use a different language.

No. As C++ is a static language, you can not dynamically eval arbitrary code.
You could interpret it or even compile and run it seperately as Keith suggested

if you mean compile C++ code on the fly and run it? sure if you have a compiler available to you

Non of the mainstream C++ implementations have that feature, as C++ is not reflective.
However, take a look at Ch, it might be what you are looking for:
http://www.softintegration.com/
http://en.wikipedia.org/wiki/Ch_interpreter
You can embed Ch interpreter into your C++ application and run dynamic C++ code inside it.

C++ cannot do this - the compiled program has no knowledge of the source language syntax, identifier names etc.
Even if you go to the effort of writing the string out to a file, calling a c++ compiler to generate a dynamic library and loading and calling it, that call won't have any knowledge of your program - it won't be able to reference your variables etc.
The most common reason to want to do this is to be able to evaluate expressions from within strings. Writing the code to do this from scratch is definitely non-trivial, but depending on your specific requirements, you should be able to find a library or embeddable scripting language to do roughly what you need.
After a quick Google, I found this - C rather than C++, and I don't know how good it is. It's written as a demo of a parser generator that I haven't heard of. You might find alternatives as demos of better known parser generators such as yacc, bison, yacc++ or antlr.

Related

Is there a C-like syntax scripting language interpreter for C++?

I've started long ago to work on a dynamic graph visualizer, editor and algorithm testing platform (graphs with nodes and arcs, not the other kinds).
For the algorithm testing platform i need to let the user write a script or call a script from a file, which will interact with the graph currently loaded. The visualizer would do things like light up nodes while they're being visited by the script algorithm, adding some artificial delay, in order to visualize the algorithm navigating and doing stuff.
Scripts would also be secondly used to add third party features that i could either make available as pre-existing scripts in the program folder OR just integrate inside the program in c++ once they're tested and working.
All my searches for an interpreter to embed in my program sent me to lua;
then i started handwriting my own recursive descent parser for my own C-like syntax scripting language (which i planned to use a subset of C++ grammar so that any code written in my scripting language can be copy-pasted in any C++ code.
It was an interesting crazy idea which i don't regret at all, I have scopes, functions, cycles, gotos, typesafe variables, expressions.
But now that i'm approaching the addition of classes, class methods, inheritance (some default classes would be necessary to interface scripts to the program), i realized it's going to take A LOT of time and effort. A bit too much for a personal project of an ungraduated student with exams to study for… but still i whish to complete this project.
The self-imposed requirement of the scripts being 100% compatible with C++ was all but necessary, it would have been just a little nice extra thing, which i can do without.
Now the question is, is there an alternative to lua with a c-like syntax that supports all i've already done plus classes and inheritance? (being able to add custom "classes" that interface scripts to the program is mandatory)
(i can't assume the user to have a full c++ compiler installed so i cant just compile their "script" at runtime as a dll to load and call it, although i whish i could)
Just-in-time compilation of C++
Parsing C++ is hard. Heck, parsing C is hard. It's difficult to get it right, and there are a lot of edge cases. Thankfully, there are a few libraries out there which can take code and even compile it for you.
libclang
libclang provides a lot of facilities for parsing c++. It's a good, clean library, and it'll parse anything the clang compiler itself will parse. This article here is a good starter
libclang provides a JIT compilation tool that allows you to write and compile C++ at runtime. See this blog post here for a overview of what it does and how to use it. It's very general, very powerful, and user-written code should be fast.
GCC also provides a library called libgccjit for just-in-time compilation during the runtime of a program. libgccjit is a C library, but there's also a C++ wrapper provided by the library maintainers. It can compile abstract syntax trees and link them at runtime, although it's still in Alpha mode.
cppast
If you don't want to use libclang, there's also a library under development called cppast, which is a C++ parser which will give you an abstract syntax tree representation of your c++ code. Unfortunately, it won't parse function bodies.
Other tools
If anyone knows any other libraries for compiling or interpreting C++ at runtime, I encourage them to update this post, or comment them so I can update it!
Here is something that lets you embed a C-like scripting language in your application (and a bunch of other cool things):
http://chaiscript.com/
There is lots of documentation:
https://codedocs.xyz/ChaiScript/ChaiScript/

What are the reasons for using Ragel to parse strings in a C++ codebase?

I inherited a C++ project which uses Ragel for string parsing.
This is the first time I have seen this being done and I would like to understand why someone would use Ragel instead of C++ to parse a string?
parser generators (improperly called "compiler-compilers") are very handsome to use and produce reliable and efficient C++ or C code (notably because parsing theory is well understood).
In general, using source code generators could be a wise thing to do. Sometimes, notably in large projects, it is sensible to write your own one (read about metaprogramming, notably SICP and even J.Pitrat's blog). Good build automation tools like GNU make or ninja can easily be configured to run C or C++ code generators and use them at build time.
Read Ragel intro. Look also into flex, bison, ANTLR, rpcgen, Qt moc, swig, gperf as common examples of C or C++ generators.
In some programs, you could even use some JIT compilation library (such as libgccjit or LLVM) to dynamically generate code at run time and use it. On POSIX systems you could also generate at runtime a temporary C or C++ file, compile it as a plugin, and load that temporary plugin using dlopen & dlsym. Having a good culture about compilers and interpreters (e.g. thru the Dragon Book) is then worthwhile.
Embedding some interpreter (like lua or guile) in your application is also an interesting approach. But it is a strong architectural decision.
In many cases, generating source code is easier than hand writing it. Of course that is not always possible.
PS. I never heard of Ragel before reading your question!

Parsing string as a line of code in C++

Is it possible that a string variable can be parsed as an actual line of code in C++? For example, can this string, "x=0", be parsed as actual code and set the value of x (some random variable in the program) to zero? What I plan to do with this is that I want to make a simple plotter in C++. The user enters the function (The function will be in terms of x and y and will have the value zero) to plot as a string (like 2*y+x), which then will be converted to a code object and then evaluated accordingly using a loop.
Because C++ is a compiled and linked language it is not suitable for on-the-fly evaluation.
But I've achieved something similar to your aims in the past with C++ by embedding a Python interpretter to evaluate Python code as strings on the fly and pass the results to the C++ code.
Some other popular scripting languages that can be embedded in a C++ program are Lua and Squirrel.
In Java I've done the same by embedding a Groovy interpretter.
You need to integrate the scripting language interpretter into your code by "embedding" it and then pass values from the scripting language code to your C++ code by a process of "marshaling"
If you really want C++ syntax that can be interpretted, it is theoretically possible to develop a dynamic parser and interpretter for a subset of the language, but C++ is a complex language and such a task would be an enormous undertaking fraught with difficulty and essentially a case of using the wrong tool for the job.
The short answer is "No". You can't compile C/C++ "on the fly" like that, as it's a compiled language, not an interpreted one.
But here's an idea: you could embed a JavaScript interpreter, using the SpiderMonkey API, which can interpret all your example code snippets, as JavaScript syntax is very similar to C/C++ in this regard.
The short answer is "Yes". Compiling C++ on the fly works just fine using a C++ JIT. From llvm.org
A Just-In-Time (JIT) code generation system, which currently supports X86, X86-64, ARM, AArch64, Mips, SystemZ, PowerPC, and PowerPC-64.
The assumption is that you're willing to link most of a compiler into your program in order to achieve this. With concerted effort you should be able to write "eval" on top of the existing API.

Using scripting language in C++

My question is a little bit stupid but I decided to ask advanced programmers like some of you. So I want to make a "dynamic" C++ program. My idea is to compile it and after compilation (maybe with scripting language like python) to change some how the code of the program. I know you will tell me that after the compilation I can not change the code but is there a way of doing that. Thank you!
You could design a modular architecture using plugins in external libraries. For example you could use the Command Pattern. That way you can dynamically load code that was generated after you main program. You would have to fix an interface though. Functions like GetProcAddress in the Windows api might be a good point to start.
For dynamic coding and rapid prototyping I recommend to have a look at Lua. The engine is very small and easy to integrate in your c++ program.
The only way to do that in C++ is to unload the DLL with the code to be
modified, modify the sources, invoke the compiler to regenerate the DLL,
and reload the DLL. It's very, very heavy weight, and it only works if
the compiler is present on the machines where the code is to be run.
(Usually the case under Unix, rarely the case with Windows.)
Interpreted languages like Python are considerably more dynamic; Python
has a built-in function to execute a string as Python code, for example.
If you need dynamically modifiable code, I'd suggest embedding Python in
your application, and using it for the dynamic parts.
Personally I have never played with re-compiling C++ during runtime, and I do not intend too. However I have been doing a lot of embedding of scripting languages lately.
Someone else mentioned the obvious first choice for embedding: Lua. Lua is a pretty popular language to embed and you will find a bunch of documentation about how to do it. Integrating it into the C++ will allow you to define behavior at runtime like you want.
What I am using is a wonderful langauge called Squirrel. It is a lot like Lua but with native object(class) support and C++-like syntax. I have managed to embed it into a C++ application, and through using the sqrat binding library both languages can share information easily.
I have squirrel building and initializing my UI. As a result, 0 compiling is required in order to craft and structure my UI for my game. I intend to take this a step further and use this gameplay-side in order to create similar behavior you are looking for(changing behavior at runtime through dynamic code usage)
I recommend Checking out squirrel here: http://www.squirrel-lang.org/
I plan on writing tutorials on how to embed squirrel and install the binding library, but I have not started on them yet. If I can remember, I will edit this post in the future (could be a few months) once I have completed them. In the meantime give it a shot yourself. You may find it to your liking.

Lisp/Scheme DSEL in C++

I came across the following post on the boost mailing lists (emphasis mine):
hello all,
does anybody know of an existing spirit/lisp implimentation, and is there
any interest in developing such a project in open source?
None yet, AFAIK.
I'll be writing an example for Spirit2
to complement the tiny-C virtual
machine in there. What's equally
interesting though is that scheme (or
at least a subset of it) can be
implemented in pure c++. No parsing,
just pure DSEL in C++. Now, imagine a
parser that targets this DSEL (through
C++) -- a source to source translator.
Essentially, your scheme code will be
compiled into highly efficient C++.
Has anyone actually done this? I would be very interested in such a DSEL.
I wrote a Lisp-like language called Funky using Spirit in C++. An Open Source version is available at http://funky.vlinder.ca. It wouldn't take too much to turn that into a Lisp-like to C++ translator.
Actually, what it would take is a run-time support library to provide generic closure times and somesuch: if you want to turn the Lisp code into efficient C++, you will basically need C++ classes (functors, etc.) to do the heavy lifting once you get to run-time, so your Lisp-to-C++ translator would need to:
parse the Lisp
create an AST from the Lisp
transform the AST to optimize it, if possible (optimizations in Lisp are different from optimizations in C++, so if you want rally fast C++, you have to optimize the Lisp and let your C++ compiler optimize the generated C++)
generate the C++, for which you'd rely on your run-time support library for things like built-in functions, functor types, etc.
If you were to start from Funky, you'd already have the parse and the AST (though Funky doesn't optimize the AST), so you could go from there an create the run-time and generate the C++...
It wouldn't be overly complicated to write one from scratch either: Lisp grammar isn't that difficult, so most of the work would go into the AST and the run-time support.
If I weren't writing an object-oriented DSL right now, I might try my hand at this.
scheme to (readable) c++
http://www.suri.cs.okayama-u.ac.jp/servlets/APPLICATION.rkt
How about this
Not sure if this is what you want, but:
http://howtowriteaprogram.blogspot.com/2010/11/lisp-interpreter-in-90-lines-of-c.html
It looks like a start, at least.