I need some help with commands

I need the user to write an input and then the console executing it like a line of code. Example:

1
2
3
4
string a;
cout<<"Please type in a command:"<<endl;
cin>>a;
a;

What appears on console:
1
2
3
<output>Please type in a command:
<user input>: cout<<"Hello!"<<endl;
<output>: Hello!
Last edited on
Its not a homework :P. I am trying to make like my own console in which everyone can create addons using a special extension. So far so good. The problem is that is a user wants to input a command i cant transform it into a function to be executed. Also i cant do multiple if's because the functions will be from multiple files...
I managed to do the converison from command to function Example: say hi -> say("hi").
but now i dont know how to execute this. I have the string. How do i execute it like a line of code? Like the compiler to compile my line of code and then execute it on the console. Kind like system(""); But instead of Batch to make it execute c++ code. Can anyone help me?
Example:

1
2
3
<output>Please type in a command:
<user input>: cout<<"Hello!"<<endl;
<output>: Hello!
I need kind of a addon/plugin system. Is it possible to be made? I just want to know if its possible. And if its possible HOW?
Last edited on
I am still not really clarified....
@masecla33
Please don't be tricked into wasting your time trying to understand gentleguy's comments. He's a known troll, who's only purpose is to disrupt this forum and confuse people who are looking for help. Ignore him.
@masecla33

what compiler and OS are you using? what have you done so far?
If you are interested in language design, than I recommend Queinnec's Lisp in Small Pieces. It has nothing to do with C++, but that's a detail and the content is excellent.

The concepts are relatively simple -- it's a three step process.
1. Break down the input into a set of tokens. ("Lex" the input)
2. Match those tokens to a particular formal grammar.
3. Walk the resulting syntax tree and do what it says.

Sometimes you need more information to resolve grammar conflicts in some languages (like C++).

C++ is extremely difficult to parse. It's context-sensitive and semantic analysis of C++ is notoriously complicated. I wouldn't recommend trying to interpret C++ from scratch, but if you really want to use C++, you should look into using LLVM as a front-end; they have done the hard work for you already. I believe some work has been done on C++ JIT compilation (did CERN have something to do with it?) but I don't know what came of it.

Other languages are probably better choices for this sort of thing. Lua, for instance, is designed to be embedded. I've not used it myself, but there is a library named "liblua" that contains an interface to an interpreter. Lisp, which is the subject of the book I referenced at the top of this post, makes it really easy to define interpreters for most languages, including itself.

You may find flex and bison (canonically named lex and yacc) useful. They are metaprograms that generate lexers and parsers, respectively. You can't handle C++ with the pair, but simpler languages are doable.

I am the wrong person to ask about this, though. It's one of those things that going to school is useful for.

Be aware of the security implications of calling "eval".
I found something that may be useful to you. ( https://root.cern.ch/cling ) It does what it says on the tin.

Example session:
1
2
3
4
5
6
7
8
9
10
mjb@mbozzi-arch ~/prj/build/cling % ./inst/bin/cling
****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <iostream>
[cling]$ std::cout<<"Hello World!\n";
Hello World!
[cling]$ .q
mjb@mbozzi-arch ~ % 

It uses LLVM in the backend.

I can see it being useful for testing short snippets of code. I haven't had a chance to read much at all about it, but I would expect that the library it built contains an interface to an interpreter.

Last edited on
Bump!
Can anyone answer this question seriously? also @mbozzi i am sorry but i couldn't understand what is cling, I am sorry D:
Last edited on
I did answer the question seriously.

From the link I posted:
What is Cling

Cling is an interactive C++ interpreter, built on the top of LLVM and Clang libraries. Its advantages over the standard interpreters are that it has command line prompt and uses just-in-time (JIT) compiler for compilation. Many of the developers (e.g. Mono in their project called CSharpRepl (link is external)) of such kind of software applications name them interactive compilers.


Cling is an interpreter for C++ code. It does what you want.

Edit:
Probably I should elaborate more.
What you want to write is an "interpreter". An interpreter executes code as it is read.
C++ was not designed to be interpreted, and because of some technical reasons, writing an interpreter for it (from scratch) will be extremely difficult. Projects that do try to interpret it, like Cling, leverage a large and well-known compiler infrastructure named LLVM to compile and run your code as you type it in.

You should consider interpreting another language, one designed to be interpreted, or at least with that capability built-in or included standard, such as Lua, like I said above.

Or, if you're feeling ambitious, you can write your own domain-specific language and design, parse, and interpret a language of your choosing. Tools like yacc and lex might help you do that.
Last edited on
closed account (48T7M4Gy)
It sounds like you need to combine a couple of things. First a console she'll type program. Second a menu system within it. And third, the use of function pointers. That should give you enough to start and here is a quick intro on function pointers.

http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work
Yeah, sure. You can do really simple stuff by mapping strings to functions.

If you incorporate a stack upon which you can push and pop those strings at will then you've got the very barebones of a Forth interpreter.
closed account (48T7M4Gy)
It's fairly easy to do in windows with C++ and there are lots of references elsewhere on the web, or perhaps on this site. IDK how it's done using OSX and Linux. Presumably the shell uses something akin to the windows API equivalent.
Topic archived. No new replies allowed.