YREA-SLS/docs/complete_operator_reference.md

547 lines
17 KiB
Markdown

## Appendix C: Complete Operator Reference
This appendix provides a complete alphabetical reference of all operators in the language.
### Operator List
`!=`, `%`, `&` (bitand), `*`, `+`, `-`, `/`, `<`, `<=`, `==`, `>`, `>=`, `^`, `|` (bitor), `and`, `at`, `bitand`, `bitnot`, `bitor`, `bitxor`, `break`, `concat`, `continue`, `depth`, `drop`, `dup`, `each`, `enum`, `eval`, `filter`, `fn`, `for`, `get`, `if`, `impl`, `inher`, `lambda`, `length`, `ln`, `log`, `logb`, `map`, `match`, `not`, `or`, `over`, `pick`, `reduce`, `reverse`, `roll`, `rot`, `set`, `shl`, `shr`, `slice`, `struct`, `substr`, `swap`, `trait`, `transpose`, `union`, `while`, `window`
### Alphabetical Operator Reference
#### !=
**Operator Type**: Comparison
**Signature**: `(Equatable Equatable -- bool)`
**Trait**: Equatable
**Description**: Test if two values are not equal.
**Example**: `5 3 != // => true`
**See Also**: ==, <, >
**Section**: 4.3 (Comparison Operators)
#### %
**Operator Type**: Arithmetic
**Signature**: `(Multiplyable Multiplyable -- Multiplyable)`
**Trait**: Multiplyable
**Description**: Remainder after division (modulo).
**Example**: `17 5 % // => 2`
**See Also**: /, *
**Section**: 4.2 (Arithmetic Operators)
#### *
**Operator Type**: Arithmetic
**Signature**: `(Multiplyable Multiplyable -- Multiplyable)`
**Trait**: Multiplyable
**Description**: Multiply two values.
**Example**: `5 6 * // => 30`
**See Also**: /, +, -
**Section**: 4.2 (Arithmetic Operators)
#### +
**Operator Type**: Arithmetic
**Signature**: `(Addable Addable -- Addable)`
**Trait**: Addable
**Description**: Add two values.
**Example**: `3 4 + // => 7`
**See Also**: -, *, /
**Section**: 4.2 (Arithmetic Operators)
#### -
**Operator Type**: Arithmetic
**Signature**: `(Addable Addable -- Addable)`
**Trait**: Addable
**Description**: Subtract second value from first.
**Example**: `10 3 - // => 7`
**See Also**: +, *, /
**Section**: 4.2 (Arithmetic Operators)
#### /
**Operator Type**: Arithmetic
**Signature**: `(Multiplyable Multiplyable -- Multiplyable)`
**Trait**: Multiplyable
**Description**: Divide first value by second.
**Example**: `20 4 / // => 5`
**See Also**: *, %, +
**Section**: 4.2 (Arithmetic Operators)
#### <
**Operator Type**: Comparison
**Signature**: `(Orderable Orderable -- bool)`
**Trait**: Orderable
**Description**: True if first value is less than second.
**Example**: `3 5 < // => true`
**See Also**: <=, >, >=
**Section**: 4.3 (Comparison Operators)
#### <=
**Operator Type**: Comparison
**Signature**: `(Orderable Orderable -- bool)`
**Trait**: Orderable
**Description**: True if first value is less than or equal to second.
**Example**: `5 5 <= // => true`
**See Also**: <, >, >=
**Section**: 4.3 (Comparison Operators)
#### ==
**Operator Type**: Comparison
**Signature**: `(Equatable Equatable -- bool)`
**Trait**: Equatable
**Description**: Test if two values are equal.
**Example**: `5 5 == // => true`
**See Also**: !=, <, >
**Section**: 4.3 (Comparison Operators)
#### >
**Operator Type**: Comparison
**Signature**: `(Orderable Orderable -- bool)`
**Trait**: Orderable
**Description**: True if first value is greater than second.
**Example**: `5 3 > // => true`
**See Also**: >=, <, <=
**Section**: 4.3 (Comparison Operators)
#### >=
**Operator Type**: Comparison
**Signature**: `(Orderable Orderable -- bool)`
**Trait**: Orderable
**Description**: True if first value is greater than or equal to second.
**Example**: `5 5 >= // => true`
**See Also**: >, <, <=
**Section**: 4.3 (Comparison Operators)
#### ^
**Operator Type**: Arithmetic
**Signature**: `(Exponentiable Exponentiable -- Exponentiable)`
**Trait**: Exponentiable
**Description**: Raise first value to power of second (exponentiation).
**Example**: `2 8 ^ // => 256`
**See Also**: log, ln, logb
**Section**: 4.2 (Arithmetic Operators)
#### and
**Operator Type**: Logical
**Signature**: `(Logical Logical -- Logical)`
**Trait**: Logical
**Description**: Logical AND - returns first if falsy, else second.
**Example**: `true false and // => false`
**See Also**: or, not
**Section**: 4.4 (Logical Operators)
#### at
**Operator Type**: Container Access
**Signature**: `(Selectable<T> Size -- T)`
**Trait**: Selectable<T>
**Description**: Access element at given index.
**Example**: `[10 20 30] 1 at // => 20`
**See Also**: slice, length
**Section**: 7.4 (Arrays)
#### bitand
**Operator Type**: Bitwise
**Signature**: `(Bitwise Bitwise -- Bitwise)`
**Trait**: Bitwise
**Description**: Bitwise AND of two values.
**Example**: `0xFF 0x0F bitand // => 0x0F`
**See Also**: bitor, bitxor, bitnot
**Section**: 4.5 (Bitwise Operators)
#### bitnot
**Operator Type**: Bitwise
**Signature**: `(Bitwise -- Bitwise)`
**Trait**: Bitwise
**Description**: Bitwise NOT (complement) of value.
**Example**: `0xFF bitnot // => (inverted bits)`
**See Also**: bitand, bitor, bitxor
**Section**: 4.5 (Bitwise Operators)
#### bitor
**Operator Type**: Bitwise
**Signature**: `(Bitwise Bitwise -- Bitwise)`
**Trait**: Bitwise
**Description**: Bitwise OR of two values.
**Example**: `0xF0 0x0F bitor // => 0xFF`
**See Also**: bitand, bitxor, bitnot
**Section**: 4.5 (Bitwise Operators)
#### bitxor
**Operator Type**: Bitwise
**Signature**: `(Bitwise Bitwise -- Bitwise)`
**Trait**: Bitwise
**Description**: Bitwise XOR of two values.
**Example**: `0xFF 0x0F bitxor // => 0xF0`
**See Also**: bitand, bitor, bitnot
**Section**: 4.5 (Bitwise Operators)
#### break
**Operator Type**: Control Flow
**Signature**: `(--)`
**Description**: Exit the current loop immediately.
**Example**: `{ true } { condition { break } { } if } while`
**See Also**: continue, while, for
**Section**: 6.4 (Loop Control)
#### concat
**Operator Type**: Container
**Signature**: `(Concatenable Concatenable -- Concatenable)`
**Trait**: Concatenable
**Description**: Concatenate two containers or strings.
**Example**: `[1 2 3] [4 5 6] concat // => [1 2 3 4 5 6]`
**See Also**: slice, reverse
**Section**: 7.4 (Arrays)
#### continue
**Operator Type**: Control Flow
**Signature**: `(--)`
**Description**: Skip to the next iteration of the current loop.
**Example**: `1 10 { dup 2 % 0 == { continue } { print } if } for`
**See Also**: break, while, for
**Section**: 6.4 (Loop Control)
#### depth
**Operator Type**: Stack Manipulation
**Signature**: `(-- Size)`
**Trait**: Stackable
**Description**: Push current stack depth onto the stack.
**Example**: `1 2 3 depth // => 1 2 3 3`
**See Also**: pick, roll
**Section**: 4.1 (Stack Operations)
#### drop
**Operator Type**: Stack Manipulation
**Signature**: `(Self --)`
**Trait**: Stackable
**Description**: Remove and discard the top item from the stack.
**Example**: `5 10 drop // => 5`
**See Also**: dup, swap
**Section**: 4.1 (Stack Operations)
#### dup
**Operator Type**: Stack Manipulation
**Signature**: `(Self -- Self Self)`
**Trait**: Stackable
**Description**: Duplicate the top item on the stack.
**Example**: `5 dup // => 5 5`
**See Also**: drop, over
**Section**: 4.1 (Stack Operations)
#### each
**Operator Type**: Array Combinator
**Signature**: `(ArrayOf<T> TokenString --)`
**Description**: Apply function to each element (side effects).
**Example**: `[1 2 3] { print } each`
**See Also**: map, filter, reduce
**Section**: 7.4 (Arrays)
#### enum
**Operator Type**: Definition
**Signature**: `(TokenString Identifier --)`
**Trait**: Implementable
**Description**: Define an enumeration type.
**Example**: `{ Pending: Active: Complete: } ::Status enum`
**See Also**: struct, union, trait
**Section**: 7.3 (Enums)
#### eval
**Operator Type**: Meta
**Signature**: `(TokenString --)`
**Trait**: Implementable
**Description**: Parse and execute TokenString as code.
**Example**: `"2 3 +" eval // => 5`
**See Also**: lambda
**Section**: 11.1 (Dynamic Code Evaluation)
#### filter
**Operator Type**: Array Combinator
**Signature**: `(ArrayOf<T> TokenString -- ArrayOf<T>)`
**Description**: Keep only elements matching predicate.
**Example**: `[1 2 3 4 5] { 2 % 0 == } filter // => [2 4]`
**See Also**: map, reduce, each
**Section**: 7.4 (Arrays)
#### fn
**Operator Type**: Definition
**Signature**: `(TypeTuple TokenString Identifier --)`
**Trait**: Implementable
**Description**: Define a function.
**Example**: `(Number -- Number) { dup * } ::square fn`
**See Also**: lambda, trait, impl
**Section**: 5.2 (Defining Functions)
#### for
**Operator Type**: Control Flow
**Signature**: `(Size Size TokenString --)`
**Description**: Loop from start to end, pushing counter each iteration.
**Example**: `1 10 { dup print } for`
**See Also**: while, break, continue
**Section**: 6.3 (For Loops)
#### get
**Operator Type**: Struct Access
**Signature**: `(Struct Identifier -- FieldValue)`
**Description**: Get field value from struct (consumes struct and field identifier).
**Example**: `point ::x get`
**See Also**: set
**Section**: 7.1 (Structs)
#### if
**Operator Type**: Control Flow
**Signature**: `(bool TokenString TokenString --)`
**Description**: Conditional execution - execute first block if true, second if false.
**Example**: `x 0 > { "positive" print } { "negative" print } if`
**See Also**: match, while
**Section**: 6.1 (Conditionals)
#### impl
**Operator Type**: Definition
**Signature**: `(Identifier TokenString Identifier --)`
**Trait**: Implementable
**Description**: Implement a trait for a type.
**Example**: `::Addable { ... } ::Point impl`
**See Also**: trait, inher
**Section**: 9.3 (Implementing Traits)
#### inher
**Operator Type**: Definition
**Signature**: `(ArrayOf<Identifier> Identifier --)`
**Trait**: Implementable
**Description**: Declare trait inheritance.
**Example**: `[ ::Orderable ::Equatable ] ::Comparable inher`
**See Also**: trait, impl
**Section**: 9.4 (Trait Inheritance)
#### lambda
**Operator Type**: Meta
**Signature**: `(TokenString -- Callable)`
**Trait**: Implementable
**Description**: Convert TokenString to callable code block.
**Example**: `{ dup * } lambda ::square swap`
**See Also**: eval, fn
**Section**: 5.6 (Lambda Functions)
#### length
**Operator Type**: Container
**Signature**: `(Sized -- i64)`
**Trait**: Sized
**Description**: Get the number of elements in a container.
**Example**: `[1 2 3 4 5] length // => 5`
**See Also**: at, slice
**Section**: 7.4 (Arrays)
#### ln
**Operator Type**: Arithmetic
**Signature**: `(Logarithmic -- Logarithmic)`
**Trait**: Logarithmic
**Description**: Natural logarithm (base e).
**Example**: `2.718 ln // => 1.0`
**See Also**: log, logb, ^
**Section**: 4.2 (Arithmetic Operators)
#### log
**Operator Type**: Arithmetic
**Signature**: `(Logarithmic -- Logarithmic)`
**Trait**: Logarithmic
**Description**: Logarithm base 10.
**Example**: `100 log // => 2.0`
**See Also**: ln, logb, ^
**Section**: 4.2 (Arithmetic Operators)
#### logb
**Operator Type**: Arithmetic
**Signature**: `(Logarithmic Logarithmic -- Logarithmic)`
**Trait**: Logarithmic
**Description**: Logarithm with custom base.
**Example**: `8 2 logb // => 3.0`
**See Also**: log, ln, ^
**Section**: 4.2 (Arithmetic Operators)
#### map
**Operator Type**: Array Combinator
**Signature**: `(ArrayOf<T> TokenString -- ArrayOf<U>)`
**Description**: Transform each element with function.
**Example**: `[1 2 3 4] { 2 * } map // => [2 4 6 8]`
**See Also**: filter, reduce, each
**Section**: 7.4 (Arrays)
#### match
**Operator Type**: Control Flow
**Signature**: `(Value TokenString --)`
**Description**: Pattern match value against multiple patterns.
**Example**: `opt { Some(x) => { x print } None => { "Nothing" print } } match`
**See Also**: if
**Section**: 6.5 (Pattern Matching)
#### not
**Operator Type**: Logical
**Signature**: `(Logical -- Logical)`
**Trait**: Logical
**Description**: Logical NOT - inverts truthiness.
**Example**: `false not // => true`
**See Also**: and, or
**Section**: 4.4 (Logical Operators)
#### or
**Operator Type**: Logical
**Signature**: `(Logical Logical -- Logical)`
**Trait**: Logical
**Description**: Logical OR - returns first if truthy, else second.
**Example**: `true false or // => true`
**See Also**: and, not
**Section**: 4.4 (Logical Operators)
#### over
**Operator Type**: Stack Manipulation
**Signature**: `(Self Self -- Self Self Self)`
**Trait**: Stackable
**Description**: Copy the second item to the top of the stack.
**Example**: `5 10 over // => 5 10 5`
**See Also**: dup, swap, rot
**Section**: 4.1 (Stack Operations)
#### pick
**Operator Type**: Stack Manipulation
**Signature**: `(Size -- Self)`
**Trait**: Stackable
**Description**: Copy nth item to top (0 = top, 1 = second, etc.).
**Example**: `1 2 3 4 2 pick // => 1 2 3 4 2`
**See Also**: roll, depth
**Section**: 4.1 (Stack Operations)
#### reduce
**Operator Type**: Array Combinator
**Signature**: `(ArrayOf<T> T TokenString -- T)`
**Description**: Fold array with accumulator function.
**Example**: `[1 2 3 4] 0 { + } reduce // => 10`
**See Also**: map, filter, each
**Section**: 7.4 (Arrays)
#### reverse
**Operator Type**: Array
**Signature**: `(ArrayOf<T> -- ArrayOf<T>)`
**Description**: Reverse order of array elements.
**Example**: `[1 2 3] reverse // => [3 2 1]`
**See Also**: transpose, concat
**Section**: 7.4 (Arrays)
#### roll
**Operator Type**: Stack Manipulation
**Signature**: `(Size Size --)`
**Trait**: Stackable
**Description**: Rotate n items, times times.
**Example**: `1 2 3 4 3 1 roll // => 1 3 4 2`
**See Also**: rot, pick
**Section**: 4.1 (Stack Operations)
#### rot
**Operator Type**: Stack Manipulation
**Signature**: `(Self Self Self -- Self Self Self)`
**Trait**: Stackable
**Description**: Rotate the top three items.
**Example**: `1 2 3 rot // => 2 3 1`
**See Also**: swap, roll
**Section**: 4.1 (Stack Operations)
#### set
**Operator Type**: Struct Access
**Signature**: `(Value Identifier -- Struct)`
**Description**: Set field value in struct (consumes value and field identifier).
**Example**: `15.0 ::x set`
**See Also**: get
**Section**: 7.1 (Structs)
#### shl
**Operator Type**: Bitwise
**Signature**: `(Bitwise Size -- Bitwise)`
**Trait**: Bitwise
**Description**: Shift bits left by n positions.
**Example**: `4 2 shl // => 16`
**See Also**: shr, bitand, bitor
**Section**: 4.5 (Bitwise Operators)
#### shr
**Operator Type**: Bitwise
**Signature**: `(Bitwise Size -- Bitwise)`
**Trait**: Bitwise
**Description**: Shift bits right by n positions.
**Example**: `16 2 shr // => 4`
**See Also**: shl, bitand, bitor
**Section**: 4.5 (Bitwise Operators)
#### slice
**Operator Type**: Container
**Signature**: `(Sliceable Size Size -- Sliceable)`
**Trait**: Sliceable
**Description**: Extract elements from start to end index.
**Example**: `[10 20 30 40] 1 3 slice // => [20 30]`
**See Also**: at, length
**Section**: 7.4 (Arrays)
#### struct
**Operator Type**: Definition
**Signature**: `(TypeTuple TokenString Identifier --)`
**Trait**: Implementable
**Description**: Define a struct type.
**Example**: `(T T --) { x: y: } ::Point<T> struct`
**See Also**: union, enum, get, set
**Section**: 7.1 (Structs)
#### substr
**Operator Type**: String
**Signature**: `(String Size Size -- String)`
**Trait**: String
**Description**: Extract substring from start to end index.
**Example**: `"hello" 1 3 substr // => "el"`
**See Also**: slice, split
**Section**: 11.3 (String Operations)
#### swap
**Operator Type**: Stack Manipulation
**Signature**: `(Self Self -- Self Self)`
**Trait**: Stackable
**Description**: Swap the top two items on the stack.
**Example**: `5 10 swap // => 10 5`
**See Also**: dup, rot
**Section**: 4.1 (Stack Operations)
#### trait
**Operator Type**: Definition
**Signature**: `(TokenString Identifier --)`
**Trait**: Implementable
**Description**: Define a new trait.
**Example**: `{ (Self -- ) draw: } ::Drawable trait`
**See Also**: impl, inher
**Section**: 9.2 (Defining Traits)
#### transpose
**Operator Type**: Array
**Signature**: `(ArrayOf<ArrayOf<T>> -- ArrayOf<ArrayOf<T>>)`
**Description**: Transpose a 2D array (swap rows and columns).
**Example**: `[[1 2] [3 4]] transpose // => [[1 3] [2 4]]`
**See Also**: reverse
**Section**: 7.4 (Arrays)
#### union
**Operator Type**: Definition
**Signature**: `(TypeTuple TokenString Identifier --)`
**Trait**: Implementable
**Description**: Define a union type with tagged variants.
**Example**: `(T --) { Some(T) None } ::Option<T> union`
**See Also**: struct, enum, match
**Section**: 7.2 (Unions)
#### while
**Operator Type**: Control Flow
**Signature**: `(TokenString TokenString --)`
**Description**: Loop while condition is truthy, executing body each iteration.
**Example**: `{ dup 10 < } { dup print 1 + } while`
**See Also**: for, break, continue, if
**Section**: 6.2 (While Loops)
#### window
**Operator Type**: Array
**Signature**: `(ArrayOf<T> Size -- ArrayOf<ArrayOf<T>>)`
**Description**: Create sliding windows of given size.
**Example**: `[1 2 3 4] 2 window // => [[1 2] [2 3] [3 4]]`
**See Also**: slice
**Section**: 7.4 (Arrays)
---