-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathYieldFinishedCompile.cs
More file actions
106 lines (94 loc) · 3.91 KB
/
YieldFinishedCompile.cs
File metadata and controls
106 lines (94 loc) · 3.91 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
using kOS.Safe.Compilation;
using kOS.Safe.Encapsulation;
using kOS.Safe.Persistence;
using kOS.Safe.Utilities;
using System.Collections.Generic;
namespace kOS.Safe.Execution
{
public class YieldFinishedCompile : YiedFinishedThreadedDetector
{
private enum CompileMode
{
RUN = 0,
LOAD = 1,
FILE = 2
}
private CompileMode compileMode;
private List<CodePart> codeParts;
private GlobalPath path;
private int startLineNum;
private string content;
private string contextId;
private CompilerOptions options;
private Volume volume;
private GlobalPath outPath;
private IProgramContext programContext;
private YieldFinishedCompile(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions)
{
compileMode = CompileMode.RUN;
path = scriptPath;
startLineNum = lineNumber;
content = fileContent;
contextId = contextIdentifier;
options = compilerOptions;
}
public override void ThreadInitialize(SafeSharedObjects shared)
{
programContext = shared.Cpu.SwitchToProgramContext();
codeParts = new List<CodePart>();
}
public override void ThreadExecute()
{
codeParts = shared.ScriptHandler.Compile(path, startLineNum, content, contextId, options);
}
public override void ThreadFinish()
{
switch (compileMode)
{
case CompileMode.RUN:
programContext.AddParts(codeParts);
shared.Cpu.StopCompileStopwatch();
break;
case CompileMode.LOAD:
int programAddress = programContext.AddObjectParts(codeParts, path.ToString());
// push the entry point address of the new program onto the stack
shared.Cpu.PushStack(programAddress);
shared.Cpu.PushStack(BooleanValue.False);
break;
case CompileMode.FILE:
VolumeFile written = volume.SaveFile(outPath, new FileContent(codeParts));
if (written == null)
{
throw new KOSFileException("Can't save compiled file: not enough space or access forbidden");
}
break;
default:
break;
}
}
protected override bool RunOnCaller()
{
return SafeHouse.Config.PauseOnCompile;
}
public static YieldFinishedCompile RunScript(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions)
{
var ret = new YieldFinishedCompile(scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions);
ret.compileMode = CompileMode.RUN;
return ret;
}
public static YieldFinishedCompile LoadScript(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions)
{
var ret = new YieldFinishedCompile(scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions);
ret.compileMode = CompileMode.LOAD;
return ret;
}
public static YieldFinishedCompile CompileScriptToFile(GlobalPath scriptPath, int lineNumber, string fileContent, CompilerOptions compilerOptions, Volume storageVolume, GlobalPath storagePath)
{
var ret = new YieldFinishedCompile(scriptPath, lineNumber, fileContent, string.Empty, compilerOptions);
ret.compileMode = CompileMode.FILE;
ret.volume = storageVolume;
ret.outPath = storagePath;
return ret;
}
}
}