Interpreter

I want to make a C++ interpreter, using a C++ IDE.
It would take input from the user ,and execute that input.
So ,if the user entered
"cout<<"hello"<<endl;" (without the quotes)
It would print "hello" on the screen and ask for input again.

All of this would be in the output window.

Most probably this is a stupid question and probably has a very simple answer, but ,well , I'm a beginner.
Last edited on
Word of warning, this seems like a bit of an advanced topic for a beginner.

The first thing you'd have to do is split the input string into tokens. For example 'cout<<"hello"<<endl;' = 'cout', '<<', '"hello"', '<<', 'endl', ';'
Also be careful if these are part of a string, ie 'cout<<"<<"<<endl;'

Then you'd have to parse literals and look up variables. I suggest using std::map to store them all as void* along with the type

Then you start following the order of operations and replacing the tokens with their results. For example 1+5*5 -> 1+25 -> 26

After that you can get started on implementing functions, structs, and so on


EDIT: I realize maybe you were asking about something more along the lines of JS's eval(). Unfortunately, C++ has no equivalent
Last edited on
closed account (48bpfSEw)
https://solarianprogrammer.com/2012/08/14/cling-cpp-11-interpreter/
Ok , does seem advanced to me. Leaving it for now.
Topic archived. No new replies allowed.