Block Expressions

blockExpr

FunctionExpr ObjectExpr bind def InterfaceExpr IfExpr ForExpr WhileExpr SwitchExpr EscapeExpr TryExpr WhenExpr LambdaExpr MetaExpr

block

{ sequence pass }

Nested Block

HideExpr

{ expr ; }

The if Expression

IfExpr

if ( expr ) block else IfExpr block
>>> if (2 < 3) { "expected" } else { "unexpected" }
"expected"

>>> def x := 5
... def y := 10
... if (x < y) { "less" } else if (x > y) { "greater" } else { "neither" }
"less"

The switch Expression

SwitchExpr

switch ( expr ) { matchers }

matchers

match pattern block
>>> def state := "day"
...
... switch (state) {
...     match =="day" {"night"}
...     match =="night" {"day"}
... }
"night"

Default matcher

Switch expressions expand to a tree of possibilities, with each matcher being tried in turn until one matches. If none of them match, then an exception is thrown with a short description of the failing specimen.

To override this behavior, specify a matcher that cannot fail. Examples of patterns that cannot fail include final and var patterns without guards, and ignore patterns:

switch (specimen):
    match ==x:
        traceln(`$specimen was just like $x`)
    match i :Int:
        traceln(`$i is an Int`)
    match _:
        traceln(`Default matcher!`)

In this example, since the final matcher always succeeds, the default behavior of throwing an exception is effectively overridden.

When Monte expands switch expressions into Kernel-Monte, the entire expression becomes a long series of if expressions. The final else throws an exception using the _switchFailed helper object. If the penultimate if test cannot fail, then the final else is unreachable, and it will be pruned by the optimizer during compilation.

Switch Expansion

>>> m`switch (specimen) { match pat1 { expr1 } }`.expand()
m`{ def specimen_1 := specimen; escape ej_2 { def pat1 exit ej_2 := specimen_1; expr1 } catch failure_3 { _switchFailed.run(specimen_1, failure_3) } }`

The try Expression

TryExpr

try block catchers

catchers

catch pattern block finally block
>>> try { 3 < "3" } catch _ { "ouch! no order defined" }
"ouch! no order defined"

Todo

expansion of various forms of try

The escape Expression

EscapeExpr

escape pattern blockCatch

If hatch is called during expr, complete with hatch‘s argument:

>>> escape hatch { def x :Int exit hatch := 1.0 }
"1.000000 does not conform to Int"

The while Loop

WhileExpr

while ( expr ) blockCatch
while (test) { body }
while (test) { body } catch p { catchblock }

Todo

while doctests, expansion

The for Loops

ForExpr

for pattern => pattern in comp blockCatch

blockCatch

block catch pattern block
for valuePatt in iterableExpression { body }
for keyPatt => valuePatt in iterableExpression { body }
for valuePatt in iterableExpression { body } catch p { catchblock }

Todo

for doctests, expansion

The when Expression

WhenExpr

when ( expr , ) -> block catchers
when (x, y) -> { whenblock } catch p { catchblock }

The fn Expression

LambdaExpr

fn pattern , block
/** docstring */ fn p, q { body }

Todo

doctest /** docstring */

Defining Objects

def

def bind name guard name objectFunction@@ assign assign

bind

bind name guard objectExpr

ObjectExpr

object bind name _ name objectExpr

objectExpr

extends order auditors { objectScript ; }

objectScript

doco pass @@meth pass matchers

matchers

match pattern block

doco

.String.

FunctionExpr

def ( pattern , ) block
object foo {
    to someMethod(p, q) {
        methBody
    }

    method rawMethod(p, q) {
        methBody
    }
     match [verb, arglist] {
         matcherBody
     }
}
object foo as someAuditor { ... }
object foo implements firstAuditor, secondAuditor { ... }
object foo extends baz { ... }

/** doc string */
object foo as someAuditor implements firstAuditor, secondAuditor extends baz { ... }
def fun(p, q) :optionalGuard { body }

Defining Interfaces

InterfaceExpr

interface namePatt guards pattern extends order , implements_@@ msgs@@

Todo

interface syntax diagram @@s

interface Foo { to interfaceMethod(p, q) { ... } }
interface Foo guards FooStamp { ... }

Todo

various items marked “@@” in railroad diagrams. Also, finish re-organizing them around precedence (use haskell codegen to test).