-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIfCommand.cs
More file actions
35 lines (28 loc) · 1.04 KB
/
IfCommand.cs
File metadata and controls
35 lines (28 loc) · 1.04 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
class IfCommand
{
private readonly ICommand[] _condition, _ifClause, _elseClause;
public IfCommand(ICommand[] condition, ICommand[] ifClause, ICommand[] elseClause)
{
_condition = condition;
_ifClause = ifClause;
_elseClause = elseClause;
}
public IEnumerable<ICommand> Compile()
{
foreach (var command in _condition)
yield return command;
yield return new PutConstantToRegisterCommand(1, 1);
yield return new AddCommand(0);
yield return new JumpCommand();
yield return new PutConstantToRegisterCommand(1, _ifClause.Length + 3);
yield return new PutConstantToRegisterCommand(0, 0);
yield return new AddCommand(0);
yield return new JumpCommand();
foreach (var command in _ifClause)
yield return command;
yield return new PutConstantToRegisterCommand(0, _elseClause.Length + 1);
yield return new JumpCommand();
foreach (var command in _elseClause)
yield return command;
}
}