0

In c is there a way to turn a string into commands? For example:

void x(string n){
   //do something
}
void y(int n){
   //do something
}
void z(int n){
   //do something
}
stringForConvert="z(10);y(2);x(\"Its cool\");";
mysteryCommand(stringForConvert);

Please note that i don't have luxury for writing to another file and compiling it.

Asim Poptani
  • 158
  • 1
  • 14

4 Answers4

5

No, since C is a compiled language this is not very easy.

C doesn't work like typical dynamic/scripting languages, which depend on a large run-time environment. Such languages often have a function (in Perl and Python, it's called [eval()]) that does what you want, but C can't do that since the compiler is not left "inside" the compiled program, it is a separate (and fairly large) program.

C can be compiled to run without any kind of external environment, but even when you do include it the C runtime is much smaller than the compiler, typically.

You're going to have to either call the actual compiler from your program, or do it all yourself.

Calling the compiler of course requires that you know exactly which compiler to use, and that it is even usable from the command-line. Then you can use e.g. system() to call the compiler, and then add code to check that the ouput was properly generated, and use another system() function call to run the newly generated program. Sharing data between your program and the newly compiled one will be very complicated.

Doing it youself requires familiarizing yourself with many of the tasks done by a compiler, such as tokenizing, parsing, and evaluating parse trees. These are large complex subjects and not easily shown by example here.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • can you show me an example please – Asim Poptani Oct 20 '14 at 08:18
  • 1
    @AsimPoptani: Nobody here is going to write your code for you, free of charge. This site is where you turn to if you encounter a specific, coding related problem. The goal is to help you understand the problem, and tell you how to solve the issue. The reason why there's no `eval` in C is explained here. The alternatives are also listed (parsing the string, compiling the string and all that). Asking for an example that is copy-paste ready is asking people to do your work for you. That's not going to happen – Elias Van Ootegem Oct 20 '14 at 10:31
2

You cannot do what you want in standard C99.

However, particularly on Linux and other Posix systems, you might consider some tricks, like

  • using some embedded interpreter like Lua or Guile, etc... AFAIK, Lua is portable to any C99 system.
  • use some bytecode virtual machine like nekovm or parrot, etc...
  • writing your own parser (e.g. using recursive descent techniques) and evaluator or interpreter for your domain specific expression language
  • generating some C code in some e.g. gen01.c file (at runtime), then forking a compilation (with gcc -Wall -fPIC gen01.c -O -g -shared -o gen01.so ....) into a shared object, then dynamically loading (using dlopen(3) on "./gen01.so" and dlsym on appropriate symbol names) that shared object. Believe me, I'm doing that in MELT, and it could be even fast enough to be compatible with interactive use (in your words, the luxury is affordable in practice)!
  • use some JIT compilation library like libjit, LLVM, GNU lightning to generate machine code on the fly

On some platforms, you might consider using tinycc: it compiles very quickly some C code (perhaps even from a string, if using its libtcc) into very slow and unoptimized machine code.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I cant use this since the i don't know the operating system this is going to be run on – Asim Poptani Oct 20 '14 at 08:05
  • You really should know that. So your first step it to understand the target environment (operating system, processor), and learn which OS and libraries and compiler is available. You cannot program in the abstract such things. – Basile Starynkevitch Oct 20 '14 at 08:07
  • A scripting language (like Lua or Guile or tcl) can be statically linked to your application. If you can compile your application for a given platform, you should be ok. Especially Lua qhose only prerequisite is to be compiled with a standard C compiler (I believe ansi would be enough). Allowing the execution of arbitrary code is a really, really bad idea. – Remo.D Oct 20 '14 at 11:58
  • I disagree with the "allowing execution of arbitrary code" is *always* a bad idea. It is a matter of trust. There are situations where you can trust that "arbitrary" code – Basile Starynkevitch Oct 20 '14 at 12:00
0

You can't, but from what I gather you might be able to run machine code. See this.

Community
  • 1
  • 1
Atuos
  • 501
  • 3
  • 12
0

You can't do that.

As the compiler translates your code into machine code which gets executed. So a String can't be get executed by C it self.

You could only use a Library(if even exists for C) which could parse such commands on runtime.

But if you want so, I would advise to use a other language than C, as C is optimized for machine code, not for parsing code.

dhein
  • 6,431
  • 4
  • 42
  • 74