DEV Community

Cover image for I made a scripting language in Rust for fun
lechat
lechat

Posted on • Updated on

I made a scripting language in Rust for fun

I wouldn't say my code is beautiful but you can see all the code in the github repository.
https://github.com/lechatthecat/oran

I made a scripting language in Rust. You can try this language by

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

Please note that you need to install git and Rust in your PC beforehand.

Then you can see 'Hello World' is printed as follows:

$ ./target/release/oran -f  ./examples/hello.orn 
Hello World.
Enter fullscreen mode Exit fullscreen mode

In this language, you can define functions as follows:

fn test () {
    println('hello');
}

test();
Enter fullscreen mode Exit fullscreen mode

You can copy-and-paste the above in ./examples/hello.orn and run it. If you run this script, you can see the function is called as follows:

$ ./target/release/oran -f  ./examples/hello.orn 
hello
Enter fullscreen mode Exit fullscreen mode

Of course you can define variables as follows:

fn test () {
    let test = 'hey';
    println(test);
}

test();
Enter fullscreen mode Exit fullscreen mode
$ ./target/release/oran -f  ./examples/hello.orn 
hey
Enter fullscreen mode Exit fullscreen mode

But variables are immutable by default. You can't substitute other values to the variable.

fn test () {
    let test = 'hey';
    test = 'hello'; // error!
    println(test);
}

test();
Enter fullscreen mode Exit fullscreen mode

You must define it as a mutable variable to substitute other values:

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

test();
Enter fullscreen mode Exit fullscreen mode

You can also use for-loop in this language.

fn test () {
    let test = ' test';
    for i in 0..5 {
        println(i << test);
    }
}

test();
Enter fullscreen mode Exit fullscreen mode

Result:

$ ./target/release/oran -f  ./examples/hello.orn 
0 test
1 test
2 test
3 test
4 test
Enter fullscreen mode Exit fullscreen mode

If you want to do inclusive for-loop, you can write as follows:

fn test () {
    let test = ' test';
    for i in 0..=5 {
        println(i << test);
    }
}

test();
Enter fullscreen mode Exit fullscreen mode

Result:

$ ./target/release/oran -f  ./examples/hello.orn 
0 test
1 test
2 test
3 test
4 test
5 test
Enter fullscreen mode Exit fullscreen mode

You can also use 'if':

fn test (test1, test2) {
    return test1 + test2;
}

if test(5,5) == 10 {
   println('it is ten!');
}
Enter fullscreen mode Exit fullscreen mode

Result:

$ ./target/release/oran -f  ./examples/hello.orn
it is ten!
Enter fullscreen mode Exit fullscreen mode

Escaping is also implemented:

fn test (test1, test2) {
    test1 + test2
}
let t = 10;
if t == test(5,5) {
   println("variable \"t\" is " << t);
}
Enter fullscreen mode Exit fullscreen mode

Result:

$ ./target/release/oran -f  ./examples/hello.orn 
variable "t" is 10
Enter fullscreen mode Exit fullscreen mode

Also, as you could see, you can omit 'return' in function just like in Rust:

/*  both functions work  */
fn test (test1, test2) {
    test1 + test2
}
fn test2 (test1, test2) {
    return test1 * test2;
}

println(test(5,5));
println(test2(5,5));
Enter fullscreen mode Exit fullscreen mode

You can see many other random dirty examples in
https://github.com/lechatthecat/oran/blob/master/examples/example.orn
You can run it by:

$ ./target/release/oran -f  ./examples/example.orn 
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
levirs565 profile image
Levi Rizki Saputra

Great project!

Collapse
 
lechatthecat profile image
lechat

Thank you very much :)