KScript docs
EN

The .kscript format

Rules

These rules decide how a .kscript file is read.

  • A statement ends at a new line, ; or the } that closes its block. It carries on when a bracket is open, the line ends with an operator (|>, ->, +, ,, and, ...), or the next line starts with |>, ->, ?, :, ., and or or.
  • In the head of if, elif, for, while and match, the first { outside brackets opens the block. Wrap a map literal in parentheses there.
  • An if, elif or else that does one statement can be written on one line with =>: if $done => return. The first => outside brackets ends the condition (one in fn(x) => ... or catch => belongs to the expression), and the statement runs to the end of the line.
  • A match holds arms, pattern => result: 1, 2 => echo 'low', _ when $x > 9 => { } for a test and _ => { } for anything else. The result is one statement on the arm's line or a { } block, and arms are separated by a new line or a comma. A match used as a value (label = match $x { 1 => 'one', _ => 'other' }) writes its arms the same way.
  • "double quotes" interpolate expressions enclosed in { }, as in greeting = "Hello { $name }". A { } always holds an expression, and {{ and }} are braces. \" and \\ are the only escapes (plus a \ at the end of a line, which joins it to the next: see Long strings and blocks of text). 'single quotes' are string literals inside expressions and are kept exactly as written.
  • In name(...) a value runs to the next , or ) outside brackets; quote it to include a comma. A bare name such as wrap means wrap="true".
  • A list of names is an array: Middleware(use: [secure_headers, json_only]), Resource(..., only: [index, show]). The XML form writes the same list as text, use="secure_headers, json_only", and kscript convert turns one into the other. The string form (use: "a, b") is an error in .kscript (KS6006) that kscript check --fix rewrites.
  • Comparisons are symbols (<, <=, >, >=, ==, !=) and logic is words (and, or, not). The comparison words lt, le, gt, ge, eq and ne are the XML spellings: kscript convert writes the symbols, and a word left in a .kscript file is reported (KS6012) with a fix.
  • A variable has its $ where it is set as well as where it is read: $total = 0, $total += 1, const $tax = 0.2. Names a statement introduces, parameters and loop items, are written bare: for item in $list, fn double(n), fn(n) => $n * 2. Writing total = 0, fn double($n) or fn($n) is reported (KS6010) with a fix.
  • The file is the body of an implied <kscript>, so kscript { } around it is optional. import goes at that top level, not inside another block.
  • A package is named only in import KScript.UI. A statement is never written with the KScript. prefix: write import KScript.UI and then Window(...) { }, not KScript.Window(...) { } (which is an error that says so). An XML file names the package in its root tag instead, <KScript.Window>, and kscript convert turns one into the other.
  • // and /* */ are comments. Either can go on its own lines, at the end of a line, or inside an expression that spans lines ([1, /* first */ 2]). kscript convert keeps them; XML has one kind of comment, so they come back from XML as // lines.