Blocks & control flow¶
Explicit blocks¶
Blocks can be explicitly delimited by enclosing a series of statements in curly brackets.
{
var x = 1 -- only visible inside the block
print(x)
}
is translated to
do
local x = 1
print(x)
end
If, while, repeat¶
Control structures work the same way in Hurdy as they do in Lua,
with the difference that word delimiters for blocks (e.g., do ... end
) are replaced with
C-style curly brackets.
if expr1 {
...
}
elseif expr2 {
...
}
else {
...
}
while expr {
...
}
repeat {
...
}
until expr
Curly brackets are optional if the block containes a single statement:
if x == true
print("True!")
For loops¶
The same applies to for loops, both numeric and generic
for x = 1, #t {
...
}
for k,v in ipairs(t) {
...
}
Curly brackets are optional if the block containes a single statement.
Continue statement¶
Hurdy includes a continue
statement that can be used to skip to the end of the current
iteration of a loop. This features requires a version of Lua that supports the goto
statement.
Internally this works by translating the continue
to goto continue
and adding a ::continue::
label at the end of the loop:
for i = 1, 10 {
if i == 2 continue
print(i)
}
is translated to the Lua code
for i = 1, 10 do
do
if i == 2 then goto continue end
print(i)
end
::continue::
}
The inner block of the loop in enclosed in an additional explicit block to allow nested
continue
statements.
Break & return¶
break
and return
statements work the same as in Lua, with the difference that
when compiled they are always wrapped in a do ... end
block. This allows to return or break
in the middle of a block/chunk.