-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathInterpreterImmediate.cs
373 lines (310 loc) · 10.2 KB
/
InterpreterImmediate.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace kOS
{
public class ImmediateMode : ExecutionContext
{
private int cursor = 0;
private int baseLineY = 0;
private static int CMD_BACKLOG = 20;
private List<String> previousCommands = new List<String>();
private int prevCmdIndex = -1;
private String inputBuffer = "";
private String commandBuffer = "";
private int CursorX = 0;
private int CursorY = 0;
private new Queue<Command> Queue = new Queue<Command>();
private new char[,] buffer = new char[COLUMNS, ROWS];
public ImmediateMode(ExecutionContext parent) : base(parent)
{
//StdOut("kOS Operating System");
//StdOut("KerboScript v" + Core.VersionInfo.ToString());
for (var y = 13; y < 16; y++)
{
string output = "";
for (var x = 11; x < 16; x++)
{
output += ((char)((y*16) + x)).ToString();
}
if (y == 15) output += " v" + Core.VersionInfo.ToString();
StdOut(output);
}
StdOut("");
StdOut("");
StdOut("Proceed.");
}
public void Add(string cmdString)
{
commandBuffer += cmdString;
string nextCmd;
int line = 0;
int comandLineStart = 0;
while (parseNext(ref commandBuffer, out nextCmd, ref line, out comandLineStart))
{
try
{
Command cmd = Command.Get(nextCmd, this, comandLineStart);
Queue.Enqueue(cmd);
}
catch (kOSException e)
{
StdOut(e.Message);
Queue.Clear(); // HALT!!
}
}
}
public override int GetCursorX()
{
return ChildContext != null ? ChildContext.GetCursorX() : CursorX;
}
public override int GetCursorY()
{
return ChildContext != null ? ChildContext.GetCursorY() : CursorY;
}
public override bool Type(char ch)
{
if (base.Type(ch)) return true;
switch (ch)
{
case (char)8:
if (cursor > 0)
{
inputBuffer = inputBuffer.Remove(cursor - 1, 1);
cursor--;
}
break;
case (char)13:
Enter();
break;
default:
inputBuffer = inputBuffer.Insert(cursor, new String(ch, 1));
cursor++;
break;
}
UpdateCursorXY();
return true;
}
public override char[,] GetBuffer()
{
var childBuffer = (ChildContext != null) ? ChildContext.GetBuffer() : null;
return childBuffer != null ? childBuffer : buffer;
}
public void UpdateCursorXY()
{
CursorX = cursor % buffer.GetLength(0);
CursorY = (cursor / buffer.GetLength(0)) + baseLineY;
}
public void ShiftUp()
{
for (int y = 0; y < buffer.GetLength(1); y++)
{
for (int x = 0; x < buffer.GetLength(0); x++)
{
if (y + 1 < buffer.GetLength(1))
{
buffer[x, y] = buffer[x, y + 1];
}
else
{
buffer[x, y] = (char)0;
}
}
}
for (int x = 0; x < buffer.GetLength(0); x++)
{
buffer[x, buffer.GetLength(1) - 1] = (char)0;
}
if (baseLineY > 0) baseLineY--;
UpdateCursorXY();
}
public override void Put(string text, int x, int y)
{
foreach (char c in text)
{
if (x >= buffer.GetLength(0) || y >= buffer.GetLength(1)) return;
buffer[x, y] = c;
x++;
}
}
public override void StdOut(string line)
{
int linesWritten = WriteLine(line);
baseLineY += linesWritten;
UpdateCursorXY();
}
public void ClearScreen()
{
baseLineY = 0;
cursor = 0;
for (int y = 0; y < buffer.GetLength(1); y++)
for (int x = 0; x < buffer.GetLength(0); x++)
{
buffer[x, y] = (char)0;
}
UpdateCursorXY();
}
public int WriteLine(string line)
{
int lineCount = (line.Length / buffer.GetLength(0)) + 1;
while (baseLineY + lineCount > buffer.GetLength(1))
{
ShiftUp();
}
for (int y = baseLineY; y < buffer.GetLength(1); y++)
for (int x = 0; x < buffer.GetLength(0); x++)
{
buffer[x, y] = (char)0;
}
char[] inputChars = line.ToCharArray();
int writeX = 0;
int writeY = baseLineY;
foreach (char c in inputChars)
{
buffer[writeX, writeY] = c;
writeX++;
if (writeX >= buffer.GetLength(0)) { writeX = 0; writeY++; }
}
return lineCount;
}
public override void Update(float time)
{
if (ChildContext == null)
{
if (Queue.Count > 0)
{
var currentCmd = Queue.Dequeue();
try
{
Push(currentCmd);
currentCmd.Evaluate();
}
catch (kOSException e)
{
StdOut(e.Message);
Queue.Clear(); // Halt all pending instructions
ChildContext = null; //
}
catch (Exception e)
{
// Non-kos exception! This is a bug, but no reason to kill the OS
StdOut("Flagrant error occured, logging");
if (CPU.RunType == CPU.kOSRunType.KSP)
{
Debug.Log("Immediate mode error");
Debug.Log(e);
}
Queue.Clear();
ChildContext = null;
}
}
else
{
WriteLine(inputBuffer);
}
}
try
{
base.Update(time);
}
catch (kOSException e)
{
StdOut(e.Message);
ChildContext = null;
Queue.Clear(); // Halt all pending instructions
}
catch (Exception e)
{
// Non-kos exception! This is a bug, but no reason to kill the OS
StdOut("Flagrant error occured, logging");
if (CPU.RunType == CPU.kOSRunType.KSP)
{
UnityEngine.Debug.Log("Immediate mode error");
UnityEngine.Debug.Log(e);
}
Queue.Clear();
ChildContext = null;
}
}
private void Enter()
{
baseLineY += WriteLine(inputBuffer);
while (baseLineY > buffer.GetLength(1) - 1) ShiftUp();
previousCommands.Add(inputBuffer);
if (previousCommands.Count > CMD_BACKLOG)
{
int overflow = previousCommands.Count - CMD_BACKLOG;
previousCommands.RemoveRange(0, overflow);
}
prevCmdIndex = -1;
Add(inputBuffer += "\n");
inputBuffer = "";
cursor = 0;
UpdateCursorXY();
}
public override void SendMessage(SystemMessage message)
{
switch (message)
{
case SystemMessage.CLEARSCREEN:
ClearScreen();
break;
default:
base.SendMessage(message);
break;
}
}
public void PreviousCommand(int direction)
{
if (previousCommands.Count == 0) return;
inputBuffer = "";
cursor = 0;
prevCmdIndex += direction;
if (prevCmdIndex <= -1)
{
inputBuffer = "";
prevCmdIndex = -1;
cursor = 0;
UpdateCursorXY();
return;
}
if (prevCmdIndex > previousCommands.Count-1)
{
prevCmdIndex = previousCommands.Count - 1;
}
inputBuffer = previousCommands[(previousCommands.Count-1) - prevCmdIndex];
cursor = inputBuffer.Length;
UpdateCursorXY();
}
public override bool SpecialKey(kOSKeys key)
{
if (base.SpecialKey(key)) return true;
switch (key)
{
case kOSKeys.UP:
PreviousCommand(1);
return true;
case kOSKeys.DOWN:
PreviousCommand(-1);
return true;
case kOSKeys.LEFT:
if (cursor > 0)
{
cursor--;
UpdateCursorXY();
}
return true;
case kOSKeys.RIGHT:
if (cursor < inputBuffer.Length)
{
cursor++;
UpdateCursorXY();
}
return true;
}
return false;
}
}
}