Skip to content

Control Flow

Socratic_Phoenix edited this page Sep 26, 2019 · 6 revisions

<-- Back | Next -->


Note: All blocks evaluate to the last statement they ran

Note: All blocks open a new scope, even if they don't have brackets

Scope Block

At any point, you can open a new scope by creating a block with { and }.

Examples:

stuff = 5
{
    println(stuff)
    things = 5
    println(things)
    ^more = "more"
}
println(things) //prints void (variable is absent from this scope)
println(more) //prints "more" (variable was pushed to this scope)

Try-Catch Block

The try-catch block runs the try block, and, if something is thrown, passes the error to the catch block. It's syntax looks like this:

try name:{ 

} catch ex name2:{

}

The name of the blocks can be omitted. You can break or continue out of a try-catch block by naming the block and using that name in the break/continue statement. However, unnamed break and continue statements will not break/continue and if block, but will instead break/continue the nearest loop

If Block

The if block is a simple branching block, it's syntax looks like this:

if(condition) name:{

} else if (otherCondition) name2:{

} else name3:{

}

The value in parenthesis is evaluated for truthiness. If it is truthy, the block is run, otherwise execution moves to the next else block, until a block is run. The else block behaves like an else if (true) block. An if block may exist without else/else-if blocks, an if block does not require an else block, and the else block, if present, is the last block in an if block. Furthermore, the name of the blocks can be omitted. You can break or continue out of an if block by naming the block and using that name in the break/continue statement. However, unnamed break and continue statements will not break/continue and if block, but will instead break/continue the nearest loop.

Examples:

if (true) {
    println("true")
} else {
    println("false")
}

condition = 5
if (condition) {
    println("truthy") 
}

While and Do-While Blocks

The while and do-while blocks behave the same, except for one thing: the do-while block is always run at least once. The syntax for while and do-while looks like this:

while (condition) name:{

}
dowhile (condition) name2:{

}

Note that the name may be omitted.

For Blocks

The for block takes an iterator and runs the block on each value of the iterator. The syntax for a for block looks like this:

for (var : iterator) name:{

}

Note that name may be omitted.

Omissions

One unique thing about Shnap is the versatility of it's syntax. In the case of blocks (at least, all blocks except for the scope block), there are many syntactic constructs that may be omitted. In the case of all blocks, parenthesis can be omitted, for example

if true {

}

is valid. Furthermore, if the block has exactly one instruction, the braces may also be omitted, meaning the following is also valid:

if true
    println("true")

This can be used to create a ternary-operator-like if-block, since if-blocks return the value of the last statement executed:

value = if (someCondition) 5 else 3
otherValue = if condition 5 else 3 //I don't like this syntax as much, but that's personal preference, and it's still valid...

Other than these general omissions, the variable name in the for and try-catch blocks may be omitted, in which case it defaults to "it", for example, all the following are valid

for (range(10)) {
    println(it)
}

for (range(10)) println(it)

for range(10) println(it)

try {

} catch {

}

try println(10) catch println(it)

Clone this wiki locally