Other Keywords

Miscellaneous keywords with other uses.

There are 5 miscellaneous keywords.

Struct

The struct keyword is used to create a fixed key <-> value data structure.

struct {
    %key% = %value%
    %key% = %value%
    ...
}

This keyword has very strict requirements. It must be immediately followed by a block ({ ... }).

The block may contain only variable assignment statements (in key = value form).

The result is a property-mapped structure.

person = struct {
    name = "Jeremy"
    surname = "Bearimy"
    age = 109
}

assert person[name] == "Jeremy"

person[age] = 42

A structure has mutable values but an immutable set of properties. Unlike a map it does not allow new properties to be added or removed.

Run

The run keyword is used to run an executable thing, such as a script, a function or an external resource.

run %executable%
run %executable% %environment%
run other.script
run other.script [name="Jeremy"]

result = run other.script

run result[function]

An environment can be provided, such as a map, structure, list or object.

The environment forms the initial variables available in the executable being run.

This allows you to pass data arguments to another script or function.

other.script
require [name]
/print hello, {name}!
run other.script [name="Jeremy"]

run other.script ["Jeremy"]

run other.script "Jeremy"

person = struct {
    name = "Jeremy"
    surname = "Bearimy"
}
run other.script person

Function

The function keyword is used to create a runnable, re-usable piece of code.

function %statement%
greet = function {
    /say hello everybody
}

run greet

Context variables outside the function are not available inside the function.

This is to prevent accidental pollution (e.g. a function from another script overwriting your variable).

Variables can be explicitly passed using the environment input in the run statement.

greet = function {
    require [name]
    /say hello {name}
}

run greet [name="Jeremy"]

run greet "Jeremy"

The result of a function is the result value of its final statement.

func = function {
    5
}

result = run func
assert result == 5

A function does not need to be a block section.

func = function /say hello
run func

The function keyword returns the following statement as a literal code statement instead of executing it.

Syntax

The syntax keyword defines new syntax that is available in the script following this statement.

syntax %string% = %statement%
syntax "greet %player%" = /print hello, {player}
greet "BaeFell"

syntax "$%number%" = "${number}"
price = $5

The pattern inputs (in % percents) will accept any statements. The input names will become variables available in the syntax statement.

When the syntax is used, the statement will be invoked with the inputs as variables.

The syntax is available after this line, so it is not available inside itself.

Do

The do keyword is used to create a local context where an objects properties are available as keywords and variables in the space.

do %statement% %statement%
do player { // this block is run in the context of `player`
    thing = name // thing = player[name]
    send_message "hello, {name}" // run send_message "hello, {player[name]}"
}

Unlike running something in an object environment, this allows the object's functional properties to be used in situ, without needing to explicitly run them.

In this way, a do section can be used to create a kind of local syntax, where an object's functional properties behave as keywords would.

Some kinds of object may also create new constants inside a do block.

This may be useful for some library objects.

import [math]
result = do math {
    number = 25
    number = sqrt number
    number = abs number
    number = max [1, number]
    round number
}

Last updated