pranavv

dev log ·2024

Tidal

A programming language I built in Rust, with 80+ GitHub stars and 400+ upvotes on Reddit.

Role
Creator
Dates
2024-05-01 → 2024-12-01
Tags
RustInterpreterLanguage design

Ok, here is a project that I have been planning for a long time. My own Programming Language. Also, This Blog is a year in the making, and I appreciate you reading it. Thanks!

log 1·

My Journey Started When I started learning Python. Generally, for my whole life, I have been a curious child. When I First Started Learning Python, I wondered - "How do you make something like this?". To Find Answers, I went into the deep dark caves of low-level programming. You Might Think, "How can a 16 year old make a Programming Language?" Well, the answer is quite simple. I have been programming for a long time, and I have made many projects along the way. One of them include my own OS (InfinityX-OS↗) that I have made using x86 Assembly, C, C++, and a little bit of Python. It is nothing fancy, it just a bootloader that boots the kernel into memory, and, the kernel recognizes the memory present using PIC↗. Along the way, I found out that making your own programming language has a very similar process. It also uses C, and C++. I decided to give it a shot, and named it Blue Lagoon. 

I wanted to it be a compiler for a file name ending with .bl. Initially, I had just implemented a basic lexer (tokenizer) for a simple expression: "3 + 4 - 2". It would tokenize it, and provide me the details. Next up, was decoding files with ending .bl. This was going to be tricky. 

Before I get onto explaining the rest, I would like you to know that I have officially switched from C to Rust for all my Low-Level Projects. I really found it neat, and, I have decided to learn it, and adapt all my projects to Rust, including this Programming Language. Even for the Initial 1.0 Version, I have made it in Rust. So, before switching to Rust, I had to learn Rust. I found the official docs to be the best to learn rust. Rust by Example↗ is the best resource to learn Rust, personally for me, because I find learning programming languages especially hard though documentation, and Rust By Examples explain Rust using Examples.

So, After I had learnt the basics of Rust, I started thinking on this programming language again. I started by reading this Article↗ which helped me understand the basic structure of the programming language. Also, this Youtube Video↗ helped me aswell, in getting a grasp on the structures. After the Basic Understanding was done, I moved on to the implementation. I wanted this language to be Interpreted, so, my idea was to compile the interpreter, in which I can input the file, and it will be read and interpreted. After that, I started implementing on a Lexer, which would read through the file, ignore all unnecesary details, and distribute the data in a way that the Parser can understand it and check for tokens, keywords, literals, etc. The Lexer was initially hard to implement, especially because I was new to rust, but with a little help from Google and StackOverFlow, I got it done. Here is a sample code to explain what the lexer does:

var x = 5;
print(x + 3);

The Lexer breaks this down into tokens like: KEYWORD(var), IDENTIFIER(x), EQUALS, INTEGER(5), SEMICOLON, NEWLINE, IDENTIFIER(print), LEFT_PAREN, IDENTIFIER(x), PLUS, NUMBER(3), RIGHT_PAREN, SEMICOLON.

log 2·

Next up is the Parser. This is where things get really interesting (and challenging). The Parser needs to take these tokens and create an Abstract Syntax Tree (AST). I've been reading up on different parsing techniques like recursive descent and operator-precedence parsing. It's a lot to take in, but I'm excited to implement it! I've also started thinking about the features I want in Blue Lagoon. I'm aiming for a mix of simplicity and power. I like how Python is readable, so I'm trying to keep that in mind. At the same time, I want to incorporate some of Rust's safety features. It's a tricky balance!

log 3·

It's been a busy few months with school, but I've still been chipping away at Blue Lagoon when I can. The Parser is now working for most basic language constructs, including function definitions, if-statements, and while loops. It's creating a proper AST, which feels like a big accomplishmentThe next big challenge is the interpreter. I've started working on it, and it can now execute simple expressions and variable assignments. Here's a little taste of what it can do:

var x = 10;
var y = 20;
print(x + y);

This actually runs and outputs 30! It's a small step, but it feels amazing to see my language actually doing something.

log 4·

These past few months have been intense but rewarding. I had exams for the last month, so, I have not been able to work as much as I was before, learning Rust. But, I've made significant progress on the interpreter, and Tidal can now handle more complex thing. One exciting development is the implementation of various data types and operations. Check this out:

var x = 5;
var y = 3.14;
var z = x + y;
print(z);

This code actually runs and prints 8.14! It's really nice to see types and mixed data types working. I've implemented support for integers, floats, strings, and booleans, along with basic arithmetic and comparison operations. I also added support for if-else statements and while loops. Here's a simple example:

var i = 0;
while (i < 5) {
     print(i);
     i = i + 1; }

This will print the numbers from 0 to 4. It's really cool to see this implementation working now!

log 5·

At this point, I had decided to change the name of the language from "Blue Lagoon" to "Tidal". I feel like this new name better reflects the fluid and powerful nature of the language I'm trying to create.

I've implemented a basic error reporting system that provides more helpful messages. For example, if you try to use a variable that hasn't been declared, you'll get a clear error message.

Another exciting development: I've added support for both mutable and immutable variables using var and novar keywords respectively. This adds a layer of safety to the language. Here's an example:

var x = 5;
x = 10; /* This is fine */
novar y = 20;
y = 30; /* This would cause an error */

I've also implemented a type() function that returns the type of a given expression. This is really useful for debugging and understanding how the type system works.

log 6·

The last few months have been a combination of coding and problem-solving. I'm excited to share that Tidal now supports more complex control structures and has a more advanced type system.I've implemented for loops, which complement the existing while loops:

for (var i = 0; i < 5; i = i + 1) {
     print(i);
}

This functionality opens up so many possibilities for what can be done with Tidal! I've also added support for break and continue statements within loops, giving more control over loop execution.

One of the most exciting developments is the addition of type casting. You can now explicitly convert between different types:

var x = 5;
var y = float(x);
print(y);

 This feature, combined with the existing type inference, makes Tidal's type system much more flexible and powerful.I've also been working on optimizing the interpreter. The lexer and parser have been refactored for better performance, and the interpreter can now handle more complex programs efficiently.

final·

This is where this blog comes to a close. This was to explain my thinking process, as well as to build in public. This does not contain all the updates / features of the programming language, and, I will keep on updating Tidal, as time goes on. I have implement a lot more features that are not listed in this blog.

And just like that, this blog comes to and end. I really appreciate you for reading till the end, and, thank you for being a part of this journey! As for Tidal, that's a different story 😉

Comments

Loading comments…

All projects