Updated docs to 0.3.1
This commit is contained in:
@@ -4,11 +4,11 @@ description: Directing execution with expression-based logic, pattern matching,
|
||||
icon: Split
|
||||
---
|
||||
|
||||
Control flow in Mist provides a bridge between C-style procedural logic and Rust's expression-oriented design. Blocks, if statements, while/for/loop loops, and match expressions all support statement bodies — meaning braces can be omitted for single-statement branches.
|
||||
Control flow in Mist provides a bridge between C-style procedural logic and Rust's expression-oriented design. Blocks, if statements, while/for/loop loops, and match expressions all support statement bodies.
|
||||
|
||||
## Conditionals
|
||||
|
||||
The `if` statement evaluates a boolean expression. Single-statement bodies don't need braces:
|
||||
The `if` statement evaluates a boolean expression:
|
||||
|
||||
```mist
|
||||
if (score > 50) {
|
||||
@@ -22,8 +22,8 @@ if (score > 50) {
|
||||
// Single-statement body (no braces needed)
|
||||
if (is_active) println!("Running");
|
||||
|
||||
// Expression body (soft return)
|
||||
var result = if (valid) "ok" else "err";
|
||||
// Expression body (implicit return)
|
||||
let result = if (valid) { "ok" } else { "err" };
|
||||
```
|
||||
|
||||
## Match
|
||||
@@ -32,18 +32,28 @@ The `match` statement provides exhaustive pattern matching with support for mult
|
||||
|
||||
```mist
|
||||
match (task_state) {
|
||||
TaskState::Pending => {
|
||||
println!("Queued");
|
||||
}
|
||||
TaskState::Pending => { println!("Queued"); }
|
||||
TaskState::Failed { reason, code } => {
|
||||
println!("Error {}: {}", code, reason);
|
||||
}
|
||||
TaskState::NotResponding | TaskState::Progress => {
|
||||
draw_loading();
|
||||
}
|
||||
_ => {
|
||||
println!("Other state");
|
||||
}
|
||||
_ => { println!("Other state"); }
|
||||
}
|
||||
```
|
||||
|
||||
Patterns support destructuring, or-patterns, and wildcards:
|
||||
|
||||
```mist
|
||||
let x = 2;
|
||||
let result;
|
||||
|
||||
match (x) {
|
||||
1 => { result = 10; }
|
||||
2 => { result = 20; }
|
||||
3 => { result = 30; }
|
||||
_ => panic!();
|
||||
}
|
||||
```
|
||||
|
||||
@@ -60,35 +70,31 @@ loop {
|
||||
}
|
||||
```
|
||||
|
||||
### C-Style For Loop
|
||||
|
||||
```mist
|
||||
for (var mut i = 0; i < 10; i++;)
|
||||
println!("Index: {}", i);
|
||||
```
|
||||
|
||||
### For-In Loop
|
||||
|
||||
For loops iterate over an expression using the `pattern : expr` syntax:
|
||||
|
||||
```mist
|
||||
for (var item in collection)
|
||||
process(item);
|
||||
for (i : 0..4) {
|
||||
sum += i;
|
||||
}
|
||||
|
||||
for ((i32 x, i32 y) in coordinates)
|
||||
draw_point(x, y);
|
||||
|
||||
// Range iteration
|
||||
for (var i in 0..10)
|
||||
println!("{}", i);
|
||||
// With pattern destructuring
|
||||
for ([k, _] : pairs) {
|
||||
keys += k;
|
||||
}
|
||||
```
|
||||
|
||||
### While Loop
|
||||
|
||||
```mist
|
||||
while (count < 5) {
|
||||
count++;
|
||||
}
|
||||
|
||||
while (active) {
|
||||
wait_for_event();
|
||||
}
|
||||
|
||||
while (count > 0) process(count--);
|
||||
```
|
||||
|
||||
## Jump Statements
|
||||
@@ -100,6 +106,6 @@ while (count > 0) process(count--);
|
||||
## Key Characteristics
|
||||
|
||||
- **Statement Bodies**: If, while, for, and loop branches can omit braces for single statements or expressions.
|
||||
- **Soft Returns**: Expression bodies (without `;`) implicitly return their value.
|
||||
- **Implicit Returns**: Expression bodies (without `;`) implicitly return their value.
|
||||
- **Pattern Integration**: Loops and match arms utilize Mist's pattern system for data destructuring.
|
||||
- **Multiple Patterns**: Match arms support `|` for matching multiple patterns.
|
||||
|
||||
@@ -4,16 +4,16 @@ description: The building blocks of logic, from literals to complex postfix chai
|
||||
icon: Binary
|
||||
---
|
||||
|
||||
Expressions in Mist are the fundamental units that evaluate to a value. The syntax follows a clean **prefix -> primary -> postfix** chain, providing a predictable structure that maps closely to Rust's mental model.
|
||||
Expressions in Mist are the fundamental units that evaluate to a value. The syntax follows a clean **prefix -> primary -> postfix** chain, providing a predictable structure.
|
||||
|
||||
## Primary Expressions
|
||||
|
||||
Primary expressions are the starting point of any logic chain. These include literal values, paths to static members, or grouped expressions in tuples.
|
||||
Primary expressions are the starting point of any logic chain. These include literal values, paths to members, tuples, arrays, and basic statements.
|
||||
|
||||
```mist
|
||||
var x = 42;
|
||||
var y = Math::PI;
|
||||
var coordinates = (10, 20, 30);
|
||||
let x = 42;
|
||||
let y = Math::PI;
|
||||
let coordinates = (10, 20, 30);
|
||||
```
|
||||
|
||||
## Postfix Operations
|
||||
@@ -21,21 +21,23 @@ var coordinates = (10, 20, 30);
|
||||
Postfix expressions allow you to build on a primary value with field access, calls, indexing, type casting, error propagation, and mutation operators.
|
||||
|
||||
```mist
|
||||
var len = list.length();
|
||||
let len = list.length();
|
||||
s.len();
|
||||
s.to_uppercase();
|
||||
|
||||
var task = Task {
|
||||
let task = Task {
|
||||
name: "Drafting",
|
||||
priority: 1,
|
||||
};
|
||||
|
||||
var first = items[0];
|
||||
let first = items[0];
|
||||
println!("Value: {}", first);
|
||||
```
|
||||
|
||||
### Increment & Decrement
|
||||
|
||||
```mist
|
||||
var mut i = 0;
|
||||
let mut i = 0;
|
||||
i++;
|
||||
i--;
|
||||
```
|
||||
@@ -56,8 +58,8 @@ flags |= 0x01;
|
||||
Use `as` to convert between compatible types:
|
||||
|
||||
```mist
|
||||
var x = 42;
|
||||
var y = x as f64;
|
||||
let x = 42;
|
||||
let y = x as f64;
|
||||
```
|
||||
|
||||
### Try Operator
|
||||
@@ -65,7 +67,7 @@ var y = x as f64;
|
||||
Propagate errors with the `?` postfix operator:
|
||||
|
||||
```mist
|
||||
var content = fs::read_to_string(path)?;
|
||||
let content = fs::read_to_string(path)?;
|
||||
```
|
||||
|
||||
### Range Operators
|
||||
@@ -80,30 +82,30 @@ var content = fs::read_to_string(path)?;
|
||||
Arrays are initialized with brackets, with an optional repeat notation:
|
||||
|
||||
```mist
|
||||
var arr = [1, 2, 3];
|
||||
var zeros = [0; 10]; // ten zeroes
|
||||
let arr = [1, 2, 3];
|
||||
let zeros = [0; 10]; // ten zeroes
|
||||
```
|
||||
|
||||
## Prefix Operations
|
||||
|
||||
Prefixes modify the primary expression that follows them.
|
||||
Prefixes modify the primary expression that follows them — dereference, reference, negation, and logical not.
|
||||
|
||||
```mist
|
||||
var mut value = 10;
|
||||
let mut value = 10;
|
||||
|
||||
var ref = &value;
|
||||
var mref = &mut value;
|
||||
var val = *ref;
|
||||
var is_false = !true;
|
||||
var neg = -42;
|
||||
let ref = &value;
|
||||
let mref = &mut value;
|
||||
let val = *ref;
|
||||
let is_false = !true;
|
||||
let neg = -42;
|
||||
```
|
||||
|
||||
## Binary Operations
|
||||
|
||||
```mist
|
||||
var sum = 10 + 20;
|
||||
var is_equal = (x == y);
|
||||
var complex = (a + b) * (c / d);
|
||||
let sum = 10 + 20;
|
||||
let is_equal = (x == y);
|
||||
let complex = (a + b) * (c / d);
|
||||
```
|
||||
|
||||
## Operator Table
|
||||
|
||||
@@ -4,62 +4,55 @@ description: Local state management with type inference and explicit mutability.
|
||||
icon: Variable
|
||||
---
|
||||
|
||||
In Mist, variables follow the language-wide `type name` convention. For local scope, the `var` keyword provides type inference, while explicit types can be used for clarity or strictness.
|
||||
Variables in Mist are declared with the `let` keyword. Like Rust, variables are immutable by default, and types are written after the name for scannability.
|
||||
|
||||
## Basic Declaration
|
||||
|
||||
Variables are declared using the `var` keyword for automatic type inference. Like Rust, variables are immutable by default.
|
||||
Variables use `let` for automatic type inference. The type annotation is optional — when omitted, the compiler infers the type from the value.
|
||||
|
||||
```mist
|
||||
var message = "Hello Mist";
|
||||
var count = 42;
|
||||
let x = 42;
|
||||
let greeting = "Hello Mist";
|
||||
```
|
||||
|
||||
## Mutability
|
||||
|
||||
To allow a variable to be reassigned, use the `mut` modifier after the `var` keyword or the explicit type.
|
||||
To allow a variable to be reassigned, use `let mut`:
|
||||
|
||||
```mist
|
||||
var mut score = 0;
|
||||
let mut score = 0;
|
||||
score = 100;
|
||||
|
||||
f32 mut price = 19.99;
|
||||
price = 14.99;
|
||||
```
|
||||
|
||||
## Explicit Typing
|
||||
|
||||
Type annotations are placed after the name:
|
||||
|
||||
```mist
|
||||
u64 large_id = 1000234;
|
||||
bool is_active = true;
|
||||
let id u64 = 1000234;
|
||||
let is_active bool = true;
|
||||
let name *str = "mist";
|
||||
```
|
||||
|
||||
## Arrays
|
||||
|
||||
```mist
|
||||
var list = [1, 2, 3]; // Standard init
|
||||
var zeros = [0; 10]; // Repeat notation: ten zeroes
|
||||
let list = [1, 2, 3]; // Standard init
|
||||
let zeros = [0; 10]; // Repeat notation: ten zeroes
|
||||
```
|
||||
|
||||
## Pattern Destructuring
|
||||
|
||||
```mist
|
||||
(i32, i32) (x, y) = get_coordinates();
|
||||
(String, i32) (name, age) = get_user_info();
|
||||
```
|
||||
|
||||
## Constants
|
||||
|
||||
Constants are immutable values evaluated at compile time with an explicit type.
|
||||
Tuples are destructured using square brackets:
|
||||
|
||||
```mist
|
||||
const i32 MAX_RETRIES = 5;
|
||||
const str* VERSION = "1.0.4";
|
||||
let [a, b] = (10, "hello");
|
||||
let [a, [b, c]] = (1, (2, 3));
|
||||
```
|
||||
|
||||
## Key Characteristics
|
||||
|
||||
- **Predictable Order**: Whether using `var` or an explicit type, the name always follows the "source" of its data.
|
||||
- **Predictable Order**: The `let` keyword signals a binding, followed by the name, optional type, and optional value.
|
||||
- **Safety First**: Immutability by default prevents accidental state changes.
|
||||
- **Zero-Cost Inference**: Type inference is handled entirely at compile time.
|
||||
- **Shadowing**: Mist supports variable shadowing within the same scope.
|
||||
|
||||
Reference in New Issue
Block a user