forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextRunProgram.cs
184 lines (162 loc) · 5.6 KB
/
ContextRunProgram.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
{
public class ContextRunProgram : ExecutionContext
{
private int accumulator;
private File file;
private String commandBuffer;
private List<Command> commands = new List<Command>();
private List<Expression> parameters = new List<Expression>();
private int executionLine = 0;
public string Filename;
public ContextRunProgram(ExecutionContext parent, List<Expression> parameters, String filename) : base(parent)
{
this.parameters = parameters;
this.Filename = filename;
}
public void Run(File file)
{
this.file = file;
State = ExecutionState.WAIT;
RunBlock(file);
}
private void RunBlock(List<String> block)
{
foreach (String rawLine in block)
{
String line = stripComment(rawLine);
commandBuffer += line + "\n";
}
string cmd;
int lineNumber = 0;
int commandLineStart = 0;
while (parseNext(ref commandBuffer, out cmd, ref lineNumber, out commandLineStart))
{
try
{
Line = commandLineStart;
Command cmdObj = Command.Get(cmd, this, commandLineStart);
commands.Add(cmdObj);
}
catch (kOSException e)
{
if (ParentContext.FindClosestParentOfType<ContextRunProgram>() != null)
{
// Error occurs in a child of another running program
StdOut("Error in '" + e.Program.Filename + "' on line " + e.LineNumber + ": " + e.Message);
State = ExecutionState.DONE;
return;
}
else
{
// Error occurs in the top level program
StdOut("Error on line " + e.LineNumber + ": " + e.Message);
State = ExecutionState.DONE;
return;
}
}
catch (Exception e)
{
// Non-kos exception! This is a bug, but no reason to kill the OS
StdOut("Flagrant error on line " + lineNumber);
UnityEngine.Debug.Log("Program error");
UnityEngine.Debug.Log(e);
State = ExecutionState.DONE;
return;
}
}
if (commandBuffer.Trim() != "")
{
StdOut("End of file reached inside unterminated statement");
State = ExecutionState.DONE;
}
}
public override bool Break()
{
State = ExecutionState.DONE;
return true;
}
public string stripComment(string line)
{
for (var i=0; i<line.Length; i++)
{
if (line[i] == '\"')
{
i = Utils.FindEndOfString(line, i + 1);
if (i == -1) break;
}
else if (i < line.Length - 1 && line.Substring(i, 2) == "//")
{
return line.Substring(0, i);
}
}
return line;
}
public override void Update(float time)
{
try
{
base.Update(time);
EvaluateNextCommand();
}
catch (kOSException e)
{
if (ParentContext.FindClosestParentOfType<ContextRunProgram>() != null)
{
// Error occurs in a child of another running program
StdOut("Error in '" + e.Program.Filename + "' on line " + e.LineNumber + ": " + e.Message);
State = ExecutionState.DONE;
return;
}
else
{
// Error occurs in the top level program
StdOut("Error on line " + e.LineNumber + ": " + e.Message);
State = ExecutionState.DONE;
return;
}
}
catch (Exception e)
{
// Non-kos exception! This is a bug, but no reason to kill the OS
StdOut("Flagrant error on line " + executionLine);
UnityEngine.Debug.Log("Program error");
UnityEngine.Debug.Log(e);
State = ExecutionState.DONE;
return;
}
}
private void EvaluateNextCommand()
{
if (this.ChildContext == null)
{
if (commands.Count > 0)
{
Command cmd = commands[0];
commands.RemoveAt(0);
ChildContext = cmd;
cmd.Evaluate();
}
else
{
State = ExecutionState.DONE;
}
}
}
public object PopParameter()
{
if (parameters.Count > 0)
{
object retValue = parameters[0].GetValue();
parameters.RemoveAt(0);
return retValue;
}
throw new kOSException("Wrong number of parameters supplied");
}
}
}