An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
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
Please note that you need to install Rust in your PC beforehand.
Then you can see 'Hello World' is printed as follows:
$ ./target/release/oran -f ./examples/hello.orn Hello World.
In this language, you can define functions as follows:
fn test () { println('hello');}test();
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
Of course you can define variables as follows:
fn test () { let test = 'hey'; println(test);}test();
$ ./target/release/oran -f ./examples/hello.orn hey
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();
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();
You can also use for-loop in this language.
fn test () { let test = ' test'; for i in 0..5 { println(i << test); }}test();
Result:
$ ./target/release/oran -f ./examples/hello.orn 0 test1 test2 test3 test4 test
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();
Result:
$ ./target/release/oran -f ./examples/hello.orn 0 test1 test2 test3 test4 test5 test
You can also use 'if':
fn test (test1, test2) { return test1 + test2;}if test(5,5) == 10 { println('it is ten!');}
Result:
$ ./target/release/oran -f ./examples/hello.ornit is ten!
Escaping is also implemented:
fn test (test1, test2) { test1 + test2}let t = 10;if t == test(5,5) { println("variable \"t\" is " << t);}
Result:
$ ./target/release/oran -f ./examples/hello.orn variable "t" is 10
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));
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
Original Link: https://dev.to/lechatthecat/i-made-a-scripting-language-in-rust-for-fun-4o2j
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To