-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoop.cs
More file actions
59 lines (52 loc) · 1.72 KB
/
Loop.cs
File metadata and controls
59 lines (52 loc) · 1.72 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
namespace GameDialog.Lang
{
/// <summary>
/// Information about loop iterations for preventing infinite loops.
/// </summary>
internal class Loop
{
/// <summary>
/// The line number where the loop is defined.
/// </summary>
public int Line => _location.Line;
/// <summary>
/// The number of iterations executed so far.
/// </summary>
private int _iterationCount;
/// <summary>
/// The location of the loop in the source code.
/// </summary>
private readonly Location _location;
/// <summary>
/// Initializes a new instance of the Loop class.
/// </summary>
/// <param name="location">The location of the loop in the source code.</param>
public Loop(Location location)
{
_location = location;
_iterationCount = 0;
}
/// <summary>
/// Increments the iteration count by one.
/// </summary>
/// <returns>The current Loop instance.</returns>
public Loop Increment()
{
_iterationCount++;
return this;
}
/// <summary>
/// Asserts that the iteration count does not exceed the specified maximum.
/// </summary>
/// <param name="maxIterations">The maximum allowed iterations.</param>
/// <returns>The current Loop instance.</returns>
public Loop Assert(int maxIterations)
{
if (_iterationCount > maxIterations)
{
throw new RuntimeError($"More than {maxIterations} iterations exceeded at line {Line}, possible infinite loop", _location);
}
return this;
}
}
}