Skip to main content

Crate conlang

Crate conlang 

Source
Expand description

The new Conlang REPL

ยงIntroduction

Conlang is a statically-typed expressional language in the ML language family.

It aims for maximal flexibility at (almost) any cost, allowing you to use (almost) any syntax in (almost) any context, with as minimal bracketing as possible.

ยงSyntax

โ“˜
//! This is the Conlang syntax tutorial example

/// `let` expressions try to bind variables
let x = 1, 2, 3;
/// ...and return `true` if they succeeded!
if let 1, two, 3 = x {
    println(two) // prints "2"
}


/// `fn` expressions bind functions to names
fn do_thing(x: i32, y: i32) -> i32 {
    x + y
}
fn do_thing_1(x: i32, y: i32) -> i32 = x + y; // Alt syntax

/// ...and return a function object!
let do_thing_2 = fn (x: i32, y: i32) = x + y;
let do_thing_3 = |x: i32, y: i32| x + y; // Alt syntax

/// You can invoke that function by `call`ing it:
do_thing(10, 20);
do_thing_1(10, 20);
do_thing_2(10, 20);
do_thing_3(10, 20);


/// `return` expressions end the containing function,
fn first_even(numbers: &[i32]) -> i32 {
    for index in 0..numbers.len() {
        // and can be used *anywhere an expression is valid!*
        if numbers[index] % 2 == 0 return index
    }
    -1
}


/// `type`, `struct`, and `enum` expressions bind types to names
enum Option<T> {
    Some(T),
    None,
}

/// ...and return a type-info object!
//     Note: this *also* introduces the name `Foo` in scope.
let ti = struct Foo { x: i32, y: i32 };
ti::x == i32 /* true */;


/// `if` expressions branch execution based on a value:
if condition() {
    do_thing(10, 20)
} else {
    do_not()
}

/// ... and return the result of whichever branch executed!
let v = if condition() { 
    do_thing(10, 20)
} else {
    do_not()
}

/// The `pass` and `fail` expressions don't have to be blocks:
let v = if condition() do_thing(10, 20) else do_not();
// but if the `pass` expression starts with something that can be
// an infix operator (i.e. `+`, `-`, `(...)`, ...), it'll
// become part of the condition instead.
//
// Let your heart guide you.


/// `loop` expressions run the same calculation multiple times:
loop print(1); // prints infinite `1`s

/// ...and return the value passed to `break`!
let v = loop break 10;


/// `while` expressions loop while a condition is true:
while condition() print(1);

/// ...and return *either* the value passed to `break`,
///    if there is one, or the result of executing their
///    `else` branch!
let v = while condition() {
    /// do calculation... Maybe `break value`...
    let next_value = 0;
    if is_special(next_value) break next_value
} else {
    default_value() // executed if the body didn't `break`
}

/// `for` expressions loop over an iterator, such as a range
for value in 1..=100 {
    println(value)
}

/// ...and have the same `break` semantics as `while` loops!
let v = for value in 1..=100 {
    if is_special(value) break value
} else default_value();


/// "Do" expressions discard the result of evaluating the
/// previous expression, and return the result of the
/// following expression:
let v = (
    print("I did this!"); // "I did this!"
    "and then returned this!"
);
print(v); // "and then returned this!"

// You'll notice that this entire tutorial section
// has been separated by `do` operators (semicolons.)


/// "Block" expressions introduce a local scope, in which
/// new names can be bound and later discarded.
/// They are otherwise equivalent to parentheses.
{
    let explanation = "This is a block expression!";
    print(explanation);
    // If a closing brace follows a `do` operator,
    // the block evaluates to the unit type/value.
}

/// After a block-expression, the parser *may* insert a
/// semicolon if it makes sense to do so.
print("So, even though there was no ';', this still works!");

Modulesยง

builtin ๐Ÿ”’

Enumsยง

ParseMode ๐Ÿ”’
What the next operation should be
Verbosity ๐Ÿ”’
How much information to show about results

Functionsยง

banner ๐Ÿ”’
Prints the --- conlang version --- banner
bubble ๐Ÿ”’
Performs experimental desugaring on expressions from the input
clear ๐Ÿ”’
Clears the terminal
inline_modules ๐Ÿ”’
Inlines modules at a given T relative to the PWD
main ๐Ÿ”’
pargs ๐Ÿ”’
Parses Args
parse ๐Ÿ”’
Parses and displays Ts from the input
plural ๐Ÿ”’
Gets the English-language pluralizer for count
run ๐Ÿ”’
Parses and executes expressions from the input
subst ๐Ÿ”’
Performs macro substitution
tokens ๐Ÿ”’
Prints the tokenization of the input
usage ๐Ÿ”’
Prints the usage string

Type Aliasesยง

Args ๐Ÿ”’
The return type of pargs