-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhileNode.cs
More file actions
57 lines (43 loc) · 1.93 KB
/
Copy pathWhileNode.cs
File metadata and controls
57 lines (43 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using NodeDev.Core.Connections;
using NodeDev.Core.Types;
using System.Linq.Expressions;
namespace NodeDev.Core.Nodes.Flow;
public class WhileNode : FlowNode
{
public override bool IsFlowNode => true;
public override bool AllowRemergingExecConnections => false;
public WhileNode(Graph graph, string? id = null) : base(graph, id)
{
Name = "While";
Inputs.Add(new("Exec", this, TypeFactory.ExecType));
Inputs.Add(new("Condition", this, TypeFactory.Get<bool>()));
Outputs.Add(new("ExecLoop", this, TypeFactory.ExecType));
Outputs.Add(new("ExecOut", this, TypeFactory.ExecType));
}
public override string GetExecOutputPathId(string pathId, Connection execOutput)
{
if (execOutput == Outputs[0])
return pathId + "-" + execOutput.Id;
else
return pathId;
}
public override bool DoesOutputPathAllowDeadEnd(Connection execOutput) => execOutput == Outputs[0]; // Loop has to be a dead end
public override bool DoesOutputPathAllowMerge(Connection execOutput) => execOutput == Outputs[1]; // Only the ExecOut path can merge. The loop path can never merge and always ends in a dead end.
private readonly string LabelName = $"break_{Random.Shared.Next(0, 100000)}";
internal override Expression BuildExpression(Dictionary<Connection, Graph.NodePathChunks>? subChunks, BuildExpressionInfo info)
{
ArgumentNullException.ThrowIfNull(subChunks);
var loopBody = Expression.Block(Graph.BuildExpression(subChunks[Outputs[0]], info)); // Build the loop body
var afterLoop = Expression.Block(Graph.BuildExpression(subChunks[Outputs[1]], info)); // Build the after loop body
var breakLabel = Expression.Label(LabelName);
var loop = Expression.Loop(
Expression.IfThenElse(
info.LocalVariables[Inputs[1]], // while condition
loopBody, // does the assign for enumerator.Current, as well as the loop body
Expression.Break(breakLabel) // break the loop
),
breakLabel
);
return Expression.Block(loop, afterLoop);
}
}