0 | job | error management block |
---|---|---|
1 | if | conditional expression |
2 | else | decision block |
3 | ladder | conditional selector |
3 | match | value based selector |
4 | loop | unconditional repetition |
5 | while | conditional repetition |
6 | for | range controlled repetition |
Job block is one of most powerful Eve feature. It enable creation of protected blocks that can handle exceptions. This block can be used in processes, routines, functions or methods to avoid runtime errors.
**job description or notes
job job_name try
** some job statements or test use-case
...
raise "error" if condition; -- intrerupt job with error
...
break if condition; -- intrerupt job without error
...
catch error1:
** handle error
catch error2:
** handle error
catch other:
** handle all errors
resolve:
** close resources, tear-down test cases, other things
...
let job_name.status = "pass" if condition; -- job status
done [name]; -- you can't use if after done
Notes:Job statement also use keywords: {raise, catch, other, break and done}, but it can use also: {expect, exit, over, panic} to intrerupt a program prematurly. A job can fail but the program will continue with next job if failure is handled properly. A job status is recorded for reporting. Job status is handled automaticly by the system. If a job raise an error, automaticly is considered failed.
Job block is similar with try block in java, except it does not have a final clause instead we use "resolve" that is more relevant for purpose of a job. A job can be rezolved in a flexible way. The most common is to set-up job "status". A job is an object in Eve, so the job has methods and properties. We reffer to current job by it's name.
Keyword "if" establish a conditional statement. Also it can be used in expressions. A conditional statement can be a simple statement or a block statement. You can also use "if" after a simple statement or before a block statement.
This is syntax for a simple statement with conditional execution. It enable or prevent a statement to be executed.
# conditioned statement
statement if boolean_variable;
statement if boolean_expression;
Next examples show how we can use conditional to print something when expression is fulfilled.
# conditional print
print "a is even" if (a % 2 = 0);
print "a is odd" if (a % 2 != 0);
>Note: The "if" is restricted for some statements. It can't be used after expect, done or next, but it can be used sometimes after "repeat" to terminate a loop. We will explain syntax for each block statement.
In next pattern we use "if/else" keywords to create a decision branch/fork.
decision diagram
# dual path decision
if expression then
** true branch
...
else
** false branch
...
done; -- you can't use if after done
A ladder is a conditional multi-path selector based on exclusive conditions. Only one branch is executed. If no condition is satisfied the alternative branch is executed. Alternative branch is optional.
decision ladder
#selection ladder
if condition_1 then
** first branch
...
else if condition_2 then
** second branch
...
else if condition_3 then
** third branch
...
[else]
** alternative branch
...
done; -- you can't use if after done
Notes: Previous pattern show a complex situation having several conditions in cascade. Cases should be exclusive and most probable case should be evaluated first. Last block, after else is optional. Also, the local scope is optional.
This is a multi-path selector similar to switch also named: jump table. It two variants: "all" or "one". That is optional keyword. If used, ALL will evaluate all values it may be matching multiple cases. If "one" is used (this is the default), only first case that has a matching value will be executed and the rest are ignored, even if a second match exist. The second match should not be present though and is probably a mistake!
Match Diagram
# Define a selector variable
class Type = {V1, V2, V3, V4} <: Ordinal;
...
process
new s := V1;
** create match selector
match s [all | one] [Type]:
** declare locals
...
when V1 then
** first path
...
when V1, V2 then
** second path
...
when [any | other] then
** V3 or V4 found
...
done; -- you can't use if after done
return;
The compiler will generate a warning message if not all cases are covered when you do not create the default block. It may be an error but not necesarly. Is better to have information to prevent wrong doing.
This is a repetitive block statement. The loop is executed until a "stop" statement is encounter. The stop can be conditioned by "if" or "match". This is called an "interruption". You can use keyword "skip" with conditional "if" to restart the loop early. This is called a "shortcut" statement.
infinite loop
#infinite loop
cycle [label]:
** declare control variable
...
loop
** loop shortcut
skip [label] [if condition];
...
** early interruption
stop [label] [if condition];
...
repeat [label] [if condition];
Execute a block of code as long as one condition is true. When the condition become false the repetitive block ends. If the condition is never true only the "else" block is executed. If the condition was true for a while, the else block is not executed.
while loop
#conditional loop
cycle [label]:
** declare control variables
...
while condition loop
** shortcut iteration
skip if condition;
...
** early interruption
stop if condition;
...
else
** alternative path
...
repeat [label]; -- you can't use "if" here
/** This driver demonstrate
how to visit members of a List */
driver while_demo:
** create a shared list
set this := ("a","b","c","d","e");
process
** start unlabeled cycle
cycle:
** volatile variables
new i := 0 : Integer;
new e : Symbol;
while i < this.length() loop
let e := this[i];
let i += 1;
if e >= "c" then
write e;
write ',' if e is not this.tail;
done;
else
panic; -- in canse of length() == 0
repeat;
** flush the write buffer
print; -- "c","d","e"
return;
"c","d","e" i = 5
This statement use one control variables to visit elements in a collection or domain. First use-case of this is to iterate over a range of numbers. In Eve the range is represented as (min..nax).
for ... loop
#for loop syntax
cycle [label]:
new var := 0;
for var in (min..max) loop
** block statements;
...
** next iteration
next if condition;
...
** early interruption
stop if condition;
...
repeat [label]; -- you can't use "if" here
Example of range iteration using step ratio 2:
#example of range iteration
cycle:
new i :Integer;
for i in (1..10:2) loop
** write only odd numbers
write i;
write ',' if (i < 9);
repeat;
print;
1,3,5,7,9
Read next: Processing