Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| language:types2 [2026/06/25 11:00] – [Macros] rajit | language:types2 [2026/06/27 20:05] (current) – [Example user-defined type with macros and methods] rajit | ||
|---|---|---|---|
| Line 285: | Line 285: | ||
| <code act> | <code act> | ||
| - | defproc stack (chan(int)? in; chan(int)!out ) | + | defproc stack (chan(int)? in; chan(int)! out) |
| { | { | ||
| ... | ... | ||
| Line 317: | Line 317: | ||
| <code act> | <code act> | ||
| - | defproc stack (chan(int)? in; chan(int)!out ) | + | defproc stack (chan(int)? in; chan(int)! out) |
| { | { | ||
| ... | ... | ||
| Line 384: | Line 384: | ||
| In the linked example, '' | In the linked example, '' | ||
| + | ==== Example user-defined type with macros and methods ==== | ||
| + | |||
| + | Combining some of these ideas, the following is an example of a user-defined type corresponding to an N-bit signed integer. Only some methods are defined. | ||
| + | |||
| + | <code act> | ||
| + | template< | ||
| + | deftype signed_int (int< | ||
| + | { | ||
| + | { W > 1 : "Need sign bit plus one bit at a minumum" | ||
| + | methods { | ||
| + | function plus (signed_int< | ||
| + | { | ||
| + | chp { | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | macro display() | ||
| + | { | ||
| + | [ x{W-1} = 1 -> log_p(" | ||
| + | [] else -> log_p(x) | ||
| + | ] | ||
| + | } | ||
| + | macro set(int< | ||
| + | | ||
| + | x := v | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | In this scenario, the following CHP adds two numbers, and then displays the result using a '' | ||
| + | <code act> | ||
| + | defproc test() | ||
| + | { | ||
| + | | ||
| + | chp { | ||
| + | | ||
| + | | ||
| + | v := v + u; | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | This should display '' | ||