- Flow-control constructs such as 'if', 'while' and 'for' obey HFL scope rules, and behave very similarly to the javascript constructs of the same name. The one major difference is that when nesting two flow-control constructs in HFL, the braces are required on the outer of the two.
-
- Conditionally executes the code block based upon evaluation of <test_clause>
- NOTE: '&&' and '||' operators in <test-clause> clauses only evaluate their second operands if the evaluation of the first operand does not satisfy the operator. Thus they can be used to prevent evaluation of expressions or function calls by having a pre-test for a deciding condition. This is consistent with how javascript does it.
- EXAMPLE:
-
var $x = 2;
// Prints: |2 IS POSITIVE OR ZERO|
if ($x < 0) {
print("$x IS NEGATIVE");
}
else {
print("$x IS POSITIVE OR ZERO");
}
-
where:<case>:=case <constant> : [<code-block>] [break ;]| default :[<code-block>] [break ;]
- Evaluates the <test-clause> expression, and then compares the result against each of the constant values of the 'case' clauses. If finds a match begins execution at the statement after the 'case' statement. If does not find a match, begins execution at the statement after the 'default' statement if specified.
- Execution will continue until encounters a 'break' statement or has executed the last statement in the 'switch' block.
- EXAMPLE:
-
var $animal = 'lion';
switch ($animal) {
case 'lion':
print('It's a lion!');
break;
case 'tiger':
print('It's a tiger!');
break;
case 'bear':
print('It's a bear!');
break;
default:
print('Lions and tigers and bears! Oh my!');
break;
}
-
- Performs a C/javascript-style 'for' loop.
- EXAMPLE:
-
// Print number 10 through 1, backwards
for ($i = 10; $i > 0; $i--) {print("$i");}
-
- Performs a Perl-style 'for' loop. Loops through the elements of the array, assigning their values to the variable on each, then calls the code block.
- EXAMPLE:
-
var $arr = ['a', 'b', 'c'], $elem;
// Print out elements of array, as:
// ARRAY ITEM IS a
// ARRAY ITEM IS b
// ARRAY ITEM IS c
for $elem ($arr) { print("ARRAY ITEM IS $elem"); }
-
- Performs a Perl-style 'for' loop on a hash table. Loops through the elements of the hash table, assigning their values to the variable on each, then calls the code block.
- Note that this form does not provide the keys for each of the elements, just the values.
- EXAMPLE:
-
var $h = hash('a', 1, 'b', 2, 'c', 3), $elem;
// Print out elements of hash, but not necessarily in order, as:
// HASH ITEM IS 1
// HASH ITEM IS 2
// HASH ITEM IS 3
for $elem ($h) { print("HASH ITEM IS $elem"); }
-
- Loops while the test clause is true, calling the code block on each pass.
- EXAMPLE:
-
var $i = 0;
// Prints numbers 0 through 9
while ($i++ < 10)
print("I IS $i");
-
- Skips the remainder of code block of the outer 'while()' or 'for()' loop and jumps to the start of the next iteration. For 'while' loops it jumps back to the <test-clause> expression. For 'for' loops it jumps back to the <incr-clause> expression.
- If the optional label is provided, it will jump top of the loop of the label. Otherwise it will jump to the top of the next outer 'while()' or 'for()' loop.
- EXAMPLE:
-
var $i = 0;
// Prints numbers 2, 4, 6, 8, and 10
while ($i++ < 10) {
if ($i % 2) continue;
print("I IS $i");
}
-
- Terminates the outer 'while()' or 'for()' loop.
- If the optional label is provided, it will terminate the loop of the label. Otherwise it will terminate the next outer 'while()' or 'for()' loop.
- EXAMPLE:
-
var $i = 0;
// Prints numbers 0 through 3
while ($i++ < 10) {
if ($i >= 4) break;
print("I IS $i");
}
-
- Skips the remainder of code block of the enclosing or specified scope and jumps to the start of the next iteration.
- If the optional label is provided, it will jump start of the code block for the scope of the label.
- Otherwise it will jump to the top of the next outer scope.
- EXAMPLE:
-
cell:all {
if (isWS($text))
next;
DoSomething($cell_num);
}
-
- Terminates the block of the enclosing or specified scope.
- If the optional label is provided, it will terminate the code block of the scope of the label.
- Otherwise it will terminate the code block of the next outer scope.
- EXAMPLE:
-
cell:all {
if ($class == 'syntax_literal') {
DoSomething($cell_num);
if ($scope_count == 1) last;
}
}
Defining Functions
- HFL supports javascript-style application-defined functions. If the function name is omitted, returns an anonymous function that can be used as a value in an expression, such as being assigned to a variable or passed as an argument in a function call.
- If the function is defined within the body of another function, it creates a closure, where it shares the values of the local variables of the enclosing function. This emulates the closure behavior of javascript.
- EXAMPLES:
-
var $sum = add(2, 3);
print("sum is $sum"); // Prints 'sum is 5'
// Define function to add two numbers
function add ($a, $b) {
var $result = $a + $b;
return $result;
}
-
- Exits execution of current application-defined function, returning result of evaluation of the <expr> expression if provided.