Block Expressions¶
blockExpr
block
Nested Block¶
HideExpr
The if Expression¶
IfExpr
>>> 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
matchers
>>> 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
catchers
>>> try { 3 < "3" } catch _ { "ouch! no order defined" }
"ouch! no order defined"
Todo
expansion of various forms of try
The escape Expression¶
EscapeExpr
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 (test) { body }
while (test) { body } catch p { catchblock }
Todo
while doctests, expansion
The for Loops¶
ForExpr
blockCatch
for valuePatt in iterableExpression { body }
for keyPatt => valuePatt in iterableExpression { body }
for valuePatt in iterableExpression { body } catch p { catchblock }
Todo
for doctests, expansion
Defining Objects¶
def
bind
ObjectExpr
objectExpr
objectScript
matchers
doco
FunctionExpr
object foo {
"A docstring for this object."
to someMethod(p, q) {
return methBody
}
method rawMethod(p, q) {
methBody
}
match [verb, args, namedArgs] {
matcherBody
}
}
object foo as someAuditor { ... }
object foo implements firstAuditor, secondAuditor { ... }
object foo extends baz { ... }
object foo as someAuditor implements firstAuditor, secondAuditor extends baz:
"A docstring."
def fun(p, q) :optionalGuard { body }
Defining Interfaces¶
InterfaceExpr
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).