I know you're wondering, why Python? Out of all other programming languages that exist? Just why?
Well, I chose it because it was simple, easy to use, and kinda intuitive, seeing that it felt like natural language to me. I also just learned it in a course I was pursuing in university and decided to give it a shot. It couldn't be that bad, right?
Actually, it wasn't bad, just a bit unfamiliar
You see, I've been creating my own programming languages behind the scenes. However, they were made in C++, which seemed like a more appropriate choice. Given that it's fast and offers better management of memory, performance, etc., you might ask, "So why didn't you just stick with it?"
I hated myself I wanted to expand my knowledge in Python and see what all the hype was about! So join me in this little endeavor of exploring the magic of Python programming!
Introducing pyp lang!
Pyp lang is a very complex and scary programming language designed to create anything you imagine with just one function!
Yeah, I wish it were that too, but in all seriousness, pyp is just a really fancy calculator that reads input from files and spits out the computed result.
We gotta start somewhere, and that somewhere is setting up our directory with simple Python files and a sample .pyp file for testing.
/pyp
index.pyp - our source code file
/src
index.py - our main file to run pyp
Our sample file - index.pyp
This file just contains a simple expression!
1 + 1
Our main file - index.py
index.py contains the code necessary for getting the source code from pyp/index.pyp. It turns out Python is really simple and abstracts away majority of the headache!
But first, we need a way to get the file path. So I tried out the pathlib and sys libraries to fetch it from the command line.
import sys
from pathlib import Path
# this is just a display help function when the file path isn't provided!
def display_help():
print("welcome to pyp!")
# the guts of the whole thing!
def run_pyp_file(args):
pyp_path = args[0] # we would fetch the pyp input file path here
pyp_file_path = Path(pyp_path) # and convert it to a path!
# if the file doesn't exist, we tell the user!
if not pyp_file_path.exists():
print(f"could not find the path {pyp_path} :(")
return
# and finally, we open the file for reading the source code
with open(pyp_file_path) as pyp_file:
source = pyp_file.read()
print(source)
def main():
# fetching the arguments from the command line
args = sys.argv[1:] # decided to get rid of the index.py path
# the arguments wouldn't have the pyp input file if the len is 0!
if len(args) < 1:
display_help() # show a helpful message
return
run_pyp_file(args) # run the guts!
if __name__ == "__main__":
main()
Finally, running the result with this command:
python src/index.py pyp/index.py
Gave me this as the output!
1 + 1
We did it!!
...
What's that sys.argv thing?
Honestly, I'm still learning about the sys library, but it just represents the command line arguments as a list!
# sample.py
# when printing sys.argv, it gives you a list based on what you provide via the command line
print(sys.argv)
If we run the above with the command python sample.py in the command line, it would print the following:
['sample.py']
If you'd like to try it yourself, you can copy the snippet and run it with more arguments! Try this one in the command line:
python sample.py hello world
You should see:
['sample.py', 'hello', 'world']
More information about sys.argv below.
argv
What on Earth is it!?
Edit: If you can, will you please write a line or two and explain how it works?
Yeah, yeah, I know Python. What's next?
Well, since we already got the source code from the pyp/index.pyp file, we need to transform it into something called tokens! Unfortunately, I won't be covering it in this post :(
However, if life allows me to, I'll for sure make a part 2 covering it, along with something called lexical analysis.
...
Thank you so much for reading my first-ever blog post! I deeply appreciate any feedback and any other comments regarding it. I hope you have a wonderful day :)
Further Reading
If you'd like to read up more about programming languages, here are some resources!
https://craftinginterpreters.com/
https://en.wikipedia.org/wiki/Programming_language
https://dev.to/johnrushx/38-programming-languages-which-is-best-584f
Top comments (0)