My name is Saptak and this article is going to explain the current state at where Peregrine is at.
If you know Python, you know how easy it is. However, it also comes with a big downgrade. Python is slow, and I'm pretty sure every python developer knows this by now. This is kind of annoying. That's where Peregrine comes in.Peregine's syntax is very similar to Python's, and it gets translated to C++, thus making it as fast as C/C++ if not faster. Read my previous post to know more.We have been rewriting it from ground up in C++ for the past month to make the compiler more efficient
If you are a python dev then you most probably know about decorators. They are used to make your code more readable. Similarly peregrine also has decorators. This is what it looks like:-
#type declaration type dec_type = def(int)->int type first_type = def(int) def decorator(dec_type func)->first_type: def value(int c): printf("Answer is %d\n",func(c)) return value @decorator def dec_test(int x)->int: return x*x def main(): dec_test(4)
This will print "Answer is 16" as expected
The above is same as follows:-
#type declaration type dec_type = def(int)->int type first_type = def(int) def decorator(dec_type func)->first_type: def value(int c): printf("Answer is %d\n",func(c)) return value def temp_dec_test(int x)->int: return x*x first_type dec_test=decorator(temp_dec_test) def main(): dec_test(4)
And as you can tell that decorators make your code a lot cleaner
Powerful Pattern Matching
Pattern matching in peregrine is extremely powerful. Let me show you an example:-
def main(): int a=0 int b=7 int c=7 match a,b,c: case 5,7,8: printf("a is 5,b is 7 and c is 8") case 4,7,_:#c can be anything printf("a is 4 but b is 7") break #we dont want default to execute case 4,_,7:#b can be anything printf("a is 4 but c is 7") break #we dont want default to execute case 8,_:#b and c can be anything printf("a is 8") case _: printf("idk") #optional default:#will be executed at the end if no break printf("\nHello\n")
Read my comments to understand how it works.
Note:Default block is optional
Peregrine is a completely open source project licensed under the MPL-2.0 license so you can build it from source because the code is freely available.
Follow the following steps to compile:-
1)Clone the rewrite branch of https://github.com/peregrine-lang/Peregrine.git
git clone -b rewrite https://github.com/peregrine-lang/Peregrine.git
2)Cd into the directory
meson builddir cd builddir ninja
This will create the binary of the compiler named
./peregrine.elf in the builddir folder
To compile it using the c++ backing just run
./peregrine.elf compile path_to_file.pe .It will create the executable named
./a.out. Run it to see the result. Check the can_comp.pe file in the root directory to know what you can do with the c++ backend at this point
./peregrine.elf compile path_to_file.js.pe -js.
node index.js. Check the can_comp.js.pe file in the root directory to know what you can do with the js backend at this point
Important point to be noted
The cli is still very rough. The only reason is that we are constantly developing it and we need some special commands for that. Like for example if you run
./peregrine.elf then it will parse
../Peregrine/test.pe and print the tokens and parse tree in the form of s expression.
Peregrine is still in it's early phases and is still nowhere near to a functional language. It is planned to release version 0.1 sometime in March, so make sure to show some support by starring the repo and make sure to press on the "Watch" button so you don't miss any updates.
We would greatly appreciate any contributions, so if you find something that you can improve, open a pull-request! You can also check out our open issues. Please make sure you contribute to the rewrite branch, as we are going to replace the main branch with the rewrite.
Thanks so much for reading <3!