-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathProtoScriptRunner.cs
More file actions
367 lines (322 loc) · 14.1 KB
/
Copy pathProtoScriptRunner.cs
File metadata and controls
367 lines (322 loc) · 14.1 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
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
using System;
using System.Collections.Generic;
using System.Text;
using ProtoCore.DSASM;
using ProtoCore.DSASM.Mirror;
using ProtoCore.Utils;
namespace ProtoScript.Runners
{
public class ProtoScriptRunner
{
public ProtoCore.DebugServices.EventSink EventSink = new ProtoCore.DebugServices.ConsoleEventSink();
private bool Compile(string code, ProtoCore.Core core, ProtoCore.CompileTime.Context context)
{
bool buildSucceeded = false;
try
{
// No More HashAngleReplace for unified parser (Fuqiang)
//String strSource = ProtoCore.Utils.LexerUtils.HashAngleReplace(code);
//defining the global Assoc block that wraps the entire .ds source file
var globalBlock = new ProtoCore.LanguageCodeBlock
{
Language = ProtoCore.Language.Associative,
Code = code
};
//passing the global Assoc wrapper block to the compiler
ProtoCore.Language id = globalBlock.Language;
core.Compilers[id].Compile(out _, null, globalBlock, context, EventSink);
core.BuildStatus.ReportBuildResult();
buildSucceeded = core.BuildStatus.BuildSucceeded;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return buildSucceeded;
}
private bool Compile(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList, ProtoCore.Core core, ProtoCore.CompileTime.Context context)
{
bool buildSucceeded = false;
if (astList.Count <= 0)
{
// Nothing to compile
buildSucceeded = true;
}
else
{
try
{
//defining the global Assoc block that wraps the entire .ds source file
ProtoCore.LanguageCodeBlock globalBlock = new ProtoCore.LanguageCodeBlock();
globalBlock.Language = ProtoCore.Language.Associative;
globalBlock.Code = string.Empty;
//passing the global Assoc wrapper block to the compiler
context.SetData(string.Empty, new Dictionary<string, object>(), null);
ProtoCore.Language id = globalBlock.Language;
ProtoCore.AST.AssociativeAST.CodeBlockNode codeblock = new ProtoCore.AST.AssociativeAST.CodeBlockNode();
codeblock.Body.AddRange(astList);
int blockId = ProtoCore.DSASM.Constants.kInvalidIndex;
core.Compilers[id].Compile(out blockId, null, globalBlock, context, EventSink, codeblock);
core.BuildStatus.ReportBuildResult();
buildSucceeded = core.BuildStatus.BuildSucceeded;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
return buildSucceeded;
}
private ProtoCore.RuntimeCore CreateRuntimeCore(ProtoCore.Core core)
{
ProtoCore.RuntimeCore runtimeCore = new ProtoCore.RuntimeCore(core.Heap);
runtimeCore.SetupForExecution(core, core.GlobOffset);
return runtimeCore;
}
/// <summary>
/// Execute the data stored in core
/// This is the entry point of all DS code to be executed
/// </summary>
/// <param name="core"></param>
/// <returns></returns>
public ProtoCore.RuntimeCore ExecuteVM(ProtoCore.Core core)
{
ProtoCore.RuntimeCore runtimeCore = CreateRuntimeCore(core);
runtimeCore.StartTimer();
try
{
foreach (ProtoCore.DSASM.CodeBlock codeblock in core.CodeBlockList)
{
// Comment Jun:
// On first bounce, the stackframe depth is initialized to -1 in the Stackfame constructor.
// Passing it to bounce() increments it so the first depth is always 0
ProtoCore.DSASM.StackFrame stackFrame = new ProtoCore.DSASM.StackFrame(core.GlobOffset);
stackFrame.FramePointer = runtimeCore.RuntimeMemory.FramePointer;
// Comment Jun: Tell the new bounce stackframe that this is an implicit bounce
// Register TX is used for this.
StackValue svCallConvention = StackValue.BuildCallingConversion((int)ProtoCore.DSASM.CallingConvention.BounceType.Implicit);
stackFrame.TX = svCallConvention;
// Initialize the entry point interpreter
int locals = 0; // This is the global scope, there are no locals
ProtoCore.DSASM.Interpreter interpreter = new ProtoCore.DSASM.Interpreter(runtimeCore);
runtimeCore.CurrentExecutive.CurrentDSASMExec = interpreter.runtime;
runtimeCore.CurrentExecutive.CurrentDSASMExec.Bounce(codeblock.codeBlockId, codeblock.instrStream.entrypoint, stackFrame, locals);
}
runtimeCore.NotifyExecutionEvent(ProtoCore.ExecutionStateEventArgs.State.ExecutionEnd);
}
catch
{
runtimeCore.NotifyExecutionEvent(ProtoCore.ExecutionStateEventArgs.State.ExecutionEnd);
throw;
}
return runtimeCore;
}
/// <summary>
/// ExecuteLive is called by the liverunner where a persistent RuntimeCore is provided
/// ExecuteLive assumes only a single global scope
/// </summary>
/// <param name="core"></param>
/// <param name="runtimeCore"></param>
/// <param name="runningBlock"></param>
/// <param name="staticContext"></param>
/// <param name="runtimeContext"></param>
/// <returns></returns>
public ProtoCore.RuntimeCore ExecuteLive(ProtoCore.Core core, ProtoCore.RuntimeCore runtimeCore)
{
try
{
Validity.Assert(runtimeCore.DSExecutable.CodeBlocks.Count == 1);
CodeBlock codeBlock = runtimeCore.DSExecutable.CodeBlocks[0];
// Comment Jun:
// On first bounce, the stackframe depth is initialized to -1 in the Stackfame constructor.
// Passing it to bounce() increments it so the first depth is always 0
var stackFrame = new ProtoCore.DSASM.StackFrame(core.GlobOffset);
stackFrame.FramePointer = runtimeCore.RuntimeMemory.FramePointer;
// Comment Jun: Tell the new bounce stackframe that this is an implicit bounce
// Register TX is used for this.
StackValue svCallConvention = StackValue.BuildCallingConversion((int)ProtoCore.DSASM.CallingConvention.BounceType.Implicit);
stackFrame.TX = svCallConvention;
// Initialize the entry point interpreter
int locals = 0; // This is the global scope, there are no locals
if (runtimeCore.CurrentExecutive.CurrentDSASMExec == null)
{
var interpreter = new ProtoCore.DSASM.Interpreter(runtimeCore);
runtimeCore.CurrentExecutive.CurrentDSASMExec = interpreter.runtime;
}
runtimeCore.CurrentExecutive.CurrentDSASMExec.BounceUsingExecutive(
runtimeCore.CurrentExecutive.CurrentDSASMExec,
codeBlock.codeBlockId,
runtimeCore.StartPC,
stackFrame,
locals);
runtimeCore.NotifyExecutionEvent(ProtoCore.ExecutionStateEventArgs.State.ExecutionEnd);
}
catch
{
runtimeCore.NotifyExecutionEvent(ProtoCore.ExecutionStateEventArgs.State.ExecutionEnd);
throw;
}
return runtimeCore;
}
/// <summary>
/// Compile and execute the source that is stored in the static context
/// </summary>
/// <param name="staticContext"></param>
/// <param name="runtimeContext"></param>
/// <param name="core"></param>
/// <param name="isTest"></param>
/// <returns></returns>
public ExecutionMirror Execute(
ProtoCore.CompileTime.Context staticContext,
ProtoCore.Core core,
out ProtoCore.RuntimeCore runtimeCoreOut,
bool isTest = true)
{
Validity.Assert(null != staticContext.SourceCode && String.Empty != staticContext.SourceCode);
ProtoCore.RuntimeCore runtimeCore = null;
core.AddContextData(staticContext.GlobalVarList);
string code = staticContext.SourceCode;
bool succeeded = CompileAndGenerateExe(code, core, staticContext);
if (succeeded)
{
runtimeCore = ExecuteVM(core);
if (!isTest)
{
runtimeCore.RuntimeMemory.Heap.Free();
}
}
else
{
throw new ProtoCore.Exceptions.CompileErrorsOccured();
}
runtimeCoreOut = runtimeCore;
if (isTest)
{
return new ExecutionMirror(runtimeCore.CurrentExecutive.CurrentDSASMExec, runtimeCore);
}
return null;
}
/// <summary>
/// Compile and execute the given list of ASTs
/// </summary>
/// <param name="astList"></param>
/// <param name="core"></param>
/// <param name="isTest"></param>
/// <returns></returns>
public ProtoCore.RuntimeCore Execute(List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList, ProtoCore.Core core, bool isTest = true)
{
ProtoCore.RuntimeCore runtimeCore = null;
bool succeeded = CompileAndGenerateExe(astList, core, new ProtoCore.CompileTime.Context());
if (succeeded)
{
runtimeCore = ExecuteVM(core);
if (!isTest)
{
runtimeCore.RuntimeMemory.Heap.Free();
}
}
else
{
throw new ProtoCore.Exceptions.CompileErrorsOccured();
}
if (isTest)
{
runtimeCore.Mirror = new ExecutionMirror(runtimeCore.CurrentExecutive.CurrentDSASMExec, runtimeCore);
}
return runtimeCore;
}
/// <summary>
/// Compile and execute the given sourcecode
/// </summary>
/// <param name="code"></param>
/// <param name="core"></param>
/// <param name="isTest"></param>
/// <returns></returns>
public ProtoCore.RuntimeCore Execute(string sourcecode, ProtoCore.Core core, bool isTest = true)
{
ProtoCore.RuntimeCore runtimeCore = null;
bool succeeded = CompileAndGenerateExe(sourcecode, core, new ProtoCore.CompileTime.Context());
if (succeeded)
{
try
{
runtimeCore = ExecuteVM(core);
}
catch (ProtoCore.Exceptions.ExecutionCancelledException)
{
Console.WriteLine("The execution has been cancelled!");
}
if (!isTest)
{
runtimeCore.RuntimeMemory.Heap.Free();
}
}
else
{
throw new ProtoCore.Exceptions.CompileErrorsOccured();
}
if (isTest)
{
runtimeCore.Mirror = new ExecutionMirror(runtimeCore.CurrentExecutive.CurrentDSASMExec, runtimeCore);
}
return runtimeCore;
}
/// <summary>
/// Load and executes the DS code in the specified file
/// </summary>
/// <param name="filename"></param>
/// <param name="core"></param>
/// <param name="isTest"></param>
/// <returns></returns>
public ProtoCore.RuntimeCore LoadAndExecute(string filename, ProtoCore.Core core, bool isTest = true)
{
System.IO.StreamReader reader = null;
try
{
reader = new System.IO.StreamReader(filename, Encoding.UTF8, true);
}
catch (System.IO.IOException)
{
throw new Exception("Cannot open file " + filename);
}
string strSource = reader.ReadToEnd();
reader.Dispose();
core.Options.RootModulePathName = ProtoCore.Utils.FileUtils.GetFullPathName(filename);
core.CurrentDSFileName = core.Options.RootModulePathName;
return Execute(strSource, core);
}
/// <summary>
/// The public method to compile DS code and stores the executable in core
/// </summary>
/// <param name="sourcecode"></param>
/// <param name="compileCore"></param>
/// <returns></returns>
public bool CompileAndGenerateExe(string sourcecode, ProtoCore.Core compileCore, ProtoCore.CompileTime.Context context)
{
bool succeeded = Compile(sourcecode, compileCore, context);
if (succeeded)
{
compileCore.GenerateExecutable();
}
return succeeded;
}
/// <summary>
/// The public method to compile DS AST and stores the executable in core
/// </summary>
/// <param name="astList"></param>
/// <param name="compileCore"></param>
/// <returns></returns>
public bool CompileAndGenerateExe(
List<ProtoCore.AST.AssociativeAST.AssociativeNode> astList,
ProtoCore.Core compileCore,
ProtoCore.CompileTime.Context context)
{
bool succeeded = Compile(astList, compileCore, context);
if (succeeded)
{
compileCore.GenerateExecutable();
}
return succeeded;
}
}
}