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/07 09:08] – [Methods] 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 56: | Line 57: | ||
- | defproc cache(chan? | + | defproc cache(chan? |
{ | { | ||
| | ||
Line 74: | Line 75: | ||
| | ||
| | ||
+ | | ||
+ | ] | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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 (" | ||
+ | ] | ||
+ | | ||
+ | | ||
+ | | ||
| | ||
] | ] |