Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
language:types2:data [2024/07/18 16:14] – rajit | language:types2:data [2025/05/26 19:46] (current) – [Parameter structures] rajit | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | A data type is defined using '' | + | A user-defined |
- | to an integer or Boolean value, | + | * //data types//, which correspond to representations of the built-in '' |
- | construct like a record or structure (from software | + | * // |
- | languages). The syntax is similar to a process, and the constraints | + | * //parameter structures//, |
- | about declarations/ | + | |
+ | In addition, data types can also be // | ||
+ | |||
+ | ===== Data types ===== | ||
+ | |||
+ | A data type corresponds to an integer or Boolean value, | ||
+ | could also be a composite construct like a record or structure (from software | ||
+ | programming | ||
+ | about declarations/ | ||
+ | the underlying built-in '' | ||
+ | level of abstraction. | ||
Often data types have some additional structure that is not required for a | Often data types have some additional structure that is not required for a | ||
Line 49: | Line 59: | ||
type is supposed to represent a circuit structure that is used to | type is supposed to represent a circuit structure that is used to | ||
represent a data value. | represent a data value. | ||
+ | |||
+ | ==== Special data methods ==== | ||
+ | |||
+ | There are two special methods that can be specified for a data type: | ||
+ | - a //set method//, used to write a value to the type; | ||
+ | - a //get method//, used to read the value of the type. | ||
+ | One can think of these as type conversion methods invoked | ||
+ | automatically to read or write the data type. When a normal | ||
+ | data type is used, the special variable '' | ||
+ | defined to be the built-in type that is implemented by the | ||
+ | user-defined data type. | ||
+ | |||
+ | <code act> | ||
+ | deftype d1of2 <: int< | ||
+ | { | ||
+ | spec { | ||
+ | exclhi(d0, | ||
+ | } | ||
+ | | ||
+ | set { | ||
+ | | ||
+ | } | ||
+ | get { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In the example above, the '' | ||
+ | '' | ||
+ | '' | ||
+ | '' | ||
+ | the methods specify conversion operations. | ||
+ | |||
+ | The selection statement in the '' | ||
+ | selection operator '' | ||
+ | that when the '' | ||
+ | cannot both be '' | ||
+ | specification body. Also, if both '' | ||
+ | (i.e. an illegal state in which to execute a get operation), the | ||
+ | variable '' | ||
+ | one of '' | ||
+ | data type. (This is different in the case of a channel, where the | ||
+ | semantics of the channel permit waiting.) | ||
Line 67: | Line 122: | ||
The distinction between a structure and another data type is that other data types are | The distinction between a structure and another data type is that other data types are | ||
implementations of one of the built-in types like '' | implementations of one of the built-in types like '' | ||
+ | |||
+ | ==== Pure structures ==== | ||
+ | |||
+ | A pure structure is one that only contains other pure structures or '' | ||
+ | In other words, they are structures that don't include channels. Structures like this correspond | ||
+ | to what is normally viewed as a record/ | ||
+ | |||
+ | Pure structures can be used with special function methods, providing a degree of object-oriented | ||
+ | programming capability within ACT. | ||
+ | |||
+ | The following is an example of a pure structure: | ||
+ | <code act> | ||
+ | deftype purestruct (int< | ||
+ | </ | ||
+ | |||
+ | Pure structures can be converted to and from an '' | ||
+ | |||
+ | <code act> | ||
+ | purestruct p; | ||
+ | int< | ||
+ | ... | ||
+ | chp { | ||
+ | ... | ||
+ | x := int(p); | ||
+ | ... | ||
+ | p := purestruct(x); | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | In the example above, the ' | ||
+ | <code act> | ||
+ | ... | ||
+ | x := {p.a, p.b, p.q}; | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | Similarly, the '' | ||
+ | ===== Parameter structures ===== | ||
+ | |||
+ | It can be convenient to group a collection of parameters for readability/ | ||
+ | <code act> | ||
+ | | ||
+ | </ | ||
+ | |||
+ | This defines '' | ||
+ | <code act> | ||
+ | template <myps p> defproc myprocess (bool x[10]) | ||
+ | { | ||
+ | // use p.a, p.b etc. | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Parameter structures can be initialized element-by-element in the usual way, or by using a built-in constructor for them: | ||
+ | <code act> | ||
+ | myps p; | ||
+ | p.a = 5; // element initialization | ||
+ | |||
+ | myps q; | ||
+ | |||
+ | q = myps (4, 8, false); | ||
+ | </ | ||
+ | |||
+ | ===== Enumerations ===== | ||
+ | |||
+ | A user-defined enumeration can be specified as follows: | ||
+ | |||
+ | <code act> | ||
+ | defenum myenum { | ||
+ | ADD, SUB, MULT | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | This defines an enumeration called '' | ||
+ | |||
+ | The enumeration constants can be accessed using the enumeration type name as follows: | ||
+ | |||
+ | <code act> | ||
+ | ... | ||
+ | myenum x; | ||
+ | ... | ||
+ | chp { | ||
+ | ... | ||
+ | x := myenum.SUB; | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Trying to set an enumeration to an integer will result in a type error: | ||
+ | |||
+ | <code act> | ||
+ | chp { | ||
+ | ... | ||
+ | x := 1; | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | will lead to: | ||
+ | |||
+ | < | ||
+ | -[ERROR]-> | ||
+ | stmt: x := 1 | ||
+ | Enumeration/ | ||
+ | </ | ||
+ | |||
+ | However, sometimes a user may want to use user-defined enumerations as integers. In order to do so, | ||
+ | the enumeration can be declared to be a valid integer as follows: | ||
+ | |||
+ | <code act> | ||
+ | defenum myenum : int { | ||
+ | ADD, SUB, MULT | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | This version will permit integer assignments. Note that it is up to the user to ensure that integer assignments are within range. The ACT simulator '' | ||