Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| language:controlflow [2019/04/18 18:19] – created rajit | language:controlflow [2022/07/17 12:07] (current) – [Loops] rajit | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| convenient way to create arrayed structures. More complex structures | convenient way to create arrayed structures. More complex structures | ||
| such as trees can be easily created using recursive instantiations. | such as trees can be easily created using recursive instantiations. | ||
| + | |||
| + | ===== Loops ===== | ||
| + | |||
| + | An example of the loop construct in ACT is shown below: | ||
| + | |||
| + | <code act> | ||
| + | ( i : 10 : bool x[i..i]; ) | ||
| + | </ | ||
| + | |||
| + | The variable '' | ||
| + | delimited by the angle brackets. The colons separate the different parts | ||
| + | of the loop construct. The number '' | ||
| + | range '' | ||
| + | |||
| + | <code act> | ||
| + | bool x[0..0]; | ||
| + | bool x[1..1]; | ||
| + | ... | ||
| + | bool x[9..9]; | ||
| + | </ | ||
| + | |||
| + | The body of the loop can contain any ACT body, and therefore can | ||
| + | have multiple statements in it. A more common use of the loop statement | ||
| + | is shown below: | ||
| + | |||
| + | <code act> | ||
| + | register r[1..8]; | ||
| + | (i : 1..8 : r[i](in[i], | ||
| + | </ | ||
| + | |||
| + | In the example above, registers numbered '' | ||
| + | created. Their first two parameters are connected to the corresponding | ||
| + | elements of arrays '' | ||
| + | control that is passed in as the third parameter. | ||
| + | |||
| + | Since loops are part of ACT bodies, they can occur in the body of | ||
| + | another loop. Thus, nested loops are also supported. However, types | ||
| + | cannot be defined in the body of a loop. | ||
| + | |||
| + | A second more general looping construct is borrowed from the guarded | ||
| + | command language. | ||
| + | |||
| + | <code act> | ||
| + | pint i; | ||
| + | i=0; | ||
| + | *[ i < 10 -> bool x[i..i]; i = i + 1; ] | ||
| + | </ | ||
| + | |||
| + | This builds an array element by element, using the guarded command | ||
| + | syntax for a general while loop. Note that in this case we are | ||
| + | modifying a '' | ||
| + | only be used in the body of a type definition, since '' | ||
| + | are immutable in global scopes. | ||
| + | |||
| + | The first form of a loop is a special case of syntactic | ||
| + | replication. The general loop syntax can include a // | ||
| + | which is used in other contexts. | ||
| + | |||
| + | In production rule bodies, the loop | ||
| + | <code act> | ||
| + | (&i:3: x[i]) | ||
| + | </ | ||
| + | expands to | ||
| + | <code act> | ||
| + | x[0] & x[1] & x[2] | ||
| + | </ | ||
| + | (notice the absence of a trailing ''&'' | ||
| + | syntax). The ''&'' | ||
| + | instantiated for different values of '' | ||
| + | |||
| + | The syntactic replication construct is written as follows: | ||
| + | <code act> | ||
| + | (sym id : range : body ) | ||
| + | </ | ||
| + | The '' | ||
| + | <code act> | ||
| + | | ||
| + | </ | ||
| + | where '' | ||
| + | |||
| + | |||
| + | ===== Selections ===== | ||
| + | |||
| + | Conditional execution is supported by the selection statement. The | ||
| + | syntax of a selection statement is: | ||
| + | |||
| + | <code act> | ||
| + | [ boolean_expression -> body | ||
| + | [] boolean_expression -> body | ||
| + | .. | ||
| + | ] | ||
| + | </ | ||
| + | The last Boolean expression in the conditional can be the keyword | ||
| + | '' | ||
| + | |||
| + | Any one body whose corresponding Boolean expression is true is | ||
| + | executed. For instance, we can create 32 registers with | ||
| + | something special for register 0 as follows: | ||
| + | |||
| + | <code act> | ||
| + | (i : 32 : | ||
| + | [ i = 0 -> r0(in[i], | ||
| + | [] else -> r[i](in[i], | ||
| + | ] | ||
| + | ) | ||
| + | </ | ||
| + | |||
| + | Boolean expressions can be constructed from Boolean variables, the | ||
| + | constants '' | ||
| + | ''&'', | ||
| + | operations respectively. | ||
| + | ''<'', | ||
| + | the operators less than, less than or equal to, greater than, greater | ||
| + | than or equal to, equal to, and not equal to respectively. | ||
| + | |||
| + | ===== Recursion ===== | ||
| + | |||
| + | Type definitions can be recursive. For instance, the following | ||
| + | definition can be used to create a tree structure. | ||
| + | |||
| + | <code act> | ||
| + | template< | ||
| + | defproc tree (bool a[N]) | ||
| + | { | ||
| + | [ N = 1 -> leaf l(a[0]); | ||
| + | [] N > 1 -> tree< | ||
| + | | ||
| + | ] | ||
| + | } | ||
| + | </ | ||
| + | |||