Adana
repl / scripting language / namespaced command line aliases
Install / Use
/learn @nbittich/AdanaREADME
Adana
Scripting programming language, repl and namespaced aliases for commands.
Table of Contents
<hr>Introduction
This project started as a way to put into practice what I learned while reading the Rust programming language book.
It includes features that I find useful, such as a REPL, a calculator, a scripting language, and a way to store, execute, and load command-line aliases based on the project I'm working on.
Best practices and performance optimization were not a priority, so the code may not be the cleanest or most optimized.
If you would like to contribute, your pull request would be welcome.
Where the name comes from?
My favorite dish 😋
Installation
- Docker
- From the docker hub:
docker run -it nbittich/adana # latest from masterdocker run -it nbittich/adana:v0.18.8 # latest release
- Manually:
- clone the repo
- build the docker image:
docker build -t adana . docker run -it adana
- From the docker hub:
- Cargo
- From crate.io:
cargo install adanaadana
- Manually:
cargo build --release./target/release/adana
- From crate.io:
- WASM Playground
- Try it out at https://nbittich.github.io/adana
Programming language
Getting Started
First, we start with the traditional hello world:
println("hello world!") # prints hello world
In the repl, you could also simply write:
"hello world!" # prints hello world
<hr>
Comments
Comments are defined like in python, starting with #.
You can put them after the last statement or before any useful code, for example:
# to go to the next line in the repl, press CTRL+x
# this will be ignored by the repl
println("hello world!") # this is also ok
<hr>
Multiline
Semicolons are not needed, if you need multiline statements, you can use a multiline block:
fancy_string = multiline {
"this string\n" +
"\tis\n" +
"\t\ton several\n" +
"lines\n"
}
Multiline is useful when you want to process different instructions in several lines:
complex_math_stuff = multiline {
1 *2
+ 5 *sqrt(2) / 2.
+ 300 / 3
* 400 % 2 - (1 * 10^3)
}
<hr>
F-Strings
For more complex strings, you can use string blocks / F-Strings. You can define them using the java syntax:
block_string= """Hello world
I hope you are well.
This is a string block. you can use stuff like "string"
there, nothing will stop you"""
Like in javascript, you can embed expressions to an f-string:
person = struct {
name : "nordine",
wasup : (age) => {
if (age > 30) {
"you are old!"
} else {
"you are young!"
}
},
age : 34
}
s1 = """Hello ${person.name}!
You are ${person.age} years old.
${person.wasup(person.age)}"""
<hr>
Operators and constants
There are 22 operators & 3 constants:
| operator | description |
| ------------ | ---------------- |
| + | add |
| - | subtract |
| / | divide |
| * | multiply |
| % | modulo |
| ^ | pow |
| ² | pow 2 |
| ³ | pow 3 |
| < | less than |
| > | greater than |
| <= | less or equal |
| >= | greater or equal |
| && | and |
| \|\| | or |
| \| | bitwise or |
| ~ | bitwise not |
| @ | bitwise and |
| $ | bitwise xor |
| << | bitwise lshift |
| >> | bitwise rshift |
| == | equal |
| () | parenthesis |
| constant | description |
| ------------ | --------------- |
| π | PI number |
| γ | EULER number |
| τ | TAU number |
Example:
5 + 5 # 10
5 + 5.5 # 10.5
5 / 5 # 1
5 / 6 # 0
5 / 6. # 0.8333333333333334 -- we force it to make a float division by adding "."
5 % 6 # 5 -- modulo on int
5 % 4.1 # 0.9000000000000004 modulo on double
5 ^ 5 # 3125
5 * 5 # 25
5 * 5.1 # 25.5
5 * (5+ 1/ (3.1 ^2) * 9) ^3. # 1046.084549281999
2² # 4
2³ # 8
You can apply an operator before re-assigning a variable, like:
x =2
x+=1 # 3
x-=2 # 1
x*=4 # 4
x%=3 # 1
x/=0.5 # 2
It is legal in some circumstances to use the multiply operator implicitly. It will only work when there is no space between a number (int, decimal) and a variable name.
Example:
x=2
3x²+2x== x*(3x+2) # true
y=0.5x # 1
<hr>
Variable definition
To define a variable, simply type the name of the variable followed by "=". Variable must always start with a letter and can have numerics or "_" in it. Add and assign(+=), subtract and assign (-=), etc are also supported.
vat = 1.21 # 1.21
sub_total1 = 10000
total = vat * sub_total1 # 12100
sub_total2 = 500 * vat # 605
sub_total1 = sub_total1 + sub_total2 # 10605
It could be simplified as such:
vat = 1.21 # 1.21
sub_total1 = 10000
total = vat * sub_total1 # 12100
sub_total2 = 500 * vat # 605
sub_total1 += sub_total2 # 10605
It's also possible to use the special variable name _ to notify the language that this value
is not used and doesn't have to be stored in context:
_ = 1
for _, n in 1..3 {
println(n)
}
_ = struct {
_: "I will not be stored!",
x: 39
}
<hr>
Memory Management
By default, everything is cloned. As a hobby project, this was a simple way to achieve more and have fun.
Now that enough features have been built, an attempt to implement automatic-ish reference counting has started.
This is highly experimental and a partial or full rewrite of the ast / parser may be needed to implement it properly. To keep the fun working on this, it is not a priority for now.
You can define a reference as such:
x = 100
y = &x # y points to x, no clone
p = 0
for _ in 0..&x {
p = p+1
}
x = 99
y = &x
x = 100 # now y == 100
<hr>
Plugins
It is possible to load plugins written in Rust dynamically. Because Rust doesn't have a stable ABI yet, the plugin must be built with the same version that was used to build adana.
The Rust version is specified when running the repl.
To load a library dynamically, you can either specify a relative path, or an
absolute path. In case of a relative path, it should be relative to the
shared lib path (by default: $HOME/.local/share/adana/db).
You can override this by providing a path when starting the repl (e.g: adana -slp /tmp).
If the path is a directory, it will try to build it using cargo, so you
need to have Rust installed on your machine.
If it is an .so file, it will automatically load it.
An example of plugin can be found in this repo (dynamic_lib/example_lib_src).
For example:
- Copy the
.sofile in tmp:cp dynamic_lib/libplugin_example.so /tmp/ - Run and override the lib path:
adana -slp /tmp - Execute the following:
lib = require("libplugin_example.so")
text = lib.hello("Nordine", "la", "forme?")
Or in one line:
text = require("libplugin_example.so").hello("Nordine", "la", "forme?")
<hr>
Standard Library
A basic standard library exists here.
You can use it in this way:
fs = require("@std/fs")
fs.api_description() # description of the api
If it is not installed yet, you will see instructions on how to install it, e.g:
[rust~/toyprograms/adana(master)] fs = require("@std/fs")
std lib doesn't exist: "/home/nbittich/.local/share/adana/lib/adana-std/fs.so".
Try to install it like so:
- wget -P /tmp https://github.com/nbittich/adana-std/releases/download/0.18.8/adana-std.tar.gz
- mkdir -p /home/nbittich/.local/share/adana/lib/adana-std && tar xvzf /tmp/adana-std.tar.gz \
-C /home/nbittich/.local/share/adana/lib/adana-std
Loops
There are two loops, the while loop and the for-each loop. The while loop looks like the one in C, while the for-each loop is a little bit more modern.
For-each loop & while loop don't require parenthesizes. You can only iterate over structs, strings and arrays.
count = 0
while(count < 10) {
println(count)
count = count + 1
}
# also valid
while count < 10 {
println(count)
count = count + 1
}
for n in [1,2,3] {
println(n)
}
You have access to the current index in a for-each:
for index, n in [1, 2, 3] {
println("ind
