₊˚ Elle ♡︎
An experimental, purely functional language built in Rust
This language is not designed to be used by any developer, it is intended simply as an experiment to better understand the internals of modern languages and how they work :3
✩ If you like this project, consider giving it a star! ✩
♡ Hello, World!
Writing a hello world program in Elle is super easy.
In fact it’s very similar to C!
pub op main() {
puts("Hello world!\n");
}
Let’s dissect the code:
-
The pub keyword declares the main function as public/exported
-
The op keyword declares the identifier as an operation (Elle’s version of a function)
-
The word main defines the function as the entry point of our program.
-
The function call puts is interoperable with C. It takes the 0th argument and writes it to the standard output.
-
That’s it! ✩
Elle uses the QBE compiler backend. This means that files compile into QBE’s intermediate language before being executed.
Let’s also take a look at the QBE IL source:
data $main.0 = { b "Hello world!\n", b 0 }
export function w $main() {
@start
call $puts(l $main.0)
ret 0
}
-
The main.0 data segment is used to store the literal string later used in puts
-
The function is exported, as denoted with the export keyword
-
The function returns a w (word), is called main, and uses the $ sigil to denote it is global.
-
The @start directive describes the beginning of the function
-
We then use the call operation and the global puts function with the l (long) data section we stored earlier.
-
The compiler falls back to returning the literal 0 if no specific return value is specified. Therefore, we ret 0 at the end.
-
Simple enough! ♡
♡ Example Syntax
Please keep in mind that syntax such as if statements, loops, match cases, optional types, and keyword arguments for function calls have not been parsed or compiled yet, only lexed. In fact, the keyword arguments haven’t even been lexed yet. This means that the code below will not compile. It is indended as a pure example of how the syntax is designed to look later on.
// Import statements follow a lib:file@{method1, method2...} format;
use elle:io@{print};
use elle:int@{random};
const languageName = "Elle";
// Use `pub` to make functions public so they can be imported by other files
// You *must* expose the main function for it to be runnable
pub op main() {
let resWithThree: Int = randomWithMultiplier(3); // Returns a random number between 0 and 10 multiplied by 3 using positional arguments
let resWithSixteen: Int = randomWithMultiplier(multiplier: 16); // Returns a random number between 0 and 10 multiplied by 16 using keyword arguments
printMessage(
"First result is %d and second is %d",
resWithThree,
resWithSixteen
);
let maybeRes: Int? = randomWithPossibleError();
if (maybeRes) {
// In this scope, maybeRes is just Int32 not Int32?
printMessage("Result is %d", maybeRes);
} else {
// This is a character because it's a single quote.
printMessage('a');
printMessage("Oh no! We failed.");
}
}
op randomWithMultiplier(Int multiplier) -> Int {
// If a function uses keyword arguments they must *all* be keyword arguments
// Use the ret keyword to return from the operation
ret random(between: 0, and: 10, included: true) * multiplier;
}
// Operations can either return a value or void.
// `nil` is the undefined/null value in Elle.
// Use the '?' operator at the end of the return type to denote that the function can return nil.
op randomWithPossibleError() -> Int? {
let result: Int = random(0, 5, true);
// Match keyword works very similar to other languages
ret match result {
3 -> nil,
val -> val,
}
}
// No return argument needed if function returns void
// Note that this is *only* if the function returns void
op printMessage(String message) {
printf(`[%s] %s`, languageName, message);
}
- Semicolons are enforced and are required for parsing
- Comments start with // and are ignored when parsing
- Single quotes are strictly for characters
- Double quotes are strictly for strings
- Constants must be at the top level of files & start with
const
- Variables must be inside of operations & start with
let
- Returning from operations is done with the
ret keyword
If you have any questions, please raise an issue :3
♡ How to run
- Elle has only one third-party dependency, so you can simply do the following:
-
Ensure you have Cargo, Rust, and the QBE compiler backend.
$ git clone https://github.com/acquitelol/elle
$ cd elle/dist
$ cargo run
$ qbe -o out.s main.ssa && cc out.s && ./a.out
-
You’re done!
You can now edit the main.elle file in the dist directory, and then rebuild to see the output.
♡ Licensing
⇡ Back to top️!
评论