KScript documentation
FR

Le format .kscript

Instructions

Chaque instruction a une écriture XML. kscript convert convertit l'une en l'autre :

.kscript XML
$n = 3 (première fois) <let name="n" value="3" />
$n = 4, $total += 1 (ensuite) <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' identique, avec l'instruction unique à l'intérieur
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' Même expression dans les attributs d'expression XML
try { } catch NotFound as err { } catch err { } finally { } <try> avec <catch type as> et <finally> à l'intérieur (when test ajoute un test à un 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"> avec des <field> à l'intérieur
stars: int from stargazers_count (dans un type) <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' (dans un type) <validate test="$value > 0" message="too small" />
normalize: $value->lower() (dans un type) <normalize value="$value->lower()" />
value: .group(1)->to_int() (dans un type) <value value=".group(1)->to_int()" />
fn area() => $w * $h (dans un type) <fn name="area" value="$w * $h" /> à l'intérieur de <type>

Une ligne de champ dans un bloc type est name[?][: type] [from path] [= default | => value] ; mettez le chemin entre guillemets s'il contient des espaces. Voir Lignes de champ pour le rôle de chaque partie. Le bloc d'un type peut aussi contenir des lignes de règle (min:, max:, pattern:, one_of:, not:, parse:, validate:, normalize: et value:), des méthodes fn et un bloc construct({ fields }) { statements } unique, dont les champs sont ce qu'un appelant peut passer. Le render d'un composant peut indiquer où il dessine à la place des paramètres (fn render(window)), ce qui est surface="window" sur le <fn>, et un composant est dessiné par une instruction portant son nom (NoteCard(title: 'Milk'), <NoteCard title="Milk" />). Une ligne telle que max: int reste un champ : la valeur d'une règle est une valeur, pas un type. Voir Types et validation pour les règles.

Tout autre nom est un élément : name(attribute: value, ...) "text" { children }, chaque partie étant facultative.

<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' }"
}