식
+ - * / % **, == != (loose: '3' == 3), === !== (strict), < <= > >=, and or not, ??, ?., a ? b : c, in, list [1, 2] and map { name: 'Joan' } literals, $p.name, $list[0] (negative indexes count from the end), ranges 0..10 and 0..100 step 5 (end excluded, also with a step such as 0.1) and 1..=10 (end included), match $x { 1 => 'one', _ => 'other' }, ->method(), |> and fn(x) => …. Write and and or to combine conditions. Prefix ! has the same precedence and truthiness as not, so !$ready, ! $ready and not $ready are equivalent. Use it on a field (!$user.active), a whole condition (if !($ready and $open) { … }), or an interpolation ("{ ! null->is_null() }"). The spelling null->!is_null() negates the method result before the next link in the chain. XML has a word for each comparison as well (lt le gt ge eq ne), for use inside quoted attributes, since XML can't hold a raw < there; the shorthand start tags accept < and > as written. In a .kscript file the words are reported (KS6012) and kscript check --fix writes the symbols.
<, <=, >, >=로 숫자와 문자열을 비교할 때, 문자열을 숫자로 해석할 수 있으면 KScript는 수치로 비교합니다. 해석에 실패하면 비교는 false를 반환합니다. 비교는 수학에서처럼 연결됩니다. 수학 절에서 보여 줍니다. 두 날짜는 ==로 비교할 때 초까지 포함해 같은 순간일 때만 같습니다.
수학
일상적인 수학에는 고유한 기호가 있으며, 각각 스크립트, 템플릿, 데이터베이스 쿼리에서 같은 방식으로 작동합니다. +은 더하고, -는 빼고, *는 곱하고, /는 나누며 소수를 유지합니다. ~/는 정수로 나누고 내림합니다. %는 나머지를 주는데, 왼쪽 수의 부호를 따릅니다. %%은 시계처럼 한 바퀴 돌아 감쌉니다. **는 거듭제곱입니다:
<let name="minutes" value="135" />
<let name="hours" value="$minutes / 60" /> <!-- 2.25 because / keeps the fraction -->
<let name="whole" value="$minutes ~/ 60" /> <!-- 2 because 60 fits into 135 twice -->
<let name="left" value="$minutes % 60" /> <!-- 15 because 135 - 2 * 60 leaves 15 -->
<let name="behind" value="-7 % 3" /> <!-- -1 because % keeps the sign of -7 -->
<let name="area" value="4 ** 2" /> <!-- 16 because 4 * 4 is 16 -->
<let name="kilobyte" value="2 ** 10" /> <!-- 1024 because ten 2s are multiplied -->$minutes = 135
$hours = $minutes / 60 // 2.25 because / keeps the fraction
$whole = $minutes ~/ 60 // 2 because 60 fits into 135 twice
$left = $minutes % 60 // 15 because 135 - 2 * 60 leaves 15
$behind = -7 % 3 // -1 because % keeps the sign of -7
$area = 4 ** 2 // 16 because 4 * 4 is 16
$kilobyte = 2 ** 10 // 1024 because ten 2s are multiplied연산자는 다음 순서로 묶이므로, 가장 강하게 묶이는 것부터 적용됩니다. 따라서 2 + 3 * 4은 *가 +보다 먼저 묶이기 때문에 14입니다:
| 우선순위 | 연산자 | 묶음 방향 |
|---|---|---|
| 1 | . -> ?. ( ) [ ] |
왼쪽에서 오른쪽으로 |
| 2 | ** |
오른쪽에서 왼쪽으로: 2 ** 3 ** 2은 2 ** 9, 512 |
| 3 | -x +x |
-2 ** 2은 **가 먼저 묶이므로 -4입니다. 그래서 -(2 ** 2) 또는 (-2) ** 2로 써야 합니다(KS6023). |
| 4 | * / ~/ % %% |
왼쪽에서 오른쪽으로 |
| 5 | + - |
왼쪽에서 오른쪽으로 |
| 6 | .. ..= step |
식마다 범위는 하나 |
| 7 | |> |
왼쪽에서 오른쪽으로 |
| 8 | < <= > >= in |
한 방향의 체인: 0 <= $n < 10 |
| 9 | == != === !== |
비교마다 하나. 더 연결하려면 and을 사용하세요. |
| 10 | not, and, or, ??, a ? b : c |
%과 %%는 음수 부호가 있을 때만 다릅니다. 나머지는 왼쪽 피연산자의 부호를 따르며, C#과 JavaScript와 같습니다. 모듈로는 오른쪽 피연산자의 부호를 따르므로 한 바퀴 돌아 감쌉니다. 자정 한 시간 전은 -1이 아니라 23입니다. 끝에서 감싸는 시계, 요일, 목록의 위치에는 %%을 쓰세요:
<let name="hour" value="9 - 12" /> <!-- -3 because 3 hours before 9 -->
<let name="late" value="$hour % 24" /> <!-- -3 because % keeps the sign of -3 -->
<let name="clock" value="$hour %% 24" /> <!-- 21 because %% wraps back round the clock -->
<let name="days" value="['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']" />
<let name="today" value="0" /> <!-- 'mon' is at 0 -->
<let name="yesterday" value="$days[($today - 1) %% 7]" /> <!-- 'sun' because -1 wraps round to the last day -->
<let name="spare" value="-15m %% 1h" /> <!-- 45m because durations wrap the same way -->$hour = 9 - 12 // -3 because 3 hours before 9
$late = $hour % 24 // -3 because % keeps the sign of -3
$clock = $hour %% 24 // 21 because %% wraps back round the clock
$days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
$today = 0 // 'mon' is at 0
$yesterday = $days[($today - 1) %% 7] // 'sun' because -1 wraps round to the last day
$spare = -15m %% 1h // 45m because durations wrap the same way~/은 0 쪽이 아니라 아래로 내림하므로 %%와 짝을 이룹니다. ($a ~/ $b) * $b + $a %% $b은 항상 $a입니다. 기간과 금액에도 작동하며, 일반 숫자를 돌려줍니다:
<let name="balance" value="-7" />
<let name="weeks" value="$balance ~/ 2" /> <!-- -4 because it rounds down, not toward zero -->
<let name="rest" value="$balance %% 2" /> <!-- 1 because -4 * 2 + 1 is -7 -->
<let name="runtime" value="2h10m" />
<let name="episodes" value="$runtime ~/ 25m" /> <!-- 5 because five 25m episodes fit into 2h10m -->
<let name="spare" value="$runtime %% 25m" /> <!-- 5m because 2h10m - 5 * 25m leaves 5m -->$balance = -7
$weeks = $balance ~/ 2 // -4 because it rounds down, not toward zero
$rest = $balance %% 2 // 1 because -4 * 2 + 1 is -7
$runtime = 2h10m
$episodes = $runtime ~/ 25m // 5 because five 25m episodes fit into 2h10m
$spare = $runtime %% 25m // 5m because 2h10m - 5 * 25m leaves 5m//는 주석을 시작하므로 정수 나눗셈이 될 수 없습니다. $hours = $minutes // $per_hour는 $minutes을 그대로 두며, KS6022는 이를 지적하고 ~/로 고치는 방법을 제시합니다.
같은 방향을 가리키는 비교는 수학에서처럼 이어서 쓸 수 있으므로, 범위 검사를 말하는 그대로 읽을 수 있습니다. 가운데 값은 각각 한 번만 계산되며, 거짓이 되는 첫 비교에서 체인이 멈춥니다:
<let name="score" value="42" />
<let name="passed" value="0 <= $score < 100" /> <!-- true because 42 is from 0 up to (not including) 100 -->
<let name="top" value="90 <= $score <= 100" /> <!-- false because 42 is below 90 -->
<let name="falling" value="10 > 7 >= 7 > 1" /> <!-- true because each step goes down or stays the same -->$score = 42
$passed = 0 <= $score < 100 // true because 42 is from 0 up to (not including) 100
$top = 90 <= $score <= 100 // false because 42 is below 90
$falling = 10 > 7 >= 7 > 1 // true because each step goes down or stays the same체인은 한 방향으로만 이어집니다. 1 < $n > 0은 1과 0의 비교에 대해 아무 정보도 주지 않으므로 오류이며, 1 < $n and $n > 0로 작성해야 합니다. ==와 !=는 이어지지 않습니다. $a == $b == $c는 $c와 비교한 참/거짓 값을 내기 때문에 $a == $b and $b == $c로 작성해야 합니다. 괄호는 체인을 끊으므로 (1 < $n) == true은 그 결과를 비교합니다. 체인은 데이터베이스 쿼리에서도 동작합니다. Person->where(fn(person) => 18 <= $person.age < 65)는 age >= 18 AND age < 65을 요청합니다. ->between()은 더 이상 쓸 수 없으며, 오류 메시지가 대신 써야 할 체인을 알려줍니다.
..은 끝 값 앞에서 멈추는 범위를 만들며, 0부터 세는 경우와 목록의 위치에 어울립니다. ..=는 끝 값을 포함하며, 1부터 세는 경우에 어울립니다. 두 가지 중 어느 것에 마우스를 올려도 범위가 어디서 시작하고 끝나는지 보여줍니다:
<let name="positions" value="0..3" /> <!-- [0, 1, 2] because .. stops before 3 -->
<let name="places" value="1..=3" /> <!-- [1, 2, 3] because ..= takes in 3 -->
<let name="quarters" value="0..=1 step 0.25" /> <!-- [0, 0.25, 0.5, 0.75, 1] because a step lands on 1 -->
<let name="odd" value="1..=10 step 2" /> <!-- [1, 3, 5, 7, 9] because no step lands on 10 -->
<let name="monday" value="'2026-11-30'->to_date()" />
<let name="week" value="$monday..=$monday + 6d" /> <!-- seven dates because ..= takes in the Sunday -->$positions = 0..3 // [0, 1, 2] because .. stops before 3
$places = 1..=3 // [1, 2, 3] because ..= takes in 3
$quarters = 0..=1 step 0.25 // [0, 0.25, 0.5, 0.75, 1] because a step lands on 1
$odd = 1..=10 step 2 // [1, 3, 5, 7, 9] because no step lands on 10
$monday = '2026-11-30'->to_date()
$week = $monday..=$monday + 6d // seven dates because ..= takes in the Sunday..와 마찬가지로, 끝 값이 시작 값보다 작으면 범위는 거꾸로 셉니다. 따라서 3..=1는 [3, 2, 1]이고 1..=0는 [1, 0]입니다. 값이 0일 수도 있는 상한까지 세려면 1..($count + 1)처럼 ..을 사용하세요. $count이 0이면 값이 하나도 나오지 않습니다.
KScript에서 ^은 거듭제곱이 아닙니다(C#과 JavaScript에서는 비트 연산자입니다). 따라서 2 ^ 10는 오류이며, 고치기 기능은 2 ** 10로 작성합니다.
비트 연산은 기호 대신 메서드 ->bits()로 수행합니다. &와 |는 and와 or와 쉽게 혼동되기 때문입니다. 정수에 대해 한 번에 한 단계씩 적용합니다:
<let name="read" value="4" />
<let name="write" value="2" />
<let name="permissions" value="6" /> <!-- read and write, because 4 + 2 is 6 -->
<let name="can_read" value="$permissions->bits(and: $read) == $read" /> <!-- true because 6 has the 4 bit -->
<let name="with_run" value="$permissions->bits(or: 1)" /> <!-- 7 because or: adds the 1 bit -->
<let name="toggled" value="$permissions->bits(xor: $write)" /> <!-- 4 because xor: flips the 2 bit off -->
<let name="doubled" value="$permissions->bits(shift: 1)" /> <!-- 12 because each bit moves one place left -->
<let name="halved" value="$permissions->bits(shift: -1)" /> <!-- 3 because a negative shift moves right -->
<let name="flipped" value="$permissions->bits(invert)" /> <!-- -7 because every bit flips -->$read = 4
$write = 2
$permissions = 6 // read and write, because 4 + 2 is 6
$can_read = $permissions->bits(and: $read) == $read // true because 6 has the 4 bit
$with_run = $permissions->bits(or: 1) // 7 because or: adds the 1 bit
$toggled = $permissions->bits(xor: $write) // 4 because xor: flips the 2 bit off
$doubled = $permissions->bits(shift: 1) // 12 because each bit moves one place left
$halved = $permissions->bits(shift: -1) // 3 because a negative shift moves right
$flipped = $permissions->bits(invert) // -7 because every bit flips&, 단독 |, 두 값 사이의 <<와 >>는 작성해야 할 ->bits() 호출을 알려주는 오류입니다. &와 |은 and 또는 or로 고치는 방법을 제시하며, 보통 의도했던 코드가 그것입니다.
($hour + 24) % 24은 숫자를 감싸는 예전 방식입니다. KS6021은 -24보다 작은 수에도 동작하는 $hour %% 24를 권장합니다.
+=, -=, *=, /=, ~/=, %=, %%=, **=, ??=는 변수를 그 자리에서 바꿉니다. 예를 들어 $total += 5은 $total = $total + 5와 같습니다. 연산자 주변의 공백은 자유지만, 연산자 안의 공백은 연산자를 둘로 나눕니다. 2 * * 10는 **을 가리키는 오류이며, 빠른 수정 기능은 공백을 제거합니다.
편집기는 연산자와 공백 뒤에 변수 목록을 보여주며, 왼쪽 값의 타입과 일치하는 변수가 먼저 나옵니다. 연산자에 마우스를 올리면 그 동작을 보여줍니다.
검사된 변환에는 expression as Type을 사용하세요. 예를 들어 $text as int와 같습니다. 대상은 선언된 레코드, 열거형 또는 정제 타입일 수도 있습니다. 검사된 변환