Skip to content

Commit 5dded24

Browse files
authored
Add mermaid diagrams for CFG and DAG
1 parent a0bbab4 commit 5dded24

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

contributor_docs/p5.strands.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ return b;
7070
// End chunk 3
7171
```
7272

73+
```mermaid
74+
flowchart TD
75+
76+
subgraph chunk1
77+
a0[let a = 0]
78+
b0[let b = 1]
79+
end
80+
81+
subgraph chunk2
82+
ifstart[if a < 2]
83+
b1[b = 10]
84+
ifend[end if]
85+
end
86+
87+
subgraph chunk3
88+
b2[b += 2]
89+
ret[return b]
90+
end
91+
92+
chunk1-->chunk2
93+
chunk2-->chunk3
94+
```
95+
7396
We store the individual states that variables can be in as nodes in a **directed acyclic graph (DAG)**. This is a fancy name that basically means each of these variable states may depend on previous variable states, and outputs can't feed back into inputs. Each time you modify a variable, that represents a new state of that variable. For example, below, it is not sufficient to know that `c` depends on `a` and `b`; you also need to know *which version of `b`* it branched off from:
7497

7598
```js
@@ -91,6 +114,19 @@ return c_0;
91114

92115
When we generate GLSL from the graph, we start from the variables we need to output, the return values of the function (e.g. `c_0` in the example above.) From there, we can track dependencies through the DAG (in this case, `b_1` and `a_1`). Each dependency has their own dependencies. We make sure we output the dependencies for a node before the node itself.
93116

117+
```mermaid
118+
flowchart TB
119+
120+
c_0-->c_0_plus((+))
121+
c_0_plus-->b_1
122+
c_0_plus-->a_0
123+
b_1-->b_1_plus((+))
124+
b_1_plus-->b_0
125+
b_1_plus-->n1_0[1]
126+
b_0-->b1_1[1]
127+
a_0-->n0[0]
128+
```
129+
94130
Each node in the DAG belongs to a chunk in the CFG. This helps us keep track of key points in the code. If we need to, for example, generate a temporary variable at the end of an if statement, we can refer to that CFG chunk rather than whatever the last value node in the if statement happens to be.
95131

96132
## Control flow

0 commit comments

Comments
 (0)