-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatementChainService.cs
More file actions
125 lines (107 loc) · 4.55 KB
/
StatementChainService.cs
File metadata and controls
125 lines (107 loc) · 4.55 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
namespace COMP_3951_BlockForge_TechPro
{
/// <summary>
/// Provides traversal helpers for horizontally connected statement rows and their execution order.
/// </summary>
public sealed class StatementChainService
{
public CodeBlock GetStatementRoot(CodeBlock block, IDictionary<string, CodeBlock> blocks)
{
CodeBlock current = block;
while (!string.IsNullOrWhiteSpace(current.PreviousStatementBlockUid) &&
blocks.TryGetValue(current.PreviousStatementBlockUid, out CodeBlock? previous))
{
current = previous;
}
return current;
}
public List<CodeBlock> GetStatementChain(CodeBlock block, IDictionary<string, CodeBlock> blocks)
{
List<CodeBlock> chain = new();
CodeBlock current = GetStatementRoot(block, blocks);
chain.Add(current);
while (!string.IsNullOrWhiteSpace(current.NextStatementBlockUid) &&
blocks.TryGetValue(current.NextStatementBlockUid, out CodeBlock? next))
{
chain.Add(next);
current = next;
}
return chain;
}
public List<CodeBlock> GetExecutionOrderedStatementRoots(IDictionary<string, CodeBlock> blocks)
{
List<CodeBlock> orderedRoots = new();
HashSet<string> seenRoots = new();
IEnumerable<CodeBlock> verticalRoots = blocks.Values
.Where(block => string.IsNullOrWhiteSpace(block.ParentBlockUid))
.OrderBy(block => block.GridRow)
.ThenBy(block => block.GridColumn);
foreach (CodeBlock verticalRoot in verticalRoots)
{
CodeBlock current = verticalRoot;
while (true)
{
CodeBlock statementRoot = GetStatementRoot(current, blocks);
if (seenRoots.Add(statementRoot.Uid))
{
orderedRoots.Add(statementRoot);
}
if (string.IsNullOrWhiteSpace(current.ChildBlockUid) ||
!blocks.TryGetValue(current.ChildBlockUid, out CodeBlock? child))
{
break;
}
current = child;
}
}
foreach (CodeBlock statementRoot in blocks.Values
.Where(block => string.IsNullOrWhiteSpace(block.PreviousStatementBlockUid))
.OrderBy(block => block.GridRow)
.ThenBy(block => block.GridColumn))
{
if (seenRoots.Add(statementRoot.Uid))
{
orderedRoots.Add(statementRoot);
}
}
return orderedRoots;
}
public string BuildStatementText(CodeBlock block, IDictionary<string, CodeBlock> blocks)
{
List<CodeBlock> chain = GetStatementChain(block, blocks);
if (chain.Count == 0)
{
return string.Empty;
}
if (chain[0].BlockType == CodeBlockType.Print)
{
string expression = string.Join(" ", chain.Skip(1).Select(GetInlineToken)).Trim();
return string.IsNullOrWhiteSpace(expression)
? "Print"
: $"Print {expression}";
}
return string.Join(" ", chain.Select(GetInlineToken)).Trim();
}
private static string GetInlineToken(CodeBlock block)
{
return block.BlockType switch
{
CodeBlockType.Variable => block.BlockName ?? "variable",
CodeBlockType.Input => block.StringValue ?? string.Empty,
CodeBlockType.Assignment => "=",
CodeBlockType.Operator => string.IsNullOrWhiteSpace(block.StringValue) ? "+" : block.StringValue,
CodeBlockType.Equals => "==",
CodeBlockType.LessThan => string.IsNullOrWhiteSpace(block.StringValue) ? "<" : block.StringValue,
CodeBlockType.GreaterThan => string.IsNullOrWhiteSpace(block.StringValue) ? ">" : block.StringValue,
CodeBlockType.Print => "Print",
CodeBlockType.If => "If",
CodeBlockType.While => "While",
CodeBlockType.Run => "Run",
_ => block.BlockName ?? block.BlockType.ToString()
};
}
}
}