!Python is a tiny language that I made to learn how compilers do their magic. The compiler of !Python is written in pure python ( no dependencies ). It currently supports limited functionalities only, but in the future I might add more. Or if you are interested you can add them, power of open source right !?.
To set up the compiler, you need to have python installed on your system. You also need g++ as the compiler uses it to compile the generated intermediate code.
python3 main.py <filename>
to compile the file<filename>.exe
to execute the program!Python uses a simple syntax. It is similar to c/c++ but with some differences. The syntax is as follows:
PRINT <expression>
: Prints the value of the expressionINPUT <variable>
: Takes input from the user and stores it in the variableIF <expression> { <statement> }
: Executes the statement if the expression is trueWHILE <expression> { <statement> }
: Executes the statement while the expression is trueLET <variable> = <expression>
: Assigns the value of the expression to the variableRETURN <expression>
: Terminates the program with expression as exit codeThat’s basically it. You can check the examples folder for more examples.
What about comments you ask ? Comments are for those who can’t write readable code. Just kidding, I will add comments later.
The following keywords are reserved in !Python:
All keywords are case-sensitive, why would you want to write print
instead of PRINT
? 🤷.
This means it is perfectly legal to do something like this:
INPUT print
PRINT "You said " print
But it is not legal to do something like this:
INPUT PRINT
PRINT "You said " PRINT
Currently, there are only 2 compiler flags:
-d
: Enables debug mode. In debug mode, the compiler preserves the intermediate code generated.
It will be saved as <filename>.exe.cpp
-o <filename>
: Specifies the name of the output file. By default, the output file is <filename>.exe
You can check the examples folder for more examples.