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

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 ./exampl…


This content originally appeared on DEV Community and was authored by lechat

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

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;

and what if they happen to be String typed integers?

let val = "5" + "6";

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;

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();

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();

You have to use mut to make it mutable variable.

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

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...


This content originally appeared on DEV Community and was authored by lechat


Print Share Comment Cite Upload Translate Updates
APA

lechat | Sciencx (2021-03-20T14:23:52+00:00) As a first project in Rust, I made a programming language on my own. Retrieved from https://www.scien.cx/2021/03/20/as-a-first-project-in-rust-i-made-a-programming-language-on-my-own/

MLA
" » As a first project in Rust, I made a programming language on my own." lechat | Sciencx - Saturday March 20, 2021, https://www.scien.cx/2021/03/20/as-a-first-project-in-rust-i-made-a-programming-language-on-my-own/
HARVARD
lechat | Sciencx Saturday March 20, 2021 » As a first project in Rust, I made a programming language on my own., viewed ,<https://www.scien.cx/2021/03/20/as-a-first-project-in-rust-i-made-a-programming-language-on-my-own/>
VANCOUVER
lechat | Sciencx - » As a first project in Rust, I made a programming language on my own. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/20/as-a-first-project-in-rust-i-made-a-programming-language-on-my-own/
CHICAGO
" » As a first project in Rust, I made a programming language on my own." lechat | Sciencx - Accessed . https://www.scien.cx/2021/03/20/as-a-first-project-in-rust-i-made-a-programming-language-on-my-own/
IEEE
" » As a first project in Rust, I made a programming language on my own." lechat | Sciencx [Online]. Available: https://www.scien.cx/2021/03/20/as-a-first-project-in-rust-i-made-a-programming-language-on-my-own/. [Accessed: ]
rf:citation
» As a first project in Rust, I made a programming language on my own | lechat | Sciencx | https://www.scien.cx/2021/03/20/as-a-first-project-in-rust-i-made-a-programming-language-on-my-own/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.