DEV Community

loading...

As a first project in Rust, I made a programming language on my own

lechatthecat profile image lechat ・2 min read

This is the project:
https://github.com/lechatthecat/oran

You can test it by:

$ git clone https://github.com/lechatthecat/oran.git
$ cd oran
$ cargo build --release
$ ./target/release/oran -f ./examples/hello.orn
$ ./target/release/oran -f ./examples/example.orn
Enter fullscreen mode Exit fullscreen mode

Motivation

1. Rust seems cool, so I wanted to make something.

And I think it is a very nice language.

2. String concatenation or addition of numbers?

In many scripting languages, the concatenation of string is done by "aaaa" + "bbbb". This will make a value "aaaabbbb".

I really don't like this syntax. Let's suppose that we just want to add up two integers:

let val = 5 + 6;
Enter fullscreen mode Exit fullscreen mode

and what if they happen to be String typed integers?

let val = "5" + "6";
Enter fullscreen mode Exit fullscreen mode

The variable val's value will end up being "56" because both of them are String, not integers.

If they aren't stored in variables, it is obvious that you need to change them to numbers beforehand, but what if they are stored in variables?

let val = varfive + varsix;
Enter fullscreen mode Exit fullscreen mode

The value of val cannot be known until I check the value in debug mode. Even though I just wanted 11 out of 5 + 6, the value is actually "56". This would cause a serious bug.

The problem is, in most scripting languages, you cannot know what type the variables have. In scripting languages, variable types are dynamically changed.

So in my language, you have to use "<<" to concatenate string values:

fn test () {
    let test = ' test';
    for i in 0..5 {
        println(i << test); // Like this.
    }
}
test();
Enter fullscreen mode Exit fullscreen mode

When "<<" is used, the variables are always treated as String. Meanwhile, when "+" is used, the variables are always treated as numbers.

3. Immutability

Immutable variables are not often used in scripting languages like PHP, Python, Ruby. But immutable/mutable variables are often used in Javascript by putting const/let in front of variable names. I feel this is quite useful feature, that should be available in PHP, Python, Ruby also. And if variables are immutable by default like in Rust, it is even better.

So in my language, variables are immutable by default.

fn test () {
    let test = 'hey'; 
    test = "hello"; // Error!!
    println(test);
}
test();
Enter fullscreen mode Exit fullscreen mode

You have to use mut to make it mutable variable.

fn test () {
    let mut test = 'hey'; 
    test = "hello"; // Now it works.
    println(test);
}
test();
Enter fullscreen mode Exit fullscreen mode

Conclusion

Rust is a great language even though I am not familiar with this language at all. The good point of Rust is, the more I learn this language, the more nice things I can get to know, like, immutable variables, option and match, memory management, difference between passing by val and by ref, functional programming style etc...

Discussion (2)

pic
Editor guide
Collapse
aocsa profile image
Alexander Ocsa

Nice way to hack a new PL

Collapse
lechatthecat profile image
lechat Author

Thank you :)