Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
intro_example:pure_struct [2025/05/01 19:58] – rajit | intro_example:pure_struct [2025/05/18 15:07] (current) – rajit | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Using pure structures ====== | ====== Using pure structures ====== | ||
- | A [[language: | + | A [[language: |
+ | make CHP more human-readable. An example of a pure structure is: | ||
<code act> | <code act> | ||
Line 24: | Line 25: | ||
[] else -> log (" | [] else -> log (" | ||
] | ] | ||
- | []~cmd -> // write command | + | |
| | ||
| | ||
Line 56: | Line 57: | ||
- | defproc cache(chan? | + | defproc cache(chan? |
{ | { | ||
| | ||
Line 70: | Line 71: | ||
[] else -> log (" | [] else -> log (" | ||
] | ] | ||
- | []~cmd -> // write command | + | []~cmd -> // write command |
| | ||
| | ||
| | ||
+ | | ||
+ | ] | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Note that method functions, like other functions in ACT, are pure functions. In other words, they cannot change the fields of the structure. If you need to modify the fields of the structure, special '' | ||
+ | |||
+ | <code act> | ||
+ | deftype cache_entry (bool valid; int< | ||
+ | { | ||
+ | methods { | ||
+ | function match (int< | ||
+ | { | ||
+ | chp { | ||
+ | self := valid & tag = addr{31..8} | ||
+ | } | ||
+ | } | ||
+ | macro setval (int< | ||
+ | valid+; | ||
+ | data := dv; | ||
+ | tag := addr{31..8} | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | defproc cache(chan? | ||
+ | { | ||
+ | | ||
+ | | ||
+ | bool cmd; | ||
+ | | ||
+ | |||
+ | chp { | ||
+ | *[ A? | ||
+ | [cmd -> // read command | ||
+ | ce := C[addr{7..0}]; | ||
+ | [ ce.match(addr) -> log (" | ||
+ | [] else -> log (" | ||
+ | ] | ||
+ | | ||
+ | | ||
+ | | ||
| | ||
] | ] | ||
Line 88: | Line 133: | ||
<code act> | <code act> | ||
+ | int< | ||
... | ... | ||
chp { | chp { | ||
Line 98: | Line 144: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | The order in which the bits are packed is from left to right in the structure definition, with the left-most field corresponding to the most significant bits of the integer. |