DEV Community

Cover image for Peregrine Update - The python-like language that's as fast as C
SaptakBhoumik
SaptakBhoumik

Posted on

Peregrine Update - The python-like language that's as fast as C

Hello everyone!
My name is Saptak and this article is going to explain the current state at where Peregrine is at.

What is Peregrine?

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

Update on Peregrine's current status

Decorators
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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Read my comments to understand how it works.
Note:Default block is optional

Javascript Backend

Other than the primary c++ backend peregrine also has a javascript backend. It is main purpose is to create website frontend which allows you to learn just 1 language to do both backend and frontend devolopment

Building it from source

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.

Requirements
1)g++(latest version)
2)meson
3)ninja
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
Enter fullscreen mode Exit fullscreen mode

2)Cd into the directory

cd Peregrine
Enter fullscreen mode Exit fullscreen mode

3)Build it

meson builddir
cd builddir
ninja
Enter fullscreen mode Exit fullscreen mode

This will create the binary of the compiler named ./peregrine.elf in the builddir folder

Using it

C++ backend
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
JS Backend
To use the javascript backend use the following command
./peregrine.elf compile path_to_file.js.pe -js.
It will create the javascript file named index.js. Run the generated javascript using 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.

Got any more questions that you would like to be answered?

You can open a new discussion that discusses your questions. You can also email saptakbhoumik@gmail.com, or join our discord server

Useful links

Github- https://github.com/peregrine-lang/Peregrine

Conclusion

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!

Top comments (0)