The Do Block

Transform the language with features from the context of an object.

Introduction

The do statement is one of the most versatile and powerful syntax elements in the language. It is also one of the most complex.

In essence, it transforms the language by giving the user access to the properties of an object as new syntax keywords.

However, since the object is defined and exists only during runtime, the parse-time information and errors available are very limited, making it a trickier construct to use.

do %statement% {
    
}

The computed object result of the statement is the target of the do block. All of that object's properties are unwrapped into syntax keywords.

These keywords either reference the value of their linked property or, if the property is a function, they directly invoke that function, removing the need for the run statement in some contexts.

do "hello" {
    assert length == 5 // "hello"[length]
    assert char_at 1 == "e" // run "hello"[char_at] 1
    assert substring 3 == "lo" // run "hello"[substring] 3
}

At face value this can be used as a syntax-simplifier, removing the need to write out x[y] property references.

However, it also allows an advanced user to alter the language with the functional properties that are available.

Example Turtle
turtle = struct {
    move = fn(int) // moves int spaces
    look = fn(direction) // turns to face a direction
    north = // direction north
    east = // ...
    south = // ...
    west = // ...
    up = // ...
    down = // ...
}

do turtle {
    look north
    move 1
    look up
    move 2
    look north
    move 1
}

The statements in the turtle's do block would be completely unintelligible in regular context, but are valid syntax because they are local keywords made from the properties of the turtle object.

Last updated