⚡ Operatory

KitsuneScript oferuje bogaty zestaw operatorów, w tym wiele nowoczesnych konstrukcji znanych z innych języków.

Operatory arytmetyczne

Operator Opis Przykład Wynik
+Dodawanie10 + 515
-Odejmowanie10 - 55
*Mnożenie10 * 550
/Dzielenie10 / 52
%Modulo10 % 31
**Potęgowanie2 ** 38

Operatory porównania

Operator Opis Przykład
==Równość5 == 5 → true
!=Nierówność5 != 3 → true
<Mniejsze3 < 5 → true
>Większe5 > 3 → true
<=Mniejsze lub równe3 <= 3 → true
>=Większe lub równe5 >= 5 → true
<=>Spaceship (trójstronny)5 <=> 10 → -1

Łańcuchowe porównania

// Porównania można łączyć jak w matematyce! let x = 5; print(1 < x < 10); // true print(10 < x < 20); // false // Spaceship operator - zwraca -1, 0, lub 1 print(5 <=> 10); // -1 (mniejsze) print(10 <=> 5); // 1 (większe) print(5 <=> 5); // 0 (równe)

Operatory logiczne

Operator Opis Przykład
&&Logiczne ANDtrue && false → false
||Logiczne ORtrue || false → true
!Logiczne NOT!true → false
notSłowne NOTnot true → false

Operatory bitowe

let and = 12 & 10; // 8 (1100 & 1010 = 1000) let or = 12 | 10; // 14 (1100 | 1010 = 1110) let xor = 12 ^ 10; // 6 (1100 ^ 1010 = 0110) let not = ~5; // -6 (bitwise NOT) let shl = 1 << 4; // 16 (shift left) let shr = 16 >> 2; // 4 (shift right) // Praktyczne użycie - flagi let READ = 1; // 0001 let WRITE = 2; // 0010 let EXECUTE = 4; // 0100 let permissions = READ | WRITE; // 0011 = 3 let hasRead = (permissions & READ) != 0; // true

Operatory null

// Null coalescing (??) - dla null let value = null; let safe = value ?? "default"; // "default" // Elvis operator (?:) - dla wszystkich falsy let zero = 0; let result = zero ?: "default"; // "default" (bo 0 jest falsy) // Optional chaining (?.) - bezpieczny dostęp let obj = {name: "Kit"}; print(obj?.name); // "Kit" print(obj?.missing); // null (nie błąd) // Safe index access (?[]) let list = [1, 2, 3]; print(list?[0]); // 1 print(list?[100]); // null (nie błąd) let nullList = null; print(nullList?[0]); // null (nie błąd)

Operatory specjalne

Ternary operator (? :)

let result = (x > 0) ? "positive" : "non-positive";

Range operators (.., ..<)

// Inclusive range (..) let nums = 1..5; // [1, 2, 3, 4, 5] let reverse = 5..1; // [5, 4, 3, 2, 1] // Exclusive range (..<) - bez końca let indices = 0..<5; // [0, 1, 2, 3, 4] // Z krokiem (step) let odds = 1..20 step 2; // [1, 3, 5, 7, ...] let countdown = 10..0 step -2; // [10, 8, 6, 4, 2, 0]

In / not in operators

let list = [1, 2, 3]; print(2 in list); // true print(10 in list); // false print(5 not in list); // true // Dla stringów print("lo" in "hello"); // true // Dla map let obj = {name: "Kit"}; print("name" in obj); // true

Spread operator (...)

let a = [1, 2, 3]; let b = [...a, 4, 5]; // [1, 2, 3, 4, 5] let obj1 = {x: 1}; let obj2 = {...obj1, y: 2}; // {x: 1, y: 2}

Pipe operator (|>)

fn double(x) { return x * 2; } fn addTen(x) { return x + 10; } // Zamiast: addTen(double(5)) let result = 5 |> double |> addTen; // 20

String/List multiplication

// Powtarzanie stringów let stars = "*" * 5; // "*****" let border = "=" * 20; // "====================" let laugh = 3 * "ha"; // "hahaha" // Powtarzanie list let pattern = [1, 2] * 3; // [1, 2, 1, 2, 1, 2] let zeros = [0] * 5; // [0, 0, 0, 0, 0]

Composition operator (>>)

fn double(x) { return x * 2; } fn addOne(x) { return x + 1; } // Kompozycja funkcji let doubleThenAdd = double >> addOne; print(doubleThenAdd(5)); // 11 (5*2 + 1)

Operatory przypisania

Operator Równoważne Opis
+=x = x + yDodaj i przypisz
-=x = x - yOdejmij i przypisz
*=x = x * yPomnóż i przypisz
/=x = x / yPodziel i przypisz
%=x = x % yModulo i przypisz
??=if null: x = yPrzypisz gdy null
||=if falsy: x = yPrzypisz gdy falsy
&&=if truthy: x = yPrzypisz gdy truthy

Priorytet operatorów

Od najwyższego do najniższego:

Priorytet Operatory
1 (najwyższy)() [] . ?. ?[]
2! ~ ++ -- (unary)
3**
4* / %
5+ -
6<< >>
7.. ..<
8< <= > >= in
9== != is
10&
11^
12|
13&&
14||
15?? ?:
16? : (ternary)
17|> >>
18 (najniższy)= += -= itd.