-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.cs
More file actions
189 lines (156 loc) · 4.83 KB
/
Program.cs
File metadata and controls
189 lines (156 loc) · 4.83 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
int[] registers = new int[2];
#region While
// int i = 0;
// while (i < 10)
// {
// Console.WriteLine("#StopRussianAgression");
// i++;
// }
//var declarations = new ICommand[]
//{
// new PutConstantToRegisterCommand(0, 0),
// new WriteCommand("i", 0),
//};
//var condition = new ICommand[]
//{
// new PutConstantToRegisterCommand(1, 10),
// new ReadCommand("i", 0),
// new LtCommand(0)
//};
//var body = new ICommand[]
//{
// new OutputCommand("#StopRussianAgression")
//}.Concat(new IncrementCommand("i", 1).Compile()).ToArray();
//var commands = declarations.Concat(new WhileCommand(condition, body)
// .Compile()).ToArray();
#endregion
#region For
//int i = 0;
//for (; i < 10; i+=2)
//{
// Console.WriteLine("#Stand with Ukraine!!!");
//}
var declarations = new ICommand[]
{
new PutConstantToRegisterCommand(0, 0),
new WriteCommand("i", 0),
};
var condition = new ICommand[]
{
new PutConstantToRegisterCommand(1, 10),
new ReadCommand("i", 0),
new LtCommand(0)
};
var body = new ICommand[]
{
new OutputCommand("#Stand with Ukraine!!!")
};
var increment = new IncrementCommand("i", 2).Compile().ToArray();
var commands = declarations.Concat(new ForCommand(condition, body, increment)
.Compile()).ToArray();
#endregion
for (int i = 0; i < commands.Length;)
{
Console.Write($"[{i.ToString().PadLeft(3, '0')}]");
var currentCommand = commands[i];
currentCommand.Dump();
Console.CursorLeft = 60;
currentCommand.Execute(registers, ref i);
Console.CursorLeft = 20;
registers.Dump();
Console.CursorLeft = 30;
Memory.Dump();
Console.WriteLine();
}
Console.ReadLine();
class ForCommand
{
private readonly ICommand[] _condition;
private readonly ICommand[] _increment;
private readonly ICommand[] _body;
public ForCommand(ICommand[] condition, ICommand[] body, ICommand[] increment)
{
_condition = condition;
_body = body;
_increment = increment;
}
public IEnumerable<ICommand> Compile()
{
var realBody = _body.Concat(_increment.Concat(new ICommand[]
{
new PutConstantToRegisterCommand(0, int.MaxValue), // Stub
new JumpCommand()
})).ToArray();
var ifCommandsCount = new IfCommand(_condition, realBody, Array.Empty<ICommand>())
.Compile().Count() - 3;
realBody[^2] = new PutConstantToRegisterCommand(0, -ifCommandsCount);
return new IfCommand(_condition, realBody, Array.Empty<ICommand>()).Compile();
}
}
class WhileCommand
{
private readonly ICommand[] _condition;
private readonly ICommand[] _body;
public WhileCommand(ICommand[] condition, ICommand[] body)
{
_condition = condition;
_body = body;
}
public IEnumerable<ICommand> Compile()
{
var realBody = _body.Concat(new ICommand[]
{
new PutConstantToRegisterCommand(0, int.MaxValue), // Stub
new JumpCommand()
}).ToArray();
var ifCommandsCount = new IfCommand(_condition, realBody, Array.Empty<ICommand>())
.Compile().Count() - 3;
realBody[^2] = new PutConstantToRegisterCommand(0, -ifCommandsCount);
return new IfCommand(_condition, realBody, Array.Empty<ICommand>()).Compile();
}
}
class IncrementCommand
{
private readonly string _address;
private readonly int _incrementingValue;
public IncrementCommand(string address, int incrementingValue)
{
_address = address;
_incrementingValue = incrementingValue;
}
public IEnumerable<ICommand> Compile()
{
yield return new ReadCommand(_address, 0);
yield return new PutConstantToRegisterCommand(1, _incrementingValue);
yield return new AddCommand(0);
yield return new WriteCommand(_address, 0);
}
}
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;
}
}