forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandBasicIO.cs
More file actions
286 lines (244 loc) · 8.95 KB
/
CommandBasicIO.cs
File metadata and controls
286 lines (244 loc) · 8.95 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
namespace kOS
{
[CommandAttribute("CLEARSCREEN")]
public class CommandClearScreen : Command
{
public CommandClearScreen(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
ParentContext.SendMessage(SystemMessage.CLEARSCREEN);
State = ExecutionState.DONE;
}
}
[CommandAttribute("SHUTDOWN")]
public class CommandShutdown : Command
{
public CommandShutdown(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
ParentContext.SendMessage(SystemMessage.SHUTDOWN);
State = ExecutionState.DONE;
}
}
[CommandAttribute("REBOOT")]
public class CommandReboot : Command
{
public CommandReboot(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
ParentContext.SendMessage(SystemMessage.RESTART);
State = ExecutionState.DONE;
}
}
[CommandAttribute("PRINT * AT_(2)")]
public class CommandPrintAt : Command
{
public CommandPrintAt(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
Expression e = new Expression(RegexMatch.Groups[1].Value, ParentContext);
Expression ex = new Expression(RegexMatch.Groups[2].Value, ParentContext);
Expression ey = new Expression(RegexMatch.Groups[3].Value, ParentContext);
if (e.IsNull()) throw new kOSException("Null value in print statement");
int x, y;
if (Int32.TryParse(ex.ToString(), out x) && Int32.TryParse(ey.ToString(), out y))
{
Put(e.ToString(), x, y);
}
else
{
throw new kOSException("Non-numeric value assigned to numeric function", this);
}
State = ExecutionState.DONE;
}
}
[CommandAttribute("PRINT *")]
public class CommandPrint : Command
{
public CommandPrint(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
Expression e = new Expression(RegexMatch.Groups[1].Value, ParentContext);
if (e.IsNull())
{
StdOut("NULL");
State = ExecutionState.DONE;
}
else
{
StdOut(e.ToString());
State = ExecutionState.DONE;
}
}
}
[CommandAttribute("TEST *")]
public class CommandTestKegex : Command
{
public CommandTestKegex(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
Expression e = new Expression(RegexMatch.Groups[1].Value, ParentContext);
StdOut(e.GetValue().ToString());
State = ExecutionState.DONE;
}
}
[CommandAttribute("DECLARE %")]
public class CommandDeclareVar : Command
{
public CommandDeclareVar(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
String varName = RegexMatch.Groups[1].Value;
Variable v = FindOrCreateVariable(varName);
if (v == null) throw new kOSException("Can't create variable '" + varName + "'", this);
State = ExecutionState.DONE;
}
}
[CommandAttribute("DECLARE PARAMETERS? *")]
public class CommandDeclareParameter : Command
{
public CommandDeclareParameter(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
if (!(ParentContext is ContextRunProgram)) throw new kOSException("DECLARE PARAMETERS can only be used within a program.", this);
foreach (String varName in RegexMatch.Groups[1].Value.Split(','))
{
Variable v = FindOrCreateVariable(varName);
if (v == null) throw new kOSException("Can't create variable '" + varName + "'", this);
var program = (ContextRunProgram)ParentContext;
v.Value = program.PopParameter();
}
State = ExecutionState.DONE;
}
}
[CommandAttribute("SET ~ TO *")]
public class CommandSet : Command
{
public CommandSet(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
Term targetTerm = new Term(RegexMatch.Groups[1].Value);
Expression e = new Expression(RegexMatch.Groups[2].Value, ParentContext);
if (targetTerm.Type == Term.TermTypes.STRUCTURE)
{
object baseObj = new Expression(targetTerm.SubTerms[0], ParentContext).GetValue();
if (baseObj is SpecialValue)
{
if (((SpecialValue)baseObj).SetSuffix(targetTerm.SubTerms[1].Text.ToUpper(), e.GetValue()))
{
State = ExecutionState.DONE;
return;
}
else
{
throw new kOSException("Suffix '" + targetTerm.SubTerms[1].Text + "' doesn't exist or is read only", this);
}
}
else
{
throw new kOSException("Can't set subvalues on a " + Expression.GetFriendlyNameOfItem(baseObj), this);
}
}
else
{
Variable v = FindOrCreateVariable(targetTerm.Text);
if (v != null)
{
v.Value = e.GetValue();
State = ExecutionState.DONE;
return;
}
}
}
}
[CommandAttribute("TOGGLE %")]
public class CommandToggle : Command
{
public CommandToggle(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
String varName = RegexMatch.Groups[1].Value;
Variable v = FindOrCreateVariable(varName);
if (v != null)
{
if (v.Value is bool)
{
v.Value = !((bool)v.Value);
State = ExecutionState.DONE;
}
else if (v.Value is float)
{
bool val = ((float)v.Value > 0) ? true : false;
v.Value = !val;
State = ExecutionState.DONE;
}
else
{
throw new kOSException("That variable can't be toggled.", this);
}
}
else
{
throw new kOSException("Can't find or create variable '" + varName + "'", this);
}
}
}
[CommandAttribute("% ON")]
public class CommandSetOn : Command
{
public CommandSetOn(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
String varName = RegexMatch.Groups[1].Value;
Variable v = FindOrCreateVariable(varName);
if (v != null)
{
if (v.Value is bool || v.Value is float)
{
v.Value = true;
State = ExecutionState.DONE;
}
else
{
throw new kOSException("That variable can't be set to 'ON'.", this);
}
}
else
{
throw new kOSException("Can't find or create variable '" + varName + "'", this);
}
}
}
[CommandAttribute("% OFF")]
public class CommandSetOff : Command
{
public CommandSetOff(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
String varName = RegexMatch.Groups[1].Value;
Variable v = FindOrCreateVariable(varName);
if (v != null)
{
if (v.Value is bool || v.Value is float)
{
v.Value = false;
State = ExecutionState.DONE;
}
else
{
throw new kOSException("That variable can't be set to 'OFF'.", this);
}
}
else
{
throw new kOSException("Can't find or create variable '" + varName + "'", this);
}
}
}
}