129 lines
3.1 KiB
Plaintext
129 lines
3.1 KiB
Plaintext
---
|
|
title: Expressions
|
|
description: The building blocks of logic, from literals to complex postfix chains.
|
|
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.
|
|
|
|
## Primary Expressions
|
|
|
|
Primary expressions are the starting point of any logic chain. These include literal values, paths to members, tuples, arrays, and basic statements.
|
|
|
|
```mist
|
|
let x = 42;
|
|
let y = Math::PI;
|
|
let coordinates = (10, 20, 30);
|
|
```
|
|
|
|
## Postfix Operations
|
|
|
|
Postfix expressions allow you to build on a primary value with field access, calls, indexing, type casting, error propagation, and mutation operators.
|
|
|
|
```mist
|
|
let len = list.length();
|
|
s.len();
|
|
s.to_uppercase();
|
|
|
|
let task = Task {
|
|
name: "Drafting",
|
|
priority: 1,
|
|
};
|
|
|
|
let first = items[0];
|
|
println!("Value: {}", first);
|
|
```
|
|
|
|
### Increment & Decrement
|
|
|
|
```mist
|
|
let mut i = 0;
|
|
i++;
|
|
i--;
|
|
```
|
|
|
|
### Compound Assignments
|
|
|
|
```mist
|
|
i += 10;
|
|
i -= 5;
|
|
i *= 2;
|
|
i /= 3;
|
|
value &= mask;
|
|
flags |= 0x01;
|
|
```
|
|
|
|
### Type Casting
|
|
|
|
Use `as` to convert between compatible types:
|
|
|
|
```mist
|
|
let x = 42;
|
|
let y = x as f64;
|
|
```
|
|
|
|
### Try Operator
|
|
|
|
Propagate errors with the `?` postfix operator:
|
|
|
|
```mist
|
|
let content = fs::read_to_string(path)?;
|
|
```
|
|
|
|
### Range Operators
|
|
|
|
```mist
|
|
0..10 // exclusive range (0 to 9)
|
|
0..=10 // inclusive range (0 to 10)
|
|
```
|
|
|
|
### Arrays
|
|
|
|
Arrays are initialized with brackets, with an optional repeat notation:
|
|
|
|
```mist
|
|
let arr = [1, 2, 3];
|
|
let zeros = [0; 10]; // ten zeroes
|
|
```
|
|
|
|
## Prefix Operations
|
|
|
|
Prefixes modify the primary expression that follows them — dereference, reference, negation, and logical not.
|
|
|
|
```mist
|
|
let mut value = 10;
|
|
|
|
let ref = &value;
|
|
let mref = &mut value;
|
|
let val = *ref;
|
|
let is_false = !true;
|
|
let neg = -42;
|
|
```
|
|
|
|
## Binary Operations
|
|
|
|
```mist
|
|
let sum = 10 + 20;
|
|
let is_equal = (x == y);
|
|
let complex = (a + b) * (c / d);
|
|
```
|
|
|
|
## Operator Table
|
|
|
|
| Category | Operators |
|
|
| -------------- | ---------------------------------------------------------------- |
|
|
| **Arithmetic** | `+`, `-`, `*`, `/`, `%` |
|
|
| **Comparison** | `==`, `!=`, `<`, `>`, `<=`, `>=` |
|
|
| **Logical** | `&&`, `||` |
|
|
| **Bitwise** | `<<`, `>>`, `&`, `\|`, `^` |
|
|
| **Range** | `..`, `..=` |
|
|
| **Assign** | `=`, `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `\|=`, `^=`, `<<=`, `>>=` |
|
|
|
|
## Key Characteristics
|
|
|
|
- **Predictable Chaining**: The `prefix* ~ primary ~ postfix*` grammar ensures complex expressions are parsed consistently.
|
|
- **Rust-Style References**: Expressions use `&` and `&mut` to create references, maintaining borrow checker compatibility.
|
|
- **Macro Integration**: Macros use `!` as a postfix operation.
|
|
- **Type Casting**: `as Type` provides explicit type conversion at the expression level.
|
|
- **Error Propagation**: The `?` operator enables early returns for `Result`/`Option` types.
|