Introduction
KScript has two ways to write the same language. .kscript files use braces and new lines, like most programming languages. .ks.xml files use XML tags. The language, the runtime and every package are the same: the parser (KScript.Syntax/Markup/CodeParser.cs) builds the exact element tree the XML parser builds, so kscript, kscript check and the language server treat both alike. The file extension picks the parser, and the two kinds of file may import each other.
Here is a .kscript script:
<import package="KScript.IO" />
<!---
Lower-cases text and joins words with dashes.
@param text The text to change.
-->
<fn name="slugify" params="text: string" value="$text->lower()->replace(' ', '-')" />
<let name="people" value="[{ name: 'Joseph', age: 28 }, { name: 'Joan', age: 82 }]" />
<for each="p" in="$people">
<if $p.age < 18>
<echo>{ $p.name } is not an adult</echo>
<continue />
</if>
<echo>{ @slugify($p.name) } is an adult</echo>
</for>import KScript.IO
/// Lower-cases text and joins words with dashes.
/// @param text The text to change.
fn slugify(text: string) => $text->lower()->replace(' ', '-')
$people = [{ name: 'Joseph', age: 28 }, { name: 'Joan', age: 82 }]
for p in $people {
if $p.age < 18 {
echo "{ $p.name } is not an adult"
continue
}
echo "{ @slugify($p.name) } is an adult"
}kscript make project, kscript make script and kscript make test write .kscript files. Pass --syntax xml, or set "make": { "syntax": "xml" } in kscript.json, to get .ks.xml instead.
Every folder under samples/ is written this way. samples/xml/ has the scripts of samples/scripts/ in the XML form, to compare the two side by side.