Entries Projects About

 C++ Interpreter (Python)

9 June 2017

Welcome to the first entry of the second release of this blog.

I created C++ interpreter in clear Python, and its general principle of action is having source code in the memory, then compiling code after every passed line and returning the output on the screen.

List of assumptions(in general):

  • Creation of separate directory for each session (can specify own directory)
  • Basic code is defined from the very beginning
  • After passing correct instruction set, compile, return output, and update basic code
  • If compilation failed, return fail and revert to last correct stack of instructions
  • There are two stacks of instructions:
    • those that start with "#"- those instructions will be added to the first stack in the beginning of the file
    • all other instructions will be added to main() function
  • After each compilation show source code
  • It is possible to remove specific lines from source code
  • It is possible to change any specific line
  • It is possible to a add few lines at a time
  • It is possible to revert to very basic stacks
  • Every command(correct or not) is added to the additional commands.log file

Examples of functionalities(part I):

Demonstration(explanation below image)
C++ Interpreter(Python)

In the first line we call "python3", it creates the directory "env1", which we can see in the second line "env: env1".

In the next line, we are in actual interpreter already. Every line that that starts with ">>" accepts instructions. I type "2;" there, so we can see that it appears in source code. Lines that starts with number and colon "number:" represent current source code.

The next prompt line says "std::cout<<"test"" and we can see that the return output from the program is actually "test".

Then I removed the fourth line by typing "4:-", simply to say that the pattern to remove a line is "number:-".

Now, let's say that we want to have clear code again without restarting the script, in which case we could type "_r" in order to go back to very basic source code. Furthermore I just type Enter to run current code.

Okay on the next prompt we can see a super cool and awesome feature, namely changing specific lines, which we can achieve by typing the number and two colons with a new code on the end, "number::new code here;".

The next amazing feature allows us to add a few lines at a time without compiling each of them one by one, instead of they will be compiled only once at the end. To use that feature you have to space left at the end of the line.

Further I wanted to demonstrate what happens when you do something wrong. You will see output from the compiler, and your source code will be restored to the last correct one.

On the second-to-last line, I've added one more line to show that everything still works after incorrect input.

On the last line, I typed "_exit" to close script.

And here we can see one more thing(on the last bash command), namely that out environment directory has a "commands.log" file that contains everything we've entered.

Examples of functionalities(part II):

Example of working environment
C++ Interpreter(Python)

Here, on the first bash command line, we specify where to create the environment directory, after which I add some code and leave the script, and then do the same for another environment.

Then I list directories to show that the script can calculate correctly to create the next catalog.

Then we can see what is in each directory, each of which always has 3 files, namely main.cpp, main.bin, and commands.log.

Last command display for us content from main.cpp files from both environments.

In source code, there are few doctest tests, which I used while creating the script. I just left them commented out.

Source code:
Github C++ Interpreter(Python)


Miscellaneous © 2014-2017-2018