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|>,->,?,:,.,andoror. - In the head of
if,elif,for,whileandmatch, the first{outside brackets opens the block. Wrap a map literal in parentheses there. - An
if,eliforelsethat does one statement can be written on one line with=>:if $done => return. The first=>outside brackets ends the condition (one infn(x) => ...orcatch =>belongs to the expression), and the statement runs to the end of the line. - A
matchholds 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 ingreeting = "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 aswrapmeanswrap="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", andkscript convertturns one into the other. The string form (use: "a, b") is an error in.kscript(KS6006) thatkscript check --fixrewrites. - Comparisons are symbols (
<,<=,>,>=,==,!=) and logic is words (and,or,not). The comparison wordslt,le,gt,ge,eqandneare the XML spellings:kscript convertwrites the symbols, and a word left in a.kscriptfile 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. Writingtotal = 0,fn double($n)orfn($n)is reported (KS6010) with a fix. - The file is the body of an implied
<kscript>, sokscript { }around it is optional.importgoes at that top level, not inside another block. - A package is named only in
import KScript.UI. A statement is never written with theKScript.prefix: writeimport KScript.UIand thenWindow(...) { }, notKScript.Window(...) { }(which is an error that says so). An XML file names the package in its root tag instead,<KScript.Window>, andkscript convertturns 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 convertkeeps them; XML has one kind of comment, so they come back from XML as//lines.