forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandFlowControl.cs
184 lines (153 loc) · 4.69 KB
/
CommandFlowControl.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace kOS
{
[CommandAttribute(@"^{(.*)}$")]
public class CommandBlock : Command
{
List<Command> commands = new List<Command>();
String commandBuffer = "";
public CommandBlock(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
String innerText = RegexMatch.Groups[1].Value;
String cmd;
commandBuffer = innerText;
while (parseNext(ref innerText, out cmd))
{
commands.Add(Command.Get(cmd, this));
}
State = (commands.Count == 0) ? ExecutionState.DONE : ExecutionState.WAIT;
}
public override bool SpecialKey(kOSKeys key)
{
return base.SpecialKey(key);
}
public override void Refresh()
{
base.Refresh();
foreach (Command c in commands)
{
c.Refresh();
}
}
public new void Break()
{
commands.Clear();
State = ExecutionState.DONE;
}
public override void Update(float time)
{
foreach (Command command in commands)
{
switch (command.State)
{
case ExecutionState.NEW:
command.Evaluate();
ChildContext = command;
return;
case ExecutionState.WAIT:
command.Update(time);
return;
}
}
State = ExecutionState.DONE;
}
}
[CommandAttribute("IF *_{}")]
public class CommandIf : Command
{
List<Command> commands = new List<Command>();
Expression expression;
Command targetCommand;
public CommandIf(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
expression = new Expression(RegexMatch.Groups[1].Value, ParentContext);
targetCommand = Command.Get(RegexMatch.Groups[2].Value, this);
if (expression.IsTrue())
{
targetCommand.Evaluate();
Push(targetCommand);
State = ExecutionState.WAIT;
}
else
{
State = ExecutionState.DONE;
}
}
public override void Update(float time)
{
base.Update(time);
if (ChildContext.State == ExecutionState.DONE)
{
ChildContext = null;
State = ExecutionState.DONE;
}
}
}
[CommandAttribute("UNTIL *_{}")]
public class CommandUntilLoop : Command
{
List<Command> commands = new List<Command>();
Expression waitExpression;
String commandString;
public CommandUntilLoop(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
waitExpression = new Expression(RegexMatch.Groups[1].Value, ParentContext);
commandString = RegexMatch.Groups[2].Value;
State = ExecutionState.WAIT;
}
public override bool Break()
{
State = ExecutionState.DONE;
return true;
}
public override bool SpecialKey(kOSKeys key)
{
if (key == kOSKeys.BREAK)
{
StdOut("Break.");
Break();
}
return base.SpecialKey(key);
}
public override void Update(float time)
{
base.Update(time);
if (ChildContext == null)
{
if (waitExpression.IsTrue())
{
State = ExecutionState.DONE;
}
else
{
ChildContext = Command.Get(commandString, this);
((Command)ChildContext).Evaluate();
}
}
else
{
if (ChildContext.State == ExecutionState.DONE)
{
ChildContext = null;
}
}
}
}
[CommandAttribute("BREAK")]
public class CommandBreak : Command
{
public CommandBreak(Match regexMatch, ExecutionContext context) : base(regexMatch, context) { }
public override void Evaluate()
{
Break();
State = ExecutionState.DONE;
}
}
}