KScript 문서
KO

언어

영어에서 자동 번역되었습니다. 원문은 영어입니다.

.kscript 형식

문장

각 명령문에는 XML 표기법이 있어요. kscript convert은 한 표기법을 다른 표기법으로 바꿔 줘요:

.kscript XML
$n = 3 (처음 실행 시) <let name="n" value="3" />
$n = 4, $total += 1 (후) <set name="n" value="4" />, <set name="total" op="+=" value="1" />
const $config = { retries: 3 } <const name="config" value="…" />
$count: int = $raw <let name="count" type="int" value="$raw" />
@greet('Hi') <do>@greet('Hi')</do>
echo "Hello, { $name }" <echo>Hello, { $name }</echo>
echo $list[1], echo(format: json) $data <echo value="$list[1]" />
if a { } elif b { } else { } <if test>, <elif test>, <else>
if a => return, elif b => @f(), else => echo "c" 같은 내용이지만 문장 하나가 들어 있는 형태
for item in $list { }, for key, value in $map { } <for each in>
for item at i in $list { } <for each="item" at="i" in="$list">
while test { }, break, continue <while test>, <break/>, <continue/>
match $x { 1, 2 => { }, _ when $x > 9 => { }, _ => { } } <match value>, <case is>, <case when>, <default>
fn greet(who: string, hi = 'Hello') { } <fn name="greet" params="who: string, hi = 'Hello'">
fn double(x: int) => $x * 2 <fn name="double" params="x: int" value="$x * 2" />
return $x, exit 1 <return value="$x" />, <exit code="1" />
@load() catch NotFound => 'Missing' XML 표현식 속성에서도 같은 표현식
try { } catch NotFound as err { } catch err { } finally { } <try>를 <catch type as> 및 <finally> 안에 넣어요 (when test는 catch에 테스트를 추가해요)
throw Validation "Bad {$x}" <throw type="Validation">Bad {$x}</throw>
import KScript.Math as math <import package="KScript.Math" as="math" />
import "lib/s.kscript" as s <import path="lib/s.kscript" as="s" />
import slugify from "lib/s.kscript" <import path="lib/s.kscript" names="slugify" />
enum Size { small, medium, large } <enum name="Size" values="small, medium, large" />
type Repo(extra: error) { … } <type name="Repo" extra="error">가 <field>를 포함하고 있어요
stars: int from stargazers_count (타입 내) <field name="stars" type="int" from="stargazers_count" />
topics?: string[] = [], label => $name + '!' <field name="topics?" type="string[]" default="[]" />, <field name="label" value="$name + '!'" />
type Admin with [User, Auditable] { … } <type name="Admin" with="User, Auditable">
type Age with [int] { min: 0; max: 150 } <type name="Age" with="int" min="0" max="150" />
validate: $value > 0, 'too small' (타입 내) <validate test="$value > 0" message="too small" />
normalize: $value->lower()(타입 안) <normalize value="$value->lower()" />
fn area() => $w * $h(타입 안) <fn name="area" value="$w * $h" />, <type> 안

A field line in a type block is name[?][: type] [from path] [= default | => value]; quote the path if it has spaces. See Field lines for what each part does. A type's block can also hold rule lines (min:, max:, pattern:, one_of:, not:, parse:, validate: and normalize:), fn methods and one construct({ fields }) { statements } block, whose fields are what a caller may pass. A component's render may name where it draws in place of parameters (fn render(window)), which is surface="window" on the <fn>, and a component is drawn by a statement named after it (NoteCard(title: "Milk"), <NoteCard title="Milk" />). A line such as max: int is still a field: a rule's value is a value, not a type. See Types and validation for the rules.

그 밖의 이름은 요소입니다: name(attribute: value, ...) "text" { children }, 각 부분은 선택 사항입니다.

<input into="old_dir">Move files from: </input>
<Row gap="8" wrap="yes">
  <Button variant="primary" on_click="$playing">{ $playing ? 'Pause' : 'Play' }</Button>
</Row>
input(into: old_dir) "Move files from: "
Row(gap: 8, wrap) {
  Button(variant: primary, on_click: $playing) "{ $playing ? 'Pause' : 'Play' }"
}