Getting started
This page walks through KScript in ten minutes. Examples are shown in the .kscript format, and each one can be switched to XML: the two are the same language. Save one to a file and run it, change it, and see what happens. In kscript docs on your computer, examples have a Run button that does that for you.
If you don't have kscript yet, see Install.
Your first script
A script is a file that ends in .kscript. Save this as hello.kscript:
<echo>Hello, World!</echo>echo 'Hello, World!'Then run it:
kscript hello.kscriptkscript run hello.kscript does the same. Anything after the script's name goes to the script as $args.
The main script
A folder's main script runs without naming it. Call it main.kscript, and kscript run (or just kscript) runs it:
| Command | What it does |
|---|---|
kscript run | Runs main.kscript in this folder. |
kscript run tools | Runs tools/main.kscript. |
kscript . Joan | Runs main.kscript with Joan as $args[0]. |
When there's no main.kscript, index.kscript is the main script. In a project, app.main in kscript.json names it instead, and the files are looked for beside kscript.json, so kscript run works from any folder in the project:
{
"app": { "main": "tools/report.kscript" } // kscript run now runs tools/report.kscript
}The order is always the same: the script you name, then app.main, then main.kscript, main.ks.xml, index.kscript and index.ks.xml. kscript compile, kscript lib add and the editor's Run Project command find it the same way. With none of them, kscript run says what it looked for, and plain kscript shows the help.
A script runs from top to bottom, one statement per line. // starts a comment.
Variables
$name = value declares a variable, and assigning to it again changes it. Values are numbers, text, true/false, lists, maps and functions. Read a variable with $name. In text, $name and { } put a value in.
<let name="name" value="'Joan'" />
<let name="age" value="82" />
<echo>{ $name } is { $age }.</echo>
<set name="age" value="$age + 1" />
<echo>Next year { $name } will be { $age }.</echo>$name = 'Joan'
$age = 82
echo "{ $name } is { $age }."
$age = $age + 1
echo "Next year { $name } will be { $age }."After =, a value is an expression, so text needs quotes. "Double quotes" evaluate expressions within { }, and 'single quotes' keep the text exactly as written:
<let name="name" value="'Joan'" />
<let name="greeting" value=""Hello { $name }, you are { 80 + 2 }."" />
<let name="label" value="'Curly { braces } stay as written, and so does C:\temp.'" />
<echo value="$greeting" />
<echo value="$label" />$name = 'Joan'
$greeting = "Hello { $name }, you are { 80 + 2 }."
$label = 'Curly { braces } stay as written, and so does C:\temp.'
echo $greeting
echo $labelLists and maps
Lists are written [1, 2, 3] and maps { name: 'Joan', age: 82 }. Every value has methods, called with ->.
<let name="people">[
{ name: 'Joan', age: 82 },
{ name: 'Sam', age: 35 },
{ name: 'Ada', age: 36 }
]</let>
<echo>{ $people->count() } people</echo>
<echo>Oldest: { $people->max(by: .age).name }</echo>
<echo>Names: { $people->map(.name)->join(', ') }</echo>
<echo>Over 35: { $people->filter(fn(p) => $p.age > 35)->map(.name) }</echo>$people = [
{ name: 'Joan', age: 82 },
{ name: 'Sam', age: 35 },
{ name: 'Ada', age: 36 }
]
echo "{ $people->count() } people"
echo "Oldest: { $people->max(by: .age).name }"
echo "Names: { $people->map(.name)->join(', ') }"
echo "Over 35: { $people->filter(fn(p) => $p.age > 35)->map(.name) }"fn(p) => $p.age is a lambda: a small function you pass to a method. |> pipes a value into the next call, which reads well for longer chains:
<let name="words" value="'the quick brown fox jumps over the lazy dog'" />
<echo>{ $words->split(' ') |> unique() |> sort() |> join(', ') }</echo>$words = 'the quick brown fox jumps over the lazy dog'
echo "{ $words->split(' ') |> unique() |> sort() |> join(', ') }"The reference lists every method of strings, lists, maps, numbers and dates.
Decisions and loops
<for each="n" in="1..16">
<if $n % 15 == 0>
<echo>FizzBuzz</echo>
<continue />
</if>
<if $n % 3 == 0>
<echo>Fizz</echo>
<continue />
</if>
<if $n % 5 == 0>
<echo>Buzz</echo>
<continue />
</if>
<echo>{ $n }</echo>
</for>for n in 1..16 {
if $n % 15 == 0 {
echo 'FizzBuzz'
continue
}
if $n % 3 == 0 {
echo 'Fizz'
continue
}
if $n % 5 == 0 {
echo 'Buzz'
continue
}
echo "{ $n }"
}1..16 counts from 1 up to 15 (the end is left out). Comparisons are written <, <=, >, >=, == and !=, and conditions are joined with the words and, or and not.
match picks one arm. Each arm is pattern => result, with _ for anything else:
<let name="day" value="'Sat'" />
<match $day>
<case is="'Sat', 'Sun'">
<echo>Weekend</echo>
</case>
<default>
<echo>Weekday</echo>
</default>
</match>$day = 'Sat'
match $day {
'Sat', 'Sun' => echo 'Weekend'
_ => echo 'Weekday'
}Functions
<fn name="greet" params="who: string, greeting = 'Hello'">
<return value="$greeting + ', ' + $who + '!'" />
</fn>
<fn name="square" params="n: number" value="$n * $n" />
<echo>{ @greet('World') }</echo>
<echo>{ @greet('Kyle', greeting: 'Hi') }</echo>
<echo>{ 7 |> @square() }</echo>fn greet(who: string, greeting = 'Hello') {
return $greeting + ', ' + $who + '!'
}
fn square(n: number) => $n * $n
echo "{ @greet('World') }"
echo "{ @greet('Kyle', greeting: 'Hi') }"
echo "{ 7 |> @square() }"Your own functions and the built-in commands are both called with @. Arguments can be named, like greeting: 'Hi'.
Errors
<fn name="check_age" params="age: int">
<if $age < 0>
<throw type="Validation">Age must be positive, got {$age}</throw>
</if>
<return value="$age" />
</fn>
<try>
<do>@check_age(-1)</do>
<catch as="err">
<echo>{ $err.type }: { $err.message }</echo>
</catch>
</try>fn check_age(age: int) {
if $age < 0 => throw Validation "Age must be positive, got {$age}"
return $age
}
try {
@check_age(-1)
} catch err {
echo "{ $err.type }: { $err.message }"
}Packages
Everything above is KScript.Core, which every script has. The rest comes in packages you import: files and folders, the terminal, maths, HTTP, web pages, desktop windows and more. See the package list.
<import package="KScript.Math" as="math" />
<import package="KScript.IO" />
<echo>√2 is { @math.sqrt(2)->round(digits: 4) }</echo>
<do>@write_text(./notes.txt, 'Buy milk')</do>
<echo>notes.txt says: { @read_text(./notes.txt) }</echo>import KScript.Math as math
import KScript.IO
echo "√2 is { @math.sqrt(2)->round(digits: 4) }"
@write_text(./notes.txt, 'Buy milk')
echo "notes.txt says: { @read_text(./notes.txt) }"Examples in the docs run in their own empty folder, so writing files here is safe.
Asking for input
KScript.Terminal reads from the keyboard. In the docs, click Input and type the answers, one per line, before you run.
<import package="KScript.Terminal" />
<input into="name" default="'stranger'">What's your name? </input>
<echo>Hello, { $name }!</echo>import KScript.Terminal
input(into: name, default: 'stranger') "What's your name? "
echo "Hello, { $name }!"The .kscript format and XML
Everything above can also be written as XML, which looks like HTML: $name = 'Joan' is <let name="name" value="'Joan'" />, and echo 'Hi' is <echo>Hi</echo>. Switch any example to XML to see it, or use the switch at the top of the page. XML scripts end in .ks.xml, and the two kinds can import each other.
<let name="people" value="[{ name: 'Joan', age: 82 }, { name: 'Sam', age: 35 }]" />
<for each="p" in="$people">
<if $p.age > 40>
<echo>{ $p.name } is over 40</echo>
<continue />
</if>
<echo>{ $p.name } is { $p.age }</echo>
</for>$people = [{ name: 'Joan', age: 82 }, { name: 'Sam', age: 35 }]
for p in $people {
if $p.age > 40 {
echo "{ $p.name } is over 40"
continue
}
echo "{ $p.name } is { $p.age }"
}kscript convert turns a file into the other format. More in the .kscript format.
Where next
- The language: the whole language, in detail.
- Web pages and routes and APIs: websites and JSON APIs with
kscript serve. - Components covers reusable templates and interactive server-backed interfaces.
- Desktop apps: windows with buttons and text boxes.
- Testing: tests for your scripts with
kscript test. - The command line: every
kscriptcommand. - The playground: a blank page to try things in.