KScript documentazione
IT

Lingua

Tradotto automaticamente dall’inglese, che è la versione originale.

Il formato .kscript

Introduzione

KScript ha due modi per scrivere lo stesso linguaggio. I file .kscript usano parentesi graffe e a capo, come la maggior parte dei linguaggi di programmazione. I file .ks.xml usano tag XML. Il linguaggio, il runtime e ogni pacchetto sono gli stessi: il parser (KScript.Syntax/Markup/CodeParser.cs) costruisce esattamente lo stesso albero di elementi che costruisce il parser XML, quindi kscript, kscript check e il language server trattano entrambi allo stesso modo. L'estensione del file determina il parser, e i due tipi di file possono importarsi a vicenda.

Ecco uno script .kscript:

<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 e kscript make test scrivono file .kscript. Passa --syntax xml, oppure imposta "make": { "syntax": "xml" } in kscript.json, per ottenere invece .ks.xml.

Ogni cartella sotto samples/ è scritta in questo modo. samples/xml/ contiene gli script di samples/scripts/ nella forma XML, per confrontare le due versioni affiancate.