-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkspaceExecutionService.cs
More file actions
374 lines (323 loc) · 13.8 KB
/
WorkspaceExecutionService.cs
File metadata and controls
374 lines (323 loc) · 13.8 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using System.Collections.Generic;
using System.Linq;
namespace COMP_3951_BlockForge_TechPro
{
/// <summary>
/// Executes the current workspace in-memory using the existing vertical flow and horizontal statement links.
/// </summary>
/// <remarks>
/// Recent execution/interpreter work completed in this chat by Asher Drybrough, A01412779.
/// </remarks>
public sealed class WorkspaceExecutionService
{
private readonly StatementChainService _statementChainService = new();
/// <summary>
/// Executes the current workspace by following the Run block's vertical chain and evaluating each horizontal statement row.
/// </summary>
/// <param name="blocks">The workspace blocks keyed by UID.</param>
/// <returns>The captured output, diagnostics, and runtime variable state for the execution pass.</returns>
public WorkspaceExecutionResult Execute(IDictionary<string, CodeBlock> blocks)
{
WorkspaceExecutionResult result = new();
if (blocks.Count == 0)
{
result.Diagnostics.Add("Execution aborted: workspace is empty.");
return result;
}
InitializeVariableState(blocks, result);
CodeBlock? runBlock = blocks.Values
.Where(block => block.BlockType == CodeBlockType.Run)
.OrderBy(block => block.GridRow)
.ThenBy(block => block.GridColumn)
.FirstOrDefault();
if (runBlock == null)
{
result.Diagnostics.Add("Execution aborted: no Run block found.");
return result;
}
HashSet<string> visited = new();
CodeBlock? current = runBlock;
while (current != null)
{
if (!visited.Add(current.Uid))
{
result.Diagnostics.Add("Execution aborted: a loop was detected in the vertical flow.");
break;
}
if (current.BlockType != CodeBlockType.Run)
{
ExecuteStatement(current, blocks, result);
}
current = string.IsNullOrWhiteSpace(current.ChildBlockUid) ||
!blocks.TryGetValue(current.ChildBlockUid, out CodeBlock? child)
? null
: child;
}
return result;
}
private void InitializeVariableState(IDictionary<string, CodeBlock> blocks, WorkspaceExecutionResult result)
{
foreach (CodeBlock block in blocks.Values
.Where(block => block.BlockType == CodeBlockType.Variable && !string.IsNullOrWhiteSpace(block.BlockName))
.OrderBy(block => block.GridRow)
.ThenBy(block => block.GridColumn))
{
if (result.VariableState.ContainsKey(block.BlockName!))
{
continue;
}
result.VariableState[block.BlockName!] = GetVariableValue(block);
}
}
private void ExecuteStatement(CodeBlock statementBlock, IDictionary<string, CodeBlock> blocks, WorkspaceExecutionResult result)
{
List<CodeBlock> chain = _statementChainService.GetStatementChain(statementBlock, blocks);
if (chain.Count == 0)
{
return;
}
CodeBlock root = chain[0];
if (root.BlockType == CodeBlockType.Print)
{
if (chain.Count == 1)
{
result.OutputLines.Add(string.Empty);
return;
}
object value = EvaluateExpression(chain.Skip(1).ToList(), result.VariableState);
result.OutputLines.Add(FormatValue(value));
return;
}
if (root.BlockType == CodeBlockType.Variable &&
chain.Count >= 3 &&
chain[1].BlockType == CodeBlockType.Assignment)
{
string variableName = root.BlockName ?? "variable";
object value = EvaluateExpression(chain.Skip(2).ToList(), result.VariableState);
object coercedValue = CoerceToVariableType(value, root.VariableType);
result.VariableState[variableName] = coercedValue;
UpdateMatchingVariableBlocks(blocks, variableName, root.VariableType, coercedValue);
return;
}
if (root.BlockType == CodeBlockType.Variable && chain.Count == 1)
{
string variableName = root.BlockName ?? "variable";
result.VariableState[variableName] = GetVariableValue(root);
return;
}
if (IsStandaloneExpressionStatement(chain))
{
_ = EvaluateExpression(chain, result.VariableState);
return;
}
result.Diagnostics.Add($"Unsupported statement: {_statementChainService.BuildStatementText(root, blocks)}");
}
private static bool IsStandaloneExpressionStatement(IReadOnlyList<CodeBlock> chain)
{
if (chain.Count < 3)
{
return false;
}
if (!IsOperandBlock(chain[0].BlockType))
{
return false;
}
for (int index = 1; index < chain.Count; index += 2)
{
if (index >= chain.Count || !IsOperatorBlock(chain[index].BlockType))
{
return false;
}
if (index + 1 >= chain.Count || !IsOperandBlock(chain[index + 1].BlockType))
{
return false;
}
}
return true;
}
private static bool IsOperandBlock(CodeBlockType blockType)
{
return blockType == CodeBlockType.Variable || blockType == CodeBlockType.Input;
}
private static bool IsOperatorBlock(CodeBlockType blockType)
{
return blockType == CodeBlockType.Operator ||
blockType == CodeBlockType.Equals ||
blockType == CodeBlockType.LessThan ||
blockType == CodeBlockType.GreaterThan;
}
private static void UpdateMatchingVariableBlocks(
IDictionary<string, CodeBlock> blocks,
string variableName,
VariableBlockType? variableType,
object value)
{
foreach (CodeBlock block in blocks.Values.Where(block => block.BlockType == CodeBlockType.Variable && block.BlockName == variableName))
{
block.StringValue = null;
block.IntValue = null;
block.BoolValue = null;
switch (variableType)
{
case VariableBlockType.String:
block.StringValue = Convert.ToString(value) ?? string.Empty;
break;
case VariableBlockType.Int:
block.IntValue = Convert.ToInt32(value);
break;
case VariableBlockType.Bool:
block.BoolValue = Convert.ToBoolean(value);
break;
}
}
}
private object EvaluateExpression(IReadOnlyList<CodeBlock> tokens, IDictionary<string, object> variableState)
{
if (tokens.Count == 0)
{
throw new InvalidOperationException("Cannot evaluate an empty expression.");
}
object value = ResolveOperand(tokens[0], variableState);
for (int index = 1; index < tokens.Count; index += 2)
{
if (index + 1 >= tokens.Count)
{
throw new InvalidOperationException("Expression ended with an operator and is incomplete.");
}
string op = GetOperatorToken(tokens[index]);
object right = ResolveOperand(tokens[index + 1], variableState);
value = ApplyOperator(value, op, right);
}
return value;
}
private static object ResolveOperand(CodeBlock block, IDictionary<string, object> variableState)
{
return block.BlockType switch
{
CodeBlockType.Variable => ResolveVariable(block, variableState),
CodeBlockType.Input => GetInputValue(block),
_ => throw new InvalidOperationException($"Unsupported operand block: {block.BlockType}.")
};
}
private static object ResolveVariable(CodeBlock block, IDictionary<string, object> variableState)
{
string variableName = block.BlockName ?? throw new InvalidOperationException("Variable block is missing a name.");
if (variableState.TryGetValue(variableName, out object? value))
{
return value;
}
object initialValue = GetVariableValue(block);
variableState[variableName] = initialValue;
return initialValue;
}
private static object GetVariableValue(CodeBlock block)
{
return block.VariableType switch
{
VariableBlockType.String => block.StringValue ?? string.Empty,
VariableBlockType.Int => block.IntValue ?? 0,
VariableBlockType.Bool => block.BoolValue ?? false,
_ => block.StringValue ?? block.BlockName ?? string.Empty
};
}
private static object GetInputValue(CodeBlock block)
{
return block.VariableType switch
{
VariableBlockType.String => block.StringValue ?? string.Empty,
VariableBlockType.Int => block.IntValue ?? 0,
VariableBlockType.Bool => block.BoolValue ?? false,
_ => block.StringValue ?? string.Empty
};
}
private static string GetOperatorToken(CodeBlock block)
{
return block.BlockType switch
{
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!,
_ => throw new InvalidOperationException($"Unsupported operator block: {block.BlockType}.")
};
}
private static object ApplyOperator(object left, string op, object right)
{
return op switch
{
"+" => ApplyAddition(left, right),
"-" => Convert.ToInt32(left) - Convert.ToInt32(right),
"*" => Convert.ToInt32(left) * Convert.ToInt32(right),
"/" => ApplyDivision(left, right),
"%" => ApplyModulo(left, right),
"==" => Equals(left, right),
"<" => CompareNumeric(left, right) < 0,
"<=" => CompareNumeric(left, right) <= 0,
">" => CompareNumeric(left, right) > 0,
">=" => CompareNumeric(left, right) >= 0,
_ => throw new InvalidOperationException($"Unsupported operator '{op}'.")
};
}
private static int CompareNumeric(object left, object right)
{
return Convert.ToInt32(left).CompareTo(Convert.ToInt32(right));
}
private static object ApplyAddition(object left, object right)
{
if (left is string || right is string)
{
return $"{FormatValue(left)}{FormatValue(right)}";
}
return Convert.ToInt32(left) + Convert.ToInt32(right);
}
private static object ApplyDivision(object left, object right)
{
int divisor = Convert.ToInt32(right);
if (divisor == 0)
{
throw new DivideByZeroException("Cannot divide by zero.");
}
return Convert.ToInt32(left) / divisor;
}
private static object ApplyModulo(object left, object right)
{
int divisor = Convert.ToInt32(right);
if (divisor == 0)
{
throw new DivideByZeroException("Cannot divide by zero.");
}
return Convert.ToInt32(left) % divisor;
}
private static object CoerceToVariableType(object value, VariableBlockType? variableType)
{
return variableType switch
{
VariableBlockType.String => Convert.ToString(value) ?? string.Empty,
VariableBlockType.Int => Convert.ToInt32(value),
VariableBlockType.Bool => Convert.ToBoolean(value),
_ => value
};
}
private static string FormatValue(object value)
{
return value switch
{
bool boolValue => boolValue ? "true" : "false",
_ => Convert.ToString(value) ?? string.Empty
};
}
}
/// <summary>
/// Stores the output and runtime variable state produced by a lightweight workspace execution pass.
/// </summary>
/// <remarks>
/// Recent execution/interpreter work completed in this chat by Asher Drybrough, A01412779.
/// </remarks>
public sealed class WorkspaceExecutionResult
{
public List<string> OutputLines { get; } = new();
public List<string> Diagnostics { get; } = new();
public Dictionary<string, object> VariableState { get; } = new(StringComparer.Ordinal);
}
}